2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
60 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
61 __isl_keep
const isl_basic_set
*bset
)
63 return (const isl_basic_map
*) bset
;
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
70 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
73 case isl_dim_param
: return 1;
74 case isl_dim_in
: return 1 + space
->nparam
;
75 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
80 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
81 enum isl_dim_type type
)
84 return isl_size_error
;
86 case isl_dim_cst
: return 1;
89 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
90 case isl_dim_div
: return bmap
->n_div
;
91 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
96 /* Return the space of "map".
98 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
100 return map
? map
->dim
: NULL
;
103 /* Return the space of "set".
105 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
107 return isl_map_peek_space(set_to_map(set
));
110 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
112 return isl_space_dim(isl_map_peek_space(map
), type
);
115 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
117 return isl_map_dim(set_to_map(set
), type
);
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
123 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
124 enum isl_dim_type type
)
128 space
= isl_basic_map_peek_space(bmap
);
130 return isl_size_error
;
135 case isl_dim_out
: return isl_space_offset(space
, type
);
136 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
139 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
140 "invalid dimension type", return isl_size_error
);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
147 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
148 enum isl_dim_type type
)
150 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
157 enum isl_dim_type type
)
160 case isl_dim_cst
: return 0;
164 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
170 enum isl_dim_type type
)
172 return isl_basic_map_offset(bset
, type
);
175 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
177 return pos(map
->dim
, type
);
180 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
181 enum isl_dim_type type
)
183 return isl_basic_map_dim(bset
, type
);
186 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
188 return isl_basic_set_dim(bset
, isl_dim_set
);
191 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
193 return isl_basic_set_dim(bset
, isl_dim_param
);
196 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
201 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
203 return isl_set_dim(set
, isl_dim_set
);
206 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
208 return isl_set_dim(set
, isl_dim_param
);
211 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
216 return isl_size_error
;
217 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
219 return isl_size_error
;
220 return dim
+ bmap
->n_div
;
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
226 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
229 return isl_size_error
;
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
236 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
238 return isl_basic_map_n_equality(bset_to_bmap(bset
));
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
244 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
247 return isl_size_error
;
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
254 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
256 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
259 /* Do "bmap1" and "bmap2" have the same parameters?
261 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
262 __isl_keep isl_basic_map
*bmap2
)
264 isl_space
*space1
, *space2
;
266 space1
= isl_basic_map_peek_space(bmap1
);
267 space2
= isl_basic_map_peek_space(bmap2
);
268 return isl_space_has_equal_params(space1
, space2
);
271 /* Do "map1" and "map2" have the same parameters?
273 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
274 __isl_keep isl_map
*map2
)
276 isl_space
*space1
, *space2
;
278 space1
= isl_map_peek_space(map1
);
279 space2
= isl_map_peek_space(map2
);
280 return isl_space_has_equal_params(space1
, space2
);
283 /* Do "map" and "set" have the same parameters?
285 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
286 __isl_keep isl_set
*set
)
288 return isl_map_has_equal_params(map
, set_to_map(set
));
291 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
293 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
294 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
296 isl_space
*bmap_space
, *bset_space
;
298 bmap_space
= isl_basic_map_peek_space(bmap
);
299 bset_space
= isl_basic_set_peek_space(bset
);
300 return isl_space_tuple_is_equal(bmap_space
, type
,
301 bset_space
, isl_dim_set
);
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
306 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
307 enum isl_dim_type type
, __isl_keep isl_set
*set
)
309 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
312 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
313 __isl_keep isl_set
*set
)
317 return isl_bool_error
;
318 m
= isl_map_has_equal_params(map
, set_to_map(set
));
321 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
324 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
325 __isl_keep isl_basic_set
*bset
)
329 return isl_bool_error
;
330 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
333 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
336 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
337 __isl_keep isl_set
*set
)
341 return isl_bool_error
;
342 m
= isl_map_has_equal_params(map
, set_to_map(set
));
345 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
348 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
349 __isl_keep isl_basic_set
*bset
)
353 return isl_bool_error
;
354 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
357 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
360 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
362 return bmap
? bmap
->ctx
: NULL
;
365 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
367 return bset
? bset
->ctx
: NULL
;
370 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
372 return map
? map
->ctx
: NULL
;
375 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
377 return set
? set
->ctx
: NULL
;
380 /* Return the space of "bmap".
382 __isl_keep isl_space
*isl_basic_map_peek_space(
383 __isl_keep
const isl_basic_map
*bmap
)
385 return bmap
? bmap
->dim
: NULL
;
388 /* Return the space of "bset".
390 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
392 return isl_basic_map_peek_space(bset_to_bmap(bset
));
395 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
397 return isl_space_copy(isl_basic_map_peek_space(bmap
));
400 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
402 return isl_basic_map_get_space(bset_to_bmap(bset
));
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
414 static __isl_give isl_space
*isl_basic_map_take_space(
415 __isl_keep isl_basic_map
*bmap
)
422 return isl_basic_map_get_space(bmap
);
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
433 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
434 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
439 if (bmap
->dim
== space
) {
440 isl_space_free(space
);
444 bmap
= isl_basic_map_cow(bmap
);
447 isl_space_free(bmap
->dim
);
452 isl_basic_map_free(bmap
);
453 isl_space_free(space
);
457 /* Extract the divs in "bmap" as a matrix.
459 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
467 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
471 ctx
= isl_basic_map_get_ctx(bmap
);
472 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
473 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
477 for (i
= 0; i
< bmap
->n_div
; ++i
)
478 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
483 /* Extract the divs in "bset" as a matrix.
485 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
487 return isl_basic_map_get_divs(bset
);
490 __isl_give isl_local_space
*isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map
*bmap
)
498 div
= isl_basic_map_get_divs(bmap
);
499 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
502 __isl_give isl_local_space
*isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set
*bset
)
505 return isl_basic_map_get_local_space(bset
);
508 /* For each known div d = floor(f/m), add the constraints
511 * -(f-(m-1)) + m d >= 0
513 * Do not finalize the result.
515 static __isl_give isl_basic_map
*add_known_div_constraints(
516 __isl_take isl_basic_map
*bmap
)
521 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
523 return isl_basic_map_free(bmap
);
526 bmap
= isl_basic_map_cow(bmap
);
527 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
530 for (i
= 0; i
< n_div
; ++i
) {
531 if (isl_int_is_zero(bmap
->div
[i
][0]))
533 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
539 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
540 __isl_take isl_local_space
*ls
)
546 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
548 ls
= isl_local_space_free(ls
);
552 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
553 n_div
, 0, 2 * n_div
);
555 for (i
= 0; i
< n_div
; ++i
)
556 if (isl_basic_map_alloc_div(bmap
) < 0)
559 for (i
= 0; i
< n_div
; ++i
)
560 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
561 bmap
= add_known_div_constraints(bmap
);
563 isl_local_space_free(ls
);
566 isl_local_space_free(ls
);
567 isl_basic_map_free(bmap
);
571 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
572 __isl_take isl_local_space
*ls
)
574 return isl_basic_map_from_local_space(ls
);
577 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
579 return isl_space_copy(isl_map_peek_space(map
));
582 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
586 return isl_space_copy(set
->dim
);
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
598 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
605 return isl_map_get_space(map
);
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
616 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
617 __isl_take isl_space
*space
)
622 if (map
->dim
== space
) {
623 isl_space_free(space
);
627 map
= isl_map_cow(map
);
630 isl_space_free(map
->dim
);
636 isl_space_free(space
);
640 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
645 space
= isl_basic_map_take_space(bmap
);
646 space
= isl_space_set_tuple_name(space
, type
, s
);
647 bmap
= isl_basic_map_restore_space(bmap
, space
);
648 bmap
= isl_basic_map_finalize(bmap
);
652 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set
*bset
, const char *s
)
655 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
659 enum isl_dim_type type
)
661 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
664 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
665 enum isl_dim_type type
, const char *s
)
670 map
= isl_map_cow(map
);
674 for (i
= 0; i
< map
->n
; ++i
) {
675 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
680 space
= isl_map_take_space(map
);
681 space
= isl_space_set_tuple_name(space
, type
, s
);
682 map
= isl_map_restore_space(map
, space
);
690 /* Replace the identifier of the tuple of type "type" by "id".
692 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map
*bmap
,
694 enum isl_dim_type type
, __isl_take isl_id
*id
)
698 space
= isl_basic_map_take_space(bmap
);
699 space
= isl_space_set_tuple_id(space
, type
, id
);
700 bmap
= isl_basic_map_restore_space(bmap
, space
);
701 bmap
= isl_basic_map_finalize(bmap
);
705 /* Replace the identifier of the tuple by "id".
707 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
710 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
713 /* Does the input or output tuple have a name?
715 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
717 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
720 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
721 enum isl_dim_type type
)
723 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
726 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
733 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
734 enum isl_dim_type type
, __isl_take isl_id
*id
)
738 space
= isl_map_take_space(map
);
739 space
= isl_space_set_tuple_id(space
, type
, id
);
740 map
= isl_map_restore_space(map
, space
);
742 return isl_map_reset_space(map
, isl_map_get_space(map
));
745 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
746 __isl_take isl_id
*id
)
748 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
751 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
752 enum isl_dim_type type
)
756 space
= isl_map_take_space(map
);
757 space
= isl_space_reset_tuple_id(space
, type
);
758 map
= isl_map_restore_space(map
, space
);
760 return isl_map_reset_space(map
, isl_map_get_space(map
));
763 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
765 return isl_map_reset_tuple_id(set
, isl_dim_set
);
768 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
770 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
773 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
774 enum isl_dim_type type
)
776 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
779 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
781 return isl_map_has_tuple_id(set
, isl_dim_set
);
784 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
786 return isl_map_get_tuple_id(set
, isl_dim_set
);
789 /* Does the set tuple have a name?
791 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
794 return isl_bool_error
;
795 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
799 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
801 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
804 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
806 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
809 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
810 enum isl_dim_type type
, unsigned pos
)
812 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
815 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
816 enum isl_dim_type type
, unsigned pos
)
818 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
821 /* Does the given dimension have a name?
823 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
824 enum isl_dim_type type
, unsigned pos
)
827 return isl_bool_error
;
828 return isl_space_has_dim_name(map
->dim
, type
, pos
);
831 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
832 enum isl_dim_type type
, unsigned pos
)
834 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
837 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
838 enum isl_dim_type type
, unsigned pos
)
840 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
843 /* Does the given dimension have a name?
845 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
846 enum isl_dim_type type
, unsigned pos
)
849 return isl_bool_error
;
850 return isl_space_has_dim_name(set
->dim
, type
, pos
);
853 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
854 __isl_take isl_basic_map
*bmap
,
855 enum isl_dim_type type
, unsigned pos
, const char *s
)
859 space
= isl_basic_map_take_space(bmap
);
860 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
861 bmap
= isl_basic_map_restore_space(bmap
, space
);
862 return isl_basic_map_finalize(bmap
);
865 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
866 enum isl_dim_type type
, unsigned pos
, const char *s
)
871 map
= isl_map_cow(map
);
875 for (i
= 0; i
< map
->n
; ++i
) {
876 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
881 space
= isl_map_take_space(map
);
882 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
883 map
= isl_map_restore_space(map
, space
);
891 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
892 __isl_take isl_basic_set
*bset
,
893 enum isl_dim_type type
, unsigned pos
, const char *s
)
895 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
899 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
900 enum isl_dim_type type
, unsigned pos
, const char *s
)
902 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
906 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
907 enum isl_dim_type type
, unsigned pos
)
910 return isl_bool_error
;
911 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
914 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
915 enum isl_dim_type type
, unsigned pos
)
917 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
920 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
921 enum isl_dim_type type
, unsigned pos
)
923 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
926 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
927 enum isl_dim_type type
, unsigned pos
)
929 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
932 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
933 enum isl_dim_type type
, unsigned pos
)
935 return isl_map_has_dim_id(set
, type
, pos
);
938 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
939 enum isl_dim_type type
, unsigned pos
)
941 return isl_map_get_dim_id(set
, type
, pos
);
944 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
945 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
949 space
= isl_map_take_space(map
);
950 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
951 map
= isl_map_restore_space(map
, space
);
953 return isl_map_reset_space(map
, isl_map_get_space(map
));
956 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
957 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
959 return isl_map_set_dim_id(set
, type
, pos
, id
);
962 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
963 __isl_keep isl_id
*id
)
967 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
970 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
971 __isl_keep isl_id
*id
)
973 return isl_map_find_dim_by_id(set
, type
, id
);
976 /* Return the position of the dimension of the given type and name
978 * Return -1 if no such dimension can be found.
980 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
981 enum isl_dim_type type
, const char *name
)
985 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
988 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
993 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
996 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
999 return isl_map_find_dim_by_name(set
, type
, name
);
1002 /* Check whether equality i of bset is a pure stride constraint
1003 * on a single dimension, i.e., of the form
1007 * with k a constant and e an existentially quantified variable.
1009 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1017 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1018 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1019 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1020 if (nparam
< 0 || d
< 0 || n_div
< 0)
1021 return isl_bool_error
;
1023 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1024 return isl_bool_false
;
1026 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1027 return isl_bool_false
;
1028 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1030 return isl_bool_false
;
1031 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1032 d
- pos1
- 1) != -1)
1033 return isl_bool_false
;
1035 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1037 return isl_bool_false
;
1038 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1039 n_div
- pos2
- 1) != -1)
1040 return isl_bool_false
;
1041 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1042 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1043 return isl_bool_false
;
1045 return isl_bool_true
;
1048 /* Reset the user pointer on all identifiers of parameters and tuples
1049 * of the space of "map".
1051 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1055 space
= isl_map_get_space(map
);
1056 space
= isl_space_reset_user(space
);
1057 map
= isl_map_reset_space(map
, space
);
1062 /* Reset the user pointer on all identifiers of parameters and tuples
1063 * of the space of "set".
1065 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1067 return isl_map_reset_user(set
);
1070 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1073 return isl_bool_error
;
1074 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1077 /* Has "map" been marked as a rational map?
1078 * In particular, have all basic maps in "map" been marked this way?
1079 * An empty map is not considered to be rational.
1080 * Maps where only some of the basic maps are marked rational
1083 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1089 return isl_bool_error
;
1091 return isl_bool_false
;
1092 rational
= isl_basic_map_is_rational(map
->p
[0]);
1095 for (i
= 1; i
< map
->n
; ++i
) {
1096 isl_bool rational_i
;
1098 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1101 if (rational
!= rational_i
)
1102 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1103 "mixed rational and integer basic maps "
1104 "not supported", return isl_bool_error
);
1110 /* Has "set" been marked as a rational set?
1111 * In particular, have all basic set in "set" been marked this way?
1112 * An empty set is not considered to be rational.
1113 * Sets where only some of the basic sets are marked rational
1116 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1118 return isl_map_is_rational(set
);
1121 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1123 return isl_basic_map_is_rational(bset
);
1126 /* Does "bmap" contain any rational points?
1128 * If "bmap" has an equality for each dimension, equating the dimension
1129 * to an integer constant, then it has no rational points, even if it
1130 * is marked as rational.
1132 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1134 isl_bool has_rational
= isl_bool_true
;
1138 return isl_bool_error
;
1139 if (isl_basic_map_plain_is_empty(bmap
))
1140 return isl_bool_false
;
1141 if (!isl_basic_map_is_rational(bmap
))
1142 return isl_bool_false
;
1143 bmap
= isl_basic_map_copy(bmap
);
1144 bmap
= isl_basic_map_implicit_equalities(bmap
);
1145 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1147 return isl_bool_error
;
1148 if (bmap
->n_eq
== total
) {
1150 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1151 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1154 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1155 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1157 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1162 if (i
== bmap
->n_eq
)
1163 has_rational
= isl_bool_false
;
1165 isl_basic_map_free(bmap
);
1167 return has_rational
;
1170 /* Does "map" contain any rational points?
1172 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1175 isl_bool has_rational
;
1178 return isl_bool_error
;
1179 for (i
= 0; i
< map
->n
; ++i
) {
1180 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1181 if (has_rational
< 0 || has_rational
)
1182 return has_rational
;
1184 return isl_bool_false
;
1187 /* Does "set" contain any rational points?
1189 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1191 return isl_map_has_rational(set
);
1194 /* Is this basic set a parameter domain?
1196 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1199 return isl_bool_error
;
1200 return isl_space_is_params(bset
->dim
);
1203 /* Is this set a parameter domain?
1205 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1208 return isl_bool_error
;
1209 return isl_space_is_params(set
->dim
);
1212 /* Is this map actually a parameter domain?
1213 * Users should never call this function. Outside of isl,
1214 * a map can never be a parameter domain.
1216 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1219 return isl_bool_error
;
1220 return isl_space_is_params(map
->dim
);
1223 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1224 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1225 unsigned n_eq
, unsigned n_ineq
)
1228 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1229 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1230 size_t row_size
= 1 + n_var
+ extra
;
1236 return isl_basic_map_free(bmap
);
1238 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1239 if (isl_blk_is_error(bmap
->block
))
1242 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1243 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1247 bmap
->block2
= isl_blk_empty();
1250 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1251 if (isl_blk_is_error(bmap
->block2
))
1254 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1259 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1260 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1262 for (i
= 0; i
< extra
; ++i
)
1263 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1267 bmap
->c_size
= n_eq
+ n_ineq
;
1268 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1269 bmap
->extra
= extra
;
1273 bmap
->sample
= NULL
;
1277 isl_basic_map_free(bmap
);
1281 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1282 unsigned nparam
, unsigned dim
, unsigned extra
,
1283 unsigned n_eq
, unsigned n_ineq
)
1285 struct isl_basic_map
*bmap
;
1288 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1292 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1293 return bset_from_bmap(bmap
);
1296 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1297 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1299 struct isl_basic_map
*bmap
;
1302 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1303 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1304 return bset_from_bmap(bmap
);
1306 isl_space_free(space
);
1310 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1311 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1313 struct isl_basic_map
*bmap
;
1317 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1322 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1324 isl_space_free(space
);
1328 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1329 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1330 unsigned n_eq
, unsigned n_ineq
)
1332 struct isl_basic_map
*bmap
;
1335 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1339 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1343 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1344 __isl_keep isl_basic_map
*src
)
1347 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1349 if (!dst
|| total
< 0)
1350 return isl_basic_map_free(dst
);
1352 for (i
= 0; i
< src
->n_eq
; ++i
) {
1353 int j
= isl_basic_map_alloc_equality(dst
);
1355 return isl_basic_map_free(dst
);
1356 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1359 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1360 int j
= isl_basic_map_alloc_inequality(dst
);
1362 return isl_basic_map_free(dst
);
1363 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1366 for (i
= 0; i
< src
->n_div
; ++i
) {
1367 int j
= isl_basic_map_alloc_div(dst
);
1369 return isl_basic_map_free(dst
);
1370 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1372 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1376 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1378 struct isl_basic_map
*dup
;
1382 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1383 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1384 dup
= dup_constraints(dup
, bmap
);
1387 dup
->flags
= bmap
->flags
;
1388 dup
->sample
= isl_vec_copy(bmap
->sample
);
1392 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1394 struct isl_basic_map
*dup
;
1396 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1397 return bset_from_bmap(dup
);
1400 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1405 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1409 return isl_basic_set_dup(bset
);
1412 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1421 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1426 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1430 bmap
= isl_basic_map_dup(bmap
);
1432 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1436 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1445 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1450 if (--bmap
->ref
> 0)
1453 isl_ctx_deref(bmap
->ctx
);
1455 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1457 isl_blk_free(bmap
->ctx
, bmap
->block
);
1458 isl_vec_free(bmap
->sample
);
1459 isl_space_free(bmap
->dim
);
1465 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1467 return isl_basic_map_free(bset_to_bmap(bset
));
1470 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1472 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1475 /* Check that "bset" does not involve any parameters.
1477 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1481 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1483 return isl_stat_error
;
1485 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1486 "basic set should not have any parameters",
1487 return isl_stat_error
);
1491 /* Check that "bset" does not involve any local variables.
1493 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1497 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1499 return isl_stat_error
;
1501 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1502 "basic set should not have any local variables",
1503 return isl_stat_error
);
1508 #define TYPE isl_map
1510 #include "isl_check_named_params_templ.c"
1513 #define TYPE isl_basic_map
1516 #include "isl_check_named_params_templ.c"
1518 /* Check that "bmap1" and "bmap2" have the same parameters,
1519 * reporting an error if they do not.
1521 static isl_stat
isl_basic_map_check_equal_params(
1522 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1526 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1528 return isl_stat_error
;
1530 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1531 "parameters don't match", return isl_stat_error
);
1536 #define TYPE isl_map
1538 #include "isl_align_params_bin_templ.c"
1543 #define ARG1 isl_map
1545 #define ARG2 isl_set
1547 #include "isl_align_params_templ.c"
1549 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1550 __isl_keep isl_map
*map2
,
1551 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1556 return isl_bool_error
;
1557 if (isl_map_has_equal_params(map1
, map2
))
1558 return fn(map1
, map2
);
1559 if (isl_map_check_named_params(map1
) < 0)
1560 return isl_bool_error
;
1561 if (isl_map_check_named_params(map2
) < 0)
1562 return isl_bool_error
;
1563 map1
= isl_map_copy(map1
);
1564 map2
= isl_map_copy(map2
);
1565 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1566 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1573 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1576 struct isl_ctx
*ctx
;
1578 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1582 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1583 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1585 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1586 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1587 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1588 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1589 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1591 int j
= isl_basic_map_alloc_inequality(bmap
);
1595 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1596 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1603 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1604 bmap
->extra
- bmap
->n_div
);
1605 return bmap
->n_eq
++;
1608 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1610 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1613 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1614 __isl_take isl_basic_map
*bmap
, unsigned n
)
1619 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1620 "invalid number of equalities",
1621 isl_basic_map_free(bmap
));
1626 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1627 __isl_take isl_basic_set
*bset
, unsigned n
)
1629 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1633 /* Drop the equality constraint at position "pos",
1634 * preserving the order of the other equality constraints.
1636 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1643 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1647 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1648 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1649 bmap
->eq
[bmap
->n_eq
] = t
;
1654 /* Turn inequality "pos" of "bmap" into an equality.
1656 * In particular, we move the inequality in front of the equalities
1657 * and move the last inequality in the position of the moved inequality.
1658 * Note that isl_tab_make_equalities_explicit depends on this particular
1659 * change in the ordering of the constraints.
1661 void isl_basic_map_inequality_to_equality(
1662 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1666 t
= bmap
->ineq
[pos
];
1667 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1668 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1673 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1674 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1675 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1676 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1679 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1681 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1684 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1687 struct isl_ctx
*ctx
;
1689 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1693 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1694 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1695 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1696 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1697 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1698 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1699 bmap
->extra
- bmap
->n_div
);
1700 return bmap
->n_ineq
++;
1703 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1705 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1708 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1709 __isl_take isl_basic_map
*bmap
, unsigned n
)
1713 if (n
> bmap
->n_ineq
)
1714 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1715 "invalid number of inequalities",
1716 return isl_basic_map_free(bmap
));
1721 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1722 __isl_take isl_basic_set
*bset
, unsigned n
)
1724 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1728 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1733 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1735 if (pos
!= bmap
->n_ineq
- 1) {
1736 t
= bmap
->ineq
[pos
];
1737 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1738 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1739 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1745 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1747 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1750 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1757 empty
= isl_basic_map_plain_is_empty(bmap
);
1759 return isl_basic_map_free(bmap
);
1763 bmap
= isl_basic_map_cow(bmap
);
1764 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1765 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1767 return isl_basic_map_free(bmap
);
1768 k
= isl_basic_map_alloc_equality(bmap
);
1771 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1774 isl_basic_map_free(bmap
);
1778 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1781 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1784 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1790 bmap
= isl_basic_map_cow(bmap
);
1791 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1792 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1794 return isl_basic_map_free(bmap
);
1795 k
= isl_basic_map_alloc_inequality(bmap
);
1798 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1801 isl_basic_map_free(bmap
);
1805 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1808 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1811 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1815 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1818 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1819 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1820 bmap
->extra
- bmap
->n_div
);
1821 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1822 return bmap
->n_div
++;
1825 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1827 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1831 #define TYPE isl_basic_map
1832 #include "check_type_range_templ.c"
1834 /* Check that there are "n" dimensions of type "type" starting at "first"
1837 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1838 enum isl_dim_type type
, unsigned first
, unsigned n
)
1840 return isl_basic_map_check_range(bset_to_bmap(bset
),
1844 /* Insert an extra integer division, prescribed by "div", to "bmap"
1845 * at (integer division) position "pos".
1847 * The integer division is first added at the end and then moved
1848 * into the right position.
1850 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1851 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1856 bmap
= isl_basic_map_cow(bmap
);
1857 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1858 if (total
< 0 || !div
)
1859 return isl_basic_map_free(bmap
);
1861 if (div
->size
!= 1 + 1 + total
)
1862 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1863 "unexpected size", return isl_basic_map_free(bmap
));
1864 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1865 return isl_basic_map_free(bmap
);
1867 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1868 k
= isl_basic_map_alloc_div(bmap
);
1870 return isl_basic_map_free(bmap
);
1871 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1872 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1874 for (i
= k
; i
> pos
; --i
)
1875 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1880 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1883 return isl_stat_error
;
1884 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1889 static __isl_give isl_basic_map
*add_constraints(
1890 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1891 unsigned i_pos
, unsigned o_pos
)
1893 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1894 unsigned o_in
, o_out
;
1897 struct isl_dim_map
*dim_map
;
1899 space
= isl_basic_map_peek_space(bmap2
);
1900 if (!bmap1
|| !space
)
1903 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1904 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1905 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1906 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1907 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1908 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1909 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1910 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1912 ctx
= isl_basic_map_get_ctx(bmap1
);
1913 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1914 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1915 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1916 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1917 isl_dim_map_div(dim_map
, bmap2
, total
);
1919 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1921 isl_basic_map_free(bmap1
);
1922 isl_basic_map_free(bmap2
);
1926 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1927 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1930 struct isl_basic_map
*ext
;
1937 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1939 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1940 room_for_ineq(base
, n_ineq
))
1943 extra
+= base
->extra
;
1945 n_ineq
+= base
->n_ineq
;
1947 space
= isl_basic_map_get_space(base
);
1948 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1953 ext
->sample
= isl_vec_copy(base
->sample
);
1954 flags
= base
->flags
;
1955 ext
= add_constraints(ext
, base
, 0, 0);
1958 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1964 isl_basic_map_free(base
);
1968 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
1969 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1971 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1972 extra
, n_eq
, n_ineq
));
1975 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
1976 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1978 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
1981 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
1982 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1984 isl_basic_map
*bmap
= bset_to_bmap(base
);
1985 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1986 return bset_from_bmap(bmap
);
1989 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1991 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1994 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1999 if (bmap
->ref
> 1) {
2001 bmap
= isl_basic_map_dup(bmap
);
2004 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2005 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2010 /* Clear all cached information in "map", either because it is about
2011 * to be modified or because it is being freed.
2012 * Always return the same pointer that is passed in.
2013 * This is needed for the use in isl_map_free.
2015 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2017 isl_basic_map_free(map
->cached_simple_hull
[0]);
2018 isl_basic_map_free(map
->cached_simple_hull
[1]);
2019 map
->cached_simple_hull
[0] = NULL
;
2020 map
->cached_simple_hull
[1] = NULL
;
2024 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2026 return isl_map_cow(set
);
2029 /* Return an isl_map that is equal to "map" and that has only
2030 * a single reference.
2032 * If the original input already has only one reference, then
2033 * simply return it, but clear all cached information, since
2034 * it may be rendered invalid by the operations that will be
2035 * performed on the result.
2037 * Otherwise, create a duplicate (without any cached information).
2039 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2045 return clear_caches(map
);
2047 return isl_map_dup(map
);
2050 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2051 unsigned a_len
, unsigned b_len
)
2053 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2054 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2055 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2058 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2059 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2064 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2067 if (n1
== 0 || n2
== 0)
2070 bmap
= isl_basic_map_cow(bmap
);
2074 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2075 if (isl_blk_is_error(blk
))
2078 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2080 bmap
->eq
[i
] + pos
, n1
, n2
);
2082 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2084 bmap
->ineq
[i
] + pos
, n1
, n2
);
2086 for (i
= 0; i
< bmap
->n_div
; ++i
)
2088 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2090 isl_blk_free(bmap
->ctx
, blk
);
2092 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2093 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2094 return isl_basic_map_finalize(bmap
);
2096 isl_basic_map_free(bmap
);
2100 /* The given basic map has turned out to be empty.
2101 * Explicitly mark it as such and change the representation
2102 * to a canonical representation of the empty basic map.
2103 * Since the basic map has conflicting constraints,
2104 * it must have at least one constraint, except perhaps
2105 * if it was already explicitly marked as being empty.
2106 * Do nothing in the latter case.
2108 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2109 __isl_take isl_basic_map
*bmap
)
2115 empty
= isl_basic_map_plain_is_empty(bmap
);
2117 return isl_basic_map_free(bmap
);
2120 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2122 return isl_basic_map_free(bmap
);
2123 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2124 return isl_basic_map_free(bmap
);
2125 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2128 if (bmap
->n_eq
> 0) {
2129 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2133 i
= isl_basic_map_alloc_equality(bmap
);
2137 isl_int_set_si(bmap
->eq
[i
][0], 1);
2138 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2139 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2140 isl_vec_free(bmap
->sample
);
2141 bmap
->sample
= NULL
;
2142 return isl_basic_map_finalize(bmap
);
2144 isl_basic_map_free(bmap
);
2148 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2149 __isl_take isl_basic_set
*bset
)
2151 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2154 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2155 __isl_take isl_basic_map
*bmap
)
2160 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2163 bmap
= isl_basic_map_cow(bmap
);
2167 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2169 return isl_basic_map_finalize(bmap
);
2172 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2173 __isl_take isl_basic_set
*bset
)
2175 return isl_basic_map_set_rational(bset
);
2178 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2179 __isl_take isl_basic_set
*bset
)
2184 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2187 bset
= isl_basic_set_cow(bset
);
2191 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2193 return isl_basic_set_finalize(bset
);
2196 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2200 map
= isl_map_cow(map
);
2203 for (i
= 0; i
< map
->n
; ++i
) {
2204 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2214 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2216 return isl_map_set_rational(set
);
2219 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2222 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2224 isl_int
*t
= bmap
->div
[a
];
2225 bmap
->div
[a
] = bmap
->div
[b
];
2229 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2230 * div definitions accordingly.
2232 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2238 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2240 return isl_basic_map_free(bmap
);
2242 swap_div(bmap
, a
, b
);
2244 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2245 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2247 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2248 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2250 for (i
= 0; i
< bmap
->n_div
; ++i
)
2251 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2257 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2259 isl_seq_cpy(c
, c
+ n
, rem
);
2260 isl_seq_clr(c
+ rem
, n
);
2263 /* Drop n dimensions starting at first.
2265 * In principle, this frees up some extra variables as the number
2266 * of columns remains constant, but we would have to extend
2267 * the div array too as the number of rows in this array is assumed
2268 * to be equal to extra.
2270 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2271 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2273 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2276 /* Move "n" divs starting at "first" to the end of the list of divs.
2278 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2279 unsigned first
, unsigned n
)
2284 if (first
+ n
== bmap
->n_div
)
2287 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2290 for (i
= 0; i
< n
; ++i
)
2291 div
[i
] = bmap
->div
[first
+ i
];
2292 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2293 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2294 for (i
= 0; i
< n
; ++i
)
2295 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2299 isl_basic_map_free(bmap
);
2304 #define TYPE isl_map
2306 #include "check_type_range_templ.c"
2308 /* Check that there are "n" dimensions of type "type" starting at "first"
2311 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2312 enum isl_dim_type type
, unsigned first
, unsigned n
)
2314 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2317 /* Drop "n" dimensions of type "type" starting at "first".
2318 * Perform the core computation, without cowing or
2319 * simplifying and finalizing the result.
2321 * In principle, this frees up some extra variables as the number
2322 * of columns remains constant, but we would have to extend
2323 * the div array too as the number of rows in this array is assumed
2324 * to be equal to extra.
2326 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2327 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2328 unsigned first
, unsigned n
)
2335 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2336 return isl_basic_map_free(bmap
);
2338 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2340 return isl_basic_map_free(bmap
);
2342 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2343 left
= total
- (offset
- 1) - n
;
2344 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2345 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2347 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2348 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2350 for (i
= 0; i
< bmap
->n_div
; ++i
)
2351 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2353 if (type
== isl_dim_div
) {
2354 bmap
= move_divs_last(bmap
, first
, n
);
2357 if (isl_basic_map_free_div(bmap
, n
) < 0)
2358 return isl_basic_map_free(bmap
);
2360 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2362 return isl_basic_map_free(bmap
);
2364 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2365 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2369 /* Drop "n" dimensions of type "type" starting at "first".
2371 * In principle, this frees up some extra variables as the number
2372 * of columns remains constant, but we would have to extend
2373 * the div array too as the number of rows in this array is assumed
2374 * to be equal to extra.
2376 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2377 enum isl_dim_type type
, unsigned first
, unsigned n
)
2381 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2384 bmap
= isl_basic_map_cow(bmap
);
2388 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2390 bmap
= isl_basic_map_simplify(bmap
);
2391 return isl_basic_map_finalize(bmap
);
2394 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2401 /* No longer consider "map" to be normalized.
2403 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2407 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2411 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2412 enum isl_dim_type type
, unsigned first
, unsigned n
)
2417 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2418 return isl_map_free(map
);
2420 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2422 map
= isl_map_cow(map
);
2426 for (i
= 0; i
< map
->n
; ++i
) {
2427 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2431 map
= isl_map_unmark_normalized(map
);
2433 space
= isl_map_take_space(map
);
2434 space
= isl_space_drop_dims(space
, type
, first
, n
);
2435 map
= isl_map_restore_space(map
, space
);
2443 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2444 enum isl_dim_type type
, unsigned first
, unsigned n
)
2446 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2449 /* Drop the integer division at position "div", which is assumed
2450 * not to appear in any of the constraints or
2451 * in any of the other integer divisions.
2453 * Since the integer division is redundant, there is no need to cow.
2455 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2456 __isl_take isl_basic_map
*bmap
, unsigned div
)
2458 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2461 /* Eliminate the specified n dimensions starting at first from the
2462 * constraints, without removing the dimensions from the space.
2463 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2465 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2466 enum isl_dim_type type
, unsigned first
, unsigned n
)
2473 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2474 return isl_map_free(map
);
2476 map
= isl_map_cow(map
);
2480 for (i
= 0; i
< map
->n
; ++i
) {
2481 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2491 /* Eliminate the specified n dimensions starting at first from the
2492 * constraints, without removing the dimensions from the space.
2493 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2495 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2496 enum isl_dim_type type
, unsigned first
, unsigned n
)
2498 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2501 /* Eliminate the specified n dimensions starting at first from the
2502 * constraints, without removing the dimensions from the space.
2503 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2505 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2506 unsigned first
, unsigned n
)
2508 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2511 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2512 __isl_take isl_basic_map
*bmap
)
2516 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2518 return isl_basic_map_free(bmap
);
2519 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2523 return isl_basic_map_finalize(bmap
);
2526 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2527 __isl_take isl_basic_set
*bset
)
2529 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2532 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2541 map
= isl_map_cow(map
);
2545 for (i
= 0; i
< map
->n
; ++i
) {
2546 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2556 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2558 return isl_map_remove_divs(set
);
2561 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2562 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2563 unsigned first
, unsigned n
)
2565 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2566 return isl_basic_map_free(bmap
);
2567 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2569 bmap
= isl_basic_map_eliminate_vars(bmap
,
2570 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2573 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2575 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2579 /* Return true if the definition of the given div (recursively) involves
2580 * any of the given variables.
2582 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2583 unsigned first
, unsigned n
)
2586 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2588 if (isl_int_is_zero(bmap
->div
[div
][0]))
2589 return isl_bool_false
;
2590 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2591 return isl_bool_true
;
2593 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2596 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2598 involves
= div_involves_vars(bmap
, i
, first
, n
);
2599 if (involves
< 0 || involves
)
2603 return isl_bool_false
;
2606 /* Try and add a lower and/or upper bound on "div" to "bmap"
2607 * based on inequality "i".
2608 * "total" is the total number of variables (excluding the divs).
2609 * "v" is a temporary object that can be used during the calculations.
2610 * If "lb" is set, then a lower bound should be constructed.
2611 * If "ub" is set, then an upper bound should be constructed.
2613 * The calling function has already checked that the inequality does not
2614 * reference "div", but we still need to check that the inequality is
2615 * of the right form. We'll consider the case where we want to construct
2616 * a lower bound. The construction of upper bounds is similar.
2618 * Let "div" be of the form
2620 * q = floor((a + f(x))/d)
2622 * We essentially check if constraint "i" is of the form
2626 * so that we can use it to derive a lower bound on "div".
2627 * However, we allow a slightly more general form
2631 * with the condition that the coefficients of g(x) - f(x) are all
2633 * Rewriting this constraint as
2637 * adding a + f(x) to both sides and dividing by d, we obtain
2639 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2641 * Taking the floor on both sides, we obtain
2643 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2647 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2649 * In the case of an upper bound, we construct the constraint
2651 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2654 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2655 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2656 unsigned total
, isl_int v
, int lb
, int ub
)
2660 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2662 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2663 bmap
->div
[div
][1 + 1 + j
]);
2664 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2667 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2668 bmap
->div
[div
][1 + 1 + j
]);
2669 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2675 bmap
= isl_basic_map_cow(bmap
);
2676 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2678 int k
= isl_basic_map_alloc_inequality(bmap
);
2681 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2682 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2683 bmap
->div
[div
][1 + j
]);
2684 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2685 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2687 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2690 int k
= isl_basic_map_alloc_inequality(bmap
);
2693 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2694 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2695 bmap
->div
[div
][1 + j
]);
2696 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2697 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2699 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2704 isl_basic_map_free(bmap
);
2708 /* This function is called right before "div" is eliminated from "bmap"
2709 * using Fourier-Motzkin.
2710 * Look through the constraints of "bmap" for constraints on the argument
2711 * of the integer division and use them to construct constraints on the
2712 * integer division itself. These constraints can then be combined
2713 * during the Fourier-Motzkin elimination.
2714 * Note that it is only useful to introduce lower bounds on "div"
2715 * if "bmap" already contains upper bounds on "div" as the newly
2716 * introduce lower bounds can then be combined with the pre-existing
2717 * upper bounds. Similarly for upper bounds.
2718 * We therefore first check if "bmap" contains any lower and/or upper bounds
2721 * It is interesting to note that the introduction of these constraints
2722 * can indeed lead to more accurate results, even when compared to
2723 * deriving constraints on the argument of "div" from constraints on "div".
2724 * Consider, for example, the set
2726 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2728 * The second constraint can be rewritten as
2730 * 2 * [(-i-2j+3)/4] + k >= 0
2732 * from which we can derive
2734 * -i - 2j + 3 >= -2k
2740 * Combined with the first constraint, we obtain
2742 * -3 <= 3 + 2k or k >= -3
2744 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2745 * the first constraint, we obtain
2747 * [(i + 2j)/4] >= [-3/4] = -1
2749 * Combining this constraint with the second constraint, we obtain
2753 static __isl_give isl_basic_map
*insert_bounds_on_div(
2754 __isl_take isl_basic_map
*bmap
, int div
)
2757 int check_lb
, check_ub
;
2764 if (isl_int_is_zero(bmap
->div
[div
][0]))
2767 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2769 return isl_basic_map_free(bmap
);
2773 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2774 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2781 if (!check_lb
&& !check_ub
)
2786 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2787 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2790 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2791 check_lb
, check_ub
);
2799 /* Remove all divs (recursively) involving any of the given dimensions
2800 * in their definitions.
2802 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2803 __isl_take isl_basic_map
*bmap
,
2804 enum isl_dim_type type
, unsigned first
, unsigned n
)
2808 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2809 return isl_basic_map_free(bmap
);
2810 first
+= isl_basic_map_offset(bmap
, type
);
2812 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2815 involves
= div_involves_vars(bmap
, i
, first
, n
);
2817 return isl_basic_map_free(bmap
);
2820 bmap
= insert_bounds_on_div(bmap
, i
);
2821 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2830 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2831 __isl_take isl_basic_set
*bset
,
2832 enum isl_dim_type type
, unsigned first
, unsigned n
)
2834 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2837 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2838 enum isl_dim_type type
, unsigned first
, unsigned n
)
2847 map
= isl_map_cow(map
);
2851 for (i
= 0; i
< map
->n
; ++i
) {
2852 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2863 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2864 enum isl_dim_type type
, unsigned first
, unsigned n
)
2866 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2870 /* Does the description of "bmap" depend on the specified dimensions?
2871 * We also check whether the dimensions appear in any of the div definitions.
2872 * In principle there is no need for this check. If the dimensions appear
2873 * in a div definition, they also appear in the defining constraints of that
2876 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2877 enum isl_dim_type type
, unsigned first
, unsigned n
)
2881 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2882 return isl_bool_error
;
2884 first
+= isl_basic_map_offset(bmap
, type
);
2885 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2886 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2887 return isl_bool_true
;
2888 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2889 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2890 return isl_bool_true
;
2891 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2892 if (isl_int_is_zero(bmap
->div
[i
][0]))
2894 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2895 return isl_bool_true
;
2898 return isl_bool_false
;
2901 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2902 enum isl_dim_type type
, unsigned first
, unsigned n
)
2906 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2907 return isl_bool_error
;
2909 for (i
= 0; i
< map
->n
; ++i
) {
2910 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2912 if (involves
< 0 || involves
)
2916 return isl_bool_false
;
2919 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2920 enum isl_dim_type type
, unsigned first
, unsigned n
)
2922 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2925 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2926 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 return isl_map_involves_dims(set
, type
, first
, n
);
2931 /* Does "bset" involve any local variables, i.e., integer divisions?
2933 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
2937 n
= isl_basic_set_dim(bset
, isl_dim_div
);
2939 return isl_bool_error
;
2940 return isl_bool_ok(n
> 0);
2943 /* isl_set_every_basic_set callback that checks whether "bset"
2944 * is free of local variables.
2946 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
2948 return isl_bool_not(isl_basic_set_involves_locals(bset
));
2951 /* Does "set" involve any local variables, i.e., integer divisions?
2953 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
2957 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
2958 return isl_bool_not(no_locals
);
2961 /* Drop all constraints in bmap that involve any of the dimensions
2962 * first to first+n-1.
2963 * This function only performs the actual removal of constraints.
2965 * This function should not call finalize since it is used by
2966 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2968 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2969 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2976 bmap
= isl_basic_map_cow(bmap
);
2981 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2982 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2984 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2985 return isl_basic_map_free(bmap
);
2988 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2989 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2991 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2992 return isl_basic_map_free(bmap
);
2998 /* Drop all constraints in bset that involve any of the dimensions
2999 * first to first+n-1.
3000 * This function only performs the actual removal of constraints.
3002 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3003 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3005 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3008 /* Drop all constraints in bmap that do not involve any of the dimensions
3009 * first to first + n - 1 of the given type.
3011 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3012 __isl_take isl_basic_map
*bmap
,
3013 enum isl_dim_type type
, unsigned first
, unsigned n
)
3018 isl_space
*space
= isl_basic_map_get_space(bmap
);
3019 isl_basic_map_free(bmap
);
3020 return isl_basic_map_universe(space
);
3022 bmap
= isl_basic_map_cow(bmap
);
3026 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3027 return isl_basic_map_free(bmap
);
3029 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3031 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3032 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3034 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3035 return isl_basic_map_free(bmap
);
3038 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3039 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3041 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3042 return isl_basic_map_free(bmap
);
3045 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3049 /* Drop all constraints in bset that do not involve any of the dimensions
3050 * first to first + n - 1 of the given type.
3052 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3053 __isl_take isl_basic_set
*bset
,
3054 enum isl_dim_type type
, unsigned first
, unsigned n
)
3056 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3060 /* Drop all constraints in bmap that involve any of the dimensions
3061 * first to first + n - 1 of the given type.
3063 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3064 __isl_take isl_basic_map
*bmap
,
3065 enum isl_dim_type type
, unsigned first
, unsigned n
)
3072 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3073 return isl_basic_map_free(bmap
);
3075 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3076 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3077 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3078 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3082 /* Drop all constraints in bset that involve any of the dimensions
3083 * first to first + n - 1 of the given type.
3085 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3086 __isl_take isl_basic_set
*bset
,
3087 enum isl_dim_type type
, unsigned first
, unsigned n
)
3089 return isl_basic_map_drop_constraints_involving_dims(bset
,
3093 /* Drop constraints from "map" by applying "drop" to each basic map.
3095 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3096 enum isl_dim_type type
, unsigned first
, unsigned n
,
3097 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3098 enum isl_dim_type type
, unsigned first
, unsigned n
))
3102 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3103 return isl_map_free(map
);
3105 map
= isl_map_cow(map
);
3109 for (i
= 0; i
< map
->n
; ++i
) {
3110 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3112 return isl_map_free(map
);
3116 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3121 /* Drop all constraints in map that involve any of the dimensions
3122 * first to first + n - 1 of the given type.
3124 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3125 __isl_take isl_map
*map
,
3126 enum isl_dim_type type
, unsigned first
, unsigned n
)
3130 return drop_constraints(map
, type
, first
, n
,
3131 &isl_basic_map_drop_constraints_involving_dims
);
3134 /* Drop all constraints in "map" that do not involve any of the dimensions
3135 * first to first + n - 1 of the given type.
3137 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3138 __isl_take isl_map
*map
,
3139 enum isl_dim_type type
, unsigned first
, unsigned n
)
3142 isl_space
*space
= isl_map_get_space(map
);
3144 return isl_map_universe(space
);
3146 return drop_constraints(map
, type
, first
, n
,
3147 &isl_basic_map_drop_constraints_not_involving_dims
);
3150 /* Drop all constraints in set that involve any of the dimensions
3151 * first to first + n - 1 of the given type.
3153 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3154 __isl_take isl_set
*set
,
3155 enum isl_dim_type type
, unsigned first
, unsigned n
)
3157 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3160 /* Drop all constraints in "set" that do not involve any of the dimensions
3161 * first to first + n - 1 of the given type.
3163 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3164 __isl_take isl_set
*set
,
3165 enum isl_dim_type type
, unsigned first
, unsigned n
)
3167 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3170 /* Does local variable "div" of "bmap" have a complete explicit representation?
3171 * Having a complete explicit representation requires not only
3172 * an explicit representation, but also that all local variables
3173 * that appear in this explicit representation in turn have
3174 * a complete explicit representation.
3176 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3179 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3182 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3183 if (marked
< 0 || marked
)
3184 return isl_bool_not(marked
);
3186 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3189 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3191 known
= isl_basic_map_div_is_known(bmap
, i
);
3192 if (known
< 0 || !known
)
3196 return isl_bool_true
;
3199 /* Remove all divs that are unknown or defined in terms of unknown divs.
3201 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3202 __isl_take isl_basic_map
*bmap
)
3209 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3210 if (isl_basic_map_div_is_known(bmap
, i
))
3212 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3221 /* Remove all divs that are unknown or defined in terms of unknown divs.
3223 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3224 __isl_take isl_basic_set
*bset
)
3226 return isl_basic_map_remove_unknown_divs(bset
);
3229 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3238 map
= isl_map_cow(map
);
3242 for (i
= 0; i
< map
->n
; ++i
) {
3243 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3253 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3255 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3258 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3259 __isl_take isl_basic_set
*bset
,
3260 enum isl_dim_type type
, unsigned first
, unsigned n
)
3262 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3263 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3264 return bset_from_bmap(bmap
);
3267 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3268 enum isl_dim_type type
, unsigned first
, unsigned n
)
3275 map
= isl_map_cow(map
);
3276 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3277 return isl_map_free(map
);
3279 for (i
= 0; i
< map
->n
; ++i
) {
3280 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3281 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3285 map
= isl_map_drop(map
, type
, first
, n
);
3292 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3293 enum isl_dim_type type
, unsigned first
, unsigned n
)
3295 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3299 /* Project out n inputs starting at first using Fourier-Motzkin */
3300 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3301 unsigned first
, unsigned n
)
3303 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3306 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3307 FILE *out
, int indent
)
3312 fprintf(out
, "null basic set\n");
3316 fprintf(out
, "%*s", indent
, "");
3317 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3318 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3319 bset
->extra
, bset
->flags
);
3321 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3322 p
= isl_printer_set_dump(p
, 1);
3323 p
= isl_printer_set_indent(p
, indent
);
3324 p
= isl_printer_start_line(p
);
3325 p
= isl_printer_print_basic_set(p
, bset
);
3326 p
= isl_printer_end_line(p
);
3327 isl_printer_free(p
);
3330 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3331 FILE *out
, int indent
)
3336 fprintf(out
, "null basic map\n");
3340 fprintf(out
, "%*s", indent
, "");
3341 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3342 "flags: %x, n_name: %d\n",
3344 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3345 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3347 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3348 p
= isl_printer_set_dump(p
, 1);
3349 p
= isl_printer_set_indent(p
, indent
);
3350 p
= isl_printer_start_line(p
);
3351 p
= isl_printer_print_basic_map(p
, bmap
);
3352 p
= isl_printer_end_line(p
);
3353 isl_printer_free(p
);
3356 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3361 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3363 return isl_basic_map_free(bmap
);
3364 if (pos
>= bmap
->n_ineq
)
3365 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3366 "invalid position", return isl_basic_map_free(bmap
));
3367 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3368 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3369 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3374 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3377 if (isl_space_check_is_set(space
) < 0)
3379 return isl_map_alloc_space(space
, n
, flags
);
3381 isl_space_free(space
);
3385 /* Make sure "map" has room for at least "n" more basic maps.
3387 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3390 struct isl_map
*grown
= NULL
;
3394 isl_assert(map
->ctx
, n
>= 0, goto error
);
3395 if (map
->n
+ n
<= map
->size
)
3397 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3400 for (i
= 0; i
< map
->n
; ++i
) {
3401 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3409 isl_map_free(grown
);
3414 /* Make sure "set" has room for at least "n" more basic sets.
3416 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3418 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3421 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3423 return isl_map_from_basic_map(bset
);
3426 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3428 struct isl_map
*map
;
3433 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3434 return isl_map_add_basic_map(map
, bmap
);
3437 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3438 __isl_take isl_basic_set
*bset
)
3440 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3441 bset_to_bmap(bset
)));
3444 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3446 return isl_map_free(set
);
3449 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3454 fprintf(out
, "null set\n");
3458 fprintf(out
, "%*s", indent
, "");
3459 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3460 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3462 for (i
= 0; i
< set
->n
; ++i
) {
3463 fprintf(out
, "%*s", indent
, "");
3464 fprintf(out
, "basic set %d:\n", i
);
3465 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3469 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3474 fprintf(out
, "null map\n");
3478 fprintf(out
, "%*s", indent
, "");
3479 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3480 "flags: %x, n_name: %d\n",
3481 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3482 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3483 for (i
= 0; i
< map
->n
; ++i
) {
3484 fprintf(out
, "%*s", indent
, "");
3485 fprintf(out
, "basic map %d:\n", i
);
3486 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3490 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3492 static isl_stat
isl_basic_map_check_compatible_domain(
3493 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3497 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3499 return isl_stat_error
;
3501 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3502 "incompatible spaces", return isl_stat_error
);
3507 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3508 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3510 struct isl_basic_map
*bmap_domain
;
3513 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3516 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3520 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3523 bmap
= isl_basic_map_cow(bmap
);
3526 bmap
= isl_basic_map_extend(bmap
,
3527 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3528 bmap_domain
= isl_basic_map_from_domain(bset
);
3529 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3531 bmap
= isl_basic_map_simplify(bmap
);
3532 return isl_basic_map_finalize(bmap
);
3534 isl_basic_map_free(bmap
);
3535 isl_basic_set_free(bset
);
3539 /* Check that the space of "bset" is the same as that of the range of "bmap".
3541 static isl_stat
isl_basic_map_check_compatible_range(
3542 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3546 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3548 return isl_stat_error
;
3550 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3551 "incompatible spaces", return isl_stat_error
);
3556 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3557 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3559 struct isl_basic_map
*bmap_range
;
3562 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3565 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3568 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3571 if (isl_basic_set_plain_is_universe(bset
)) {
3572 isl_basic_set_free(bset
);
3576 bmap
= isl_basic_map_cow(bmap
);
3579 bmap
= isl_basic_map_extend(bmap
,
3580 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3581 bmap_range
= bset_to_bmap(bset
);
3582 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3584 bmap
= isl_basic_map_simplify(bmap
);
3585 return isl_basic_map_finalize(bmap
);
3587 isl_basic_map_free(bmap
);
3588 isl_basic_set_free(bset
);
3592 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3593 __isl_keep isl_vec
*vec
)
3599 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3600 if (total
< 0 || !vec
)
3601 return isl_bool_error
;
3603 if (1 + total
!= vec
->size
)
3604 return isl_bool_false
;
3608 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3609 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3610 if (!isl_int_is_zero(s
)) {
3612 return isl_bool_false
;
3616 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3617 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3618 if (isl_int_is_neg(s
)) {
3620 return isl_bool_false
;
3626 return isl_bool_true
;
3629 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3630 __isl_keep isl_vec
*vec
)
3632 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3635 __isl_give isl_basic_map
*isl_basic_map_intersect(
3636 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3638 struct isl_vec
*sample
= NULL
;
3639 isl_space
*space1
, *space2
;
3640 isl_size dim1
, dim2
, nparam1
, nparam2
;
3642 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3644 space1
= isl_basic_map_peek_space(bmap1
);
3645 space2
= isl_basic_map_peek_space(bmap2
);
3646 dim1
= isl_space_dim(space1
, isl_dim_all
);
3647 dim2
= isl_space_dim(space2
, isl_dim_all
);
3648 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3649 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3650 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3652 if (dim1
== nparam1
&& dim2
!= nparam2
)
3653 return isl_basic_map_intersect(bmap2
, bmap1
);
3655 if (dim2
!= nparam2
&&
3656 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3659 if (isl_basic_map_plain_is_empty(bmap1
)) {
3660 isl_basic_map_free(bmap2
);
3663 if (isl_basic_map_plain_is_empty(bmap2
)) {
3664 isl_basic_map_free(bmap1
);
3668 if (bmap1
->sample
&&
3669 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3670 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3671 sample
= isl_vec_copy(bmap1
->sample
);
3672 else if (bmap2
->sample
&&
3673 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3674 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3675 sample
= isl_vec_copy(bmap2
->sample
);
3677 bmap1
= isl_basic_map_cow(bmap1
);
3680 bmap1
= isl_basic_map_extend(bmap1
,
3681 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3682 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3685 isl_vec_free(sample
);
3687 isl_vec_free(bmap1
->sample
);
3688 bmap1
->sample
= sample
;
3691 bmap1
= isl_basic_map_simplify(bmap1
);
3692 return isl_basic_map_finalize(bmap1
);
3695 isl_vec_free(sample
);
3696 isl_basic_map_free(bmap1
);
3697 isl_basic_map_free(bmap2
);
3701 __isl_give isl_basic_set
*isl_basic_set_intersect(
3702 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3704 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3705 bset_to_bmap(bset2
)));
3708 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3709 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3711 return isl_basic_set_intersect(bset1
, bset2
);
3714 /* Special case of isl_map_intersect, where both map1 and map2
3715 * are convex, without any divs and such that either map1 or map2
3716 * contains a single constraint. This constraint is then simply
3717 * added to the other map.
3719 static __isl_give isl_map
*map_intersect_add_constraint(
3720 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3722 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3723 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3724 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3725 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3727 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3728 return isl_map_intersect(map2
, map1
);
3730 map1
= isl_map_cow(map1
);
3733 if (isl_map_plain_is_empty(map1
)) {
3737 if (map2
->p
[0]->n_eq
== 1)
3738 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3740 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3741 map2
->p
[0]->ineq
[0]);
3743 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3744 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3748 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3749 isl_basic_map_free(map1
->p
[0]);
3755 map1
= isl_map_unmark_normalized(map1
);
3763 /* map2 may be either a parameter domain or a map living in the same
3766 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3767 __isl_take isl_map
*map2
)
3773 isl_size dim2
, nparam2
;
3778 if ((isl_map_plain_is_empty(map1
) ||
3779 isl_map_plain_is_universe(map2
)) &&
3780 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3784 if ((isl_map_plain_is_empty(map2
) ||
3785 isl_map_plain_is_universe(map1
)) &&
3786 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3791 if (map1
->n
== 1 && map2
->n
== 1 &&
3792 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3793 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3794 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3795 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3796 return map_intersect_add_constraint(map1
, map2
);
3798 equal
= isl_map_plain_is_equal(map1
, map2
);
3806 dim2
= isl_map_dim(map2
, isl_dim_all
);
3807 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3808 if (dim2
< 0 || nparam2
< 0)
3810 if (dim2
!= nparam2
)
3811 isl_assert(map1
->ctx
,
3812 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3814 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3815 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3816 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3818 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3819 map1
->n
* map2
->n
, flags
);
3822 for (i
= 0; i
< map1
->n
; ++i
)
3823 for (j
= 0; j
< map2
->n
; ++j
) {
3824 struct isl_basic_map
*part
;
3825 part
= isl_basic_map_intersect(
3826 isl_basic_map_copy(map1
->p
[i
]),
3827 isl_basic_map_copy(map2
->p
[j
]));
3828 if (isl_basic_map_is_empty(part
) < 0)
3829 part
= isl_basic_map_free(part
);
3830 result
= isl_map_add_basic_map(result
, part
);
3843 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3844 __isl_take isl_map
*map2
)
3846 if (isl_map_check_equal_space(map1
, map2
) < 0)
3848 return map_intersect_internal(map1
, map2
);
3855 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3856 __isl_take isl_map
*map2
)
3858 isl_map_align_params_bin(&map1
, &map2
);
3859 return map_intersect(map1
, map2
);
3862 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3863 __isl_take isl_set
*set2
)
3865 return set_from_map(isl_map_intersect(set_to_map(set1
),
3869 /* map_intersect_internal accepts intersections
3870 * with parameter domains, so we can just call that function.
3872 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3873 __isl_take isl_set
*params
)
3875 isl_map_align_params_set(&map
, ¶ms
);
3876 return map_intersect_internal(map
, params
);
3879 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3880 __isl_take isl_set
*params
)
3882 return isl_map_intersect_params(set
, params
);
3885 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3893 bmap
= isl_basic_map_cow(bmap
);
3896 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3897 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3898 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3899 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3900 if (n1
< 0 || n2
< 0)
3901 bmap
= isl_basic_map_free(bmap
);
3902 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3903 return isl_basic_map_reset_space(bmap
, space
);
3906 /* Given a basic map A -> (B -> C), return the corresponding basic map
3909 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
3910 __isl_take isl_basic_map
*bmap
)
3913 isl_size offset
, n1
, n2
;
3915 space
= isl_basic_map_peek_space(bmap
);
3916 if (isl_space_check_range_is_wrapping(space
) < 0)
3917 return isl_basic_map_free(bmap
);
3918 offset
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
3919 n1
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_in
);
3920 n2
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_out
);
3921 if (offset
< 0 || n1
< 0 || n2
< 0)
3922 return isl_basic_map_free(bmap
);
3924 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
3926 space
= isl_basic_map_take_space(bmap
);
3927 space
= isl_space_range_reverse(space
);
3928 bmap
= isl_basic_map_restore_space(bmap
, space
);
3933 static __isl_give isl_basic_map
*basic_map_space_reset(
3934 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3940 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3943 space
= isl_basic_map_get_space(bmap
);
3944 space
= isl_space_reset(space
, type
);
3945 bmap
= isl_basic_map_reset_space(bmap
, space
);
3949 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3950 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3951 unsigned pos
, unsigned n
)
3953 isl_bool rational
, is_empty
;
3954 isl_space
*res_space
;
3955 struct isl_basic_map
*res
;
3956 struct isl_dim_map
*dim_map
;
3959 enum isl_dim_type t
;
3962 return basic_map_space_reset(bmap
, type
);
3964 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3965 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3966 if (is_empty
< 0 || total
< 0)
3967 return isl_basic_map_free(bmap
);
3968 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3971 return isl_basic_map_free(bmap
);
3973 isl_basic_map_free(bmap
);
3974 return isl_basic_map_empty(res_space
);
3977 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
3979 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3983 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3985 isl_size size
= isl_basic_map_dim(bmap
, t
);
3987 dim_map
= isl_dim_map_free(dim_map
);
3988 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3990 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3991 pos
, size
- pos
, off
+ pos
+ n
);
3993 dim
= isl_space_dim(res_space
, t
);
3995 dim_map
= isl_dim_map_free(dim_map
);
3998 isl_dim_map_div(dim_map
, bmap
, off
);
4000 res
= isl_basic_map_alloc_space(res_space
,
4001 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4002 rational
= isl_basic_map_is_rational(bmap
);
4004 res
= isl_basic_map_free(res
);
4006 res
= isl_basic_map_set_rational(res
);
4007 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4008 return isl_basic_map_finalize(res
);
4011 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4012 __isl_take isl_basic_set
*bset
,
4013 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4015 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4018 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4019 enum isl_dim_type type
, unsigned n
)
4023 dim
= isl_basic_map_dim(bmap
, type
);
4025 return isl_basic_map_free(bmap
);
4026 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4029 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4030 enum isl_dim_type type
, unsigned n
)
4034 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4035 return isl_basic_map_add_dims(bset
, type
, n
);
4037 isl_basic_set_free(bset
);
4041 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4042 enum isl_dim_type type
)
4046 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4049 space
= isl_map_get_space(map
);
4050 space
= isl_space_reset(space
, type
);
4051 map
= isl_map_reset_space(map
, space
);
4055 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4056 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4062 return map_space_reset(map
, type
);
4064 map
= isl_map_cow(map
);
4068 for (i
= 0; i
< map
->n
; ++i
) {
4069 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4074 space
= isl_map_take_space(map
);
4075 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4076 map
= isl_map_restore_space(map
, space
);
4084 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4085 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4087 return isl_map_insert_dims(set
, type
, pos
, n
);
4090 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4091 enum isl_dim_type type
, unsigned n
)
4095 dim
= isl_map_dim(map
, type
);
4097 return isl_map_free(map
);
4098 return isl_map_insert_dims(map
, type
, dim
, n
);
4101 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4102 enum isl_dim_type type
, unsigned n
)
4106 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4107 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4113 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4114 __isl_take isl_basic_map
*bmap
,
4115 enum isl_dim_type dst_type
, unsigned dst_pos
,
4116 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4119 struct isl_dim_map
*dim_map
;
4120 struct isl_basic_map
*res
;
4121 enum isl_dim_type t
;
4128 bmap
= isl_basic_map_reset(bmap
, src_type
);
4129 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4133 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4134 return isl_basic_map_free(bmap
);
4136 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4139 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4141 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4142 pos(bmap
->dim
, src_type
) + src_pos
+
4143 ((src_type
< dst_type
) ? n
: 0)) {
4144 space
= isl_basic_map_take_space(bmap
);
4145 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4146 src_type
, src_pos
, n
);
4147 bmap
= isl_basic_map_restore_space(bmap
, space
);
4148 bmap
= isl_basic_map_finalize(bmap
);
4153 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4155 return isl_basic_map_free(bmap
);
4156 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4159 space
= isl_basic_map_peek_space(bmap
);
4160 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4161 isl_size size
= isl_space_dim(space
, t
);
4163 dim_map
= isl_dim_map_free(dim_map
);
4164 if (t
== dst_type
) {
4165 isl_dim_map_dim_range(dim_map
, space
, t
,
4168 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4171 isl_dim_map_dim_range(dim_map
, space
, t
,
4172 dst_pos
, size
- dst_pos
, off
);
4173 off
+= size
- dst_pos
;
4174 } else if (t
== src_type
) {
4175 isl_dim_map_dim_range(dim_map
, space
, t
,
4178 isl_dim_map_dim_range(dim_map
, space
, t
,
4179 src_pos
+ n
, size
- src_pos
- n
, off
);
4180 off
+= size
- src_pos
- n
;
4182 isl_dim_map_dim(dim_map
, space
, t
, off
);
4186 isl_dim_map_div(dim_map
, bmap
, off
);
4188 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4189 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4190 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4191 space
= isl_basic_map_take_space(bmap
);
4192 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4193 src_type
, src_pos
, n
);
4194 bmap
= isl_basic_map_restore_space(bmap
, space
);
4198 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4199 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4200 bmap
= isl_basic_map_finalize(bmap
);
4204 isl_basic_map_free(bmap
);
4208 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4209 enum isl_dim_type dst_type
, unsigned dst_pos
,
4210 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4212 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4213 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4214 src_type
, src_pos
, n
);
4215 return bset_from_bmap(bmap
);
4218 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4219 enum isl_dim_type dst_type
, unsigned dst_pos
,
4220 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4224 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4225 return set_from_map(isl_map_move_dims(set_to_map(set
),
4226 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4232 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4233 enum isl_dim_type dst_type
, unsigned dst_pos
,
4234 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4240 map
= isl_map_reset(map
, src_type
);
4241 map
= isl_map_reset(map
, dst_type
);
4245 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4246 return isl_map_free(map
);
4248 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4251 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4253 map
= isl_map_cow(map
);
4257 for (i
= 0; i
< map
->n
; ++i
) {
4258 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4260 src_type
, src_pos
, n
);
4265 space
= isl_map_take_space(map
);
4266 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4267 src_type
, src_pos
, n
);
4268 map
= isl_map_restore_space(map
, space
);
4276 /* Move the specified dimensions to the last columns right before
4277 * the divs. Don't change the dimension specification of bmap.
4278 * That's the responsibility of the caller.
4280 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4281 enum isl_dim_type type
, unsigned first
, unsigned n
)
4284 struct isl_dim_map
*dim_map
;
4285 struct isl_basic_map
*res
;
4286 enum isl_dim_type t
;
4292 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4293 isl_basic_map_offset(bmap
, isl_dim_div
))
4296 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4298 return isl_basic_map_free(bmap
);
4299 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4302 space
= isl_basic_map_peek_space(bmap
);
4303 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4304 isl_size size
= isl_space_dim(space
, t
);
4306 dim_map
= isl_dim_map_free(dim_map
);
4308 isl_dim_map_dim_range(dim_map
, space
, t
,
4311 isl_dim_map_dim_range(dim_map
, space
, t
,
4312 first
, n
, total
- bmap
->n_div
- n
);
4313 isl_dim_map_dim_range(dim_map
, space
, t
,
4314 first
+ n
, size
- (first
+ n
), off
);
4315 off
+= size
- (first
+ n
);
4317 isl_dim_map_dim(dim_map
, space
, t
, off
);
4321 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4323 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4324 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4325 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4329 /* Insert "n" rows in the divs of "bmap".
4331 * The number of columns is not changed, which means that the last
4332 * dimensions of "bmap" are being reintepreted as the new divs.
4333 * The space of "bmap" is not adjusted, however, which means
4334 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4335 * from the space of "bmap" is the responsibility of the caller.
4337 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4345 bmap
= isl_basic_map_cow(bmap
);
4349 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4350 old
= bmap
->block2
.data
;
4351 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4352 (bmap
->extra
+ n
) * (1 + row_size
));
4353 if (!bmap
->block2
.data
)
4354 return isl_basic_map_free(bmap
);
4355 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4357 return isl_basic_map_free(bmap
);
4358 for (i
= 0; i
< n
; ++i
) {
4359 new_div
[i
] = bmap
->block2
.data
+
4360 (bmap
->extra
+ i
) * (1 + row_size
);
4361 isl_seq_clr(new_div
[i
], 1 + row_size
);
4363 for (i
= 0; i
< bmap
->extra
; ++i
)
4364 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4366 bmap
->div
= new_div
;
4373 /* Drop constraints from "bmap" that only involve the variables
4374 * of "type" in the range [first, first + n] that are not related
4375 * to any of the variables outside that interval.
4376 * These constraints cannot influence the values for the variables
4377 * outside the interval, except in case they cause "bmap" to be empty.
4378 * Only drop the constraints if "bmap" is known to be non-empty.
4380 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4381 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4382 unsigned first
, unsigned n
)
4386 isl_size dim
, n_div
;
4389 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4391 return isl_basic_map_free(bmap
);
4395 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4396 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4397 if (dim
< 0 || n_div
< 0)
4398 return isl_basic_map_free(bmap
);
4399 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4401 return isl_basic_map_free(bmap
);
4402 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4403 for (i
= 0; i
< first
; ++i
)
4405 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4408 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4413 /* Turn the n dimensions of type type, starting at first
4414 * into existentially quantified variables.
4416 * If a subset of the projected out variables are unrelated
4417 * to any of the variables that remain, then the constraints
4418 * involving this subset are simply dropped first.
4420 __isl_give isl_basic_map
*isl_basic_map_project_out(
4421 __isl_take isl_basic_map
*bmap
,
4422 enum isl_dim_type type
, unsigned first
, unsigned n
)
4428 return basic_map_space_reset(bmap
, type
);
4429 if (type
== isl_dim_div
)
4430 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4431 "cannot project out existentially quantified variables",
4432 return isl_basic_map_free(bmap
));
4434 empty
= isl_basic_map_plain_is_empty(bmap
);
4436 return isl_basic_map_free(bmap
);
4438 bmap
= isl_basic_map_set_to_empty(bmap
);
4440 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4444 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4445 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4447 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4448 return isl_basic_map_free(bmap
);
4450 bmap
= move_last(bmap
, type
, first
, n
);
4451 bmap
= isl_basic_map_cow(bmap
);
4452 bmap
= insert_div_rows(bmap
, n
);
4454 space
= isl_basic_map_take_space(bmap
);
4455 space
= isl_space_drop_dims(space
, type
, first
, n
);
4456 bmap
= isl_basic_map_restore_space(bmap
, space
);
4457 bmap
= isl_basic_map_simplify(bmap
);
4458 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4459 return isl_basic_map_finalize(bmap
);
4462 /* Turn the n dimensions of type type, starting at first
4463 * into existentially quantified variables.
4465 __isl_give isl_basic_set
*isl_basic_set_project_out(
4466 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4467 unsigned first
, unsigned n
)
4469 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4473 /* Turn the n dimensions of type type, starting at first
4474 * into existentially quantified variables.
4476 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4477 enum isl_dim_type type
, unsigned first
, unsigned n
)
4483 return map_space_reset(map
, type
);
4485 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4486 return isl_map_free(map
);
4488 map
= isl_map_cow(map
);
4492 for (i
= 0; i
< map
->n
; ++i
) {
4493 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4499 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4500 map
= isl_map_unmark_normalized(map
);
4502 space
= isl_map_take_space(map
);
4503 space
= isl_space_drop_dims(space
, type
, first
, n
);
4504 map
= isl_map_restore_space(map
, space
);
4513 #define TYPE isl_map
4514 #include "isl_project_out_all_params_templ.c"
4516 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4517 * into existentially quantified variables.
4519 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4520 enum isl_dim_type type
, unsigned first
, unsigned n
)
4524 dim
= isl_map_dim(map
, type
);
4525 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4526 return isl_map_free(map
);
4527 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4528 map
= isl_map_project_out(map
, type
, 0, first
);
4532 /* Turn the n dimensions of type type, starting at first
4533 * into existentially quantified variables.
4535 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4536 enum isl_dim_type type
, unsigned first
, unsigned n
)
4538 return set_from_map(isl_map_project_out(set_to_map(set
),
4542 /* If "set" involves a parameter with identifier "id",
4543 * then turn it into an existentially quantified variable.
4545 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4546 __isl_take isl_id
*id
)
4552 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
4556 return isl_set_project_out(set
, isl_dim_param
, pos
, 1);
4563 /* If "set" involves any of the parameters with identifiers in "list",
4564 * then turn them into existentially quantified variables.
4566 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4567 __isl_take isl_id_list
*list
)
4572 n
= isl_id_list_size(list
);
4575 for (i
= 0; i
< n
; ++i
) {
4578 id
= isl_id_list_get_at(list
, i
);
4579 set
= isl_set_project_out_param_id(set
, id
);
4582 isl_id_list_free(list
);
4585 isl_id_list_free(list
);
4590 /* Project out all parameters from "set" by existentially quantifying
4593 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4595 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4598 /* Return a map that projects the elements in "set" onto their
4599 * "n" set dimensions starting at "first".
4600 * "type" should be equal to isl_dim_set.
4602 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4603 enum isl_dim_type type
, unsigned first
, unsigned n
)
4608 if (type
!= isl_dim_set
)
4609 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4610 "only set dimensions can be projected out", goto error
);
4611 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4612 return isl_set_free(set
);
4614 map
= isl_map_from_domain(set
);
4615 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4616 for (i
= 0; i
< n
; ++i
)
4617 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4625 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4631 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4633 return isl_basic_map_free(bmap
);
4634 for (i
= 0; i
< n
; ++i
) {
4635 j
= isl_basic_map_alloc_div(bmap
);
4638 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4642 isl_basic_map_free(bmap
);
4646 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4648 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4649 __isl_keep isl_basic_map
*bmap2
)
4651 isl_space
*space1
, *space2
;
4653 space1
= isl_basic_map_peek_space(bmap1
);
4654 space2
= isl_basic_map_peek_space(bmap2
);
4655 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4656 space2
, isl_dim_in
);
4659 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4661 static isl_stat
isl_basic_map_check_applies_range(
4662 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4666 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4668 return isl_stat_error
;
4670 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4671 "spaces don't match", return isl_stat_error
);
4675 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4676 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4678 isl_space
*space_result
= NULL
;
4679 struct isl_basic_map
*bmap
;
4680 isl_size n_in
, n_out
, n
, nparam
;
4681 unsigned total
, pos
;
4682 struct isl_dim_map
*dim_map1
, *dim_map2
;
4684 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4686 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4689 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4690 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4691 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4692 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4693 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4696 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4697 isl_basic_map_get_space(bmap2
));
4699 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4700 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4701 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4702 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4703 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4704 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4705 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4706 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4707 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4708 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4709 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4711 bmap
= isl_basic_map_alloc_space(space_result
,
4712 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4713 bmap1
->n_eq
+ bmap2
->n_eq
,
4714 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4715 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4716 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4717 bmap
= add_divs(bmap
, n
);
4718 bmap
= isl_basic_map_simplify(bmap
);
4719 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4720 return isl_basic_map_finalize(bmap
);
4722 isl_basic_map_free(bmap1
);
4723 isl_basic_map_free(bmap2
);
4727 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4728 __isl_take isl_basic_map
*bmap
)
4730 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4733 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4736 isl_basic_set_free(bset
);
4737 isl_basic_map_free(bmap
);
4741 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4742 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4744 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4746 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4747 bmap2
->dim
, isl_dim_in
))
4748 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4749 "spaces don't match", goto error
);
4751 bmap1
= isl_basic_map_reverse(bmap1
);
4752 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4753 return isl_basic_map_reverse(bmap1
);
4755 isl_basic_map_free(bmap1
);
4756 isl_basic_map_free(bmap2
);
4760 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4761 * A \cap B -> f(A) + f(B)
4763 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4764 __isl_take isl_basic_map
*bmap2
)
4766 isl_size n_in
, n_out
, nparam
;
4767 unsigned total
, pos
;
4768 struct isl_basic_map
*bmap
= NULL
;
4769 struct isl_dim_map
*dim_map1
, *dim_map2
;
4772 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4775 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4776 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4777 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4778 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4781 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4782 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4783 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4784 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4785 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4786 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4787 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4788 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4789 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4790 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4791 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4793 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4794 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4795 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4796 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4797 for (i
= 0; i
< n_out
; ++i
) {
4798 int j
= isl_basic_map_alloc_equality(bmap
);
4801 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4802 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4803 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4804 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4806 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4807 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4808 bmap
= add_divs(bmap
, 2 * n_out
);
4810 bmap
= isl_basic_map_simplify(bmap
);
4811 return isl_basic_map_finalize(bmap
);
4813 isl_basic_map_free(bmap
);
4814 isl_basic_map_free(bmap1
);
4815 isl_basic_map_free(bmap2
);
4819 /* Given two maps A -> f(A) and B -> g(B), construct a map
4820 * A \cap B -> f(A) + f(B)
4822 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4823 __isl_take isl_map
*map2
)
4825 struct isl_map
*result
;
4828 if (isl_map_check_equal_space(map1
, map2
) < 0)
4831 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4832 map1
->n
* map2
->n
, 0);
4835 for (i
= 0; i
< map1
->n
; ++i
)
4836 for (j
= 0; j
< map2
->n
; ++j
) {
4837 struct isl_basic_map
*part
;
4838 part
= isl_basic_map_sum(
4839 isl_basic_map_copy(map1
->p
[i
]),
4840 isl_basic_map_copy(map2
->p
[j
]));
4841 if (isl_basic_map_is_empty(part
))
4842 isl_basic_map_free(part
);
4844 result
= isl_map_add_basic_map(result
, part
);
4857 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4858 __isl_take isl_set
*set2
)
4860 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4863 /* Given a basic map A -> f(A), construct A -> -f(A).
4865 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4871 bmap
= isl_basic_map_cow(bmap
);
4872 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4874 return isl_basic_map_free(bmap
);
4876 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4877 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4878 for (j
= 0; j
< n
; ++j
)
4879 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4880 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4881 for (j
= 0; j
< n
; ++j
)
4882 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4883 for (i
= 0; i
< bmap
->n_div
; ++i
)
4884 for (j
= 0; j
< n
; ++j
)
4885 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4886 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4887 return isl_basic_map_finalize(bmap
);
4890 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4892 return isl_basic_map_neg(bset
);
4895 /* Given a map A -> f(A), construct A -> -f(A).
4897 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4901 map
= isl_map_cow(map
);
4905 for (i
= 0; i
< map
->n
; ++i
) {
4906 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4917 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4919 return set_from_map(isl_map_neg(set_to_map(set
)));
4922 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4923 * A -> floor(f(A)/d).
4925 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4928 isl_size n_in
, n_out
, nparam
;
4929 unsigned total
, pos
;
4930 struct isl_basic_map
*result
= NULL
;
4931 struct isl_dim_map
*dim_map
;
4934 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4935 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4936 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4937 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4938 return isl_basic_map_free(bmap
);
4940 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4941 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4942 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4943 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4944 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4945 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4947 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4948 bmap
->n_div
+ n_out
,
4949 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4950 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4951 result
= add_divs(result
, n_out
);
4952 for (i
= 0; i
< n_out
; ++i
) {
4954 j
= isl_basic_map_alloc_inequality(result
);
4957 isl_seq_clr(result
->ineq
[j
], 1+total
);
4958 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4959 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4960 j
= isl_basic_map_alloc_inequality(result
);
4963 isl_seq_clr(result
->ineq
[j
], 1+total
);
4964 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4965 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4966 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4969 result
= isl_basic_map_simplify(result
);
4970 return isl_basic_map_finalize(result
);
4972 isl_basic_map_free(result
);
4976 /* Given a map A -> f(A) and an integer d, construct a map
4977 * A -> floor(f(A)/d).
4979 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4983 map
= isl_map_cow(map
);
4987 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4988 for (i
= 0; i
< map
->n
; ++i
) {
4989 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4993 map
= isl_map_unmark_normalized(map
);
5001 /* Given a map A -> f(A) and an integer d, construct a map
5002 * A -> floor(f(A)/d).
5004 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5005 __isl_take isl_val
*d
)
5009 if (!isl_val_is_int(d
))
5010 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5011 "expecting integer denominator", goto error
);
5012 map
= isl_map_floordiv(map
, d
->n
);
5021 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5029 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5030 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5031 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5032 if (total
< 0 || nparam
< 0 || n_in
< 0)
5033 return isl_basic_map_free(bmap
);
5034 i
= isl_basic_map_alloc_equality(bmap
);
5037 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5038 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5039 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5040 return isl_basic_map_finalize(bmap
);
5042 isl_basic_map_free(bmap
);
5046 /* Add a constraint to "bmap" expressing i_pos < o_pos
5048 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5056 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5057 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5058 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5059 if (total
< 0 || nparam
< 0 || n_in
< 0)
5060 return isl_basic_map_free(bmap
);
5061 i
= isl_basic_map_alloc_inequality(bmap
);
5064 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5065 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5066 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5067 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5068 return isl_basic_map_finalize(bmap
);
5070 isl_basic_map_free(bmap
);
5074 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5076 static __isl_give isl_basic_map
*var_less_or_equal(
5077 __isl_take isl_basic_map
*bmap
, unsigned pos
)
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_inequality(bmap
);
5092 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5093 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5094 isl_int_set_si(bmap
->ineq
[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_more(__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_more_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 __isl_give isl_basic_map
*isl_basic_map_equal(
5157 __isl_take isl_space
*space
, unsigned n_equal
)
5160 struct isl_basic_map
*bmap
;
5161 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5164 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5165 bmap
= var_equal(bmap
, i
);
5166 return isl_basic_map_finalize(bmap
);
5169 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5171 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5175 struct isl_basic_map
*bmap
;
5176 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5179 for (i
= 0; i
< pos
&& bmap
; ++i
)
5180 bmap
= var_equal(bmap
, i
);
5182 bmap
= var_less(bmap
, pos
);
5183 return isl_basic_map_finalize(bmap
);
5186 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5188 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5189 __isl_take isl_space
*space
, unsigned pos
)
5192 isl_basic_map
*bmap
;
5194 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5195 for (i
= 0; i
< pos
; ++i
)
5196 bmap
= var_equal(bmap
, i
);
5197 bmap
= var_less_or_equal(bmap
, pos
);
5198 return isl_basic_map_finalize(bmap
);
5201 /* Return a relation on "space" expressing i_pos > o_pos
5203 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5207 struct isl_basic_map
*bmap
;
5208 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5211 for (i
= 0; i
< pos
&& bmap
; ++i
)
5212 bmap
= var_equal(bmap
, i
);
5214 bmap
= var_more(bmap
, pos
);
5215 return isl_basic_map_finalize(bmap
);
5218 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5220 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5221 __isl_take isl_space
*space
, unsigned pos
)
5224 isl_basic_map
*bmap
;
5226 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5227 for (i
= 0; i
< pos
; ++i
)
5228 bmap
= var_equal(bmap
, i
);
5229 bmap
= var_more_or_equal(bmap
, pos
);
5230 return isl_basic_map_finalize(bmap
);
5233 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5234 unsigned n
, int equal
)
5236 struct isl_map
*map
;
5239 if (n
== 0 && equal
)
5240 return isl_map_universe(space
);
5242 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5244 for (i
= 0; i
+ 1 < n
; ++i
)
5245 map
= isl_map_add_basic_map(map
,
5246 isl_basic_map_less_at(isl_space_copy(space
), i
));
5249 map
= isl_map_add_basic_map(map
,
5250 isl_basic_map_less_or_equal_at(space
, n
- 1));
5252 map
= isl_map_add_basic_map(map
,
5253 isl_basic_map_less_at(space
, n
- 1));
5255 isl_space_free(space
);
5260 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5264 return map_lex_lte_first(space
, space
->n_out
, equal
);
5267 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5270 return map_lex_lte_first(space
, n
, 0);
5273 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5276 return map_lex_lte_first(space
, n
, 1);
5279 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5281 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5284 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5286 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5289 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5290 unsigned n
, int equal
)
5292 struct isl_map
*map
;
5295 if (n
== 0 && equal
)
5296 return isl_map_universe(space
);
5298 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5300 for (i
= 0; i
+ 1 < n
; ++i
)
5301 map
= isl_map_add_basic_map(map
,
5302 isl_basic_map_more_at(isl_space_copy(space
), i
));
5305 map
= isl_map_add_basic_map(map
,
5306 isl_basic_map_more_or_equal_at(space
, n
- 1));
5308 map
= isl_map_add_basic_map(map
,
5309 isl_basic_map_more_at(space
, n
- 1));
5311 isl_space_free(space
);
5316 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5320 return map_lex_gte_first(space
, space
->n_out
, equal
);
5323 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5326 return map_lex_gte_first(space
, n
, 0);
5329 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5332 return map_lex_gte_first(space
, n
, 1);
5335 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5337 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5340 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5342 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5345 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5346 __isl_take isl_set
*set2
)
5349 map
= isl_map_lex_le(isl_set_get_space(set1
));
5350 map
= isl_map_intersect_domain(map
, set1
);
5351 map
= isl_map_intersect_range(map
, set2
);
5355 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5356 __isl_take isl_set
*set2
)
5359 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5360 map
= isl_map_intersect_domain(map
, set1
);
5361 map
= isl_map_intersect_range(map
, set2
);
5365 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5366 __isl_take isl_set
*set2
)
5369 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5370 map
= isl_map_intersect_domain(map
, set1
);
5371 map
= isl_map_intersect_range(map
, set2
);
5375 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5376 __isl_take isl_set
*set2
)
5379 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5380 map
= isl_map_intersect_domain(map
, set1
);
5381 map
= isl_map_intersect_range(map
, set2
);
5385 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5386 __isl_take isl_map
*map2
)
5389 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5390 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5391 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5395 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5396 __isl_take isl_map
*map2
)
5399 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5400 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5401 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5405 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5406 __isl_take isl_map
*map2
)
5409 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5410 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5411 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5415 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5416 __isl_take isl_map
*map2
)
5419 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5420 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5421 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5425 /* For the div d = floor(f/m) at position "div", add the constraint
5429 static __isl_give isl_basic_map
*add_upper_div_constraint(
5430 __isl_take isl_basic_map
*bmap
, unsigned div
)
5433 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5437 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5438 if (v_div
< 0 || n_div
< 0)
5439 return isl_basic_map_free(bmap
);
5441 i
= isl_basic_map_alloc_inequality(bmap
);
5443 return isl_basic_map_free(bmap
);
5444 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5445 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5450 /* For the div d = floor(f/m) at position "div", add the constraint
5452 * -(f-(m-1)) + m d >= 0
5454 static __isl_give isl_basic_map
*add_lower_div_constraint(
5455 __isl_take isl_basic_map
*bmap
, unsigned div
)
5458 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5462 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5463 if (v_div
< 0 || n_div
< 0)
5464 return isl_basic_map_free(bmap
);
5466 i
= isl_basic_map_alloc_inequality(bmap
);
5468 return isl_basic_map_free(bmap
);
5469 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5470 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5471 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5472 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5477 /* For the div d = floor(f/m) at position "pos", add the constraints
5480 * -(f-(m-1)) + m d >= 0
5482 * Note that the second constraint is the negation of
5486 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5487 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5489 bmap
= add_upper_div_constraint(bmap
, pos
);
5490 bmap
= add_lower_div_constraint(bmap
, pos
);
5494 /* For each known div d = floor(f/m), add the constraints
5497 * -(f-(m-1)) + m d >= 0
5499 * Remove duplicate constraints in case of some these div constraints
5500 * already appear in "bmap".
5502 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5503 __isl_take isl_basic_map
*bmap
)
5507 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5509 return isl_basic_map_free(bmap
);
5513 bmap
= add_known_div_constraints(bmap
);
5514 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5515 bmap
= isl_basic_map_finalize(bmap
);
5519 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5521 * In particular, if this div is of the form d = floor(f/m),
5522 * then add the constraint
5526 * if sign < 0 or the constraint
5528 * -(f-(m-1)) + m d >= 0
5532 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5533 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5536 return add_upper_div_constraint(bmap
, div
);
5538 return add_lower_div_constraint(bmap
, div
);
5541 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5542 __isl_take isl_basic_map
*bmap
)
5548 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5550 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5551 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5552 return bset_from_bmap(bmap
);
5553 bmap
= isl_basic_map_cow(bmap
);
5556 space
= isl_basic_map_take_space(bmap
);
5557 space
= isl_space_underlying(space
, bmap
->n_div
);
5558 bmap
= isl_basic_map_restore_space(bmap
, space
);
5561 bmap
->extra
-= bmap
->n_div
;
5563 bmap
= isl_basic_map_finalize(bmap
);
5564 return bset_from_bmap(bmap
);
5566 isl_basic_map_free(bmap
);
5570 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5571 __isl_take isl_basic_set
*bset
)
5573 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5576 /* Replace each element in "list" by the result of applying
5577 * isl_basic_map_underlying_set to the element.
5579 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5580 __isl_take isl_basic_map_list
*list
)
5585 n
= isl_basic_map_list_n_basic_map(list
);
5589 for (i
= 0; i
< n
; ++i
) {
5590 isl_basic_map
*bmap
;
5591 isl_basic_set
*bset
;
5593 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5594 bset
= isl_basic_set_underlying_set(bmap
);
5595 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5600 isl_basic_map_list_free(list
);
5604 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5605 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5607 struct isl_basic_map
*bmap
;
5608 struct isl_ctx
*ctx
;
5609 isl_size dim
, bmap_total
;
5616 if (isl_basic_set_check_no_params(bset
) < 0 ||
5617 isl_basic_set_check_no_locals(bset
) < 0)
5619 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5620 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5621 if (dim
< 0 || bmap_total
< 0)
5623 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5624 if (like
->n_div
== 0) {
5625 isl_space
*space
= isl_basic_map_get_space(like
);
5626 isl_basic_map_free(like
);
5627 return isl_basic_map_reset_space(bset
, space
);
5629 bset
= isl_basic_set_cow(bset
);
5632 total
= dim
+ bset
->extra
;
5633 bmap
= bset_to_bmap(bset
);
5634 isl_space_free(isl_basic_map_take_space(bmap
));
5635 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5638 bmap
->n_div
= like
->n_div
;
5639 bmap
->extra
+= like
->n_div
;
5643 ltotal
= total
- bmap
->extra
+ like
->extra
;
5646 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5647 bmap
->extra
* (1 + 1 + total
));
5648 if (isl_blk_is_error(bmap
->block2
))
5650 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5654 for (i
= 0; i
< bmap
->extra
; ++i
)
5655 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5656 for (i
= 0; i
< like
->n_div
; ++i
) {
5657 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5658 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5660 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5662 isl_basic_map_free(like
);
5663 bmap
= isl_basic_map_simplify(bmap
);
5664 bmap
= isl_basic_map_finalize(bmap
);
5667 isl_basic_map_free(like
);
5668 isl_basic_set_free(bset
);
5672 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5673 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5675 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5676 bset_to_bmap(like
)));
5679 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5683 map
= isl_map_cow(map
);
5686 map
->dim
= isl_space_cow(map
->dim
);
5690 for (i
= 1; i
< map
->n
; ++i
)
5691 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5693 for (i
= 0; i
< map
->n
; ++i
) {
5694 map
->p
[i
] = bset_to_bmap(
5695 isl_basic_map_underlying_set(map
->p
[i
]));
5700 map
->dim
= isl_space_underlying(map
->dim
, 0);
5702 isl_space_free(map
->dim
);
5703 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5707 return set_from_map(map
);
5713 /* Replace the space of "bmap" by "space".
5715 * If the space of "bmap" is identical to "space" (including the identifiers
5716 * of the input and output dimensions), then simply return the original input.
5718 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5719 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5722 isl_space
*bmap_space
;
5724 bmap_space
= isl_basic_map_peek_space(bmap
);
5725 equal
= isl_space_is_equal(bmap_space
, space
);
5726 if (equal
>= 0 && equal
)
5727 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5731 isl_space_free(space
);
5734 isl_space_free(isl_basic_map_take_space(bmap
));
5735 bmap
= isl_basic_map_restore_space(bmap
, space
);
5737 bmap
= isl_basic_map_finalize(bmap
);
5741 isl_basic_map_free(bmap
);
5742 isl_space_free(space
);
5746 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5747 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5749 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5753 /* Check that the total dimensions of "map" and "space" are the same.
5755 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5756 __isl_keep isl_space
*space
)
5758 isl_size dim1
, dim2
;
5760 dim1
= isl_map_dim(map
, isl_dim_all
);
5761 dim2
= isl_space_dim(space
, isl_dim_all
);
5762 if (dim1
< 0 || dim2
< 0)
5763 return isl_stat_error
;
5766 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5767 "total dimensions do not match", return isl_stat_error
);
5770 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5771 __isl_take isl_space
*space
)
5775 map
= isl_map_cow(map
);
5779 for (i
= 0; i
< map
->n
; ++i
) {
5780 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5781 isl_space_copy(space
));
5785 isl_space_free(isl_map_take_space(map
));
5786 map
= isl_map_restore_space(map
, space
);
5791 isl_space_free(space
);
5795 /* Replace the space of "map" by "space", without modifying
5796 * the dimension of "map".
5798 * If the space of "map" is identical to "space" (including the identifiers
5799 * of the input and output dimensions), then simply return the original input.
5801 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5802 __isl_take isl_space
*space
)
5805 isl_space
*map_space
;
5807 map_space
= isl_map_peek_space(map
);
5808 equal
= isl_space_is_equal(map_space
, space
);
5809 if (equal
>= 0 && equal
)
5810 equal
= isl_space_has_equal_ids(map_space
, space
);
5814 isl_space_free(space
);
5817 if (check_map_space_equal_total_dim(map
, space
) < 0)
5819 return isl_map_reset_space(map
, space
);
5822 isl_space_free(space
);
5826 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5827 __isl_take isl_space
*space
)
5829 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5832 /* Compute the parameter domain of the given basic set.
5834 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5840 is_params
= isl_basic_set_is_params(bset
);
5842 return isl_basic_set_free(bset
);
5846 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5848 return isl_basic_set_free(bset
);
5849 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5850 space
= isl_basic_set_get_space(bset
);
5851 space
= isl_space_params(space
);
5852 bset
= isl_basic_set_reset_space(bset
, space
);
5856 /* Construct a zero-dimensional basic set with the given parameter domain.
5858 __isl_give isl_basic_set
*isl_basic_set_from_params(
5859 __isl_take isl_basic_set
*bset
)
5862 space
= isl_basic_set_get_space(bset
);
5863 space
= isl_space_set_from_params(space
);
5864 bset
= isl_basic_set_reset_space(bset
, space
);
5868 /* Compute the parameter domain of the given set.
5870 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5872 return isl_map_params(set_to_map(set
));
5875 /* Construct a zero-dimensional set with the given parameter domain.
5877 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5880 space
= isl_set_get_space(set
);
5881 space
= isl_space_set_from_params(space
);
5882 set
= isl_set_reset_space(set
, space
);
5886 /* Compute the parameter domain of the given map.
5888 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5891 isl_size n_in
, n_out
;
5893 n_in
= isl_map_dim(map
, isl_dim_in
);
5894 n_out
= isl_map_dim(map
, isl_dim_out
);
5895 if (n_in
< 0 || n_out
< 0)
5896 return isl_map_free(map
);
5897 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5898 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5899 space
= isl_map_get_space(map
);
5900 space
= isl_space_params(space
);
5901 map
= isl_map_reset_space(map
, space
);
5905 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5910 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5912 return isl_basic_map_free(bmap
);
5913 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5915 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5917 return isl_basic_map_reset_space(bmap
, space
);
5920 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5923 return isl_bool_error
;
5924 return isl_space_may_be_set(bmap
->dim
);
5927 /* Is this basic map actually a set?
5928 * Users should never call this function. Outside of isl,
5929 * the type should indicate whether something is a set or a map.
5931 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5934 return isl_bool_error
;
5935 return isl_space_is_set(bmap
->dim
);
5938 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
5942 is_set
= isl_basic_map_is_set(bmap
);
5947 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5949 isl_basic_map_free(bmap
);
5953 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5954 __isl_take isl_basic_map
*bmap
)
5958 isl_basic_map
*domain
;
5959 isl_size nparam
, n_in
, n_out
;
5961 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5962 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5963 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5964 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5965 return isl_basic_map_free(bmap
);
5967 space
= isl_basic_map_get_space(bmap
);
5968 space
= isl_space_from_range(isl_space_domain(space
));
5969 domain
= isl_basic_map_universe(space
);
5971 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5972 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5973 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5975 for (i
= 0; i
< n_in
; ++i
)
5976 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5979 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5980 return isl_basic_map_finalize(bmap
);
5983 __isl_give isl_basic_map
*isl_basic_map_range_map(
5984 __isl_take isl_basic_map
*bmap
)
5988 isl_basic_map
*range
;
5989 isl_size nparam
, n_in
, n_out
;
5991 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5992 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5993 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5994 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5995 return isl_basic_map_free(bmap
);
5997 space
= isl_basic_map_get_space(bmap
);
5998 space
= isl_space_from_range(isl_space_range(space
));
5999 range
= isl_basic_map_universe(space
);
6001 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6002 bmap
= isl_basic_map_apply_range(bmap
, range
);
6003 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6005 for (i
= 0; i
< n_out
; ++i
)
6006 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6009 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6010 return isl_basic_map_finalize(bmap
);
6013 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6017 return isl_space_may_be_set(map
->dim
);
6020 /* Is this map actually a set?
6021 * Users should never call this function. Outside of isl,
6022 * the type should indicate whether something is a set or a map.
6024 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6027 return isl_bool_error
;
6028 return isl_space_is_set(map
->dim
);
6031 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6036 n_in
= isl_map_dim(map
, isl_dim_in
);
6038 return set_from_map(isl_map_free(map
));
6039 space
= isl_space_range(isl_map_get_space(map
));
6041 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6043 return set_from_map(isl_map_reset_space(map
, space
));
6046 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6047 * to each of its basic maps.
6049 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6050 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6051 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6056 map
= isl_map_cow(map
);
6060 for (i
= 0; i
< map
->n
; ++i
) {
6061 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6063 return isl_map_free(map
);
6065 map
= isl_map_unmark_normalized(map
);
6067 space
= isl_map_take_space(map
);
6068 space
= fn_space(space
);
6069 map
= isl_map_restore_space(map
, space
);
6074 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6076 return isl_map_transform(map
, &isl_space_domain_map
,
6077 &isl_basic_map_domain_map
);
6080 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6082 return isl_map_transform(map
, &isl_space_range_map
,
6083 &isl_basic_map_range_map
);
6086 /* Given a wrapped map of the form A[B -> C],
6087 * return the map A[B -> C] -> B.
6089 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6096 if (!isl_set_has_tuple_id(set
))
6097 return isl_map_domain_map(isl_set_unwrap(set
));
6099 id
= isl_set_get_tuple_id(set
);
6100 map
= isl_map_domain_map(isl_set_unwrap(set
));
6101 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6106 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6107 __isl_take isl_basic_set
*bset
)
6109 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6112 __isl_give isl_basic_map
*isl_basic_map_from_range(
6113 __isl_take isl_basic_set
*bset
)
6116 space
= isl_basic_set_get_space(bset
);
6117 space
= isl_space_from_range(space
);
6118 bset
= isl_basic_set_reset_space(bset
, space
);
6119 return bset_to_bmap(bset
);
6122 /* Create a relation with the given set as range.
6123 * The domain of the created relation is a zero-dimensional
6124 * flat anonymous space.
6126 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6129 space
= isl_set_get_space(set
);
6130 space
= isl_space_from_range(space
);
6131 set
= isl_set_reset_space(set
, space
);
6132 return set_to_map(set
);
6135 /* Create a relation with the given set as domain.
6136 * The range of the created relation is a zero-dimensional
6137 * flat anonymous space.
6139 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6141 return isl_map_reverse(isl_map_from_range(set
));
6144 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6145 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6147 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6150 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6151 __isl_take isl_set
*range
)
6153 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6156 /* Return a newly allocated isl_map with given space and flags and
6157 * room for "n" basic maps.
6158 * Make sure that all cached information is cleared.
6160 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6163 struct isl_map
*map
;
6168 isl_die(space
->ctx
, isl_error_internal
,
6169 "negative number of basic maps", goto error
);
6170 map
= isl_calloc(space
->ctx
, struct isl_map
,
6171 sizeof(struct isl_map
) +
6172 (n
- 1) * sizeof(struct isl_basic_map
*));
6176 map
->ctx
= space
->ctx
;
6177 isl_ctx_ref(map
->ctx
);
6185 isl_space_free(space
);
6189 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6191 struct isl_basic_map
*bmap
;
6192 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6193 bmap
= isl_basic_map_set_to_empty(bmap
);
6197 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6199 struct isl_basic_set
*bset
;
6200 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6201 bset
= isl_basic_set_set_to_empty(bset
);
6205 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6207 struct isl_basic_map
*bmap
;
6208 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6209 bmap
= isl_basic_map_finalize(bmap
);
6213 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6215 struct isl_basic_set
*bset
;
6216 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6217 bset
= isl_basic_set_finalize(bset
);
6221 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6222 __isl_take isl_space
*space
)
6225 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6226 isl_basic_map
*bmap
;
6229 space
= isl_space_free(space
);
6230 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6231 for (i
= 0; i
< total
; ++i
) {
6232 int k
= isl_basic_map_alloc_inequality(bmap
);
6235 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6236 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6240 isl_basic_map_free(bmap
);
6244 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6245 __isl_take isl_space
*space
)
6247 return isl_basic_map_nat_universe(space
);
6250 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6252 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6255 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6257 return isl_map_nat_universe(space
);
6260 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6262 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6265 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6267 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6270 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6272 struct isl_map
*map
;
6275 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6276 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6280 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6282 struct isl_set
*set
;
6285 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6286 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6290 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6293 struct isl_map
*dup
;
6297 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6298 for (i
= 0; i
< map
->n
; ++i
)
6299 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6303 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6304 __isl_take isl_basic_map
*bmap
)
6308 if (isl_basic_map_plain_is_empty(bmap
)) {
6309 isl_basic_map_free(bmap
);
6312 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6314 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6315 map
->p
[map
->n
] = bmap
;
6317 map
= isl_map_unmark_normalized(map
);
6323 isl_basic_map_free(bmap
);
6327 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6338 isl_ctx_deref(map
->ctx
);
6339 for (i
= 0; i
< map
->n
; ++i
)
6340 isl_basic_map_free(map
->p
[i
]);
6341 isl_space_free(map
->dim
);
6347 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6348 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6353 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6355 return isl_basic_map_free(bmap
);
6357 bmap
= isl_basic_map_cow(bmap
);
6358 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6359 j
= isl_basic_map_alloc_equality(bmap
);
6362 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6363 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6364 isl_int_set_si(bmap
->eq
[j
][0], value
);
6365 bmap
= isl_basic_map_simplify(bmap
);
6366 return isl_basic_map_finalize(bmap
);
6368 isl_basic_map_free(bmap
);
6372 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6373 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6378 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6380 return isl_basic_map_free(bmap
);
6382 bmap
= isl_basic_map_cow(bmap
);
6383 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6384 j
= isl_basic_map_alloc_equality(bmap
);
6387 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6388 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6389 isl_int_set(bmap
->eq
[j
][0], value
);
6390 bmap
= isl_basic_map_simplify(bmap
);
6391 return isl_basic_map_finalize(bmap
);
6393 isl_basic_map_free(bmap
);
6397 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6398 enum isl_dim_type type
, unsigned pos
, int value
)
6400 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6401 return isl_basic_map_free(bmap
);
6402 return isl_basic_map_fix_pos_si(bmap
,
6403 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6406 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6407 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6409 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6410 return isl_basic_map_free(bmap
);
6411 return isl_basic_map_fix_pos(bmap
,
6412 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6415 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6416 * to be equal to "v".
6418 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6419 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6423 if (!isl_val_is_int(v
))
6424 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6425 "expecting integer value", goto error
);
6426 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6428 pos
+= isl_basic_map_offset(bmap
, type
);
6429 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6433 isl_basic_map_free(bmap
);
6438 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6439 * to be equal to "v".
6441 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6442 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6444 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6447 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6448 enum isl_dim_type type
, unsigned pos
, int value
)
6450 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6454 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6455 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6457 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6461 /* Remove the basic map at position "i" from "map" if this basic map
6462 * is (obviously) empty.
6464 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6471 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6473 return isl_map_free(map
);
6477 isl_basic_map_free(map
->p
[i
]);
6480 map
->p
[i
] = map
->p
[map
->n
];
6481 map
= isl_map_unmark_normalized(map
);
6488 /* Perform "fn" on each basic map of "map", where we may not be holding
6489 * the only reference to "map".
6490 * In particular, "fn" should be a semantics preserving operation
6491 * that we want to apply to all copies of "map". We therefore need
6492 * to be careful not to modify "map" in a way that breaks "map"
6493 * in case anything goes wrong.
6495 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6496 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6498 struct isl_basic_map
*bmap
;
6504 for (i
= map
->n
- 1; i
>= 0; --i
) {
6505 bmap
= isl_basic_map_copy(map
->p
[i
]);
6509 isl_basic_map_free(map
->p
[i
]);
6511 map
= remove_if_empty(map
, i
);
6522 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6523 enum isl_dim_type type
, unsigned pos
, int value
)
6527 map
= isl_map_cow(map
);
6528 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6529 return isl_map_free(map
);
6530 for (i
= map
->n
- 1; i
>= 0; --i
) {
6531 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6532 map
= remove_if_empty(map
, i
);
6536 map
= isl_map_unmark_normalized(map
);
6540 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6541 enum isl_dim_type type
, unsigned pos
, int value
)
6543 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6546 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6547 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6551 map
= isl_map_cow(map
);
6552 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6553 return isl_map_free(map
);
6554 for (i
= 0; i
< map
->n
; ++i
) {
6555 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6559 map
= isl_map_unmark_normalized(map
);
6566 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6567 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6569 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6572 /* Fix the value of the variable at position "pos" of type "type" of "map"
6573 * to be equal to "v".
6575 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6576 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6580 map
= isl_map_cow(map
);
6584 if (!isl_val_is_int(v
))
6585 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6586 "expecting integer value", goto error
);
6587 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6589 for (i
= map
->n
- 1; i
>= 0; --i
) {
6590 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6592 map
= remove_if_empty(map
, i
);
6596 map
= isl_map_unmark_normalized(map
);
6605 /* Fix the value of the variable at position "pos" of type "type" of "set"
6606 * to be equal to "v".
6608 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6609 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6611 return isl_map_fix_val(set
, type
, pos
, v
);
6614 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6615 unsigned input
, int value
)
6617 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6620 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6623 return set_from_map(isl_map_fix_si(set_to_map(set
),
6624 isl_dim_set
, dim
, value
));
6627 static __isl_give isl_basic_map
*basic_map_bound_si(
6628 __isl_take isl_basic_map
*bmap
,
6629 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6634 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6635 return isl_basic_map_free(bmap
);
6636 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6638 return isl_basic_map_free(bmap
);
6639 pos
+= isl_basic_map_offset(bmap
, type
);
6640 bmap
= isl_basic_map_cow(bmap
);
6641 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6642 j
= isl_basic_map_alloc_inequality(bmap
);
6645 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6647 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6648 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6650 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6651 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6653 bmap
= isl_basic_map_simplify(bmap
);
6654 return isl_basic_map_finalize(bmap
);
6656 isl_basic_map_free(bmap
);
6660 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6661 __isl_take isl_basic_map
*bmap
,
6662 enum isl_dim_type type
, unsigned pos
, int value
)
6664 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6667 /* Constrain the values of the given dimension to be no greater than "value".
6669 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6670 __isl_take isl_basic_map
*bmap
,
6671 enum isl_dim_type type
, unsigned pos
, int value
)
6673 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6676 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6677 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6681 map
= isl_map_cow(map
);
6682 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6683 return isl_map_free(map
);
6684 for (i
= 0; i
< map
->n
; ++i
) {
6685 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6686 type
, pos
, value
, upper
);
6690 map
= isl_map_unmark_normalized(map
);
6697 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6698 enum isl_dim_type type
, unsigned pos
, int value
)
6700 return map_bound_si(map
, type
, pos
, value
, 0);
6703 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6704 enum isl_dim_type type
, unsigned pos
, int value
)
6706 return map_bound_si(map
, type
, pos
, value
, 1);
6709 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6710 enum isl_dim_type type
, unsigned pos
, int value
)
6712 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6716 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6717 enum isl_dim_type type
, unsigned pos
, int value
)
6719 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6722 /* Bound the given variable of "bmap" from below (or above is "upper"
6723 * is set) to "value".
6725 static __isl_give isl_basic_map
*basic_map_bound(
6726 __isl_take isl_basic_map
*bmap
,
6727 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6732 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6733 return isl_basic_map_free(bmap
);
6734 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6736 return isl_basic_map_free(bmap
);
6737 pos
+= isl_basic_map_offset(bmap
, type
);
6738 bmap
= isl_basic_map_cow(bmap
);
6739 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6740 j
= isl_basic_map_alloc_inequality(bmap
);
6743 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6745 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6746 isl_int_set(bmap
->ineq
[j
][0], value
);
6748 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6749 isl_int_neg(bmap
->ineq
[j
][0], value
);
6751 bmap
= isl_basic_map_simplify(bmap
);
6752 return isl_basic_map_finalize(bmap
);
6754 isl_basic_map_free(bmap
);
6758 /* Bound the given variable of "map" from below (or above is "upper"
6759 * is set) to "value".
6761 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6762 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6766 map
= isl_map_cow(map
);
6767 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6768 return isl_map_free(map
);
6769 for (i
= map
->n
- 1; i
>= 0; --i
) {
6770 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6771 map
= remove_if_empty(map
, i
);
6775 map
= isl_map_unmark_normalized(map
);
6779 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6780 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6782 return map_bound(map
, type
, pos
, value
, 0);
6785 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6786 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6788 return map_bound(map
, type
, pos
, value
, 1);
6791 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6792 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6794 return isl_map_lower_bound(set
, type
, pos
, value
);
6797 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6798 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6800 return isl_map_upper_bound(set
, type
, pos
, value
);
6803 /* Force the values of the variable at position "pos" of type "type" of "map"
6804 * to be no smaller than "value".
6806 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6807 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6811 if (!isl_val_is_int(value
))
6812 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6813 "expecting integer value", goto error
);
6814 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6815 isl_val_free(value
);
6818 isl_val_free(value
);
6823 /* Force the values of the variable at position "pos" of type "type" of "set"
6824 * to be no smaller than "value".
6826 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6827 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6831 map
= set_to_map(set
);
6832 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6835 /* Force the values of the variable at position "pos" of type "type" of "map"
6836 * to be no greater than "value".
6838 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6839 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6843 if (!isl_val_is_int(value
))
6844 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6845 "expecting integer value", goto error
);
6846 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6847 isl_val_free(value
);
6850 isl_val_free(value
);
6855 /* Force the values of the variable at position "pos" of type "type" of "set"
6856 * to be no greater than "value".
6858 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6859 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6863 map
= set_to_map(set
);
6864 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6869 #include "isl_map_bound_templ.c"
6871 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6872 * for each set dimension, by treating the set as a map.
6874 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
6875 __isl_take isl_multi_val
*bound
,
6876 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6877 unsigned pos
, __isl_take isl_val
*value
))
6881 map
= set_to_map(set
);
6882 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
6887 #include "isl_map_bound_templ.c"
6889 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6890 * for each set dimension, by converting the set and the bound
6891 * to objects living in a map space.
6893 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
6894 __isl_take isl_multi_pw_aff
*bound
,
6895 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
6896 unsigned pos
, __isl_take TYPE
*value
))
6900 map
= isl_map_from_range(set
);
6901 bound
= isl_multi_pw_aff_from_range(bound
);
6902 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
6903 return isl_map_range(map
);
6906 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6907 * setting a bound on the given output dimension.
6909 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
6910 unsigned pos
, __isl_take isl_val
*v
)
6912 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
6915 /* Force the values of the set dimensions of "set"
6916 * to be no smaller than the corresponding values in "lower".
6918 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
6919 __isl_take isl_multi_val
*lower
)
6921 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
6924 /* Force the values of the output dimensions of "map"
6925 * to be no smaller than the corresponding values in "lower".
6927 __isl_give isl_map
*isl_map_lower_bound_multi_val(__isl_take isl_map
*map
,
6928 __isl_take isl_multi_val
*lower
)
6930 return map_bound_multi_val(map
, lower
, &map_lower_bound_val
);
6933 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6934 * setting a bound on the given output dimension.
6936 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
6937 unsigned pos
, __isl_take isl_val
*v
)
6939 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
6942 /* Force the values of the set dimensions of "set"
6943 * to be no greater than the corresponding values in "upper".
6945 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
6946 __isl_take isl_multi_val
*upper
)
6948 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
6951 /* Force the values of the set dimensions of "set"
6952 * to be no greater than the corresponding values in "upper".
6954 __isl_give isl_map
*isl_map_upper_bound_multi_val(__isl_take isl_map
*map
,
6955 __isl_take isl_multi_val
*upper
)
6957 return map_bound_multi_val(map
, upper
, &map_upper_bound_val
);
6960 /* Force the symbolic constant expression "bound"
6961 * to satisfy the relation "order" with respect to
6962 * the output variable at position "pos" of "map".
6964 * Create an affine expression representing the output variable
6965 * in terms of the range and
6966 * compare it using "order" to "bound" (defined on the domain).
6967 * The result is a relation between elements in domain and range that
6968 * can be intersected with "map".
6970 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
6971 unsigned pos
, __isl_take isl_pw_aff
*bound
,
6972 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
6973 __isl_take isl_pw_aff
*pa2
))
6976 isl_local_space
*ls
;
6979 space
= isl_space_range(isl_map_get_space(map
));
6980 ls
= isl_local_space_from_space(space
);
6981 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
6982 map
= isl_map_intersect(map
, order(bound
, var
));
6986 /* Force the values of the output variable at position "pos" of "map"
6987 * to be no smaller than the symbolic constant expression "lower".
6989 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
6990 unsigned pos
, __isl_take isl_pw_aff
*lower
)
6992 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
6995 /* Force the values of the output variable at position "pos" of "map"
6996 * to be no greater than the symbolic constant expression "upper".
6998 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
6999 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7001 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7004 /* Force the values of the set dimensions of "set"
7005 * to be no smaller than the corresponding constant symbolic expressions
7008 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7009 __isl_take isl_multi_pw_aff
*lower
)
7011 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7014 /* Force the values of the set dimensions of "set"
7015 * to be no greater than the corresponding constant symbolic expressions
7018 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7019 __isl_take isl_multi_pw_aff
*upper
)
7021 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7024 /* Force the values of the output dimensions of "map"
7025 * to be no smaller than the corresponding constant symbolic expressions
7028 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7029 __isl_take isl_multi_pw_aff
*lower
)
7031 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7034 /* Force the values of the output dimensions of "map"
7035 * to be no greater than the corresponding constant symbolic expressions
7038 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7039 __isl_take isl_multi_pw_aff
*upper
)
7041 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7044 /* Bound the given variable of "bset" from below (or above is "upper"
7045 * is set) to "value".
7047 static __isl_give isl_basic_set
*isl_basic_set_bound(
7048 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7049 isl_int value
, int upper
)
7051 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7052 type
, pos
, value
, upper
));
7055 /* Bound the given variable of "bset" from below (or above is "upper"
7056 * is set) to "value".
7058 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7059 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7060 __isl_take isl_val
*value
, int upper
)
7064 if (!isl_val_is_int(value
))
7065 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7066 "expecting integer value", goto error
);
7067 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7068 isl_val_free(value
);
7071 isl_val_free(value
);
7072 isl_basic_set_free(bset
);
7076 /* Bound the given variable of "bset" from below to "value".
7078 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7079 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7080 __isl_take isl_val
*value
)
7082 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7085 /* Bound the given variable of "bset" from above to "value".
7087 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7088 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7089 __isl_take isl_val
*value
)
7091 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7094 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7096 return isl_map_transform(map
, &isl_space_reverse
,
7097 &isl_basic_map_reverse
);
7100 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7102 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7104 return isl_map_transform(map
, &isl_space_range_reverse
,
7105 &isl_basic_map_range_reverse
);
7109 #define TYPE isl_pw_multi_aff
7111 #define SUFFIX _pw_multi_aff
7113 #define EMPTY isl_pw_multi_aff_empty
7115 #define ADD isl_pw_multi_aff_union_add
7116 #include "isl_map_lexopt_templ.c"
7118 /* Given a map "map", compute the lexicographically minimal
7119 * (or maximal) image element for each domain element in dom,
7120 * in the form of an isl_pw_multi_aff.
7121 * If "empty" is not NULL, then set *empty to those elements in dom that
7122 * do not have an image element.
7123 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7124 * should be computed over the domain of "map". "empty" is also NULL
7127 * We first compute the lexicographically minimal or maximal element
7128 * in the first basic map. This results in a partial solution "res"
7129 * and a subset "todo" of dom that still need to be handled.
7130 * We then consider each of the remaining maps in "map" and successively
7131 * update both "res" and "todo".
7132 * If "empty" is NULL, then the todo sets are not needed and therefore
7133 * also not computed.
7135 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7136 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7137 __isl_give isl_set
**empty
, unsigned flags
)
7141 isl_pw_multi_aff
*res
;
7144 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7145 if (!map
|| (!full
&& !dom
))
7148 if (isl_map_plain_is_empty(map
)) {
7153 return isl_pw_multi_aff_from_map(map
);
7156 res
= basic_map_partial_lexopt_pw_multi_aff(
7157 isl_basic_map_copy(map
->p
[0]),
7158 isl_set_copy(dom
), empty
, flags
);
7162 for (i
= 1; i
< map
->n
; ++i
) {
7163 isl_pw_multi_aff
*res_i
;
7165 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7166 isl_basic_map_copy(map
->p
[i
]),
7167 isl_set_copy(dom
), empty
, flags
);
7169 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7170 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7172 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7175 todo
= isl_set_intersect(todo
, *empty
);
7194 #define TYPE isl_map
7198 #define EMPTY isl_map_empty
7200 #define ADD isl_map_union_disjoint
7201 #include "isl_map_lexopt_templ.c"
7203 /* Given a map "map", compute the lexicographically minimal
7204 * (or maximal) image element for each domain element in "dom",
7205 * in the form of an isl_map.
7206 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7207 * do not have an image element.
7208 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7209 * should be computed over the domain of "map". "empty" is also NULL
7212 * If the input consists of more than one disjunct, then first
7213 * compute the desired result in the form of an isl_pw_multi_aff and
7214 * then convert that into an isl_map.
7216 * This function used to have an explicit implementation in terms
7217 * of isl_maps, but it would continually intersect the domains of
7218 * partial results with the complement of the domain of the next
7219 * partial solution, potentially leading to an explosion in the number
7220 * of disjuncts if there are several disjuncts in the input.
7221 * An even earlier implementation of this function would look for
7222 * better results in the domain of the partial result and for extra
7223 * results in the complement of this domain, which would lead to
7224 * even more splintering.
7226 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7227 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7228 __isl_give isl_set
**empty
, unsigned flags
)
7231 struct isl_map
*res
;
7232 isl_pw_multi_aff
*pma
;
7234 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7235 if (!map
|| (!full
&& !dom
))
7238 if (isl_map_plain_is_empty(map
)) {
7247 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7253 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7255 return isl_map_from_pw_multi_aff_internal(pma
);
7264 __isl_give isl_map
*isl_map_partial_lexmax(
7265 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7266 __isl_give isl_set
**empty
)
7268 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7271 __isl_give isl_map
*isl_map_partial_lexmin(
7272 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7273 __isl_give isl_set
**empty
)
7275 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7278 __isl_give isl_set
*isl_set_partial_lexmin(
7279 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7280 __isl_give isl_set
**empty
)
7282 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7286 __isl_give isl_set
*isl_set_partial_lexmax(
7287 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7288 __isl_give isl_set
**empty
)
7290 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7294 /* Compute the lexicographic minimum (or maximum if "flags" includes
7295 * ISL_OPT_MAX) of "bset" over its parametric domain.
7297 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7300 return isl_basic_map_lexopt(bset
, flags
);
7303 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7305 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7308 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7310 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7313 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7315 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7318 /* Compute the lexicographic minimum of "bset" over its parametric domain
7319 * for the purpose of quantifier elimination.
7320 * That is, find an explicit representation for all the existentially
7321 * quantified variables in "bset" by computing their lexicographic
7324 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7325 __isl_take isl_basic_set
*bset
)
7327 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7330 /* Given a basic map with one output dimension, compute the minimum or
7331 * maximum of that dimension as an isl_pw_aff.
7333 * Compute the optimum as a lexicographic optimum over the single
7334 * output dimension and extract the single isl_pw_aff from the result.
7336 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7339 isl_pw_multi_aff
*pma
;
7342 bmap
= isl_basic_map_copy(bmap
);
7343 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7344 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7345 isl_pw_multi_aff_free(pma
);
7350 /* Compute the minimum or maximum of the given output dimension
7351 * as a function of the parameters and the input dimensions,
7352 * but independently of the other output dimensions.
7354 * We first project out the other output dimension and then compute
7355 * the "lexicographic" maximum in each basic map, combining the results
7356 * using isl_pw_aff_union_max.
7358 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7365 n_out
= isl_map_dim(map
, isl_dim_out
);
7367 map
= isl_map_free(map
);
7368 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7369 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7374 isl_space
*space
= isl_map_get_space(map
);
7376 return isl_pw_aff_empty(space
);
7379 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7380 for (i
= 1; i
< map
->n
; ++i
) {
7381 isl_pw_aff
*pwaff_i
;
7383 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7384 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7392 /* Compute the minimum of the given output dimension as a function of the
7393 * parameters and input dimensions, but independently of
7394 * the other output dimensions.
7396 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7398 return map_dim_opt(map
, pos
, 0);
7401 /* Compute the maximum of the given output dimension as a function of the
7402 * parameters and input dimensions, but independently of
7403 * the other output dimensions.
7405 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7407 return map_dim_opt(map
, pos
, 1);
7410 /* Compute the minimum or maximum of the given set dimension
7411 * as a function of the parameters,
7412 * but independently of the other set dimensions.
7414 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7417 return map_dim_opt(set
, pos
, max
);
7420 /* Compute the maximum of the given set dimension as a function of the
7421 * parameters, but independently of the other set dimensions.
7423 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7425 return set_dim_opt(set
, pos
, 1);
7428 /* Compute the minimum of the given set dimension as a function of the
7429 * parameters, but independently of the other set dimensions.
7431 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7433 return set_dim_opt(set
, pos
, 0);
7436 /* Apply a preimage specified by "mat" on the parameters of "bset".
7437 * bset is assumed to have only parameters and divs.
7439 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7440 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7444 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7445 if (nparam
< 0 || !mat
)
7448 bset
->dim
= isl_space_cow(bset
->dim
);
7452 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7454 bset
->dim
->nparam
= 0;
7455 bset
->dim
->n_out
= nparam
;
7456 bset
= isl_basic_set_preimage(bset
, mat
);
7458 bset
->dim
->nparam
= bset
->dim
->n_out
;
7459 bset
->dim
->n_out
= 0;
7464 isl_basic_set_free(bset
);
7468 /* Apply a preimage specified by "mat" on the parameters of "set".
7469 * set is assumed to have only parameters and divs.
7471 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7472 __isl_take isl_mat
*mat
)
7477 nparam
= isl_set_dim(set
, isl_dim_param
);
7478 if (nparam
< 0 || !mat
)
7481 if (mat
->n_row
!= 1 + nparam
)
7482 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7483 "unexpected number of rows", goto error
);
7485 space
= isl_set_get_space(set
);
7486 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7487 isl_dim_param
, 0, nparam
);
7488 set
= isl_set_reset_space(set
, space
);
7489 set
= isl_set_preimage(set
, mat
);
7490 nparam
= isl_set_dim(set
, isl_dim_out
);
7492 set
= isl_set_free(set
);
7493 space
= isl_set_get_space(set
);
7494 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7495 isl_dim_out
, 0, nparam
);
7496 set
= isl_set_reset_space(set
, space
);
7504 /* Intersect the basic set "bset" with the affine space specified by the
7505 * equalities in "eq".
7507 static __isl_give isl_basic_set
*basic_set_append_equalities(
7508 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7516 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7520 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7521 for (i
= 0; i
< eq
->n_row
; ++i
) {
7522 k
= isl_basic_set_alloc_equality(bset
);
7525 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7526 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7530 bset
= isl_basic_set_gauss(bset
, NULL
);
7531 bset
= isl_basic_set_finalize(bset
);
7536 isl_basic_set_free(bset
);
7540 /* Intersect the set "set" with the affine space specified by the
7541 * equalities in "eq".
7543 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7544 __isl_take isl_mat
*eq
)
7551 for (i
= 0; i
< set
->n
; ++i
) {
7552 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7565 /* Given a basic set "bset" that only involves parameters and existentially
7566 * quantified variables, return the index of the first equality
7567 * that only involves parameters. If there is no such equality then
7568 * return bset->n_eq.
7570 * This function assumes that isl_basic_set_gauss has been called on "bset".
7572 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7575 isl_size nparam
, n_div
;
7577 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7578 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7579 if (nparam
< 0 || n_div
< 0)
7582 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7583 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7590 /* Compute an explicit representation for the existentially quantified
7591 * variables in "bset" by computing the "minimal value" of the set
7592 * variables. Since there are no set variables, the computation of
7593 * the minimal value essentially computes an explicit representation
7594 * of the non-empty part(s) of "bset".
7596 * The input only involves parameters and existentially quantified variables.
7597 * All equalities among parameters have been removed.
7599 * Since the existentially quantified variables in the result are in general
7600 * going to be different from those in the input, we first replace
7601 * them by the minimal number of variables based on their equalities.
7602 * This should simplify the parametric integer programming.
7604 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7606 isl_morph
*morph1
, *morph2
;
7612 if (bset
->n_eq
== 0)
7613 return isl_basic_set_lexmin_compute_divs(bset
);
7615 morph1
= isl_basic_set_parameter_compression(bset
);
7616 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7617 bset
= isl_basic_set_lift(bset
);
7618 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7619 bset
= isl_morph_basic_set(morph2
, bset
);
7620 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7622 bset
= isl_basic_set_free(bset
);
7623 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7625 set
= isl_basic_set_lexmin_compute_divs(bset
);
7627 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7632 /* Project the given basic set onto its parameter domain, possibly introducing
7633 * new, explicit, existential variables in the constraints.
7634 * The input has parameters and (possibly implicit) existential variables.
7635 * The output has the same parameters, but only
7636 * explicit existentially quantified variables.
7638 * The actual projection is performed by pip, but pip doesn't seem
7639 * to like equalities very much, so we first remove the equalities
7640 * among the parameters by performing a variable compression on
7641 * the parameters. Afterward, an inverse transformation is performed
7642 * and the equalities among the parameters are inserted back in.
7644 * The variable compression on the parameters may uncover additional
7645 * equalities that were only implicit before. We therefore check
7646 * if there are any new parameter equalities in the result and
7647 * if so recurse. The removal of parameter equalities is required
7648 * for the parameter compression performed by base_compute_divs.
7650 static __isl_give isl_set
*parameter_compute_divs(
7651 __isl_take isl_basic_set
*bset
)
7655 struct isl_mat
*T
, *T2
;
7656 struct isl_set
*set
;
7659 bset
= isl_basic_set_cow(bset
);
7663 if (bset
->n_eq
== 0)
7664 return base_compute_divs(bset
);
7666 bset
= isl_basic_set_gauss(bset
, NULL
);
7669 if (isl_basic_set_plain_is_empty(bset
))
7670 return isl_set_from_basic_set(bset
);
7672 i
= first_parameter_equality(bset
);
7673 if (i
== bset
->n_eq
)
7674 return base_compute_divs(bset
);
7676 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7678 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7679 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7681 eq
= isl_mat_cow(eq
);
7682 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7683 if (T
&& T
->n_col
== 0) {
7687 bset
= isl_basic_set_set_to_empty(bset
);
7688 return isl_set_from_basic_set(bset
);
7690 bset
= basic_set_parameter_preimage(bset
, T
);
7692 i
= first_parameter_equality(bset
);
7695 else if (i
== bset
->n_eq
)
7696 set
= base_compute_divs(bset
);
7698 set
= parameter_compute_divs(bset
);
7699 set
= set_parameter_preimage(set
, T2
);
7700 set
= set_append_equalities(set
, eq
);
7704 /* Insert the divs from "ls" before those of "bmap".
7706 * The number of columns is not changed, which means that the last
7707 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7708 * The caller is responsible for removing the same number of dimensions
7709 * from the space of "bmap".
7711 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7712 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7718 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7720 return isl_basic_map_free(bmap
);
7724 old_n_div
= bmap
->n_div
;
7725 bmap
= insert_div_rows(bmap
, n_div
);
7729 for (i
= 0; i
< n_div
; ++i
) {
7730 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7731 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7737 /* Replace the space of "bmap" by the space and divs of "ls".
7739 * If "ls" has any divs, then we simplify the result since we may
7740 * have discovered some additional equalities that could simplify
7741 * the div expressions.
7743 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7744 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7748 bmap
= isl_basic_map_cow(bmap
);
7749 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7750 if (!bmap
|| n_div
< 0)
7753 bmap
= insert_divs_from_local_space(bmap
, ls
);
7757 isl_space_free(bmap
->dim
);
7758 bmap
->dim
= isl_local_space_get_space(ls
);
7762 isl_local_space_free(ls
);
7764 bmap
= isl_basic_map_simplify(bmap
);
7765 bmap
= isl_basic_map_finalize(bmap
);
7768 isl_basic_map_free(bmap
);
7769 isl_local_space_free(ls
);
7773 /* Replace the space of "map" by the space and divs of "ls".
7775 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7776 __isl_take isl_local_space
*ls
)
7780 map
= isl_map_cow(map
);
7784 for (i
= 0; i
< map
->n
; ++i
) {
7785 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7786 isl_local_space_copy(ls
));
7790 isl_space_free(isl_map_take_space(map
));
7791 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7793 isl_local_space_free(ls
);
7796 isl_local_space_free(ls
);
7801 /* Compute an explicit representation for the existentially
7802 * quantified variables for which do not know any explicit representation yet.
7804 * We first sort the existentially quantified variables so that the
7805 * existentially quantified variables for which we already have an explicit
7806 * representation are placed before those for which we do not.
7807 * The input dimensions, the output dimensions and the existentially
7808 * quantified variables for which we already have an explicit
7809 * representation are then turned into parameters.
7810 * compute_divs returns a map with the same parameters and
7811 * no input or output dimensions and the dimension specification
7812 * is reset to that of the input, including the existentially quantified
7813 * variables for which we already had an explicit representation.
7815 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7817 struct isl_basic_set
*bset
;
7818 struct isl_set
*set
;
7819 struct isl_map
*map
;
7821 isl_local_space
*ls
;
7828 bmap
= isl_basic_map_sort_divs(bmap
);
7829 bmap
= isl_basic_map_cow(bmap
);
7833 n_known
= isl_basic_map_first_unknown_div(bmap
);
7834 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7835 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7836 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7837 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7838 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7840 space
= isl_space_set_alloc(bmap
->ctx
,
7841 nparam
+ n_in
+ n_out
+ n_known
, 0);
7845 ls
= isl_basic_map_get_local_space(bmap
);
7846 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7847 n_known
, bmap
->n_div
- n_known
);
7849 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7850 swap_div(bmap
, i
- n_known
, i
);
7851 bmap
->n_div
-= n_known
;
7852 bmap
->extra
-= n_known
;
7854 bmap
= isl_basic_map_reset_space(bmap
, space
);
7855 bset
= bset_from_bmap(bmap
);
7857 set
= parameter_compute_divs(bset
);
7858 map
= set_to_map(set
);
7859 map
= replace_space_by_local_space(map
, ls
);
7863 isl_basic_map_free(bmap
);
7867 /* Remove the explicit representation of local variable "div",
7870 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7871 __isl_take isl_basic_map
*bmap
, int div
)
7875 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7877 return isl_basic_map_free(bmap
);
7881 bmap
= isl_basic_map_cow(bmap
);
7884 isl_int_set_si(bmap
->div
[div
][0], 0);
7888 /* Is local variable "div" of "bmap" marked as not having an explicit
7890 * Note that even if "div" is not marked in this way and therefore
7891 * has an explicit representation, this representation may still
7892 * depend (indirectly) on other local variables that do not
7893 * have an explicit representation.
7895 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7898 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7899 return isl_bool_error
;
7900 return isl_int_is_zero(bmap
->div
[div
][0]);
7903 /* Return the position of the first local variable that does not
7904 * have an explicit representation.
7905 * Return the total number of local variables if they all have
7906 * an explicit representation.
7907 * Return -1 on error.
7909 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7916 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7917 if (!isl_basic_map_div_is_known(bmap
, i
))
7923 /* Return the position of the first local variable that does not
7924 * have an explicit representation.
7925 * Return the total number of local variables if they all have
7926 * an explicit representation.
7927 * Return -1 on error.
7929 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7931 return isl_basic_map_first_unknown_div(bset
);
7934 /* Does "bmap" have an explicit representation for all local variables?
7936 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7941 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7942 first
= isl_basic_map_first_unknown_div(bmap
);
7943 if (n
< 0 || first
< 0)
7944 return isl_bool_error
;
7948 /* Do all basic maps in "map" have an explicit representation
7949 * for all local variables?
7951 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7956 return isl_bool_error
;
7958 for (i
= 0; i
< map
->n
; ++i
) {
7959 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7964 return isl_bool_true
;
7967 /* If bmap contains any unknown divs, then compute explicit
7968 * expressions for them. However, this computation may be
7969 * quite expensive, so first try to remove divs that aren't
7972 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7975 struct isl_map
*map
;
7977 known
= isl_basic_map_divs_known(bmap
);
7981 return isl_map_from_basic_map(bmap
);
7983 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7985 known
= isl_basic_map_divs_known(bmap
);
7989 return isl_map_from_basic_map(bmap
);
7991 map
= compute_divs(bmap
);
7994 isl_basic_map_free(bmap
);
7998 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8002 struct isl_map
*res
;
8009 known
= isl_map_divs_known(map
);
8017 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8018 for (i
= 1 ; i
< map
->n
; ++i
) {
8020 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8021 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8022 res
= isl_map_union_disjoint(res
, r2
);
8024 res
= isl_map_union(res
, r2
);
8031 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8033 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8036 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8038 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8041 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8046 n_out
= isl_map_dim(map
, isl_dim_out
);
8048 return set_from_map(isl_map_free(map
));
8049 space
= isl_space_domain(isl_map_get_space(map
));
8051 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8053 return set_from_map(isl_map_reset_space(map
, space
));
8056 /* Return the union of "map1" and "map2", where we assume for now that
8057 * "map1" and "map2" are disjoint. Note that the basic maps inside
8058 * "map1" or "map2" may not be disjoint from each other.
8059 * Also note that this function is also called from isl_map_union,
8060 * which takes care of handling the situation where "map1" and "map2"
8061 * may not be disjoint.
8063 * If one of the inputs is empty, we can simply return the other input.
8064 * Similarly, if one of the inputs is universal, then it is equal to the union.
8066 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8067 __isl_take isl_map
*map2
)
8071 struct isl_map
*map
= NULL
;
8074 if (isl_map_check_equal_space(map1
, map2
) < 0)
8086 is_universe
= isl_map_plain_is_universe(map1
);
8087 if (is_universe
< 0)
8094 is_universe
= isl_map_plain_is_universe(map2
);
8095 if (is_universe
< 0)
8102 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8103 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8104 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8106 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8107 map1
->n
+ map2
->n
, flags
);
8110 for (i
= 0; i
< map1
->n
; ++i
) {
8111 map
= isl_map_add_basic_map(map
,
8112 isl_basic_map_copy(map1
->p
[i
]));
8116 for (i
= 0; i
< map2
->n
; ++i
) {
8117 map
= isl_map_add_basic_map(map
,
8118 isl_basic_map_copy(map2
->p
[i
]));
8132 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8133 * guaranteed to be disjoint by the caller.
8135 * Note that this functions is called from within isl_map_make_disjoint,
8136 * so we have to be careful not to touch the constraints of the inputs
8139 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8140 __isl_take isl_map
*map2
)
8142 isl_map_align_params_bin(&map1
, &map2
);
8143 return map_union_disjoint(map1
, map2
);
8146 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8149 * We currently simply call map_union_disjoint, the internal operation
8150 * of which does not really depend on the inputs being disjoint.
8151 * If the result contains more than one basic map, then we clear
8152 * the disjoint flag since the result may contain basic maps from
8153 * both inputs and these are not guaranteed to be disjoint.
8155 * As a special case, if "map1" and "map2" are obviously equal,
8156 * then we simply return "map1".
8158 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8159 __isl_take isl_map
*map2
)
8163 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8166 equal
= isl_map_plain_is_equal(map1
, map2
);
8174 map1
= map_union_disjoint(map1
, map2
);
8178 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8186 __isl_give isl_set
*isl_set_union_disjoint(
8187 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8189 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8193 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8194 __isl_take isl_set
*set2
)
8196 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8199 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8200 * the results in a map living in "space".
8202 * "map" and "set" are assumed to be compatible and non-NULL.
8204 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8205 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8206 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8207 __isl_take isl_basic_set
*bset
))
8210 struct isl_map
*result
;
8213 if (isl_set_plain_is_universe(set
)) {
8215 return isl_map_reset_equal_dim_space(map
, space
);
8218 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8219 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8220 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8222 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8223 for (i
= 0; result
&& i
< map
->n
; ++i
)
8224 for (j
= 0; j
< set
->n
; ++j
) {
8225 result
= isl_map_add_basic_map(result
,
8226 fn(isl_basic_map_copy(map
->p
[i
]),
8227 isl_basic_set_copy(set
->p
[j
])));
8237 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8238 __isl_take isl_set
*set
)
8243 isl_map_align_params_set(&map
, &set
);
8244 ok
= isl_map_compatible_range(map
, set
);
8248 isl_die(set
->ctx
, isl_error_invalid
,
8249 "incompatible spaces", goto error
);
8251 space
= isl_map_get_space(map
);
8252 return map_intersect_set(map
, space
, set
,
8253 &isl_basic_map_intersect_range
);
8260 /* Intersect the domain of "map" with "set".
8262 * If the domain dimensions of "map" do not have any identifiers,
8263 * then copy them over from "set".
8265 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8266 __isl_take isl_set
*set
)
8271 isl_map_align_params_set(&map
, &set
);
8272 ok
= isl_map_compatible_domain(map
, set
);
8276 isl_die(set
->ctx
, isl_error_invalid
,
8277 "incompatible spaces", goto error
);
8279 space
= isl_map_get_space(map
);
8280 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8281 isl_set_peek_space(set
), isl_dim_set
);
8282 return map_intersect_set(map
, space
, set
,
8283 &isl_basic_map_intersect_domain
);
8290 /* Data structure that specifies how isl_map_intersect_factor
8293 * "preserve_type" is the tuple where the factor differs from
8294 * the input map and of which the identifiers needs
8295 * to be preserved explicitly.
8296 * "other_factor" is used to extract the space of the other factor
8297 * from the space of the product ("map").
8298 * "product" is used to combine the given factor and a universe map
8299 * in the space returned by "other_factor" to produce a map
8300 * that lives in the same space as the input map.
8302 struct isl_intersect_factor_control
{
8303 enum isl_dim_type preserve_type
;
8304 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8305 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8306 __isl_take isl_map
*other
);
8309 /* Given a map "map" in some product space and a map "factor"
8310 * living in some factor space, return the intersection.
8312 * After aligning the parameters,
8313 * the map "factor" is first extended to a map living in the same space
8314 * as "map" and then a regular intersection is computed.
8316 * Note that the extension is computed as a product, which is anonymous
8317 * by default. If "map" has an identifier on the corresponding tuple,
8318 * then this identifier needs to be set on the product
8319 * before the intersection is computed.
8321 static __isl_give isl_map
*isl_map_intersect_factor(
8322 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8323 struct isl_intersect_factor_control
*control
)
8325 isl_bool equal
, has_id
;
8328 isl_map
*other
, *product
;
8330 equal
= isl_map_has_equal_params(map
, factor
);
8334 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8335 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8338 space
= isl_map_get_space(map
);
8339 has_id
= isl_space_has_tuple_id(space
, control
->preserve_type
);
8341 space
= isl_space_free(space
);
8343 id
= isl_space_get_tuple_id(space
, control
->preserve_type
);
8345 other
= isl_map_universe(control
->other_factor(space
));
8346 product
= control
->product(factor
, other
);
8348 if (has_id
>= 0 && has_id
)
8349 product
= isl_map_set_tuple_id(product
,
8350 control
->preserve_type
, id
);
8352 return map_intersect(map
, product
);
8355 isl_map_free(factor
);
8359 /* Return the domain product of "map2" and "map1".
8361 static __isl_give isl_map
*isl_map_reverse_domain_product(
8362 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8364 return isl_map_domain_product(map2
, map1
);
8367 /* Return the range product of "map2" and "map1".
8369 static __isl_give isl_map
*isl_map_reverse_range_product(
8370 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8372 return isl_map_range_product(map2
, map1
);
8375 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8376 * in the space A -> C, return the intersection.
8378 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8379 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8381 struct isl_intersect_factor_control control
= {
8382 .preserve_type
= isl_dim_in
,
8383 .other_factor
= isl_space_domain_factor_range
,
8384 .product
= isl_map_domain_product
,
8387 return isl_map_intersect_factor(map
, factor
, &control
);
8390 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8391 * in the space B -> C, return the intersection.
8393 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8394 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8396 struct isl_intersect_factor_control control
= {
8397 .preserve_type
= isl_dim_in
,
8398 .other_factor
= isl_space_domain_factor_domain
,
8399 .product
= isl_map_reverse_domain_product
,
8402 return isl_map_intersect_factor(map
, factor
, &control
);
8405 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8406 * in the space A -> B, return the intersection.
8408 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8409 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8411 struct isl_intersect_factor_control control
= {
8412 .preserve_type
= isl_dim_out
,
8413 .other_factor
= isl_space_range_factor_range
,
8414 .product
= isl_map_range_product
,
8417 return isl_map_intersect_factor(map
, factor
, &control
);
8420 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8421 * in the space A -> C, return the intersection.
8423 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8424 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8426 struct isl_intersect_factor_control control
= {
8427 .preserve_type
= isl_dim_out
,
8428 .other_factor
= isl_space_range_factor_domain
,
8429 .product
= isl_map_reverse_range_product
,
8432 return isl_map_intersect_factor(map
, factor
, &control
);
8435 /* Given a set "set" in a space [A -> B] and a set "domain"
8436 * in the space A, return the intersection.
8438 * The set "domain" is first extended to a set living in the space
8439 * [A -> B] and then a regular intersection is computed.
8441 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8442 __isl_take isl_set
*domain
)
8444 struct isl_intersect_factor_control control
= {
8445 .preserve_type
= isl_dim_set
,
8446 .other_factor
= isl_space_factor_range
,
8447 .product
= isl_map_range_product
,
8450 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8451 set_to_map(domain
), &control
));
8454 /* Given a set "set" in a space [A -> B] and a set "range"
8455 * in the space B, return the intersection.
8457 * The set "range" is first extended to a set living in the space
8458 * [A -> B] and then a regular intersection is computed.
8460 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8461 __isl_take isl_set
*range
)
8463 struct isl_intersect_factor_control control
= {
8464 .preserve_type
= isl_dim_set
,
8465 .other_factor
= isl_space_factor_domain
,
8466 .product
= isl_map_reverse_range_product
,
8469 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8470 set_to_map(range
), &control
));
8473 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8474 __isl_take isl_map
*map2
)
8476 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8478 map1
= isl_map_reverse(map1
);
8479 map1
= isl_map_apply_range(map1
, map2
);
8480 return isl_map_reverse(map1
);
8487 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8488 __isl_take isl_map
*map2
)
8491 struct isl_map
*result
;
8494 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8497 space
= isl_space_join(isl_space_copy(map1
->dim
),
8498 isl_space_copy(map2
->dim
));
8500 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8503 for (i
= 0; i
< map1
->n
; ++i
)
8504 for (j
= 0; j
< map2
->n
; ++j
) {
8505 result
= isl_map_add_basic_map(result
,
8506 isl_basic_map_apply_range(
8507 isl_basic_map_copy(map1
->p
[i
]),
8508 isl_basic_map_copy(map2
->p
[j
])));
8514 if (result
&& result
->n
<= 1)
8515 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8523 /* Is "bmap" a transformation, i.e.,
8524 * does it relate elements from the same space.
8526 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8530 space
= isl_basic_map_peek_space(bmap
);
8531 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8534 /* Check that "bmap" is a transformation, i.e.,
8535 * that it relates elements from the same space.
8537 static isl_stat
isl_basic_map_check_transformation(
8538 __isl_keep isl_basic_map
*bmap
)
8542 equal
= isl_basic_map_is_transformation(bmap
);
8544 return isl_stat_error
;
8546 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8547 "domain and range don't match", return isl_stat_error
);
8552 * returns range - domain
8554 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8556 isl_space
*target_space
;
8557 struct isl_basic_set
*bset
;
8563 if (isl_basic_map_check_transformation(bmap
) < 0)
8564 return isl_basic_map_free(bmap
);
8565 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8566 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8567 if (dim
< 0 || nparam
< 0)
8569 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8570 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8571 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8572 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8574 bmap
= isl_basic_map_free(bmap
);
8575 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8576 for (i
= 0; i
< dim
; ++i
) {
8577 int j
= isl_basic_map_alloc_equality(bmap
);
8579 bmap
= isl_basic_map_free(bmap
);
8582 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8583 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8584 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8585 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8587 bset
= isl_basic_map_domain(bmap
);
8588 bset
= isl_basic_set_reset_space(bset
, target_space
);
8591 isl_basic_map_free(bmap
);
8595 /* Is the tuple of type "type1" of "map" the same as
8596 * the tuple of type "type2" of "space"?
8598 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8599 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8600 enum isl_dim_type type2
)
8602 isl_space
*map_space
;
8604 map_space
= isl_map_peek_space(map
);
8605 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8608 /* Is the tuple of type "type1" of "map1" the same as
8609 * the tuple of type "type2" of "map2"?
8611 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8612 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8613 enum isl_dim_type type2
)
8615 isl_space
*space1
, *space2
;
8617 space1
= isl_map_peek_space(map1
);
8618 space2
= isl_map_peek_space(map2
);
8619 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8622 /* Is the space of "obj" equal to "space", ignoring parameters?
8624 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8625 __isl_keep isl_space
*space
)
8627 isl_space
*map_space
;
8629 map_space
= isl_map_peek_space(map
);
8630 return isl_space_has_equal_tuples(map_space
, space
);
8633 /* Check that "map" is a transformation, i.e.,
8634 * that it relates elements from the same space.
8636 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8640 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8642 return isl_stat_error
;
8644 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8645 "domain and range don't match", return isl_stat_error
);
8650 * returns range - domain
8652 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8656 struct isl_set
*result
;
8658 if (isl_map_check_transformation(map
) < 0)
8660 space
= isl_map_get_space(map
);
8661 space
= isl_space_domain(space
);
8662 result
= isl_set_alloc_space(space
, map
->n
, 0);
8665 for (i
= 0; i
< map
->n
; ++i
)
8666 result
= isl_set_add_basic_set(result
,
8667 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8676 * returns [domain -> range] -> range - domain
8678 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8679 __isl_take isl_basic_map
*bmap
)
8683 isl_basic_map
*domain
;
8687 if (isl_basic_map_check_transformation(bmap
) < 0)
8688 return isl_basic_map_free(bmap
);
8690 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8691 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8692 if (nparam
< 0 || n
< 0)
8693 return isl_basic_map_free(bmap
);
8695 space
= isl_basic_map_get_space(bmap
);
8696 space
= isl_space_from_range(isl_space_domain(space
));
8697 domain
= isl_basic_map_universe(space
);
8699 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8700 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8701 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8703 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8705 return isl_basic_map_free(bmap
);
8707 for (i
= 0; i
< n
; ++i
) {
8708 k
= isl_basic_map_alloc_equality(bmap
);
8711 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8712 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8713 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8714 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8717 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8718 return isl_basic_map_finalize(bmap
);
8720 isl_basic_map_free(bmap
);
8725 * returns [domain -> range] -> range - domain
8727 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8729 if (isl_map_check_transformation(map
) < 0)
8730 return isl_map_free(map
);
8732 return isl_map_transform(map
, &isl_space_range_map
,
8733 &isl_basic_map_deltas_map
);
8736 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8738 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
8743 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
8744 map
= isl_map_deltas_map(isl_map_universe(space
));
8745 map
= isl_map_intersect_range(map
, deltas
);
8747 return isl_set_unwrap(isl_map_domain(map
));
8750 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8752 isl_size n_in
, n_out
;
8754 n_in
= isl_space_dim(space
, isl_dim_in
);
8755 n_out
= isl_space_dim(space
, isl_dim_out
);
8756 if (n_in
< 0 || n_out
< 0)
8759 isl_die(space
->ctx
, isl_error_invalid
,
8760 "number of input and output dimensions needs to be "
8761 "the same", goto error
);
8762 return isl_basic_map_equal(space
, n_in
);
8764 isl_space_free(space
);
8768 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8770 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8773 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8775 isl_space
*space
= isl_set_get_space(set
);
8777 id
= isl_map_identity(isl_space_map_from_set(space
));
8778 return isl_map_intersect_range(id
, set
);
8781 /* Construct a basic set with all set dimensions having only non-negative
8784 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8785 __isl_take isl_space
*space
)
8791 struct isl_basic_set
*bset
;
8793 nparam
= isl_space_dim(space
, isl_dim_param
);
8794 dim
= isl_space_dim(space
, isl_dim_set
);
8795 total
= isl_space_dim(space
, isl_dim_all
);
8796 if (nparam
< 0 || dim
< 0 || total
< 0)
8797 space
= isl_space_free(space
);
8798 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8801 for (i
= 0; i
< dim
; ++i
) {
8802 int k
= isl_basic_set_alloc_inequality(bset
);
8805 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8806 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8810 isl_basic_set_free(bset
);
8814 /* Construct the half-space x_pos >= 0.
8816 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8821 isl_basic_set
*nonneg
;
8823 total
= isl_space_dim(space
, isl_dim_all
);
8825 space
= isl_space_free(space
);
8826 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8827 k
= isl_basic_set_alloc_inequality(nonneg
);
8830 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8831 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8833 return isl_basic_set_finalize(nonneg
);
8835 isl_basic_set_free(nonneg
);
8839 /* Construct the half-space x_pos <= -1.
8841 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8848 total
= isl_space_dim(space
, isl_dim_all
);
8850 space
= isl_space_free(space
);
8851 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8852 k
= isl_basic_set_alloc_inequality(neg
);
8855 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8856 isl_int_set_si(neg
->ineq
[k
][0], -1);
8857 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8859 return isl_basic_set_finalize(neg
);
8861 isl_basic_set_free(neg
);
8865 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8866 enum isl_dim_type type
, unsigned first
, unsigned n
)
8870 isl_basic_set
*nonneg
;
8876 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8877 return isl_set_free(set
);
8879 offset
= pos(set
->dim
, type
);
8880 for (i
= 0; i
< n
; ++i
) {
8881 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8882 offset
+ first
+ i
);
8883 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8885 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8891 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8893 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8899 return isl_stat_error
;
8900 if (isl_set_plain_is_empty(set
)) {
8905 return fn(set
, signs
, user
);
8908 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8910 half
= isl_set_intersect(half
, isl_set_copy(set
));
8911 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8915 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8917 half
= isl_set_intersect(half
, set
);
8918 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8921 return isl_stat_error
;
8924 /* Call "fn" on the intersections of "set" with each of the orthants
8925 * (except for obviously empty intersections). The orthant is identified
8926 * by the signs array, with each entry having value 1 or -1 according
8927 * to the sign of the corresponding variable.
8929 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8930 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8939 return isl_stat_error
;
8940 if (isl_set_plain_is_empty(set
))
8943 nparam
= isl_set_dim(set
, isl_dim_param
);
8944 nvar
= isl_set_dim(set
, isl_dim_set
);
8945 if (nparam
< 0 || nvar
< 0)
8946 return isl_stat_error
;
8948 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8950 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8958 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8960 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8963 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8964 __isl_keep isl_basic_map
*bmap2
)
8967 struct isl_map
*map1
;
8968 struct isl_map
*map2
;
8970 if (!bmap1
|| !bmap2
)
8971 return isl_bool_error
;
8973 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8974 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8976 is_subset
= isl_map_is_subset(map1
, map2
);
8984 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8985 __isl_keep isl_basic_set
*bset2
)
8987 return isl_basic_map_is_subset(bset1
, bset2
);
8990 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8991 __isl_keep isl_basic_map
*bmap2
)
8995 if (!bmap1
|| !bmap2
)
8996 return isl_bool_error
;
8997 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8998 if (is_subset
!= isl_bool_true
)
9000 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9004 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9005 __isl_keep isl_basic_set
*bset2
)
9007 return isl_basic_map_is_equal(
9008 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9011 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9017 return isl_bool_error
;
9018 for (i
= 0; i
< map
->n
; ++i
) {
9019 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9021 return isl_bool_error
;
9023 return isl_bool_false
;
9025 return isl_bool_true
;
9028 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9030 return map
? map
->n
== 0 : isl_bool_error
;
9033 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9035 return set
? set
->n
== 0 : isl_bool_error
;
9038 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9040 return isl_map_is_empty(set_to_map(set
));
9044 #define TYPE isl_basic_map
9047 #include "isl_type_has_equal_space_bin_templ.c"
9048 #include "isl_type_check_equal_space_templ.c"
9050 /* Check that "bset1" and "bset2" live in the same space,
9051 * reporting an error if they do not.
9053 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9054 __isl_keep isl_basic_set
*bset2
)
9056 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9057 bset_to_bmap(bset1
));
9061 #define TYPE isl_map
9063 #include "isl_type_has_equal_space_bin_templ.c"
9064 #include "isl_type_check_equal_space_templ.c"
9065 #include "isl_type_has_space_templ.c"
9067 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9068 __isl_keep isl_set
*set2
)
9070 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9074 #define TYPE1 isl_map
9076 #define TYPE2 isl_basic_map
9078 #define TYPE_PAIR isl_map_basic_map
9081 #include "isl_type_has_equal_space_templ.c"
9082 #include "isl_type_check_equal_space_templ.c"
9084 /* Check that "set" and "bset" live in the same space,
9085 * reporting an error if they do not.
9087 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9088 __isl_keep isl_basic_set
*bset
)
9090 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9091 bset_to_bmap(bset
));
9094 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9099 return isl_bool_error
;
9100 is_subset
= isl_map_is_subset(map1
, map2
);
9101 if (is_subset
!= isl_bool_true
)
9103 is_subset
= isl_map_is_subset(map2
, map1
);
9107 /* Is "map1" equal to "map2"?
9109 * First check if they are obviously equal.
9110 * If not, then perform a more detailed analysis.
9112 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9116 equal
= isl_map_plain_is_equal(map1
, map2
);
9117 if (equal
< 0 || equal
)
9119 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9122 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9123 __isl_keep isl_basic_map
*bmap2
)
9127 if (!bmap1
|| !bmap2
)
9128 return isl_bool_error
;
9129 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9130 if (is_subset
!= isl_bool_true
)
9132 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9133 return isl_bool_not(is_subset
);
9136 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9137 __isl_keep isl_map
*map2
)
9142 return isl_bool_error
;
9143 is_subset
= isl_map_is_subset(map1
, map2
);
9144 if (is_subset
!= isl_bool_true
)
9146 is_subset
= isl_map_is_subset(map2
, map1
);
9147 return isl_bool_not(is_subset
);
9150 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9151 __isl_keep isl_set
*set2
)
9153 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9156 /* Is "bmap" obviously equal to the universe with the same space?
9158 * That is, does it not have any constraints?
9160 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9163 return isl_bool_error
;
9164 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9167 /* Is "bset" obviously equal to the universe with the same space?
9169 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9171 return isl_basic_map_plain_is_universe(bset
);
9174 /* If "c" does not involve any existentially quantified variables,
9175 * then set *univ to false and abort
9177 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9179 isl_bool
*univ
= user
;
9182 n
= isl_constraint_dim(c
, isl_dim_div
);
9184 c
= isl_constraint_free(c
);
9185 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9186 isl_constraint_free(c
);
9187 if (*univ
< 0 || !*univ
)
9188 return isl_stat_error
;
9192 /* Is "bmap" equal to the universe with the same space?
9194 * First check if it is obviously equal to the universe.
9195 * If not and if there are any constraints not involving
9196 * existentially quantified variables, then it is certainly
9197 * not equal to the universe.
9198 * Otherwise, check if the universe is a subset of "bmap".
9200 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9204 isl_basic_map
*test
;
9206 univ
= isl_basic_map_plain_is_universe(bmap
);
9207 if (univ
< 0 || univ
)
9209 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9211 return isl_bool_error
;
9213 return isl_bool_false
;
9214 univ
= isl_bool_true
;
9215 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9217 return isl_bool_error
;
9218 if (univ
< 0 || !univ
)
9220 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9221 univ
= isl_basic_map_is_subset(test
, bmap
);
9222 isl_basic_map_free(test
);
9226 /* Is "bset" equal to the universe with the same space?
9228 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9230 return isl_basic_map_is_universe(bset
);
9233 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9238 return isl_bool_error
;
9240 for (i
= 0; i
< map
->n
; ++i
) {
9241 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9246 return isl_bool_false
;
9249 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9251 return isl_map_plain_is_universe(set_to_map(set
));
9254 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9256 struct isl_basic_set
*bset
= NULL
;
9257 struct isl_vec
*sample
= NULL
;
9258 isl_bool empty
, non_empty
;
9261 return isl_bool_error
;
9263 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9264 return isl_bool_true
;
9266 if (isl_basic_map_plain_is_universe(bmap
))
9267 return isl_bool_false
;
9269 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9270 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9271 copy
= isl_basic_map_remove_redundancies(copy
);
9272 empty
= isl_basic_map_plain_is_empty(copy
);
9273 isl_basic_map_free(copy
);
9277 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9279 return isl_bool_error
;
9281 return isl_bool_false
;
9282 isl_vec_free(bmap
->sample
);
9283 bmap
->sample
= NULL
;
9284 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9286 return isl_bool_error
;
9287 sample
= isl_basic_set_sample_vec(bset
);
9289 return isl_bool_error
;
9290 empty
= sample
->size
== 0;
9291 isl_vec_free(bmap
->sample
);
9292 bmap
->sample
= sample
;
9294 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9299 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9302 return isl_bool_error
;
9303 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9306 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9309 return isl_bool_error
;
9310 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9313 /* Is "bmap" known to be non-empty?
9315 * That is, is the cached sample still valid?
9317 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9322 return isl_bool_error
;
9324 return isl_bool_false
;
9325 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9327 return isl_bool_error
;
9328 if (bmap
->sample
->size
!= 1 + total
)
9329 return isl_bool_false
;
9330 return isl_basic_map_contains(bmap
, bmap
->sample
);
9333 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9335 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9338 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9339 __isl_take isl_basic_map
*bmap2
)
9341 struct isl_map
*map
;
9343 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9346 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9349 map
= isl_map_add_basic_map(map
, bmap1
);
9350 map
= isl_map_add_basic_map(map
, bmap2
);
9353 isl_basic_map_free(bmap1
);
9354 isl_basic_map_free(bmap2
);
9358 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9359 __isl_take isl_basic_set
*bset2
)
9361 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9362 bset_to_bmap(bset2
)));
9365 /* Order divs such that any div only depends on previous divs */
9366 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9367 __isl_take isl_basic_map
*bmap
)
9372 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9374 return isl_basic_map_free(bmap
);
9376 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9378 if (isl_int_is_zero(bmap
->div
[i
][0]))
9380 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9385 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9386 "integer division depends on itself",
9387 return isl_basic_map_free(bmap
));
9388 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9396 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9403 for (i
= 0; i
< map
->n
; ++i
) {
9404 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9415 /* Sort the local variables of "bset".
9417 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9418 __isl_take isl_basic_set
*bset
)
9420 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9423 /* Apply the expansion computed by isl_merge_divs.
9424 * The expansion itself is given by "exp" while the resulting
9425 * list of divs is given by "div".
9427 * Move the integer divisions of "bmap" into the right position
9428 * according to "exp" and then introduce the additional integer
9429 * divisions, adding div constraints.
9430 * The moving should be done first to avoid moving coefficients
9431 * in the definitions of the extra integer divisions.
9433 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9434 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9439 bmap
= isl_basic_map_cow(bmap
);
9443 if (div
->n_row
< bmap
->n_div
)
9444 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9445 "not an expansion", goto error
);
9447 n_div
= bmap
->n_div
;
9448 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9449 2 * (div
->n_row
- n_div
));
9451 for (i
= n_div
; i
< div
->n_row
; ++i
)
9452 if (isl_basic_map_alloc_div(bmap
) < 0)
9455 for (j
= n_div
- 1; j
>= 0; --j
) {
9458 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9463 for (i
= 0; i
< div
->n_row
; ++i
) {
9464 if (j
< n_div
&& exp
[j
] == i
) {
9467 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9468 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9470 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9479 isl_basic_map_free(bmap
);
9484 /* Apply the expansion computed by isl_merge_divs.
9485 * The expansion itself is given by "exp" while the resulting
9486 * list of divs is given by "div".
9488 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9489 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9491 return isl_basic_map_expand_divs(bset
, div
, exp
);
9494 /* Look for a div in dst that corresponds to the div "div" in src.
9495 * The divs before "div" in src and dst are assumed to be the same.
9497 * Return the position of the corresponding div in dst
9498 * if there is one. Otherwise, return a position beyond the integer divisions.
9499 * Return -1 on error.
9501 static int find_div(__isl_keep isl_basic_map
*dst
,
9502 __isl_keep isl_basic_map
*src
, unsigned div
)
9508 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9509 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9510 if (n_div
< 0 || v_div
< 0)
9512 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9513 for (i
= div
; i
< n_div
; ++i
)
9514 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9515 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9521 /* Align the divs of "dst" to those of "src", adding divs from "src"
9522 * if needed. That is, make sure that the first src->n_div divs
9523 * of the result are equal to those of src.
9525 * The result is not finalized as by design it will have redundant
9526 * divs if any divs from "src" were copied.
9528 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9529 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9538 return isl_basic_map_free(dst
);
9540 if (src
->n_div
== 0)
9543 known
= isl_basic_map_divs_known(src
);
9545 return isl_basic_map_free(dst
);
9547 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9548 "some src divs are unknown",
9549 return isl_basic_map_free(dst
));
9551 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9553 return isl_basic_map_free(dst
);
9555 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
9557 return isl_basic_map_free(dst
);
9560 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9562 dst
= isl_basic_map_free(dst
);
9563 for (i
= 0; i
< src
->n_div
; ++i
) {
9564 int j
= find_div(dst
, src
, i
);
9566 dst
= isl_basic_map_free(dst
);
9567 if (j
== dst_n_div
) {
9569 int extra
= src
->n_div
- i
;
9570 dst
= isl_basic_map_cow(dst
);
9573 dst
= isl_basic_map_extend(dst
,
9574 extra
, 0, 2 * extra
);
9577 j
= isl_basic_map_alloc_div(dst
);
9580 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9581 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9583 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9588 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9592 isl_basic_map_free(src
);
9595 isl_basic_map_free(src
);
9596 isl_basic_map_free(dst
);
9600 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9608 map
= isl_map_compute_divs(map
);
9609 map
= isl_map_cow(map
);
9613 for (i
= 1; i
< map
->n
; ++i
)
9614 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9615 for (i
= 1; i
< map
->n
; ++i
) {
9616 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9618 return isl_map_free(map
);
9621 map
= isl_map_unmark_normalized(map
);
9625 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9627 return isl_map_align_divs_internal(map
);
9630 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9632 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9635 /* Align the divs of the basic maps in "map" to those
9636 * of the basic maps in "list", as well as to the other basic maps in "map".
9637 * The elements in "list" are assumed to have known divs.
9639 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9640 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9645 n
= isl_basic_map_list_n_basic_map(list
);
9646 map
= isl_map_compute_divs(map
);
9647 map
= isl_map_cow(map
);
9649 return isl_map_free(map
);
9653 for (i
= 0; i
< n
; ++i
) {
9654 isl_basic_map
*bmap
;
9656 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9657 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9658 isl_basic_map_free(bmap
);
9661 return isl_map_free(map
);
9663 return isl_map_align_divs_internal(map
);
9666 /* Align the divs of each element of "list" to those of "bmap".
9667 * Both "bmap" and the elements of "list" are assumed to have known divs.
9669 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9670 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9675 n
= isl_basic_map_list_n_basic_map(list
);
9677 return isl_basic_map_list_free(list
);
9679 for (i
= 0; i
< n
; ++i
) {
9680 isl_basic_map
*bmap_i
;
9682 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9683 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9684 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9690 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9691 __isl_take isl_map
*map
)
9695 isl_map_align_params_set(&map
, &set
);
9696 ok
= isl_map_compatible_domain(map
, set
);
9700 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9701 "incompatible spaces", goto error
);
9702 map
= isl_map_intersect_domain(map
, set
);
9703 set
= isl_map_range(map
);
9711 /* There is no need to cow as removing empty parts doesn't change
9712 * the meaning of the set.
9714 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9721 for (i
= map
->n
- 1; i
>= 0; --i
)
9722 map
= remove_if_empty(map
, i
);
9727 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9729 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9732 /* Create a binary relation that maps the shared initial "pos" dimensions
9733 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9735 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9736 __isl_keep isl_basic_set
*bset2
, int pos
)
9738 isl_basic_map
*bmap1
;
9739 isl_basic_map
*bmap2
;
9741 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9742 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9743 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9744 isl_dim_out
, 0, pos
);
9745 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9746 isl_dim_out
, 0, pos
);
9747 return isl_basic_map_range_product(bmap1
, bmap2
);
9750 /* Given two basic sets bset1 and bset2, compute the maximal difference
9751 * between the values of dimension pos in bset1 and those in bset2
9752 * for any common value of the parameters and dimensions preceding pos.
9754 static enum isl_lp_result
basic_set_maximal_difference_at(
9755 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9756 int pos
, isl_int
*opt
)
9758 isl_basic_map
*bmap1
;
9759 struct isl_ctx
*ctx
;
9760 struct isl_vec
*obj
;
9764 enum isl_lp_result res
;
9766 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9767 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9768 if (nparam
< 0 || dim1
< 0 || !bset2
)
9769 return isl_lp_error
;
9771 bmap1
= join_initial(bset1
, bset2
, pos
);
9772 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9774 return isl_lp_error
;
9777 obj
= isl_vec_alloc(ctx
, 1 + total
);
9780 isl_seq_clr(obj
->block
.data
, 1 + total
);
9781 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9782 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9783 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9785 isl_basic_map_free(bmap1
);
9789 isl_basic_map_free(bmap1
);
9790 return isl_lp_error
;
9793 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9794 * for any common value of the parameters and dimensions preceding pos
9795 * in both basic sets, the values of dimension pos in bset1 are
9796 * smaller or larger than those in bset2.
9799 * 1 if bset1 follows bset2
9800 * -1 if bset1 precedes bset2
9801 * 0 if bset1 and bset2 are incomparable
9802 * -2 if some error occurred.
9804 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9805 __isl_keep isl_basic_set
*bset2
, int pos
)
9808 enum isl_lp_result res
;
9813 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9815 if (res
== isl_lp_empty
)
9817 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9818 res
== isl_lp_unbounded
)
9820 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9829 /* Given two basic sets bset1 and bset2, check whether
9830 * for any common value of the parameters and dimensions preceding pos
9831 * there is a value of dimension pos in bset1 that is larger
9832 * than a value of the same dimension in bset2.
9835 * 1 if there exists such a pair
9836 * 0 if there is no such pair, but there is a pair of equal values
9838 * -2 if some error occurred.
9840 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9841 __isl_keep isl_basic_set
*bset2
, int pos
)
9844 isl_basic_map
*bmap
;
9847 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9850 bmap
= join_initial(bset1
, bset2
, pos
);
9851 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9852 isl_dim_out
, dim1
- pos
);
9853 empty
= isl_basic_map_is_empty(bmap
);
9857 isl_basic_map_free(bmap
);
9860 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9861 isl_dim_out
, dim1
- pos
);
9862 empty
= isl_basic_map_is_empty(bmap
);
9865 isl_basic_map_free(bmap
);
9870 isl_basic_map_free(bmap
);
9874 /* Given two sets set1 and set2, check whether
9875 * for any common value of the parameters and dimensions preceding pos
9876 * there is a value of dimension pos in set1 that is larger
9877 * than a value of the same dimension in set2.
9880 * 1 if there exists such a pair
9881 * 0 if there is no such pair, but there is a pair of equal values
9883 * -2 if some error occurred.
9885 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9886 __isl_keep isl_set
*set2
, int pos
)
9894 for (i
= 0; i
< set1
->n
; ++i
)
9895 for (j
= 0; j
< set2
->n
; ++j
) {
9897 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9898 if (f
== 1 || f
== -2)
9907 static isl_bool
isl_basic_map_plain_has_fixed_var(
9908 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9914 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9916 return isl_bool_error
;
9917 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9918 for (; d
+1 > pos
; --d
)
9919 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9923 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9924 return isl_bool_false
;
9925 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9926 return isl_bool_false
;
9927 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9928 return isl_bool_false
;
9930 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9931 return isl_bool_true
;
9933 return isl_bool_false
;
9936 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9937 unsigned pos
, isl_int
*val
)
9945 return isl_bool_error
;
9947 return isl_bool_false
;
9949 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9952 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9953 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9954 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9955 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9956 fixed
= isl_bool_false
;
9959 isl_int_set(*val
, v
);
9965 static isl_bool
isl_basic_set_plain_has_fixed_var(
9966 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9968 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9972 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9973 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9975 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9976 return isl_bool_error
;
9977 return isl_basic_map_plain_has_fixed_var(bmap
,
9978 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9981 /* If "bmap" obviously lies on a hyperplane where the given dimension
9982 * has a fixed value, then return that value.
9983 * Otherwise return NaN.
9985 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9986 __isl_keep isl_basic_map
*bmap
,
9987 enum isl_dim_type type
, unsigned pos
)
9995 ctx
= isl_basic_map_get_ctx(bmap
);
9996 v
= isl_val_alloc(ctx
);
9999 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10001 return isl_val_free(v
);
10003 isl_int_set_si(v
->d
, 1);
10007 return isl_val_nan(ctx
);
10010 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10011 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10013 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10014 return isl_bool_error
;
10015 return isl_map_plain_has_fixed_var(map
,
10016 map_offset(map
, type
) - 1 + pos
, val
);
10019 /* If "map" obviously lies on a hyperplane where the given dimension
10020 * has a fixed value, then return that value.
10021 * Otherwise return NaN.
10023 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10024 enum isl_dim_type type
, unsigned pos
)
10032 ctx
= isl_map_get_ctx(map
);
10033 v
= isl_val_alloc(ctx
);
10036 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10038 return isl_val_free(v
);
10040 isl_int_set_si(v
->d
, 1);
10044 return isl_val_nan(ctx
);
10047 /* If "set" obviously lies on a hyperplane where the given dimension
10048 * has a fixed value, then return that value.
10049 * Otherwise return NaN.
10051 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10052 enum isl_dim_type type
, unsigned pos
)
10054 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10057 /* Return a sequence of values in the same space as "set"
10058 * that are equal to the corresponding set dimensions of "set"
10059 * for those set dimensions that obviously lie on a hyperplane
10060 * where the dimension has a fixed value.
10061 * The other elements are set to NaN.
10063 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10064 __isl_keep isl_set
*set
)
10071 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10072 mv
= isl_multi_val_alloc(space
);
10073 n
= isl_multi_val_size(mv
);
10075 return isl_multi_val_free(mv
);
10077 for (i
= 0; i
< n
; ++i
) {
10080 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10081 mv
= isl_multi_val_set_val(mv
, i
, v
);
10087 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10088 * then return this fixed value in *val.
10090 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10091 unsigned dim
, isl_int
*val
)
10095 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10097 return isl_bool_error
;
10098 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10101 /* Return -1 if the constraint "c1" should be sorted before "c2"
10102 * and 1 if it should be sorted after "c2".
10103 * Return 0 if the two constraints are the same (up to the constant term).
10105 * In particular, if a constraint involves later variables than another
10106 * then it is sorted after this other constraint.
10107 * uset_gist depends on constraints without existentially quantified
10108 * variables sorting first.
10110 * For constraints that have the same latest variable, those
10111 * with the same coefficient for this latest variable (first in absolute value
10112 * and then in actual value) are grouped together.
10113 * This is useful for detecting pairs of constraints that can
10114 * be chained in their printed representation.
10116 * Finally, within a group, constraints are sorted according to
10117 * their coefficients (excluding the constant term).
10119 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10121 isl_int
**c1
= (isl_int
**) p1
;
10122 isl_int
**c2
= (isl_int
**) p2
;
10124 unsigned size
= *(unsigned *) arg
;
10127 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10128 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10133 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10136 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10140 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10143 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10144 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10145 * and 0 if the two constraints are the same (up to the constant term).
10147 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10148 isl_int
*c1
, isl_int
*c2
)
10153 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10157 return sort_constraint_cmp(&c1
, &c2
, &size
);
10160 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10161 __isl_take isl_basic_map
*bmap
)
10168 if (bmap
->n_ineq
== 0)
10170 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10172 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10174 return isl_basic_map_free(bmap
);
10176 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10177 &sort_constraint_cmp
, &size
) < 0)
10178 return isl_basic_map_free(bmap
);
10179 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10183 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10184 __isl_take isl_basic_set
*bset
)
10186 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10187 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10190 __isl_give isl_basic_map
*isl_basic_map_normalize(
10191 __isl_take isl_basic_map
*bmap
)
10193 bmap
= isl_basic_map_remove_redundancies(bmap
);
10194 bmap
= isl_basic_map_sort_constraints(bmap
);
10197 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10198 __isl_keep isl_basic_map
*bmap2
)
10202 isl_space
*space1
, *space2
;
10204 if (!bmap1
|| !bmap2
)
10207 if (bmap1
== bmap2
)
10209 space1
= isl_basic_map_peek_space(bmap1
);
10210 space2
= isl_basic_map_peek_space(bmap2
);
10211 cmp
= isl_space_cmp(space1
, space2
);
10214 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10215 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10216 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10217 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10218 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10220 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10222 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10224 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10225 return bmap1
->n_eq
- bmap2
->n_eq
;
10226 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10227 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10228 if (bmap1
->n_div
!= bmap2
->n_div
)
10229 return bmap1
->n_div
- bmap2
->n_div
;
10230 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10233 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10234 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10238 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10239 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10243 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10244 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10251 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10252 __isl_keep isl_basic_set
*bset2
)
10254 return isl_basic_map_plain_cmp(bset1
, bset2
);
10257 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10263 if (set1
->n
!= set2
->n
)
10264 return set1
->n
- set2
->n
;
10266 for (i
= 0; i
< set1
->n
; ++i
) {
10267 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10275 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10276 __isl_keep isl_basic_map
*bmap2
)
10278 if (!bmap1
|| !bmap2
)
10279 return isl_bool_error
;
10280 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10283 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10284 __isl_keep isl_basic_set
*bset2
)
10286 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10287 bset_to_bmap(bset2
));
10290 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10292 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10293 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10295 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10298 /* Sort the basic maps of "map" and remove duplicate basic maps.
10300 * While removing basic maps, we make sure that the basic maps remain
10301 * sorted because isl_map_normalize expects the basic maps of the result
10304 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10308 map
= isl_map_remove_empty_parts(map
);
10311 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10312 for (i
= map
->n
- 1; i
>= 1; --i
) {
10313 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10315 isl_basic_map_free(map
->p
[i
-1]);
10316 for (j
= i
; j
< map
->n
; ++j
)
10317 map
->p
[j
- 1] = map
->p
[j
];
10324 /* Remove obvious duplicates among the basic maps of "map".
10326 * Unlike isl_map_normalize, this function does not remove redundant
10327 * constraints and only removes duplicates that have exactly the same
10328 * constraints in the input. It does sort the constraints and
10329 * the basic maps to ease the detection of duplicates.
10331 * If "map" has already been normalized or if the basic maps are
10332 * disjoint, then there can be no duplicates.
10334 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10337 isl_basic_map
*bmap
;
10343 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10345 for (i
= 0; i
< map
->n
; ++i
) {
10346 bmap
= isl_basic_map_copy(map
->p
[i
]);
10347 bmap
= isl_basic_map_sort_constraints(bmap
);
10349 return isl_map_free(map
);
10350 isl_basic_map_free(map
->p
[i
]);
10354 map
= sort_and_remove_duplicates(map
);
10358 /* We normalize in place, but if anything goes wrong we need
10359 * to return NULL, so we need to make sure we don't change the
10360 * meaning of any possible other copies of map.
10362 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10365 struct isl_basic_map
*bmap
;
10369 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10371 for (i
= 0; i
< map
->n
; ++i
) {
10372 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10375 isl_basic_map_free(map
->p
[i
]);
10379 map
= sort_and_remove_duplicates(map
);
10381 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10388 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10390 return set_from_map(isl_map_normalize(set_to_map(set
)));
10393 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10394 __isl_keep isl_map
*map2
)
10399 if (!map1
|| !map2
)
10400 return isl_bool_error
;
10403 return isl_bool_true
;
10404 equal
= isl_map_has_equal_space(map1
, map2
);
10405 if (equal
< 0 || !equal
)
10408 map1
= isl_map_copy(map1
);
10409 map2
= isl_map_copy(map2
);
10410 map1
= isl_map_normalize(map1
);
10411 map2
= isl_map_normalize(map2
);
10412 if (!map1
|| !map2
)
10414 equal
= map1
->n
== map2
->n
;
10415 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10416 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10420 isl_map_free(map1
);
10421 isl_map_free(map2
);
10424 isl_map_free(map1
);
10425 isl_map_free(map2
);
10426 return isl_bool_error
;
10429 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10430 __isl_keep isl_set
*set2
)
10432 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10435 /* Return the basic maps in "map" as a list.
10437 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10438 __isl_keep isl_map
*map
)
10442 isl_basic_map_list
*list
;
10446 ctx
= isl_map_get_ctx(map
);
10447 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10449 for (i
= 0; i
< map
->n
; ++i
) {
10450 isl_basic_map
*bmap
;
10452 bmap
= isl_basic_map_copy(map
->p
[i
]);
10453 list
= isl_basic_map_list_add(list
, bmap
);
10459 /* Return the intersection of the elements in the non-empty list "list".
10460 * All elements are assumed to live in the same space.
10462 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10463 __isl_take isl_basic_map_list
*list
)
10467 isl_basic_map
*bmap
;
10469 n
= isl_basic_map_list_n_basic_map(list
);
10473 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10474 "expecting non-empty list", goto error
);
10476 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10477 for (i
= 1; i
< n
; ++i
) {
10478 isl_basic_map
*bmap_i
;
10480 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10481 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10484 isl_basic_map_list_free(list
);
10487 isl_basic_map_list_free(list
);
10491 /* Return the intersection of the elements in the non-empty list "list".
10492 * All elements are assumed to live in the same space.
10494 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10495 __isl_take isl_basic_set_list
*list
)
10497 return isl_basic_map_list_intersect(list
);
10500 /* Return the union of the elements of "list".
10501 * The list is required to have at least one element.
10503 __isl_give isl_set
*isl_basic_set_list_union(
10504 __isl_take isl_basic_set_list
*list
)
10509 isl_basic_set
*bset
;
10512 n
= isl_basic_set_list_n_basic_set(list
);
10516 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10517 "expecting non-empty list", goto error
);
10519 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10520 space
= isl_basic_set_get_space(bset
);
10521 isl_basic_set_free(bset
);
10523 set
= isl_set_alloc_space(space
, n
, 0);
10524 for (i
= 0; i
< n
; ++i
) {
10525 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10526 set
= isl_set_add_basic_set(set
, bset
);
10529 isl_basic_set_list_free(list
);
10532 isl_basic_set_list_free(list
);
10536 /* Return the union of the elements in the non-empty list "list".
10537 * All elements are assumed to live in the same space.
10539 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10545 n
= isl_set_list_n_set(list
);
10549 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10550 "expecting non-empty list", goto error
);
10552 set
= isl_set_list_get_set(list
, 0);
10553 for (i
= 1; i
< n
; ++i
) {
10556 set_i
= isl_set_list_get_set(list
, i
);
10557 set
= isl_set_union(set
, set_i
);
10560 isl_set_list_free(list
);
10563 isl_set_list_free(list
);
10567 __isl_give isl_basic_map
*isl_basic_map_product(
10568 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10570 isl_space
*space_result
= NULL
;
10571 struct isl_basic_map
*bmap
;
10572 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10573 struct isl_dim_map
*dim_map1
, *dim_map2
;
10575 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10577 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10578 isl_space_copy(bmap2
->dim
));
10580 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10581 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10582 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10583 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10584 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10586 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10587 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10588 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10589 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10590 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10591 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10592 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10593 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10594 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10595 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10596 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10598 bmap
= isl_basic_map_alloc_space(space_result
,
10599 bmap1
->n_div
+ bmap2
->n_div
,
10600 bmap1
->n_eq
+ bmap2
->n_eq
,
10601 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10602 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10603 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10604 bmap
= isl_basic_map_simplify(bmap
);
10605 return isl_basic_map_finalize(bmap
);
10607 isl_basic_map_free(bmap1
);
10608 isl_basic_map_free(bmap2
);
10612 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10613 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10615 isl_basic_map
*prod
;
10617 prod
= isl_basic_map_product(bmap1
, bmap2
);
10618 prod
= isl_basic_map_flatten(prod
);
10622 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10623 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10625 return isl_basic_map_flat_range_product(bset1
, bset2
);
10628 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10629 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10631 isl_space
*space1
, *space2
;
10632 isl_space
*space_result
= NULL
;
10633 isl_basic_map
*bmap
;
10634 isl_size in1
, in2
, out
, nparam
;
10635 unsigned total
, pos
;
10636 struct isl_dim_map
*dim_map1
, *dim_map2
;
10638 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10639 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10640 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10641 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10642 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10645 space1
= isl_basic_map_get_space(bmap1
);
10646 space2
= isl_basic_map_get_space(bmap2
);
10647 space_result
= isl_space_domain_product(space1
, space2
);
10649 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10650 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10651 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10652 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10653 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10654 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10655 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10656 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10657 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10658 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10659 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10661 bmap
= isl_basic_map_alloc_space(space_result
,
10662 bmap1
->n_div
+ bmap2
->n_div
,
10663 bmap1
->n_eq
+ bmap2
->n_eq
,
10664 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10665 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10666 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10667 bmap
= isl_basic_map_simplify(bmap
);
10668 return isl_basic_map_finalize(bmap
);
10670 isl_basic_map_free(bmap1
);
10671 isl_basic_map_free(bmap2
);
10675 __isl_give isl_basic_map
*isl_basic_map_range_product(
10676 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10679 isl_space
*space_result
= NULL
;
10680 isl_basic_map
*bmap
;
10681 isl_size in
, out1
, out2
, nparam
;
10682 unsigned total
, pos
;
10683 struct isl_dim_map
*dim_map1
, *dim_map2
;
10685 rational
= isl_basic_map_is_rational(bmap1
);
10686 if (rational
>= 0 && rational
)
10687 rational
= isl_basic_map_is_rational(bmap2
);
10688 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10689 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10690 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10691 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10692 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10695 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10698 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10699 isl_space_copy(bmap2
->dim
));
10701 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10702 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10703 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10704 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10705 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10706 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10707 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10708 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10709 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10710 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10711 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10713 bmap
= isl_basic_map_alloc_space(space_result
,
10714 bmap1
->n_div
+ bmap2
->n_div
,
10715 bmap1
->n_eq
+ bmap2
->n_eq
,
10716 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10717 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10718 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10720 bmap
= isl_basic_map_set_rational(bmap
);
10721 bmap
= isl_basic_map_simplify(bmap
);
10722 return isl_basic_map_finalize(bmap
);
10724 isl_basic_map_free(bmap1
);
10725 isl_basic_map_free(bmap2
);
10729 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10730 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10732 isl_basic_map
*prod
;
10734 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10735 prod
= isl_basic_map_flatten_range(prod
);
10739 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10740 * and collect the results.
10741 * The result live in the space obtained by calling "space_product"
10742 * on the spaces of "map1" and "map2".
10743 * If "remove_duplicates" is set then the result may contain duplicates
10744 * (even if the inputs do not) and so we try and remove the obvious
10747 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10748 __isl_take isl_map
*map2
,
10749 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10750 __isl_take isl_space
*right
),
10751 __isl_give isl_basic_map
*(*basic_map_product
)(
10752 __isl_take isl_basic_map
*left
,
10753 __isl_take isl_basic_map
*right
),
10754 int remove_duplicates
)
10756 unsigned flags
= 0;
10757 struct isl_map
*result
;
10761 m
= isl_map_has_equal_params(map1
, map2
);
10765 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10766 "parameters don't match", goto error
);
10768 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10769 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10770 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10772 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10773 isl_space_copy(map2
->dim
)),
10774 map1
->n
* map2
->n
, flags
);
10777 for (i
= 0; i
< map1
->n
; ++i
)
10778 for (j
= 0; j
< map2
->n
; ++j
) {
10779 struct isl_basic_map
*part
;
10780 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10781 isl_basic_map_copy(map2
->p
[j
]));
10782 if (isl_basic_map_is_empty(part
))
10783 isl_basic_map_free(part
);
10785 result
= isl_map_add_basic_map(result
, part
);
10789 if (remove_duplicates
)
10790 result
= isl_map_remove_obvious_duplicates(result
);
10791 isl_map_free(map1
);
10792 isl_map_free(map2
);
10795 isl_map_free(map1
);
10796 isl_map_free(map2
);
10800 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10802 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10803 __isl_take isl_map
*map2
)
10805 isl_map_align_params_bin(&map1
, &map2
);
10806 return map_product(map1
, map2
, &isl_space_product
,
10807 &isl_basic_map_product
, 0);
10810 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10812 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10813 __isl_take isl_map
*map2
)
10817 prod
= isl_map_product(map1
, map2
);
10818 prod
= isl_map_flatten(prod
);
10822 /* Given two set A and B, construct its Cartesian product A x B.
10824 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10825 __isl_take isl_set
*set2
)
10827 return isl_map_range_product(set1
, set2
);
10830 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10831 __isl_take isl_set
*set2
)
10833 return isl_map_flat_range_product(set1
, set2
);
10836 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10838 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10839 __isl_take isl_map
*map2
)
10841 isl_map_align_params_bin(&map1
, &map2
);
10842 return map_product(map1
, map2
, &isl_space_domain_product
,
10843 &isl_basic_map_domain_product
, 1);
10846 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10848 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10849 __isl_take isl_map
*map2
)
10851 isl_map_align_params_bin(&map1
, &map2
);
10852 return map_product(map1
, map2
, &isl_space_range_product
,
10853 &isl_basic_map_range_product
, 1);
10856 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10858 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10861 isl_size total1
, keep1
, total2
, keep2
;
10863 total1
= isl_map_dim(map
, isl_dim_in
);
10864 total2
= isl_map_dim(map
, isl_dim_out
);
10865 if (total1
< 0 || total2
< 0)
10866 return isl_map_free(map
);
10867 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10868 !isl_space_range_is_wrapping(map
->dim
))
10869 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10870 "not a product", return isl_map_free(map
));
10872 space
= isl_map_get_space(map
);
10873 space
= isl_space_factor_domain(space
);
10874 keep1
= isl_space_dim(space
, isl_dim_in
);
10875 keep2
= isl_space_dim(space
, isl_dim_out
);
10876 if (keep1
< 0 || keep2
< 0)
10877 map
= isl_map_free(map
);
10878 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10879 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10880 map
= isl_map_reset_space(map
, space
);
10885 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10887 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10890 isl_size total1
, keep1
, total2
, keep2
;
10892 total1
= isl_map_dim(map
, isl_dim_in
);
10893 total2
= isl_map_dim(map
, isl_dim_out
);
10894 if (total1
< 0 || total2
< 0)
10895 return isl_map_free(map
);
10896 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10897 !isl_space_range_is_wrapping(map
->dim
))
10898 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10899 "not a product", return isl_map_free(map
));
10901 space
= isl_map_get_space(map
);
10902 space
= isl_space_factor_range(space
);
10903 keep1
= isl_space_dim(space
, isl_dim_in
);
10904 keep2
= isl_space_dim(space
, isl_dim_out
);
10905 if (keep1
< 0 || keep2
< 0)
10906 map
= isl_map_free(map
);
10907 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10908 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10909 map
= isl_map_reset_space(map
, space
);
10914 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10916 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10919 isl_size total
, keep
;
10921 total
= isl_map_dim(map
, isl_dim_in
);
10923 return isl_map_free(map
);
10924 if (!isl_space_domain_is_wrapping(map
->dim
))
10925 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10926 "domain is not a product", return isl_map_free(map
));
10928 space
= isl_map_get_space(map
);
10929 space
= isl_space_domain_factor_domain(space
);
10930 keep
= isl_space_dim(space
, isl_dim_in
);
10932 map
= isl_map_free(map
);
10933 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10934 map
= isl_map_reset_space(map
, space
);
10939 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10941 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10944 isl_size total
, keep
;
10946 total
= isl_map_dim(map
, isl_dim_in
);
10948 return isl_map_free(map
);
10949 if (!isl_space_domain_is_wrapping(map
->dim
))
10950 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10951 "domain is not a product", return isl_map_free(map
));
10953 space
= isl_map_get_space(map
);
10954 space
= isl_space_domain_factor_range(space
);
10955 keep
= isl_space_dim(space
, isl_dim_in
);
10957 map
= isl_map_free(map
);
10958 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10959 map
= isl_map_reset_space(map
, space
);
10964 /* Given a map A -> [B -> C], extract the map A -> B.
10966 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10969 isl_size total
, keep
;
10971 total
= isl_map_dim(map
, isl_dim_out
);
10973 return isl_map_free(map
);
10974 if (!isl_space_range_is_wrapping(map
->dim
))
10975 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10976 "range is not a product", return isl_map_free(map
));
10978 space
= isl_map_get_space(map
);
10979 space
= isl_space_range_factor_domain(space
);
10980 keep
= isl_space_dim(space
, isl_dim_out
);
10982 map
= isl_map_free(map
);
10983 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10984 map
= isl_map_reset_space(map
, space
);
10989 /* Given a map A -> [B -> C], extract the map A -> C.
10991 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10994 isl_size total
, keep
;
10996 total
= isl_map_dim(map
, isl_dim_out
);
10998 return isl_map_free(map
);
10999 if (!isl_space_range_is_wrapping(map
->dim
))
11000 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11001 "range is not a product", return isl_map_free(map
));
11003 space
= isl_map_get_space(map
);
11004 space
= isl_space_range_factor_range(space
);
11005 keep
= isl_space_dim(space
, isl_dim_out
);
11007 map
= isl_map_free(map
);
11008 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11009 map
= isl_map_reset_space(map
, space
);
11014 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11016 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11017 __isl_take isl_map
*map2
)
11021 prod
= isl_map_domain_product(map1
, map2
);
11022 prod
= isl_map_flatten_domain(prod
);
11026 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11028 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11029 __isl_take isl_map
*map2
)
11033 prod
= isl_map_range_product(map1
, map2
);
11034 prod
= isl_map_flatten_range(prod
);
11038 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11041 uint32_t hash
= isl_hash_init();
11046 bmap
= isl_basic_map_copy(bmap
);
11047 bmap
= isl_basic_map_normalize(bmap
);
11048 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11051 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11052 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11054 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11055 isl_hash_hash(hash
, c_hash
);
11057 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11058 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11060 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11061 isl_hash_hash(hash
, c_hash
);
11063 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11064 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11066 if (isl_int_is_zero(bmap
->div
[i
][0]))
11068 isl_hash_byte(hash
, i
& 0xFF);
11069 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11070 isl_hash_hash(hash
, c_hash
);
11072 isl_basic_map_free(bmap
);
11076 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11078 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11081 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11088 map
= isl_map_copy(map
);
11089 map
= isl_map_normalize(map
);
11093 hash
= isl_hash_init();
11094 for (i
= 0; i
< map
->n
; ++i
) {
11095 uint32_t bmap_hash
;
11096 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11097 isl_hash_hash(hash
, bmap_hash
);
11105 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11107 return isl_map_get_hash(set_to_map(set
));
11110 /* Return the number of basic maps in the (current) representation of "map".
11112 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11114 return map
? map
->n
: isl_size_error
;
11117 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11119 return set
? set
->n
: isl_size_error
;
11122 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11123 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11128 return isl_stat_error
;
11130 for (i
= 0; i
< map
->n
; ++i
)
11131 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11132 return isl_stat_error
;
11134 return isl_stat_ok
;
11137 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11138 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11143 return isl_stat_error
;
11145 for (i
= 0; i
< set
->n
; ++i
)
11146 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11147 return isl_stat_error
;
11149 return isl_stat_ok
;
11152 /* Does "test" succeed on every basic set in "set"?
11154 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11155 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11161 return isl_bool_error
;
11163 for (i
= 0; i
< set
->n
; ++i
) {
11166 r
= test(set
->p
[i
], user
);
11171 return isl_bool_true
;
11174 /* Return a list of basic sets, the union of which is equal to "set".
11176 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11177 __isl_keep isl_set
*set
)
11180 isl_basic_set_list
*list
;
11185 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11186 for (i
= 0; i
< set
->n
; ++i
) {
11187 isl_basic_set
*bset
;
11189 bset
= isl_basic_set_copy(set
->p
[i
]);
11190 list
= isl_basic_set_list_add(list
, bset
);
11196 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11203 bset
= isl_basic_set_cow(bset
);
11207 space
= isl_basic_set_get_space(bset
);
11208 space
= isl_space_lift(space
, bset
->n_div
);
11211 isl_space_free(bset
->dim
);
11213 bset
->extra
-= bset
->n_div
;
11216 bset
= isl_basic_set_finalize(bset
);
11220 isl_basic_set_free(bset
);
11224 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11230 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11235 set
= isl_set_cow(set
);
11239 n_div
= set
->p
[0]->n_div
;
11240 space
= isl_set_get_space(set
);
11241 space
= isl_space_lift(space
, n_div
);
11244 isl_space_free(set
->dim
);
11247 for (i
= 0; i
< set
->n
; ++i
) {
11248 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11259 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11264 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11267 size
+= bset
->n_eq
* (1 + dim
);
11268 size
+= bset
->n_ineq
* (1 + dim
);
11269 size
+= bset
->n_div
* (2 + dim
);
11274 int isl_set_size(__isl_keep isl_set
*set
)
11282 for (i
= 0; i
< set
->n
; ++i
)
11283 size
+= isl_basic_set_size(set
->p
[i
]);
11288 /* Check if there is any lower bound (if lower == 0) and/or upper
11289 * bound (if upper == 0) on the specified dim.
11291 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11292 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11296 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11297 return isl_bool_error
;
11299 pos
+= isl_basic_map_offset(bmap
, type
);
11301 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11302 if (isl_int_is_zero(bmap
->div
[i
][0]))
11304 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11305 return isl_bool_true
;
11308 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11309 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11310 return isl_bool_true
;
11312 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11313 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11320 return lower
&& upper
;
11323 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11324 enum isl_dim_type type
, unsigned pos
)
11326 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11329 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11330 enum isl_dim_type type
, unsigned pos
)
11332 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11335 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11336 enum isl_dim_type type
, unsigned pos
)
11338 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11341 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11342 enum isl_dim_type type
, unsigned pos
)
11347 return isl_bool_error
;
11349 for (i
= 0; i
< map
->n
; ++i
) {
11351 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11352 if (bounded
< 0 || !bounded
)
11356 return isl_bool_true
;
11359 /* Return true if the specified dim is involved in both an upper bound
11360 * and a lower bound.
11362 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11363 enum isl_dim_type type
, unsigned pos
)
11365 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11368 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11370 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11371 enum isl_dim_type type
, unsigned pos
,
11372 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11373 enum isl_dim_type type
, unsigned pos
))
11378 return isl_bool_error
;
11380 for (i
= 0; i
< map
->n
; ++i
) {
11382 bounded
= fn(map
->p
[i
], type
, pos
);
11383 if (bounded
< 0 || bounded
)
11387 return isl_bool_false
;
11390 /* Return 1 if the specified dim is involved in any lower bound.
11392 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11393 enum isl_dim_type type
, unsigned pos
)
11395 return has_any_bound(set
, type
, pos
,
11396 &isl_basic_map_dim_has_lower_bound
);
11399 /* Return 1 if the specified dim is involved in any upper bound.
11401 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11402 enum isl_dim_type type
, unsigned pos
)
11404 return has_any_bound(set
, type
, pos
,
11405 &isl_basic_map_dim_has_upper_bound
);
11408 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11410 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11411 enum isl_dim_type type
, unsigned pos
,
11412 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11413 enum isl_dim_type type
, unsigned pos
))
11418 return isl_bool_error
;
11420 for (i
= 0; i
< map
->n
; ++i
) {
11422 bounded
= fn(map
->p
[i
], type
, pos
);
11423 if (bounded
< 0 || !bounded
)
11427 return isl_bool_true
;
11430 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11432 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11433 enum isl_dim_type type
, unsigned pos
)
11435 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11438 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11440 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11441 enum isl_dim_type type
, unsigned pos
)
11443 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11446 /* For each of the "n" variables starting at "first", determine
11447 * the sign of the variable and put the results in the first "n"
11448 * elements of the array "signs".
11450 * 1 means that the variable is non-negative
11451 * -1 means that the variable is non-positive
11452 * 0 means the variable attains both positive and negative values.
11454 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11455 unsigned first
, unsigned n
, int *signs
)
11457 isl_vec
*bound
= NULL
;
11458 struct isl_tab
*tab
= NULL
;
11459 struct isl_tab_undo
*snap
;
11463 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11464 if (total
< 0 || !signs
)
11465 return isl_stat_error
;
11467 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11468 tab
= isl_tab_from_basic_set(bset
, 0);
11469 if (!bound
|| !tab
)
11472 isl_seq_clr(bound
->el
, bound
->size
);
11473 isl_int_set_si(bound
->el
[0], -1);
11475 snap
= isl_tab_snap(tab
);
11476 for (i
= 0; i
< n
; ++i
) {
11479 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11480 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11482 empty
= tab
->empty
;
11483 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11484 if (isl_tab_rollback(tab
, snap
) < 0)
11492 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11493 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11495 empty
= tab
->empty
;
11496 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11497 if (isl_tab_rollback(tab
, snap
) < 0)
11500 signs
[i
] = empty
? -1 : 0;
11504 isl_vec_free(bound
);
11505 return isl_stat_ok
;
11508 isl_vec_free(bound
);
11509 return isl_stat_error
;
11512 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11513 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11515 if (!bset
|| !signs
)
11516 return isl_stat_error
;
11517 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11518 return isl_stat_error
;
11520 first
+= pos(bset
->dim
, type
) - 1;
11521 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11524 /* Is it possible for the integer division "div" to depend (possibly
11525 * indirectly) on any output dimensions?
11527 * If the div is undefined, then we conservatively assume that it
11528 * may depend on them.
11529 * Otherwise, we check if it actually depends on them or on any integer
11530 * divisions that may depend on them.
11532 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11535 isl_size n_out
, n_div
;
11536 unsigned o_out
, o_div
;
11538 if (isl_int_is_zero(bmap
->div
[div
][0]))
11539 return isl_bool_true
;
11541 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11543 return isl_bool_error
;
11544 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11546 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11547 return isl_bool_true
;
11549 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11551 return isl_bool_error
;
11552 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11554 for (i
= 0; i
< n_div
; ++i
) {
11555 isl_bool may_involve
;
11557 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11559 may_involve
= div_may_involve_output(bmap
, i
);
11560 if (may_involve
< 0 || may_involve
)
11561 return may_involve
;
11564 return isl_bool_false
;
11567 /* Return the first integer division of "bmap" in the range
11568 * [first, first + n[ that may depend on any output dimensions and
11569 * that has a non-zero coefficient in "c" (where the first coefficient
11570 * in "c" corresponds to integer division "first").
11572 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11573 isl_int
*c
, int first
, int n
)
11580 for (k
= first
; k
< first
+ n
; ++k
) {
11581 isl_bool may_involve
;
11583 if (isl_int_is_zero(c
[k
]))
11585 may_involve
= div_may_involve_output(bmap
, k
);
11586 if (may_involve
< 0)
11595 /* Look for a pair of inequality constraints in "bmap" of the form
11597 * -l + i >= 0 or i >= l
11599 * n + l - i >= 0 or i <= l + n
11601 * with n < "m" and i the output dimension at position "pos".
11602 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11603 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11604 * and earlier output dimensions, as well as integer divisions that do
11605 * not involve any of the output dimensions.
11607 * Return the index of the first inequality constraint or bmap->n_ineq
11608 * if no such pair can be found.
11610 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11611 int pos
, isl_int m
)
11616 isl_size n_div
, n_out
;
11617 unsigned o_div
, o_out
;
11620 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11621 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11622 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11623 if (total
< 0 || n_out
< 0 || n_div
< 0)
11626 ctx
= isl_basic_map_get_ctx(bmap
);
11627 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11628 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11629 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11630 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11632 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11633 n_out
- (pos
+ 1)) != -1)
11635 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11638 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11639 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11642 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11643 bmap
->ineq
[j
] + 1, total
))
11647 if (j
>= bmap
->n_ineq
)
11649 isl_int_add(bmap
->ineq
[i
][0],
11650 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11651 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11652 isl_int_sub(bmap
->ineq
[i
][0],
11653 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11656 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11662 return bmap
->n_ineq
;
11665 /* Return the index of the equality of "bmap" that defines
11666 * the output dimension "pos" in terms of earlier dimensions.
11667 * The equality may also involve integer divisions, as long
11668 * as those integer divisions are defined in terms of
11669 * parameters or input dimensions.
11670 * In this case, *div is set to the number of integer divisions and
11671 * *ineq is set to the number of inequality constraints (provided
11672 * div and ineq are not NULL).
11674 * The equality may also involve a single integer division involving
11675 * the output dimensions (typically only output dimension "pos") as
11676 * long as the coefficient of output dimension "pos" is 1 or -1 and
11677 * there is a pair of constraints i >= l and i <= l + n, with i referring
11678 * to output dimension "pos", l an expression involving only earlier
11679 * dimensions and n smaller than the coefficient of the integer division
11680 * in the equality. In this case, the output dimension can be defined
11681 * in terms of a modulo expression that does not involve the integer division.
11682 * *div is then set to this single integer division and
11683 * *ineq is set to the index of constraint i >= l.
11685 * Return bmap->n_eq if there is no such equality.
11686 * Return -1 on error.
11688 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11689 int pos
, int *div
, int *ineq
)
11692 isl_size n_div
, n_out
;
11693 unsigned o_div
, o_out
;
11695 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11696 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11697 if (n_out
< 0 || n_div
< 0)
11700 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11701 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11704 *ineq
= bmap
->n_ineq
;
11707 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11708 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11710 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11711 n_out
- (pos
+ 1)) != -1)
11713 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11717 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11718 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11720 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11721 k
+ 1, n_div
- (k
+1)) < n_div
)
11723 l
= find_modulo_constraint_pair(bmap
, pos
,
11724 bmap
->eq
[j
][o_div
+ k
]);
11727 if (l
>= bmap
->n_ineq
)
11739 /* Check if the given basic map is obviously single-valued.
11740 * In particular, for each output dimension, check that there is
11741 * an equality that defines the output dimension in terms of
11742 * earlier dimensions.
11744 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11749 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11751 return isl_bool_error
;
11753 for (i
= 0; i
< n_out
; ++i
) {
11756 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11759 return isl_bool_error
;
11760 if (eq
>= bmap
->n_eq
)
11761 return isl_bool_false
;
11764 return isl_bool_true
;
11767 /* Check if the given basic map is single-valued.
11768 * We simply compute
11772 * and check if the result is a subset of the identity mapping.
11774 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11777 isl_basic_map
*test
;
11781 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11785 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11786 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11788 space
= isl_basic_map_get_space(bmap
);
11789 space
= isl_space_map_from_set(isl_space_range(space
));
11790 id
= isl_basic_map_identity(space
);
11792 sv
= isl_basic_map_is_subset(test
, id
);
11794 isl_basic_map_free(test
);
11795 isl_basic_map_free(id
);
11800 /* Check if the given map is obviously single-valued.
11802 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11805 return isl_bool_error
;
11807 return isl_bool_true
;
11809 return isl_bool_false
;
11811 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11814 /* Check if the given map is single-valued.
11815 * We simply compute
11819 * and check if the result is a subset of the identity mapping.
11821 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11828 sv
= isl_map_plain_is_single_valued(map
);
11832 test
= isl_map_reverse(isl_map_copy(map
));
11833 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11835 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11836 id
= isl_map_identity(space
);
11838 sv
= isl_map_is_subset(test
, id
);
11840 isl_map_free(test
);
11846 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11850 map
= isl_map_copy(map
);
11851 map
= isl_map_reverse(map
);
11852 in
= isl_map_is_single_valued(map
);
11858 /* Check if the given map is obviously injective.
11860 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11864 map
= isl_map_copy(map
);
11865 map
= isl_map_reverse(map
);
11866 in
= isl_map_plain_is_single_valued(map
);
11872 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11876 sv
= isl_map_is_single_valued(map
);
11880 return isl_map_is_injective(map
);
11883 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11885 return isl_map_is_single_valued(set_to_map(set
));
11888 /* Does "map" only map elements to themselves?
11890 * If the domain and range spaces are different, then "map"
11891 * is considered not to be an identity relation, even if it is empty.
11892 * Otherwise, construct the maximal identity relation and
11893 * check whether "map" is a subset of this relation.
11895 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11898 isl_bool equal
, is_identity
;
11900 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
11901 if (equal
< 0 || !equal
)
11904 id
= isl_map_identity(isl_map_get_space(map
));
11905 is_identity
= isl_map_is_subset(map
, id
);
11908 return is_identity
;
11911 int isl_map_is_translation(__isl_keep isl_map
*map
)
11916 delta
= isl_map_deltas(isl_map_copy(map
));
11917 ok
= isl_set_is_singleton(delta
);
11918 isl_set_free(delta
);
11923 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11925 if (isl_seq_first_non_zero(p
, pos
) != -1)
11927 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11932 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11935 isl_size nvar
, n_div
;
11938 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11940 return isl_bool_error
;
11942 return isl_bool_false
;
11944 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11946 return isl_bool_error
;
11947 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11948 for (j
= 0; j
< nvar
; ++j
) {
11949 int lower
= 0, upper
= 0;
11950 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11951 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11953 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11954 return isl_bool_false
;
11957 if (i
< bset
->n_eq
)
11959 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11960 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11962 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11963 return isl_bool_false
;
11964 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11969 if (!lower
|| !upper
)
11970 return isl_bool_false
;
11973 return isl_bool_true
;
11976 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11979 return isl_bool_error
;
11981 return isl_bool_false
;
11983 return isl_basic_set_is_box(set
->p
[0]);
11986 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11989 return isl_bool_error
;
11991 return isl_space_is_wrapping(bset
->dim
);
11994 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11997 return isl_bool_error
;
11999 return isl_space_is_wrapping(set
->dim
);
12002 /* Modify the space of "map" through a call to "change".
12003 * If "can_change" is set (not NULL), then first call it to check
12004 * if the modification is allowed, printing the error message "cannot_change"
12007 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12008 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12009 const char *cannot_change
,
12010 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12018 ok
= can_change
? can_change(map
) : isl_bool_true
;
12020 return isl_map_free(map
);
12022 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12023 return isl_map_free(map
));
12025 space
= change(isl_map_get_space(map
));
12026 map
= isl_map_reset_space(map
, space
);
12031 /* Is the domain of "map" a wrapped relation?
12033 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12036 return isl_bool_error
;
12038 return isl_space_domain_is_wrapping(map
->dim
);
12041 /* Does "map" have a wrapped relation in both domain and range?
12043 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12045 return isl_space_is_product(isl_map_peek_space(map
));
12048 /* Is the range of "map" a wrapped relation?
12050 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12053 return isl_bool_error
;
12055 return isl_space_range_is_wrapping(map
->dim
);
12058 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12062 space
= isl_basic_map_take_space(bmap
);
12063 space
= isl_space_wrap(space
);
12064 bmap
= isl_basic_map_restore_space(bmap
, space
);
12066 bmap
= isl_basic_map_finalize(bmap
);
12068 return bset_from_bmap(bmap
);
12071 /* Given a map A -> B, return the set (A -> B).
12073 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12075 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12078 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12080 bset
= isl_basic_set_cow(bset
);
12084 bset
->dim
= isl_space_unwrap(bset
->dim
);
12088 bset
= isl_basic_set_finalize(bset
);
12090 return bset_to_bmap(bset
);
12092 isl_basic_set_free(bset
);
12096 /* Given a set (A -> B), return the map A -> B.
12097 * Error out if "set" is not of the form (A -> B).
12099 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12101 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12102 "not a wrapping set", &isl_space_unwrap
);
12105 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12106 enum isl_dim_type type
)
12110 space
= isl_basic_map_take_space(bmap
);
12111 space
= isl_space_reset(space
, type
);
12112 bmap
= isl_basic_map_restore_space(bmap
, space
);
12114 bmap
= isl_basic_map_mark_final(bmap
);
12119 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12120 enum isl_dim_type type
)
12128 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12131 map
= isl_map_cow(map
);
12135 for (i
= 0; i
< map
->n
; ++i
) {
12136 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12141 space
= isl_map_take_space(map
);
12142 space
= isl_space_reset(space
, type
);
12143 map
= isl_map_restore_space(map
, space
);
12151 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12155 space
= isl_basic_map_take_space(bmap
);
12156 space
= isl_space_flatten(space
);
12157 bmap
= isl_basic_map_restore_space(bmap
, space
);
12159 bmap
= isl_basic_map_mark_final(bmap
);
12164 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12166 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12169 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12170 __isl_take isl_basic_map
*bmap
)
12174 space
= isl_basic_map_take_space(bmap
);
12175 space
= isl_space_flatten_domain(space
);
12176 bmap
= isl_basic_map_restore_space(bmap
, space
);
12178 bmap
= isl_basic_map_mark_final(bmap
);
12183 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12184 __isl_take isl_basic_map
*bmap
)
12188 space
= isl_basic_map_take_space(bmap
);
12189 space
= isl_space_flatten_range(space
);
12190 bmap
= isl_basic_map_restore_space(bmap
, space
);
12192 bmap
= isl_basic_map_mark_final(bmap
);
12197 /* Remove any internal structure from the spaces of domain and range of "map".
12199 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12204 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12207 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12210 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12212 return set_from_map(isl_map_flatten(set_to_map(set
)));
12215 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12217 isl_space
*space
, *flat_space
;
12220 space
= isl_set_get_space(set
);
12221 flat_space
= isl_space_flatten(isl_space_copy(space
));
12222 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12224 map
= isl_map_intersect_domain(map
, set
);
12229 /* Remove any internal structure from the space of the domain of "map".
12231 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12236 if (!map
->dim
->nested
[0])
12239 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12242 /* Remove any internal structure from the space of the range of "map".
12244 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12249 if (!map
->dim
->nested
[1])
12252 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12255 /* Reorder the dimensions of "bmap" according to the given dim_map
12256 * and set the dimension specification to "space" and
12257 * perform Gaussian elimination on the result.
12259 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12260 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12262 isl_basic_map
*res
;
12266 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12267 if (n_div
< 0 || !space
|| !dim_map
)
12270 flags
= bmap
->flags
;
12271 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12272 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12273 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12274 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12275 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12277 res
->flags
= flags
;
12278 res
= isl_basic_map_gauss(res
, NULL
);
12279 res
= isl_basic_map_finalize(res
);
12282 isl_dim_map_free(dim_map
);
12283 isl_basic_map_free(bmap
);
12284 isl_space_free(space
);
12288 /* Reorder the dimensions of "map" according to given reordering.
12290 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12291 __isl_take isl_reordering
*r
)
12294 struct isl_dim_map
*dim_map
;
12296 map
= isl_map_cow(map
);
12297 dim_map
= isl_dim_map_from_reordering(r
);
12298 if (!map
|| !r
|| !dim_map
)
12301 for (i
= 0; i
< map
->n
; ++i
) {
12302 struct isl_dim_map
*dim_map_i
;
12305 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12307 space
= isl_reordering_get_space(r
);
12308 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12314 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12315 map
= isl_map_unmark_normalized(map
);
12317 isl_reordering_free(r
);
12318 isl_dim_map_free(dim_map
);
12321 isl_dim_map_free(dim_map
);
12323 isl_reordering_free(r
);
12327 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12328 __isl_take isl_reordering
*r
)
12330 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12333 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12334 __isl_take isl_space
*model
)
12339 if (!map
|| !model
)
12342 ctx
= isl_space_get_ctx(model
);
12343 if (!isl_space_has_named_params(model
))
12344 isl_die(ctx
, isl_error_invalid
,
12345 "model has unnamed parameters", goto error
);
12346 if (isl_map_check_named_params(map
) < 0)
12348 aligned
= isl_map_space_has_equal_params(map
, model
);
12352 isl_reordering
*exp
;
12354 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12355 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12356 map
= isl_map_realign(map
, exp
);
12359 isl_space_free(model
);
12362 isl_space_free(model
);
12367 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12368 __isl_take isl_space
*model
)
12370 return isl_map_align_params(set
, model
);
12373 /* Align the parameters of "bmap" to those of "model", introducing
12374 * additional parameters if needed.
12376 __isl_give isl_basic_map
*isl_basic_map_align_params(
12377 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12380 isl_bool equal_params
;
12382 if (!bmap
|| !model
)
12385 ctx
= isl_space_get_ctx(model
);
12386 if (!isl_space_has_named_params(model
))
12387 isl_die(ctx
, isl_error_invalid
,
12388 "model has unnamed parameters", goto error
);
12389 if (isl_basic_map_check_named_params(bmap
) < 0)
12391 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12392 if (equal_params
< 0)
12394 if (!equal_params
) {
12395 isl_reordering
*exp
;
12396 struct isl_dim_map
*dim_map
;
12398 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12399 exp
= isl_reordering_extend_space(exp
,
12400 isl_basic_map_get_space(bmap
));
12401 dim_map
= isl_dim_map_from_reordering(exp
);
12402 bmap
= isl_basic_map_realign(bmap
,
12403 isl_reordering_get_space(exp
),
12404 isl_dim_map_extend(dim_map
, bmap
));
12405 isl_reordering_free(exp
);
12406 isl_dim_map_free(dim_map
);
12409 isl_space_free(model
);
12412 isl_space_free(model
);
12413 isl_basic_map_free(bmap
);
12417 /* Do "bset" and "space" have the same parameters?
12419 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12420 __isl_keep isl_space
*space
)
12422 isl_space
*bset_space
;
12424 bset_space
= isl_basic_set_peek_space(bset
);
12425 return isl_space_has_equal_params(bset_space
, space
);
12428 /* Do "map" and "space" have the same parameters?
12430 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12431 __isl_keep isl_space
*space
)
12433 isl_space
*map_space
;
12435 map_space
= isl_map_peek_space(map
);
12436 return isl_space_has_equal_params(map_space
, space
);
12439 /* Do "set" and "space" have the same parameters?
12441 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12442 __isl_keep isl_space
*space
)
12444 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12447 /* Align the parameters of "bset" to those of "model", introducing
12448 * additional parameters if needed.
12450 __isl_give isl_basic_set
*isl_basic_set_align_params(
12451 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12453 return isl_basic_map_align_params(bset
, model
);
12456 /* Drop all parameters not referenced by "map".
12458 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12463 n
= isl_map_dim(map
, isl_dim_param
);
12464 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12465 return isl_map_free(map
);
12467 for (i
= n
- 1; i
>= 0; i
--) {
12470 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12472 return isl_map_free(map
);
12474 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12480 /* Drop all parameters not referenced by "set".
12482 __isl_give isl_set
*isl_set_drop_unused_params(
12483 __isl_take isl_set
*set
)
12485 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12488 /* Drop all parameters not referenced by "bmap".
12490 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12491 __isl_take isl_basic_map
*bmap
)
12496 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12497 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12498 return isl_basic_map_free(bmap
);
12500 for (i
= nparam
- 1; i
>= 0; i
--) {
12503 involves
= isl_basic_map_involves_dims(bmap
,
12504 isl_dim_param
, i
, 1);
12506 return isl_basic_map_free(bmap
);
12508 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12514 /* Drop all parameters not referenced by "bset".
12516 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12517 __isl_take isl_basic_set
*bset
)
12519 return bset_from_bmap(isl_basic_map_drop_unused_params(
12520 bset_to_bmap(bset
)));
12523 /* Given a tuple of identifiers "tuple" in a space that corresponds
12524 * to that of "set", if any of those identifiers appear as parameters
12525 * in "set", then equate those parameters with the corresponding
12526 * set dimensions and project out the parameters.
12527 * The result therefore has no such parameters.
12529 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12530 __isl_keep isl_multi_id
*tuple
)
12534 isl_space
*set_space
, *tuple_space
;
12536 set_space
= isl_set_peek_space(set
);
12537 tuple_space
= isl_multi_id_peek_space(tuple
);
12538 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12539 return isl_set_free(set
);
12540 n
= isl_multi_id_size(tuple
);
12542 return isl_set_free(set
);
12543 for (i
= 0; i
< n
; ++i
) {
12547 id
= isl_multi_id_get_at(tuple
, i
);
12549 return isl_set_free(set
);
12550 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12554 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12555 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12560 /* Bind the set dimensions of "set" to parameters with identifiers
12561 * specified by "tuple", living in the same space as "set".
12563 * If no parameters with these identifiers appear in "set" already,
12564 * then the set dimensions are simply reinterpreted as parameters.
12565 * Otherwise, the parameters are first equated to the corresponding
12568 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12569 __isl_take isl_multi_id
*tuple
)
12573 set
= equate_params(set
, tuple
);
12574 space
= isl_set_get_space(set
);
12575 space
= isl_space_bind_set(space
, tuple
);
12576 isl_multi_id_free(tuple
);
12577 set
= isl_set_reset_space(set
, space
);
12582 /* Given a tuple of identifiers "tuple" in a space that corresponds
12583 * to the domain of "map", if any of those identifiers appear as parameters
12584 * in "map", then equate those parameters with the corresponding
12585 * input dimensions and project out the parameters.
12586 * The result therefore has no such parameters.
12588 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12589 __isl_keep isl_multi_id
*tuple
)
12593 isl_space
*map_space
, *tuple_space
;
12595 map_space
= isl_map_peek_space(map
);
12596 tuple_space
= isl_multi_id_peek_space(tuple
);
12597 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12598 return isl_map_free(map
);
12599 n
= isl_multi_id_size(tuple
);
12601 return isl_map_free(map
);
12602 for (i
= 0; i
< n
; ++i
) {
12606 id
= isl_multi_id_get_at(tuple
, i
);
12608 return isl_map_free(map
);
12609 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12613 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12614 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12619 /* Bind the input dimensions of "map" to parameters with identifiers
12620 * specified by "tuple", living in the domain space of "map".
12622 * If no parameters with these identifiers appear in "map" already,
12623 * then the input dimensions are simply reinterpreted as parameters.
12624 * Otherwise, the parameters are first equated to the corresponding
12625 * input dimensions.
12627 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12628 __isl_take isl_multi_id
*tuple
)
12633 map
= map_equate_params(map
, tuple
);
12634 space
= isl_map_get_space(map
);
12635 space
= isl_space_bind_map_domain(space
, tuple
);
12636 isl_multi_id_free(tuple
);
12637 set
= set_from_map(isl_map_reset_space(map
, space
));
12642 /* Bind the output dimensions of "map" to parameters with identifiers
12643 * specified by "tuple", living in the range space of "map".
12645 * Since binding is more easily implemented on the domain,
12646 * bind the input dimensions of the inverse of "map".
12648 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12649 __isl_take isl_multi_id
*tuple
)
12651 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12654 /* Insert a domain corresponding to "tuple"
12655 * into the nullary or unary relation "set".
12656 * The result has an extra initial tuple and is therefore
12657 * either a unary or binary relation.
12658 * Any parameters with identifiers in "tuple" are reinterpreted
12659 * as the corresponding domain dimensions.
12661 static __isl_give isl_map
*unbind_params_insert_domain(
12662 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12667 space
= isl_set_peek_space(set
);
12668 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12669 isl_multi_id_free(tuple
);
12671 return isl_map_realign(set_to_map(set
), r
);
12674 /* Construct a set with "tuple" as domain from the parameter domain "set".
12675 * Any parameters with identifiers in "tuple" are reinterpreted
12676 * as the corresponding set dimensions.
12678 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12679 __isl_take isl_multi_id
*tuple
)
12681 isl_bool is_params
;
12683 is_params
= isl_set_is_params(set
);
12685 set
= isl_set_free(set
);
12686 else if (!is_params
)
12687 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12688 "expecting parameter domain", set
= isl_set_free(set
));
12689 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12692 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12694 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12696 isl_bool is_params
;
12698 is_params
= isl_set_is_params(set
);
12700 return isl_stat_error
;
12701 else if (is_params
)
12702 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12703 "expecting proper set", return isl_stat_error
);
12705 return isl_stat_ok
;
12708 /* Construct a map with "domain" as domain and "set" as range.
12709 * Any parameters with identifiers in "domain" are reinterpreted
12710 * as the corresponding domain dimensions.
12712 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12713 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12715 if (isl_set_check_is_set(set
) < 0)
12716 set
= isl_set_free(set
);
12717 return unbind_params_insert_domain(set
, domain
);
12720 /* Construct a map with "domain" as domain and "set" as range.
12722 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12723 __isl_take isl_space
*domain
)
12729 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12730 domain
= isl_space_free(domain
);
12731 dim
= isl_space_dim(domain
, isl_dim_set
);
12733 domain
= isl_space_free(domain
);
12734 space
= isl_set_get_space(set
);
12735 domain
= isl_space_replace_params(domain
, space
);
12736 space
= isl_space_map_from_domain_and_range(domain
, space
);
12738 map
= isl_map_from_range(set
);
12739 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12740 map
= isl_map_reset_space(map
, space
);
12745 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12746 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12747 enum isl_dim_type c2
, enum isl_dim_type c3
,
12748 enum isl_dim_type c4
, enum isl_dim_type c5
)
12750 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12751 struct isl_mat
*mat
;
12756 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12759 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12762 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12763 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12764 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12765 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12767 return isl_mat_free(mat
);
12768 for (k
= 0; k
< dim
; ++k
) {
12769 isl_int_set(mat
->row
[i
][pos
],
12770 bmap
->eq
[i
][off
+ k
]);
12778 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12779 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12780 enum isl_dim_type c2
, enum isl_dim_type c3
,
12781 enum isl_dim_type c4
, enum isl_dim_type c5
)
12783 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12784 struct isl_mat
*mat
;
12789 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12792 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12795 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12796 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12797 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12798 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12800 return isl_mat_free(mat
);
12801 for (k
= 0; k
< dim
; ++k
) {
12802 isl_int_set(mat
->row
[i
][pos
],
12803 bmap
->ineq
[i
][off
+ k
]);
12811 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12812 __isl_take isl_space
*space
,
12813 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12814 enum isl_dim_type c2
, enum isl_dim_type c3
,
12815 enum isl_dim_type c4
, enum isl_dim_type c5
)
12817 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12818 isl_basic_map
*bmap
= NULL
;
12825 dim
= isl_space_dim(space
, isl_dim_all
);
12826 if (dim
< 0 || !eq
|| !ineq
)
12829 if (eq
->n_col
!= ineq
->n_col
)
12830 isl_die(space
->ctx
, isl_error_invalid
,
12831 "equalities and inequalities matrices should have "
12832 "same number of columns", goto error
);
12836 if (eq
->n_col
< total
)
12837 isl_die(space
->ctx
, isl_error_invalid
,
12838 "number of columns too small", goto error
);
12840 extra
= eq
->n_col
- total
;
12842 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12843 eq
->n_row
, ineq
->n_row
);
12846 for (i
= 0; i
< extra
; ++i
) {
12847 k
= isl_basic_map_alloc_div(bmap
);
12850 isl_int_set_si(bmap
->div
[k
][0], 0);
12852 for (i
= 0; i
< eq
->n_row
; ++i
) {
12853 l
= isl_basic_map_alloc_equality(bmap
);
12856 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12857 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12858 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12861 for (k
= 0; k
< dim
; ++k
) {
12862 isl_int_set(bmap
->eq
[l
][off
+ k
],
12868 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12869 l
= isl_basic_map_alloc_inequality(bmap
);
12872 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12873 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12874 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12877 for (k
= 0; k
< dim
; ++k
) {
12878 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12879 ineq
->row
[i
][pos
]);
12885 isl_space_free(space
);
12887 isl_mat_free(ineq
);
12889 bmap
= isl_basic_map_simplify(bmap
);
12890 return isl_basic_map_finalize(bmap
);
12892 isl_space_free(space
);
12894 isl_mat_free(ineq
);
12895 isl_basic_map_free(bmap
);
12899 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12900 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12901 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12903 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12904 c1
, c2
, c3
, c4
, isl_dim_in
);
12907 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12908 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12909 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12911 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12912 c1
, c2
, c3
, c4
, isl_dim_in
);
12915 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12916 __isl_take isl_space
*space
,
12917 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12918 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12920 isl_basic_map
*bmap
;
12921 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
12922 c1
, c2
, c3
, c4
, isl_dim_in
);
12923 return bset_from_bmap(bmap
);
12926 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12929 return isl_bool_error
;
12931 return isl_space_can_zip(bmap
->dim
);
12934 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12937 return isl_bool_error
;
12939 return isl_space_can_zip(map
->dim
);
12942 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12943 * (A -> C) -> (B -> D).
12945 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12955 if (!isl_basic_map_can_zip(bmap
))
12956 isl_die(bmap
->ctx
, isl_error_invalid
,
12957 "basic map cannot be zipped", goto error
);
12958 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12959 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12960 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12961 if (n_in
< 0 || n1
< 0 || n2
< 0)
12962 return isl_basic_map_free(bmap
);
12963 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12964 bmap
= isl_basic_map_cow(bmap
);
12965 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12968 bmap
->dim
= isl_space_zip(bmap
->dim
);
12971 bmap
= isl_basic_map_mark_final(bmap
);
12974 isl_basic_map_free(bmap
);
12978 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12979 * (A -> C) -> (B -> D).
12981 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12986 if (!isl_map_can_zip(map
))
12987 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12990 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
12996 /* Can we apply isl_basic_map_curry to "bmap"?
12997 * That is, does it have a nested relation in its domain?
12999 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13002 return isl_bool_error
;
13004 return isl_space_can_curry(bmap
->dim
);
13007 /* Can we apply isl_map_curry to "map"?
13008 * That is, does it have a nested relation in its domain?
13010 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13013 return isl_bool_error
;
13015 return isl_space_can_curry(map
->dim
);
13018 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13021 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13027 if (!isl_basic_map_can_curry(bmap
))
13028 isl_die(bmap
->ctx
, isl_error_invalid
,
13029 "basic map cannot be curried", goto error
);
13030 bmap
= isl_basic_map_cow(bmap
);
13033 bmap
->dim
= isl_space_curry(bmap
->dim
);
13036 bmap
= isl_basic_map_mark_final(bmap
);
13039 isl_basic_map_free(bmap
);
13043 /* Given a map (A -> B) -> C, return the corresponding map
13046 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13048 return isl_map_change_space(map
, &isl_map_can_curry
,
13049 "map cannot be curried", &isl_space_curry
);
13052 /* Can isl_map_range_curry be applied to "map"?
13053 * That is, does it have a nested relation in its range,
13054 * the domain of which is itself a nested relation?
13056 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13059 return isl_bool_error
;
13061 return isl_space_can_range_curry(map
->dim
);
13064 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13065 * A -> (B -> (C -> D)).
13067 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13069 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13070 "map range cannot be curried",
13071 &isl_space_range_curry
);
13074 /* Can we apply isl_basic_map_uncurry to "bmap"?
13075 * That is, does it have a nested relation in its domain?
13077 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13080 return isl_bool_error
;
13082 return isl_space_can_uncurry(bmap
->dim
);
13085 /* Can we apply isl_map_uncurry to "map"?
13086 * That is, does it have a nested relation in its domain?
13088 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13091 return isl_bool_error
;
13093 return isl_space_can_uncurry(map
->dim
);
13096 /* Given a basic map A -> (B -> C), return the corresponding basic map
13099 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13105 if (!isl_basic_map_can_uncurry(bmap
))
13106 isl_die(bmap
->ctx
, isl_error_invalid
,
13107 "basic map cannot be uncurried",
13108 return isl_basic_map_free(bmap
));
13109 bmap
= isl_basic_map_cow(bmap
);
13112 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13114 return isl_basic_map_free(bmap
);
13115 bmap
= isl_basic_map_mark_final(bmap
);
13119 /* Given a map A -> (B -> C), return the corresponding map
13122 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13124 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13125 "map cannot be uncurried", &isl_space_uncurry
);
13128 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13129 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13131 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13134 /* Construct a basic map where the given dimensions are equal to each other.
13136 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13137 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13139 isl_basic_map
*bmap
= NULL
;
13143 total
= isl_space_dim(space
, isl_dim_all
);
13145 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13146 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13149 if (type1
== type2
&& pos1
== pos2
)
13150 return isl_basic_map_universe(space
);
13152 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13153 i
= isl_basic_map_alloc_equality(bmap
);
13156 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13157 pos1
+= isl_basic_map_offset(bmap
, type1
);
13158 pos2
+= isl_basic_map_offset(bmap
, type2
);
13159 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13160 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13161 bmap
= isl_basic_map_finalize(bmap
);
13162 isl_space_free(space
);
13165 isl_space_free(space
);
13166 isl_basic_map_free(bmap
);
13170 /* Add a constraint imposing that the given two dimensions are equal.
13172 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13173 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13177 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13179 bmap
= isl_basic_map_intersect(bmap
, eq
);
13184 /* Add a constraint imposing that the given two dimensions are equal.
13186 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13187 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13189 isl_basic_map
*bmap
;
13191 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13193 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13198 /* Add a constraint imposing that the given two dimensions have opposite values.
13200 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13201 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13203 isl_basic_map
*bmap
= NULL
;
13207 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13208 return isl_map_free(map
);
13209 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13210 return isl_map_free(map
);
13212 total
= isl_map_dim(map
, isl_dim_all
);
13214 return isl_map_free(map
);
13215 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13216 i
= isl_basic_map_alloc_equality(bmap
);
13219 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13220 pos1
+= isl_basic_map_offset(bmap
, type1
);
13221 pos2
+= isl_basic_map_offset(bmap
, type2
);
13222 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13223 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13224 bmap
= isl_basic_map_finalize(bmap
);
13226 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13230 isl_basic_map_free(bmap
);
13235 /* Construct a constraint imposing that the value of the first dimension is
13236 * greater than or equal to that of the second.
13238 static __isl_give isl_constraint
*constraint_order_ge(
13239 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13240 enum isl_dim_type type2
, int pos2
)
13244 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13245 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13246 space
= isl_space_free(space
);
13250 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13252 if (type1
== type2
&& pos1
== pos2
)
13255 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13256 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13261 /* Add a constraint imposing that the value of the first dimension is
13262 * greater than or equal to that of the second.
13264 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13265 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13270 if (type1
== type2
&& pos1
== pos2
)
13272 space
= isl_basic_map_get_space(bmap
);
13273 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13274 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13279 /* Add a constraint imposing that the value of the first dimension is
13280 * greater than or equal to that of the second.
13282 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13283 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13288 if (type1
== type2
&& pos1
== pos2
)
13290 space
= isl_map_get_space(map
);
13291 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13292 map
= isl_map_add_constraint(map
, c
);
13297 /* Add a constraint imposing that the value of the first dimension is
13298 * less than or equal to that of the second.
13300 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13301 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13303 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13306 /* Construct a basic map where the value of the first dimension is
13307 * greater than that of the second.
13309 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13310 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13312 isl_basic_map
*bmap
= NULL
;
13316 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13317 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13320 if (type1
== type2
&& pos1
== pos2
)
13321 return isl_basic_map_empty(space
);
13323 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13324 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13325 i
= isl_basic_map_alloc_inequality(bmap
);
13326 if (total
< 0 || i
< 0)
13327 return isl_basic_map_free(bmap
);
13328 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13329 pos1
+= isl_basic_map_offset(bmap
, type1
);
13330 pos2
+= isl_basic_map_offset(bmap
, type2
);
13331 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13332 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13333 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13334 bmap
= isl_basic_map_finalize(bmap
);
13338 isl_space_free(space
);
13339 isl_basic_map_free(bmap
);
13343 /* Add a constraint imposing that the value of the first dimension is
13344 * greater than that of the second.
13346 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13347 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13351 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13353 bmap
= isl_basic_map_intersect(bmap
, gt
);
13358 /* Add a constraint imposing that the value of the first dimension is
13359 * greater than that of the second.
13361 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13362 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13364 isl_basic_map
*bmap
;
13366 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13368 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13373 /* Add a constraint imposing that the value of the first dimension is
13374 * smaller than that of the second.
13376 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13377 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13379 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13382 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13386 isl_local_space
*ls
;
13391 if (!isl_basic_map_divs_known(bmap
))
13392 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13393 "some divs are unknown", return NULL
);
13395 ls
= isl_basic_map_get_local_space(bmap
);
13396 div
= isl_local_space_get_div(ls
, pos
);
13397 isl_local_space_free(ls
);
13402 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13405 return isl_basic_map_get_div(bset
, pos
);
13408 /* Plug in "subs" for dimension "type", "pos" of "bset".
13410 * Let i be the dimension to replace and let "subs" be of the form
13414 * Any integer division with a non-zero coefficient for i,
13416 * floor((a i + g)/m)
13420 * floor((a f + d g)/(m d))
13422 * Constraints of the form
13430 * We currently require that "subs" is an integral expression.
13431 * Handling rational expressions may require us to add stride constraints
13432 * as we do in isl_basic_set_preimage_multi_aff.
13434 __isl_give isl_basic_set
*isl_basic_set_substitute(
13435 __isl_take isl_basic_set
*bset
,
13436 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13443 if (bset
&& isl_basic_set_plain_is_empty(bset
))
13446 bset
= isl_basic_set_cow(bset
);
13447 if (!bset
|| !subs
)
13450 ctx
= isl_basic_set_get_ctx(bset
);
13451 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
13452 isl_die(ctx
, isl_error_invalid
,
13453 "spaces don't match", goto error
);
13454 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
13458 isl_die(ctx
, isl_error_unsupported
,
13459 "cannot handle divs yet", goto error
);
13460 if (!isl_int_is_one(subs
->v
->el
[0]))
13461 isl_die(ctx
, isl_error_invalid
,
13462 "can only substitute integer expressions", goto error
);
13464 pos
+= isl_basic_set_offset(bset
, type
);
13468 for (i
= 0; i
< bset
->n_eq
; ++i
) {
13469 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
13471 isl_int_set(v
, bset
->eq
[i
][pos
]);
13472 isl_int_set_si(bset
->eq
[i
][pos
], 0);
13473 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
13474 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13477 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
13478 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
13480 isl_int_set(v
, bset
->ineq
[i
][pos
]);
13481 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
13482 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
13483 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13486 for (i
= 0; i
< bset
->n_div
; ++i
) {
13487 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
13489 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
13490 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
13491 isl_seq_combine(bset
->div
[i
] + 1,
13492 subs
->v
->el
[0], bset
->div
[i
] + 1,
13493 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13494 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
13499 bset
= isl_basic_set_simplify(bset
);
13500 return isl_basic_set_finalize(bset
);
13502 isl_basic_set_free(bset
);
13506 /* Plug in "subs" for dimension "type", "pos" of "set".
13508 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13509 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13513 if (set
&& isl_set_plain_is_empty(set
))
13516 set
= isl_set_cow(set
);
13520 for (i
= set
->n
- 1; i
>= 0; --i
) {
13521 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
13522 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
13533 /* Check if the range of "ma" is compatible with the domain or range
13534 * (depending on "type") of "bmap".
13536 static isl_stat
check_basic_map_compatible_range_multi_aff(
13537 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13538 __isl_keep isl_multi_aff
*ma
)
13541 isl_space
*ma_space
;
13543 ma_space
= isl_multi_aff_get_space(ma
);
13545 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13549 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13550 "parameters don't match", goto error
);
13551 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13555 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13556 "spaces don't match", goto error
);
13558 isl_space_free(ma_space
);
13559 return isl_stat_ok
;
13561 isl_space_free(ma_space
);
13562 return isl_stat_error
;
13565 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13566 * coefficients before the transformed range of dimensions,
13567 * the "n_after" coefficients after the transformed range of dimensions
13568 * and the coefficients of the other divs in "bmap".
13570 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13571 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13576 isl_local_space
*ls
;
13581 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13582 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13583 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13584 if (n_param
< 0 || n_set
< 0)
13585 return isl_basic_map_free(bmap
);
13587 for (i
= 0; i
< n_div
; ++i
) {
13588 int o_bmap
= 0, o_ls
= 0;
13590 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13591 o_bmap
+= 1 + 1 + n_param
;
13592 o_ls
+= 1 + 1 + n_param
;
13593 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13594 o_bmap
+= n_before
;
13595 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13596 ls
->div
->row
[i
] + o_ls
, n_set
);
13599 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13601 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13602 ls
->div
->row
[i
] + o_ls
, n_div
);
13605 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13606 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13611 isl_local_space_free(ls
);
13614 isl_local_space_free(ls
);
13615 return isl_basic_map_free(bmap
);
13618 /* How many stride constraints does "ma" enforce?
13619 * That is, how many of the affine expressions have a denominator
13620 * different from one?
13622 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13627 for (i
= 0; i
< ma
->n
; ++i
)
13628 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13634 /* For each affine expression in ma of the form
13636 * x_i = (f_i y + h_i)/m_i
13638 * with m_i different from one, add a constraint to "bmap"
13641 * f_i y + h_i = m_i alpha_i
13643 * with alpha_i an additional existentially quantified variable.
13645 * The input variables of "ma" correspond to a subset of the variables
13646 * of "bmap". There are "n_before" variables in "bmap" before this
13647 * subset and "n_after" variables after this subset.
13648 * The integer divisions of the affine expressions in "ma" are assumed
13649 * to have been aligned. There are "n_div_ma" of them and
13650 * they appear first in "bmap", straight after the "n_after" variables.
13652 static __isl_give isl_basic_map
*add_ma_strides(
13653 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13654 int n_before
, int n_after
, int n_div_ma
)
13662 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13663 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13664 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13665 if (total
< 0 || n_param
< 0 || n_in
< 0)
13666 return isl_basic_map_free(bmap
);
13667 for (i
= 0; i
< ma
->n
; ++i
) {
13668 int o_bmap
= 0, o_ma
= 1;
13670 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13672 div
= isl_basic_map_alloc_div(bmap
);
13673 k
= isl_basic_map_alloc_equality(bmap
);
13674 if (div
< 0 || k
< 0)
13676 isl_int_set_si(bmap
->div
[div
][0], 0);
13677 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13678 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13679 o_bmap
+= 1 + n_param
;
13680 o_ma
+= 1 + n_param
;
13681 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13682 o_bmap
+= n_before
;
13683 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13684 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13687 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13689 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13690 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13691 o_bmap
+= n_div_ma
;
13693 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13694 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13700 isl_basic_map_free(bmap
);
13704 /* Replace the domain or range space (depending on "type) of "space" by "set".
13706 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13707 enum isl_dim_type type
, __isl_take isl_space
*set
)
13709 if (type
== isl_dim_in
) {
13710 space
= isl_space_range(space
);
13711 space
= isl_space_map_from_domain_and_range(set
, space
);
13713 space
= isl_space_domain(space
);
13714 space
= isl_space_map_from_domain_and_range(space
, set
);
13720 /* Compute the preimage of the domain or range (depending on "type")
13721 * of "bmap" under the function represented by "ma".
13722 * In other words, plug in "ma" in the domain or range of "bmap".
13723 * The result is a basic map that lives in the same space as "bmap"
13724 * except that the domain or range has been replaced by
13725 * the domain space of "ma".
13727 * If bmap is represented by
13729 * A(p) + S u + B x + T v + C(divs) >= 0,
13731 * where u and x are input and output dimensions if type == isl_dim_out
13732 * while x and v are input and output dimensions if type == isl_dim_in,
13733 * and ma is represented by
13735 * x = D(p) + F(y) + G(divs')
13737 * then the result is
13739 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13741 * The divs in the input set are similarly adjusted.
13744 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13748 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13749 * B_i G(divs') + c_i(divs))/n_i)
13751 * If bmap is not a rational map and if F(y) involves any denominators
13753 * x_i = (f_i y + h_i)/m_i
13755 * then additional constraints are added to ensure that we only
13756 * map back integer points. That is we enforce
13758 * f_i y + h_i = m_i alpha_i
13760 * with alpha_i an additional existentially quantified variable.
13762 * We first copy over the divs from "ma".
13763 * Then we add the modified constraints and divs from "bmap".
13764 * Finally, we add the stride constraints, if needed.
13766 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13767 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13768 __isl_take isl_multi_aff
*ma
)
13772 isl_basic_map
*res
= NULL
;
13773 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13774 isl_int f
, c1
, c2
, g
;
13783 ma
= isl_multi_aff_align_divs(ma
);
13786 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13789 if (type
== isl_dim_in
) {
13791 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13793 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13796 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13797 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13798 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13801 space
= isl_multi_aff_get_domain_space(ma
);
13802 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13803 rational
= isl_basic_map_is_rational(bmap
);
13804 strides
= rational
? 0 : multi_aff_strides(ma
);
13805 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13806 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13808 res
= isl_basic_map_set_rational(res
);
13810 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13811 if (isl_basic_map_alloc_div(res
) < 0)
13814 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13818 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13819 k
= isl_basic_map_alloc_equality(res
);
13822 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13823 n_after
, n_div_ma
, n_div_bmap
,
13824 f
, c1
, c2
, g
, 0) < 0)
13828 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13829 k
= isl_basic_map_alloc_inequality(res
);
13832 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13833 n_after
, n_div_ma
, n_div_bmap
,
13834 f
, c1
, c2
, g
, 0) < 0)
13838 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13839 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13840 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13843 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13844 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13845 f
, c1
, c2
, g
, 1) < 0)
13850 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13856 isl_basic_map_free(bmap
);
13857 isl_multi_aff_free(ma
);
13858 res
= isl_basic_map_simplify(res
);
13859 return isl_basic_map_finalize(res
);
13865 isl_basic_map_free(bmap
);
13866 isl_multi_aff_free(ma
);
13867 isl_basic_map_free(res
);
13871 /* Compute the preimage of "bset" under the function represented by "ma".
13872 * In other words, plug in "ma" in "bset". The result is a basic set
13873 * that lives in the domain space of "ma".
13875 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13876 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13878 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13881 /* Compute the preimage of the domain of "bmap" under the function
13882 * represented by "ma".
13883 * In other words, plug in "ma" in the domain of "bmap".
13884 * The result is a basic map that lives in the same space as "bmap"
13885 * except that the domain has been replaced by the domain space of "ma".
13887 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13888 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13890 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13893 /* Compute the preimage of the range of "bmap" under the function
13894 * represented by "ma".
13895 * In other words, plug in "ma" in the range of "bmap".
13896 * The result is a basic map that lives in the same space as "bmap"
13897 * except that the range has been replaced by the domain space of "ma".
13899 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13900 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13902 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13905 /* Check if the range of "ma" is compatible with the domain or range
13906 * (depending on "type") of "map".
13907 * Return isl_stat_error if anything is wrong.
13909 static isl_stat
check_map_compatible_range_multi_aff(
13910 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13911 __isl_keep isl_multi_aff
*ma
)
13914 isl_space
*ma_space
;
13916 ma_space
= isl_multi_aff_get_space(ma
);
13917 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13918 isl_space_free(ma_space
);
13920 return isl_stat_error
;
13922 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13923 "spaces don't match", return isl_stat_error
);
13924 return isl_stat_ok
;
13927 /* Compute the preimage of the domain or range (depending on "type")
13928 * of "map" under the function represented by "ma".
13929 * In other words, plug in "ma" in the domain or range of "map".
13930 * The result is a map that lives in the same space as "map"
13931 * except that the domain or range has been replaced by
13932 * the domain space of "ma".
13934 * The parameters are assumed to have been aligned.
13936 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13937 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13942 map
= isl_map_cow(map
);
13943 ma
= isl_multi_aff_align_divs(ma
);
13946 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13949 for (i
= 0; i
< map
->n
; ++i
) {
13950 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13951 isl_multi_aff_copy(ma
));
13956 space
= isl_multi_aff_get_domain_space(ma
);
13957 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13959 isl_space_free(isl_map_take_space(map
));
13960 map
= isl_map_restore_space(map
, space
);
13964 isl_multi_aff_free(ma
);
13966 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13967 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13970 isl_multi_aff_free(ma
);
13975 /* Compute the preimage of the domain or range (depending on "type")
13976 * of "map" under the function represented by "ma".
13977 * In other words, plug in "ma" in the domain or range of "map".
13978 * The result is a map that lives in the same space as "map"
13979 * except that the domain or range has been replaced by
13980 * the domain space of "ma".
13982 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13983 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13990 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13994 return map_preimage_multi_aff(map
, type
, ma
);
13996 if (isl_map_check_named_params(map
) < 0)
13998 if (!isl_space_has_named_params(ma
->space
))
13999 isl_die(map
->ctx
, isl_error_invalid
,
14000 "unaligned unnamed parameters", goto error
);
14001 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14002 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14004 return map_preimage_multi_aff(map
, type
, ma
);
14006 isl_multi_aff_free(ma
);
14007 return isl_map_free(map
);
14010 /* Compute the preimage of "set" under the function represented by "ma".
14011 * In other words, plug in "ma" in "set". The result is a set
14012 * that lives in the domain space of "ma".
14014 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14015 __isl_take isl_multi_aff
*ma
)
14017 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14020 /* Compute the preimage of the domain of "map" under the function
14021 * represented by "ma".
14022 * In other words, plug in "ma" in the domain of "map".
14023 * The result is a map that lives in the same space as "map"
14024 * except that the domain has been replaced by the domain space of "ma".
14026 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14027 __isl_take isl_multi_aff
*ma
)
14029 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14032 /* Compute the preimage of the range of "map" under the function
14033 * represented by "ma".
14034 * In other words, plug in "ma" in the range of "map".
14035 * The result is a map that lives in the same space as "map"
14036 * except that the range has been replaced by the domain space of "ma".
14038 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14039 __isl_take isl_multi_aff
*ma
)
14041 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14044 /* Compute the preimage of "map" under the function represented by "pma".
14045 * In other words, plug in "pma" in the domain or range of "map".
14046 * The result is a map that lives in the same space as "map",
14047 * except that the space of type "type" has been replaced by
14048 * the domain space of "pma".
14050 * The parameters of "map" and "pma" are assumed to have been aligned.
14052 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14053 __isl_take isl_map
*map
, enum isl_dim_type type
,
14054 __isl_take isl_pw_multi_aff
*pma
)
14063 isl_pw_multi_aff_free(pma
);
14064 res
= isl_map_empty(isl_map_get_space(map
));
14069 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14070 isl_multi_aff_copy(pma
->p
[0].maff
));
14071 if (type
== isl_dim_in
)
14072 res
= isl_map_intersect_domain(res
,
14073 isl_map_copy(pma
->p
[0].set
));
14075 res
= isl_map_intersect_range(res
,
14076 isl_map_copy(pma
->p
[0].set
));
14078 for (i
= 1; i
< pma
->n
; ++i
) {
14081 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14082 isl_multi_aff_copy(pma
->p
[i
].maff
));
14083 if (type
== isl_dim_in
)
14084 res_i
= isl_map_intersect_domain(res_i
,
14085 isl_map_copy(pma
->p
[i
].set
));
14087 res_i
= isl_map_intersect_range(res_i
,
14088 isl_map_copy(pma
->p
[i
].set
));
14089 res
= isl_map_union(res
, res_i
);
14092 isl_pw_multi_aff_free(pma
);
14096 isl_pw_multi_aff_free(pma
);
14101 /* Compute the preimage of "map" under the function represented by "pma".
14102 * In other words, plug in "pma" in the domain or range of "map".
14103 * The result is a map that lives in the same space as "map",
14104 * except that the space of type "type" has been replaced by
14105 * the domain space of "pma".
14107 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14108 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14115 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14119 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14121 if (isl_map_check_named_params(map
) < 0)
14123 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14125 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14126 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14128 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14130 isl_pw_multi_aff_free(pma
);
14131 return isl_map_free(map
);
14134 /* Compute the preimage of "set" under the function represented by "pma".
14135 * In other words, plug in "pma" in "set". The result is a set
14136 * that lives in the domain space of "pma".
14138 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14139 __isl_take isl_pw_multi_aff
*pma
)
14141 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14144 /* Compute the preimage of the domain of "map" under the function
14145 * represented by "pma".
14146 * In other words, plug in "pma" in the domain of "map".
14147 * The result is a map that lives in the same space as "map",
14148 * except that domain space has been replaced by the domain space of "pma".
14150 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14151 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14153 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14156 /* Compute the preimage of the range of "map" under the function
14157 * represented by "pma".
14158 * In other words, plug in "pma" in the range of "map".
14159 * The result is a map that lives in the same space as "map",
14160 * except that range space has been replaced by the domain space of "pma".
14162 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14163 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14165 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14168 /* Compute the preimage of "map" under the function represented by "mpa".
14169 * In other words, plug in "mpa" in the domain or range of "map".
14170 * The result is a map that lives in the same space as "map",
14171 * except that the space of type "type" has been replaced by
14172 * the domain space of "mpa".
14174 * If the map does not involve any constraints that refer to the
14175 * dimensions of the substituted space, then the only possible
14176 * effect of "mpa" on the map is to map the space to a different space.
14177 * We create a separate isl_multi_aff to effectuate this change
14178 * in order to avoid spurious splitting of the map along the pieces
14180 * If "mpa" has a non-trivial explicit domain, however,
14181 * then the full substitution should be performed.
14183 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14184 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14188 isl_pw_multi_aff
*pma
;
14190 n
= isl_map_dim(map
, type
);
14194 full
= isl_map_involves_dims(map
, type
, 0, n
);
14195 if (full
>= 0 && !full
)
14196 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14203 space
= isl_multi_pw_aff_get_space(mpa
);
14204 isl_multi_pw_aff_free(mpa
);
14205 ma
= isl_multi_aff_zero(space
);
14206 return isl_map_preimage_multi_aff(map
, type
, ma
);
14209 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14210 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14213 isl_multi_pw_aff_free(mpa
);
14217 /* Compute the preimage of "map" under the function represented by "mpa".
14218 * In other words, plug in "mpa" in the domain "map".
14219 * The result is a map that lives in the same space as "map",
14220 * except that domain space has been replaced by the domain space of "mpa".
14222 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14223 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14225 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14228 /* Compute the preimage of "set" by the function represented by "mpa".
14229 * In other words, plug in "mpa" in "set".
14231 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14232 __isl_take isl_multi_pw_aff
*mpa
)
14234 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14237 /* Return a copy of the equality constraints of "bset" as a matrix.
14239 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14240 __isl_keep isl_basic_set
*bset
)
14245 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14249 ctx
= isl_basic_set_get_ctx(bset
);
14250 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14253 /* Are the "n" "coefficients" starting at "first" of the integer division
14254 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14256 * The "coefficient" at position 0 is the denominator.
14257 * The "coefficient" at position 1 is the constant term.
14259 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14260 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14261 unsigned first
, unsigned n
)
14263 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14264 return isl_bool_error
;
14265 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14266 return isl_bool_error
;
14267 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14268 bmap2
->div
[pos2
] + first
, n
);
14271 /* Are the integer division expressions at position "pos1" in "bmap1" and
14272 * "pos2" in "bmap2" equal to each other, except that the constant terms
14275 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14276 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14277 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14280 isl_size total
, total2
;
14282 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14283 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14284 if (total
< 0 || total2
< 0)
14285 return isl_bool_error
;
14286 if (total
!= total2
)
14287 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14288 "incomparable div expressions", return isl_bool_error
);
14289 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14291 if (equal
< 0 || !equal
)
14293 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14295 if (equal
< 0 || equal
)
14296 return isl_bool_not(equal
);
14297 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14301 /* Replace the numerator of the constant term of the integer division
14302 * expression at position "div" in "bmap" by "value".
14303 * The caller guarantees that this does not change the meaning
14306 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14307 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14309 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14310 return isl_basic_map_free(bmap
);
14312 isl_int_set_si(bmap
->div
[div
][1], value
);
14317 /* Is the point "inner" internal to inequality constraint "ineq"
14319 * The point is considered to be internal to the inequality constraint,
14320 * if it strictly lies on the positive side of the inequality constraint,
14321 * or if it lies on the constraint and the constraint is lexico-positive.
14323 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14324 __isl_keep isl_basic_set
*bset
, int ineq
)
14330 if (!inner
|| !bset
)
14331 return isl_bool_error
;
14333 ctx
= isl_basic_set_get_ctx(bset
);
14334 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14335 &ctx
->normalize_gcd
);
14336 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14337 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14339 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14341 return isl_bool_error
;
14342 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14343 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14346 /* Tighten the inequality constraints of "bset" that are outward with respect
14347 * to the point "vec".
14348 * That is, tighten the constraints that are not satisfied by "vec".
14350 * "vec" is a point internal to some superset S of "bset" that is used
14351 * to make the subsets of S disjoint, by tightening one half of the constraints
14352 * that separate two subsets. In particular, the constraints of S
14353 * are all satisfied by "vec" and should not be tightened.
14354 * Of the internal constraints, those that have "vec" on the outside
14355 * are tightened. The shared facet is included in the adjacent subset
14356 * with the opposite constraint.
14357 * For constraints that saturate "vec", this criterion cannot be used
14358 * to determine which of the two sides should be tightened.
14359 * Instead, the sign of the first non-zero coefficient is used
14360 * to make this choice. Note that this second criterion is never used
14361 * on the constraints of S since "vec" is interior to "S".
14363 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14364 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14368 bset
= isl_basic_set_cow(bset
);
14371 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14374 internal
= is_internal(vec
, bset
, j
);
14376 return isl_basic_set_free(bset
);
14379 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14385 /* Replace the variables x of type "type" starting at "first" in "bmap"
14386 * by x' with x = M x' with M the matrix trans.
14387 * That is, replace the corresponding coefficients c by c M.
14389 * The transformation matrix should be a square matrix.
14391 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14392 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14393 __isl_take isl_mat
*trans
)
14397 bmap
= isl_basic_map_cow(bmap
);
14398 if (!bmap
|| !trans
)
14401 if (trans
->n_row
!= trans
->n_col
)
14402 isl_die(trans
->ctx
, isl_error_invalid
,
14403 "expecting square transformation matrix", goto error
);
14404 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14407 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14409 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14410 isl_mat_copy(trans
)) < 0)
14412 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14413 isl_mat_copy(trans
)) < 0)
14415 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14416 isl_mat_copy(trans
)) < 0)
14419 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14420 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14422 isl_mat_free(trans
);
14425 isl_mat_free(trans
);
14426 isl_basic_map_free(bmap
);
14430 /* Replace the variables x of type "type" starting at "first" in "bset"
14431 * by x' with x = M x' with M the matrix trans.
14432 * That is, replace the corresponding coefficients c by c M.
14434 * The transformation matrix should be a square matrix.
14436 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14437 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14438 __isl_take isl_mat
*trans
)
14440 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);