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
9 * Copyright 2021-2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
23 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
27 #include <isl_ctx_private.h>
28 #include <isl_map_private.h>
30 #include <isl_id_private.h>
31 #include <isl/constraint.h>
32 #include "isl_space_private.h"
33 #include "isl_equalities.h"
34 #include <isl_lp_private.h>
38 #include <isl_reordering.h>
39 #include "isl_sample.h"
43 #include <isl_mat_private.h>
44 #include <isl_vec_private.h>
45 #include <isl_dim_map.h>
46 #include <isl_local_space_private.h>
47 #include <isl_aff_private.h>
48 #include <isl_options_private.h>
49 #include <isl_morph.h>
50 #include <isl_val_private.h>
51 #include <isl_printer_private.h>
52 #include <isl_maybe_aff.h>
54 #include <bset_to_bmap.c>
55 #include <bset_from_bmap.c>
56 #include <set_to_map.c>
57 #include <set_from_map.c>
59 /* Treat "bset" as a basic map.
60 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
61 * this function performs a redundant cast.
63 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
64 __isl_keep
const isl_basic_set
*bset
)
66 return (const isl_basic_map
*) bset
;
70 #define TYPE isl_basic_map
71 #include "has_single_reference_templ.c"
73 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
76 case isl_dim_param
: return 1;
77 case isl_dim_in
: return 1 + space
->nparam
;
78 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
83 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
84 enum isl_dim_type type
)
87 return isl_size_error
;
89 case isl_dim_cst
: return 1;
92 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
93 case isl_dim_div
: return bmap
->n_div
;
94 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
99 /* Return the space of "map".
101 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
103 return map
? map
->dim
: NULL
;
106 /* Return the space of "set".
108 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
110 return isl_map_peek_space(set_to_map(set
));
113 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
115 return isl_space_dim(isl_map_peek_space(map
), type
);
118 /* Return the dimensionality of the domain (tuple) of the map.
120 isl_size
isl_map_domain_tuple_dim(__isl_keep isl_map
*map
)
122 return isl_map_dim(map
, isl_dim_in
);
125 /* Return the dimensionality of the range (tuple) of the map.
127 isl_size
isl_map_range_tuple_dim(__isl_keep isl_map
*map
)
129 return isl_map_dim(map
, isl_dim_out
);
132 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
134 return isl_map_dim(set_to_map(set
), type
);
137 /* Return the dimensionality of the (tuple of the) set.
139 isl_size
isl_set_tuple_dim(__isl_keep isl_set
*set
)
141 return isl_set_dim(set
, isl_dim_set
);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bmap".
147 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
148 enum isl_dim_type type
)
152 space
= isl_basic_map_peek_space(bmap
);
157 case isl_dim_out
: return isl_space_offset(space
, type
);
158 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
161 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
162 "invalid dimension type", return isl_size_error
);
166 /* Return the position of the variables of the given type
167 * within the sequence of variables of "bset".
169 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
170 enum isl_dim_type type
)
172 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
175 /* Return the position of the coefficients of the variables of the given type
176 * within the sequence of coefficients of "bmap".
178 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
179 enum isl_dim_type type
)
182 case isl_dim_cst
: return 0;
186 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
191 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
192 enum isl_dim_type type
)
194 return isl_basic_map_offset(bset
, type
);
197 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
199 return pos(map
->dim
, type
);
202 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
203 enum isl_dim_type type
)
205 return isl_basic_map_dim(bset
, type
);
208 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
210 return isl_basic_set_dim(bset
, isl_dim_set
);
213 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
215 return isl_basic_set_dim(bset
, isl_dim_param
);
218 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
220 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
223 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
225 return isl_set_dim(set
, isl_dim_set
);
228 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
230 return isl_set_dim(set
, isl_dim_param
);
233 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
238 return isl_size_error
;
239 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
241 return isl_size_error
;
242 return dim
+ bmap
->n_div
;
245 /* Return the number of equality constraints in the description of "bmap".
246 * Return isl_size_error on error.
248 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
251 return isl_size_error
;
255 /* Return the number of equality constraints in the description of "bset".
256 * Return isl_size_error on error.
258 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
260 return isl_basic_map_n_equality(bset_to_bmap(bset
));
263 /* Return the number of inequality constraints in the description of "bmap".
264 * Return isl_size_error on error.
266 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
269 return isl_size_error
;
273 /* Return the number of inequality constraints in the description of "bset".
274 * Return isl_size_error on error.
276 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
278 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
281 /* Do "bmap1" and "bmap2" have the same parameters?
283 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
284 __isl_keep isl_basic_map
*bmap2
)
286 isl_space
*space1
, *space2
;
288 space1
= isl_basic_map_peek_space(bmap1
);
289 space2
= isl_basic_map_peek_space(bmap2
);
290 return isl_space_has_equal_params(space1
, space2
);
293 /* Do "map1" and "map2" have the same parameters?
295 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
296 __isl_keep isl_map
*map2
)
298 isl_space
*space1
, *space2
;
300 space1
= isl_map_peek_space(map1
);
301 space2
= isl_map_peek_space(map2
);
302 return isl_space_has_equal_params(space1
, space2
);
305 /* Do "map" and "set" have the same parameters?
307 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
308 __isl_keep isl_set
*set
)
310 return isl_map_has_equal_params(map
, set_to_map(set
));
313 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
315 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
316 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
318 isl_space
*bmap_space
, *bset_space
;
320 bmap_space
= isl_basic_map_peek_space(bmap
);
321 bset_space
= isl_basic_set_peek_space(bset
);
322 return isl_space_tuple_is_equal(bmap_space
, type
,
323 bset_space
, isl_dim_set
);
326 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
328 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
329 enum isl_dim_type type
, __isl_keep isl_set
*set
)
331 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
334 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
335 __isl_keep isl_set
*set
)
339 return isl_bool_error
;
340 m
= isl_map_has_equal_params(map
, set_to_map(set
));
343 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
346 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
347 __isl_keep isl_basic_set
*bset
)
351 return isl_bool_error
;
352 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
355 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
358 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
359 __isl_keep isl_set
*set
)
363 return isl_bool_error
;
364 m
= isl_map_has_equal_params(map
, set_to_map(set
));
367 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
370 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
371 __isl_keep isl_basic_set
*bset
)
375 return isl_bool_error
;
376 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
379 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
382 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
384 return bmap
? bmap
->ctx
: NULL
;
387 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
389 return bset
? bset
->ctx
: NULL
;
392 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
394 return map
? map
->ctx
: NULL
;
397 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
399 return set
? set
->ctx
: NULL
;
402 /* Return the space of "bmap".
404 __isl_keep isl_space
*isl_basic_map_peek_space(
405 __isl_keep
const isl_basic_map
*bmap
)
407 return bmap
? bmap
->dim
: NULL
;
410 /* Return the space of "bset".
412 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
414 return isl_basic_map_peek_space(bset_to_bmap(bset
));
417 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
419 return isl_space_copy(isl_basic_map_peek_space(bmap
));
422 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
424 return isl_basic_map_get_space(bset_to_bmap(bset
));
427 /* Return the space of "bmap".
428 * This may be either a copy or the space itself
429 * if there is only one reference to "bmap".
430 * This allows the space to be modified inplace
431 * if both the basic map and its space have only a single reference.
432 * The caller is not allowed to modify "bmap" between this call and
433 * a subsequent call to isl_basic_map_restore_space.
434 * The only exception is that isl_basic_map_free can be called instead.
436 static __isl_give isl_space
*isl_basic_map_take_space(
437 __isl_keep isl_basic_map
*bmap
)
444 return isl_basic_map_get_space(bmap
);
450 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
451 * due to a preceding call to isl_basic_map_take_space.
452 * However, in this case, "bmap" only has a single reference and
453 * then the call to isl_basic_map_cow has no effect.
455 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
456 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
461 if (bmap
->dim
== space
) {
462 isl_space_free(space
);
466 bmap
= isl_basic_map_cow(bmap
);
469 isl_space_free(bmap
->dim
);
474 isl_basic_map_free(bmap
);
475 isl_space_free(space
);
479 /* Extract the divs in "bmap" as a matrix.
481 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
489 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
493 ctx
= isl_basic_map_get_ctx(bmap
);
494 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
495 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
499 for (i
= 0; i
< bmap
->n_div
; ++i
)
500 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
505 /* Extract the divs in "bset" as a matrix.
507 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
509 return isl_basic_map_get_divs(bset
);
512 __isl_give isl_local_space
*isl_basic_map_get_local_space(
513 __isl_keep isl_basic_map
*bmap
)
520 div
= isl_basic_map_get_divs(bmap
);
521 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
524 __isl_give isl_local_space
*isl_basic_set_get_local_space(
525 __isl_keep isl_basic_set
*bset
)
527 return isl_basic_map_get_local_space(bset
);
530 /* For each known div d = floor(f/m), add the constraints
533 * -(f-(m-1)) + m d >= 0
535 * Do not finalize the result.
537 static __isl_give isl_basic_map
*add_known_div_constraints(
538 __isl_take isl_basic_map
*bmap
)
543 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
545 return isl_basic_map_free(bmap
);
548 bmap
= isl_basic_map_cow(bmap
);
549 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
552 for (i
= 0; i
< n_div
; ++i
) {
553 if (isl_int_is_zero(bmap
->div
[i
][0]))
555 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
561 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
562 __isl_take isl_local_space
*ls
)
568 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
570 ls
= isl_local_space_free(ls
);
574 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
575 n_div
, 0, 2 * n_div
);
577 for (i
= 0; i
< n_div
; ++i
)
578 if (isl_basic_map_alloc_div(bmap
) < 0)
581 for (i
= 0; i
< n_div
; ++i
)
582 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
583 bmap
= add_known_div_constraints(bmap
);
585 isl_local_space_free(ls
);
588 isl_local_space_free(ls
);
589 isl_basic_map_free(bmap
);
593 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
594 __isl_take isl_local_space
*ls
)
596 return isl_basic_map_from_local_space(ls
);
599 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
601 return isl_space_copy(isl_map_peek_space(map
));
604 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
608 return isl_space_copy(set
->dim
);
611 /* Return the space of "map".
612 * This may be either a copy or the space itself
613 * if there is only one reference to "map".
614 * This allows the space to be modified inplace
615 * if both the map and its space have only a single reference.
616 * The caller is not allowed to modify "map" between this call and
617 * a subsequent call to isl_map_restore_space.
618 * The only exception is that isl_map_free can be called instead.
620 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
627 return isl_map_get_space(map
);
633 /* Set the space of "map" to "space", where the space of "map" may be missing
634 * due to a preceding call to isl_map_take_space.
635 * However, in this case, "map" only has a single reference and
636 * then the call to isl_map_cow has no effect.
638 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
639 __isl_take isl_space
*space
)
644 if (map
->dim
== space
) {
645 isl_space_free(space
);
649 map
= isl_map_cow(map
);
652 isl_space_free(map
->dim
);
658 isl_space_free(space
);
662 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
663 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
667 space
= isl_basic_map_take_space(bmap
);
668 space
= isl_space_set_tuple_name(space
, type
, s
);
669 bmap
= isl_basic_map_restore_space(bmap
, space
);
670 bmap
= isl_basic_map_finalize(bmap
);
674 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
675 __isl_take isl_basic_set
*bset
, const char *s
)
677 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
680 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
681 enum isl_dim_type type
)
683 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
686 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
687 enum isl_dim_type type
, const char *s
)
692 map
= isl_map_cow(map
);
696 for (i
= 0; i
< map
->n
; ++i
) {
697 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
702 space
= isl_map_take_space(map
);
703 space
= isl_space_set_tuple_name(space
, type
, s
);
704 map
= isl_map_restore_space(map
, space
);
712 /* Replace the identifier of the tuple of type "type" by "id".
714 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
715 __isl_take isl_basic_map
*bmap
,
716 enum isl_dim_type type
, __isl_take isl_id
*id
)
720 space
= isl_basic_map_take_space(bmap
);
721 space
= isl_space_set_tuple_id(space
, type
, id
);
722 bmap
= isl_basic_map_restore_space(bmap
, space
);
723 bmap
= isl_basic_map_finalize(bmap
);
727 /* Replace the identifier of the tuple by "id".
729 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
730 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
732 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
735 /* Does the input or output tuple have a name?
737 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
739 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
742 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
743 enum isl_dim_type type
)
745 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
748 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
751 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
755 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
756 enum isl_dim_type type
, __isl_take isl_id
*id
)
760 space
= isl_map_take_space(map
);
761 space
= isl_space_set_tuple_id(space
, type
, id
);
762 map
= isl_map_restore_space(map
, space
);
764 return isl_map_reset_space(map
, isl_map_get_space(map
));
767 /* Replace the identifier of the domain tuple of "map" by "id".
769 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
770 __isl_take isl_id
*id
)
772 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
775 /* Replace the identifier of the range tuple of "map" by "id".
777 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
778 __isl_take isl_id
*id
)
780 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
783 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
784 __isl_take isl_id
*id
)
786 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
789 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
790 enum isl_dim_type type
)
794 space
= isl_map_take_space(map
);
795 space
= isl_space_reset_tuple_id(space
, type
);
796 map
= isl_map_restore_space(map
, space
);
798 return isl_map_reset_space(map
, isl_map_get_space(map
));
801 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
803 return isl_map_reset_tuple_id(set
, isl_dim_set
);
806 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
808 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
811 /* Does the domain tuple of "map" have an identifier?
813 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
815 return isl_map_has_tuple_id(map
, isl_dim_in
);
818 /* Does the range tuple of "map" have an identifier?
820 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
822 return isl_map_has_tuple_id(map
, isl_dim_out
);
825 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
826 enum isl_dim_type type
)
828 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
831 /* Return the identifier of the domain tuple of "map", assuming it has one.
833 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
835 return isl_map_get_tuple_id(map
, isl_dim_in
);
838 /* Return the identifier of the range tuple of "map", assuming it has one.
840 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
842 return isl_map_get_tuple_id(map
, isl_dim_out
);
845 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
847 return isl_map_has_tuple_id(set
, isl_dim_set
);
850 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
852 return isl_map_get_tuple_id(set
, isl_dim_set
);
855 /* Does the set tuple have a name?
857 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
860 return isl_bool_error
;
861 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
865 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
867 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
870 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
872 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
875 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
876 enum isl_dim_type type
, unsigned pos
)
878 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
881 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
882 enum isl_dim_type type
, unsigned pos
)
884 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
887 /* Does the given dimension have a name?
889 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
890 enum isl_dim_type type
, unsigned pos
)
893 return isl_bool_error
;
894 return isl_space_has_dim_name(map
->dim
, type
, pos
);
897 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
898 enum isl_dim_type type
, unsigned pos
)
900 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
903 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
904 enum isl_dim_type type
, unsigned pos
)
906 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
909 /* Does the given dimension have a name?
911 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
912 enum isl_dim_type type
, unsigned pos
)
915 return isl_bool_error
;
916 return isl_space_has_dim_name(set
->dim
, type
, pos
);
919 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
920 __isl_take isl_basic_map
*bmap
,
921 enum isl_dim_type type
, unsigned pos
, const char *s
)
925 space
= isl_basic_map_take_space(bmap
);
926 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
927 bmap
= isl_basic_map_restore_space(bmap
, space
);
928 return isl_basic_map_finalize(bmap
);
931 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
932 enum isl_dim_type type
, unsigned pos
, const char *s
)
937 map
= isl_map_cow(map
);
941 for (i
= 0; i
< map
->n
; ++i
) {
942 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
947 space
= isl_map_take_space(map
);
948 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
949 map
= isl_map_restore_space(map
, space
);
957 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
958 __isl_take isl_basic_set
*bset
,
959 enum isl_dim_type type
, unsigned pos
, const char *s
)
961 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
965 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
966 enum isl_dim_type type
, unsigned pos
, const char *s
)
968 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
972 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
973 enum isl_dim_type type
, unsigned pos
)
976 return isl_bool_error
;
977 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
980 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
981 enum isl_dim_type type
, unsigned pos
)
983 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
986 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
987 enum isl_dim_type type
, unsigned pos
)
989 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
992 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
993 enum isl_dim_type type
, unsigned pos
)
995 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
998 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
999 enum isl_dim_type type
, unsigned pos
)
1001 return isl_map_has_dim_id(set
, type
, pos
);
1004 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
1005 enum isl_dim_type type
, unsigned pos
)
1007 return isl_map_get_dim_id(set
, type
, pos
);
1010 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
1011 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1015 space
= isl_map_take_space(map
);
1016 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1017 map
= isl_map_restore_space(map
, space
);
1019 return isl_map_reset_space(map
, isl_map_get_space(map
));
1022 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1023 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1025 return isl_map_set_dim_id(set
, type
, pos
, id
);
1028 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1029 __isl_keep isl_id
*id
)
1033 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1036 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1037 __isl_keep isl_id
*id
)
1039 return isl_map_find_dim_by_id(set
, type
, id
);
1042 /* Return the position of the dimension of the given type and name
1044 * Return -1 if no such dimension can be found.
1046 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1047 enum isl_dim_type type
, const char *name
)
1051 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1054 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1059 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1062 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1065 return isl_map_find_dim_by_name(set
, type
, name
);
1068 /* Check whether equality i of bset is a pure stride constraint
1069 * on a single dimension, i.e., of the form
1073 * with k a constant and e an existentially quantified variable.
1075 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1083 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1084 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1085 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1086 if (nparam
< 0 || d
< 0 || n_div
< 0)
1087 return isl_bool_error
;
1089 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1090 return isl_bool_false
;
1092 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1093 return isl_bool_false
;
1094 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1096 return isl_bool_false
;
1097 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1098 d
- pos1
- 1) != -1)
1099 return isl_bool_false
;
1101 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1103 return isl_bool_false
;
1104 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1105 n_div
- pos2
- 1) != -1)
1106 return isl_bool_false
;
1107 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1108 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1109 return isl_bool_false
;
1111 return isl_bool_true
;
1114 /* Reset the user pointer on all identifiers of parameters and tuples
1115 * of the space of "map".
1117 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1121 space
= isl_map_get_space(map
);
1122 space
= isl_space_reset_user(space
);
1123 map
= isl_map_reset_space(map
, space
);
1128 /* Reset the user pointer on all identifiers of parameters and tuples
1129 * of the space of "set".
1131 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1133 return isl_map_reset_user(set
);
1136 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1139 return isl_bool_error
;
1140 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1143 /* Has "map" been marked as a rational map?
1144 * In particular, have all basic maps in "map" been marked this way?
1145 * An empty map is not considered to be rational.
1146 * Maps where only some of the basic maps are marked rational
1149 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1155 return isl_bool_error
;
1157 return isl_bool_false
;
1158 rational
= isl_basic_map_is_rational(map
->p
[0]);
1161 for (i
= 1; i
< map
->n
; ++i
) {
1162 isl_bool rational_i
;
1164 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1167 if (rational
!= rational_i
)
1168 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1169 "mixed rational and integer basic maps "
1170 "not supported", return isl_bool_error
);
1176 /* Has "set" been marked as a rational set?
1177 * In particular, have all basic set in "set" been marked this way?
1178 * An empty set is not considered to be rational.
1179 * Sets where only some of the basic sets are marked rational
1182 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1184 return isl_map_is_rational(set
);
1187 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1189 return isl_basic_map_is_rational(bset
);
1192 /* Does "bmap" contain any rational points?
1194 * If "bmap" has an equality for each dimension, equating the dimension
1195 * to an integer constant, then it has no rational points, even if it
1196 * is marked as rational.
1198 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1200 isl_bool has_rational
= isl_bool_true
;
1204 return isl_bool_error
;
1205 if (isl_basic_map_plain_is_empty(bmap
))
1206 return isl_bool_false
;
1207 if (!isl_basic_map_is_rational(bmap
))
1208 return isl_bool_false
;
1209 bmap
= isl_basic_map_copy(bmap
);
1210 bmap
= isl_basic_map_implicit_equalities(bmap
);
1211 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1213 return isl_bool_error
;
1214 if (bmap
->n_eq
== total
) {
1216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1217 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1220 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1221 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1223 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1228 if (i
== bmap
->n_eq
)
1229 has_rational
= isl_bool_false
;
1231 isl_basic_map_free(bmap
);
1233 return has_rational
;
1236 /* Does "map" contain any rational points?
1238 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1241 isl_bool has_rational
;
1244 return isl_bool_error
;
1245 for (i
= 0; i
< map
->n
; ++i
) {
1246 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1247 if (has_rational
< 0 || has_rational
)
1248 return has_rational
;
1250 return isl_bool_false
;
1253 /* Does "set" contain any rational points?
1255 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1257 return isl_map_has_rational(set
);
1260 /* Is this basic set a parameter domain?
1262 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1265 return isl_bool_error
;
1266 return isl_space_is_params(bset
->dim
);
1269 /* Is this set a parameter domain?
1271 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1274 return isl_bool_error
;
1275 return isl_space_is_params(set
->dim
);
1278 /* Is this map actually a parameter domain?
1279 * Users should never call this function. Outside of isl,
1280 * a map can never be a parameter domain.
1282 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1285 return isl_bool_error
;
1286 return isl_space_is_params(map
->dim
);
1289 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1290 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1291 unsigned n_eq
, unsigned n_ineq
)
1294 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1295 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1296 size_t row_size
= 1 + n_var
+ extra
;
1302 return isl_basic_map_free(bmap
);
1304 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1305 if (isl_blk_is_error(bmap
->block
))
1308 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1309 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1313 bmap
->block2
= isl_blk_empty();
1316 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1317 if (isl_blk_is_error(bmap
->block2
))
1320 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1325 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1326 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1328 for (i
= 0; i
< extra
; ++i
)
1329 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1333 bmap
->c_size
= n_eq
+ n_ineq
;
1334 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1335 bmap
->extra
= extra
;
1339 bmap
->sample
= NULL
;
1343 isl_basic_map_free(bmap
);
1347 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1348 unsigned nparam
, unsigned dim
, unsigned extra
,
1349 unsigned n_eq
, unsigned n_ineq
)
1351 struct isl_basic_map
*bmap
;
1354 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1358 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1359 return bset_from_bmap(bmap
);
1362 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1363 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1365 struct isl_basic_map
*bmap
;
1368 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1369 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1370 return bset_from_bmap(bmap
);
1372 isl_space_free(space
);
1376 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1377 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1379 struct isl_basic_map
*bmap
;
1383 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1388 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1390 isl_space_free(space
);
1394 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1395 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1396 unsigned n_eq
, unsigned n_ineq
)
1398 struct isl_basic_map
*bmap
;
1401 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1405 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1409 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1410 __isl_keep isl_basic_map
*src
)
1413 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1415 if (!dst
|| total
< 0)
1416 return isl_basic_map_free(dst
);
1418 for (i
= 0; i
< src
->n_eq
; ++i
) {
1419 int j
= isl_basic_map_alloc_equality(dst
);
1421 return isl_basic_map_free(dst
);
1422 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1425 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1426 int j
= isl_basic_map_alloc_inequality(dst
);
1428 return isl_basic_map_free(dst
);
1429 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1432 for (i
= 0; i
< src
->n_div
; ++i
) {
1433 int j
= isl_basic_map_alloc_div(dst
);
1435 return isl_basic_map_free(dst
);
1436 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1438 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1442 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1444 struct isl_basic_map
*dup
;
1448 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1449 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1450 dup
= dup_constraints(dup
, bmap
);
1453 dup
->flags
= bmap
->flags
;
1454 dup
->sample
= isl_vec_copy(bmap
->sample
);
1458 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1460 struct isl_basic_map
*dup
;
1462 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1463 return bset_from_bmap(dup
);
1466 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1468 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset
)));
1471 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1480 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1485 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1489 bmap
= isl_basic_map_dup(bmap
);
1491 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1495 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1504 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1509 if (--bmap
->ref
> 0)
1512 isl_ctx_deref(bmap
->ctx
);
1514 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1516 isl_blk_free(bmap
->ctx
, bmap
->block
);
1517 isl_vec_free(bmap
->sample
);
1518 isl_space_free(bmap
->dim
);
1524 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1526 return isl_basic_map_free(bset_to_bmap(bset
));
1529 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1531 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1534 /* Check that "bset" does not involve any parameters.
1536 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1540 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1542 return isl_stat_error
;
1544 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1545 "basic set should not have any parameters",
1546 return isl_stat_error
);
1550 /* Check that "bset" does not involve any local variables.
1552 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1556 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1558 return isl_stat_error
;
1560 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1561 "basic set should not have any local variables",
1562 return isl_stat_error
);
1567 #define TYPE isl_map
1569 #include "isl_check_named_params_templ.c"
1572 #define TYPE isl_basic_map
1575 #include "isl_check_named_params_templ.c"
1577 /* Check that "bmap1" and "bmap2" have the same parameters,
1578 * reporting an error if they do not.
1580 static isl_stat
isl_basic_map_check_equal_params(
1581 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1585 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1587 return isl_stat_error
;
1589 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1590 "parameters don't match", return isl_stat_error
);
1595 #define TYPE isl_map
1597 #include "isl_align_params_bin_templ.c"
1602 #define ARG1 isl_map
1604 #define ARG2 isl_set
1606 #include "isl_align_params_templ.c"
1608 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1609 __isl_keep isl_map
*map2
,
1610 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1615 return isl_bool_error
;
1616 if (isl_map_has_equal_params(map1
, map2
))
1617 return fn(map1
, map2
);
1618 if (isl_map_check_named_params(map1
) < 0)
1619 return isl_bool_error
;
1620 if (isl_map_check_named_params(map2
) < 0)
1621 return isl_bool_error
;
1622 map1
= isl_map_copy(map1
);
1623 map2
= isl_map_copy(map2
);
1624 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1625 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1632 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1635 struct isl_ctx
*ctx
;
1637 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1641 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1642 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1644 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1645 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1646 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1647 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1648 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1650 int j
= isl_basic_map_alloc_inequality(bmap
);
1654 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1655 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1662 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1663 bmap
->extra
- bmap
->n_div
);
1664 return bmap
->n_eq
++;
1667 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1669 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1672 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1673 __isl_take isl_basic_map
*bmap
, unsigned n
)
1678 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1679 "invalid number of equalities",
1680 isl_basic_map_free(bmap
));
1685 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1686 __isl_take isl_basic_set
*bset
, unsigned n
)
1688 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1692 /* Drop the equality constraint at position "pos",
1693 * preserving the order of the other equality constraints.
1695 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1702 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1706 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1707 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1708 bmap
->eq
[bmap
->n_eq
] = t
;
1713 /* Turn inequality "pos" of "bmap" into an equality.
1715 * In particular, we move the inequality in front of the equalities
1716 * and move the last inequality in the position of the moved inequality.
1717 * Note that isl_tab_make_equalities_explicit depends on this particular
1718 * change in the ordering of the constraints.
1720 void isl_basic_map_inequality_to_equality(
1721 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1725 t
= bmap
->ineq
[pos
];
1726 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1727 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1735 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1738 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1740 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1743 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1746 struct isl_ctx
*ctx
;
1748 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1752 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1753 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1754 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1755 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1756 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1757 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1758 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1759 bmap
->extra
- bmap
->n_div
);
1760 return bmap
->n_ineq
++;
1763 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1765 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1768 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1769 __isl_take isl_basic_map
*bmap
, unsigned n
)
1773 if (n
> bmap
->n_ineq
)
1774 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1775 "invalid number of inequalities",
1776 return isl_basic_map_free(bmap
));
1781 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1782 __isl_take isl_basic_set
*bset
, unsigned n
)
1784 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1788 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1793 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1795 if (pos
!= bmap
->n_ineq
- 1) {
1796 t
= bmap
->ineq
[pos
];
1797 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1798 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1799 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1805 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1807 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1810 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1817 empty
= isl_basic_map_plain_is_empty(bmap
);
1819 return isl_basic_map_free(bmap
);
1823 bmap
= isl_basic_map_cow(bmap
);
1824 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1825 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1827 return isl_basic_map_free(bmap
);
1828 k
= isl_basic_map_alloc_equality(bmap
);
1831 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1834 isl_basic_map_free(bmap
);
1838 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1841 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1844 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1850 bmap
= isl_basic_map_cow(bmap
);
1851 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1852 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1854 return isl_basic_map_free(bmap
);
1855 k
= isl_basic_map_alloc_inequality(bmap
);
1858 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1861 isl_basic_map_free(bmap
);
1865 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1868 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1871 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1875 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1878 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1879 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1880 bmap
->extra
- bmap
->n_div
);
1881 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1882 return bmap
->n_div
++;
1885 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1887 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1891 #define TYPE isl_basic_map
1892 #include "check_type_range_templ.c"
1894 /* Check that there are "n" dimensions of type "type" starting at "first"
1897 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1898 enum isl_dim_type type
, unsigned first
, unsigned n
)
1900 return isl_basic_map_check_range(bset_to_bmap(bset
),
1904 /* Insert an extra integer division, prescribed by "div", to "bmap"
1905 * at (integer division) position "pos".
1907 * The integer division is first added at the end and then moved
1908 * into the right position.
1910 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1911 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1916 bmap
= isl_basic_map_cow(bmap
);
1917 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1918 if (total
< 0 || !div
)
1919 return isl_basic_map_free(bmap
);
1921 if (div
->size
!= 1 + 1 + total
)
1922 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1923 "unexpected size", return isl_basic_map_free(bmap
));
1924 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1925 return isl_basic_map_free(bmap
);
1927 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1928 k
= isl_basic_map_alloc_div(bmap
);
1930 return isl_basic_map_free(bmap
);
1931 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1932 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1934 for (i
= k
; i
> pos
; --i
)
1935 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1940 /* Insert an extra integer division, prescribed by "div", to "bset"
1941 * at (integer division) position "pos".
1943 __isl_give isl_basic_set
*isl_basic_set_insert_div(
1944 __isl_take isl_basic_set
*bset
, int pos
, __isl_keep isl_vec
*div
)
1946 isl_basic_map
*bmap
= bset_to_bmap(bset
);
1947 bmap
= isl_basic_map_insert_div(bmap
, pos
, div
);
1948 return bset_from_bmap(bmap
);
1951 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1954 return isl_stat_error
;
1955 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1960 static __isl_give isl_basic_map
*add_constraints(
1961 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1962 unsigned i_pos
, unsigned o_pos
)
1964 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1965 unsigned o_in
, o_out
;
1968 struct isl_dim_map
*dim_map
;
1970 space
= isl_basic_map_peek_space(bmap2
);
1971 if (!bmap1
|| !space
)
1974 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1975 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1976 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1977 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1978 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1979 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1980 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1981 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1983 ctx
= isl_basic_map_get_ctx(bmap1
);
1984 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1985 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1986 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1987 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1988 isl_dim_map_div(dim_map
, bmap2
, total
);
1990 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1992 isl_basic_map_free(bmap1
);
1993 isl_basic_map_free(bmap2
);
1997 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1998 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2001 struct isl_basic_map
*ext
;
2008 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
2010 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
2011 room_for_ineq(base
, n_ineq
))
2014 extra
+= base
->extra
;
2016 n_ineq
+= base
->n_ineq
;
2018 space
= isl_basic_map_get_space(base
);
2019 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2024 ext
->sample
= isl_vec_copy(base
->sample
);
2025 flags
= base
->flags
;
2026 ext
= add_constraints(ext
, base
, 0, 0);
2029 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2035 isl_basic_map_free(base
);
2039 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2040 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2042 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2043 extra
, n_eq
, n_ineq
));
2046 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2047 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2049 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2052 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2053 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2055 isl_basic_map
*bmap
= bset_to_bmap(base
);
2056 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2057 return bset_from_bmap(bmap
);
2060 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2062 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2065 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2070 if (bmap
->ref
> 1) {
2072 bmap
= isl_basic_map_dup(bmap
);
2075 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2079 /* Clear all cached information in "map", either because it is about
2080 * to be modified or because it is being freed.
2081 * Always return the same pointer that is passed in.
2082 * This is needed for the use in isl_map_free.
2084 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2086 isl_basic_map_free(map
->cached_simple_hull
[0]);
2087 isl_basic_map_free(map
->cached_simple_hull
[1]);
2088 map
->cached_simple_hull
[0] = NULL
;
2089 map
->cached_simple_hull
[1] = NULL
;
2093 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2095 return isl_map_cow(set
);
2098 /* Return an isl_map that is equal to "map" and that has only
2099 * a single reference.
2101 * If the original input already has only one reference, then
2102 * simply return it, but clear all cached information, since
2103 * it may be rendered invalid by the operations that will be
2104 * performed on the result.
2106 * Otherwise, create a duplicate (without any cached information).
2108 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2114 return clear_caches(map
);
2116 return isl_map_dup(map
);
2119 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2120 unsigned a_len
, unsigned b_len
)
2122 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2123 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2124 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2127 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2128 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2133 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2136 if (n1
== 0 || n2
== 0)
2139 bmap
= isl_basic_map_cow(bmap
);
2143 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2144 if (isl_blk_is_error(blk
))
2147 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2149 bmap
->eq
[i
] + pos
, n1
, n2
);
2151 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2153 bmap
->ineq
[i
] + pos
, n1
, n2
);
2155 for (i
= 0; i
< bmap
->n_div
; ++i
)
2157 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2159 isl_blk_free(bmap
->ctx
, blk
);
2161 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2162 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2163 return isl_basic_map_finalize(bmap
);
2165 isl_basic_map_free(bmap
);
2169 /* The given basic map has turned out to be empty.
2170 * Explicitly mark it as such and change the representation
2171 * to a canonical representation of the empty basic map.
2172 * Since the basic map has conflicting constraints,
2173 * it must have at least one constraint, except perhaps
2174 * if it was already explicitly marked as being empty.
2175 * Do nothing in the latter case, i.e., if it has been marked empty and
2176 * has no constraints.
2178 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2179 __isl_take isl_basic_map
*bmap
)
2186 n
= isl_basic_map_n_constraint(bmap
);
2187 empty
= isl_basic_map_plain_is_empty(bmap
);
2188 if (n
< 0 || empty
< 0)
2189 return isl_basic_map_free(bmap
);
2190 if (n
== 0 && empty
)
2192 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2194 return isl_basic_map_free(bmap
);
2195 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2196 return isl_basic_map_free(bmap
);
2197 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2200 if (bmap
->n_eq
> 0) {
2201 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2205 i
= isl_basic_map_alloc_equality(bmap
);
2209 isl_int_set_si(bmap
->eq
[i
][0], 1);
2210 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2211 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2212 isl_vec_free(bmap
->sample
);
2213 bmap
->sample
= NULL
;
2214 return isl_basic_map_finalize(bmap
);
2216 isl_basic_map_free(bmap
);
2220 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2221 __isl_take isl_basic_set
*bset
)
2223 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2226 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2227 __isl_take isl_basic_map
*bmap
)
2232 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2235 bmap
= isl_basic_map_cow(bmap
);
2239 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2241 return isl_basic_map_finalize(bmap
);
2244 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2245 __isl_take isl_basic_set
*bset
)
2247 return isl_basic_map_set_rational(bset
);
2250 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2251 __isl_take isl_basic_set
*bset
)
2256 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2259 bset
= isl_basic_set_cow(bset
);
2263 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2265 return isl_basic_set_finalize(bset
);
2268 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2272 map
= isl_map_cow(map
);
2275 for (i
= 0; i
< map
->n
; ++i
) {
2276 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2286 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2288 return isl_map_set_rational(set
);
2291 /* Given a constraint "c" that expresses a bound
2292 * on the variable at position "pos" in terms of the first "len" variables
2293 * (other than the variable itself if pos < len), extract this bound
2294 * as a function of those first "len" variables.
2296 * That is, the constraint is of one of the following forms
2298 * -e(...) + m x >= 0
2303 * Return (e(...)) / m, with the denominator m in the first position.
2305 static __isl_give isl_vec
*extract_bound_from_constraint(isl_ctx
*ctx
,
2306 isl_int
*c
, int len
, int pos
)
2310 v
= isl_vec_alloc(ctx
, 1 + 1 + len
);
2313 if (isl_int_is_pos(c
[1 + pos
])) {
2314 isl_int_set(v
->el
[0], c
[1 + pos
]);
2315 isl_seq_neg(v
->el
+ 1, c
, 1 + len
);
2317 isl_int_neg(v
->el
[0], c
[1 + pos
]);
2318 isl_seq_cpy(v
->el
+ 1, c
, 1 + len
);
2321 isl_int_set_si(v
->el
[1 + 1 + pos
], 0);
2326 /* Return the position of the last non-zero coefficient of
2327 * inequality constraint "ineq" of "bmap" or length "len",
2328 * given that the coefficient at position "first" is non-zero,
2329 * or that it is known that there is at least one coefficient
2330 * after "first" that is non-zero.
2332 static int extend_last_non_zero(__isl_keep isl_basic_map
*bmap
, int ineq
,
2333 int first
, unsigned len
)
2337 last
= isl_seq_last_non_zero(bmap
->ineq
[ineq
] + 1 + first
+ 1,
2342 return first
+ 1 + last
;
2345 /* Do the inequality constraints "i" and "j" of "bmap"
2346 * form a pair of opposite constraints, in the (first) "len" coefficients?
2348 static int is_constraint_pair(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
2351 return isl_seq_is_neg(bmap
->ineq
[i
] + 1, bmap
->ineq
[j
] + 1, len
);
2354 /* Given that inequality constraints "i" and "j" of "bmap"
2355 * form a pair of opposite constraints
2364 * do they allow for at most "bound" values in that direction?
2365 * That is, is the sum of their constant terms smaller than "bound"?
2367 * "tmp" is a temporary location that can be used to store the sum.
2369 static int constraint_pair_has_bound(__isl_keep isl_basic_map
*bmap
,
2370 int i
, int j
, isl_int bound
, isl_int
*tmp
)
2372 isl_int_add(*tmp
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
2373 return isl_int_abs_lt(*tmp
, bound
);
2376 /* Return the position of an inequality constraint in "bmap"
2377 * that together with inequality constraint "ineq" forms
2378 * a pair of opposite constraints that allow at most "bound" values
2379 * in their shared direction and that appears before "ineq".
2380 * Return a position beyond the number of inequality constraints
2381 * if no such constraint can be found.
2383 * The constraints of "bmap" are assumed to have been sorted.
2384 * This means that as soon as a constraint is found where the value
2385 * of the last coefficient (in absolute value) is different from that of "ineq",
2386 * no opposite constraint can be found.
2387 * It also means that only the coefficients up to this last coefficient
2388 * need to be compared.
2390 * "pos" is the position of a coefficient that is known to be non-zero.
2391 * If no such position is known a priori, then the value 0 can be passed in.
2392 * "len" is the number of (relevant) coefficients in the constraints.
2393 * "tmp" is a temporary location that can be used to store the sum.
2395 static isl_size
find_earlier_constraint_in_pair(__isl_keep isl_basic_map
*bmap
,
2396 int ineq
, int pos
, int len
, isl_int bound
, isl_int
*tmp
)
2402 n_ineq
= isl_basic_map_n_inequality(bmap
);
2404 return isl_size_error
;
2406 last
= extend_last_non_zero(bmap
, ineq
, pos
, len
);
2408 for (j
= ineq
- 1; j
>= 0; --j
) {
2409 if (!isl_int_abs_eq(bmap
->ineq
[ineq
][1 + last
],
2410 bmap
->ineq
[j
][1 + last
]))
2412 if (!is_constraint_pair(bmap
, ineq
, j
, last
+ 1))
2414 if (constraint_pair_has_bound(bmap
, ineq
, j
, bound
, tmp
))
2422 /* Return the position of an inequality constraint in "bmap"
2423 * that together with inequality constraint "ineq" forms
2424 * a pair of opposite constraints that allow at most "bound" values
2425 * in their shared direction and that appears after "ineq".
2426 * Return a position beyond the number of inequality constraints
2427 * if no such constraint can be found.
2429 * The constraints of "bmap" are assumed to have been sorted.
2430 * This means that as soon as a constraint is found where the value
2431 * of the last coefficient (in absolute value) is different from that of "ineq",
2432 * no opposite constraint can be found.
2433 * It also means that only the coefficients up to this last coefficient
2434 * need to be compared.
2436 * "pos" is the position of a coefficient that is known to be non-zero.
2437 * If no such position is known a priori, then the value 0 can be passed in.
2438 * "len" is the number of (relevant) coefficients in the constraints.
2439 * "tmp" is a temporary location that can be used to store the sum.
2441 static isl_size
find_later_constraint_in_pair(__isl_keep isl_basic_map
*bmap
,
2442 int ineq
, int pos
, int len
, isl_int bound
, isl_int
*tmp
)
2448 n_ineq
= isl_basic_map_n_inequality(bmap
);
2450 return isl_size_error
;
2452 last
= extend_last_non_zero(bmap
, ineq
, pos
, len
);
2454 for (j
= ineq
+ 1; j
< n_ineq
; ++j
) {
2455 if (!isl_int_abs_eq(bmap
->ineq
[ineq
][1 + last
],
2456 bmap
->ineq
[j
][1 + last
]))
2458 if (isl_seq_last_non_zero(bmap
->ineq
[j
] + 1 + last
+ 1,
2459 len
- (last
+ 1)) != -1)
2461 if (!is_constraint_pair(bmap
, ineq
, j
, last
+ 1))
2463 if (constraint_pair_has_bound(bmap
, ineq
, j
, bound
, tmp
))
2471 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2474 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2476 isl_int
*t
= bmap
->div
[a
];
2477 bmap
->div
[a
] = bmap
->div
[b
];
2481 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2482 * div definitions accordingly.
2484 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2490 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2492 return isl_basic_map_free(bmap
);
2494 swap_div(bmap
, a
, b
);
2496 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2497 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2499 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2500 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2502 for (i
= 0; i
< bmap
->n_div
; ++i
)
2503 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2509 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2511 isl_seq_cpy(c
, c
+ n
, rem
);
2512 isl_seq_clr(c
+ rem
, n
);
2515 /* Drop n dimensions starting at first.
2517 * In principle, this frees up some extra variables as the number
2518 * of columns remains constant, but we would have to extend
2519 * the div array too as the number of rows in this array is assumed
2520 * to be equal to extra.
2522 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2523 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2525 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2528 /* Move "n" divs starting at "first" to the end of the list of divs.
2530 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2531 unsigned first
, unsigned n
)
2536 if (first
+ n
== bmap
->n_div
)
2539 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2542 for (i
= 0; i
< n
; ++i
)
2543 div
[i
] = bmap
->div
[first
+ i
];
2544 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2545 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2546 for (i
= 0; i
< n
; ++i
)
2547 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2551 isl_basic_map_free(bmap
);
2556 #define TYPE isl_map
2558 #include "check_type_range_templ.c"
2560 /* Check that there are "n" dimensions of type "type" starting at "first"
2563 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2564 enum isl_dim_type type
, unsigned first
, unsigned n
)
2566 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2569 /* Drop "n" dimensions of type "type" starting at "first".
2570 * Perform the core computation, without cowing or
2571 * simplifying and finalizing the result.
2573 * In principle, this frees up some extra variables as the number
2574 * of columns remains constant, but we would have to extend
2575 * the div array too as the number of rows in this array is assumed
2576 * to be equal to extra.
2578 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2579 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2580 unsigned first
, unsigned n
)
2587 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2588 return isl_basic_map_free(bmap
);
2590 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2592 return isl_basic_map_free(bmap
);
2594 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2595 left
= total
- (offset
- 1) - n
;
2596 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2597 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2599 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2600 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2602 for (i
= 0; i
< bmap
->n_div
; ++i
)
2603 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2605 if (type
== isl_dim_div
) {
2606 bmap
= move_divs_last(bmap
, first
, n
);
2609 if (isl_basic_map_free_div(bmap
, n
) < 0)
2610 return isl_basic_map_free(bmap
);
2612 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2614 return isl_basic_map_free(bmap
);
2616 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2617 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2618 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2622 /* Drop "n" dimensions of type "type" starting at "first".
2624 * In principle, this frees up some extra variables as the number
2625 * of columns remains constant, but we would have to extend
2626 * the div array too as the number of rows in this array is assumed
2627 * to be equal to extra.
2629 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2630 enum isl_dim_type type
, unsigned first
, unsigned n
)
2634 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2637 bmap
= isl_basic_map_cow(bmap
);
2641 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2643 bmap
= isl_basic_map_simplify(bmap
);
2644 return isl_basic_map_finalize(bmap
);
2647 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2648 enum isl_dim_type type
, unsigned first
, unsigned n
)
2650 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2654 /* No longer consider "map" to be normalized.
2656 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2660 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2664 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2665 enum isl_dim_type type
, unsigned first
, unsigned n
)
2670 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2671 return isl_map_free(map
);
2673 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2675 map
= isl_map_cow(map
);
2679 for (i
= 0; i
< map
->n
; ++i
) {
2680 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2684 map
= isl_map_unmark_normalized(map
);
2686 space
= isl_map_take_space(map
);
2687 space
= isl_space_drop_dims(space
, type
, first
, n
);
2688 map
= isl_map_restore_space(map
, space
);
2696 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2697 enum isl_dim_type type
, unsigned first
, unsigned n
)
2699 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2702 /* Drop the integer division at position "div", which is assumed
2703 * not to appear in any of the constraints or
2704 * in any of the other integer divisions.
2706 * Since the integer division is redundant, there is no need to cow.
2708 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2709 __isl_take isl_basic_map
*bmap
, unsigned div
)
2711 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2714 /* Eliminate the specified n dimensions starting at first from the
2715 * constraints, without removing the dimensions from the space.
2716 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2718 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2719 enum isl_dim_type type
, unsigned first
, unsigned n
)
2726 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2727 return isl_map_free(map
);
2729 map
= isl_map_cow(map
);
2733 for (i
= 0; i
< map
->n
; ++i
) {
2734 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2744 /* Eliminate the specified n dimensions starting at first from the
2745 * constraints, without removing the dimensions from the space.
2746 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2748 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2749 enum isl_dim_type type
, unsigned first
, unsigned n
)
2751 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2754 /* Eliminate the specified n dimensions starting at first from the
2755 * constraints, without removing the dimensions from the space.
2756 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2758 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2759 unsigned first
, unsigned n
)
2761 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2764 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2765 __isl_take isl_basic_map
*bmap
)
2769 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2771 return isl_basic_map_free(bmap
);
2772 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2776 return isl_basic_map_finalize(bmap
);
2779 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2780 __isl_take isl_basic_set
*bset
)
2782 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2785 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2794 map
= isl_map_cow(map
);
2798 for (i
= 0; i
< map
->n
; ++i
) {
2799 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2809 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2811 return isl_map_remove_divs(set
);
2814 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2815 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2816 unsigned first
, unsigned n
)
2818 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2819 return isl_basic_map_free(bmap
);
2820 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2822 bmap
= isl_basic_map_eliminate_vars(bmap
,
2823 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2826 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2828 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2832 /* Does the local variable "div" of "bmap" have a known expression
2833 * that involves the "n" variables starting at "first"?
2835 isl_bool
isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map
*bmap
,
2836 int div
, unsigned first
, unsigned n
)
2840 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2841 if (unknown
< 0 || unknown
)
2842 return isl_bool_not(unknown
);
2843 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
) >= 0)
2844 return isl_bool_true
;
2846 return isl_bool_false
;
2849 /* Return true if the definition of the given div (recursively) involves
2850 * any of the given variables.
2852 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2853 unsigned first
, unsigned n
)
2857 isl_size n_div
, v_div
;
2859 involves
= isl_basic_map_div_expr_involves_vars(bmap
, div
, first
, n
);
2860 if (involves
< 0 || involves
)
2863 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2864 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2865 if (n_div
< 0 || v_div
< 0)
2866 return isl_bool_error
;
2867 for (i
= n_div
- 1; i
>= 0; --i
) {
2870 if (isl_int_is_zero(bmap
->div
[div
][1 + 1 + v_div
+ i
]))
2872 involves
= div_involves_vars(bmap
, i
, first
, n
);
2873 if (involves
< 0 || involves
)
2877 return isl_bool_false
;
2880 /* Does the definition of any integer division involve
2881 * any of the given variables?
2883 isl_bool
isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map
*bmap
,
2884 unsigned first
, unsigned n
)
2889 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2891 return isl_bool_error
;
2893 for (i
= 0; i
< n_div
; ++i
) {
2896 has
= isl_basic_map_div_expr_involves_vars(bmap
, i
, first
, n
);
2901 return isl_bool_false
;
2904 /* Try and add a lower and/or upper bound on "div" to "bmap"
2905 * based on inequality "i".
2906 * "total" is the total number of variables (excluding the divs).
2907 * "v" is a temporary object that can be used during the calculations.
2908 * If "lb" is set, then a lower bound should be constructed.
2909 * If "ub" is set, then an upper bound should be constructed.
2911 * The calling function has already checked that the inequality does not
2912 * reference "div", but we still need to check that the inequality is
2913 * of the right form. We'll consider the case where we want to construct
2914 * a lower bound. The construction of upper bounds is similar.
2916 * Let "div" be of the form
2918 * q = floor((a + f(x))/d)
2920 * We essentially check if constraint "i" is of the form
2924 * so that we can use it to derive a lower bound on "div".
2925 * However, we allow a slightly more general form
2929 * with the condition that the coefficients of g(x) - f(x) are all
2931 * Rewriting this constraint as
2935 * adding a + f(x) to both sides and dividing by d, we obtain
2937 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2939 * Taking the floor on both sides, we obtain
2941 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2945 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2947 * In the case of an upper bound, we construct the constraint
2949 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2952 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2953 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2954 unsigned total
, isl_int v
, int lb
, int ub
)
2958 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2960 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2961 bmap
->div
[div
][1 + 1 + j
]);
2962 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2965 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2966 bmap
->div
[div
][1 + 1 + j
]);
2967 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2973 bmap
= isl_basic_map_cow(bmap
);
2974 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2976 int k
= isl_basic_map_alloc_inequality(bmap
);
2979 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2980 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2981 bmap
->div
[div
][1 + j
]);
2982 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2983 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2985 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2988 int k
= isl_basic_map_alloc_inequality(bmap
);
2991 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2992 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2993 bmap
->div
[div
][1 + j
]);
2994 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2995 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2997 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
3002 isl_basic_map_free(bmap
);
3006 /* This function is called right before "div" is eliminated from "bmap"
3007 * using Fourier-Motzkin.
3008 * Look through the constraints of "bmap" for constraints on the argument
3009 * of the integer division and use them to construct constraints on the
3010 * integer division itself. These constraints can then be combined
3011 * during the Fourier-Motzkin elimination.
3012 * Note that it is only useful to introduce lower bounds on "div"
3013 * if "bmap" already contains upper bounds on "div" as the newly
3014 * introduce lower bounds can then be combined with the pre-existing
3015 * upper bounds. Similarly for upper bounds.
3016 * We therefore first check if "bmap" contains any lower and/or upper bounds
3019 * It is interesting to note that the introduction of these constraints
3020 * can indeed lead to more accurate results, even when compared to
3021 * deriving constraints on the argument of "div" from constraints on "div".
3022 * Consider, for example, the set
3024 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
3026 * The second constraint can be rewritten as
3028 * 2 * [(-i-2j+3)/4] + k >= 0
3030 * from which we can derive
3032 * -i - 2j + 3 >= -2k
3038 * Combined with the first constraint, we obtain
3040 * -3 <= 3 + 2k or k >= -3
3042 * If, on the other hand we derive a constraint on [(i+2j)/4] from
3043 * the first constraint, we obtain
3045 * [(i + 2j)/4] >= [-3/4] = -1
3047 * Combining this constraint with the second constraint, we obtain
3051 static __isl_give isl_basic_map
*insert_bounds_on_div(
3052 __isl_take isl_basic_map
*bmap
, int div
)
3055 int check_lb
, check_ub
;
3062 if (isl_int_is_zero(bmap
->div
[div
][0]))
3065 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
3067 return isl_basic_map_free(bmap
);
3071 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
3072 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
3079 if (!check_lb
&& !check_ub
)
3084 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
3085 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
3088 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
3089 check_lb
, check_ub
);
3097 /* Remove all divs (recursively) involving any of the given variables
3098 * in their definitions.
3100 static __isl_give isl_basic_map
*remove_divs_involving_vars(
3101 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3105 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3108 involves
= div_involves_vars(bmap
, i
, first
, n
);
3110 return isl_basic_map_free(bmap
);
3113 bmap
= insert_bounds_on_div(bmap
, i
);
3114 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3123 /* Data structure for communicating data between detect_mod and
3124 * substitute_div_mod.
3126 * "pos" is the position of the variable that is being examined.
3128 * "lower_f" is the index of the constraint
3129 * -f(x) + m i + t m n h(alpha) >= 0
3130 * "lower_g" is the index of the constraint
3132 * "m" and "n" correspond to the values in the first constraint.
3133 * "sum" is a temporary variable that is used internally inside detect_mod.
3135 struct isl_detect_mod_data
{
3147 /* Initialize "data".
3149 static void isl_detect_mod_data_init(struct isl_detect_mod_data
*data
)
3151 isl_int_init(data
->m
);
3152 isl_int_init(data
->n
);
3153 isl_int_init(data
->sum
);
3156 /* Free any memory allocated by "data".
3158 static void isl_detect_mod_data_clear(struct isl_detect_mod_data
*data
)
3160 isl_int_clear(data
->sum
);
3161 isl_int_clear(data
->n
);
3162 isl_int_clear(data
->m
);
3165 /* Is the variable at position data->pos
3166 * equal to a specific case of a nested modulo?
3168 * In particular, look for two pairs of constraints
3170 * -f(x) + m i + t m n h(alpha) >= 0
3171 * f(x) - m i - t m n h(alpha) + c >= 0
3178 * where f(x) and g(x) are expressions in the other variables,
3179 * excluding local variables,
3180 * h(alpha) is a non-zero expression in the local variable,
3185 * If these pairs of constraints are found,
3186 * then store the constraint index of the first f-constraint in data->lower_f,
3187 * the index of the first g-constraint in data->lower_g and
3188 * the values m and n in data->m and data->n.
3190 * The constraints are assumed to have been sorted,
3191 * which means that the constraints in a pair are close to each other.
3192 * The sorting also means that the f-constraints appear
3193 * after the g-constraints.
3195 static isl_bool
detect_mod(__isl_keep isl_basic_map
*bmap
,
3196 struct isl_detect_mod_data
*data
)
3201 isl_size v_div
, n_div
, total
;
3203 n_ineq
= isl_basic_map_n_inequality(bmap
);
3204 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
3205 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3206 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3207 if (n_ineq
< 0 || v_div
< 0 || n_div
< 0 || total
< 0)
3208 return isl_bool_error
;
3210 return isl_bool_false
;
3212 for (i
= n_ineq
- 1; i
>= 1; --i
) {
3213 isl_seq_gcd(bmap
->ineq
[i
] + 1 + v_div
, n_div
, &data
->n
);
3214 if (isl_int_is_zero(data
->n
))
3215 return isl_bool_false
;
3216 isl_int_abs(data
->m
, bmap
->ineq
[i
][1 + data
->pos
]);
3217 j
= find_earlier_constraint_in_pair(bmap
, i
, data
->pos
, total
,
3218 data
->m
, &data
->sum
);
3220 return isl_bool_error
;
3223 if (!isl_int_is_divisible_by(data
->n
, data
->m
))
3225 if (isl_int_is_pos(bmap
->ineq
[i
][1 + data
->pos
]))
3229 isl_int_divexact(data
->n
, data
->n
, data
->m
);
3230 isl_int_abs(data
->n
, data
->n
);
3234 return isl_bool_false
;
3235 for (i
= j
- 1; i
>= 1; --i
) {
3238 f
= isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + v_div
, n_div
);
3241 if (!isl_int_is_one(bmap
->ineq
[i
][1 + data
->pos
]) &&
3242 !isl_int_is_negone(bmap
->ineq
[i
][1 + data
->pos
]))
3244 j
= find_earlier_constraint_in_pair(bmap
, i
, data
->pos
, v_div
,
3245 data
->m
, &data
->sum
);
3247 return isl_bool_error
;
3250 if (isl_int_is_pos(bmap
->ineq
[i
][1 + data
->pos
]))
3257 return isl_bool_false
;
3258 return isl_bool_true
;
3261 /* Given that inequality "ineq" of "bset" expresses a lower bound
3262 * on the variable at position "pos" in terms of the other variables,
3263 * extract this lower bound as a function of those other variables,
3264 * excluding any local variables.
3266 static __isl_give isl_aff
*extract_lower_bound_aff(
3267 __isl_keep isl_basic_set
*bset
, int ineq
, int pos
)
3273 isl_local_space
*ls
;
3275 space
= isl_basic_set_peek_space(bset
);
3276 dim
= isl_space_dim(space
, isl_dim_all
);
3279 ls
= isl_local_space_from_space(isl_space_copy(space
));
3280 ctx
= isl_basic_set_get_ctx(bset
);
3281 v
= extract_bound_from_constraint(ctx
, bset
->ineq
[ineq
], dim
, pos
);
3282 return isl_aff_alloc_vec(ls
, v
);
3285 /* Given that there are two pairs of constraints
3287 * -f(x) + m i + t m n h(alpha) >= 0
3288 * f(x) - m i - t m n h(alpha) + c >= 0
3295 * where f(x) and g(x) are expressions in the other variables,
3296 * excluding local variables,
3297 * h(alpha) is a non-zero expression in the local variable,
3301 * derive an expression for i and plug that into "bmap".
3303 * In particular, the first pair of constraints implies
3305 * 0 <= (-f(x) + m i) % (m n) <= c
3306 * 0 <= (m floor(-f(x)/m) + (-f(x)) mod m + m i) % (m n) <= c
3307 * 0 <= (m floor(-f(x)/m) + m i) % (m n) + (-f(x)) mod m <= c
3308 * 0 <= m ((floor(-f(x)/m) + i) % n) + (-f(x)) mod m <= c
3312 * (floor(-f(x)/m) + i) % n = 0
3314 * because c < m, which can be rewritten to
3316 * (floor(-f(x)/m) + g(x) - g(x) + i) % n = 0
3320 * (-g(x) + i) % n = (-floor(-f(x)/m) - g(x)) % n
3322 * Since 0 <= -g(x) + i <= d < n,
3324 * -g(x) + i = (-floor(-f(x)/m) - g(x)) % n
3328 * i = (-floor(-f(x)/m) - g(x)) % n + g(x)
3330 * Note that the lower bounds on i derived from the original constraints are
3335 * (ignoring local variables).
3337 * The expression for i is plugged into "bmap", which results
3338 * in a basic map equivalent to "bmap" given i is equal to the expression,
3339 * so an equality constraint is added to ensure the meaning is preserved.
3341 static __isl_give isl_basic_map
*substitute_div_mod(
3342 __isl_take isl_basic_map
*bmap
, unsigned pos
,
3343 struct isl_detect_mod_data
*data
)
3346 isl_basic_set
*bset
, *eq
;
3348 isl_aff
*g
, *f
, *aff
, *it
;
3353 v_in
= isl_basic_map_var_offset(bmap
, isl_dim_in
);
3355 return isl_basic_map_free(bmap
);
3357 ctx
= isl_basic_map_get_ctx(bmap
);
3358 bset
= isl_basic_map_wrap(bmap
);
3359 space
= isl_basic_set_get_space(bset
);
3360 g
= extract_lower_bound_aff(bset
, data
->lower_g
, pos
);
3361 f
= extract_lower_bound_aff(bset
, data
->lower_f
, pos
);
3362 ma
= isl_space_identity_multi_aff_on_domain(space
);
3363 aff
= isl_aff_neg(isl_aff_floor(isl_aff_neg(f
)));
3364 aff
= isl_aff_sub(aff
, isl_aff_copy(g
));
3365 v
= isl_val_int_from_isl_int(ctx
, data
->n
);
3366 aff
= isl_aff_mod_val(aff
, v
);
3367 aff
= isl_aff_add(aff
, g
);
3368 it
= isl_multi_aff_get_at(ma
, pos
- v_in
);
3369 eq
= isl_aff_eq_basic_set(it
, isl_aff_copy(aff
));
3370 ma
= isl_multi_aff_set_at(ma
, pos
- v_in
, aff
);
3371 bset
= isl_basic_set_preimage_multi_aff(bset
, ma
);
3372 bset
= isl_basic_set_intersect(bset
, eq
);
3373 return isl_basic_set_unwrap(bset
);
3376 /* Remove all divs (recursively) involving any of the given dimensions
3377 * in their definitions.
3379 * If it is a single input or output dimension that
3380 * should not appear in any integer division expression and
3381 * if there is indeed an integer division expression
3382 * involving that dimension, then check if there are any constraints
3383 * that imply that the given dimension is equal to some expression
3384 * in the other dimensions.
3385 * If so, plug in that expression for the given dimension
3386 * so that the integer division expressions no longer
3387 * involve that dimension.
3389 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
3390 __isl_take isl_basic_map
*bmap
,
3391 enum isl_dim_type type
, unsigned first
, unsigned n
)
3396 struct isl_detect_mod_data data
;
3398 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3399 return isl_basic_map_free(bmap
);
3400 off
= isl_basic_map_var_offset(bmap
, type
);
3402 return isl_basic_map_free(bmap
);
3405 if (type
== isl_dim_param
|| type
== isl_dim_div
|| n
!= 1)
3406 return remove_divs_involving_vars(bmap
, first
, n
);
3407 used
= isl_basic_map_any_div_involves_vars(bmap
, first
, n
);
3409 return isl_basic_map_free(bmap
);
3412 bmap
= isl_basic_map_sort_constraints(bmap
);
3413 isl_detect_mod_data_init(&data
);
3415 found
= detect_mod(bmap
, &data
);
3417 bmap
= remove_divs_involving_vars(bmap
, first
, n
);
3419 bmap
= substitute_div_mod(bmap
, first
, &data
);
3420 isl_detect_mod_data_clear(&data
);
3424 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
3425 __isl_take isl_basic_set
*bset
,
3426 enum isl_dim_type type
, unsigned first
, unsigned n
)
3428 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
3431 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
3432 enum isl_dim_type type
, unsigned first
, unsigned n
)
3441 map
= isl_map_cow(map
);
3445 for (i
= 0; i
< map
->n
; ++i
) {
3446 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
3457 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
3458 enum isl_dim_type type
, unsigned first
, unsigned n
)
3460 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
3464 /* Does the description of "bmap" depend on the specified dimensions?
3465 * We also check whether the dimensions appear in any of the div definitions.
3466 * In principle there is no need for this check. If the dimensions appear
3467 * in a div definition, they also appear in the defining constraints of that
3470 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
3471 enum isl_dim_type type
, unsigned first
, unsigned n
)
3475 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3476 return isl_bool_error
;
3478 first
+= isl_basic_map_offset(bmap
, type
);
3479 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3480 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
3481 return isl_bool_true
;
3482 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3483 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
3484 return isl_bool_true
;
3485 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3486 if (isl_int_is_zero(bmap
->div
[i
][0]))
3488 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
3489 return isl_bool_true
;
3492 return isl_bool_false
;
3495 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
3496 enum isl_dim_type type
, unsigned first
, unsigned n
)
3500 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3501 return isl_bool_error
;
3503 for (i
= 0; i
< map
->n
; ++i
) {
3504 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
3506 if (involves
< 0 || involves
)
3510 return isl_bool_false
;
3513 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
3514 enum isl_dim_type type
, unsigned first
, unsigned n
)
3516 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
3519 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
3520 enum isl_dim_type type
, unsigned first
, unsigned n
)
3522 return isl_map_involves_dims(set
, type
, first
, n
);
3525 /* Does "bset" involve any local variables, i.e., integer divisions?
3527 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3531 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3533 return isl_bool_error
;
3534 return isl_bool_ok(n
> 0);
3537 /* isl_set_every_basic_set callback that checks whether "bset"
3538 * is free of local variables.
3540 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3542 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3545 /* Does "set" involve any local variables, i.e., integer divisions?
3547 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3551 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3552 return isl_bool_not(no_locals
);
3555 /* Drop all constraints in bmap that involve any of the dimensions
3556 * first to first+n-1.
3557 * This function only performs the actual removal of constraints.
3559 * This function should not call finalize since it is used by
3560 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3562 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3563 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3570 bmap
= isl_basic_map_cow(bmap
);
3575 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3576 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3578 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3579 return isl_basic_map_free(bmap
);
3582 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3583 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3585 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3586 return isl_basic_map_free(bmap
);
3592 /* Drop all constraints in bset that involve any of the dimensions
3593 * first to first+n-1.
3594 * This function only performs the actual removal of constraints.
3596 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3597 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3599 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3602 /* Drop all constraints in bmap that do not involve any of the dimensions
3603 * first to first + n - 1 of the given type.
3605 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3606 __isl_take isl_basic_map
*bmap
,
3607 enum isl_dim_type type
, unsigned first
, unsigned n
)
3612 isl_space
*space
= isl_basic_map_get_space(bmap
);
3613 isl_basic_map_free(bmap
);
3614 return isl_basic_map_universe(space
);
3616 bmap
= isl_basic_map_cow(bmap
);
3620 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3621 return isl_basic_map_free(bmap
);
3623 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3625 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3626 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3628 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3629 return isl_basic_map_free(bmap
);
3632 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3633 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3635 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3636 return isl_basic_map_free(bmap
);
3639 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3643 /* Drop all constraints in bset that do not involve any of the dimensions
3644 * first to first + n - 1 of the given type.
3646 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3647 __isl_take isl_basic_set
*bset
,
3648 enum isl_dim_type type
, unsigned first
, unsigned n
)
3650 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3654 /* Drop all constraints in bmap that involve any of the dimensions
3655 * first to first + n - 1 of the given type.
3657 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3658 __isl_take isl_basic_map
*bmap
,
3659 enum isl_dim_type type
, unsigned first
, unsigned n
)
3666 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3667 return isl_basic_map_free(bmap
);
3669 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3670 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3671 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3672 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3676 /* Drop all constraints in bset that involve any of the dimensions
3677 * first to first + n - 1 of the given type.
3679 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3680 __isl_take isl_basic_set
*bset
,
3681 enum isl_dim_type type
, unsigned first
, unsigned n
)
3683 return isl_basic_map_drop_constraints_involving_dims(bset
,
3687 /* Drop constraints from "map" by applying "drop" to each basic map.
3689 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3690 enum isl_dim_type type
, unsigned first
, unsigned n
,
3691 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3692 enum isl_dim_type type
, unsigned first
, unsigned n
))
3696 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3697 return isl_map_free(map
);
3699 map
= isl_map_cow(map
);
3703 for (i
= 0; i
< map
->n
; ++i
) {
3704 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3706 return isl_map_free(map
);
3710 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3715 /* Drop all constraints in map that involve any of the dimensions
3716 * first to first + n - 1 of the given type.
3718 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3719 __isl_take isl_map
*map
,
3720 enum isl_dim_type type
, unsigned first
, unsigned n
)
3724 return drop_constraints(map
, type
, first
, n
,
3725 &isl_basic_map_drop_constraints_involving_dims
);
3728 /* Drop all constraints in "map" that do not involve any of the dimensions
3729 * first to first + n - 1 of the given type.
3731 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3732 __isl_take isl_map
*map
,
3733 enum isl_dim_type type
, unsigned first
, unsigned n
)
3736 isl_space
*space
= isl_map_get_space(map
);
3738 return isl_map_universe(space
);
3740 return drop_constraints(map
, type
, first
, n
,
3741 &isl_basic_map_drop_constraints_not_involving_dims
);
3744 /* Drop all constraints in set that involve any of the dimensions
3745 * first to first + n - 1 of the given type.
3747 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3748 __isl_take isl_set
*set
,
3749 enum isl_dim_type type
, unsigned first
, unsigned n
)
3751 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3754 /* Drop all constraints in "set" that do not involve any of the dimensions
3755 * first to first + n - 1 of the given type.
3757 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3758 __isl_take isl_set
*set
,
3759 enum isl_dim_type type
, unsigned first
, unsigned n
)
3761 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3764 /* Does local variable "div" of "bmap" have a complete explicit representation?
3765 * Having a complete explicit representation requires not only
3766 * an explicit representation, but also that all local variables
3767 * that appear in this explicit representation in turn have
3768 * a complete explicit representation.
3770 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3773 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3776 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3777 if (marked
< 0 || marked
)
3778 return isl_bool_not(marked
);
3780 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3783 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3785 known
= isl_basic_map_div_is_known(bmap
, i
);
3786 if (known
< 0 || !known
)
3790 return isl_bool_true
;
3793 /* Remove all divs that are unknown or defined in terms of unknown divs.
3795 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3796 __isl_take isl_basic_map
*bmap
)
3803 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3804 if (isl_basic_map_div_is_known(bmap
, i
))
3806 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3815 /* Remove all divs that are unknown or defined in terms of unknown divs.
3817 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3818 __isl_take isl_basic_set
*bset
)
3820 return isl_basic_map_remove_unknown_divs(bset
);
3823 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3832 map
= isl_map_cow(map
);
3836 for (i
= 0; i
< map
->n
; ++i
) {
3837 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3847 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3849 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3852 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3853 __isl_take isl_basic_set
*bset
,
3854 enum isl_dim_type type
, unsigned first
, unsigned n
)
3856 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3857 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3858 return bset_from_bmap(bmap
);
3861 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3862 enum isl_dim_type type
, unsigned first
, unsigned n
)
3869 map
= isl_map_cow(map
);
3870 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3871 return isl_map_free(map
);
3873 for (i
= 0; i
< map
->n
; ++i
) {
3874 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3875 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3879 map
= isl_map_drop(map
, type
, first
, n
);
3886 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3887 enum isl_dim_type type
, unsigned first
, unsigned n
)
3889 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3893 /* Project out n inputs starting at first using Fourier-Motzkin */
3894 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3895 unsigned first
, unsigned n
)
3897 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3900 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3901 FILE *out
, int indent
)
3906 fprintf(out
, "null basic set\n");
3910 fprintf(out
, "%*s", indent
, "");
3911 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3912 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3913 bset
->extra
, bset
->flags
);
3915 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3916 p
= isl_printer_set_dump(p
, 1);
3917 p
= isl_printer_set_indent(p
, indent
);
3918 p
= isl_printer_start_line(p
);
3919 p
= isl_printer_print_basic_set(p
, bset
);
3920 p
= isl_printer_end_line(p
);
3921 isl_printer_free(p
);
3924 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3925 FILE *out
, int indent
)
3930 fprintf(out
, "null basic map\n");
3934 fprintf(out
, "%*s", indent
, "");
3935 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3936 "flags: %x, n_name: %d\n",
3938 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3939 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3941 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3942 p
= isl_printer_set_dump(p
, 1);
3943 p
= isl_printer_set_indent(p
, indent
);
3944 p
= isl_printer_start_line(p
);
3945 p
= isl_printer_print_basic_map(p
, bmap
);
3946 p
= isl_printer_end_line(p
);
3947 isl_printer_free(p
);
3950 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3955 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3957 return isl_basic_map_free(bmap
);
3958 if (pos
>= bmap
->n_ineq
)
3959 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3960 "invalid position", return isl_basic_map_free(bmap
));
3961 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3962 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3963 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3964 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3968 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3971 if (isl_space_check_is_set(space
) < 0)
3973 return isl_map_alloc_space(space
, n
, flags
);
3975 isl_space_free(space
);
3979 /* Make sure "map" has room for at least "n" more basic maps.
3981 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3984 struct isl_map
*grown
= NULL
;
3988 isl_assert(map
->ctx
, n
>= 0, goto error
);
3989 if (map
->n
+ n
<= map
->size
)
3991 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3994 for (i
= 0; i
< map
->n
; ++i
) {
3995 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
4003 isl_map_free(grown
);
4008 /* Make sure "set" has room for at least "n" more basic sets.
4010 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
4012 return set_from_map(isl_map_grow(set_to_map(set
), n
));
4015 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
4017 return isl_map_from_basic_map(bset
);
4020 /* This function performs the same operation as isl_set_from_basic_set,
4021 * but is considered as a function on an isl_basic_set when exported.
4023 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
4025 return isl_set_from_basic_set(bset
);
4028 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
4030 struct isl_map
*map
;
4035 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
4036 return isl_map_add_basic_map(map
, bmap
);
4039 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
4040 __isl_take isl_basic_set
*bset
)
4042 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
4043 bset_to_bmap(bset
)));
4046 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
4048 return isl_map_free(set
);
4051 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
4056 fprintf(out
, "null set\n");
4060 fprintf(out
, "%*s", indent
, "");
4061 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
4062 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
4064 for (i
= 0; i
< set
->n
; ++i
) {
4065 fprintf(out
, "%*s", indent
, "");
4066 fprintf(out
, "basic set %d:\n", i
);
4067 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
4071 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
4076 fprintf(out
, "null map\n");
4080 fprintf(out
, "%*s", indent
, "");
4081 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
4082 "flags: %x, n_name: %d\n",
4083 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
4084 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
4085 for (i
= 0; i
< map
->n
; ++i
) {
4086 fprintf(out
, "%*s", indent
, "");
4087 fprintf(out
, "basic map %d:\n", i
);
4088 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
4092 /* Check that the space of "bset" is the same as that of the domain of "bmap".
4094 static isl_stat
isl_basic_map_check_compatible_domain(
4095 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
4099 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
4101 return isl_stat_error
;
4103 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
4104 "incompatible spaces", return isl_stat_error
);
4109 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
4110 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4112 struct isl_basic_map
*bmap_domain
;
4115 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
4118 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4122 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4125 bmap
= isl_basic_map_cow(bmap
);
4128 bmap
= isl_basic_map_extend(bmap
,
4129 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
4130 bmap_domain
= isl_basic_map_from_domain(bset
);
4131 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
4133 bmap
= isl_basic_map_simplify(bmap
);
4134 return isl_basic_map_finalize(bmap
);
4136 isl_basic_map_free(bmap
);
4137 isl_basic_set_free(bset
);
4141 /* Check that the space of "bset" is the same as that of the range of "bmap".
4143 static isl_stat
isl_basic_map_check_compatible_range(
4144 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
4148 ok
= isl_basic_map_compatible_range(bmap
, bset
);
4150 return isl_stat_error
;
4152 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
4153 "incompatible spaces", return isl_stat_error
);
4158 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
4159 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4161 struct isl_basic_map
*bmap_range
;
4164 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
4167 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4170 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
4173 if (isl_basic_set_plain_is_universe(bset
)) {
4174 isl_basic_set_free(bset
);
4178 bmap
= isl_basic_map_cow(bmap
);
4181 bmap
= isl_basic_map_extend(bmap
,
4182 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
4183 bmap_range
= bset_to_bmap(bset
);
4184 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
4186 bmap
= isl_basic_map_simplify(bmap
);
4187 return isl_basic_map_finalize(bmap
);
4189 isl_basic_map_free(bmap
);
4190 isl_basic_set_free(bset
);
4194 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
4195 __isl_keep isl_vec
*vec
)
4201 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4202 if (total
< 0 || !vec
)
4203 return isl_bool_error
;
4205 if (1 + total
!= vec
->size
)
4206 return isl_bool_false
;
4210 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4211 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
4212 if (!isl_int_is_zero(s
)) {
4214 return isl_bool_false
;
4218 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4219 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
4220 if (isl_int_is_neg(s
)) {
4222 return isl_bool_false
;
4228 return isl_bool_true
;
4231 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
4232 __isl_keep isl_vec
*vec
)
4234 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
4237 __isl_give isl_basic_map
*isl_basic_map_intersect(
4238 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4240 struct isl_vec
*sample
= NULL
;
4241 isl_space
*space1
, *space2
;
4242 isl_size dim1
, dim2
, nparam1
, nparam2
;
4244 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4246 space1
= isl_basic_map_peek_space(bmap1
);
4247 space2
= isl_basic_map_peek_space(bmap2
);
4248 dim1
= isl_space_dim(space1
, isl_dim_all
);
4249 dim2
= isl_space_dim(space2
, isl_dim_all
);
4250 nparam1
= isl_space_dim(space1
, isl_dim_param
);
4251 nparam2
= isl_space_dim(space2
, isl_dim_param
);
4252 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
4254 if (dim1
== nparam1
&& dim2
!= nparam2
)
4255 return isl_basic_map_intersect(bmap2
, bmap1
);
4257 if (dim2
!= nparam2
&&
4258 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4261 if (isl_basic_map_plain_is_empty(bmap1
)) {
4262 isl_basic_map_free(bmap2
);
4265 if (isl_basic_map_plain_is_empty(bmap2
)) {
4266 isl_basic_map_free(bmap1
);
4270 if (bmap1
->sample
&&
4271 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
4272 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
4273 sample
= isl_vec_copy(bmap1
->sample
);
4274 else if (bmap2
->sample
&&
4275 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
4276 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
4277 sample
= isl_vec_copy(bmap2
->sample
);
4279 bmap1
= isl_basic_map_cow(bmap1
);
4282 bmap1
= isl_basic_map_extend(bmap1
,
4283 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4284 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
4287 isl_vec_free(sample
);
4289 isl_vec_free(bmap1
->sample
);
4290 bmap1
->sample
= sample
;
4293 bmap1
= isl_basic_map_simplify(bmap1
);
4294 return isl_basic_map_finalize(bmap1
);
4297 isl_vec_free(sample
);
4298 isl_basic_map_free(bmap1
);
4299 isl_basic_map_free(bmap2
);
4303 __isl_give isl_basic_set
*isl_basic_set_intersect(
4304 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
4306 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
4307 bset_to_bmap(bset2
)));
4310 /* Intersect the parameter domain of "bmap" with "bset".
4312 * isl_basic_map_intersect handles this as a special case.
4314 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
4315 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4317 return isl_basic_map_intersect(bmap
, bset
);
4320 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
4321 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
4323 isl_basic_map
*bmap
;
4325 bmap
= bset_to_bmap(bset1
);
4326 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
4327 return bset_from_bmap(bmap
);
4330 /* Does "map" consist of a single disjunct, without any local variables?
4332 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
4337 return isl_bool_error
;
4339 return isl_bool_false
;
4340 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
4342 return isl_bool_error
;
4344 return isl_bool_false
;
4345 return isl_bool_true
;
4348 /* Check that "map" consists of a single disjunct, without any local variables.
4350 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
4354 ok
= is_convex_no_locals(map
);
4356 return isl_stat_error
;
4360 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
4361 "unexpectedly not convex or involving local variables",
4362 return isl_stat_error
);
4365 /* Special case of isl_map_intersect, where both map1 and map2
4366 * are convex, without any divs and such that either map1 or map2
4367 * contains a single constraint. This constraint is then simply
4368 * added to the other map.
4370 static __isl_give isl_map
*map_intersect_add_constraint(
4371 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
4373 if (check_convex_no_locals(map1
) < 0 ||
4374 check_convex_no_locals(map2
) < 0)
4377 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
4378 return isl_map_intersect(map2
, map1
);
4380 map1
= isl_map_cow(map1
);
4383 if (isl_map_plain_is_empty(map1
)) {
4387 if (map2
->p
[0]->n_eq
== 1)
4388 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
4390 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
4391 map2
->p
[0]->ineq
[0]);
4393 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
4394 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
4398 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
4399 isl_basic_map_free(map1
->p
[0]);
4405 map1
= isl_map_unmark_normalized(map1
);
4413 /* map2 may be either a parameter domain or a map living in the same
4416 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
4417 __isl_take isl_map
*map2
)
4423 isl_size dim2
, nparam2
;
4428 if ((isl_map_plain_is_empty(map1
) ||
4429 isl_map_plain_is_universe(map2
)) &&
4430 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4434 if ((isl_map_plain_is_empty(map2
) ||
4435 isl_map_plain_is_universe(map1
)) &&
4436 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4441 if (is_convex_no_locals(map1
) == isl_bool_true
&&
4442 is_convex_no_locals(map2
) == isl_bool_true
&&
4443 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
4444 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
4445 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
4446 return map_intersect_add_constraint(map1
, map2
);
4448 equal
= isl_map_plain_is_equal(map1
, map2
);
4456 dim2
= isl_map_dim(map2
, isl_dim_all
);
4457 nparam2
= isl_map_dim(map2
, isl_dim_param
);
4458 if (dim2
< 0 || nparam2
< 0)
4460 if (dim2
!= nparam2
)
4461 isl_assert(map1
->ctx
,
4462 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4464 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4465 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4466 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4468 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4469 map1
->n
* map2
->n
, flags
);
4472 for (i
= 0; i
< map1
->n
; ++i
)
4473 for (j
= 0; j
< map2
->n
; ++j
) {
4474 struct isl_basic_map
*part
;
4475 part
= isl_basic_map_intersect(
4476 isl_basic_map_copy(map1
->p
[i
]),
4477 isl_basic_map_copy(map2
->p
[j
]));
4478 if (isl_basic_map_is_empty(part
) < 0)
4479 part
= isl_basic_map_free(part
);
4480 result
= isl_map_add_basic_map(result
, part
);
4493 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
4494 __isl_take isl_map
*map2
)
4496 if (isl_map_check_equal_space(map1
, map2
) < 0)
4498 return map_intersect_internal(map1
, map2
);
4505 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
4506 __isl_take isl_map
*map2
)
4508 isl_map_align_params_bin(&map1
, &map2
);
4509 return map_intersect(map1
, map2
);
4512 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
4513 __isl_take isl_set
*set2
)
4515 return set_from_map(isl_map_intersect(set_to_map(set1
),
4519 /* map_intersect_internal accepts intersections
4520 * with parameter domains, so we can just call that function.
4522 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
4523 __isl_take isl_set
*params
)
4525 isl_map_align_params_set(&map
, ¶ms
);
4526 return map_intersect_internal(map
, params
);
4529 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4530 __isl_take isl_set
*params
)
4532 return isl_map_intersect_params(set
, params
);
4535 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4543 bmap
= isl_basic_map_cow(bmap
);
4546 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4547 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4548 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4549 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4550 if (n1
< 0 || n2
< 0)
4551 bmap
= isl_basic_map_free(bmap
);
4552 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4553 return isl_basic_map_reset_space(bmap
, space
);
4556 /* Given a basic map where the tuple of type "type" is a wrapped map,
4557 * swap domain and range of that wrapped map.
4559 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4560 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4563 isl_size offset
, n1
, n2
;
4565 space
= isl_basic_map_peek_space(bmap
);
4566 offset
= isl_basic_map_var_offset(bmap
, type
);
4567 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4568 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4569 if (offset
< 0 || n1
< 0 || n2
< 0)
4570 return isl_basic_map_free(bmap
);
4572 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4574 space
= isl_basic_map_take_space(bmap
);
4575 space
= isl_space_reverse_wrapped(space
, type
);
4576 bmap
= isl_basic_map_restore_space(bmap
, space
);
4581 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4584 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4585 __isl_take isl_basic_map
*bmap
)
4589 space
= isl_basic_map_peek_space(bmap
);
4590 if (isl_space_check_domain_is_wrapping(space
) < 0)
4591 return isl_basic_map_free(bmap
);
4592 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4597 /* Given a basic map A -> (B -> C), return the corresponding basic map
4600 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4601 __isl_take isl_basic_map
*bmap
)
4605 space
= isl_basic_map_peek_space(bmap
);
4606 if (isl_space_check_range_is_wrapping(space
) < 0)
4607 return isl_basic_map_free(bmap
);
4608 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4613 /* Given a basic map that is actually a basic set (A -> B),
4614 * return the corresponding basic set (B -> A) as a basic map.
4616 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4617 __isl_take isl_basic_map
*bmap
)
4621 space
= isl_basic_map_peek_space(bmap
);
4622 if (isl_space_check_is_wrapping(space
) < 0)
4623 return isl_basic_map_free(bmap
);
4624 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4629 static __isl_give isl_basic_map
*basic_map_space_reset(
4630 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4636 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4639 space
= isl_basic_map_get_space(bmap
);
4640 space
= isl_space_reset(space
, type
);
4641 bmap
= isl_basic_map_reset_space(bmap
, space
);
4645 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4646 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4647 unsigned pos
, unsigned n
)
4649 isl_bool rational
, is_empty
;
4650 isl_space
*res_space
;
4651 struct isl_basic_map
*res
;
4652 struct isl_dim_map
*dim_map
;
4655 enum isl_dim_type t
;
4658 return basic_map_space_reset(bmap
, type
);
4660 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4661 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4662 if (is_empty
< 0 || total
< 0)
4663 return isl_basic_map_free(bmap
);
4664 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4667 return isl_basic_map_free(bmap
);
4669 isl_basic_map_free(bmap
);
4670 return isl_basic_map_empty(res_space
);
4673 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4675 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4679 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4681 isl_size size
= isl_basic_map_dim(bmap
, t
);
4683 dim_map
= isl_dim_map_free(dim_map
);
4684 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4686 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4687 pos
, size
- pos
, off
+ pos
+ n
);
4689 dim
= isl_space_dim(res_space
, t
);
4691 dim_map
= isl_dim_map_free(dim_map
);
4694 isl_dim_map_div(dim_map
, bmap
, off
);
4696 res
= isl_basic_map_alloc_space(res_space
,
4697 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4698 rational
= isl_basic_map_is_rational(bmap
);
4700 res
= isl_basic_map_free(res
);
4702 res
= isl_basic_map_set_rational(res
);
4703 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4704 return isl_basic_map_finalize(res
);
4707 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4708 __isl_take isl_basic_set
*bset
,
4709 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4711 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4714 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4715 enum isl_dim_type type
, unsigned n
)
4719 dim
= isl_basic_map_dim(bmap
, type
);
4721 return isl_basic_map_free(bmap
);
4722 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4725 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4726 enum isl_dim_type type
, unsigned n
)
4730 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4731 return isl_basic_map_add_dims(bset
, type
, n
);
4733 isl_basic_set_free(bset
);
4737 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4738 enum isl_dim_type type
)
4742 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4745 space
= isl_map_get_space(map
);
4746 space
= isl_space_reset(space
, type
);
4747 map
= isl_map_reset_space(map
, space
);
4751 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4752 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4758 return map_space_reset(map
, type
);
4760 map
= isl_map_cow(map
);
4764 for (i
= 0; i
< map
->n
; ++i
) {
4765 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4770 space
= isl_map_take_space(map
);
4771 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4772 map
= isl_map_restore_space(map
, space
);
4780 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4781 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4783 return isl_map_insert_dims(set
, type
, pos
, n
);
4786 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4787 enum isl_dim_type type
, unsigned n
)
4791 dim
= isl_map_dim(map
, type
);
4793 return isl_map_free(map
);
4794 return isl_map_insert_dims(map
, type
, dim
, n
);
4797 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4798 enum isl_dim_type type
, unsigned n
)
4802 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4803 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4809 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4810 __isl_take isl_basic_map
*bmap
,
4811 enum isl_dim_type dst_type
, unsigned dst_pos
,
4812 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4815 struct isl_dim_map
*dim_map
;
4816 struct isl_basic_map
*res
;
4817 enum isl_dim_type t
;
4824 bmap
= isl_basic_map_reset(bmap
, src_type
);
4825 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4829 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4830 return isl_basic_map_free(bmap
);
4832 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4835 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4837 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4838 pos(bmap
->dim
, src_type
) + src_pos
+
4839 ((src_type
< dst_type
) ? n
: 0)) {
4840 space
= isl_basic_map_take_space(bmap
);
4841 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4842 src_type
, src_pos
, n
);
4843 bmap
= isl_basic_map_restore_space(bmap
, space
);
4844 bmap
= isl_basic_map_finalize(bmap
);
4849 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4851 return isl_basic_map_free(bmap
);
4852 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4855 space
= isl_basic_map_peek_space(bmap
);
4856 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4857 isl_size size
= isl_space_dim(space
, t
);
4859 dim_map
= isl_dim_map_free(dim_map
);
4860 if (t
== dst_type
) {
4861 isl_dim_map_dim_range(dim_map
, space
, t
,
4864 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4867 isl_dim_map_dim_range(dim_map
, space
, t
,
4868 dst_pos
, size
- dst_pos
, off
);
4869 off
+= size
- dst_pos
;
4870 } else if (t
== src_type
) {
4871 isl_dim_map_dim_range(dim_map
, space
, t
,
4874 isl_dim_map_dim_range(dim_map
, space
, t
,
4875 src_pos
+ n
, size
- src_pos
- n
, off
);
4876 off
+= size
- src_pos
- n
;
4878 isl_dim_map_dim(dim_map
, space
, t
, off
);
4882 isl_dim_map_div(dim_map
, bmap
, off
);
4884 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4885 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4886 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4887 space
= isl_basic_map_take_space(bmap
);
4888 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4889 src_type
, src_pos
, n
);
4890 bmap
= isl_basic_map_restore_space(bmap
, space
);
4894 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4895 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4896 bmap
= isl_basic_map_finalize(bmap
);
4900 isl_basic_map_free(bmap
);
4904 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4905 enum isl_dim_type dst_type
, unsigned dst_pos
,
4906 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4908 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4909 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4910 src_type
, src_pos
, n
);
4911 return bset_from_bmap(bmap
);
4914 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4915 enum isl_dim_type dst_type
, unsigned dst_pos
,
4916 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4920 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4921 return set_from_map(isl_map_move_dims(set_to_map(set
),
4922 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4928 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4929 enum isl_dim_type dst_type
, unsigned dst_pos
,
4930 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4936 map
= isl_map_reset(map
, src_type
);
4937 map
= isl_map_reset(map
, dst_type
);
4941 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4942 return isl_map_free(map
);
4944 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4947 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4949 map
= isl_map_cow(map
);
4953 for (i
= 0; i
< map
->n
; ++i
) {
4954 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4956 src_type
, src_pos
, n
);
4961 space
= isl_map_take_space(map
);
4962 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4963 src_type
, src_pos
, n
);
4964 map
= isl_map_restore_space(map
, space
);
4972 /* Move the specified dimensions to the last columns right before
4973 * the divs. Don't change the dimension specification of bmap.
4974 * That's the responsibility of the caller.
4976 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4977 enum isl_dim_type type
, unsigned first
, unsigned n
)
4980 struct isl_dim_map
*dim_map
;
4981 struct isl_basic_map
*res
;
4982 enum isl_dim_type t
;
4988 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4989 isl_basic_map_offset(bmap
, isl_dim_div
))
4992 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4994 return isl_basic_map_free(bmap
);
4995 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4998 space
= isl_basic_map_peek_space(bmap
);
4999 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
5000 isl_size size
= isl_space_dim(space
, t
);
5002 dim_map
= isl_dim_map_free(dim_map
);
5004 isl_dim_map_dim_range(dim_map
, space
, t
,
5007 isl_dim_map_dim_range(dim_map
, space
, t
,
5008 first
, n
, total
- bmap
->n_div
- n
);
5009 isl_dim_map_dim_range(dim_map
, space
, t
,
5010 first
+ n
, size
- (first
+ n
), off
);
5011 off
+= size
- (first
+ n
);
5013 isl_dim_map_dim(dim_map
, space
, t
, off
);
5017 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
5019 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
5020 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
5021 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
5025 /* Insert "n" rows in the divs of "bmap".
5027 * The number of columns is not changed, which means that the last
5028 * dimensions of "bmap" are being reintepreted as the new divs.
5029 * The space of "bmap" is not adjusted, however, which means
5030 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
5031 * from the space of "bmap" is the responsibility of the caller.
5033 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
5041 bmap
= isl_basic_map_cow(bmap
);
5045 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
5046 old
= bmap
->block2
.data
;
5047 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
5048 (bmap
->extra
+ n
) * (1 + row_size
));
5049 if (!bmap
->block2
.data
)
5050 return isl_basic_map_free(bmap
);
5051 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
5053 return isl_basic_map_free(bmap
);
5054 for (i
= 0; i
< n
; ++i
) {
5055 new_div
[i
] = bmap
->block2
.data
+
5056 (bmap
->extra
+ i
) * (1 + row_size
);
5057 isl_seq_clr(new_div
[i
], 1 + row_size
);
5059 for (i
= 0; i
< bmap
->extra
; ++i
)
5060 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
5062 bmap
->div
= new_div
;
5069 /* Drop constraints from "bmap" that only involve the variables
5070 * of "type" in the range [first, first + n] that are not related
5071 * to any of the variables outside that interval.
5072 * These constraints cannot influence the values for the variables
5073 * outside the interval, except in case they cause "bmap" to be empty.
5074 * Only drop the constraints if "bmap" is known to be non-empty.
5076 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
5077 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
5078 unsigned first
, unsigned n
)
5082 isl_size dim
, n_div
;
5085 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
5087 return isl_basic_map_free(bmap
);
5091 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
5092 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5093 if (dim
< 0 || n_div
< 0)
5094 return isl_basic_map_free(bmap
);
5095 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
5097 return isl_basic_map_free(bmap
);
5098 first
+= isl_basic_map_offset(bmap
, type
) - 1;
5099 for (i
= 0; i
< first
; ++i
)
5101 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
5104 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
5109 /* Turn the n dimensions of type type, starting at first
5110 * into existentially quantified variables.
5112 * If a subset of the projected out variables are unrelated
5113 * to any of the variables that remain, then the constraints
5114 * involving this subset are simply dropped first.
5116 __isl_give isl_basic_map
*isl_basic_map_project_out(
5117 __isl_take isl_basic_map
*bmap
,
5118 enum isl_dim_type type
, unsigned first
, unsigned n
)
5124 return basic_map_space_reset(bmap
, type
);
5125 if (type
== isl_dim_div
)
5126 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5127 "cannot project out existentially quantified variables",
5128 return isl_basic_map_free(bmap
));
5130 empty
= isl_basic_map_plain_is_empty(bmap
);
5132 return isl_basic_map_free(bmap
);
5134 bmap
= isl_basic_map_set_to_empty(bmap
);
5136 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
5140 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
5141 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
5143 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
5144 return isl_basic_map_free(bmap
);
5146 bmap
= move_last(bmap
, type
, first
, n
);
5147 bmap
= isl_basic_map_cow(bmap
);
5148 bmap
= insert_div_rows(bmap
, n
);
5150 space
= isl_basic_map_take_space(bmap
);
5151 space
= isl_space_drop_dims(space
, type
, first
, n
);
5152 bmap
= isl_basic_map_restore_space(bmap
, space
);
5153 bmap
= isl_basic_map_simplify(bmap
);
5154 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5155 return isl_basic_map_finalize(bmap
);
5158 /* Turn the n dimensions of type type, starting at first
5159 * into existentially quantified variables.
5161 __isl_give isl_basic_set
*isl_basic_set_project_out(
5162 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
5163 unsigned first
, unsigned n
)
5165 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
5169 /* Turn the n dimensions of type type, starting at first
5170 * into existentially quantified variables.
5172 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
5173 enum isl_dim_type type
, unsigned first
, unsigned n
)
5179 return map_space_reset(map
, type
);
5181 if (isl_map_check_range(map
, type
, first
, n
) < 0)
5182 return isl_map_free(map
);
5184 map
= isl_map_cow(map
);
5188 for (i
= 0; i
< map
->n
; ++i
) {
5189 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
5195 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5196 map
= isl_map_unmark_normalized(map
);
5198 space
= isl_map_take_space(map
);
5199 space
= isl_space_drop_dims(space
, type
, first
, n
);
5200 map
= isl_map_restore_space(map
, space
);
5209 #define TYPE isl_map
5210 #include "isl_project_out_all_params_templ.c"
5211 #include "isl_project_out_param_templ.c"
5213 /* Turn all the dimensions of type "type", except the "n" starting at "first"
5214 * into existentially quantified variables.
5216 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
5217 enum isl_dim_type type
, unsigned first
, unsigned n
)
5221 dim
= isl_map_dim(map
, type
);
5222 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
5223 return isl_map_free(map
);
5224 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
5225 map
= isl_map_project_out(map
, type
, 0, first
);
5229 /* Turn the n dimensions of type type, starting at first
5230 * into existentially quantified variables.
5232 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
5233 enum isl_dim_type type
, unsigned first
, unsigned n
)
5235 return set_from_map(isl_map_project_out(set_to_map(set
),
5239 /* If "set" involves a parameter with identifier "id",
5240 * then turn it into an existentially quantified variable.
5242 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
5243 __isl_take isl_id
*id
)
5245 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
5248 /* If "set" involves any of the parameters with identifiers in "list",
5249 * then turn them into existentially quantified variables.
5251 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
5252 __isl_take isl_id_list
*list
)
5256 map
= set_to_map(set
);
5257 map
= isl_map_project_out_param_id_list(map
, list
);
5258 return set_from_map(map
);
5261 /* Project out all parameters from "set" by existentially quantifying
5264 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
5266 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
5269 /* Return a map that projects the elements in "set" onto their
5270 * "n" set dimensions starting at "first".
5271 * "type" should be equal to isl_dim_set.
5273 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
5274 enum isl_dim_type type
, unsigned first
, unsigned n
)
5279 if (type
!= isl_dim_set
)
5280 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5281 "only set dimensions can be projected out", goto error
);
5282 if (isl_set_check_range(set
, type
, first
, n
) < 0)
5283 return isl_set_free(set
);
5285 map
= isl_map_from_domain(set
);
5286 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
5287 for (i
= 0; i
< n
; ++i
)
5288 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
5296 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
5302 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5304 return isl_basic_map_free(bmap
);
5305 for (i
= 0; i
< n
; ++i
) {
5306 j
= isl_basic_map_alloc_div(bmap
);
5309 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
5313 isl_basic_map_free(bmap
);
5317 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
5319 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
5320 __isl_keep isl_basic_map
*bmap2
)
5322 isl_space
*space1
, *space2
;
5324 space1
= isl_basic_map_peek_space(bmap1
);
5325 space2
= isl_basic_map_peek_space(bmap2
);
5326 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
5327 space2
, isl_dim_in
);
5330 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
5332 static isl_stat
isl_basic_map_check_applies_range(
5333 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
5337 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
5339 return isl_stat_error
;
5341 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
5342 "spaces don't match", return isl_stat_error
);
5346 __isl_give isl_basic_map
*isl_basic_map_apply_range(
5347 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
5349 isl_space
*space_result
= NULL
;
5350 struct isl_basic_map
*bmap
;
5351 isl_size n_in
, n_out
, n
, nparam
;
5352 unsigned total
, pos
;
5353 struct isl_dim_map
*dim_map1
, *dim_map2
;
5355 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
5357 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
5360 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
5361 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
5362 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
5363 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
5364 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
5367 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
5368 isl_basic_map_get_space(bmap2
));
5370 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
5371 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5372 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5373 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5374 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
5375 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5376 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
5377 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
5378 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5379 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
5380 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
5382 bmap
= isl_basic_map_alloc_space(space_result
,
5383 bmap1
->n_div
+ bmap2
->n_div
+ n
,
5384 bmap1
->n_eq
+ bmap2
->n_eq
,
5385 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5386 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5387 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5388 bmap
= add_divs(bmap
, n
);
5389 bmap
= isl_basic_map_simplify(bmap
);
5390 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5391 return isl_basic_map_finalize(bmap
);
5393 isl_basic_map_free(bmap1
);
5394 isl_basic_map_free(bmap2
);
5398 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
5399 __isl_take isl_basic_map
*bmap
)
5401 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
5404 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
5407 isl_basic_set_free(bset
);
5408 isl_basic_map_free(bmap
);
5412 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
5413 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
5415 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
5417 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
5418 bmap2
->dim
, isl_dim_in
))
5419 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
5420 "spaces don't match", goto error
);
5422 bmap1
= isl_basic_map_reverse(bmap1
);
5423 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
5424 return isl_basic_map_reverse(bmap1
);
5426 isl_basic_map_free(bmap1
);
5427 isl_basic_map_free(bmap2
);
5431 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5432 * A \cap B -> f(A) + f(B)
5434 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
5435 __isl_take isl_basic_map
*bmap2
)
5437 isl_size n_in
, n_out
, nparam
;
5438 unsigned total
, pos
;
5439 struct isl_basic_map
*bmap
= NULL
;
5440 struct isl_dim_map
*dim_map1
, *dim_map2
;
5443 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
5446 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
5447 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
5448 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
5449 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5452 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
5453 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5454 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
5455 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5456 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
5457 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5458 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
5459 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
5460 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5461 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
5462 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
5464 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
5465 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
5466 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
5467 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5468 for (i
= 0; i
< n_out
; ++i
) {
5469 int j
= isl_basic_map_alloc_equality(bmap
);
5472 isl_seq_clr(bmap
->eq
[j
], 1+total
);
5473 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
5474 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
5475 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
5477 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5478 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5479 bmap
= add_divs(bmap
, 2 * n_out
);
5481 bmap
= isl_basic_map_simplify(bmap
);
5482 return isl_basic_map_finalize(bmap
);
5484 isl_basic_map_free(bmap
);
5485 isl_basic_map_free(bmap1
);
5486 isl_basic_map_free(bmap2
);
5490 /* Given two maps A -> f(A) and B -> g(B), construct a map
5491 * A \cap B -> f(A) + f(B)
5493 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
5494 __isl_take isl_map
*map2
)
5496 struct isl_map
*result
;
5499 if (isl_map_check_equal_space(map1
, map2
) < 0)
5502 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5503 map1
->n
* map2
->n
, 0);
5506 for (i
= 0; i
< map1
->n
; ++i
)
5507 for (j
= 0; j
< map2
->n
; ++j
) {
5508 struct isl_basic_map
*part
;
5509 part
= isl_basic_map_sum(
5510 isl_basic_map_copy(map1
->p
[i
]),
5511 isl_basic_map_copy(map2
->p
[j
]));
5512 if (isl_basic_map_is_empty(part
))
5513 isl_basic_map_free(part
);
5515 result
= isl_map_add_basic_map(result
, part
);
5528 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
5529 __isl_take isl_set
*set2
)
5531 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5534 /* Given a basic map A -> f(A), construct A -> -f(A).
5536 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5542 bmap
= isl_basic_map_cow(bmap
);
5543 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5545 return isl_basic_map_free(bmap
);
5547 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5548 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5549 for (j
= 0; j
< n
; ++j
)
5550 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5551 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5552 for (j
= 0; j
< n
; ++j
)
5553 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5554 for (i
= 0; i
< bmap
->n_div
; ++i
)
5555 for (j
= 0; j
< n
; ++j
)
5556 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5557 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5558 return isl_basic_map_finalize(bmap
);
5561 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5563 return isl_basic_map_neg(bset
);
5566 /* Given a map A -> f(A), construct A -> -f(A).
5568 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5572 map
= isl_map_cow(map
);
5576 for (i
= 0; i
< map
->n
; ++i
) {
5577 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5588 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5590 return set_from_map(isl_map_neg(set_to_map(set
)));
5593 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5594 * A -> floor(f(A)/d).
5596 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5599 isl_size n_in
, n_out
, nparam
;
5600 unsigned total
, pos
;
5601 struct isl_basic_map
*result
= NULL
;
5602 struct isl_dim_map
*dim_map
;
5605 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5606 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5607 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5608 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5609 return isl_basic_map_free(bmap
);
5611 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5612 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5613 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5614 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5615 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5616 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5618 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5619 bmap
->n_div
+ n_out
,
5620 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5621 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5622 result
= add_divs(result
, n_out
);
5623 for (i
= 0; i
< n_out
; ++i
) {
5625 j
= isl_basic_map_alloc_inequality(result
);
5628 isl_seq_clr(result
->ineq
[j
], 1+total
);
5629 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5630 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5631 j
= isl_basic_map_alloc_inequality(result
);
5634 isl_seq_clr(result
->ineq
[j
], 1+total
);
5635 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5636 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5637 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5640 result
= isl_basic_map_simplify(result
);
5641 return isl_basic_map_finalize(result
);
5643 isl_basic_map_free(result
);
5647 /* Given a map A -> f(A) and an integer d, construct a map
5648 * A -> floor(f(A)/d).
5650 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5654 map
= isl_map_cow(map
);
5658 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5659 for (i
= 0; i
< map
->n
; ++i
) {
5660 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5664 map
= isl_map_unmark_normalized(map
);
5672 /* Given a map A -> f(A) and an integer d, construct a map
5673 * A -> floor(f(A)/d).
5675 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5676 __isl_take isl_val
*d
)
5680 if (!isl_val_is_int(d
))
5681 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5682 "expecting integer denominator", goto error
);
5683 map
= isl_map_floordiv(map
, d
->n
);
5692 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5700 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5701 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5702 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5703 if (total
< 0 || nparam
< 0 || n_in
< 0)
5704 return isl_basic_map_free(bmap
);
5705 i
= isl_basic_map_alloc_equality(bmap
);
5708 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5709 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5710 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5711 return isl_basic_map_finalize(bmap
);
5713 isl_basic_map_free(bmap
);
5717 /* Add a constraint to "bmap" expressing i_pos < o_pos
5719 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5727 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5728 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5729 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5730 if (total
< 0 || nparam
< 0 || n_in
< 0)
5731 return isl_basic_map_free(bmap
);
5732 i
= isl_basic_map_alloc_inequality(bmap
);
5735 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5736 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5737 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5738 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5739 return isl_basic_map_finalize(bmap
);
5741 isl_basic_map_free(bmap
);
5745 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5747 static __isl_give isl_basic_map
*var_less_or_equal(
5748 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5755 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5756 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5757 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5758 if (total
< 0 || nparam
< 0 || n_in
< 0)
5759 return isl_basic_map_free(bmap
);
5760 i
= isl_basic_map_alloc_inequality(bmap
);
5763 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5764 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5765 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5766 return isl_basic_map_finalize(bmap
);
5768 isl_basic_map_free(bmap
);
5772 /* Add a constraint to "bmap" expressing i_pos > o_pos
5774 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5782 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5783 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5784 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5785 if (total
< 0 || nparam
< 0 || n_in
< 0)
5786 return isl_basic_map_free(bmap
);
5787 i
= isl_basic_map_alloc_inequality(bmap
);
5790 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5791 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5792 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5793 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5794 return isl_basic_map_finalize(bmap
);
5796 isl_basic_map_free(bmap
);
5800 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5802 static __isl_give isl_basic_map
*var_more_or_equal(
5803 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5810 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5811 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5812 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5813 if (total
< 0 || nparam
< 0 || n_in
< 0)
5814 return isl_basic_map_free(bmap
);
5815 i
= isl_basic_map_alloc_inequality(bmap
);
5818 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5819 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5820 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5821 return isl_basic_map_finalize(bmap
);
5823 isl_basic_map_free(bmap
);
5827 __isl_give isl_basic_map
*isl_basic_map_equal(
5828 __isl_take isl_space
*space
, unsigned n_equal
)
5831 struct isl_basic_map
*bmap
;
5832 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5835 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5836 bmap
= var_equal(bmap
, i
);
5837 return isl_basic_map_finalize(bmap
);
5840 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5842 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5846 struct isl_basic_map
*bmap
;
5847 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5850 for (i
= 0; i
< pos
&& bmap
; ++i
)
5851 bmap
= var_equal(bmap
, i
);
5853 bmap
= var_less(bmap
, pos
);
5854 return isl_basic_map_finalize(bmap
);
5857 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5859 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5860 __isl_take isl_space
*space
, unsigned pos
)
5863 isl_basic_map
*bmap
;
5865 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5866 for (i
= 0; i
< pos
; ++i
)
5867 bmap
= var_equal(bmap
, i
);
5868 bmap
= var_less_or_equal(bmap
, pos
);
5869 return isl_basic_map_finalize(bmap
);
5872 /* Return a relation on "space" expressing i_pos > o_pos
5874 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5878 struct isl_basic_map
*bmap
;
5879 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5882 for (i
= 0; i
< pos
&& bmap
; ++i
)
5883 bmap
= var_equal(bmap
, i
);
5885 bmap
= var_more(bmap
, pos
);
5886 return isl_basic_map_finalize(bmap
);
5889 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5891 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5892 __isl_take isl_space
*space
, unsigned pos
)
5895 isl_basic_map
*bmap
;
5897 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5898 for (i
= 0; i
< pos
; ++i
)
5899 bmap
= var_equal(bmap
, i
);
5900 bmap
= var_more_or_equal(bmap
, pos
);
5901 return isl_basic_map_finalize(bmap
);
5904 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5905 unsigned n
, int equal
)
5907 struct isl_map
*map
;
5910 if (n
== 0 && equal
)
5911 return isl_map_universe(space
);
5913 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5915 for (i
= 0; i
+ 1 < n
; ++i
)
5916 map
= isl_map_add_basic_map(map
,
5917 isl_basic_map_less_at(isl_space_copy(space
), i
));
5920 map
= isl_map_add_basic_map(map
,
5921 isl_basic_map_less_or_equal_at(space
, n
- 1));
5923 map
= isl_map_add_basic_map(map
,
5924 isl_basic_map_less_at(space
, n
- 1));
5926 isl_space_free(space
);
5931 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5935 return map_lex_lte_first(space
, space
->n_out
, equal
);
5938 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5941 return map_lex_lte_first(space
, n
, 0);
5944 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5947 return map_lex_lte_first(space
, n
, 1);
5950 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5952 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5955 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5957 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5960 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5961 unsigned n
, int equal
)
5963 struct isl_map
*map
;
5966 if (n
== 0 && equal
)
5967 return isl_map_universe(space
);
5969 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5971 for (i
= 0; i
+ 1 < n
; ++i
)
5972 map
= isl_map_add_basic_map(map
,
5973 isl_basic_map_more_at(isl_space_copy(space
), i
));
5976 map
= isl_map_add_basic_map(map
,
5977 isl_basic_map_more_or_equal_at(space
, n
- 1));
5979 map
= isl_map_add_basic_map(map
,
5980 isl_basic_map_more_at(space
, n
- 1));
5982 isl_space_free(space
);
5987 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5991 return map_lex_gte_first(space
, space
->n_out
, equal
);
5994 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5997 return map_lex_gte_first(space
, n
, 0);
6000 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
6003 return map_lex_gte_first(space
, n
, 1);
6006 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
6008 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
6011 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
6013 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
6016 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
6017 __isl_take isl_set
*set2
)
6020 map
= isl_map_lex_le(isl_set_get_space(set1
));
6021 map
= isl_map_intersect_domain(map
, set1
);
6022 map
= isl_map_intersect_range(map
, set2
);
6026 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
6027 __isl_take isl_set
*set2
)
6030 map
= isl_map_lex_lt(isl_set_get_space(set1
));
6031 map
= isl_map_intersect_domain(map
, set1
);
6032 map
= isl_map_intersect_range(map
, set2
);
6036 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
6037 __isl_take isl_set
*set2
)
6040 map
= isl_map_lex_ge(isl_set_get_space(set1
));
6041 map
= isl_map_intersect_domain(map
, set1
);
6042 map
= isl_map_intersect_range(map
, set2
);
6046 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
6047 __isl_take isl_set
*set2
)
6050 map
= isl_map_lex_gt(isl_set_get_space(set1
));
6051 map
= isl_map_intersect_domain(map
, set1
);
6052 map
= isl_map_intersect_range(map
, set2
);
6056 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
6057 __isl_take isl_map
*map2
)
6060 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
6061 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6062 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6066 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
6067 __isl_take isl_map
*map2
)
6070 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
6071 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6072 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6076 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
6077 __isl_take isl_map
*map2
)
6080 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
6081 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6082 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6086 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
6087 __isl_take isl_map
*map2
)
6090 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
6091 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6092 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6096 /* For the div d = floor(f/m) at position "div", add the constraint
6100 static __isl_give isl_basic_map
*add_upper_div_constraint(
6101 __isl_take isl_basic_map
*bmap
, unsigned div
)
6104 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
6108 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6109 if (v_div
< 0 || n_div
< 0)
6110 return isl_basic_map_free(bmap
);
6112 i
= isl_basic_map_alloc_inequality(bmap
);
6114 return isl_basic_map_free(bmap
);
6115 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
6116 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
6121 /* For the div d = floor(f/m) at position "div", add the constraint
6123 * -(f-(m-1)) + m d >= 0
6125 static __isl_give isl_basic_map
*add_lower_div_constraint(
6126 __isl_take isl_basic_map
*bmap
, unsigned div
)
6129 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
6133 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6134 if (v_div
< 0 || n_div
< 0)
6135 return isl_basic_map_free(bmap
);
6137 i
= isl_basic_map_alloc_inequality(bmap
);
6139 return isl_basic_map_free(bmap
);
6140 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
6141 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
6142 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
6143 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
6148 /* For the div d = floor(f/m) at position "pos", add the constraints
6151 * -(f-(m-1)) + m d >= 0
6153 * Note that the second constraint is the negation of
6157 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
6158 __isl_take isl_basic_map
*bmap
, unsigned pos
)
6160 bmap
= add_upper_div_constraint(bmap
, pos
);
6161 bmap
= add_lower_div_constraint(bmap
, pos
);
6165 /* For the div d = floor(f/m) at position "pos", add the constraints
6168 * -(f-(m-1)) + m d >= 0
6170 * Note that the second constraint is the negation of
6174 __isl_give isl_basic_set
*isl_basic_set_add_div_constraints(
6175 __isl_take isl_basic_set
*bset
, unsigned pos
)
6177 isl_basic_map
*bmap
= bset_to_bmap(bset
);
6178 bmap
= isl_basic_map_add_div_constraints(bmap
, pos
);
6179 return bset_from_bmap(bmap
);
6182 /* For each known div d = floor(f/m), add the constraints
6185 * -(f-(m-1)) + m d >= 0
6187 * Remove duplicate constraints in case of some these div constraints
6188 * already appear in "bmap".
6190 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
6191 __isl_take isl_basic_map
*bmap
)
6195 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6197 return isl_basic_map_free(bmap
);
6201 bmap
= add_known_div_constraints(bmap
);
6202 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
6203 bmap
= isl_basic_map_finalize(bmap
);
6207 /* Add the div constraint of sign "sign" for div "div" of "bmap".
6209 * In particular, if this div is of the form d = floor(f/m),
6210 * then add the constraint
6214 * if sign < 0 or the constraint
6216 * -(f-(m-1)) + m d >= 0
6220 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
6221 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
6224 return add_upper_div_constraint(bmap
, div
);
6226 return add_lower_div_constraint(bmap
, div
);
6229 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
6230 __isl_take isl_basic_map
*bmap
)
6236 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
6238 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
6239 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
6240 return bset_from_bmap(bmap
);
6241 bmap
= isl_basic_map_cow(bmap
);
6244 space
= isl_basic_map_take_space(bmap
);
6245 space
= isl_space_underlying(space
, bmap
->n_div
);
6246 bmap
= isl_basic_map_restore_space(bmap
, space
);
6249 bmap
->extra
-= bmap
->n_div
;
6251 bmap
= isl_basic_map_finalize(bmap
);
6252 return bset_from_bmap(bmap
);
6254 isl_basic_map_free(bmap
);
6258 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
6259 __isl_take isl_basic_set
*bset
)
6261 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
6264 /* Replace each element in "list" by the result of applying
6265 * isl_basic_map_underlying_set to the element.
6267 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
6268 __isl_take isl_basic_map_list
*list
)
6273 n
= isl_basic_map_list_n_basic_map(list
);
6277 for (i
= 0; i
< n
; ++i
) {
6278 isl_basic_map
*bmap
;
6279 isl_basic_set
*bset
;
6281 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
6282 bset
= isl_basic_set_underlying_set(bmap
);
6283 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
6288 isl_basic_map_list_free(list
);
6292 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
6293 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
6295 struct isl_basic_map
*bmap
;
6296 struct isl_ctx
*ctx
;
6297 isl_size dim
, bmap_total
;
6304 if (isl_basic_set_check_no_params(bset
) < 0 ||
6305 isl_basic_set_check_no_locals(bset
) < 0)
6307 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
6308 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
6309 if (dim
< 0 || bmap_total
< 0)
6311 isl_assert(ctx
, dim
== bmap_total
, goto error
);
6312 if (like
->n_div
== 0) {
6313 isl_space
*space
= isl_basic_map_get_space(like
);
6314 isl_basic_map_free(like
);
6315 return isl_basic_map_reset_space(bset
, space
);
6317 bset
= isl_basic_set_cow(bset
);
6320 total
= dim
+ bset
->extra
;
6321 bmap
= bset_to_bmap(bset
);
6322 isl_space_free(isl_basic_map_take_space(bmap
));
6323 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
6326 bmap
->n_div
= like
->n_div
;
6327 bmap
->extra
+= like
->n_div
;
6331 ltotal
= total
- bmap
->extra
+ like
->extra
;
6334 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
6335 bmap
->extra
* (1 + 1 + total
));
6336 if (isl_blk_is_error(bmap
->block2
))
6338 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
6342 for (i
= 0; i
< bmap
->extra
; ++i
)
6343 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
6344 for (i
= 0; i
< like
->n_div
; ++i
) {
6345 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
6346 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
6348 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
6350 isl_basic_map_free(like
);
6351 bmap
= isl_basic_map_simplify(bmap
);
6352 bmap
= isl_basic_map_finalize(bmap
);
6355 isl_basic_map_free(like
);
6356 isl_basic_set_free(bset
);
6360 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
6361 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
6363 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
6364 bset_to_bmap(like
)));
6367 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
6371 map
= isl_map_cow(map
);
6374 map
->dim
= isl_space_cow(map
->dim
);
6378 for (i
= 1; i
< map
->n
; ++i
)
6379 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
6381 for (i
= 0; i
< map
->n
; ++i
) {
6382 map
->p
[i
] = bset_to_bmap(
6383 isl_basic_map_underlying_set(map
->p
[i
]));
6388 map
->dim
= isl_space_underlying(map
->dim
, 0);
6390 isl_space_free(map
->dim
);
6391 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
6395 return set_from_map(map
);
6401 /* Replace the space of "bmap" by "space".
6403 * If the space of "bmap" is identical to "space" (including the identifiers
6404 * of the input and output dimensions), then simply return the original input.
6406 __isl_give isl_basic_map
*isl_basic_map_reset_space(
6407 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
6410 isl_space
*bmap_space
;
6412 bmap_space
= isl_basic_map_peek_space(bmap
);
6413 equal
= isl_space_is_equal(bmap_space
, space
);
6414 if (equal
>= 0 && equal
)
6415 equal
= isl_space_has_equal_ids(bmap_space
, space
);
6419 isl_space_free(space
);
6422 isl_space_free(isl_basic_map_take_space(bmap
));
6423 bmap
= isl_basic_map_restore_space(bmap
, space
);
6425 bmap
= isl_basic_map_finalize(bmap
);
6429 isl_basic_map_free(bmap
);
6430 isl_space_free(space
);
6434 __isl_give isl_basic_set
*isl_basic_set_reset_space(
6435 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
6437 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
6441 /* Check that the total dimensions of "map" and "space" are the same.
6443 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
6444 __isl_keep isl_space
*space
)
6446 isl_size dim1
, dim2
;
6448 dim1
= isl_map_dim(map
, isl_dim_all
);
6449 dim2
= isl_space_dim(space
, isl_dim_all
);
6450 if (dim1
< 0 || dim2
< 0)
6451 return isl_stat_error
;
6454 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6455 "total dimensions do not match", return isl_stat_error
);
6458 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
6459 __isl_take isl_space
*space
)
6463 map
= isl_map_cow(map
);
6467 for (i
= 0; i
< map
->n
; ++i
) {
6468 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
6469 isl_space_copy(space
));
6473 isl_space_free(isl_map_take_space(map
));
6474 map
= isl_map_restore_space(map
, space
);
6479 isl_space_free(space
);
6483 /* Replace the space of "map" by "space", without modifying
6484 * the dimension of "map".
6486 * If the space of "map" is identical to "space" (including the identifiers
6487 * of the input and output dimensions), then simply return the original input.
6489 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
6490 __isl_take isl_space
*space
)
6493 isl_space
*map_space
;
6495 map_space
= isl_map_peek_space(map
);
6496 equal
= isl_space_is_equal(map_space
, space
);
6497 if (equal
>= 0 && equal
)
6498 equal
= isl_space_has_equal_ids(map_space
, space
);
6502 isl_space_free(space
);
6505 if (check_map_space_equal_total_dim(map
, space
) < 0)
6507 return isl_map_reset_space(map
, space
);
6510 isl_space_free(space
);
6514 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
6515 __isl_take isl_space
*space
)
6517 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
6520 /* Compute the parameter domain of the given basic set.
6522 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
6528 is_params
= isl_basic_set_is_params(bset
);
6530 return isl_basic_set_free(bset
);
6534 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6536 return isl_basic_set_free(bset
);
6537 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6538 space
= isl_basic_set_get_space(bset
);
6539 space
= isl_space_params(space
);
6540 bset
= isl_basic_set_reset_space(bset
, space
);
6544 /* Construct a zero-dimensional basic set with the given parameter domain.
6546 __isl_give isl_basic_set
*isl_basic_set_from_params(
6547 __isl_take isl_basic_set
*bset
)
6550 space
= isl_basic_set_get_space(bset
);
6551 space
= isl_space_set_from_params(space
);
6552 bset
= isl_basic_set_reset_space(bset
, space
);
6556 /* Compute the parameter domain of the given set.
6558 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6560 return isl_map_params(set_to_map(set
));
6563 /* Construct a zero-dimensional set with the given parameter domain.
6565 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6568 space
= isl_set_get_space(set
);
6569 space
= isl_space_set_from_params(space
);
6570 set
= isl_set_reset_space(set
, space
);
6574 /* Compute the parameter domain of the given map.
6576 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6579 isl_size n_in
, n_out
;
6581 n_in
= isl_map_dim(map
, isl_dim_in
);
6582 n_out
= isl_map_dim(map
, isl_dim_out
);
6583 if (n_in
< 0 || n_out
< 0)
6584 return isl_map_free(map
);
6585 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6586 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6587 space
= isl_map_get_space(map
);
6588 space
= isl_space_params(space
);
6589 map
= isl_map_reset_space(map
, space
);
6593 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6598 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6600 return isl_basic_map_free(bmap
);
6601 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6603 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6605 return isl_basic_map_reset_space(bmap
, space
);
6608 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6611 return isl_bool_error
;
6612 return isl_space_may_be_set(bmap
->dim
);
6615 /* Is this basic map actually a set?
6616 * Users should never call this function. Outside of isl,
6617 * the type should indicate whether something is a set or a map.
6619 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6622 return isl_bool_error
;
6623 return isl_space_is_set(bmap
->dim
);
6626 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6630 is_set
= isl_basic_map_is_set(bmap
);
6635 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6637 isl_basic_map_free(bmap
);
6641 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6642 __isl_take isl_basic_map
*bmap
)
6646 isl_basic_map
*domain
;
6647 isl_size nparam
, n_in
, n_out
;
6649 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6650 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6651 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6652 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6653 return isl_basic_map_free(bmap
);
6655 space
= isl_basic_map_get_space(bmap
);
6656 space
= isl_space_from_range(isl_space_domain(space
));
6657 domain
= isl_basic_map_universe(space
);
6659 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6660 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6661 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6663 for (i
= 0; i
< n_in
; ++i
)
6664 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6667 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6668 return isl_basic_map_finalize(bmap
);
6671 __isl_give isl_basic_map
*isl_basic_map_range_map(
6672 __isl_take isl_basic_map
*bmap
)
6676 isl_basic_map
*range
;
6677 isl_size nparam
, n_in
, n_out
;
6679 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6680 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6681 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6682 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6683 return isl_basic_map_free(bmap
);
6685 space
= isl_basic_map_get_space(bmap
);
6686 space
= isl_space_from_range(isl_space_range(space
));
6687 range
= isl_basic_map_universe(space
);
6689 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6690 bmap
= isl_basic_map_apply_range(bmap
, range
);
6691 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6693 for (i
= 0; i
< n_out
; ++i
)
6694 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6697 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6698 return isl_basic_map_finalize(bmap
);
6701 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6705 return isl_space_may_be_set(map
->dim
);
6708 /* Is this map actually a set?
6709 * Users should never call this function. Outside of isl,
6710 * the type should indicate whether something is a set or a map.
6712 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6715 return isl_bool_error
;
6716 return isl_space_is_set(map
->dim
);
6719 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6724 n_in
= isl_map_dim(map
, isl_dim_in
);
6726 return set_from_map(isl_map_free(map
));
6727 space
= isl_space_range(isl_map_get_space(map
));
6729 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6731 return set_from_map(isl_map_reset_space(map
, space
));
6734 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6735 * to each of its basic maps.
6737 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6738 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6739 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6744 map
= isl_map_cow(map
);
6748 for (i
= 0; i
< map
->n
; ++i
) {
6749 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6751 return isl_map_free(map
);
6753 map
= isl_map_unmark_normalized(map
);
6755 space
= isl_map_take_space(map
);
6756 space
= fn_space(space
);
6757 map
= isl_map_restore_space(map
, space
);
6762 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6764 return isl_map_transform(map
, &isl_space_domain_map
,
6765 &isl_basic_map_domain_map
);
6768 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6770 return isl_map_transform(map
, &isl_space_range_map
,
6771 &isl_basic_map_range_map
);
6774 /* Given a wrapped map of the form A[B -> C],
6775 * return the map A[B -> C] -> B.
6777 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6784 if (!isl_set_has_tuple_id(set
))
6785 return isl_map_domain_map(isl_set_unwrap(set
));
6787 id
= isl_set_get_tuple_id(set
);
6788 map
= isl_map_domain_map(isl_set_unwrap(set
));
6789 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6794 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6795 __isl_take isl_basic_set
*bset
)
6797 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6800 __isl_give isl_basic_map
*isl_basic_map_from_range(
6801 __isl_take isl_basic_set
*bset
)
6804 space
= isl_basic_set_get_space(bset
);
6805 space
= isl_space_from_range(space
);
6806 bset
= isl_basic_set_reset_space(bset
, space
);
6807 return bset_to_bmap(bset
);
6810 /* Create a relation with the given set as range.
6811 * The domain of the created relation is a zero-dimensional
6812 * flat anonymous space.
6814 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6817 space
= isl_set_get_space(set
);
6818 space
= isl_space_from_range(space
);
6819 set
= isl_set_reset_space(set
, space
);
6820 return set_to_map(set
);
6823 /* Create a relation with the given set as domain.
6824 * The range of the created relation is a zero-dimensional
6825 * flat anonymous space.
6827 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6829 return isl_map_reverse(isl_map_from_range(set
));
6832 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6833 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6835 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6838 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6839 __isl_take isl_set
*range
)
6841 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6844 /* Return a newly allocated isl_map with given space and flags and
6845 * room for "n" basic maps.
6846 * Make sure that all cached information is cleared.
6848 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6851 struct isl_map
*map
;
6856 isl_die(space
->ctx
, isl_error_internal
,
6857 "negative number of basic maps", goto error
);
6858 map
= isl_calloc(space
->ctx
, struct isl_map
,
6859 sizeof(struct isl_map
) +
6860 (n
- 1) * sizeof(struct isl_basic_map
*));
6864 map
->ctx
= space
->ctx
;
6865 isl_ctx_ref(map
->ctx
);
6873 isl_space_free(space
);
6877 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6879 struct isl_basic_map
*bmap
;
6880 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6881 bmap
= isl_basic_map_set_to_empty(bmap
);
6885 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6887 struct isl_basic_set
*bset
;
6888 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6889 bset
= isl_basic_set_set_to_empty(bset
);
6893 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6895 struct isl_basic_map
*bmap
;
6896 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6897 bmap
= isl_basic_map_finalize(bmap
);
6901 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6903 struct isl_basic_set
*bset
;
6904 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6905 bset
= isl_basic_set_finalize(bset
);
6909 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6910 __isl_take isl_space
*space
)
6913 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6914 isl_basic_map
*bmap
;
6917 space
= isl_space_free(space
);
6918 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6919 for (i
= 0; i
< total
; ++i
) {
6920 int k
= isl_basic_map_alloc_inequality(bmap
);
6923 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6924 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6928 isl_basic_map_free(bmap
);
6932 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6933 __isl_take isl_space
*space
)
6935 return isl_basic_map_nat_universe(space
);
6938 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6940 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6943 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6945 return isl_map_nat_universe(space
);
6948 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6950 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6953 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6955 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6958 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6960 struct isl_map
*map
;
6963 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6964 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6968 /* This function performs the same operation as isl_map_universe,
6969 * but is considered as a function on an isl_space when exported.
6971 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6973 return isl_map_universe(space
);
6976 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6978 struct isl_set
*set
;
6981 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6982 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6986 /* This function performs the same operation as isl_set_universe,
6987 * but is considered as a function on an isl_space when exported.
6989 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6991 return isl_set_universe(space
);
6994 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6997 struct isl_map
*dup
;
7001 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
7002 for (i
= 0; i
< map
->n
; ++i
)
7003 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
7007 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
7008 __isl_take isl_basic_map
*bmap
)
7012 if (isl_basic_map_plain_is_empty(bmap
)) {
7013 isl_basic_map_free(bmap
);
7016 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
7018 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
7019 map
->p
[map
->n
] = bmap
;
7021 map
= isl_map_unmark_normalized(map
);
7027 isl_basic_map_free(bmap
);
7031 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
7042 isl_ctx_deref(map
->ctx
);
7043 for (i
= 0; i
< map
->n
; ++i
)
7044 isl_basic_map_free(map
->p
[i
]);
7045 isl_space_free(map
->dim
);
7051 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
7052 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
7057 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7059 return isl_basic_map_free(bmap
);
7061 bmap
= isl_basic_map_cow(bmap
);
7062 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
7063 j
= isl_basic_map_alloc_equality(bmap
);
7066 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
7067 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
7068 isl_int_set_si(bmap
->eq
[j
][0], value
);
7069 bmap
= isl_basic_map_simplify(bmap
);
7070 return isl_basic_map_finalize(bmap
);
7072 isl_basic_map_free(bmap
);
7076 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
7077 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
7082 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7084 return isl_basic_map_free(bmap
);
7086 bmap
= isl_basic_map_cow(bmap
);
7087 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
7088 j
= isl_basic_map_alloc_equality(bmap
);
7091 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
7092 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
7093 isl_int_set(bmap
->eq
[j
][0], value
);
7094 bmap
= isl_basic_map_simplify(bmap
);
7095 return isl_basic_map_finalize(bmap
);
7097 isl_basic_map_free(bmap
);
7101 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
7102 enum isl_dim_type type
, unsigned pos
, int value
)
7104 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7105 return isl_basic_map_free(bmap
);
7106 return isl_basic_map_fix_pos_si(bmap
,
7107 isl_basic_map_offset(bmap
, type
) + pos
, value
);
7110 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
7111 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7113 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7114 return isl_basic_map_free(bmap
);
7115 return isl_basic_map_fix_pos(bmap
,
7116 isl_basic_map_offset(bmap
, type
) + pos
, value
);
7119 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
7120 * to be equal to "v".
7122 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
7123 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7127 if (!isl_val_is_int(v
))
7128 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
7129 "expecting integer value", goto error
);
7130 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7132 pos
+= isl_basic_map_offset(bmap
, type
);
7133 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
7137 isl_basic_map_free(bmap
);
7142 /* Fix the value of the variable at position "pos" of type "type" of "bset"
7143 * to be equal to "v".
7145 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
7146 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7148 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
7151 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
7152 enum isl_dim_type type
, unsigned pos
, int value
)
7154 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
7158 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
7159 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7161 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
7165 /* Remove the basic map at position "i" from "map" if this basic map
7166 * is (obviously) empty.
7168 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
7175 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
7177 return isl_map_free(map
);
7181 isl_basic_map_free(map
->p
[i
]);
7184 map
->p
[i
] = map
->p
[map
->n
];
7185 map
= isl_map_unmark_normalized(map
);
7192 /* Perform "fn" on each basic map of "map", where we may not be holding
7193 * the only reference to "map".
7194 * In particular, "fn" should be a semantics preserving operation
7195 * that we want to apply to all copies of "map". We therefore need
7196 * to be careful not to modify "map" in a way that breaks "map"
7197 * in case anything goes wrong.
7199 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
7200 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
7202 struct isl_basic_map
*bmap
;
7208 for (i
= map
->n
- 1; i
>= 0; --i
) {
7209 bmap
= isl_basic_map_copy(map
->p
[i
]);
7213 isl_basic_map_free(map
->p
[i
]);
7215 map
= remove_if_empty(map
, i
);
7226 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
7227 enum isl_dim_type type
, unsigned pos
, int value
)
7231 map
= isl_map_cow(map
);
7232 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7233 return isl_map_free(map
);
7234 for (i
= map
->n
- 1; i
>= 0; --i
) {
7235 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
7236 map
= remove_if_empty(map
, i
);
7240 map
= isl_map_unmark_normalized(map
);
7244 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
7245 enum isl_dim_type type
, unsigned pos
, int value
)
7247 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
7250 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
7251 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7255 map
= isl_map_cow(map
);
7256 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7257 return isl_map_free(map
);
7258 for (i
= 0; i
< map
->n
; ++i
) {
7259 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
7263 map
= isl_map_unmark_normalized(map
);
7270 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
7271 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7273 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
7276 /* Fix the value of the variable at position "pos" of type "type" of "map"
7277 * to be equal to "v".
7279 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
7280 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7284 map
= isl_map_cow(map
);
7288 if (!isl_val_is_int(v
))
7289 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7290 "expecting integer value", goto error
);
7291 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7293 for (i
= map
->n
- 1; i
>= 0; --i
) {
7294 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
7296 map
= remove_if_empty(map
, i
);
7300 map
= isl_map_unmark_normalized(map
);
7309 /* Fix the value of the variable at position "pos" of type "type" of "set"
7310 * to be equal to "v".
7312 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
7313 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7315 return isl_map_fix_val(set
, type
, pos
, v
);
7318 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
7319 unsigned input
, int value
)
7321 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
7324 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
7327 return set_from_map(isl_map_fix_si(set_to_map(set
),
7328 isl_dim_set
, dim
, value
));
7331 static __isl_give isl_basic_map
*basic_map_bound_si(
7332 __isl_take isl_basic_map
*bmap
,
7333 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
7338 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7339 return isl_basic_map_free(bmap
);
7340 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7342 return isl_basic_map_free(bmap
);
7343 pos
+= isl_basic_map_offset(bmap
, type
);
7344 bmap
= isl_basic_map_cow(bmap
);
7345 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
7346 j
= isl_basic_map_alloc_inequality(bmap
);
7349 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
7351 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
7352 isl_int_set_si(bmap
->ineq
[j
][0], value
);
7354 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
7355 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
7357 bmap
= isl_basic_map_simplify(bmap
);
7358 return isl_basic_map_finalize(bmap
);
7360 isl_basic_map_free(bmap
);
7364 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
7365 __isl_take isl_basic_map
*bmap
,
7366 enum isl_dim_type type
, unsigned pos
, int value
)
7368 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
7371 /* Constrain the values of the given dimension to be no greater than "value".
7373 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
7374 __isl_take isl_basic_map
*bmap
,
7375 enum isl_dim_type type
, unsigned pos
, int value
)
7377 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
7380 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
7381 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
7385 map
= isl_map_cow(map
);
7386 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7387 return isl_map_free(map
);
7388 for (i
= 0; i
< map
->n
; ++i
) {
7389 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
7390 type
, pos
, value
, upper
);
7394 map
= isl_map_unmark_normalized(map
);
7401 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
7402 enum isl_dim_type type
, unsigned pos
, int value
)
7404 return map_bound_si(map
, type
, pos
, value
, 0);
7407 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
7408 enum isl_dim_type type
, unsigned pos
, int value
)
7410 return map_bound_si(map
, type
, pos
, value
, 1);
7413 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
7414 enum isl_dim_type type
, unsigned pos
, int value
)
7416 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
7420 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
7421 enum isl_dim_type type
, unsigned pos
, int value
)
7423 return isl_map_upper_bound_si(set
, type
, pos
, value
);
7426 /* Bound the given variable of "bmap" from below (or above is "upper"
7427 * is set) to "value".
7429 static __isl_give isl_basic_map
*basic_map_bound(
7430 __isl_take isl_basic_map
*bmap
,
7431 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7436 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7437 return isl_basic_map_free(bmap
);
7438 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7440 return isl_basic_map_free(bmap
);
7441 pos
+= isl_basic_map_offset(bmap
, type
);
7442 bmap
= isl_basic_map_cow(bmap
);
7443 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
7444 j
= isl_basic_map_alloc_inequality(bmap
);
7447 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
7449 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
7450 isl_int_set(bmap
->ineq
[j
][0], value
);
7452 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
7453 isl_int_neg(bmap
->ineq
[j
][0], value
);
7455 bmap
= isl_basic_map_simplify(bmap
);
7456 return isl_basic_map_finalize(bmap
);
7458 isl_basic_map_free(bmap
);
7462 /* Bound the given variable of "map" from below (or above is "upper"
7463 * is set) to "value".
7465 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7466 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7470 map
= isl_map_cow(map
);
7471 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7472 return isl_map_free(map
);
7473 for (i
= map
->n
- 1; i
>= 0; --i
) {
7474 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
7475 map
= remove_if_empty(map
, i
);
7479 map
= isl_map_unmark_normalized(map
);
7483 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
7484 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7486 return map_bound(map
, type
, pos
, value
, 0);
7489 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
7490 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7492 return map_bound(map
, type
, pos
, value
, 1);
7495 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
7496 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7498 return isl_map_lower_bound(set
, type
, pos
, value
);
7501 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
7502 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7504 return isl_map_upper_bound(set
, type
, pos
, value
);
7507 /* Force the values of the variable at position "pos" of type "type" of "map"
7508 * to be no smaller than "value".
7510 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
7511 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7515 if (!isl_val_is_int(value
))
7516 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7517 "expecting integer value", goto error
);
7518 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
7519 isl_val_free(value
);
7522 isl_val_free(value
);
7527 /* Force the values of the variable at position "pos" of type "type" of "set"
7528 * to be no smaller than "value".
7530 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
7531 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7535 map
= set_to_map(set
);
7536 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
7539 /* Force the values of the variable at position "pos" of type "type" of "map"
7540 * to be no greater than "value".
7542 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
7543 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7547 if (!isl_val_is_int(value
))
7548 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7549 "expecting integer value", goto error
);
7550 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7551 isl_val_free(value
);
7554 isl_val_free(value
);
7559 /* Force the values of the variable at position "pos" of type "type" of "set"
7560 * to be no greater than "value".
7562 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7563 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7567 map
= set_to_map(set
);
7568 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7571 /* If "mv" has an explicit domain, then intersect the domain of "map"
7572 * with this explicit domain.
7574 * An isl_multi_val object never has an explicit domain,
7575 * so simply return "map".
7577 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7578 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7585 #include "isl_map_bound_templ.c"
7587 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7588 * for each set dimension, by treating the set as a map.
7590 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7591 __isl_take isl_multi_val
*bound
,
7592 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7593 unsigned pos
, __isl_take isl_val
*value
))
7597 map
= set_to_map(set
);
7598 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7603 #include "isl_map_bound_templ.c"
7605 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7606 * for each set dimension, by converting the set and the bound
7607 * to objects living in a map space.
7609 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7610 __isl_take isl_multi_pw_aff
*bound
,
7611 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7612 unsigned pos
, __isl_take TYPE
*value
))
7616 map
= isl_map_from_range(set
);
7617 bound
= isl_multi_pw_aff_from_range(bound
);
7618 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7619 return isl_map_range(map
);
7622 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7623 * setting a bound on the given output dimension.
7625 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7626 unsigned pos
, __isl_take isl_val
*v
)
7628 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7631 /* Force the values of the set dimensions of "set"
7632 * to be no smaller than the corresponding values in "lower".
7634 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7635 __isl_take isl_multi_val
*lower
)
7637 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7640 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7641 * setting a bound on the given output dimension.
7643 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7644 unsigned pos
, __isl_take isl_val
*v
)
7646 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7649 /* Force the values of the set dimensions of "set"
7650 * to be no greater than the corresponding values in "upper".
7652 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7653 __isl_take isl_multi_val
*upper
)
7655 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7658 /* Force the symbolic constant expression "bound"
7659 * to satisfy the relation "order" with respect to
7660 * the output variable at position "pos" of "map".
7662 * Create an affine expression representing the output variable
7663 * in terms of the range and
7664 * compare it using "order" to "bound" (defined on the domain).
7665 * The result is a relation between elements in domain and range that
7666 * can be intersected with "map".
7668 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7669 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7670 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7671 __isl_take isl_pw_aff
*pa2
))
7674 isl_local_space
*ls
;
7677 space
= isl_space_range(isl_map_get_space(map
));
7678 ls
= isl_local_space_from_space(space
);
7679 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7680 map
= isl_map_intersect(map
, order(bound
, var
));
7684 /* Force the values of the output variable at position "pos" of "map"
7685 * to be no smaller than the symbolic constant expression "lower".
7687 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7688 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7690 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7693 /* Force the values of the output variable at position "pos" of "map"
7694 * to be no greater than the symbolic constant expression "upper".
7696 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7697 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7699 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7702 /* Force the values of the set dimensions of "set"
7703 * to be no smaller than the corresponding constant symbolic expressions
7706 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7707 __isl_take isl_multi_pw_aff
*lower
)
7709 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7712 /* Force the values of the set dimensions of "set"
7713 * to be no greater than the corresponding constant symbolic expressions
7716 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7717 __isl_take isl_multi_pw_aff
*upper
)
7719 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7722 /* Force the values of the output dimensions of "map"
7723 * to be no smaller than the corresponding constant symbolic expressions
7726 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7727 __isl_take isl_multi_pw_aff
*lower
)
7729 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7732 /* Force the values of the output dimensions of "map"
7733 * to be no greater than the corresponding constant symbolic expressions
7736 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7737 __isl_take isl_multi_pw_aff
*upper
)
7739 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7742 /* Bound the given variable of "bset" from below (or above is "upper"
7743 * is set) to "value".
7745 static __isl_give isl_basic_set
*isl_basic_set_bound(
7746 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7747 isl_int value
, int upper
)
7749 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7750 type
, pos
, value
, upper
));
7753 /* Bound the given variable of "bset" from below (or above is "upper"
7754 * is set) to "value".
7756 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7757 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7758 __isl_take isl_val
*value
, int upper
)
7762 if (!isl_val_is_int(value
))
7763 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7764 "expecting integer value", goto error
);
7765 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7766 isl_val_free(value
);
7769 isl_val_free(value
);
7770 isl_basic_set_free(bset
);
7774 /* Bound the given variable of "bset" from below to "value".
7776 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7777 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7778 __isl_take isl_val
*value
)
7780 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7783 /* Bound the given variable of "bset" from above to "value".
7785 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7786 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7787 __isl_take isl_val
*value
)
7789 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7792 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7794 return isl_map_transform(map
, &isl_space_reverse
,
7795 &isl_basic_map_reverse
);
7798 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7800 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7802 return isl_map_transform(map
, &isl_space_domain_reverse
,
7803 &isl_basic_map_domain_reverse
);
7806 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7808 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7810 return isl_map_transform(map
, &isl_space_range_reverse
,
7811 &isl_basic_map_range_reverse
);
7814 /* Given a set (A -> B), return the corresponding set (B -> A).
7816 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7818 isl_map
*map
= set_to_map(set
);
7820 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7821 &isl_basic_map_set_reverse
);
7822 return set_from_map(map
);
7826 #define TYPE isl_pw_multi_aff
7828 #define SUFFIX _pw_multi_aff
7830 #define EMPTY isl_pw_multi_aff_empty
7832 #define ADD isl_pw_multi_aff_union_add
7833 #include "isl_map_lexopt_templ.c"
7835 /* Given a map "map", compute the lexicographically minimal
7836 * (or maximal) image element for each domain element in dom,
7837 * in the form of an isl_pw_multi_aff.
7838 * If "empty" is not NULL, then set *empty to those elements in dom that
7839 * do not have an image element.
7840 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7841 * should be computed over the domain of "map". "empty" is also NULL
7844 * We first compute the lexicographically minimal or maximal element
7845 * in the first basic map. This results in a partial solution "res"
7846 * and a subset "todo" of dom that still need to be handled.
7847 * We then consider each of the remaining maps in "map" and successively
7848 * update both "res" and "todo".
7849 * If "empty" is NULL, then the todo sets are not needed and therefore
7850 * also not computed.
7852 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7853 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7854 __isl_give isl_set
**empty
, unsigned flags
)
7858 isl_pw_multi_aff
*res
;
7861 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7862 if (!map
|| (!full
&& !dom
))
7865 if (isl_map_plain_is_empty(map
)) {
7870 return isl_pw_multi_aff_from_map(map
);
7873 res
= basic_map_partial_lexopt_pw_multi_aff(
7874 isl_basic_map_copy(map
->p
[0]),
7875 isl_set_copy(dom
), empty
, flags
);
7879 for (i
= 1; i
< map
->n
; ++i
) {
7880 isl_pw_multi_aff
*res_i
;
7882 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7883 isl_basic_map_copy(map
->p
[i
]),
7884 isl_set_copy(dom
), empty
, flags
);
7886 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7887 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7889 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7892 todo
= isl_set_intersect(todo
, *empty
);
7911 #define TYPE isl_map
7915 #define EMPTY isl_map_empty
7917 #define ADD isl_map_union_disjoint
7918 #include "isl_map_lexopt_templ.c"
7920 /* Given a map "map", compute the lexicographically minimal
7921 * (or maximal) image element for each domain element in "dom",
7922 * in the form of an isl_map.
7923 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7924 * do not have an image element.
7925 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7926 * should be computed over the domain of "map". "empty" is also NULL
7929 * If the input consists of more than one disjunct, then first
7930 * compute the desired result in the form of an isl_pw_multi_aff and
7931 * then convert that into an isl_map.
7933 * This function used to have an explicit implementation in terms
7934 * of isl_maps, but it would continually intersect the domains of
7935 * partial results with the complement of the domain of the next
7936 * partial solution, potentially leading to an explosion in the number
7937 * of disjuncts if there are several disjuncts in the input.
7938 * An even earlier implementation of this function would look for
7939 * better results in the domain of the partial result and for extra
7940 * results in the complement of this domain, which would lead to
7941 * even more splintering.
7943 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7944 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7945 __isl_give isl_set
**empty
, unsigned flags
)
7948 struct isl_map
*res
;
7949 isl_pw_multi_aff
*pma
;
7951 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7952 if (!map
|| (!full
&& !dom
))
7955 if (isl_map_plain_is_empty(map
)) {
7964 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7970 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7972 return isl_map_from_pw_multi_aff_internal(pma
);
7981 __isl_give isl_map
*isl_map_partial_lexmax(
7982 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7983 __isl_give isl_set
**empty
)
7985 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7988 __isl_give isl_map
*isl_map_partial_lexmin(
7989 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7990 __isl_give isl_set
**empty
)
7992 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7995 __isl_give isl_set
*isl_set_partial_lexmin(
7996 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7997 __isl_give isl_set
**empty
)
7999 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
8003 __isl_give isl_set
*isl_set_partial_lexmax(
8004 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
8005 __isl_give isl_set
**empty
)
8007 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
8011 /* Compute the lexicographic minimum (or maximum if "flags" includes
8012 * ISL_OPT_MAX) of "bset" over its parametric domain.
8014 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
8017 return isl_basic_map_lexopt(bset
, flags
);
8020 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
8022 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
8025 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
8027 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
8030 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
8032 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
8035 /* Compute the lexicographic minimum of "bset" over its parametric domain
8036 * for the purpose of quantifier elimination.
8037 * That is, find an explicit representation for all the existentially
8038 * quantified variables in "bset" by computing their lexicographic
8041 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
8042 __isl_take isl_basic_set
*bset
)
8044 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
8047 /* Given a basic map with one output dimension, compute the minimum or
8048 * maximum of that dimension as an isl_pw_aff.
8050 * Compute the optimum as a lexicographic optimum over the single
8051 * output dimension and extract the single isl_pw_aff from the result.
8053 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
8056 isl_pw_multi_aff
*pma
;
8059 bmap
= isl_basic_map_copy(bmap
);
8060 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
8061 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
8062 isl_pw_multi_aff_free(pma
);
8067 /* Compute the minimum or maximum of the given output dimension
8068 * as a function of the parameters and the input dimensions,
8069 * but independently of the other output dimensions.
8071 * We first project out the other output dimension and then compute
8072 * the "lexicographic" maximum in each basic map, combining the results
8073 * using isl_pw_aff_union_max.
8075 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
8082 n_out
= isl_map_dim(map
, isl_dim_out
);
8084 map
= isl_map_free(map
);
8085 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
8086 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
8091 isl_space
*space
= isl_map_get_space(map
);
8093 return isl_pw_aff_empty(space
);
8096 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
8097 for (i
= 1; i
< map
->n
; ++i
) {
8098 isl_pw_aff
*pwaff_i
;
8100 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
8101 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
8109 /* Compute the minimum of the given output dimension as a function of the
8110 * parameters and input dimensions, but independently of
8111 * the other output dimensions.
8113 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
8115 return map_dim_opt(map
, pos
, 0);
8118 /* Compute the maximum of the given output dimension as a function of the
8119 * parameters and input dimensions, but independently of
8120 * the other output dimensions.
8122 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
8124 return map_dim_opt(map
, pos
, 1);
8127 /* Compute the minimum or maximum of the given set dimension
8128 * as a function of the parameters,
8129 * but independently of the other set dimensions.
8131 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
8134 return map_dim_opt(set
, pos
, max
);
8137 /* Compute the maximum of the given set dimension as a function of the
8138 * parameters, but independently of the other set dimensions.
8140 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
8142 return set_dim_opt(set
, pos
, 1);
8145 /* Compute the minimum of the given set dimension as a function of the
8146 * parameters, but independently of the other set dimensions.
8148 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
8150 return set_dim_opt(set
, pos
, 0);
8153 /* Apply a preimage specified by "mat" on the parameters of "bset".
8154 * bset is assumed to have only parameters and divs.
8156 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
8157 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
8161 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8162 if (nparam
< 0 || !mat
)
8165 bset
->dim
= isl_space_cow(bset
->dim
);
8169 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
8171 bset
->dim
->nparam
= 0;
8172 bset
->dim
->n_out
= nparam
;
8173 bset
= isl_basic_set_preimage(bset
, mat
);
8175 bset
->dim
->nparam
= bset
->dim
->n_out
;
8176 bset
->dim
->n_out
= 0;
8181 isl_basic_set_free(bset
);
8185 /* Apply a preimage specified by "mat" on the parameters of "set".
8186 * set is assumed to have only parameters and divs.
8188 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
8189 __isl_take isl_mat
*mat
)
8194 nparam
= isl_set_dim(set
, isl_dim_param
);
8195 if (nparam
< 0 || !mat
)
8198 if (mat
->n_row
!= 1 + nparam
)
8199 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
8200 "unexpected number of rows", goto error
);
8202 space
= isl_set_get_space(set
);
8203 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
8204 isl_dim_param
, 0, nparam
);
8205 set
= isl_set_reset_space(set
, space
);
8206 set
= isl_set_preimage(set
, mat
);
8207 nparam
= isl_set_dim(set
, isl_dim_out
);
8209 set
= isl_set_free(set
);
8210 space
= isl_set_get_space(set
);
8211 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
8212 isl_dim_out
, 0, nparam
);
8213 set
= isl_set_reset_space(set
, space
);
8221 /* Intersect the basic set "bset" with the affine space specified by the
8222 * equalities in "eq".
8224 static __isl_give isl_basic_set
*basic_set_append_equalities(
8225 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
8233 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
8237 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
8238 for (i
= 0; i
< eq
->n_row
; ++i
) {
8239 k
= isl_basic_set_alloc_equality(bset
);
8242 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
8243 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
8247 bset
= isl_basic_set_gauss(bset
, NULL
);
8248 bset
= isl_basic_set_finalize(bset
);
8253 isl_basic_set_free(bset
);
8257 /* Intersect the set "set" with the affine space specified by the
8258 * equalities in "eq".
8260 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
8261 __isl_take isl_mat
*eq
)
8268 for (i
= 0; i
< set
->n
; ++i
) {
8269 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
8282 /* Given a basic set "bset" that only involves parameters and existentially
8283 * quantified variables, return the index of the first equality
8284 * that only involves parameters. If there is no such equality then
8285 * return bset->n_eq.
8287 * This function assumes that isl_basic_set_gauss has been called on "bset".
8289 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
8292 isl_size nparam
, n_div
;
8294 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8295 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
8296 if (nparam
< 0 || n_div
< 0)
8299 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
8300 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
8307 /* Compute an explicit representation for the existentially quantified
8308 * variables in "bset" by computing the "minimal value" of the set
8309 * variables. Since there are no set variables, the computation of
8310 * the minimal value essentially computes an explicit representation
8311 * of the non-empty part(s) of "bset".
8313 * The input only involves parameters and existentially quantified variables.
8314 * All equalities among parameters have been removed.
8316 * Since the existentially quantified variables in the result are in general
8317 * going to be different from those in the input, we first replace
8318 * them by the minimal number of variables based on their equalities.
8319 * This should simplify the parametric integer programming.
8321 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
8323 isl_morph
*morph1
, *morph2
;
8329 if (bset
->n_eq
== 0)
8330 return isl_basic_set_lexmin_compute_divs(bset
);
8332 morph1
= isl_basic_set_parameter_compression(bset
);
8333 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
8334 bset
= isl_basic_set_lift(bset
);
8335 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
8336 bset
= isl_morph_basic_set(morph2
, bset
);
8337 n
= isl_basic_set_dim(bset
, isl_dim_set
);
8339 bset
= isl_basic_set_free(bset
);
8340 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
8342 set
= isl_basic_set_lexmin_compute_divs(bset
);
8344 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
8349 /* Project the given basic set onto its parameter domain, possibly introducing
8350 * new, explicit, existential variables in the constraints.
8351 * The input has parameters and (possibly implicit) existential variables.
8352 * The output has the same parameters, but only
8353 * explicit existentially quantified variables.
8355 * The actual projection is performed by pip, but pip doesn't seem
8356 * to like equalities very much, so we first remove the equalities
8357 * among the parameters by performing a variable compression on
8358 * the parameters. Afterward, an inverse transformation is performed
8359 * and the equalities among the parameters are inserted back in.
8361 * The variable compression on the parameters may uncover additional
8362 * equalities that were only implicit before. We therefore check
8363 * if there are any new parameter equalities in the result and
8364 * if so recurse. The removal of parameter equalities is required
8365 * for the parameter compression performed by base_compute_divs.
8367 static __isl_give isl_set
*parameter_compute_divs(
8368 __isl_take isl_basic_set
*bset
)
8372 struct isl_mat
*T
, *T2
;
8373 struct isl_set
*set
;
8376 bset
= isl_basic_set_cow(bset
);
8380 if (bset
->n_eq
== 0)
8381 return base_compute_divs(bset
);
8383 bset
= isl_basic_set_gauss(bset
, NULL
);
8386 if (isl_basic_set_plain_is_empty(bset
))
8387 return isl_set_from_basic_set(bset
);
8389 i
= first_parameter_equality(bset
);
8390 if (i
== bset
->n_eq
)
8391 return base_compute_divs(bset
);
8393 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8395 return isl_set_from_basic_set(isl_basic_set_free(bset
));
8396 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
8398 eq
= isl_mat_cow(eq
);
8399 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
8400 if (T
&& T
->n_col
== 0) {
8404 bset
= isl_basic_set_set_to_empty(bset
);
8405 return isl_set_from_basic_set(bset
);
8407 bset
= basic_set_parameter_preimage(bset
, T
);
8409 i
= first_parameter_equality(bset
);
8412 else if (i
== bset
->n_eq
)
8413 set
= base_compute_divs(bset
);
8415 set
= parameter_compute_divs(bset
);
8416 set
= set_parameter_preimage(set
, T2
);
8417 set
= set_append_equalities(set
, eq
);
8421 /* Insert the divs from "ls" before those of "bmap".
8423 * The number of columns is not changed, which means that the last
8424 * dimensions of "bmap" are being reintepreted as the divs from "ls".
8425 * The caller is responsible for removing the same number of dimensions
8426 * from the space of "bmap".
8428 static __isl_give isl_basic_map
*insert_divs_from_local_space(
8429 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
8435 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8437 return isl_basic_map_free(bmap
);
8441 old_n_div
= bmap
->n_div
;
8442 bmap
= insert_div_rows(bmap
, n_div
);
8446 for (i
= 0; i
< n_div
; ++i
) {
8447 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
8448 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
8454 /* Replace the space of "bmap" by the space and divs of "ls".
8456 * If "ls" has any divs, then we simplify the result since we may
8457 * have discovered some additional equalities that could simplify
8458 * the div expressions.
8460 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
8461 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
8465 bmap
= isl_basic_map_cow(bmap
);
8466 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8467 if (!bmap
|| n_div
< 0)
8470 bmap
= insert_divs_from_local_space(bmap
, ls
);
8474 isl_space_free(bmap
->dim
);
8475 bmap
->dim
= isl_local_space_get_space(ls
);
8479 isl_local_space_free(ls
);
8481 bmap
= isl_basic_map_simplify(bmap
);
8482 bmap
= isl_basic_map_finalize(bmap
);
8485 isl_basic_map_free(bmap
);
8486 isl_local_space_free(ls
);
8490 /* Replace the space of "map" by the space and divs of "ls".
8492 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
8493 __isl_take isl_local_space
*ls
)
8497 map
= isl_map_cow(map
);
8501 for (i
= 0; i
< map
->n
; ++i
) {
8502 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
8503 isl_local_space_copy(ls
));
8507 isl_space_free(isl_map_take_space(map
));
8508 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
8510 isl_local_space_free(ls
);
8513 isl_local_space_free(ls
);
8518 /* Compute an explicit representation for the existentially
8519 * quantified variables for which do not know any explicit representation yet.
8521 * We first sort the existentially quantified variables so that the
8522 * existentially quantified variables for which we already have an explicit
8523 * representation are placed before those for which we do not.
8524 * The input dimensions, the output dimensions and the existentially
8525 * quantified variables for which we already have an explicit
8526 * representation are then turned into parameters.
8527 * compute_divs returns a map with the same parameters and
8528 * no input or output dimensions and the dimension specification
8529 * is reset to that of the input, including the existentially quantified
8530 * variables for which we already had an explicit representation.
8532 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
8534 struct isl_basic_set
*bset
;
8535 struct isl_set
*set
;
8536 struct isl_map
*map
;
8538 isl_local_space
*ls
;
8545 bmap
= isl_basic_map_sort_divs(bmap
);
8546 bmap
= isl_basic_map_cow(bmap
);
8550 n_known
= isl_basic_map_first_unknown_div(bmap
);
8551 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8552 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8553 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8554 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8555 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8557 space
= isl_space_set_alloc(bmap
->ctx
,
8558 nparam
+ n_in
+ n_out
+ n_known
, 0);
8562 ls
= isl_basic_map_get_local_space(bmap
);
8563 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8564 n_known
, bmap
->n_div
- n_known
);
8566 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8567 swap_div(bmap
, i
- n_known
, i
);
8568 bmap
->n_div
-= n_known
;
8569 bmap
->extra
-= n_known
;
8571 bmap
= isl_basic_map_reset_space(bmap
, space
);
8572 bset
= bset_from_bmap(bmap
);
8574 set
= parameter_compute_divs(bset
);
8575 map
= set_to_map(set
);
8576 map
= replace_space_by_local_space(map
, ls
);
8580 isl_basic_map_free(bmap
);
8584 /* Is the integer division at position "div" of "bmap" integral?
8585 * That is, does it have denominator 1?
8587 isl_bool
isl_basic_map_div_is_integral(__isl_keep isl_basic_map
*bmap
, int div
)
8589 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8590 return isl_bool_error
;
8591 return isl_int_is_one(bmap
->div
[div
][0]);
8594 /* Remove the explicit representation of local variable "div",
8597 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8598 __isl_take isl_basic_map
*bmap
, int div
)
8602 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8604 return isl_basic_map_free(bmap
);
8608 bmap
= isl_basic_map_cow(bmap
);
8611 isl_int_set_si(bmap
->div
[div
][0], 0);
8615 /* Is local variable "div" of "bmap" marked as not having an explicit
8617 * Note that even if "div" is not marked in this way and therefore
8618 * has an explicit representation, this representation may still
8619 * depend (indirectly) on other local variables that do not
8620 * have an explicit representation.
8622 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8625 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8626 return isl_bool_error
;
8627 return isl_int_is_zero(bmap
->div
[div
][0]);
8630 /* Return the position of the first local variable that does not
8631 * have an explicit representation.
8632 * Return the total number of local variables if they all have
8633 * an explicit representation.
8634 * Return -1 on error.
8636 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8643 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8644 if (!isl_basic_map_div_is_known(bmap
, i
))
8650 /* Return the position of the first local variable that does not
8651 * have an explicit representation.
8652 * Return the total number of local variables if they all have
8653 * an explicit representation.
8654 * Return -1 on error.
8656 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8658 return isl_basic_map_first_unknown_div(bset
);
8661 /* Does "bmap" have an explicit representation for all local variables?
8663 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8668 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8669 first
= isl_basic_map_first_unknown_div(bmap
);
8670 if (n
< 0 || first
< 0)
8671 return isl_bool_error
;
8675 /* Do all basic maps in "map" have an explicit representation
8676 * for all local variables?
8678 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8683 return isl_bool_error
;
8685 for (i
= 0; i
< map
->n
; ++i
) {
8686 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8691 return isl_bool_true
;
8694 /* If bmap contains any unknown divs, then compute explicit
8695 * expressions for them. However, this computation may be
8696 * quite expensive, so first try to remove divs that aren't
8699 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8702 struct isl_map
*map
;
8704 known
= isl_basic_map_divs_known(bmap
);
8708 return isl_map_from_basic_map(bmap
);
8710 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8712 known
= isl_basic_map_divs_known(bmap
);
8716 return isl_map_from_basic_map(bmap
);
8718 map
= compute_divs(bmap
);
8721 isl_basic_map_free(bmap
);
8725 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8729 struct isl_map
*res
;
8736 known
= isl_map_divs_known(map
);
8744 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8745 for (i
= 1 ; i
< map
->n
; ++i
) {
8747 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8748 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8749 res
= isl_map_union_disjoint(res
, r2
);
8751 res
= isl_map_union(res
, r2
);
8758 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8760 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8763 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8765 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8768 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8773 n_out
= isl_map_dim(map
, isl_dim_out
);
8775 return set_from_map(isl_map_free(map
));
8776 space
= isl_space_domain(isl_map_get_space(map
));
8778 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8780 return set_from_map(isl_map_reset_space(map
, space
));
8783 /* Return the union of "map1" and "map2", where we assume for now that
8784 * "map1" and "map2" are disjoint. Note that the basic maps inside
8785 * "map1" or "map2" may not be disjoint from each other.
8786 * Also note that this function is also called from isl_map_union,
8787 * which takes care of handling the situation where "map1" and "map2"
8788 * may not be disjoint.
8790 * If one of the inputs is empty, we can simply return the other input.
8791 * Similarly, if one of the inputs is universal, then it is equal to the union.
8793 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8794 __isl_take isl_map
*map2
)
8798 struct isl_map
*map
= NULL
;
8801 if (isl_map_check_equal_space(map1
, map2
) < 0)
8813 is_universe
= isl_map_plain_is_universe(map1
);
8814 if (is_universe
< 0)
8821 is_universe
= isl_map_plain_is_universe(map2
);
8822 if (is_universe
< 0)
8829 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8830 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8831 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8833 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8834 map1
->n
+ map2
->n
, flags
);
8837 for (i
= 0; i
< map1
->n
; ++i
) {
8838 map
= isl_map_add_basic_map(map
,
8839 isl_basic_map_copy(map1
->p
[i
]));
8843 for (i
= 0; i
< map2
->n
; ++i
) {
8844 map
= isl_map_add_basic_map(map
,
8845 isl_basic_map_copy(map2
->p
[i
]));
8859 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8860 * guaranteed to be disjoint by the caller.
8862 * Note that this functions is called from within isl_map_make_disjoint,
8863 * so we have to be careful not to touch the constraints of the inputs
8866 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8867 __isl_take isl_map
*map2
)
8869 isl_map_align_params_bin(&map1
, &map2
);
8870 return map_union_disjoint(map1
, map2
);
8873 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8876 * We currently simply call map_union_disjoint, the internal operation
8877 * of which does not really depend on the inputs being disjoint.
8878 * If the result contains more than one basic map, then we clear
8879 * the disjoint flag since the result may contain basic maps from
8880 * both inputs and these are not guaranteed to be disjoint.
8882 * As a special case, if "map1" and "map2" are obviously equal,
8883 * then we simply return "map1".
8885 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8886 __isl_take isl_map
*map2
)
8890 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8893 equal
= isl_map_plain_is_equal(map1
, map2
);
8901 map1
= map_union_disjoint(map1
, map2
);
8905 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8913 __isl_give isl_set
*isl_set_union_disjoint(
8914 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8916 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8920 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8921 __isl_take isl_set
*set2
)
8923 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8926 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8927 * the results in a map living in "space".
8929 * "map" and "set" are assumed to be compatible and non-NULL.
8931 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8932 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8933 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8934 __isl_take isl_basic_set
*bset
))
8937 struct isl_map
*result
;
8940 if (isl_set_plain_is_universe(set
)) {
8942 return isl_map_reset_equal_dim_space(map
, space
);
8945 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8946 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8947 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8949 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8950 for (i
= 0; result
&& i
< map
->n
; ++i
)
8951 for (j
= 0; j
< set
->n
; ++j
) {
8952 result
= isl_map_add_basic_map(result
,
8953 fn(isl_basic_map_copy(map
->p
[i
]),
8954 isl_basic_set_copy(set
->p
[j
])));
8964 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8965 __isl_take isl_set
*set
)
8970 isl_map_align_params_set(&map
, &set
);
8971 ok
= isl_map_compatible_range(map
, set
);
8975 isl_die(set
->ctx
, isl_error_invalid
,
8976 "incompatible spaces", goto error
);
8978 space
= isl_map_get_space(map
);
8979 return map_intersect_set(map
, space
, set
,
8980 &isl_basic_map_intersect_range
);
8987 /* Intersect the domain of "map" with "set".
8989 * If the domain dimensions of "map" do not have any identifiers,
8990 * then copy them over from "set".
8992 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8993 __isl_take isl_set
*set
)
8998 isl_map_align_params_set(&map
, &set
);
8999 ok
= isl_map_compatible_domain(map
, set
);
9003 isl_die(set
->ctx
, isl_error_invalid
,
9004 "incompatible spaces", goto error
);
9006 space
= isl_map_get_space(map
);
9007 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
9008 isl_set_peek_space(set
), isl_dim_set
);
9009 return map_intersect_set(map
, space
, set
,
9010 &isl_basic_map_intersect_domain
);
9020 #include "isl_copy_tuple_id_templ.c"
9022 /* Data structure that specifies how isl_map_intersect_factor
9025 * "preserve_type" is the tuple where the factor differs from
9026 * the input map and of which the identifiers needs
9027 * to be preserved explicitly.
9028 * "other_factor" is used to extract the space of the other factor
9029 * from the space of the product ("map").
9030 * "product" is used to combine the given factor and a universe map
9031 * in the space returned by "other_factor" to produce a map
9032 * that lives in the same space as the input map.
9034 struct isl_intersect_factor_control
{
9035 enum isl_dim_type preserve_type
;
9036 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
9037 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
9038 __isl_take isl_map
*other
);
9041 /* Given a map "map" in some product space and a map "factor"
9042 * living in some factor space, return the intersection.
9044 * After aligning the parameters,
9045 * the map "factor" is first extended to a map living in the same space
9046 * as "map" and then a regular intersection is computed.
9048 * Note that the extension is computed as a product, which is anonymous
9049 * by default. If "map" has an identifier on the corresponding tuple,
9050 * then this identifier needs to be set on the product
9051 * before the intersection is computed.
9053 static __isl_give isl_map
*isl_map_intersect_factor(
9054 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
9055 struct isl_intersect_factor_control
*control
)
9059 isl_map
*other
, *product
;
9061 equal
= isl_map_has_equal_params(map
, factor
);
9065 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
9066 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
9069 space
= isl_map_get_space(map
);
9070 other
= isl_map_universe(control
->other_factor(space
));
9071 product
= control
->product(factor
, other
);
9073 space
= isl_map_peek_space(map
);
9074 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
9075 space
, control
->preserve_type
);
9076 return map_intersect(map
, product
);
9079 isl_map_free(factor
);
9083 /* Return the domain product of "map2" and "map1".
9085 static __isl_give isl_map
*isl_map_reverse_domain_product(
9086 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
9088 return isl_map_domain_product(map2
, map1
);
9091 /* Return the range product of "map2" and "map1".
9093 static __isl_give isl_map
*isl_map_reverse_range_product(
9094 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
9096 return isl_map_range_product(map2
, map1
);
9099 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9100 * in the space A -> C, return the intersection.
9102 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
9103 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9105 struct isl_intersect_factor_control control
= {
9106 .preserve_type
= isl_dim_in
,
9107 .other_factor
= isl_space_domain_factor_range
,
9108 .product
= isl_map_domain_product
,
9111 return isl_map_intersect_factor(map
, factor
, &control
);
9114 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9115 * in the space B -> C, return the intersection.
9117 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
9118 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9120 struct isl_intersect_factor_control control
= {
9121 .preserve_type
= isl_dim_in
,
9122 .other_factor
= isl_space_domain_factor_domain
,
9123 .product
= isl_map_reverse_domain_product
,
9126 return isl_map_intersect_factor(map
, factor
, &control
);
9129 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9130 * in the space A -> B, return the intersection.
9132 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
9133 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9135 struct isl_intersect_factor_control control
= {
9136 .preserve_type
= isl_dim_out
,
9137 .other_factor
= isl_space_range_factor_range
,
9138 .product
= isl_map_range_product
,
9141 return isl_map_intersect_factor(map
, factor
, &control
);
9144 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9145 * in the space A -> C, return the intersection.
9147 __isl_give isl_map
*isl_map_intersect_range_factor_range(
9148 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9150 struct isl_intersect_factor_control control
= {
9151 .preserve_type
= isl_dim_out
,
9152 .other_factor
= isl_space_range_factor_domain
,
9153 .product
= isl_map_reverse_range_product
,
9156 return isl_map_intersect_factor(map
, factor
, &control
);
9159 /* Given a set "set" in a space [A -> B] and a set "domain"
9160 * in the space A, return the intersection.
9162 * The set "domain" is first extended to a set living in the space
9163 * [A -> B] and then a regular intersection is computed.
9165 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
9166 __isl_take isl_set
*domain
)
9168 struct isl_intersect_factor_control control
= {
9169 .preserve_type
= isl_dim_set
,
9170 .other_factor
= isl_space_factor_range
,
9171 .product
= isl_map_range_product
,
9174 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
9175 set_to_map(domain
), &control
));
9178 /* Given a set "set" in a space [A -> B] and a set "range"
9179 * in the space B, return the intersection.
9181 * The set "range" is first extended to a set living in the space
9182 * [A -> B] and then a regular intersection is computed.
9184 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
9185 __isl_take isl_set
*range
)
9187 struct isl_intersect_factor_control control
= {
9188 .preserve_type
= isl_dim_set
,
9189 .other_factor
= isl_space_factor_domain
,
9190 .product
= isl_map_reverse_range_product
,
9193 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
9194 set_to_map(range
), &control
));
9200 #include "isl_copy_tuple_id_templ.c"
9202 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
9203 * in the space A, return the intersection.
9205 * The set "domain" is extended to a set living in the space [A -> B] and
9206 * the domain of "map" is intersected with this set.
9208 * If "map" has an identifier on the domain tuple,
9209 * then this identifier needs to be set on this product
9210 * before the intersection is computed.
9212 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
9213 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
9218 isl_map_align_params_set(&map
, &domain
);
9219 space
= isl_map_get_space(map
);
9220 space
= isl_space_domain_wrapped_range(space
);
9221 factor
= isl_set_universe(space
);
9222 domain
= isl_set_product(domain
, factor
);
9223 space
= isl_map_peek_space(map
);
9224 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
9225 return isl_map_intersect_domain(map
, domain
);
9228 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
9229 * in the space B, return the intersection.
9231 * The set "domain" is extended to a set living in the space [B -> C] and
9232 * the range of "map" is intersected with this set.
9234 * If "map" has an identifier on the range tuple,
9235 * then this identifier needs to be set on this product
9236 * before the intersection is computed.
9238 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
9239 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
9244 isl_map_align_params_set(&map
, &domain
);
9245 space
= isl_map_get_space(map
);
9246 space
= isl_space_range_wrapped_range(space
);
9247 factor
= isl_set_universe(space
);
9248 domain
= isl_set_product(domain
, factor
);
9249 space
= isl_map_peek_space(map
);
9250 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
9251 return isl_map_intersect_range(map
, domain
);
9254 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
9255 __isl_take isl_map
*map2
)
9257 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
9259 map1
= isl_map_reverse(map1
);
9260 map1
= isl_map_apply_range(map1
, map2
);
9261 return isl_map_reverse(map1
);
9268 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
9269 __isl_take isl_map
*map2
)
9272 struct isl_map
*result
;
9275 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
9278 space
= isl_space_join(isl_space_copy(map1
->dim
),
9279 isl_space_copy(map2
->dim
));
9281 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
9284 for (i
= 0; i
< map1
->n
; ++i
)
9285 for (j
= 0; j
< map2
->n
; ++j
) {
9286 result
= isl_map_add_basic_map(result
,
9287 isl_basic_map_apply_range(
9288 isl_basic_map_copy(map1
->p
[i
]),
9289 isl_basic_map_copy(map2
->p
[j
])));
9295 if (result
&& result
->n
<= 1)
9296 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
9304 /* Is "bmap" a transformation, i.e.,
9305 * does it relate elements from the same space.
9307 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
9311 space
= isl_basic_map_peek_space(bmap
);
9312 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
9315 /* Check that "bmap" is a transformation, i.e.,
9316 * that it relates elements from the same space.
9318 static isl_stat
isl_basic_map_check_transformation(
9319 __isl_keep isl_basic_map
*bmap
)
9323 equal
= isl_basic_map_is_transformation(bmap
);
9325 return isl_stat_error
;
9327 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9328 "domain and range don't match", return isl_stat_error
);
9333 * returns range - domain
9335 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
9337 isl_space
*target_space
;
9338 struct isl_basic_set
*bset
;
9344 if (isl_basic_map_check_transformation(bmap
) < 0)
9345 return isl_basic_map_free(bmap
);
9346 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
9347 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
9348 if (dim
< 0 || nparam
< 0)
9350 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
9351 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
9352 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
9353 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9355 bmap
= isl_basic_map_free(bmap
);
9356 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
9357 for (i
= 0; i
< dim
; ++i
) {
9358 int j
= isl_basic_map_alloc_equality(bmap
);
9360 bmap
= isl_basic_map_free(bmap
);
9363 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
9364 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
9365 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
9366 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
9368 bset
= isl_basic_map_domain(bmap
);
9369 bset
= isl_basic_set_reset_space(bset
, target_space
);
9372 isl_basic_map_free(bmap
);
9376 /* Is the tuple of type "type1" of "map" the same as
9377 * the tuple of type "type2" of "space"?
9379 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
9380 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
9381 enum isl_dim_type type2
)
9383 isl_space
*map_space
;
9385 map_space
= isl_map_peek_space(map
);
9386 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
9389 /* Is the tuple of type "type1" of "map1" the same as
9390 * the tuple of type "type2" of "map2"?
9392 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
9393 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
9394 enum isl_dim_type type2
)
9396 isl_space
*space1
, *space2
;
9398 space1
= isl_map_peek_space(map1
);
9399 space2
= isl_map_peek_space(map2
);
9400 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
9403 /* Is the space of "obj" equal to "space", ignoring parameters?
9405 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
9406 __isl_keep isl_space
*space
)
9408 isl_space
*map_space
;
9410 map_space
= isl_map_peek_space(map
);
9411 return isl_space_has_equal_tuples(map_space
, space
);
9414 /* Check that "map" is a transformation, i.e.,
9415 * that it relates elements from the same space.
9417 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
9421 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
9423 return isl_stat_error
;
9425 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9426 "domain and range don't match", return isl_stat_error
);
9431 * returns range - domain
9433 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
9437 struct isl_set
*result
;
9439 if (isl_map_check_transformation(map
) < 0)
9441 space
= isl_map_get_space(map
);
9442 space
= isl_space_domain(space
);
9443 result
= isl_set_alloc_space(space
, map
->n
, 0);
9446 for (i
= 0; i
< map
->n
; ++i
)
9447 result
= isl_set_add_basic_set(result
,
9448 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
9457 * returns [domain -> range] -> range - domain
9459 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
9460 __isl_take isl_basic_map
*bmap
)
9464 isl_basic_map
*domain
;
9468 if (isl_basic_map_check_transformation(bmap
) < 0)
9469 return isl_basic_map_free(bmap
);
9471 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
9472 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
9473 if (nparam
< 0 || n
< 0)
9474 return isl_basic_map_free(bmap
);
9476 space
= isl_basic_map_get_space(bmap
);
9477 space
= isl_space_from_range(isl_space_domain(space
));
9478 domain
= isl_basic_map_universe(space
);
9480 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
9481 bmap
= isl_basic_map_apply_range(bmap
, domain
);
9482 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
9484 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9486 return isl_basic_map_free(bmap
);
9488 for (i
= 0; i
< n
; ++i
) {
9489 k
= isl_basic_map_alloc_equality(bmap
);
9492 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
9493 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
9494 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
9495 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
9498 bmap
= isl_basic_map_gauss(bmap
, NULL
);
9499 return isl_basic_map_finalize(bmap
);
9501 isl_basic_map_free(bmap
);
9506 * returns [domain -> range] -> range - domain
9508 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
9510 if (isl_map_check_transformation(map
) < 0)
9511 return isl_map_free(map
);
9513 return isl_map_transform(map
, &isl_space_range_map
,
9514 &isl_basic_map_deltas_map
);
9517 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9519 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
9524 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
9525 map
= isl_map_deltas_map(isl_map_universe(space
));
9526 map
= isl_map_intersect_range(map
, deltas
);
9528 return isl_set_unwrap(isl_map_domain(map
));
9531 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
9533 isl_size n_in
, n_out
;
9535 n_in
= isl_space_dim(space
, isl_dim_in
);
9536 n_out
= isl_space_dim(space
, isl_dim_out
);
9537 if (n_in
< 0 || n_out
< 0)
9540 isl_die(space
->ctx
, isl_error_invalid
,
9541 "number of input and output dimensions needs to be "
9542 "the same", goto error
);
9543 return isl_basic_map_equal(space
, n_in
);
9545 isl_space_free(space
);
9549 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
9551 return isl_map_from_basic_map(isl_basic_map_identity(space
));
9554 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
9556 isl_space
*space
= isl_set_get_space(set
);
9558 id
= isl_map_identity(isl_space_map_from_set(space
));
9559 return isl_map_intersect_range(id
, set
);
9562 /* Construct a basic set with all set dimensions having only non-negative
9565 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9566 __isl_take isl_space
*space
)
9572 struct isl_basic_set
*bset
;
9574 nparam
= isl_space_dim(space
, isl_dim_param
);
9575 dim
= isl_space_dim(space
, isl_dim_set
);
9576 total
= isl_space_dim(space
, isl_dim_all
);
9577 if (nparam
< 0 || dim
< 0 || total
< 0)
9578 space
= isl_space_free(space
);
9579 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9582 for (i
= 0; i
< dim
; ++i
) {
9583 int k
= isl_basic_set_alloc_inequality(bset
);
9586 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9587 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9591 isl_basic_set_free(bset
);
9595 /* Construct the half-space x_pos >= 0.
9597 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9602 isl_basic_set
*nonneg
;
9604 total
= isl_space_dim(space
, isl_dim_all
);
9606 space
= isl_space_free(space
);
9607 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9608 k
= isl_basic_set_alloc_inequality(nonneg
);
9611 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9612 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9614 return isl_basic_set_finalize(nonneg
);
9616 isl_basic_set_free(nonneg
);
9620 /* Construct the half-space x_pos <= -1.
9622 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9629 total
= isl_space_dim(space
, isl_dim_all
);
9631 space
= isl_space_free(space
);
9632 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9633 k
= isl_basic_set_alloc_inequality(neg
);
9636 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9637 isl_int_set_si(neg
->ineq
[k
][0], -1);
9638 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9640 return isl_basic_set_finalize(neg
);
9642 isl_basic_set_free(neg
);
9646 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9647 enum isl_dim_type type
, unsigned first
, unsigned n
)
9651 isl_basic_set
*nonneg
;
9657 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9658 return isl_set_free(set
);
9660 offset
= pos(set
->dim
, type
);
9661 for (i
= 0; i
< n
; ++i
) {
9662 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9663 offset
+ first
+ i
);
9664 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9666 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9672 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9674 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9680 return isl_stat_error
;
9681 if (isl_set_plain_is_empty(set
)) {
9686 return fn(set
, signs
, user
);
9689 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9691 half
= isl_set_intersect(half
, isl_set_copy(set
));
9692 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9696 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9698 half
= isl_set_intersect(half
, set
);
9699 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9702 return isl_stat_error
;
9705 /* Call "fn" on the intersections of "set" with each of the orthants
9706 * (except for obviously empty intersections). The orthant is identified
9707 * by the signs array, with each entry having value 1 or -1 according
9708 * to the sign of the corresponding variable.
9710 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9711 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9720 return isl_stat_error
;
9721 if (isl_set_plain_is_empty(set
))
9724 nparam
= isl_set_dim(set
, isl_dim_param
);
9725 nvar
= isl_set_dim(set
, isl_dim_set
);
9726 if (nparam
< 0 || nvar
< 0)
9727 return isl_stat_error
;
9729 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9731 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9739 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9741 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9744 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9745 __isl_keep isl_basic_map
*bmap2
)
9748 struct isl_map
*map1
;
9749 struct isl_map
*map2
;
9751 if (!bmap1
|| !bmap2
)
9752 return isl_bool_error
;
9754 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9755 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9757 is_subset
= isl_map_is_subset(map1
, map2
);
9765 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9766 __isl_keep isl_basic_set
*bset2
)
9768 return isl_basic_map_is_subset(bset1
, bset2
);
9771 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9772 __isl_keep isl_basic_map
*bmap2
)
9776 if (!bmap1
|| !bmap2
)
9777 return isl_bool_error
;
9778 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9779 if (is_subset
!= isl_bool_true
)
9781 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9785 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9786 __isl_keep isl_basic_set
*bset2
)
9788 return isl_basic_map_is_equal(
9789 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9792 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9798 return isl_bool_error
;
9799 for (i
= 0; i
< map
->n
; ++i
) {
9800 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9802 return isl_bool_error
;
9804 return isl_bool_false
;
9806 return isl_bool_true
;
9809 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9811 return map
? map
->n
== 0 : isl_bool_error
;
9814 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9816 return set
? set
->n
== 0 : isl_bool_error
;
9819 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9821 return isl_map_is_empty(set_to_map(set
));
9825 #define TYPE isl_basic_map
9828 #include "isl_type_has_equal_space_bin_templ.c"
9829 #include "isl_type_check_equal_space_templ.c"
9831 /* Check that "bset1" and "bset2" live in the same space,
9832 * reporting an error if they do not.
9834 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9835 __isl_keep isl_basic_set
*bset2
)
9837 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9838 bset_to_bmap(bset1
));
9842 #define TYPE isl_map
9844 #include "isl_type_has_equal_space_bin_templ.c"
9845 #include "isl_type_check_equal_space_templ.c"
9846 #include "isl_type_has_space_templ.c"
9848 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9849 __isl_keep isl_set
*set2
)
9851 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9855 #define TYPE1 isl_map
9857 #define TYPE2 isl_basic_map
9859 #define TYPE_PAIR isl_map_basic_map
9862 #include "isl_type_has_equal_space_templ.c"
9863 #include "isl_type_check_equal_space_templ.c"
9865 /* Check that "set" and "bset" live in the same space,
9866 * reporting an error if they do not.
9868 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9869 __isl_keep isl_basic_set
*bset
)
9871 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9872 bset_to_bmap(bset
));
9875 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9880 return isl_bool_error
;
9881 is_subset
= isl_map_is_subset(map1
, map2
);
9882 if (is_subset
!= isl_bool_true
)
9884 is_subset
= isl_map_is_subset(map2
, map1
);
9888 /* Is "map1" equal to "map2"?
9890 * First check if they are obviously equal.
9891 * If not, then perform a more detailed analysis.
9893 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9897 equal
= isl_map_plain_is_equal(map1
, map2
);
9898 if (equal
< 0 || equal
)
9900 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9903 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9904 __isl_keep isl_basic_map
*bmap2
)
9908 if (!bmap1
|| !bmap2
)
9909 return isl_bool_error
;
9910 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9911 if (is_subset
!= isl_bool_true
)
9913 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9914 return isl_bool_not(is_subset
);
9917 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9918 __isl_keep isl_map
*map2
)
9923 return isl_bool_error
;
9924 is_subset
= isl_map_is_subset(map1
, map2
);
9925 if (is_subset
!= isl_bool_true
)
9927 is_subset
= isl_map_is_subset(map2
, map1
);
9928 return isl_bool_not(is_subset
);
9931 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9932 __isl_keep isl_set
*set2
)
9934 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9937 /* Is "bmap" obviously equal to the universe with the same space?
9939 * That is, does it not have any constraints?
9941 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9944 return isl_bool_error
;
9945 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9948 /* Is "bset" obviously equal to the universe with the same space?
9950 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9952 return isl_basic_map_plain_is_universe(bset
);
9955 /* If "c" does not involve any existentially quantified variables,
9956 * then set *univ to false and abort
9958 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9960 isl_bool
*univ
= user
;
9963 n
= isl_constraint_dim(c
, isl_dim_div
);
9965 c
= isl_constraint_free(c
);
9966 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9967 isl_constraint_free(c
);
9968 if (*univ
< 0 || !*univ
)
9969 return isl_stat_error
;
9973 /* Is "bmap" equal to the universe with the same space?
9975 * First check if it is obviously equal to the universe.
9976 * If not and if there are any constraints not involving
9977 * existentially quantified variables, then it is certainly
9978 * not equal to the universe.
9979 * Otherwise, check if the universe is a subset of "bmap".
9981 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9985 isl_basic_map
*test
;
9987 univ
= isl_basic_map_plain_is_universe(bmap
);
9988 if (univ
< 0 || univ
)
9990 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9992 return isl_bool_error
;
9994 return isl_bool_false
;
9995 univ
= isl_bool_true
;
9996 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9998 return isl_bool_error
;
9999 if (univ
< 0 || !univ
)
10001 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
10002 univ
= isl_basic_map_is_subset(test
, bmap
);
10003 isl_basic_map_free(test
);
10007 /* Is "bset" equal to the universe with the same space?
10009 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
10011 return isl_basic_map_is_universe(bset
);
10014 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
10019 return isl_bool_error
;
10021 for (i
= 0; i
< map
->n
; ++i
) {
10022 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
10027 return isl_bool_false
;
10030 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
10032 return isl_map_plain_is_universe(set_to_map(set
));
10035 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
10037 struct isl_basic_set
*bset
= NULL
;
10038 struct isl_vec
*sample
= NULL
;
10039 isl_bool empty
, non_empty
;
10042 return isl_bool_error
;
10044 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
10045 return isl_bool_true
;
10047 if (isl_basic_map_plain_is_universe(bmap
))
10048 return isl_bool_false
;
10050 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
10051 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
10052 copy
= isl_basic_map_remove_redundancies(copy
);
10053 empty
= isl_basic_map_plain_is_empty(copy
);
10054 isl_basic_map_free(copy
);
10058 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
10060 return isl_bool_error
;
10062 return isl_bool_false
;
10063 isl_vec_free(bmap
->sample
);
10064 bmap
->sample
= NULL
;
10065 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
10067 return isl_bool_error
;
10068 sample
= isl_basic_set_sample_vec(bset
);
10070 return isl_bool_error
;
10071 empty
= sample
->size
== 0;
10072 isl_vec_free(bmap
->sample
);
10073 bmap
->sample
= sample
;
10075 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
10080 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
10083 return isl_bool_error
;
10084 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
10087 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
10090 return isl_bool_error
;
10091 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
10094 /* Is "bmap" known to be non-empty?
10096 * That is, is the cached sample still valid?
10098 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
10103 return isl_bool_error
;
10105 return isl_bool_false
;
10106 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10108 return isl_bool_error
;
10109 if (bmap
->sample
->size
!= 1 + total
)
10110 return isl_bool_false
;
10111 return isl_basic_map_contains(bmap
, bmap
->sample
);
10114 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
10116 return isl_basic_map_is_empty(bset_to_bmap(bset
));
10119 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
10120 __isl_take isl_basic_map
*bmap2
)
10122 struct isl_map
*map
;
10124 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
10127 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
10130 map
= isl_map_add_basic_map(map
, bmap1
);
10131 map
= isl_map_add_basic_map(map
, bmap2
);
10134 isl_basic_map_free(bmap1
);
10135 isl_basic_map_free(bmap2
);
10139 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
10140 __isl_take isl_basic_set
*bset2
)
10142 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
10143 bset_to_bmap(bset2
)));
10146 /* Order divs such that any div only depends on previous divs */
10147 __isl_give isl_basic_map
*isl_basic_map_order_divs(
10148 __isl_take isl_basic_map
*bmap
)
10153 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
10155 return isl_basic_map_free(bmap
);
10157 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10159 if (isl_int_is_zero(bmap
->div
[i
][0]))
10161 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
10166 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
10167 "integer division depends on itself",
10168 return isl_basic_map_free(bmap
));
10169 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
10177 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
10184 for (i
= 0; i
< map
->n
; ++i
) {
10185 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
10196 /* Sort the local variables of "bset".
10198 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
10199 __isl_take isl_basic_set
*bset
)
10201 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
10204 /* Apply the expansion computed by isl_merge_divs.
10205 * The expansion itself is given by "exp" while the resulting
10206 * list of divs is given by "div".
10208 * Move the integer divisions of "bmap" into the right position
10209 * according to "exp" and then introduce the additional integer
10210 * divisions, adding div constraints.
10211 * The moving should be done first to avoid moving coefficients
10212 * in the definitions of the extra integer divisions.
10214 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
10215 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
10220 bmap
= isl_basic_map_cow(bmap
);
10224 if (div
->n_row
< bmap
->n_div
)
10225 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
10226 "not an expansion", goto error
);
10228 n_div
= bmap
->n_div
;
10229 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
10230 2 * (div
->n_row
- n_div
));
10232 for (i
= n_div
; i
< div
->n_row
; ++i
)
10233 if (isl_basic_map_alloc_div(bmap
) < 0)
10236 for (j
= n_div
- 1; j
>= 0; --j
) {
10239 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
10244 for (i
= 0; i
< div
->n_row
; ++i
) {
10245 if (j
< n_div
&& exp
[j
] == i
) {
10248 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
10249 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
10251 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
10260 isl_basic_map_free(bmap
);
10265 /* Apply the expansion computed by isl_merge_divs.
10266 * The expansion itself is given by "exp" while the resulting
10267 * list of divs is given by "div".
10269 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
10270 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
10272 return isl_basic_map_expand_divs(bset
, div
, exp
);
10275 /* Look for a div in dst that corresponds to the div "div" in src.
10276 * The divs before "div" in src and dst are assumed to be the same.
10278 * Return the position of the corresponding div in dst
10279 * if there is one. Otherwise, return a position beyond the integer divisions.
10280 * Return isl_size_error on error.
10282 static isl_size
find_div(__isl_keep isl_basic_map
*dst
,
10283 __isl_keep isl_basic_map
*src
, unsigned div
)
10289 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
10290 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
10291 if (n_div
< 0 || v_div
< 0)
10292 return isl_size_error
;
10293 isl_assert(dst
->ctx
, div
<= n_div
, return isl_size_error
);
10294 for (i
= div
; i
< n_div
; ++i
)
10295 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
10296 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
10297 n_div
- div
) == -1)
10302 /* Align the local variables of "dst" to those of "src",
10303 * adding local variables from "src" if needed.
10304 * That is, make sure that the first src->n_div local variables
10305 * of the result correspond to those of src.
10306 * For any integer division that is copied to "dst",
10307 * the defining constraints are also introduced to "dst".
10308 * For an existentially quantified variable (without an explicit definition)
10309 * only an unconstrained existentially quantified variable
10310 * in the same positions is introduced.
10311 * The integer division of "src" are assumed to be ordered.
10313 * The integer divisions are swapped into the right position
10314 * (possibly after adding them first). This may result
10315 * in the remaining integer divisions appearing in the wrong order,
10316 * i.e., with some integer division appearing before
10317 * some other integer division on which it depends.
10318 * The integer divisions therefore need to be ordered.
10319 * This will not affect the integer divisions aligned to those of "src",
10320 * since "src" is assumed to have ordered integer divisions.
10322 * The result is not finalized as by design it will have redundant
10323 * divs if any divs from "src" were copied.
10325 __isl_give isl_basic_map
*isl_basic_map_align_divs(
10326 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
10331 isl_size dst_n_div
, src_n_div
;
10333 src_n_div
= isl_basic_map_dim(src
, isl_dim_div
);
10334 if (!dst
|| src_n_div
< 0)
10335 return isl_basic_map_free(dst
);
10337 if (src_n_div
== 0)
10340 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
10342 return isl_basic_map_free(dst
);
10345 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
10347 dst
= isl_basic_map_free(dst
);
10348 for (i
= 0; i
< src_n_div
; ++i
) {
10352 known
= isl_basic_map_div_is_known(src
, i
);
10354 return isl_basic_map_free(dst
);
10355 j
= known
? find_div(dst
, src
, i
) : dst_n_div
;
10357 dst
= isl_basic_map_free(dst
);
10358 if (j
== dst_n_div
) {
10360 int extra
= src_n_div
- i
;
10361 dst
= isl_basic_map_cow(dst
);
10363 return isl_basic_map_free(dst
);
10364 dst
= isl_basic_map_extend(dst
,
10365 extra
, 0, 2 * extra
);
10368 j
= isl_basic_map_alloc_div(dst
);
10370 return isl_basic_map_free(dst
);
10374 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
10375 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst_n_div
- i
);
10376 dst
= isl_basic_map_add_div_constraints(dst
, j
);
10378 return isl_basic_map_free(dst
);
10381 dst
= isl_basic_map_swap_div(dst
, i
, j
);
10383 return isl_basic_map_free(dst
);
10385 return isl_basic_map_order_divs(dst
);
10388 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
10396 map
= isl_map_compute_divs(map
);
10397 map
= isl_map_order_divs(map
);
10398 map
= isl_map_cow(map
);
10402 for (i
= 1; i
< map
->n
; ++i
)
10403 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
10404 for (i
= 1; i
< map
->n
; ++i
) {
10405 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
10407 return isl_map_free(map
);
10410 map
= isl_map_unmark_normalized(map
);
10414 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
10416 return isl_map_align_divs_internal(map
);
10419 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
10421 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10424 /* Align the divs of the basic maps in "map" to those
10425 * of the basic maps in "list", as well as to the other basic maps in "map".
10426 * The elements in "list" are assumed to have known divs.
10428 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
10429 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
10434 n
= isl_basic_map_list_n_basic_map(list
);
10435 map
= isl_map_compute_divs(map
);
10436 map
= isl_map_cow(map
);
10438 return isl_map_free(map
);
10442 for (i
= 0; i
< n
; ++i
) {
10443 isl_basic_map
*bmap
;
10445 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
10446 bmap
= isl_basic_map_order_divs(bmap
);
10447 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
10448 isl_basic_map_free(bmap
);
10451 return isl_map_free(map
);
10453 return isl_map_align_divs_internal(map
);
10456 /* Align the divs of each element of "list" to those of "bmap".
10457 * Both "bmap" and the elements of "list" are assumed to have known divs.
10459 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
10460 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
10465 n
= isl_basic_map_list_n_basic_map(list
);
10466 if (n
< 0 || !bmap
)
10467 return isl_basic_map_list_free(list
);
10469 for (i
= 0; i
< n
; ++i
) {
10470 isl_basic_map
*bmap_i
;
10472 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10473 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
10474 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
10480 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
10481 __isl_take isl_map
*map
)
10485 isl_map_align_params_set(&map
, &set
);
10486 ok
= isl_map_compatible_domain(map
, set
);
10490 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
10491 "incompatible spaces", goto error
);
10492 map
= isl_map_intersect_domain(map
, set
);
10493 set
= isl_map_range(map
);
10501 /* There is no need to cow as removing empty parts doesn't change
10502 * the meaning of the set.
10504 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10511 for (i
= map
->n
- 1; i
>= 0; --i
)
10512 map
= remove_if_empty(map
, i
);
10517 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10519 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10522 /* Create a binary relation that maps the shared initial "pos" dimensions
10523 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10525 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10526 __isl_keep isl_basic_set
*bset2
, int pos
)
10528 isl_basic_map
*bmap1
;
10529 isl_basic_map
*bmap2
;
10531 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10532 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10533 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10534 isl_dim_out
, 0, pos
);
10535 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10536 isl_dim_out
, 0, pos
);
10537 return isl_basic_map_range_product(bmap1
, bmap2
);
10540 /* Given two basic sets bset1 and bset2, compute the maximal difference
10541 * between the values of dimension pos in bset1 and those in bset2
10542 * for any common value of the parameters and dimensions preceding pos.
10544 static enum isl_lp_result
basic_set_maximal_difference_at(
10545 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10546 int pos
, isl_int
*opt
)
10548 isl_basic_map
*bmap1
;
10549 struct isl_ctx
*ctx
;
10550 struct isl_vec
*obj
;
10554 enum isl_lp_result res
;
10556 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10557 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10558 if (nparam
< 0 || dim1
< 0 || !bset2
)
10559 return isl_lp_error
;
10561 bmap1
= join_initial(bset1
, bset2
, pos
);
10562 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10564 return isl_lp_error
;
10567 obj
= isl_vec_alloc(ctx
, 1 + total
);
10570 isl_seq_clr(obj
->block
.data
, 1 + total
);
10571 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10572 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10573 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10575 isl_basic_map_free(bmap1
);
10579 isl_basic_map_free(bmap1
);
10580 return isl_lp_error
;
10583 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10584 * for any common value of the parameters and dimensions preceding pos
10585 * in both basic sets, the values of dimension pos in bset1 are
10586 * smaller or larger than those in bset2.
10589 * 1 if bset1 follows bset2
10590 * -1 if bset1 precedes bset2
10591 * 0 if bset1 and bset2 are incomparable
10592 * -2 if some error occurred.
10594 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10595 __isl_keep isl_basic_set
*bset2
, int pos
)
10598 enum isl_lp_result res
;
10603 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10605 if (res
== isl_lp_empty
)
10607 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10608 res
== isl_lp_unbounded
)
10610 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10615 isl_int_clear(opt
);
10619 /* Given two basic sets bset1 and bset2, check whether
10620 * for any common value of the parameters and dimensions preceding pos
10621 * there is a value of dimension pos in bset1 that is larger
10622 * than a value of the same dimension in bset2.
10625 * 1 if there exists such a pair
10626 * 0 if there is no such pair, but there is a pair of equal values
10628 * -2 if some error occurred.
10630 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10631 __isl_keep isl_basic_set
*bset2
, int pos
)
10634 isl_basic_map
*bmap
;
10637 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10640 bmap
= join_initial(bset1
, bset2
, pos
);
10641 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10642 isl_dim_out
, dim1
- pos
);
10643 empty
= isl_basic_map_is_empty(bmap
);
10647 isl_basic_map_free(bmap
);
10650 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10651 isl_dim_out
, dim1
- pos
);
10652 empty
= isl_basic_map_is_empty(bmap
);
10655 isl_basic_map_free(bmap
);
10660 isl_basic_map_free(bmap
);
10664 /* Given two sets set1 and set2, check whether
10665 * for any common value of the parameters and dimensions preceding pos
10666 * there is a value of dimension pos in set1 that is larger
10667 * than a value of the same dimension in set2.
10670 * 1 if there exists such a pair
10671 * 0 if there is no such pair, but there is a pair of equal values
10673 * -2 if some error occurred.
10675 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10676 __isl_keep isl_set
*set2
, int pos
)
10681 if (!set1
|| !set2
)
10684 for (i
= 0; i
< set1
->n
; ++i
)
10685 for (j
= 0; j
< set2
->n
; ++j
) {
10687 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10688 if (f
== 1 || f
== -2)
10697 static isl_bool
isl_basic_map_plain_has_fixed_var(
10698 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10704 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10706 return isl_bool_error
;
10707 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10708 for (; d
+1 > pos
; --d
)
10709 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10713 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10714 return isl_bool_false
;
10715 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10716 return isl_bool_false
;
10717 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10718 return isl_bool_false
;
10720 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10721 return isl_bool_true
;
10723 return isl_bool_false
;
10726 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10727 unsigned pos
, isl_int
*val
)
10735 return isl_bool_error
;
10737 return isl_bool_false
;
10739 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10742 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10743 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10744 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10745 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10746 fixed
= isl_bool_false
;
10749 isl_int_set(*val
, v
);
10750 isl_int_clear(tmp
);
10755 static isl_bool
isl_basic_set_plain_has_fixed_var(
10756 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10758 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10762 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10763 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10765 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10766 return isl_bool_error
;
10767 return isl_basic_map_plain_has_fixed_var(bmap
,
10768 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10771 /* If "bmap" obviously lies on a hyperplane where the given dimension
10772 * has a fixed value, then return that value.
10773 * Otherwise return NaN.
10775 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10776 __isl_keep isl_basic_map
*bmap
,
10777 enum isl_dim_type type
, unsigned pos
)
10785 ctx
= isl_basic_map_get_ctx(bmap
);
10786 v
= isl_val_alloc(ctx
);
10789 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10791 return isl_val_free(v
);
10793 isl_int_set_si(v
->d
, 1);
10797 return isl_val_nan(ctx
);
10800 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10801 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10803 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10804 return isl_bool_error
;
10805 return isl_map_plain_has_fixed_var(map
,
10806 map_offset(map
, type
) - 1 + pos
, val
);
10809 /* If "map" obviously lies on a hyperplane where the given dimension
10810 * has a fixed value, then return that value.
10811 * Otherwise return NaN.
10813 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10814 enum isl_dim_type type
, unsigned pos
)
10822 ctx
= isl_map_get_ctx(map
);
10823 v
= isl_val_alloc(ctx
);
10826 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10828 return isl_val_free(v
);
10830 isl_int_set_si(v
->d
, 1);
10834 return isl_val_nan(ctx
);
10837 /* If "set" obviously lies on a hyperplane where the given dimension
10838 * has a fixed value, then return that value.
10839 * Otherwise return NaN.
10841 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10842 enum isl_dim_type type
, unsigned pos
)
10844 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10847 /* Return a sequence of values in the same space as "set"
10848 * that are equal to the corresponding set dimensions of "set"
10849 * for those set dimensions that obviously lie on a hyperplane
10850 * where the dimension has a fixed value.
10851 * The other elements are set to NaN.
10853 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10854 __isl_keep isl_set
*set
)
10861 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10862 mv
= isl_multi_val_alloc(space
);
10863 n
= isl_multi_val_size(mv
);
10865 return isl_multi_val_free(mv
);
10867 for (i
= 0; i
< n
; ++i
) {
10870 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10871 mv
= isl_multi_val_set_val(mv
, i
, v
);
10877 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10878 * then return this fixed value in *val.
10880 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10881 unsigned dim
, isl_int
*val
)
10885 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10887 return isl_bool_error
;
10888 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10891 /* Return -1 if the constraint "c1" should be sorted before "c2"
10892 * and 1 if it should be sorted after "c2".
10893 * Return 0 if the two constraints are the same (up to the constant term).
10895 * In particular, if a constraint involves later variables than another
10896 * then it is sorted after this other constraint.
10897 * uset_gist depends on constraints without existentially quantified
10898 * variables sorting first.
10900 * For constraints that have the same latest variable, those
10901 * with the same coefficient for this latest variable (first in absolute value
10902 * and then in actual value) are grouped together.
10903 * This is useful for detecting pairs of constraints that can
10904 * be chained in their printed representation.
10906 * Finally, within a group, constraints are sorted according to
10907 * their coefficients (excluding the constant term).
10909 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10911 isl_int
**c1
= (isl_int
**) p1
;
10912 isl_int
**c2
= (isl_int
**) p2
;
10914 unsigned size
= *(unsigned *) arg
;
10917 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10918 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10923 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10926 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10930 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10933 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10934 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10935 * and 0 if the two constraints are the same (up to the constant term).
10937 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10938 isl_int
*c1
, isl_int
*c2
)
10943 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10947 return sort_constraint_cmp(&c1
, &c2
, &size
);
10950 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10951 __isl_take isl_basic_map
*bmap
)
10958 if (bmap
->n_ineq
== 0)
10960 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10962 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10964 return isl_basic_map_free(bmap
);
10966 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10967 &sort_constraint_cmp
, &size
) < 0)
10968 return isl_basic_map_free(bmap
);
10969 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10973 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10974 __isl_take isl_basic_set
*bset
)
10976 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10977 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10980 __isl_give isl_basic_map
*isl_basic_map_normalize(
10981 __isl_take isl_basic_map
*bmap
)
10983 bmap
= isl_basic_map_remove_redundancies(bmap
);
10984 bmap
= isl_basic_map_sort_constraints(bmap
);
10987 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10988 __isl_keep isl_basic_map
*bmap2
)
10992 isl_space
*space1
, *space2
;
10994 if (!bmap1
|| !bmap2
)
10997 if (bmap1
== bmap2
)
10999 space1
= isl_basic_map_peek_space(bmap1
);
11000 space2
= isl_basic_map_peek_space(bmap2
);
11001 cmp
= isl_space_cmp(space1
, space2
);
11004 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
11005 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
11006 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
11007 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
11008 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11010 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
11012 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11014 if (bmap1
->n_eq
!= bmap2
->n_eq
)
11015 return bmap1
->n_eq
- bmap2
->n_eq
;
11016 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
11017 return bmap1
->n_ineq
- bmap2
->n_ineq
;
11018 if (bmap1
->n_div
!= bmap2
->n_div
)
11019 return bmap1
->n_div
- bmap2
->n_div
;
11020 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
11023 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
11024 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
11028 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
11029 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
11033 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
11034 isl_bool unknown1
, unknown2
;
11036 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
11037 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
11038 if (unknown1
< 0 || unknown2
< 0)
11040 if (unknown1
&& unknown2
)
11046 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
11053 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
11054 __isl_keep isl_basic_set
*bset2
)
11056 return isl_basic_map_plain_cmp(bset1
, bset2
);
11059 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
11065 if (set1
->n
!= set2
->n
)
11066 return set1
->n
- set2
->n
;
11068 for (i
= 0; i
< set1
->n
; ++i
) {
11069 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
11077 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
11078 __isl_keep isl_basic_map
*bmap2
)
11080 if (!bmap1
|| !bmap2
)
11081 return isl_bool_error
;
11082 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
11085 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
11086 __isl_keep isl_basic_set
*bset2
)
11088 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
11089 bset_to_bmap(bset2
));
11092 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
11094 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
11095 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
11097 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
11100 /* Sort the basic maps of "map" and remove duplicate basic maps.
11102 * While removing basic maps, we make sure that the basic maps remain
11103 * sorted because isl_map_normalize expects the basic maps of the result
11106 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
11110 map
= isl_map_remove_empty_parts(map
);
11113 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
11114 for (i
= map
->n
- 1; i
>= 1; --i
) {
11115 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
11117 isl_basic_map_free(map
->p
[i
-1]);
11118 for (j
= i
; j
< map
->n
; ++j
)
11119 map
->p
[j
- 1] = map
->p
[j
];
11126 /* Remove obvious duplicates among the basic maps of "map".
11128 * Unlike isl_map_normalize, this function does not remove redundant
11129 * constraints and only removes duplicates that have exactly the same
11130 * constraints in the input. It does sort the constraints and
11131 * the basic maps to ease the detection of duplicates.
11133 * If "map" has already been normalized or if the basic maps are
11134 * disjoint, then there can be no duplicates.
11136 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
11139 isl_basic_map
*bmap
;
11145 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
11147 for (i
= 0; i
< map
->n
; ++i
) {
11148 bmap
= isl_basic_map_copy(map
->p
[i
]);
11149 bmap
= isl_basic_map_sort_constraints(bmap
);
11151 return isl_map_free(map
);
11152 isl_basic_map_free(map
->p
[i
]);
11156 map
= sort_and_remove_duplicates(map
);
11160 /* We normalize in place, but if anything goes wrong we need
11161 * to return NULL, so we need to make sure we don't change the
11162 * meaning of any possible other copies of map.
11164 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
11167 struct isl_basic_map
*bmap
;
11171 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
11173 for (i
= 0; i
< map
->n
; ++i
) {
11174 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
11177 isl_basic_map_free(map
->p
[i
]);
11181 map
= sort_and_remove_duplicates(map
);
11183 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
11190 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
11192 return set_from_map(isl_map_normalize(set_to_map(set
)));
11195 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
11196 __isl_keep isl_map
*map2
)
11201 if (!map1
|| !map2
)
11202 return isl_bool_error
;
11205 return isl_bool_true
;
11206 equal
= isl_map_has_equal_space(map1
, map2
);
11207 if (equal
< 0 || !equal
)
11210 map1
= isl_map_copy(map1
);
11211 map2
= isl_map_copy(map2
);
11212 map1
= isl_map_normalize(map1
);
11213 map2
= isl_map_normalize(map2
);
11214 if (!map1
|| !map2
)
11216 equal
= map1
->n
== map2
->n
;
11217 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
11218 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
11222 isl_map_free(map1
);
11223 isl_map_free(map2
);
11226 isl_map_free(map1
);
11227 isl_map_free(map2
);
11228 return isl_bool_error
;
11231 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
11232 __isl_keep isl_set
*set2
)
11234 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
11237 /* Return the basic maps in "map" as a list.
11239 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
11240 __isl_keep isl_map
*map
)
11244 isl_basic_map_list
*list
;
11248 ctx
= isl_map_get_ctx(map
);
11249 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
11251 for (i
= 0; i
< map
->n
; ++i
) {
11252 isl_basic_map
*bmap
;
11254 bmap
= isl_basic_map_copy(map
->p
[i
]);
11255 list
= isl_basic_map_list_add(list
, bmap
);
11261 /* Return the intersection of the elements in the non-empty list "list".
11262 * All elements are assumed to live in the same space.
11264 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
11265 __isl_take isl_basic_map_list
*list
)
11269 isl_basic_map
*bmap
;
11271 n
= isl_basic_map_list_n_basic_map(list
);
11275 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
11276 "expecting non-empty list", goto error
);
11278 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
11279 for (i
= 1; i
< n
; ++i
) {
11280 isl_basic_map
*bmap_i
;
11282 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
11283 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
11286 isl_basic_map_list_free(list
);
11289 isl_basic_map_list_free(list
);
11293 /* Return the intersection of the elements in the non-empty list "list".
11294 * All elements are assumed to live in the same space.
11296 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
11297 __isl_take isl_basic_set_list
*list
)
11299 return isl_basic_map_list_intersect(list
);
11302 /* Return the union of the elements of "list".
11303 * The list is required to have at least one element.
11305 __isl_give isl_set
*isl_basic_set_list_union(
11306 __isl_take isl_basic_set_list
*list
)
11311 isl_basic_set
*bset
;
11314 n
= isl_basic_set_list_n_basic_set(list
);
11318 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
11319 "expecting non-empty list", goto error
);
11321 bset
= isl_basic_set_list_get_basic_set(list
, 0);
11322 space
= isl_basic_set_get_space(bset
);
11323 isl_basic_set_free(bset
);
11325 set
= isl_set_alloc_space(space
, n
, 0);
11326 for (i
= 0; i
< n
; ++i
) {
11327 bset
= isl_basic_set_list_get_basic_set(list
, i
);
11328 set
= isl_set_add_basic_set(set
, bset
);
11331 isl_basic_set_list_free(list
);
11334 isl_basic_set_list_free(list
);
11338 /* Return the union of the elements in the non-empty list "list".
11339 * All elements are assumed to live in the same space.
11341 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
11347 n
= isl_set_list_n_set(list
);
11351 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
11352 "expecting non-empty list", goto error
);
11354 set
= isl_set_list_get_set(list
, 0);
11355 for (i
= 1; i
< n
; ++i
) {
11358 set_i
= isl_set_list_get_set(list
, i
);
11359 set
= isl_set_union(set
, set_i
);
11362 isl_set_list_free(list
);
11365 isl_set_list_free(list
);
11369 __isl_give isl_basic_map
*isl_basic_map_product(
11370 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11372 isl_space
*space_result
= NULL
;
11373 struct isl_basic_map
*bmap
;
11374 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
11375 struct isl_dim_map
*dim_map1
, *dim_map2
;
11377 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11379 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
11380 isl_space_copy(bmap2
->dim
));
11382 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11383 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11384 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11385 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11386 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11388 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11389 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11390 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11391 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11392 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11393 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11394 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11395 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11396 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11397 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11398 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11400 bmap
= isl_basic_map_alloc_space(space_result
,
11401 bmap1
->n_div
+ bmap2
->n_div
,
11402 bmap1
->n_eq
+ bmap2
->n_eq
,
11403 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11404 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11405 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11406 bmap
= isl_basic_map_simplify(bmap
);
11407 return isl_basic_map_finalize(bmap
);
11409 isl_basic_map_free(bmap1
);
11410 isl_basic_map_free(bmap2
);
11414 __isl_give isl_basic_map
*isl_basic_map_flat_product(
11415 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11417 isl_basic_map
*prod
;
11419 prod
= isl_basic_map_product(bmap1
, bmap2
);
11420 prod
= isl_basic_map_flatten(prod
);
11424 __isl_give isl_basic_set
*isl_basic_set_flat_product(
11425 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
11427 return isl_basic_map_flat_range_product(bset1
, bset2
);
11430 __isl_give isl_basic_map
*isl_basic_map_domain_product(
11431 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11433 isl_space
*space1
, *space2
;
11434 isl_space
*space_result
= NULL
;
11435 isl_basic_map
*bmap
;
11436 isl_size in1
, in2
, out
, nparam
;
11437 unsigned total
, pos
;
11438 struct isl_dim_map
*dim_map1
, *dim_map2
;
11440 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11441 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11442 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11443 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11444 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
11447 space1
= isl_basic_map_get_space(bmap1
);
11448 space2
= isl_basic_map_get_space(bmap2
);
11449 space_result
= isl_space_domain_product(space1
, space2
);
11451 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
11452 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11453 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11454 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11455 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11456 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11457 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11458 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11459 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
11460 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
11461 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11463 bmap
= isl_basic_map_alloc_space(space_result
,
11464 bmap1
->n_div
+ bmap2
->n_div
,
11465 bmap1
->n_eq
+ bmap2
->n_eq
,
11466 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11467 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11468 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11469 bmap
= isl_basic_map_simplify(bmap
);
11470 return isl_basic_map_finalize(bmap
);
11472 isl_basic_map_free(bmap1
);
11473 isl_basic_map_free(bmap2
);
11477 __isl_give isl_basic_map
*isl_basic_map_range_product(
11478 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11481 isl_space
*space_result
= NULL
;
11482 isl_basic_map
*bmap
;
11483 isl_size in
, out1
, out2
, nparam
;
11484 unsigned total
, pos
;
11485 struct isl_dim_map
*dim_map1
, *dim_map2
;
11487 rational
= isl_basic_map_is_rational(bmap1
);
11488 if (rational
>= 0 && rational
)
11489 rational
= isl_basic_map_is_rational(bmap2
);
11490 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11491 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11492 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11493 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11494 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
11497 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11500 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
11501 isl_space_copy(bmap2
->dim
));
11503 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11504 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11505 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11506 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11507 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11508 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11509 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11510 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11511 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11512 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11513 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11515 bmap
= isl_basic_map_alloc_space(space_result
,
11516 bmap1
->n_div
+ bmap2
->n_div
,
11517 bmap1
->n_eq
+ bmap2
->n_eq
,
11518 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11519 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11520 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11522 bmap
= isl_basic_map_set_rational(bmap
);
11523 bmap
= isl_basic_map_simplify(bmap
);
11524 return isl_basic_map_finalize(bmap
);
11526 isl_basic_map_free(bmap1
);
11527 isl_basic_map_free(bmap2
);
11531 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11532 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11534 isl_basic_map
*prod
;
11536 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11537 prod
= isl_basic_map_flatten_range(prod
);
11541 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11542 * and collect the results.
11543 * The result live in the space obtained by calling "space_product"
11544 * on the spaces of "map1" and "map2".
11545 * If "remove_duplicates" is set then the result may contain duplicates
11546 * (even if the inputs do not) and so we try and remove the obvious
11549 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11550 __isl_take isl_map
*map2
,
11551 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11552 __isl_take isl_space
*right
),
11553 __isl_give isl_basic_map
*(*basic_map_product
)(
11554 __isl_take isl_basic_map
*left
,
11555 __isl_take isl_basic_map
*right
),
11556 int remove_duplicates
)
11558 unsigned flags
= 0;
11559 struct isl_map
*result
;
11563 m
= isl_map_has_equal_params(map1
, map2
);
11567 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11568 "parameters don't match", goto error
);
11570 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11571 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11572 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11574 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11575 isl_space_copy(map2
->dim
)),
11576 map1
->n
* map2
->n
, flags
);
11579 for (i
= 0; i
< map1
->n
; ++i
)
11580 for (j
= 0; j
< map2
->n
; ++j
) {
11581 struct isl_basic_map
*part
;
11582 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11583 isl_basic_map_copy(map2
->p
[j
]));
11584 if (isl_basic_map_is_empty(part
))
11585 isl_basic_map_free(part
);
11587 result
= isl_map_add_basic_map(result
, part
);
11591 if (remove_duplicates
)
11592 result
= isl_map_remove_obvious_duplicates(result
);
11593 isl_map_free(map1
);
11594 isl_map_free(map2
);
11597 isl_map_free(map1
);
11598 isl_map_free(map2
);
11602 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11604 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11605 __isl_take isl_map
*map2
)
11607 isl_map_align_params_bin(&map1
, &map2
);
11608 return map_product(map1
, map2
, &isl_space_product
,
11609 &isl_basic_map_product
, 0);
11612 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11614 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11615 __isl_take isl_map
*map2
)
11619 prod
= isl_map_product(map1
, map2
);
11620 prod
= isl_map_flatten(prod
);
11624 /* Given two set A and B, construct its Cartesian product A x B.
11626 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11627 __isl_take isl_set
*set2
)
11629 return isl_map_range_product(set1
, set2
);
11632 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11633 __isl_take isl_set
*set2
)
11635 return isl_map_flat_range_product(set1
, set2
);
11638 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11640 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11641 __isl_take isl_map
*map2
)
11643 isl_map_align_params_bin(&map1
, &map2
);
11644 return map_product(map1
, map2
, &isl_space_domain_product
,
11645 &isl_basic_map_domain_product
, 1);
11648 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11650 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11651 __isl_take isl_map
*map2
)
11653 isl_map_align_params_bin(&map1
, &map2
);
11654 return map_product(map1
, map2
, &isl_space_range_product
,
11655 &isl_basic_map_range_product
, 1);
11658 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11660 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11663 isl_size total1
, keep1
, total2
, keep2
;
11665 total1
= isl_map_dim(map
, isl_dim_in
);
11666 total2
= isl_map_dim(map
, isl_dim_out
);
11667 if (total1
< 0 || total2
< 0)
11668 return isl_map_free(map
);
11669 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11670 !isl_space_range_is_wrapping(map
->dim
))
11671 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11672 "not a product", return isl_map_free(map
));
11674 space
= isl_map_get_space(map
);
11675 space
= isl_space_factor_domain(space
);
11676 keep1
= isl_space_dim(space
, isl_dim_in
);
11677 keep2
= isl_space_dim(space
, isl_dim_out
);
11678 if (keep1
< 0 || keep2
< 0)
11679 map
= isl_map_free(map
);
11680 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11681 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11682 map
= isl_map_reset_space(map
, space
);
11687 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11689 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11692 isl_size total1
, keep1
, total2
, keep2
;
11694 total1
= isl_map_dim(map
, isl_dim_in
);
11695 total2
= isl_map_dim(map
, isl_dim_out
);
11696 if (total1
< 0 || total2
< 0)
11697 return isl_map_free(map
);
11698 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11699 !isl_space_range_is_wrapping(map
->dim
))
11700 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11701 "not a product", return isl_map_free(map
));
11703 space
= isl_map_get_space(map
);
11704 space
= isl_space_factor_range(space
);
11705 keep1
= isl_space_dim(space
, isl_dim_in
);
11706 keep2
= isl_space_dim(space
, isl_dim_out
);
11707 if (keep1
< 0 || keep2
< 0)
11708 map
= isl_map_free(map
);
11709 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11710 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11711 map
= isl_map_reset_space(map
, space
);
11716 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11718 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11721 isl_size total
, keep
;
11723 total
= isl_map_dim(map
, isl_dim_in
);
11725 return isl_map_free(map
);
11726 if (!isl_space_domain_is_wrapping(map
->dim
))
11727 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11728 "domain is not a product", return isl_map_free(map
));
11730 space
= isl_map_get_space(map
);
11731 space
= isl_space_domain_factor_domain(space
);
11732 keep
= isl_space_dim(space
, isl_dim_in
);
11734 map
= isl_map_free(map
);
11735 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11736 map
= isl_map_reset_space(map
, space
);
11741 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11743 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11746 isl_size total
, keep
;
11748 total
= isl_map_dim(map
, isl_dim_in
);
11750 return isl_map_free(map
);
11751 if (!isl_space_domain_is_wrapping(map
->dim
))
11752 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11753 "domain is not a product", return isl_map_free(map
));
11755 space
= isl_map_get_space(map
);
11756 space
= isl_space_domain_factor_range(space
);
11757 keep
= isl_space_dim(space
, isl_dim_in
);
11759 map
= isl_map_free(map
);
11760 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11761 map
= isl_map_reset_space(map
, space
);
11766 /* Given a map A -> [B -> C], extract the map A -> B.
11768 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11771 isl_size total
, keep
;
11773 total
= isl_map_dim(map
, isl_dim_out
);
11775 return isl_map_free(map
);
11776 if (!isl_space_range_is_wrapping(map
->dim
))
11777 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11778 "range is not a product", return isl_map_free(map
));
11780 space
= isl_map_get_space(map
);
11781 space
= isl_space_range_factor_domain(space
);
11782 keep
= isl_space_dim(space
, isl_dim_out
);
11784 map
= isl_map_free(map
);
11785 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11786 map
= isl_map_reset_space(map
, space
);
11791 /* Given a map A -> [B -> C], extract the map A -> C.
11793 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11796 isl_size total
, keep
;
11798 total
= isl_map_dim(map
, isl_dim_out
);
11800 return isl_map_free(map
);
11801 if (!isl_space_range_is_wrapping(map
->dim
))
11802 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11803 "range is not a product", return isl_map_free(map
));
11805 space
= isl_map_get_space(map
);
11806 space
= isl_space_range_factor_range(space
);
11807 keep
= isl_space_dim(space
, isl_dim_out
);
11809 map
= isl_map_free(map
);
11810 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11811 map
= isl_map_reset_space(map
, space
);
11816 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11818 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11819 __isl_take isl_map
*map2
)
11823 prod
= isl_map_domain_product(map1
, map2
);
11824 prod
= isl_map_flatten_domain(prod
);
11828 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11830 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11831 __isl_take isl_map
*map2
)
11835 prod
= isl_map_range_product(map1
, map2
);
11836 prod
= isl_map_flatten_range(prod
);
11840 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11843 uint32_t hash
= isl_hash_init();
11848 bmap
= isl_basic_map_copy(bmap
);
11849 bmap
= isl_basic_map_normalize(bmap
);
11850 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11853 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11854 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11856 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11857 isl_hash_hash(hash
, c_hash
);
11859 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11860 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11862 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11863 isl_hash_hash(hash
, c_hash
);
11865 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11866 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11868 if (isl_int_is_zero(bmap
->div
[i
][0]))
11870 isl_hash_byte(hash
, i
& 0xFF);
11871 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11872 isl_hash_hash(hash
, c_hash
);
11874 isl_basic_map_free(bmap
);
11878 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11880 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11883 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11890 map
= isl_map_copy(map
);
11891 map
= isl_map_normalize(map
);
11895 hash
= isl_hash_init();
11896 for (i
= 0; i
< map
->n
; ++i
) {
11897 uint32_t bmap_hash
;
11898 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11899 isl_hash_hash(hash
, bmap_hash
);
11907 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11909 return isl_map_get_hash(set_to_map(set
));
11912 /* Return the number of basic maps in the (current) representation of "map".
11914 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11916 return map
? map
->n
: isl_size_error
;
11919 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11921 return set
? set
->n
: isl_size_error
;
11924 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11925 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11930 return isl_stat_error
;
11932 for (i
= 0; i
< map
->n
; ++i
)
11933 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11934 return isl_stat_error
;
11936 return isl_stat_ok
;
11939 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11940 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11945 return isl_stat_error
;
11947 for (i
= 0; i
< set
->n
; ++i
)
11948 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11949 return isl_stat_error
;
11951 return isl_stat_ok
;
11954 /* Does "test" succeed on every basic set in "set"?
11956 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11957 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11963 return isl_bool_error
;
11965 for (i
= 0; i
< set
->n
; ++i
) {
11968 r
= test(set
->p
[i
], user
);
11973 return isl_bool_true
;
11976 /* Return a list of basic sets, the union of which is equal to "set".
11978 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11979 __isl_keep isl_set
*set
)
11982 isl_basic_set_list
*list
;
11987 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11988 for (i
= 0; i
< set
->n
; ++i
) {
11989 isl_basic_set
*bset
;
11991 bset
= isl_basic_set_copy(set
->p
[i
]);
11992 list
= isl_basic_set_list_add(list
, bset
);
11998 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
12005 bset
= isl_basic_set_cow(bset
);
12009 space
= isl_basic_set_get_space(bset
);
12010 space
= isl_space_lift(space
, bset
->n_div
);
12013 isl_space_free(bset
->dim
);
12015 bset
->extra
-= bset
->n_div
;
12018 bset
= isl_basic_set_finalize(bset
);
12022 isl_basic_set_free(bset
);
12026 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
12032 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
12037 set
= isl_set_cow(set
);
12041 n_div
= set
->p
[0]->n_div
;
12042 space
= isl_set_get_space(set
);
12043 space
= isl_space_lift(space
, n_div
);
12046 isl_space_free(set
->dim
);
12049 for (i
= 0; i
< set
->n
; ++i
) {
12050 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
12061 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
12066 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
12069 size
+= bset
->n_eq
* (1 + dim
);
12070 size
+= bset
->n_ineq
* (1 + dim
);
12071 size
+= bset
->n_div
* (2 + dim
);
12076 int isl_set_size(__isl_keep isl_set
*set
)
12084 for (i
= 0; i
< set
->n
; ++i
)
12085 size
+= isl_basic_set_size(set
->p
[i
]);
12090 /* Check if there is any lower bound (if lower == 0) and/or upper
12091 * bound (if upper == 0) on the specified dim.
12093 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12094 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
12100 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
12101 return isl_bool_error
;
12103 off
= isl_basic_map_var_offset(bmap
, type
);
12105 return isl_bool_error
;
12106 involves
= isl_basic_map_any_div_involves_vars(bmap
, off
+ pos
, 1);
12107 if (involves
< 0 || involves
)
12110 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12111 if (!isl_int_is_zero(bmap
->eq
[i
][1 + off
+ pos
]))
12112 return isl_bool_true
;
12114 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12115 int sgn
= isl_int_sgn(bmap
->ineq
[i
][1 + off
+ pos
]);
12122 return lower
&& upper
;
12125 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12126 enum isl_dim_type type
, unsigned pos
)
12128 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
12131 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
12132 enum isl_dim_type type
, unsigned pos
)
12134 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
12137 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
12138 enum isl_dim_type type
, unsigned pos
)
12140 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
12143 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
12144 enum isl_dim_type type
, unsigned pos
)
12149 return isl_bool_error
;
12151 for (i
= 0; i
< map
->n
; ++i
) {
12153 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
12154 if (bounded
< 0 || !bounded
)
12158 return isl_bool_true
;
12161 /* Return true if the specified dim is involved in both an upper bound
12162 * and a lower bound.
12164 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
12165 enum isl_dim_type type
, unsigned pos
)
12167 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
12170 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12172 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
12173 enum isl_dim_type type
, unsigned pos
,
12174 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12175 enum isl_dim_type type
, unsigned pos
))
12180 return isl_bool_error
;
12182 for (i
= 0; i
< map
->n
; ++i
) {
12184 bounded
= fn(map
->p
[i
], type
, pos
);
12185 if (bounded
< 0 || bounded
)
12189 return isl_bool_false
;
12192 /* Return 1 if the specified dim is involved in any lower bound.
12194 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
12195 enum isl_dim_type type
, unsigned pos
)
12197 return has_any_bound(set
, type
, pos
,
12198 &isl_basic_map_dim_has_lower_bound
);
12201 /* Return 1 if the specified dim is involved in any upper bound.
12203 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
12204 enum isl_dim_type type
, unsigned pos
)
12206 return has_any_bound(set
, type
, pos
,
12207 &isl_basic_map_dim_has_upper_bound
);
12210 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12212 static isl_bool
has_bound(__isl_keep isl_map
*map
,
12213 enum isl_dim_type type
, unsigned pos
,
12214 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12215 enum isl_dim_type type
, unsigned pos
))
12220 return isl_bool_error
;
12222 for (i
= 0; i
< map
->n
; ++i
) {
12224 bounded
= fn(map
->p
[i
], type
, pos
);
12225 if (bounded
< 0 || !bounded
)
12229 return isl_bool_true
;
12232 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12234 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
12235 enum isl_dim_type type
, unsigned pos
)
12237 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
12240 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12242 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
12243 enum isl_dim_type type
, unsigned pos
)
12245 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
12248 /* For each of the "n" variables starting at "first", determine
12249 * the sign of the variable and put the results in the first "n"
12250 * elements of the array "signs".
12252 * 1 means that the variable is non-negative
12253 * -1 means that the variable is non-positive
12254 * 0 means the variable attains both positive and negative values.
12256 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
12257 unsigned first
, unsigned n
, int *signs
)
12259 isl_vec
*bound
= NULL
;
12260 struct isl_tab
*tab
= NULL
;
12261 struct isl_tab_undo
*snap
;
12265 total
= isl_basic_set_dim(bset
, isl_dim_all
);
12266 if (total
< 0 || !signs
)
12267 return isl_stat_error
;
12269 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
12270 tab
= isl_tab_from_basic_set(bset
, 0);
12271 if (!bound
|| !tab
)
12274 isl_seq_clr(bound
->el
, bound
->size
);
12275 isl_int_set_si(bound
->el
[0], -1);
12277 snap
= isl_tab_snap(tab
);
12278 for (i
= 0; i
< n
; ++i
) {
12281 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
12282 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12284 empty
= tab
->empty
;
12285 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12286 if (isl_tab_rollback(tab
, snap
) < 0)
12294 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
12295 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12297 empty
= tab
->empty
;
12298 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12299 if (isl_tab_rollback(tab
, snap
) < 0)
12302 signs
[i
] = empty
? -1 : 0;
12306 isl_vec_free(bound
);
12307 return isl_stat_ok
;
12310 isl_vec_free(bound
);
12311 return isl_stat_error
;
12314 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
12315 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
12317 if (!bset
|| !signs
)
12318 return isl_stat_error
;
12319 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
12320 return isl_stat_error
;
12322 first
+= pos(bset
->dim
, type
) - 1;
12323 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
12326 /* Is it possible for the integer division "div" to depend (possibly
12327 * indirectly) on any output dimensions?
12329 * If the div is undefined, then we conservatively assume that it
12330 * may depend on them.
12331 * Otherwise, we check if it actually depends on them or on any integer
12332 * divisions that may depend on them.
12334 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
12337 isl_size n_out
, n_div
;
12338 unsigned o_out
, o_div
;
12340 if (isl_int_is_zero(bmap
->div
[div
][0]))
12341 return isl_bool_true
;
12343 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12345 return isl_bool_error
;
12346 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12348 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
12349 return isl_bool_true
;
12351 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12353 return isl_bool_error
;
12354 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12356 for (i
= 0; i
< n_div
; ++i
) {
12357 isl_bool may_involve
;
12359 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
12361 may_involve
= div_may_involve_output(bmap
, i
);
12362 if (may_involve
< 0 || may_involve
)
12363 return may_involve
;
12366 return isl_bool_false
;
12369 /* Return the first integer division of "bmap" in the range
12370 * [first, first + n[ that may depend on any output dimensions and
12371 * that has a non-zero coefficient in "c" (where the first coefficient
12372 * in "c" corresponds to integer division "first").
12374 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
12375 isl_int
*c
, int first
, int n
)
12382 for (k
= first
; k
< first
+ n
; ++k
) {
12383 isl_bool may_involve
;
12385 if (isl_int_is_zero(c
[k
]))
12387 may_involve
= div_may_involve_output(bmap
, k
);
12388 if (may_involve
< 0)
12397 /* Look for a pair of inequality constraints in "bmap" of the form
12399 * -l + i >= 0 or i >= l
12401 * n + l - i >= 0 or i <= l + n
12403 * with n < "m" and i the output dimension at position "pos".
12404 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12405 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12406 * and earlier output dimensions, as well as integer divisions that do
12407 * not involve any of the output dimensions.
12409 * Return the index of the first inequality constraint or bmap->n_ineq
12410 * if no such pair can be found.
12412 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
12413 int pos
, isl_int m
)
12418 isl_size n_div
, n_out
;
12419 unsigned o_div
, o_out
;
12422 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12423 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12424 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12425 if (total
< 0 || n_out
< 0 || n_div
< 0)
12428 ctx
= isl_basic_map_get_ctx(bmap
);
12429 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12430 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12431 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12432 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
12434 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
12435 n_out
- (pos
+ 1)) != -1)
12437 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
12440 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
12441 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
12444 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
12445 bmap
->ineq
[j
] + 1, total
))
12449 if (j
>= bmap
->n_ineq
)
12451 isl_int_add(bmap
->ineq
[i
][0],
12452 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12453 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
12454 isl_int_sub(bmap
->ineq
[i
][0],
12455 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12458 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
12464 return bmap
->n_ineq
;
12467 /* Return the index of the equality of "bmap" that defines
12468 * the output dimension "pos" in terms of earlier dimensions.
12469 * The equality may also involve integer divisions, as long
12470 * as those integer divisions are defined in terms of
12471 * parameters or input dimensions.
12472 * In this case, *div is set to the number of integer divisions and
12473 * *ineq is set to the number of inequality constraints (provided
12474 * div and ineq are not NULL).
12476 * The equality may also involve a single integer division involving
12477 * the output dimensions (typically only output dimension "pos") as
12478 * long as the coefficient of output dimension "pos" is 1 or -1 and
12479 * there is a pair of constraints i >= l and i <= l + n, with i referring
12480 * to output dimension "pos", l an expression involving only earlier
12481 * dimensions and n smaller than the coefficient of the integer division
12482 * in the equality. In this case, the output dimension can be defined
12483 * in terms of a modulo expression that does not involve the integer division.
12484 * *div is then set to this single integer division and
12485 * *ineq is set to the index of constraint i >= l.
12487 * Return bmap->n_eq if there is no such equality.
12488 * Return -1 on error.
12490 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
12491 int pos
, int *div
, int *ineq
)
12494 isl_size n_div
, n_out
;
12495 unsigned o_div
, o_out
;
12497 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12498 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12499 if (n_out
< 0 || n_div
< 0)
12502 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12503 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12506 *ineq
= bmap
->n_ineq
;
12509 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12510 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12512 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12513 n_out
- (pos
+ 1)) != -1)
12515 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12519 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12520 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12522 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12523 k
+ 1, n_div
- (k
+1)) < n_div
)
12525 l
= find_modulo_constraint_pair(bmap
, pos
,
12526 bmap
->eq
[j
][o_div
+ k
]);
12529 if (l
>= bmap
->n_ineq
)
12541 /* Check if the given basic map is obviously single-valued.
12542 * In particular, for each output dimension, check that there is
12543 * an equality that defines the output dimension in terms of
12544 * earlier dimensions.
12546 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12551 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12553 return isl_bool_error
;
12555 for (i
= 0; i
< n_out
; ++i
) {
12558 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12561 return isl_bool_error
;
12562 if (eq
>= bmap
->n_eq
)
12563 return isl_bool_false
;
12566 return isl_bool_true
;
12569 /* Check if the given basic map is single-valued.
12570 * We simply compute
12574 * and check if the result is a subset of the identity mapping.
12576 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12579 isl_basic_map
*test
;
12583 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12587 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12588 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12590 space
= isl_basic_map_get_space(bmap
);
12591 space
= isl_space_map_from_set(isl_space_range(space
));
12592 id
= isl_basic_map_identity(space
);
12594 sv
= isl_basic_map_is_subset(test
, id
);
12596 isl_basic_map_free(test
);
12597 isl_basic_map_free(id
);
12602 /* Check if the given map is obviously single-valued.
12604 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12607 return isl_bool_error
;
12609 return isl_bool_true
;
12611 return isl_bool_false
;
12613 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12616 /* Check if the given map is single-valued.
12617 * We simply compute
12621 * and check if the result is a subset of the identity mapping.
12623 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12630 sv
= isl_map_plain_is_single_valued(map
);
12634 test
= isl_map_reverse(isl_map_copy(map
));
12635 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12637 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12638 id
= isl_map_identity(space
);
12640 sv
= isl_map_is_subset(test
, id
);
12642 isl_map_free(test
);
12648 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12652 map
= isl_map_copy(map
);
12653 map
= isl_map_reverse(map
);
12654 in
= isl_map_is_single_valued(map
);
12660 /* Check if the given map is obviously injective.
12662 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12666 map
= isl_map_copy(map
);
12667 map
= isl_map_reverse(map
);
12668 in
= isl_map_plain_is_single_valued(map
);
12674 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12678 sv
= isl_map_is_single_valued(map
);
12682 return isl_map_is_injective(map
);
12685 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12687 return isl_map_is_single_valued(set_to_map(set
));
12690 /* Does "map" only map elements to themselves?
12692 * If the domain and range spaces are different, then "map"
12693 * is considered not to be an identity relation, even if it is empty.
12694 * Otherwise, construct the maximal identity relation and
12695 * check whether "map" is a subset of this relation.
12697 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12700 isl_bool equal
, is_identity
;
12702 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12703 if (equal
< 0 || !equal
)
12706 id
= isl_map_identity(isl_map_get_space(map
));
12707 is_identity
= isl_map_is_subset(map
, id
);
12710 return is_identity
;
12713 int isl_map_is_translation(__isl_keep isl_map
*map
)
12718 delta
= isl_map_deltas(isl_map_copy(map
));
12719 ok
= isl_set_is_singleton(delta
);
12720 isl_set_free(delta
);
12725 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12727 if (isl_seq_first_non_zero(p
, pos
) != -1)
12729 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12734 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12737 isl_size nvar
, ovar
, n_div
;
12739 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12741 return isl_bool_error
;
12743 return isl_bool_false
;
12745 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12746 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12747 if (nvar
< 0 || ovar
< 0)
12748 return isl_bool_error
;
12749 for (j
= 0; j
< nvar
; ++j
) {
12750 int lower
= 0, upper
= 0;
12751 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12752 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12754 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12755 return isl_bool_false
;
12758 if (i
< bset
->n_eq
)
12760 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12761 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12763 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12764 return isl_bool_false
;
12765 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12770 if (!lower
|| !upper
)
12771 return isl_bool_false
;
12774 return isl_bool_true
;
12777 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12780 return isl_bool_error
;
12782 return isl_bool_false
;
12784 return isl_basic_set_is_box(set
->p
[0]);
12787 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12790 return isl_bool_error
;
12792 return isl_space_is_wrapping(bset
->dim
);
12795 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12798 return isl_bool_error
;
12800 return isl_space_is_wrapping(set
->dim
);
12803 /* Modify the space of "map" through a call to "change".
12804 * If "can_change" is set (not NULL), then first call it to check
12805 * if the modification is allowed, printing the error message "cannot_change"
12808 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12809 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12810 const char *cannot_change
,
12811 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12819 ok
= can_change
? can_change(map
) : isl_bool_true
;
12821 return isl_map_free(map
);
12823 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12824 return isl_map_free(map
));
12826 space
= change(isl_map_get_space(map
));
12827 map
= isl_map_reset_space(map
, space
);
12832 /* Is the domain of "map" a wrapped relation?
12834 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12837 return isl_bool_error
;
12839 return isl_space_domain_is_wrapping(map
->dim
);
12842 /* Does "map" have a wrapped relation in both domain and range?
12844 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12846 return isl_space_is_product(isl_map_peek_space(map
));
12849 /* Is the range of "map" a wrapped relation?
12851 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12854 return isl_bool_error
;
12856 return isl_space_range_is_wrapping(map
->dim
);
12859 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12863 space
= isl_basic_map_take_space(bmap
);
12864 space
= isl_space_wrap(space
);
12865 bmap
= isl_basic_map_restore_space(bmap
, space
);
12867 bmap
= isl_basic_map_finalize(bmap
);
12869 return bset_from_bmap(bmap
);
12872 /* Given a map A -> B, return the set (A -> B).
12874 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12876 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12879 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12881 bset
= isl_basic_set_cow(bset
);
12885 bset
->dim
= isl_space_unwrap(bset
->dim
);
12889 bset
= isl_basic_set_finalize(bset
);
12891 return bset_to_bmap(bset
);
12893 isl_basic_set_free(bset
);
12897 /* Given a set (A -> B), return the map A -> B.
12898 * Error out if "set" is not of the form (A -> B).
12900 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12902 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12903 "not a wrapping set", &isl_space_unwrap
);
12906 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12907 enum isl_dim_type type
)
12911 space
= isl_basic_map_take_space(bmap
);
12912 space
= isl_space_reset(space
, type
);
12913 bmap
= isl_basic_map_restore_space(bmap
, space
);
12915 bmap
= isl_basic_map_mark_final(bmap
);
12920 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12921 enum isl_dim_type type
)
12929 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12932 map
= isl_map_cow(map
);
12936 for (i
= 0; i
< map
->n
; ++i
) {
12937 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12942 space
= isl_map_take_space(map
);
12943 space
= isl_space_reset(space
, type
);
12944 map
= isl_map_restore_space(map
, space
);
12952 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12956 space
= isl_basic_map_take_space(bmap
);
12957 space
= isl_space_flatten(space
);
12958 bmap
= isl_basic_map_restore_space(bmap
, space
);
12960 bmap
= isl_basic_map_mark_final(bmap
);
12965 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12967 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12970 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12971 __isl_take isl_basic_map
*bmap
)
12975 space
= isl_basic_map_take_space(bmap
);
12976 space
= isl_space_flatten_domain(space
);
12977 bmap
= isl_basic_map_restore_space(bmap
, space
);
12979 bmap
= isl_basic_map_mark_final(bmap
);
12984 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12985 __isl_take isl_basic_map
*bmap
)
12989 space
= isl_basic_map_take_space(bmap
);
12990 space
= isl_space_flatten_range(space
);
12991 bmap
= isl_basic_map_restore_space(bmap
, space
);
12993 bmap
= isl_basic_map_mark_final(bmap
);
12998 /* Remove any internal structure from the spaces of domain and range of "map".
13000 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
13005 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
13008 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
13011 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
13013 return set_from_map(isl_map_flatten(set_to_map(set
)));
13016 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
13018 isl_space
*space
, *flat_space
;
13021 space
= isl_set_get_space(set
);
13022 flat_space
= isl_space_flatten(isl_space_copy(space
));
13023 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
13025 map
= isl_map_intersect_domain(map
, set
);
13030 /* Remove any internal structure from the space of the domain of "map".
13032 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
13037 if (!map
->dim
->nested
[0])
13040 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
13043 /* Remove any internal structure from the space of the range of "map".
13045 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
13050 if (!map
->dim
->nested
[1])
13053 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
13056 /* Reorder the dimensions of "bmap" according to the given dim_map
13057 * and set the dimension specification to "space" and
13058 * perform Gaussian elimination on the result.
13060 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
13061 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
13063 isl_basic_map
*res
;
13067 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
13068 if (n_div
< 0 || !space
|| !dim_map
)
13071 flags
= bmap
->flags
;
13072 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
13073 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
13074 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13075 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
13076 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
13078 res
->flags
= flags
;
13079 res
= isl_basic_map_gauss(res
, NULL
);
13080 res
= isl_basic_map_finalize(res
);
13083 isl_dim_map_free(dim_map
);
13084 isl_basic_map_free(bmap
);
13085 isl_space_free(space
);
13089 /* Reorder the dimensions of "map" according to given reordering.
13091 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
13092 __isl_take isl_reordering
*r
)
13095 struct isl_dim_map
*dim_map
;
13097 map
= isl_map_cow(map
);
13098 dim_map
= isl_dim_map_from_reordering(r
);
13099 if (!map
|| !r
|| !dim_map
)
13102 for (i
= 0; i
< map
->n
; ++i
) {
13103 struct isl_dim_map
*dim_map_i
;
13106 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
13108 space
= isl_reordering_get_space(r
);
13109 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
13115 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
13116 map
= isl_map_unmark_normalized(map
);
13118 isl_reordering_free(r
);
13119 isl_dim_map_free(dim_map
);
13122 isl_dim_map_free(dim_map
);
13124 isl_reordering_free(r
);
13128 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
13129 __isl_take isl_reordering
*r
)
13131 return set_from_map(isl_map_realign(set_to_map(set
), r
));
13134 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
13135 __isl_take isl_space
*model
)
13140 if (!map
|| !model
)
13143 ctx
= isl_space_get_ctx(model
);
13144 if (!isl_space_has_named_params(model
))
13145 isl_die(ctx
, isl_error_invalid
,
13146 "model has unnamed parameters", goto error
);
13147 if (isl_map_check_named_params(map
) < 0)
13149 aligned
= isl_map_space_has_equal_params(map
, model
);
13154 isl_reordering
*exp
;
13156 space
= isl_map_peek_space(map
);
13157 exp
= isl_parameter_alignment_reordering(space
, model
);
13158 map
= isl_map_realign(map
, exp
);
13161 isl_space_free(model
);
13164 isl_space_free(model
);
13169 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
13170 __isl_take isl_space
*model
)
13172 return isl_map_align_params(set
, model
);
13175 /* Align the parameters of "bmap" to those of "model", introducing
13176 * additional parameters if needed.
13178 __isl_give isl_basic_map
*isl_basic_map_align_params(
13179 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
13182 isl_bool equal_params
;
13183 isl_space
*bmap_space
;
13185 if (!bmap
|| !model
)
13188 ctx
= isl_space_get_ctx(model
);
13189 if (!isl_space_has_named_params(model
))
13190 isl_die(ctx
, isl_error_invalid
,
13191 "model has unnamed parameters", goto error
);
13192 if (isl_basic_map_check_named_params(bmap
) < 0)
13194 bmap_space
= isl_basic_map_peek_space(bmap
);
13195 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
13196 if (equal_params
< 0)
13198 if (!equal_params
) {
13199 isl_reordering
*exp
;
13200 struct isl_dim_map
*dim_map
;
13202 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
13203 dim_map
= isl_dim_map_from_reordering(exp
);
13204 bmap
= isl_basic_map_realign(bmap
,
13205 isl_reordering_get_space(exp
),
13206 isl_dim_map_extend(dim_map
, bmap
));
13207 isl_reordering_free(exp
);
13208 isl_dim_map_free(dim_map
);
13211 isl_space_free(model
);
13214 isl_space_free(model
);
13215 isl_basic_map_free(bmap
);
13219 /* Do "bset" and "space" have the same parameters?
13221 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
13222 __isl_keep isl_space
*space
)
13224 isl_space
*bset_space
;
13226 bset_space
= isl_basic_set_peek_space(bset
);
13227 return isl_space_has_equal_params(bset_space
, space
);
13230 /* Do "map" and "space" have the same parameters?
13232 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
13233 __isl_keep isl_space
*space
)
13235 isl_space
*map_space
;
13237 map_space
= isl_map_peek_space(map
);
13238 return isl_space_has_equal_params(map_space
, space
);
13241 /* Do "set" and "space" have the same parameters?
13243 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
13244 __isl_keep isl_space
*space
)
13246 return isl_map_space_has_equal_params(set_to_map(set
), space
);
13249 /* Align the parameters of "bset" to those of "model", introducing
13250 * additional parameters if needed.
13252 __isl_give isl_basic_set
*isl_basic_set_align_params(
13253 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
13255 return isl_basic_map_align_params(bset
, model
);
13259 #define TYPE isl_map
13260 #define isl_map_drop_dims isl_map_drop
13261 #include "isl_drop_unused_params_templ.c"
13263 /* Drop all parameters not referenced by "set".
13265 __isl_give isl_set
*isl_set_drop_unused_params(
13266 __isl_take isl_set
*set
)
13268 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
13272 #define TYPE isl_basic_map
13273 #define isl_basic_map_drop_dims isl_basic_map_drop
13274 #include "isl_drop_unused_params_templ.c"
13276 /* Drop all parameters not referenced by "bset".
13278 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
13279 __isl_take isl_basic_set
*bset
)
13281 return bset_from_bmap(isl_basic_map_drop_unused_params(
13282 bset_to_bmap(bset
)));
13285 /* Given a tuple of identifiers "tuple" in a space that corresponds
13286 * to that of "set", if any of those identifiers appear as parameters
13287 * in "set", then equate those parameters with the corresponding
13288 * set dimensions and project out the parameters.
13289 * The result therefore has no such parameters.
13291 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
13292 __isl_keep isl_multi_id
*tuple
)
13296 isl_space
*set_space
, *tuple_space
;
13298 set_space
= isl_set_peek_space(set
);
13299 tuple_space
= isl_multi_id_peek_space(tuple
);
13300 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
13301 return isl_set_free(set
);
13302 n
= isl_multi_id_size(tuple
);
13304 return isl_set_free(set
);
13305 for (i
= 0; i
< n
; ++i
) {
13309 id
= isl_multi_id_get_at(tuple
, i
);
13311 return isl_set_free(set
);
13312 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
13316 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
13317 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
13322 /* Bind the set dimensions of "set" to parameters with identifiers
13323 * specified by "tuple", living in the same space as "set".
13325 * If no parameters with these identifiers appear in "set" already,
13326 * then the set dimensions are simply reinterpreted as parameters.
13327 * Otherwise, the parameters are first equated to the corresponding
13330 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
13331 __isl_take isl_multi_id
*tuple
)
13335 set
= equate_params(set
, tuple
);
13336 space
= isl_set_get_space(set
);
13337 space
= isl_space_bind_set(space
, tuple
);
13338 isl_multi_id_free(tuple
);
13339 set
= isl_set_reset_space(set
, space
);
13344 /* Given a tuple of identifiers "tuple" in a space that corresponds
13345 * to the domain of "map", if any of those identifiers appear as parameters
13346 * in "map", then equate those parameters with the corresponding
13347 * input dimensions and project out the parameters.
13348 * The result therefore has no such parameters.
13350 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
13351 __isl_keep isl_multi_id
*tuple
)
13355 isl_space
*map_space
, *tuple_space
;
13357 map_space
= isl_map_peek_space(map
);
13358 tuple_space
= isl_multi_id_peek_space(tuple
);
13359 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
13360 return isl_map_free(map
);
13361 n
= isl_multi_id_size(tuple
);
13363 return isl_map_free(map
);
13364 for (i
= 0; i
< n
; ++i
) {
13368 id
= isl_multi_id_get_at(tuple
, i
);
13370 return isl_map_free(map
);
13371 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
13375 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
13376 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
13381 /* Bind the input dimensions of "map" to parameters with identifiers
13382 * specified by "tuple", living in the domain space of "map".
13384 * If no parameters with these identifiers appear in "map" already,
13385 * then the input dimensions are simply reinterpreted as parameters.
13386 * Otherwise, the parameters are first equated to the corresponding
13387 * input dimensions.
13389 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
13390 __isl_take isl_multi_id
*tuple
)
13395 map
= map_equate_params(map
, tuple
);
13396 space
= isl_map_get_space(map
);
13397 space
= isl_space_bind_map_domain(space
, tuple
);
13398 isl_multi_id_free(tuple
);
13399 set
= set_from_map(isl_map_reset_space(map
, space
));
13404 /* Bind the output dimensions of "map" to parameters with identifiers
13405 * specified by "tuple", living in the range space of "map".
13407 * Since binding is more easily implemented on the domain,
13408 * bind the input dimensions of the inverse of "map".
13410 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
13411 __isl_take isl_multi_id
*tuple
)
13413 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
13416 /* Insert a domain corresponding to "tuple"
13417 * into the nullary or unary relation "set".
13418 * The result has an extra initial tuple and is therefore
13419 * either a unary or binary relation.
13420 * Any parameters with identifiers in "tuple" are reinterpreted
13421 * as the corresponding domain dimensions.
13423 static __isl_give isl_map
*unbind_params_insert_domain(
13424 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
13429 space
= isl_set_peek_space(set
);
13430 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
13431 isl_multi_id_free(tuple
);
13433 return isl_map_realign(set_to_map(set
), r
);
13436 /* Construct a set with "tuple" as domain from the parameter domain "set".
13437 * Any parameters with identifiers in "tuple" are reinterpreted
13438 * as the corresponding set dimensions.
13440 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
13441 __isl_take isl_multi_id
*tuple
)
13443 isl_bool is_params
;
13445 is_params
= isl_set_is_params(set
);
13447 set
= isl_set_free(set
);
13448 else if (!is_params
)
13449 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13450 "expecting parameter domain", set
= isl_set_free(set
));
13451 return set_from_map(unbind_params_insert_domain(set
, tuple
));
13454 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13456 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
13458 isl_bool is_params
;
13460 is_params
= isl_set_is_params(set
);
13462 return isl_stat_error
;
13463 else if (is_params
)
13464 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13465 "expecting proper set", return isl_stat_error
);
13467 return isl_stat_ok
;
13470 /* Construct a map with "domain" as domain and "set" as range.
13471 * Any parameters with identifiers in "domain" are reinterpreted
13472 * as the corresponding domain dimensions.
13474 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
13475 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
13477 if (isl_set_check_is_set(set
) < 0)
13478 set
= isl_set_free(set
);
13479 return unbind_params_insert_domain(set
, domain
);
13482 /* Construct a map with "domain" as domain and "set" as range.
13484 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
13485 __isl_take isl_space
*domain
)
13491 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
13492 domain
= isl_space_free(domain
);
13493 dim
= isl_space_dim(domain
, isl_dim_set
);
13495 domain
= isl_space_free(domain
);
13496 space
= isl_set_get_space(set
);
13497 domain
= isl_space_replace_params(domain
, space
);
13498 space
= isl_space_map_from_domain_and_range(domain
, space
);
13500 map
= isl_map_from_range(set
);
13501 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
13502 map
= isl_map_reset_space(map
, space
);
13507 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13508 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13509 enum isl_dim_type c2
, enum isl_dim_type c3
,
13510 enum isl_dim_type c4
, enum isl_dim_type c5
)
13512 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13513 struct isl_mat
*mat
;
13518 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13521 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13524 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13525 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13526 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13527 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13529 return isl_mat_free(mat
);
13530 for (k
= 0; k
< dim
; ++k
) {
13531 isl_int_set(mat
->row
[i
][pos
],
13532 bmap
->eq
[i
][off
+ k
]);
13540 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13541 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13542 enum isl_dim_type c2
, enum isl_dim_type c3
,
13543 enum isl_dim_type c4
, enum isl_dim_type c5
)
13545 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13546 struct isl_mat
*mat
;
13551 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13554 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13557 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13558 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13559 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13560 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13562 return isl_mat_free(mat
);
13563 for (k
= 0; k
< dim
; ++k
) {
13564 isl_int_set(mat
->row
[i
][pos
],
13565 bmap
->ineq
[i
][off
+ k
]);
13573 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13574 __isl_take isl_space
*space
,
13575 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13576 enum isl_dim_type c2
, enum isl_dim_type c3
,
13577 enum isl_dim_type c4
, enum isl_dim_type c5
)
13579 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13580 isl_basic_map
*bmap
= NULL
;
13587 dim
= isl_space_dim(space
, isl_dim_all
);
13588 if (dim
< 0 || !eq
|| !ineq
)
13591 if (eq
->n_col
!= ineq
->n_col
)
13592 isl_die(space
->ctx
, isl_error_invalid
,
13593 "equalities and inequalities matrices should have "
13594 "same number of columns", goto error
);
13598 if (eq
->n_col
< total
)
13599 isl_die(space
->ctx
, isl_error_invalid
,
13600 "number of columns too small", goto error
);
13602 extra
= eq
->n_col
- total
;
13604 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13605 eq
->n_row
, ineq
->n_row
);
13608 for (i
= 0; i
< extra
; ++i
) {
13609 k
= isl_basic_map_alloc_div(bmap
);
13612 isl_int_set_si(bmap
->div
[k
][0], 0);
13614 for (i
= 0; i
< eq
->n_row
; ++i
) {
13615 l
= isl_basic_map_alloc_equality(bmap
);
13618 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13619 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13620 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13623 for (k
= 0; k
< dim
; ++k
) {
13624 isl_int_set(bmap
->eq
[l
][off
+ k
],
13630 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13631 l
= isl_basic_map_alloc_inequality(bmap
);
13634 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13635 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13636 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13639 for (k
= 0; k
< dim
; ++k
) {
13640 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13641 ineq
->row
[i
][pos
]);
13647 isl_space_free(space
);
13649 isl_mat_free(ineq
);
13651 bmap
= isl_basic_map_simplify(bmap
);
13652 return isl_basic_map_finalize(bmap
);
13654 isl_space_free(space
);
13656 isl_mat_free(ineq
);
13657 isl_basic_map_free(bmap
);
13661 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13662 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13663 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13665 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13666 c1
, c2
, c3
, c4
, isl_dim_in
);
13669 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13670 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13671 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13673 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13674 c1
, c2
, c3
, c4
, isl_dim_in
);
13677 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13678 __isl_take isl_space
*space
,
13679 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13680 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13682 isl_basic_map
*bmap
;
13683 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13684 c1
, c2
, c3
, c4
, isl_dim_in
);
13685 return bset_from_bmap(bmap
);
13688 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13691 return isl_bool_error
;
13693 return isl_space_can_zip(bmap
->dim
);
13696 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13699 return isl_bool_error
;
13701 return isl_space_can_zip(map
->dim
);
13704 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13705 * (A -> C) -> (B -> D).
13707 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13717 if (!isl_basic_map_can_zip(bmap
))
13718 isl_die(bmap
->ctx
, isl_error_invalid
,
13719 "basic map cannot be zipped", goto error
);
13720 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13721 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13722 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13723 if (n_in
< 0 || n1
< 0 || n2
< 0)
13724 return isl_basic_map_free(bmap
);
13725 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13726 bmap
= isl_basic_map_cow(bmap
);
13727 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13730 bmap
->dim
= isl_space_zip(bmap
->dim
);
13733 bmap
= isl_basic_map_mark_final(bmap
);
13736 isl_basic_map_free(bmap
);
13740 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13741 * (A -> C) -> (B -> D).
13743 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13748 if (!isl_map_can_zip(map
))
13749 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13752 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13758 /* Can we apply isl_basic_map_curry to "bmap"?
13759 * That is, does it have a nested relation in its domain?
13761 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13764 return isl_bool_error
;
13766 return isl_space_can_curry(bmap
->dim
);
13769 /* Can we apply isl_map_curry to "map"?
13770 * That is, does it have a nested relation in its domain?
13772 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13775 return isl_bool_error
;
13777 return isl_space_can_curry(map
->dim
);
13780 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13783 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13789 if (!isl_basic_map_can_curry(bmap
))
13790 isl_die(bmap
->ctx
, isl_error_invalid
,
13791 "basic map cannot be curried", goto error
);
13792 bmap
= isl_basic_map_cow(bmap
);
13795 bmap
->dim
= isl_space_curry(bmap
->dim
);
13798 bmap
= isl_basic_map_mark_final(bmap
);
13801 isl_basic_map_free(bmap
);
13805 /* Given a map (A -> B) -> C, return the corresponding map
13808 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13810 return isl_map_change_space(map
, &isl_map_can_curry
,
13811 "map cannot be curried", &isl_space_curry
);
13814 /* Can isl_map_range_curry be applied to "map"?
13815 * That is, does it have a nested relation in its range,
13816 * the domain of which is itself a nested relation?
13818 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13821 return isl_bool_error
;
13823 return isl_space_can_range_curry(map
->dim
);
13826 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13827 * A -> (B -> (C -> D)).
13829 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13831 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13832 "map range cannot be curried",
13833 &isl_space_range_curry
);
13836 /* Can we apply isl_basic_map_uncurry to "bmap"?
13837 * That is, does it have a nested relation in its domain?
13839 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13842 return isl_bool_error
;
13844 return isl_space_can_uncurry(bmap
->dim
);
13847 /* Can we apply isl_map_uncurry to "map"?
13848 * That is, does it have a nested relation in its domain?
13850 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13853 return isl_bool_error
;
13855 return isl_space_can_uncurry(map
->dim
);
13858 /* Given a basic map A -> (B -> C), return the corresponding basic map
13861 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13867 if (!isl_basic_map_can_uncurry(bmap
))
13868 isl_die(bmap
->ctx
, isl_error_invalid
,
13869 "basic map cannot be uncurried",
13870 return isl_basic_map_free(bmap
));
13871 bmap
= isl_basic_map_cow(bmap
);
13874 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13876 return isl_basic_map_free(bmap
);
13877 bmap
= isl_basic_map_mark_final(bmap
);
13881 /* Given a map A -> (B -> C), return the corresponding map
13884 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13886 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13887 "map cannot be uncurried", &isl_space_uncurry
);
13890 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13891 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13893 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13896 /* Construct a basic map where the given dimensions are equal to each other.
13898 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13899 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13901 isl_basic_map
*bmap
= NULL
;
13905 total
= isl_space_dim(space
, isl_dim_all
);
13907 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13908 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13911 if (type1
== type2
&& pos1
== pos2
)
13912 return isl_basic_map_universe(space
);
13914 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13915 i
= isl_basic_map_alloc_equality(bmap
);
13918 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13919 pos1
+= isl_basic_map_offset(bmap
, type1
);
13920 pos2
+= isl_basic_map_offset(bmap
, type2
);
13921 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13922 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13923 bmap
= isl_basic_map_finalize(bmap
);
13924 isl_space_free(space
);
13927 isl_space_free(space
);
13928 isl_basic_map_free(bmap
);
13932 /* Add a constraint imposing that the given two dimensions are equal.
13934 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13935 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13939 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13941 bmap
= isl_basic_map_intersect(bmap
, eq
);
13946 /* Add a constraint imposing that the given two dimensions are equal.
13948 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13949 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13951 isl_basic_map
*bmap
;
13953 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13955 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13960 /* Add a constraint imposing that the given two dimensions have opposite values.
13962 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13963 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13965 isl_basic_map
*bmap
= NULL
;
13969 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13970 return isl_map_free(map
);
13971 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13972 return isl_map_free(map
);
13974 total
= isl_map_dim(map
, isl_dim_all
);
13976 return isl_map_free(map
);
13977 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13978 i
= isl_basic_map_alloc_equality(bmap
);
13981 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13982 pos1
+= isl_basic_map_offset(bmap
, type1
);
13983 pos2
+= isl_basic_map_offset(bmap
, type2
);
13984 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13985 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13986 bmap
= isl_basic_map_finalize(bmap
);
13988 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13992 isl_basic_map_free(bmap
);
13997 /* Construct a constraint imposing that the value of the first dimension is
13998 * greater than or equal to that of the second.
14000 static __isl_give isl_constraint
*constraint_order_ge(
14001 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
14002 enum isl_dim_type type2
, int pos2
)
14006 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14007 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14008 space
= isl_space_free(space
);
14012 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
14014 if (type1
== type2
&& pos1
== pos2
)
14017 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
14018 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
14023 /* Add a constraint imposing that the value of the first dimension is
14024 * greater than or equal to that of the second.
14026 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
14027 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14032 if (type1
== type2
&& pos1
== pos2
)
14034 space
= isl_basic_map_get_space(bmap
);
14035 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14036 bmap
= isl_basic_map_add_constraint(bmap
, c
);
14041 /* Add a constraint imposing that the value of the first dimension is
14042 * greater than or equal to that of the second.
14044 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
14045 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14050 if (type1
== type2
&& pos1
== pos2
)
14052 space
= isl_map_get_space(map
);
14053 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14054 map
= isl_map_add_constraint(map
, c
);
14059 /* Add a constraint imposing that the value of the first dimension is
14060 * less than or equal to that of the second.
14062 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
14063 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14065 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
14068 /* Construct a basic map where the value of the first dimension is
14069 * greater than that of the second.
14071 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
14072 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14074 isl_basic_map
*bmap
= NULL
;
14078 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14079 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14082 if (type1
== type2
&& pos1
== pos2
)
14083 return isl_basic_map_empty(space
);
14085 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
14086 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14087 i
= isl_basic_map_alloc_inequality(bmap
);
14088 if (total
< 0 || i
< 0)
14089 return isl_basic_map_free(bmap
);
14090 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
14091 pos1
+= isl_basic_map_offset(bmap
, type1
);
14092 pos2
+= isl_basic_map_offset(bmap
, type2
);
14093 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
14094 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
14095 isl_int_set_si(bmap
->ineq
[i
][0], -1);
14096 bmap
= isl_basic_map_finalize(bmap
);
14100 isl_space_free(space
);
14101 isl_basic_map_free(bmap
);
14105 /* Add a constraint imposing that the value of the first dimension is
14106 * greater than that of the second.
14108 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
14109 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14113 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
14115 bmap
= isl_basic_map_intersect(bmap
, gt
);
14120 /* Add a constraint imposing that the value of the first dimension is
14121 * greater than that of the second.
14123 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
14124 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14126 isl_basic_map
*bmap
;
14128 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
14130 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
14135 /* Add a constraint imposing that the value of the first dimension is
14136 * smaller than that of the second.
14138 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
14139 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14141 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
14144 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
14148 isl_local_space
*ls
;
14153 if (!isl_basic_map_divs_known(bmap
))
14154 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14155 "some divs are unknown", return NULL
);
14157 ls
= isl_basic_map_get_local_space(bmap
);
14158 div
= isl_local_space_get_div(ls
, pos
);
14159 isl_local_space_free(ls
);
14164 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
14167 return isl_basic_map_get_div(bset
, pos
);
14170 /* Plug in "subs" for set dimension "pos" of "set".
14172 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
14173 unsigned pos
, __isl_keep isl_aff
*subs
)
14177 if (set
&& isl_set_plain_is_empty(set
))
14180 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
14181 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
14182 return isl_set_preimage_multi_aff(set
, ma
);
14185 /* Check if the range of "ma" is compatible with the domain or range
14186 * (depending on "type") of "bmap".
14188 static isl_stat
check_basic_map_compatible_range_multi_aff(
14189 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
14190 __isl_keep isl_multi_aff
*ma
)
14193 isl_space
*ma_space
;
14195 ma_space
= isl_multi_aff_get_space(ma
);
14197 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
14201 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14202 "parameters don't match", goto error
);
14203 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
14207 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14208 "spaces don't match", goto error
);
14210 isl_space_free(ma_space
);
14211 return isl_stat_ok
;
14213 isl_space_free(ma_space
);
14214 return isl_stat_error
;
14217 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14218 * coefficients before the transformed range of dimensions,
14219 * the "n_after" coefficients after the transformed range of dimensions
14220 * and the coefficients of the other divs in "bmap".
14222 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
14223 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
14228 isl_local_space
*ls
;
14233 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
14234 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
14235 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
14236 if (n_param
< 0 || n_set
< 0)
14237 return isl_basic_map_free(bmap
);
14239 for (i
= 0; i
< n_div
; ++i
) {
14240 int o_bmap
= 0, o_ls
= 0;
14242 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
14243 o_bmap
+= 1 + 1 + n_param
;
14244 o_ls
+= 1 + 1 + n_param
;
14245 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
14246 o_bmap
+= n_before
;
14247 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14248 ls
->div
->row
[i
] + o_ls
, n_set
);
14251 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
14253 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14254 ls
->div
->row
[i
] + o_ls
, n_div
);
14257 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
14258 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
14263 isl_local_space_free(ls
);
14266 isl_local_space_free(ls
);
14267 return isl_basic_map_free(bmap
);
14270 /* How many stride constraints does "ma" enforce?
14271 * That is, how many of the affine expressions have a denominator
14272 * different from one?
14274 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
14279 for (i
= 0; i
< ma
->n
; ++i
)
14280 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14286 /* For each affine expression in ma of the form
14288 * x_i = (f_i y + h_i)/m_i
14290 * with m_i different from one, add a constraint to "bmap"
14293 * f_i y + h_i = m_i alpha_i
14295 * with alpha_i an additional existentially quantified variable.
14297 * The input variables of "ma" correspond to a subset of the variables
14298 * of "bmap". There are "n_before" variables in "bmap" before this
14299 * subset and "n_after" variables after this subset.
14300 * The integer divisions of the affine expressions in "ma" are assumed
14301 * to have been aligned. There are "n_div_ma" of them and
14302 * they appear first in "bmap", straight after the "n_after" variables.
14304 static __isl_give isl_basic_map
*add_ma_strides(
14305 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
14306 int n_before
, int n_after
, int n_div_ma
)
14314 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14315 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
14316 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
14317 if (total
< 0 || n_param
< 0 || n_in
< 0)
14318 return isl_basic_map_free(bmap
);
14319 for (i
= 0; i
< ma
->n
; ++i
) {
14320 int o_bmap
= 0, o_ma
= 1;
14322 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14324 div
= isl_basic_map_alloc_div(bmap
);
14325 k
= isl_basic_map_alloc_equality(bmap
);
14326 if (div
< 0 || k
< 0)
14328 isl_int_set_si(bmap
->div
[div
][0], 0);
14329 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14330 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
14331 o_bmap
+= 1 + n_param
;
14332 o_ma
+= 1 + n_param
;
14333 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
14334 o_bmap
+= n_before
;
14335 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14336 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
14339 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
14341 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14342 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
14343 o_bmap
+= n_div_ma
;
14345 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
14346 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
14352 isl_basic_map_free(bmap
);
14356 /* Replace the domain or range space (depending on "type) of "space" by "set".
14358 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
14359 enum isl_dim_type type
, __isl_take isl_space
*set
)
14361 if (type
== isl_dim_in
) {
14362 space
= isl_space_range(space
);
14363 space
= isl_space_map_from_domain_and_range(set
, space
);
14365 space
= isl_space_domain(space
);
14366 space
= isl_space_map_from_domain_and_range(space
, set
);
14372 /* Compute the preimage of the domain or range (depending on "type")
14373 * of "bmap" under the function represented by "ma".
14374 * In other words, plug in "ma" in the domain or range of "bmap".
14375 * The result is a basic map that lives in the same space as "bmap"
14376 * except that the domain or range has been replaced by
14377 * the domain space of "ma".
14379 * If bmap is represented by
14381 * A(p) + S u + B x + T v + C(divs) >= 0,
14383 * where u and x are input and output dimensions if type == isl_dim_out
14384 * while x and v are input and output dimensions if type == isl_dim_in,
14385 * and ma is represented by
14387 * x = D(p) + F(y) + G(divs')
14389 * then the result is
14391 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14393 * The divs in the input set are similarly adjusted.
14396 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14400 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14401 * B_i G(divs') + c_i(divs))/n_i)
14403 * If bmap is not a rational map and if F(y) involves any denominators
14405 * x_i = (f_i y + h_i)/m_i
14407 * then additional constraints are added to ensure that we only
14408 * map back integer points. That is we enforce
14410 * f_i y + h_i = m_i alpha_i
14412 * with alpha_i an additional existentially quantified variable.
14414 * We first copy over the divs from "ma".
14415 * Then we add the modified constraints and divs from "bmap".
14416 * Finally, we add the stride constraints, if needed.
14418 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
14419 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
14420 __isl_take isl_multi_aff
*ma
)
14424 isl_basic_map
*res
= NULL
;
14425 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
14426 isl_int f
, c1
, c2
, g
;
14435 ma
= isl_multi_aff_align_divs(ma
);
14438 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
14441 if (type
== isl_dim_in
) {
14443 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
14445 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
14448 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
14449 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
14450 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
14453 space
= isl_multi_aff_get_domain_space(ma
);
14454 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
14455 rational
= isl_basic_map_is_rational(bmap
);
14456 strides
= rational
? 0 : multi_aff_strides(ma
);
14457 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
14458 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
14460 res
= isl_basic_map_set_rational(res
);
14462 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
14463 if (isl_basic_map_alloc_div(res
) < 0)
14466 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
14470 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
14471 k
= isl_basic_map_alloc_equality(res
);
14474 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
14475 n_after
, n_div_ma
, n_div_bmap
,
14476 f
, c1
, c2
, g
, 0) < 0)
14480 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
14481 k
= isl_basic_map_alloc_inequality(res
);
14484 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
14485 n_after
, n_div_ma
, n_div_bmap
,
14486 f
, c1
, c2
, g
, 0) < 0)
14490 for (i
= 0; i
< bmap
->n_div
; ++i
) {
14491 if (isl_int_is_zero(bmap
->div
[i
][0])) {
14492 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
14495 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
14496 n_before
, n_after
, n_div_ma
, n_div_bmap
,
14497 f
, c1
, c2
, g
, 1) < 0)
14502 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14508 isl_basic_map_free(bmap
);
14509 isl_multi_aff_free(ma
);
14510 res
= isl_basic_map_simplify(res
);
14511 return isl_basic_map_finalize(res
);
14517 isl_basic_map_free(bmap
);
14518 isl_multi_aff_free(ma
);
14519 isl_basic_map_free(res
);
14523 /* Compute the preimage of "bset" under the function represented by "ma".
14524 * In other words, plug in "ma" in "bset". The result is a basic set
14525 * that lives in the domain space of "ma".
14527 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14528 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14530 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14533 /* Compute the preimage of the domain of "bmap" under the function
14534 * represented by "ma".
14535 * In other words, plug in "ma" in the domain of "bmap".
14536 * The result is a basic map that lives in the same space as "bmap"
14537 * except that the domain has been replaced by the domain space of "ma".
14539 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14540 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14542 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14545 /* Compute the preimage of the range of "bmap" under the function
14546 * represented by "ma".
14547 * In other words, plug in "ma" in the range of "bmap".
14548 * The result is a basic map that lives in the same space as "bmap"
14549 * except that the range has been replaced by the domain space of "ma".
14551 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14552 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14554 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14557 /* Check if the range of "ma" is compatible with the domain or range
14558 * (depending on "type") of "map".
14559 * Return isl_stat_error if anything is wrong.
14561 static isl_stat
check_map_compatible_range_multi_aff(
14562 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14563 __isl_keep isl_multi_aff
*ma
)
14566 isl_space
*ma_space
;
14568 ma_space
= isl_multi_aff_get_space(ma
);
14569 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14570 isl_space_free(ma_space
);
14572 return isl_stat_error
;
14574 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14575 "spaces don't match", return isl_stat_error
);
14576 return isl_stat_ok
;
14579 /* Compute the preimage of the domain or range (depending on "type")
14580 * of "map" under the function represented by "ma".
14581 * In other words, plug in "ma" in the domain or range of "map".
14582 * The result is a map that lives in the same space as "map"
14583 * except that the domain or range has been replaced by
14584 * the domain space of "ma".
14586 * The parameters are assumed to have been aligned.
14588 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14589 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14594 map
= isl_map_cow(map
);
14595 ma
= isl_multi_aff_align_divs(ma
);
14598 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14601 for (i
= 0; i
< map
->n
; ++i
) {
14602 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14603 isl_multi_aff_copy(ma
));
14608 space
= isl_multi_aff_get_domain_space(ma
);
14609 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14611 isl_space_free(isl_map_take_space(map
));
14612 map
= isl_map_restore_space(map
, space
);
14616 isl_multi_aff_free(ma
);
14618 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14619 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14622 isl_multi_aff_free(ma
);
14627 /* Compute the preimage of the domain or range (depending on "type")
14628 * of "map" under the function represented by "ma".
14629 * In other words, plug in "ma" in the domain or range of "map".
14630 * The result is a map that lives in the same space as "map"
14631 * except that the domain or range has been replaced by
14632 * the domain space of "ma".
14634 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14635 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14642 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14646 return map_preimage_multi_aff(map
, type
, ma
);
14648 if (isl_map_check_named_params(map
) < 0)
14650 if (!isl_space_has_named_params(ma
->space
))
14651 isl_die(map
->ctx
, isl_error_invalid
,
14652 "unaligned unnamed parameters", goto error
);
14653 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14654 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14656 return map_preimage_multi_aff(map
, type
, ma
);
14658 isl_multi_aff_free(ma
);
14659 return isl_map_free(map
);
14662 /* Compute the preimage of "set" under the function represented by "ma".
14663 * In other words, plug in "ma" in "set". The result is a set
14664 * that lives in the domain space of "ma".
14666 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14667 __isl_take isl_multi_aff
*ma
)
14669 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14672 /* Compute the preimage of the domain of "map" under the function
14673 * represented by "ma".
14674 * In other words, plug in "ma" in the domain of "map".
14675 * The result is a map that lives in the same space as "map"
14676 * except that the domain has been replaced by the domain space of "ma".
14678 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14679 __isl_take isl_multi_aff
*ma
)
14681 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14684 /* Compute the preimage of the range of "map" under the function
14685 * represented by "ma".
14686 * In other words, plug in "ma" in the range of "map".
14687 * The result is a map that lives in the same space as "map"
14688 * except that the range has been replaced by the domain space of "ma".
14690 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14691 __isl_take isl_multi_aff
*ma
)
14693 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14696 /* Compute the preimage of "map" under the function represented by "pma".
14697 * In other words, plug in "pma" in the domain or range of "map".
14698 * The result is a map that lives in the same space as "map",
14699 * except that the space of type "type" has been replaced by
14700 * the domain space of "pma".
14702 * The parameters of "map" and "pma" are assumed to have been aligned.
14704 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14705 __isl_take isl_map
*map
, enum isl_dim_type type
,
14706 __isl_take isl_pw_multi_aff
*pma
)
14717 space
= isl_pw_multi_aff_get_domain_space(pma
);
14718 isl_pw_multi_aff_free(pma
);
14719 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14721 return isl_map_empty(space
);
14724 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14725 isl_multi_aff_copy(pma
->p
[0].maff
));
14726 if (type
== isl_dim_in
)
14727 res
= isl_map_intersect_domain(res
,
14728 isl_map_copy(pma
->p
[0].set
));
14730 res
= isl_map_intersect_range(res
,
14731 isl_map_copy(pma
->p
[0].set
));
14733 for (i
= 1; i
< pma
->n
; ++i
) {
14736 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14737 isl_multi_aff_copy(pma
->p
[i
].maff
));
14738 if (type
== isl_dim_in
)
14739 res_i
= isl_map_intersect_domain(res_i
,
14740 isl_map_copy(pma
->p
[i
].set
));
14742 res_i
= isl_map_intersect_range(res_i
,
14743 isl_map_copy(pma
->p
[i
].set
));
14744 res
= isl_map_union(res
, res_i
);
14747 isl_pw_multi_aff_free(pma
);
14751 isl_pw_multi_aff_free(pma
);
14756 /* Compute the preimage of "map" under the function represented by "pma".
14757 * In other words, plug in "pma" in the domain or range of "map".
14758 * The result is a map that lives in the same space as "map",
14759 * except that the space of type "type" has been replaced by
14760 * the domain space of "pma".
14762 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14763 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14770 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14774 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14776 if (isl_map_check_named_params(map
) < 0)
14778 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14780 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14781 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14783 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14785 isl_pw_multi_aff_free(pma
);
14786 return isl_map_free(map
);
14789 /* Compute the preimage of "set" under the function represented by "pma".
14790 * In other words, plug in "pma" in "set". The result is a set
14791 * that lives in the domain space of "pma".
14793 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14794 __isl_take isl_pw_multi_aff
*pma
)
14796 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14799 /* Compute the preimage of the domain of "map" under the function
14800 * represented by "pma".
14801 * In other words, plug in "pma" in the domain of "map".
14802 * The result is a map that lives in the same space as "map",
14803 * except that domain space has been replaced by the domain space of "pma".
14805 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14806 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14808 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14811 /* Compute the preimage of the range of "map" under the function
14812 * represented by "pma".
14813 * In other words, plug in "pma" in the range of "map".
14814 * The result is a map that lives in the same space as "map",
14815 * except that range space has been replaced by the domain space of "pma".
14817 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14818 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14820 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14823 /* Compute the preimage of "map" under the function represented by "mpa".
14824 * In other words, plug in "mpa" in the domain or range of "map".
14825 * The result is a map that lives in the same space as "map",
14826 * except that the space of type "type" has been replaced by
14827 * the domain space of "mpa".
14829 * If the map does not involve any constraints that refer to the
14830 * dimensions of the substituted space, then the only possible
14831 * effect of "mpa" on the map is to map the space to a different space.
14832 * We create a separate isl_multi_aff to effectuate this change
14833 * in order to avoid spurious splitting of the map along the pieces
14835 * If "mpa" has a non-trivial explicit domain, however,
14836 * then the full substitution should be performed.
14838 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14839 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14843 isl_pw_multi_aff
*pma
;
14845 n
= isl_map_dim(map
, type
);
14849 full
= isl_map_involves_dims(map
, type
, 0, n
);
14850 if (full
>= 0 && !full
)
14851 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14858 space
= isl_multi_pw_aff_get_space(mpa
);
14859 isl_multi_pw_aff_free(mpa
);
14860 ma
= isl_multi_aff_zero(space
);
14861 return isl_map_preimage_multi_aff(map
, type
, ma
);
14864 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14865 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14868 isl_multi_pw_aff_free(mpa
);
14872 /* Compute the preimage of "map" under the function represented by "mpa".
14873 * In other words, plug in "mpa" in the domain "map".
14874 * The result is a map that lives in the same space as "map",
14875 * except that domain space has been replaced by the domain space of "mpa".
14877 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14878 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14880 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14883 /* Compute the preimage of "set" by the function represented by "mpa".
14884 * In other words, plug in "mpa" in "set".
14886 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14887 __isl_take isl_multi_pw_aff
*mpa
)
14889 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14892 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14893 * on the output dimension "pos" in terms of the parameters,
14894 * the input dimensions and possibly some integer divisions,
14895 * but not any other output dimensions, extract this upper bound
14896 * as a function of all dimensions (with zero coefficients
14897 * for the output dimensions).
14899 * That is, the inequality is of the form
14901 * e(...) + c - m x >= 0
14903 * where e does not depend on any other output dimensions.
14904 * Return (e(...) + c) / m, with the denominator m in the first position.
14906 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14907 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14910 isl_size v_out
, total
;
14912 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14913 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14914 if (v_out
< 0 || total
< 0)
14916 ctx
= isl_basic_map_get_ctx(bmap
);
14917 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14918 total
, v_out
+ pos
);
14921 /* Is constraint "c" of "bmap" of the form
14923 * e(...) + c1 - m x >= 0
14927 * -e(...) + c2 + m x >= 0
14929 * where m > 1 and e does not involve any other output variables?
14931 * "v_out" is the offset to the output variables.
14932 * "d" is the position of x among the output variables.
14933 * "v_div" is the offset to the local variables.
14934 * "total" is the total number of variables.
14936 * Since the purpose of this function is to use the constraint
14937 * to express the output variable as an integer division,
14938 * do not allow the constraint to involve any local variables
14939 * that do not have an explicit representation.
14941 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14942 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14946 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14947 return isl_bool_false
;
14948 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14949 return isl_bool_false
;
14950 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14951 return isl_bool_false
;
14952 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14953 return isl_bool_false
;
14954 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14955 v_div
- (v_out
+ d
+ 1)) != -1)
14956 return isl_bool_false
;
14957 for (i
= 0; v_div
+ i
< total
; ++i
) {
14958 isl_bool known
, involves
;
14960 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14962 known
= isl_basic_map_div_is_known(bmap
, i
);
14963 if (known
< 0 || !known
)
14965 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14966 if (involves
< 0 || involves
)
14967 return isl_bool_not(involves
);
14969 return isl_bool_true
;
14972 /* Look for a pair of constraints
14974 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14978 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14980 * that express that the output dimension x at position "pos"
14981 * is some integer division of an expression in terms of the parameters,
14982 * input dimensions and integer divisions.
14983 * If such a pair can be found, then return the index
14984 * of the upper bound constraint, m x <= e(...) + c1.
14985 * Otherwise, return an index beyond the number of constraints.
14987 * The constraints of "bmap" are assumed to have been sorted.
14989 * In order for the constraints above to express an integer division,
14990 * m needs to be greater than 1 and such that
14992 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14994 * In particular, this ensures that
14996 * x = floor((e(...) + c1) / m)
14998 isl_size
isl_basic_map_find_output_upper_div_constraint(
14999 __isl_keep isl_basic_map
*bmap
, int pos
)
15004 isl_size v_out
, v_div
;
15008 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
15009 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15010 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15011 n_ineq
= isl_basic_map_n_inequality(bmap
);
15012 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
15013 return isl_size_error
;
15016 for (i
= 0; i
< n_ineq
; ++i
) {
15017 isl_bool potential
;
15019 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
15020 v_out
, pos
, v_div
, total
);
15026 j
= find_later_constraint_in_pair(bmap
, i
, v_out
+ pos
, total
,
15027 bmap
->ineq
[i
][1 + v_out
+ pos
], &sum
);
15033 isl_int_clear(sum
);
15037 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
15042 isl_int_clear(sum
);
15043 return isl_size_error
;
15046 /* Look for a pair of constraints
15049 * g(x) - i + d >= 0
15051 * in "bmap" on the output dimension at position "pos" such that
15052 * g(x) is an expression in the parameters and input dimensions, and
15054 * Return the index of the first constraint if such a pair can be found.
15055 * Otherwise, return an index beyond the number of inequality constraints.
15057 * The constraints are assumed to have been sorted.
15059 static isl_size
find_output_lower_mod_constraint(__isl_keep isl_basic_map
*bmap
,
15060 int pos
, isl_int n
)
15064 isl_size v_out
, n_out
;
15065 isl_size v_div
, n_div
;
15069 n_ineq
= isl_basic_map_n_inequality(bmap
);
15070 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15071 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15072 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15073 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15074 if (n_ineq
< 0 || v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15075 return isl_size_error
;
15078 for (i
= n_ineq
- 1; i
>= 0; --i
) {
15079 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]) &&
15080 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15082 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_div
,
15085 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_out
, pos
) != -1)
15087 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_out
+ pos
+ 1,
15088 n_out
- (pos
+ 1)) != -1)
15090 j
= find_earlier_constraint_in_pair(bmap
, i
, v_out
+ pos
, v_div
,
15094 isl_int_clear(tmp
);
15097 if (isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15102 isl_int_clear(tmp
);
15106 /* Given that equality "eq" of "bset" expresses
15107 * the variable at position "pos" in terms of the other variables,
15108 * extract this expression as a function of those other variables,
15109 * excluding any local variables.
15111 static __isl_give isl_aff
*extract_expr_aff(
15112 __isl_keep isl_basic_set
*bset
, int eq
, int pos
)
15118 isl_local_space
*ls
;
15120 space
= isl_basic_set_peek_space(bset
);
15121 dim
= isl_space_dim(space
, isl_dim_all
);
15124 ctx
= isl_basic_set_get_ctx(bset
);
15125 v
= extract_bound_from_constraint(ctx
, bset
->eq
[eq
], dim
, pos
);
15126 ls
= isl_local_space_from_space(isl_space_copy(space
));
15127 return isl_aff_alloc_vec(ls
, v
);
15130 /* Given the presence in "bmap" of an equality constraint "eq"
15132 * f(x) + t m i + m n h(y) = 0
15134 * and a pair of inequality constraints
15136 * -g(x) + i >= 0 ("lower")
15137 * g(x) - i + d >= 0
15139 * where i is the output dimension at position "pos",
15140 * f(x) and g(x) are expressions in the parameters and
15141 * input dimensions (if any),
15142 * h(y) is an expression in the other output dimensions,
15143 * t is +1 or -1, and
15145 * return the expression
15147 * (-t f(x)/m - g(x)) mod n + g(x)
15149 * The expression is first constructed in terms of the wrapped space of "bmap".
15150 * Since this expression does not depend on the original output dimensions,
15151 * they can be removed, resulting in an expression in terms
15152 * of the input dimensions.
15153 * If "bmap" is actually a set, then the resulting expression has no domain.
15155 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15156 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15158 static __isl_give isl_aff
*construct_mod(__isl_keep isl_basic_map
*bmap
,
15159 int pos
, int eq
, int lower
, isl_int n
)
15162 isl_basic_set
*bset
;
15163 isl_aff
*f
, *g
, *mod
;
15167 is_set
= isl_basic_map_is_set(bmap
);
15170 ctx
= isl_basic_map_get_ctx(bmap
);
15173 bset
= bset_from_bmap(isl_basic_map_copy(bmap
));
15175 bset
= isl_basic_map_wrap(isl_basic_map_copy(bmap
));
15176 g
= extract_lower_bound_aff(bset
, lower
, pos
);
15177 f
= extract_expr_aff(bset
, eq
, pos
);
15178 isl_basic_set_free(bset
);
15180 mod
= isl_aff_sub(f
, isl_aff_copy(g
));
15181 v
= isl_val_int_from_isl_int(ctx
, n
);
15182 mod
= isl_aff_mod_val(mod
, v
);
15183 mod
= isl_aff_add(mod
, g
);
15185 mod
= isl_aff_project_domain_on_params(mod
);
15187 mod
= isl_aff_domain_factor_domain(mod
);
15192 /* Look for a combination of constraints in "bmap" that ensure
15193 * that output dimension "pos" is equal to some modulo expression
15194 * in the parameters and input dimensions and
15195 * return this expression if found.
15197 * The constraints are assumed to have been sorted.
15199 * In particular look for an equality constraint
15201 * f(x) + t m i + m n h(y) = 0
15203 * and a pair of inequality constraints
15206 * g(x) - i + d >= 0
15208 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15209 * h(y) is an expression in local variables and other output dimensions,
15210 * t is +1 or -1, and
15213 * If such a combination of constraints can be found
15216 * i = -t f(x)/m - t n h(y)
15217 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15219 * and so, because 0 <= i - g(x) <= d < n,
15221 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15222 * = (-t f(x)/m - g(x)) mod n
15226 * i = (-t f(x)/m - g(x)) mod n + g(x)
15228 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_mod(
15229 __isl_keep isl_basic_map
*bmap
, int pos
)
15232 isl_size n_eq
, n_ineq
;
15233 isl_size v_out
, n_out
;
15234 isl_size v_div
, n_div
;
15237 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15239 n_eq
= isl_basic_map_n_equality(bmap
);
15240 n_ineq
= isl_basic_map_n_inequality(bmap
);
15241 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15242 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15243 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15244 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15245 if (n_eq
< 0 || n_ineq
< 0 ||
15246 v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15248 if (n_eq
== 0 || n_ineq
< 2 || n_out
== 1)
15252 for (i
= 0; i
< n_eq
; ++i
) {
15253 if (isl_int_is_zero(bmap
->eq
[i
][1 + v_out
+ pos
]))
15255 isl_seq_gcd(bmap
->eq
[i
] + 1 + v_div
, n_div
, &gcd
);
15256 for (j
= 0; j
< n_out
; ++j
) {
15259 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ j
]);
15261 if (!isl_int_abs_gt(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15263 if (!isl_int_is_divisible_by(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15265 isl_int_divexact(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]);
15266 isl_int_abs(gcd
, gcd
);
15267 lower
= find_output_lower_mod_constraint(bmap
, pos
, gcd
);
15268 if (lower
>= n_ineq
)
15270 res
.valid
= isl_bool_true
;
15272 res
.value
= construct_mod(bmap
, v_out
+ pos
, i
,
15275 res
.valid
= isl_bool_error
;
15276 isl_int_clear(gcd
);
15280 isl_int_clear(gcd
);
15283 res
.valid
= isl_bool_error
;
15287 /* Construct an isl_aff from the given domain local space "ls" and
15288 * coefficients "v", where the local space may involve
15289 * local variables without a known expression, as long as these
15290 * do not have a non-zero coefficient in "v".
15291 * These need to be pruned away first since an isl_aff cannot
15292 * reference any local variables without a known expression.
15293 * For simplicity, remove all local variables that have a zero coefficient and
15294 * that are not used in other local variables with a non-zero coefficient.
15296 static __isl_give isl_aff
*isl_aff_alloc_vec_prune(
15297 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
15300 isl_size n_div
, v_div
;
15302 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
15303 v_div
= isl_local_space_var_offset(ls
, isl_dim_div
);
15304 if (n_div
< 0 || v_div
< 0 || !v
)
15306 for (i
= n_div
- 1; i
>= 0; --i
) {
15309 if (!isl_int_is_zero(v
->el
[1 + 1 + v_div
+ i
]))
15311 involves
= isl_local_space_involves_dims(ls
, isl_dim_div
, i
, 1);
15316 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
15317 v
= isl_vec_drop_els(v
, 1 + 1 + v_div
+ i
, 1);
15322 return isl_aff_alloc_vec(ls
, v
);
15324 isl_local_space_free(ls
);
15329 /* Look for a pair of constraints in "bmap" that ensure
15330 * that output dimension "pos" is equal to some integer division expression
15331 * in the parameters and input dimensions and
15332 * return this expression if found.
15334 * In particular, looks for a pair of constraints
15336 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
15340 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
15342 * where m > 1 and e only depends on parameters and input dimensions,
15345 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15347 * If such a pair of constraints can be found
15350 * x = floor((e(...) + c1) / m)
15352 * with e(...) an expression that does not involve any other output dimensions.
15354 * Note that we know that
15358 * If c1 + c2 were 0, then we would have detected an equality during
15359 * simplification. If c1 + c2 were negative, then we would have detected
15362 * The constraint defining the integer division is guaranteed not to involve
15363 * any local variables without a known expression, but such local variables
15364 * may appear in other constraints. They therefore need to be removed
15365 * during the construction of the affine expression.
15367 static __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div(
15368 __isl_keep isl_basic_map
*bmap
, int pos
)
15372 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15373 isl_local_space
*ls
;
15378 n_ineq
= isl_basic_map_n_inequality(bmap
);
15382 i
= isl_basic_map_find_output_upper_div_constraint(bmap
, pos
);
15388 is_set
= isl_basic_map_is_set(bmap
);
15390 bmap
= isl_basic_map_free(bmap
);
15392 ls
= isl_basic_map_get_local_space(bmap
);
15394 ls
= isl_local_space_wrap(ls
);
15395 v
= isl_basic_map_inequality_extract_output_upper_bound(bmap
, i
, pos
);
15397 aff
= isl_aff_alloc_vec_prune(ls
, v
);
15398 aff
= isl_aff_floor(aff
);
15401 aff
= isl_aff_project_domain_on_params(aff
);
15403 aff
= isl_aff_domain_factor_domain(aff
);
15405 res
.valid
= isl_bool_true
;
15409 res
.valid
= isl_bool_error
;
15413 /* Look for a combination of constraints in "bmap" that ensure
15414 * that output dimension "pos" is equal to some integer division or
15415 * modulo expression in the parameters and input dimensions and
15416 * return this expression if found.
15418 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div_mod(
15419 __isl_keep isl_basic_map
*bmap
, int pos
)
15421 isl_maybe_isl_aff div
;
15423 div
= isl_basic_map_try_find_output_div(bmap
, pos
);
15424 if (div
.valid
< 0 || div
.valid
)
15426 return isl_basic_map_try_find_output_mod(bmap
, pos
);
15429 /* Look for a combination of constraints in "bmap" that ensure
15430 * that any output dimension is equal to some integer division or
15431 * modulo expression in the parameters and input dimensions and
15432 * return this expression if found.
15433 * The position of the output dimension (if any) is returned in "pos".
15435 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_any_output_div_mod(
15436 __isl_keep isl_basic_map
*bmap
, int *pos
)
15439 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15441 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
15445 for (*pos
= 0; *pos
< dim
; ++*pos
) {
15446 res
= isl_basic_map_try_find_output_div_mod(bmap
, *pos
);
15447 if (res
.valid
< 0 || res
.valid
)
15453 res
.valid
= isl_bool_error
;
15457 /* Return a copy of the equality constraints of "bset" as a matrix.
15459 __isl_give isl_mat
*isl_basic_set_extract_equalities(
15460 __isl_keep isl_basic_set
*bset
)
15465 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15469 ctx
= isl_basic_set_get_ctx(bset
);
15470 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
15473 /* Are the "n" "coefficients" starting at "first" of the integer division
15474 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15476 * The "coefficient" at position 0 is the denominator.
15477 * The "coefficient" at position 1 is the constant term.
15479 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
15480 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
15481 unsigned first
, unsigned n
)
15483 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
15484 return isl_bool_error
;
15485 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
15486 return isl_bool_error
;
15487 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
15488 bmap2
->div
[pos2
] + first
, n
);
15491 /* Are the integer division expressions at position "pos1" in "bmap1" and
15492 * "pos2" in "bmap2" equal to each other, except that the constant terms
15495 isl_bool
isl_basic_map_equal_div_expr_except_constant(
15496 __isl_keep isl_basic_map
*bmap1
, int pos1
,
15497 __isl_keep isl_basic_map
*bmap2
, int pos2
)
15500 isl_size total
, total2
;
15502 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
15503 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
15504 if (total
< 0 || total2
< 0)
15505 return isl_bool_error
;
15506 if (total
!= total2
)
15507 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
15508 "incomparable div expressions", return isl_bool_error
);
15509 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15511 if (equal
< 0 || !equal
)
15513 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15515 if (equal
< 0 || equal
)
15516 return isl_bool_not(equal
);
15517 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15521 /* Replace the numerator of the constant term of the integer division
15522 * expression at position "div" in "bmap" by "value".
15523 * The caller guarantees that this does not change the meaning
15526 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
15527 __isl_take isl_basic_map
*bmap
, int div
, int value
)
15529 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
15530 return isl_basic_map_free(bmap
);
15532 isl_int_set_si(bmap
->div
[div
][1], value
);
15537 /* Is the point "inner" internal to inequality constraint "ineq"
15539 * The point is considered to be internal to the inequality constraint,
15540 * if it strictly lies on the positive side of the inequality constraint,
15541 * or if it lies on the constraint and the constraint is lexico-positive.
15543 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
15544 __isl_keep isl_basic_set
*bset
, int ineq
)
15550 if (!inner
|| !bset
)
15551 return isl_bool_error
;
15553 ctx
= isl_basic_set_get_ctx(bset
);
15554 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
15555 &ctx
->normalize_gcd
);
15556 if (!isl_int_is_zero(ctx
->normalize_gcd
))
15557 return isl_int_is_nonneg(ctx
->normalize_gcd
);
15559 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15561 return isl_bool_error
;
15562 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
15563 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
15566 /* Tighten the inequality constraints of "bset" that are outward with respect
15567 * to the point "vec".
15568 * That is, tighten the constraints that are not satisfied by "vec".
15570 * "vec" is a point internal to some superset S of "bset" that is used
15571 * to make the subsets of S disjoint, by tightening one half of the constraints
15572 * that separate two subsets. In particular, the constraints of S
15573 * are all satisfied by "vec" and should not be tightened.
15574 * Of the internal constraints, those that have "vec" on the outside
15575 * are tightened. The shared facet is included in the adjacent subset
15576 * with the opposite constraint.
15577 * For constraints that saturate "vec", this criterion cannot be used
15578 * to determine which of the two sides should be tightened.
15579 * Instead, the sign of the first non-zero coefficient is used
15580 * to make this choice. Note that this second criterion is never used
15581 * on the constraints of S since "vec" is interior to "S".
15583 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
15584 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
15588 bset
= isl_basic_set_cow(bset
);
15591 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
15594 internal
= is_internal(vec
, bset
, j
);
15596 return isl_basic_set_free(bset
);
15599 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
15605 /* Replace the variables x of type "type" starting at "first" in "bmap"
15606 * by x' with x = M x' with M the matrix trans.
15607 * That is, replace the corresponding coefficients c by c M.
15609 * The transformation matrix should be a square matrix.
15611 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
15612 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
15613 __isl_take isl_mat
*trans
)
15617 bmap
= isl_basic_map_cow(bmap
);
15618 if (!bmap
|| !trans
)
15621 if (trans
->n_row
!= trans
->n_col
)
15622 isl_die(trans
->ctx
, isl_error_invalid
,
15623 "expecting square transformation matrix", goto error
);
15624 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
15627 pos
= isl_basic_map_offset(bmap
, type
) + first
;
15629 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
15630 isl_mat_copy(trans
)) < 0)
15632 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
15633 isl_mat_copy(trans
)) < 0)
15635 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
15636 isl_mat_copy(trans
)) < 0)
15639 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
15640 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
15641 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
15643 isl_mat_free(trans
);
15646 isl_mat_free(trans
);
15647 isl_basic_map_free(bmap
);
15651 /* Replace the variables x of type "type" starting at "first" in "bset"
15652 * by x' with x = M x' with M the matrix trans.
15653 * That is, replace the corresponding coefficients c by c M.
15655 * The transformation matrix should be a square matrix.
15657 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
15658 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
15659 __isl_take isl_mat
*trans
)
15661 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);