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 -1 on error.
10282 static int 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)
10293 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
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 divs of "dst" to those of "src", adding divs from "src"
10303 * if needed. That is, make sure that the first src->n_div divs
10304 * of the result are equal to those of src.
10305 * The integer division of "src" are assumed to be ordered.
10307 * The integer divisions are swapped into the right position
10308 * (possibly after adding them first). This may result
10309 * in the remaining integer divisions appearing in the wrong order,
10310 * i.e., with some integer division appearing before
10311 * some other integer division on which it depends.
10312 * The integer divisions therefore need to be ordered.
10313 * This will not affect the integer divisions aligned to those of "src",
10314 * since "src" is assumed to have ordered integer divisions.
10316 * The result is not finalized as by design it will have redundant
10317 * divs if any divs from "src" were copied.
10319 __isl_give isl_basic_map
*isl_basic_map_align_divs(
10320 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
10326 isl_size dst_n_div
;
10329 return isl_basic_map_free(dst
);
10331 if (src
->n_div
== 0)
10334 known
= isl_basic_map_divs_known(src
);
10336 return isl_basic_map_free(dst
);
10338 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
10339 "some src divs are unknown",
10340 return isl_basic_map_free(dst
));
10342 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
10344 return isl_basic_map_free(dst
);
10347 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
10349 dst
= isl_basic_map_free(dst
);
10350 for (i
= 0; i
< src
->n_div
; ++i
) {
10351 int j
= find_div(dst
, src
, i
);
10353 dst
= isl_basic_map_free(dst
);
10354 if (j
== dst_n_div
) {
10356 int extra
= src
->n_div
- i
;
10357 dst
= isl_basic_map_cow(dst
);
10359 return isl_basic_map_free(dst
);
10360 dst
= isl_basic_map_extend(dst
,
10361 extra
, 0, 2 * extra
);
10364 j
= isl_basic_map_alloc_div(dst
);
10366 return isl_basic_map_free(dst
);
10367 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
10368 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
10370 dst
= isl_basic_map_add_div_constraints(dst
, j
);
10372 return isl_basic_map_free(dst
);
10375 dst
= isl_basic_map_swap_div(dst
, i
, j
);
10377 return isl_basic_map_free(dst
);
10379 return isl_basic_map_order_divs(dst
);
10382 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
10390 map
= isl_map_compute_divs(map
);
10391 map
= isl_map_order_divs(map
);
10392 map
= isl_map_cow(map
);
10396 for (i
= 1; i
< map
->n
; ++i
)
10397 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
10398 for (i
= 1; i
< map
->n
; ++i
) {
10399 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
10401 return isl_map_free(map
);
10404 map
= isl_map_unmark_normalized(map
);
10408 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
10410 return isl_map_align_divs_internal(map
);
10413 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
10415 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10418 /* Align the divs of the basic maps in "map" to those
10419 * of the basic maps in "list", as well as to the other basic maps in "map".
10420 * The elements in "list" are assumed to have known divs.
10422 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
10423 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
10428 n
= isl_basic_map_list_n_basic_map(list
);
10429 map
= isl_map_compute_divs(map
);
10430 map
= isl_map_cow(map
);
10432 return isl_map_free(map
);
10436 for (i
= 0; i
< n
; ++i
) {
10437 isl_basic_map
*bmap
;
10439 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
10440 bmap
= isl_basic_map_order_divs(bmap
);
10441 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
10442 isl_basic_map_free(bmap
);
10445 return isl_map_free(map
);
10447 return isl_map_align_divs_internal(map
);
10450 /* Align the divs of each element of "list" to those of "bmap".
10451 * Both "bmap" and the elements of "list" are assumed to have known divs.
10453 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
10454 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
10459 n
= isl_basic_map_list_n_basic_map(list
);
10460 if (n
< 0 || !bmap
)
10461 return isl_basic_map_list_free(list
);
10463 for (i
= 0; i
< n
; ++i
) {
10464 isl_basic_map
*bmap_i
;
10466 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10467 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
10468 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
10474 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
10475 __isl_take isl_map
*map
)
10479 isl_map_align_params_set(&map
, &set
);
10480 ok
= isl_map_compatible_domain(map
, set
);
10484 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
10485 "incompatible spaces", goto error
);
10486 map
= isl_map_intersect_domain(map
, set
);
10487 set
= isl_map_range(map
);
10495 /* There is no need to cow as removing empty parts doesn't change
10496 * the meaning of the set.
10498 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10505 for (i
= map
->n
- 1; i
>= 0; --i
)
10506 map
= remove_if_empty(map
, i
);
10511 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10513 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10516 /* Create a binary relation that maps the shared initial "pos" dimensions
10517 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10519 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10520 __isl_keep isl_basic_set
*bset2
, int pos
)
10522 isl_basic_map
*bmap1
;
10523 isl_basic_map
*bmap2
;
10525 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10526 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10527 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10528 isl_dim_out
, 0, pos
);
10529 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10530 isl_dim_out
, 0, pos
);
10531 return isl_basic_map_range_product(bmap1
, bmap2
);
10534 /* Given two basic sets bset1 and bset2, compute the maximal difference
10535 * between the values of dimension pos in bset1 and those in bset2
10536 * for any common value of the parameters and dimensions preceding pos.
10538 static enum isl_lp_result
basic_set_maximal_difference_at(
10539 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10540 int pos
, isl_int
*opt
)
10542 isl_basic_map
*bmap1
;
10543 struct isl_ctx
*ctx
;
10544 struct isl_vec
*obj
;
10548 enum isl_lp_result res
;
10550 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10551 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10552 if (nparam
< 0 || dim1
< 0 || !bset2
)
10553 return isl_lp_error
;
10555 bmap1
= join_initial(bset1
, bset2
, pos
);
10556 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10558 return isl_lp_error
;
10561 obj
= isl_vec_alloc(ctx
, 1 + total
);
10564 isl_seq_clr(obj
->block
.data
, 1 + total
);
10565 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10566 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10567 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10569 isl_basic_map_free(bmap1
);
10573 isl_basic_map_free(bmap1
);
10574 return isl_lp_error
;
10577 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10578 * for any common value of the parameters and dimensions preceding pos
10579 * in both basic sets, the values of dimension pos in bset1 are
10580 * smaller or larger than those in bset2.
10583 * 1 if bset1 follows bset2
10584 * -1 if bset1 precedes bset2
10585 * 0 if bset1 and bset2 are incomparable
10586 * -2 if some error occurred.
10588 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10589 __isl_keep isl_basic_set
*bset2
, int pos
)
10592 enum isl_lp_result res
;
10597 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10599 if (res
== isl_lp_empty
)
10601 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10602 res
== isl_lp_unbounded
)
10604 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10609 isl_int_clear(opt
);
10613 /* Given two basic sets bset1 and bset2, check whether
10614 * for any common value of the parameters and dimensions preceding pos
10615 * there is a value of dimension pos in bset1 that is larger
10616 * than a value of the same dimension in bset2.
10619 * 1 if there exists such a pair
10620 * 0 if there is no such pair, but there is a pair of equal values
10622 * -2 if some error occurred.
10624 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10625 __isl_keep isl_basic_set
*bset2
, int pos
)
10628 isl_basic_map
*bmap
;
10631 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10634 bmap
= join_initial(bset1
, bset2
, pos
);
10635 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10636 isl_dim_out
, dim1
- pos
);
10637 empty
= isl_basic_map_is_empty(bmap
);
10641 isl_basic_map_free(bmap
);
10644 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10645 isl_dim_out
, dim1
- pos
);
10646 empty
= isl_basic_map_is_empty(bmap
);
10649 isl_basic_map_free(bmap
);
10654 isl_basic_map_free(bmap
);
10658 /* Given two sets set1 and set2, check whether
10659 * for any common value of the parameters and dimensions preceding pos
10660 * there is a value of dimension pos in set1 that is larger
10661 * than a value of the same dimension in set2.
10664 * 1 if there exists such a pair
10665 * 0 if there is no such pair, but there is a pair of equal values
10667 * -2 if some error occurred.
10669 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10670 __isl_keep isl_set
*set2
, int pos
)
10675 if (!set1
|| !set2
)
10678 for (i
= 0; i
< set1
->n
; ++i
)
10679 for (j
= 0; j
< set2
->n
; ++j
) {
10681 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10682 if (f
== 1 || f
== -2)
10691 static isl_bool
isl_basic_map_plain_has_fixed_var(
10692 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10698 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10700 return isl_bool_error
;
10701 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10702 for (; d
+1 > pos
; --d
)
10703 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10707 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10708 return isl_bool_false
;
10709 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10710 return isl_bool_false
;
10711 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10712 return isl_bool_false
;
10714 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10715 return isl_bool_true
;
10717 return isl_bool_false
;
10720 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10721 unsigned pos
, isl_int
*val
)
10729 return isl_bool_error
;
10731 return isl_bool_false
;
10733 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10736 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10737 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10738 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10739 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10740 fixed
= isl_bool_false
;
10743 isl_int_set(*val
, v
);
10744 isl_int_clear(tmp
);
10749 static isl_bool
isl_basic_set_plain_has_fixed_var(
10750 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10752 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10756 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10757 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10759 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10760 return isl_bool_error
;
10761 return isl_basic_map_plain_has_fixed_var(bmap
,
10762 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10765 /* If "bmap" obviously lies on a hyperplane where the given dimension
10766 * has a fixed value, then return that value.
10767 * Otherwise return NaN.
10769 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10770 __isl_keep isl_basic_map
*bmap
,
10771 enum isl_dim_type type
, unsigned pos
)
10779 ctx
= isl_basic_map_get_ctx(bmap
);
10780 v
= isl_val_alloc(ctx
);
10783 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10785 return isl_val_free(v
);
10787 isl_int_set_si(v
->d
, 1);
10791 return isl_val_nan(ctx
);
10794 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10795 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10797 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10798 return isl_bool_error
;
10799 return isl_map_plain_has_fixed_var(map
,
10800 map_offset(map
, type
) - 1 + pos
, val
);
10803 /* If "map" obviously lies on a hyperplane where the given dimension
10804 * has a fixed value, then return that value.
10805 * Otherwise return NaN.
10807 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10808 enum isl_dim_type type
, unsigned pos
)
10816 ctx
= isl_map_get_ctx(map
);
10817 v
= isl_val_alloc(ctx
);
10820 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10822 return isl_val_free(v
);
10824 isl_int_set_si(v
->d
, 1);
10828 return isl_val_nan(ctx
);
10831 /* If "set" obviously lies on a hyperplane where the given dimension
10832 * has a fixed value, then return that value.
10833 * Otherwise return NaN.
10835 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10836 enum isl_dim_type type
, unsigned pos
)
10838 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10841 /* Return a sequence of values in the same space as "set"
10842 * that are equal to the corresponding set dimensions of "set"
10843 * for those set dimensions that obviously lie on a hyperplane
10844 * where the dimension has a fixed value.
10845 * The other elements are set to NaN.
10847 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10848 __isl_keep isl_set
*set
)
10855 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10856 mv
= isl_multi_val_alloc(space
);
10857 n
= isl_multi_val_size(mv
);
10859 return isl_multi_val_free(mv
);
10861 for (i
= 0; i
< n
; ++i
) {
10864 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10865 mv
= isl_multi_val_set_val(mv
, i
, v
);
10871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10872 * then return this fixed value in *val.
10874 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10875 unsigned dim
, isl_int
*val
)
10879 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10881 return isl_bool_error
;
10882 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10885 /* Return -1 if the constraint "c1" should be sorted before "c2"
10886 * and 1 if it should be sorted after "c2".
10887 * Return 0 if the two constraints are the same (up to the constant term).
10889 * In particular, if a constraint involves later variables than another
10890 * then it is sorted after this other constraint.
10891 * uset_gist depends on constraints without existentially quantified
10892 * variables sorting first.
10894 * For constraints that have the same latest variable, those
10895 * with the same coefficient for this latest variable (first in absolute value
10896 * and then in actual value) are grouped together.
10897 * This is useful for detecting pairs of constraints that can
10898 * be chained in their printed representation.
10900 * Finally, within a group, constraints are sorted according to
10901 * their coefficients (excluding the constant term).
10903 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10905 isl_int
**c1
= (isl_int
**) p1
;
10906 isl_int
**c2
= (isl_int
**) p2
;
10908 unsigned size
= *(unsigned *) arg
;
10911 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10912 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10917 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10920 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10924 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10927 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10928 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10929 * and 0 if the two constraints are the same (up to the constant term).
10931 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10932 isl_int
*c1
, isl_int
*c2
)
10937 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10941 return sort_constraint_cmp(&c1
, &c2
, &size
);
10944 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10945 __isl_take isl_basic_map
*bmap
)
10952 if (bmap
->n_ineq
== 0)
10954 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10956 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10958 return isl_basic_map_free(bmap
);
10960 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10961 &sort_constraint_cmp
, &size
) < 0)
10962 return isl_basic_map_free(bmap
);
10963 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10967 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10968 __isl_take isl_basic_set
*bset
)
10970 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10971 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10974 __isl_give isl_basic_map
*isl_basic_map_normalize(
10975 __isl_take isl_basic_map
*bmap
)
10977 bmap
= isl_basic_map_remove_redundancies(bmap
);
10978 bmap
= isl_basic_map_sort_constraints(bmap
);
10981 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10982 __isl_keep isl_basic_map
*bmap2
)
10986 isl_space
*space1
, *space2
;
10988 if (!bmap1
|| !bmap2
)
10991 if (bmap1
== bmap2
)
10993 space1
= isl_basic_map_peek_space(bmap1
);
10994 space2
= isl_basic_map_peek_space(bmap2
);
10995 cmp
= isl_space_cmp(space1
, space2
);
10998 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10999 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
11000 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
11001 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
11002 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11004 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
11006 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11008 if (bmap1
->n_eq
!= bmap2
->n_eq
)
11009 return bmap1
->n_eq
- bmap2
->n_eq
;
11010 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
11011 return bmap1
->n_ineq
- bmap2
->n_ineq
;
11012 if (bmap1
->n_div
!= bmap2
->n_div
)
11013 return bmap1
->n_div
- bmap2
->n_div
;
11014 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
11017 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
11018 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
11022 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
11023 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
11027 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
11028 isl_bool unknown1
, unknown2
;
11030 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
11031 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
11032 if (unknown1
< 0 || unknown2
< 0)
11034 if (unknown1
&& unknown2
)
11040 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
11047 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
11048 __isl_keep isl_basic_set
*bset2
)
11050 return isl_basic_map_plain_cmp(bset1
, bset2
);
11053 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
11059 if (set1
->n
!= set2
->n
)
11060 return set1
->n
- set2
->n
;
11062 for (i
= 0; i
< set1
->n
; ++i
) {
11063 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
11071 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
11072 __isl_keep isl_basic_map
*bmap2
)
11074 if (!bmap1
|| !bmap2
)
11075 return isl_bool_error
;
11076 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
11079 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
11080 __isl_keep isl_basic_set
*bset2
)
11082 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
11083 bset_to_bmap(bset2
));
11086 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
11088 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
11089 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
11091 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
11094 /* Sort the basic maps of "map" and remove duplicate basic maps.
11096 * While removing basic maps, we make sure that the basic maps remain
11097 * sorted because isl_map_normalize expects the basic maps of the result
11100 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
11104 map
= isl_map_remove_empty_parts(map
);
11107 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
11108 for (i
= map
->n
- 1; i
>= 1; --i
) {
11109 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
11111 isl_basic_map_free(map
->p
[i
-1]);
11112 for (j
= i
; j
< map
->n
; ++j
)
11113 map
->p
[j
- 1] = map
->p
[j
];
11120 /* Remove obvious duplicates among the basic maps of "map".
11122 * Unlike isl_map_normalize, this function does not remove redundant
11123 * constraints and only removes duplicates that have exactly the same
11124 * constraints in the input. It does sort the constraints and
11125 * the basic maps to ease the detection of duplicates.
11127 * If "map" has already been normalized or if the basic maps are
11128 * disjoint, then there can be no duplicates.
11130 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
11133 isl_basic_map
*bmap
;
11139 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
11141 for (i
= 0; i
< map
->n
; ++i
) {
11142 bmap
= isl_basic_map_copy(map
->p
[i
]);
11143 bmap
= isl_basic_map_sort_constraints(bmap
);
11145 return isl_map_free(map
);
11146 isl_basic_map_free(map
->p
[i
]);
11150 map
= sort_and_remove_duplicates(map
);
11154 /* We normalize in place, but if anything goes wrong we need
11155 * to return NULL, so we need to make sure we don't change the
11156 * meaning of any possible other copies of map.
11158 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
11161 struct isl_basic_map
*bmap
;
11165 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
11167 for (i
= 0; i
< map
->n
; ++i
) {
11168 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
11171 isl_basic_map_free(map
->p
[i
]);
11175 map
= sort_and_remove_duplicates(map
);
11177 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
11184 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
11186 return set_from_map(isl_map_normalize(set_to_map(set
)));
11189 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
11190 __isl_keep isl_map
*map2
)
11195 if (!map1
|| !map2
)
11196 return isl_bool_error
;
11199 return isl_bool_true
;
11200 equal
= isl_map_has_equal_space(map1
, map2
);
11201 if (equal
< 0 || !equal
)
11204 map1
= isl_map_copy(map1
);
11205 map2
= isl_map_copy(map2
);
11206 map1
= isl_map_normalize(map1
);
11207 map2
= isl_map_normalize(map2
);
11208 if (!map1
|| !map2
)
11210 equal
= map1
->n
== map2
->n
;
11211 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
11212 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
11216 isl_map_free(map1
);
11217 isl_map_free(map2
);
11220 isl_map_free(map1
);
11221 isl_map_free(map2
);
11222 return isl_bool_error
;
11225 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
11226 __isl_keep isl_set
*set2
)
11228 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
11231 /* Return the basic maps in "map" as a list.
11233 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
11234 __isl_keep isl_map
*map
)
11238 isl_basic_map_list
*list
;
11242 ctx
= isl_map_get_ctx(map
);
11243 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
11245 for (i
= 0; i
< map
->n
; ++i
) {
11246 isl_basic_map
*bmap
;
11248 bmap
= isl_basic_map_copy(map
->p
[i
]);
11249 list
= isl_basic_map_list_add(list
, bmap
);
11255 /* Return the intersection of the elements in the non-empty list "list".
11256 * All elements are assumed to live in the same space.
11258 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
11259 __isl_take isl_basic_map_list
*list
)
11263 isl_basic_map
*bmap
;
11265 n
= isl_basic_map_list_n_basic_map(list
);
11269 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
11270 "expecting non-empty list", goto error
);
11272 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
11273 for (i
= 1; i
< n
; ++i
) {
11274 isl_basic_map
*bmap_i
;
11276 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
11277 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
11280 isl_basic_map_list_free(list
);
11283 isl_basic_map_list_free(list
);
11287 /* Return the intersection of the elements in the non-empty list "list".
11288 * All elements are assumed to live in the same space.
11290 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
11291 __isl_take isl_basic_set_list
*list
)
11293 return isl_basic_map_list_intersect(list
);
11296 /* Return the union of the elements of "list".
11297 * The list is required to have at least one element.
11299 __isl_give isl_set
*isl_basic_set_list_union(
11300 __isl_take isl_basic_set_list
*list
)
11305 isl_basic_set
*bset
;
11308 n
= isl_basic_set_list_n_basic_set(list
);
11312 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
11313 "expecting non-empty list", goto error
);
11315 bset
= isl_basic_set_list_get_basic_set(list
, 0);
11316 space
= isl_basic_set_get_space(bset
);
11317 isl_basic_set_free(bset
);
11319 set
= isl_set_alloc_space(space
, n
, 0);
11320 for (i
= 0; i
< n
; ++i
) {
11321 bset
= isl_basic_set_list_get_basic_set(list
, i
);
11322 set
= isl_set_add_basic_set(set
, bset
);
11325 isl_basic_set_list_free(list
);
11328 isl_basic_set_list_free(list
);
11332 /* Return the union of the elements in the non-empty list "list".
11333 * All elements are assumed to live in the same space.
11335 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
11341 n
= isl_set_list_n_set(list
);
11345 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
11346 "expecting non-empty list", goto error
);
11348 set
= isl_set_list_get_set(list
, 0);
11349 for (i
= 1; i
< n
; ++i
) {
11352 set_i
= isl_set_list_get_set(list
, i
);
11353 set
= isl_set_union(set
, set_i
);
11356 isl_set_list_free(list
);
11359 isl_set_list_free(list
);
11363 __isl_give isl_basic_map
*isl_basic_map_product(
11364 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11366 isl_space
*space_result
= NULL
;
11367 struct isl_basic_map
*bmap
;
11368 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
11369 struct isl_dim_map
*dim_map1
, *dim_map2
;
11371 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11373 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
11374 isl_space_copy(bmap2
->dim
));
11376 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11377 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11378 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11379 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11380 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11382 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11383 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11384 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11385 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11386 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11387 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11388 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11389 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11390 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11391 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11392 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11394 bmap
= isl_basic_map_alloc_space(space_result
,
11395 bmap1
->n_div
+ bmap2
->n_div
,
11396 bmap1
->n_eq
+ bmap2
->n_eq
,
11397 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11398 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11399 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11400 bmap
= isl_basic_map_simplify(bmap
);
11401 return isl_basic_map_finalize(bmap
);
11403 isl_basic_map_free(bmap1
);
11404 isl_basic_map_free(bmap2
);
11408 __isl_give isl_basic_map
*isl_basic_map_flat_product(
11409 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11411 isl_basic_map
*prod
;
11413 prod
= isl_basic_map_product(bmap1
, bmap2
);
11414 prod
= isl_basic_map_flatten(prod
);
11418 __isl_give isl_basic_set
*isl_basic_set_flat_product(
11419 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
11421 return isl_basic_map_flat_range_product(bset1
, bset2
);
11424 __isl_give isl_basic_map
*isl_basic_map_domain_product(
11425 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11427 isl_space
*space1
, *space2
;
11428 isl_space
*space_result
= NULL
;
11429 isl_basic_map
*bmap
;
11430 isl_size in1
, in2
, out
, nparam
;
11431 unsigned total
, pos
;
11432 struct isl_dim_map
*dim_map1
, *dim_map2
;
11434 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11435 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11436 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11437 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11438 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
11441 space1
= isl_basic_map_get_space(bmap1
);
11442 space2
= isl_basic_map_get_space(bmap2
);
11443 space_result
= isl_space_domain_product(space1
, space2
);
11445 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
11446 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11447 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11448 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11449 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11450 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11451 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11452 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11453 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
11454 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
11455 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11457 bmap
= isl_basic_map_alloc_space(space_result
,
11458 bmap1
->n_div
+ bmap2
->n_div
,
11459 bmap1
->n_eq
+ bmap2
->n_eq
,
11460 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11461 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11462 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11463 bmap
= isl_basic_map_simplify(bmap
);
11464 return isl_basic_map_finalize(bmap
);
11466 isl_basic_map_free(bmap1
);
11467 isl_basic_map_free(bmap2
);
11471 __isl_give isl_basic_map
*isl_basic_map_range_product(
11472 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11475 isl_space
*space_result
= NULL
;
11476 isl_basic_map
*bmap
;
11477 isl_size in
, out1
, out2
, nparam
;
11478 unsigned total
, pos
;
11479 struct isl_dim_map
*dim_map1
, *dim_map2
;
11481 rational
= isl_basic_map_is_rational(bmap1
);
11482 if (rational
>= 0 && rational
)
11483 rational
= isl_basic_map_is_rational(bmap2
);
11484 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11485 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11486 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11487 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11488 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
11491 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11494 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
11495 isl_space_copy(bmap2
->dim
));
11497 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11498 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11499 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11500 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11501 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11502 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11503 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11504 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11505 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11506 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11507 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11509 bmap
= isl_basic_map_alloc_space(space_result
,
11510 bmap1
->n_div
+ bmap2
->n_div
,
11511 bmap1
->n_eq
+ bmap2
->n_eq
,
11512 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11513 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11514 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11516 bmap
= isl_basic_map_set_rational(bmap
);
11517 bmap
= isl_basic_map_simplify(bmap
);
11518 return isl_basic_map_finalize(bmap
);
11520 isl_basic_map_free(bmap1
);
11521 isl_basic_map_free(bmap2
);
11525 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11526 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11528 isl_basic_map
*prod
;
11530 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11531 prod
= isl_basic_map_flatten_range(prod
);
11535 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11536 * and collect the results.
11537 * The result live in the space obtained by calling "space_product"
11538 * on the spaces of "map1" and "map2".
11539 * If "remove_duplicates" is set then the result may contain duplicates
11540 * (even if the inputs do not) and so we try and remove the obvious
11543 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11544 __isl_take isl_map
*map2
,
11545 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11546 __isl_take isl_space
*right
),
11547 __isl_give isl_basic_map
*(*basic_map_product
)(
11548 __isl_take isl_basic_map
*left
,
11549 __isl_take isl_basic_map
*right
),
11550 int remove_duplicates
)
11552 unsigned flags
= 0;
11553 struct isl_map
*result
;
11557 m
= isl_map_has_equal_params(map1
, map2
);
11561 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11562 "parameters don't match", goto error
);
11564 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11565 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11566 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11568 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11569 isl_space_copy(map2
->dim
)),
11570 map1
->n
* map2
->n
, flags
);
11573 for (i
= 0; i
< map1
->n
; ++i
)
11574 for (j
= 0; j
< map2
->n
; ++j
) {
11575 struct isl_basic_map
*part
;
11576 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11577 isl_basic_map_copy(map2
->p
[j
]));
11578 if (isl_basic_map_is_empty(part
))
11579 isl_basic_map_free(part
);
11581 result
= isl_map_add_basic_map(result
, part
);
11585 if (remove_duplicates
)
11586 result
= isl_map_remove_obvious_duplicates(result
);
11587 isl_map_free(map1
);
11588 isl_map_free(map2
);
11591 isl_map_free(map1
);
11592 isl_map_free(map2
);
11596 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11598 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11599 __isl_take isl_map
*map2
)
11601 isl_map_align_params_bin(&map1
, &map2
);
11602 return map_product(map1
, map2
, &isl_space_product
,
11603 &isl_basic_map_product
, 0);
11606 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11608 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11609 __isl_take isl_map
*map2
)
11613 prod
= isl_map_product(map1
, map2
);
11614 prod
= isl_map_flatten(prod
);
11618 /* Given two set A and B, construct its Cartesian product A x B.
11620 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11621 __isl_take isl_set
*set2
)
11623 return isl_map_range_product(set1
, set2
);
11626 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11627 __isl_take isl_set
*set2
)
11629 return isl_map_flat_range_product(set1
, set2
);
11632 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11634 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11635 __isl_take isl_map
*map2
)
11637 isl_map_align_params_bin(&map1
, &map2
);
11638 return map_product(map1
, map2
, &isl_space_domain_product
,
11639 &isl_basic_map_domain_product
, 1);
11642 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11644 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11645 __isl_take isl_map
*map2
)
11647 isl_map_align_params_bin(&map1
, &map2
);
11648 return map_product(map1
, map2
, &isl_space_range_product
,
11649 &isl_basic_map_range_product
, 1);
11652 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11654 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11657 isl_size total1
, keep1
, total2
, keep2
;
11659 total1
= isl_map_dim(map
, isl_dim_in
);
11660 total2
= isl_map_dim(map
, isl_dim_out
);
11661 if (total1
< 0 || total2
< 0)
11662 return isl_map_free(map
);
11663 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11664 !isl_space_range_is_wrapping(map
->dim
))
11665 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11666 "not a product", return isl_map_free(map
));
11668 space
= isl_map_get_space(map
);
11669 space
= isl_space_factor_domain(space
);
11670 keep1
= isl_space_dim(space
, isl_dim_in
);
11671 keep2
= isl_space_dim(space
, isl_dim_out
);
11672 if (keep1
< 0 || keep2
< 0)
11673 map
= isl_map_free(map
);
11674 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11675 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11676 map
= isl_map_reset_space(map
, space
);
11681 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11683 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11686 isl_size total1
, keep1
, total2
, keep2
;
11688 total1
= isl_map_dim(map
, isl_dim_in
);
11689 total2
= isl_map_dim(map
, isl_dim_out
);
11690 if (total1
< 0 || total2
< 0)
11691 return isl_map_free(map
);
11692 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11693 !isl_space_range_is_wrapping(map
->dim
))
11694 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11695 "not a product", return isl_map_free(map
));
11697 space
= isl_map_get_space(map
);
11698 space
= isl_space_factor_range(space
);
11699 keep1
= isl_space_dim(space
, isl_dim_in
);
11700 keep2
= isl_space_dim(space
, isl_dim_out
);
11701 if (keep1
< 0 || keep2
< 0)
11702 map
= isl_map_free(map
);
11703 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11704 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11705 map
= isl_map_reset_space(map
, space
);
11710 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11712 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11715 isl_size total
, keep
;
11717 total
= isl_map_dim(map
, isl_dim_in
);
11719 return isl_map_free(map
);
11720 if (!isl_space_domain_is_wrapping(map
->dim
))
11721 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11722 "domain is not a product", return isl_map_free(map
));
11724 space
= isl_map_get_space(map
);
11725 space
= isl_space_domain_factor_domain(space
);
11726 keep
= isl_space_dim(space
, isl_dim_in
);
11728 map
= isl_map_free(map
);
11729 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11730 map
= isl_map_reset_space(map
, space
);
11735 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11737 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11740 isl_size total
, keep
;
11742 total
= isl_map_dim(map
, isl_dim_in
);
11744 return isl_map_free(map
);
11745 if (!isl_space_domain_is_wrapping(map
->dim
))
11746 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11747 "domain is not a product", return isl_map_free(map
));
11749 space
= isl_map_get_space(map
);
11750 space
= isl_space_domain_factor_range(space
);
11751 keep
= isl_space_dim(space
, isl_dim_in
);
11753 map
= isl_map_free(map
);
11754 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11755 map
= isl_map_reset_space(map
, space
);
11760 /* Given a map A -> [B -> C], extract the map A -> B.
11762 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11765 isl_size total
, keep
;
11767 total
= isl_map_dim(map
, isl_dim_out
);
11769 return isl_map_free(map
);
11770 if (!isl_space_range_is_wrapping(map
->dim
))
11771 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11772 "range is not a product", return isl_map_free(map
));
11774 space
= isl_map_get_space(map
);
11775 space
= isl_space_range_factor_domain(space
);
11776 keep
= isl_space_dim(space
, isl_dim_out
);
11778 map
= isl_map_free(map
);
11779 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11780 map
= isl_map_reset_space(map
, space
);
11785 /* Given a map A -> [B -> C], extract the map A -> C.
11787 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11790 isl_size total
, keep
;
11792 total
= isl_map_dim(map
, isl_dim_out
);
11794 return isl_map_free(map
);
11795 if (!isl_space_range_is_wrapping(map
->dim
))
11796 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11797 "range is not a product", return isl_map_free(map
));
11799 space
= isl_map_get_space(map
);
11800 space
= isl_space_range_factor_range(space
);
11801 keep
= isl_space_dim(space
, isl_dim_out
);
11803 map
= isl_map_free(map
);
11804 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11805 map
= isl_map_reset_space(map
, space
);
11810 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11812 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11813 __isl_take isl_map
*map2
)
11817 prod
= isl_map_domain_product(map1
, map2
);
11818 prod
= isl_map_flatten_domain(prod
);
11822 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11824 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11825 __isl_take isl_map
*map2
)
11829 prod
= isl_map_range_product(map1
, map2
);
11830 prod
= isl_map_flatten_range(prod
);
11834 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11837 uint32_t hash
= isl_hash_init();
11842 bmap
= isl_basic_map_copy(bmap
);
11843 bmap
= isl_basic_map_normalize(bmap
);
11844 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11847 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11848 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11850 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11851 isl_hash_hash(hash
, c_hash
);
11853 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11854 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11856 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11857 isl_hash_hash(hash
, c_hash
);
11859 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11860 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11862 if (isl_int_is_zero(bmap
->div
[i
][0]))
11864 isl_hash_byte(hash
, i
& 0xFF);
11865 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11866 isl_hash_hash(hash
, c_hash
);
11868 isl_basic_map_free(bmap
);
11872 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11874 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11877 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11884 map
= isl_map_copy(map
);
11885 map
= isl_map_normalize(map
);
11889 hash
= isl_hash_init();
11890 for (i
= 0; i
< map
->n
; ++i
) {
11891 uint32_t bmap_hash
;
11892 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11893 isl_hash_hash(hash
, bmap_hash
);
11901 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11903 return isl_map_get_hash(set_to_map(set
));
11906 /* Return the number of basic maps in the (current) representation of "map".
11908 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11910 return map
? map
->n
: isl_size_error
;
11913 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11915 return set
? set
->n
: isl_size_error
;
11918 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11919 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11924 return isl_stat_error
;
11926 for (i
= 0; i
< map
->n
; ++i
)
11927 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11928 return isl_stat_error
;
11930 return isl_stat_ok
;
11933 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11934 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11939 return isl_stat_error
;
11941 for (i
= 0; i
< set
->n
; ++i
)
11942 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11943 return isl_stat_error
;
11945 return isl_stat_ok
;
11948 /* Does "test" succeed on every basic set in "set"?
11950 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11951 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11957 return isl_bool_error
;
11959 for (i
= 0; i
< set
->n
; ++i
) {
11962 r
= test(set
->p
[i
], user
);
11967 return isl_bool_true
;
11970 /* Return a list of basic sets, the union of which is equal to "set".
11972 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11973 __isl_keep isl_set
*set
)
11976 isl_basic_set_list
*list
;
11981 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11982 for (i
= 0; i
< set
->n
; ++i
) {
11983 isl_basic_set
*bset
;
11985 bset
= isl_basic_set_copy(set
->p
[i
]);
11986 list
= isl_basic_set_list_add(list
, bset
);
11992 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11999 bset
= isl_basic_set_cow(bset
);
12003 space
= isl_basic_set_get_space(bset
);
12004 space
= isl_space_lift(space
, bset
->n_div
);
12007 isl_space_free(bset
->dim
);
12009 bset
->extra
-= bset
->n_div
;
12012 bset
= isl_basic_set_finalize(bset
);
12016 isl_basic_set_free(bset
);
12020 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
12026 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
12031 set
= isl_set_cow(set
);
12035 n_div
= set
->p
[0]->n_div
;
12036 space
= isl_set_get_space(set
);
12037 space
= isl_space_lift(space
, n_div
);
12040 isl_space_free(set
->dim
);
12043 for (i
= 0; i
< set
->n
; ++i
) {
12044 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
12055 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
12060 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
12063 size
+= bset
->n_eq
* (1 + dim
);
12064 size
+= bset
->n_ineq
* (1 + dim
);
12065 size
+= bset
->n_div
* (2 + dim
);
12070 int isl_set_size(__isl_keep isl_set
*set
)
12078 for (i
= 0; i
< set
->n
; ++i
)
12079 size
+= isl_basic_set_size(set
->p
[i
]);
12084 /* Check if there is any lower bound (if lower == 0) and/or upper
12085 * bound (if upper == 0) on the specified dim.
12087 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12088 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
12094 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
12095 return isl_bool_error
;
12097 off
= isl_basic_map_var_offset(bmap
, type
);
12099 return isl_bool_error
;
12100 involves
= isl_basic_map_any_div_involves_vars(bmap
, off
+ pos
, 1);
12101 if (involves
< 0 || involves
)
12104 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12105 if (!isl_int_is_zero(bmap
->eq
[i
][1 + off
+ pos
]))
12106 return isl_bool_true
;
12108 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12109 int sgn
= isl_int_sgn(bmap
->ineq
[i
][1 + off
+ pos
]);
12116 return lower
&& upper
;
12119 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12120 enum isl_dim_type type
, unsigned pos
)
12122 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
12125 isl_bool
isl_basic_map_dim_has_lower_bound(__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, 1);
12131 isl_bool
isl_basic_map_dim_has_upper_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
, 1, 0);
12137 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
12138 enum isl_dim_type type
, unsigned pos
)
12143 return isl_bool_error
;
12145 for (i
= 0; i
< map
->n
; ++i
) {
12147 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
12148 if (bounded
< 0 || !bounded
)
12152 return isl_bool_true
;
12155 /* Return true if the specified dim is involved in both an upper bound
12156 * and a lower bound.
12158 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
12159 enum isl_dim_type type
, unsigned pos
)
12161 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
12164 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12166 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
12167 enum isl_dim_type type
, unsigned pos
,
12168 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12169 enum isl_dim_type type
, unsigned pos
))
12174 return isl_bool_error
;
12176 for (i
= 0; i
< map
->n
; ++i
) {
12178 bounded
= fn(map
->p
[i
], type
, pos
);
12179 if (bounded
< 0 || bounded
)
12183 return isl_bool_false
;
12186 /* Return 1 if the specified dim is involved in any lower bound.
12188 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
12189 enum isl_dim_type type
, unsigned pos
)
12191 return has_any_bound(set
, type
, pos
,
12192 &isl_basic_map_dim_has_lower_bound
);
12195 /* Return 1 if the specified dim is involved in any upper bound.
12197 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
12198 enum isl_dim_type type
, unsigned pos
)
12200 return has_any_bound(set
, type
, pos
,
12201 &isl_basic_map_dim_has_upper_bound
);
12204 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12206 static isl_bool
has_bound(__isl_keep isl_map
*map
,
12207 enum isl_dim_type type
, unsigned pos
,
12208 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12209 enum isl_dim_type type
, unsigned pos
))
12214 return isl_bool_error
;
12216 for (i
= 0; i
< map
->n
; ++i
) {
12218 bounded
= fn(map
->p
[i
], type
, pos
);
12219 if (bounded
< 0 || !bounded
)
12223 return isl_bool_true
;
12226 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12228 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
12229 enum isl_dim_type type
, unsigned pos
)
12231 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
12234 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12236 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
12237 enum isl_dim_type type
, unsigned pos
)
12239 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
12242 /* For each of the "n" variables starting at "first", determine
12243 * the sign of the variable and put the results in the first "n"
12244 * elements of the array "signs".
12246 * 1 means that the variable is non-negative
12247 * -1 means that the variable is non-positive
12248 * 0 means the variable attains both positive and negative values.
12250 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
12251 unsigned first
, unsigned n
, int *signs
)
12253 isl_vec
*bound
= NULL
;
12254 struct isl_tab
*tab
= NULL
;
12255 struct isl_tab_undo
*snap
;
12259 total
= isl_basic_set_dim(bset
, isl_dim_all
);
12260 if (total
< 0 || !signs
)
12261 return isl_stat_error
;
12263 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
12264 tab
= isl_tab_from_basic_set(bset
, 0);
12265 if (!bound
|| !tab
)
12268 isl_seq_clr(bound
->el
, bound
->size
);
12269 isl_int_set_si(bound
->el
[0], -1);
12271 snap
= isl_tab_snap(tab
);
12272 for (i
= 0; i
< n
; ++i
) {
12275 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
12276 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12278 empty
= tab
->empty
;
12279 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12280 if (isl_tab_rollback(tab
, snap
) < 0)
12288 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
12289 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12291 empty
= tab
->empty
;
12292 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12293 if (isl_tab_rollback(tab
, snap
) < 0)
12296 signs
[i
] = empty
? -1 : 0;
12300 isl_vec_free(bound
);
12301 return isl_stat_ok
;
12304 isl_vec_free(bound
);
12305 return isl_stat_error
;
12308 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
12309 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
12311 if (!bset
|| !signs
)
12312 return isl_stat_error
;
12313 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
12314 return isl_stat_error
;
12316 first
+= pos(bset
->dim
, type
) - 1;
12317 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
12320 /* Is it possible for the integer division "div" to depend (possibly
12321 * indirectly) on any output dimensions?
12323 * If the div is undefined, then we conservatively assume that it
12324 * may depend on them.
12325 * Otherwise, we check if it actually depends on them or on any integer
12326 * divisions that may depend on them.
12328 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
12331 isl_size n_out
, n_div
;
12332 unsigned o_out
, o_div
;
12334 if (isl_int_is_zero(bmap
->div
[div
][0]))
12335 return isl_bool_true
;
12337 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12339 return isl_bool_error
;
12340 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12342 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
12343 return isl_bool_true
;
12345 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12347 return isl_bool_error
;
12348 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12350 for (i
= 0; i
< n_div
; ++i
) {
12351 isl_bool may_involve
;
12353 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
12355 may_involve
= div_may_involve_output(bmap
, i
);
12356 if (may_involve
< 0 || may_involve
)
12357 return may_involve
;
12360 return isl_bool_false
;
12363 /* Return the first integer division of "bmap" in the range
12364 * [first, first + n[ that may depend on any output dimensions and
12365 * that has a non-zero coefficient in "c" (where the first coefficient
12366 * in "c" corresponds to integer division "first").
12368 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
12369 isl_int
*c
, int first
, int n
)
12376 for (k
= first
; k
< first
+ n
; ++k
) {
12377 isl_bool may_involve
;
12379 if (isl_int_is_zero(c
[k
]))
12381 may_involve
= div_may_involve_output(bmap
, k
);
12382 if (may_involve
< 0)
12391 /* Look for a pair of inequality constraints in "bmap" of the form
12393 * -l + i >= 0 or i >= l
12395 * n + l - i >= 0 or i <= l + n
12397 * with n < "m" and i the output dimension at position "pos".
12398 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12399 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12400 * and earlier output dimensions, as well as integer divisions that do
12401 * not involve any of the output dimensions.
12403 * Return the index of the first inequality constraint or bmap->n_ineq
12404 * if no such pair can be found.
12406 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
12407 int pos
, isl_int m
)
12412 isl_size n_div
, n_out
;
12413 unsigned o_div
, o_out
;
12416 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12417 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12418 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12419 if (total
< 0 || n_out
< 0 || n_div
< 0)
12422 ctx
= isl_basic_map_get_ctx(bmap
);
12423 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12424 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12425 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12426 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
12428 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
12429 n_out
- (pos
+ 1)) != -1)
12431 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
12434 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
12435 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
12438 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
12439 bmap
->ineq
[j
] + 1, total
))
12443 if (j
>= bmap
->n_ineq
)
12445 isl_int_add(bmap
->ineq
[i
][0],
12446 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12447 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
12448 isl_int_sub(bmap
->ineq
[i
][0],
12449 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12452 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
12458 return bmap
->n_ineq
;
12461 /* Return the index of the equality of "bmap" that defines
12462 * the output dimension "pos" in terms of earlier dimensions.
12463 * The equality may also involve integer divisions, as long
12464 * as those integer divisions are defined in terms of
12465 * parameters or input dimensions.
12466 * In this case, *div is set to the number of integer divisions and
12467 * *ineq is set to the number of inequality constraints (provided
12468 * div and ineq are not NULL).
12470 * The equality may also involve a single integer division involving
12471 * the output dimensions (typically only output dimension "pos") as
12472 * long as the coefficient of output dimension "pos" is 1 or -1 and
12473 * there is a pair of constraints i >= l and i <= l + n, with i referring
12474 * to output dimension "pos", l an expression involving only earlier
12475 * dimensions and n smaller than the coefficient of the integer division
12476 * in the equality. In this case, the output dimension can be defined
12477 * in terms of a modulo expression that does not involve the integer division.
12478 * *div is then set to this single integer division and
12479 * *ineq is set to the index of constraint i >= l.
12481 * Return bmap->n_eq if there is no such equality.
12482 * Return -1 on error.
12484 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
12485 int pos
, int *div
, int *ineq
)
12488 isl_size n_div
, n_out
;
12489 unsigned o_div
, o_out
;
12491 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12492 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12493 if (n_out
< 0 || n_div
< 0)
12496 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12497 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12500 *ineq
= bmap
->n_ineq
;
12503 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12504 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12506 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12507 n_out
- (pos
+ 1)) != -1)
12509 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12513 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12514 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12516 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12517 k
+ 1, n_div
- (k
+1)) < n_div
)
12519 l
= find_modulo_constraint_pair(bmap
, pos
,
12520 bmap
->eq
[j
][o_div
+ k
]);
12523 if (l
>= bmap
->n_ineq
)
12535 /* Check if the given basic map is obviously single-valued.
12536 * In particular, for each output dimension, check that there is
12537 * an equality that defines the output dimension in terms of
12538 * earlier dimensions.
12540 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12545 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12547 return isl_bool_error
;
12549 for (i
= 0; i
< n_out
; ++i
) {
12552 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12555 return isl_bool_error
;
12556 if (eq
>= bmap
->n_eq
)
12557 return isl_bool_false
;
12560 return isl_bool_true
;
12563 /* Check if the given basic map is single-valued.
12564 * We simply compute
12568 * and check if the result is a subset of the identity mapping.
12570 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12573 isl_basic_map
*test
;
12577 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12581 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12582 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12584 space
= isl_basic_map_get_space(bmap
);
12585 space
= isl_space_map_from_set(isl_space_range(space
));
12586 id
= isl_basic_map_identity(space
);
12588 sv
= isl_basic_map_is_subset(test
, id
);
12590 isl_basic_map_free(test
);
12591 isl_basic_map_free(id
);
12596 /* Check if the given map is obviously single-valued.
12598 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12601 return isl_bool_error
;
12603 return isl_bool_true
;
12605 return isl_bool_false
;
12607 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12610 /* Check if the given map is single-valued.
12611 * We simply compute
12615 * and check if the result is a subset of the identity mapping.
12617 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12624 sv
= isl_map_plain_is_single_valued(map
);
12628 test
= isl_map_reverse(isl_map_copy(map
));
12629 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12631 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12632 id
= isl_map_identity(space
);
12634 sv
= isl_map_is_subset(test
, id
);
12636 isl_map_free(test
);
12642 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12646 map
= isl_map_copy(map
);
12647 map
= isl_map_reverse(map
);
12648 in
= isl_map_is_single_valued(map
);
12654 /* Check if the given map is obviously injective.
12656 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12660 map
= isl_map_copy(map
);
12661 map
= isl_map_reverse(map
);
12662 in
= isl_map_plain_is_single_valued(map
);
12668 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12672 sv
= isl_map_is_single_valued(map
);
12676 return isl_map_is_injective(map
);
12679 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12681 return isl_map_is_single_valued(set_to_map(set
));
12684 /* Does "map" only map elements to themselves?
12686 * If the domain and range spaces are different, then "map"
12687 * is considered not to be an identity relation, even if it is empty.
12688 * Otherwise, construct the maximal identity relation and
12689 * check whether "map" is a subset of this relation.
12691 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12694 isl_bool equal
, is_identity
;
12696 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12697 if (equal
< 0 || !equal
)
12700 id
= isl_map_identity(isl_map_get_space(map
));
12701 is_identity
= isl_map_is_subset(map
, id
);
12704 return is_identity
;
12707 int isl_map_is_translation(__isl_keep isl_map
*map
)
12712 delta
= isl_map_deltas(isl_map_copy(map
));
12713 ok
= isl_set_is_singleton(delta
);
12714 isl_set_free(delta
);
12719 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12721 if (isl_seq_first_non_zero(p
, pos
) != -1)
12723 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12728 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12731 isl_size nvar
, ovar
, n_div
;
12733 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12735 return isl_bool_error
;
12737 return isl_bool_false
;
12739 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12740 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12741 if (nvar
< 0 || ovar
< 0)
12742 return isl_bool_error
;
12743 for (j
= 0; j
< nvar
; ++j
) {
12744 int lower
= 0, upper
= 0;
12745 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12746 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12748 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12749 return isl_bool_false
;
12752 if (i
< bset
->n_eq
)
12754 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12755 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12757 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12758 return isl_bool_false
;
12759 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12764 if (!lower
|| !upper
)
12765 return isl_bool_false
;
12768 return isl_bool_true
;
12771 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12774 return isl_bool_error
;
12776 return isl_bool_false
;
12778 return isl_basic_set_is_box(set
->p
[0]);
12781 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12784 return isl_bool_error
;
12786 return isl_space_is_wrapping(bset
->dim
);
12789 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12792 return isl_bool_error
;
12794 return isl_space_is_wrapping(set
->dim
);
12797 /* Modify the space of "map" through a call to "change".
12798 * If "can_change" is set (not NULL), then first call it to check
12799 * if the modification is allowed, printing the error message "cannot_change"
12802 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12803 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12804 const char *cannot_change
,
12805 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12813 ok
= can_change
? can_change(map
) : isl_bool_true
;
12815 return isl_map_free(map
);
12817 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12818 return isl_map_free(map
));
12820 space
= change(isl_map_get_space(map
));
12821 map
= isl_map_reset_space(map
, space
);
12826 /* Is the domain of "map" a wrapped relation?
12828 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12831 return isl_bool_error
;
12833 return isl_space_domain_is_wrapping(map
->dim
);
12836 /* Does "map" have a wrapped relation in both domain and range?
12838 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12840 return isl_space_is_product(isl_map_peek_space(map
));
12843 /* Is the range of "map" a wrapped relation?
12845 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12848 return isl_bool_error
;
12850 return isl_space_range_is_wrapping(map
->dim
);
12853 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12857 space
= isl_basic_map_take_space(bmap
);
12858 space
= isl_space_wrap(space
);
12859 bmap
= isl_basic_map_restore_space(bmap
, space
);
12861 bmap
= isl_basic_map_finalize(bmap
);
12863 return bset_from_bmap(bmap
);
12866 /* Given a map A -> B, return the set (A -> B).
12868 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12870 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12873 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12875 bset
= isl_basic_set_cow(bset
);
12879 bset
->dim
= isl_space_unwrap(bset
->dim
);
12883 bset
= isl_basic_set_finalize(bset
);
12885 return bset_to_bmap(bset
);
12887 isl_basic_set_free(bset
);
12891 /* Given a set (A -> B), return the map A -> B.
12892 * Error out if "set" is not of the form (A -> B).
12894 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12896 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12897 "not a wrapping set", &isl_space_unwrap
);
12900 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12901 enum isl_dim_type type
)
12905 space
= isl_basic_map_take_space(bmap
);
12906 space
= isl_space_reset(space
, type
);
12907 bmap
= isl_basic_map_restore_space(bmap
, space
);
12909 bmap
= isl_basic_map_mark_final(bmap
);
12914 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12915 enum isl_dim_type type
)
12923 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12926 map
= isl_map_cow(map
);
12930 for (i
= 0; i
< map
->n
; ++i
) {
12931 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12936 space
= isl_map_take_space(map
);
12937 space
= isl_space_reset(space
, type
);
12938 map
= isl_map_restore_space(map
, space
);
12946 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12950 space
= isl_basic_map_take_space(bmap
);
12951 space
= isl_space_flatten(space
);
12952 bmap
= isl_basic_map_restore_space(bmap
, space
);
12954 bmap
= isl_basic_map_mark_final(bmap
);
12959 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12961 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12964 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12965 __isl_take isl_basic_map
*bmap
)
12969 space
= isl_basic_map_take_space(bmap
);
12970 space
= isl_space_flatten_domain(space
);
12971 bmap
= isl_basic_map_restore_space(bmap
, space
);
12973 bmap
= isl_basic_map_mark_final(bmap
);
12978 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12979 __isl_take isl_basic_map
*bmap
)
12983 space
= isl_basic_map_take_space(bmap
);
12984 space
= isl_space_flatten_range(space
);
12985 bmap
= isl_basic_map_restore_space(bmap
, space
);
12987 bmap
= isl_basic_map_mark_final(bmap
);
12992 /* Remove any internal structure from the spaces of domain and range of "map".
12994 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12999 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
13002 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
13005 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
13007 return set_from_map(isl_map_flatten(set_to_map(set
)));
13010 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
13012 isl_space
*space
, *flat_space
;
13015 space
= isl_set_get_space(set
);
13016 flat_space
= isl_space_flatten(isl_space_copy(space
));
13017 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
13019 map
= isl_map_intersect_domain(map
, set
);
13024 /* Remove any internal structure from the space of the domain of "map".
13026 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
13031 if (!map
->dim
->nested
[0])
13034 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
13037 /* Remove any internal structure from the space of the range of "map".
13039 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
13044 if (!map
->dim
->nested
[1])
13047 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
13050 /* Reorder the dimensions of "bmap" according to the given dim_map
13051 * and set the dimension specification to "space" and
13052 * perform Gaussian elimination on the result.
13054 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
13055 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
13057 isl_basic_map
*res
;
13061 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
13062 if (n_div
< 0 || !space
|| !dim_map
)
13065 flags
= bmap
->flags
;
13066 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
13067 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
13068 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13069 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
13070 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
13072 res
->flags
= flags
;
13073 res
= isl_basic_map_gauss(res
, NULL
);
13074 res
= isl_basic_map_finalize(res
);
13077 isl_dim_map_free(dim_map
);
13078 isl_basic_map_free(bmap
);
13079 isl_space_free(space
);
13083 /* Reorder the dimensions of "map" according to given reordering.
13085 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
13086 __isl_take isl_reordering
*r
)
13089 struct isl_dim_map
*dim_map
;
13091 map
= isl_map_cow(map
);
13092 dim_map
= isl_dim_map_from_reordering(r
);
13093 if (!map
|| !r
|| !dim_map
)
13096 for (i
= 0; i
< map
->n
; ++i
) {
13097 struct isl_dim_map
*dim_map_i
;
13100 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
13102 space
= isl_reordering_get_space(r
);
13103 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
13109 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
13110 map
= isl_map_unmark_normalized(map
);
13112 isl_reordering_free(r
);
13113 isl_dim_map_free(dim_map
);
13116 isl_dim_map_free(dim_map
);
13118 isl_reordering_free(r
);
13122 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
13123 __isl_take isl_reordering
*r
)
13125 return set_from_map(isl_map_realign(set_to_map(set
), r
));
13128 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
13129 __isl_take isl_space
*model
)
13134 if (!map
|| !model
)
13137 ctx
= isl_space_get_ctx(model
);
13138 if (!isl_space_has_named_params(model
))
13139 isl_die(ctx
, isl_error_invalid
,
13140 "model has unnamed parameters", goto error
);
13141 if (isl_map_check_named_params(map
) < 0)
13143 aligned
= isl_map_space_has_equal_params(map
, model
);
13148 isl_reordering
*exp
;
13150 space
= isl_map_peek_space(map
);
13151 exp
= isl_parameter_alignment_reordering(space
, model
);
13152 map
= isl_map_realign(map
, exp
);
13155 isl_space_free(model
);
13158 isl_space_free(model
);
13163 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
13164 __isl_take isl_space
*model
)
13166 return isl_map_align_params(set
, model
);
13169 /* Align the parameters of "bmap" to those of "model", introducing
13170 * additional parameters if needed.
13172 __isl_give isl_basic_map
*isl_basic_map_align_params(
13173 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
13176 isl_bool equal_params
;
13177 isl_space
*bmap_space
;
13179 if (!bmap
|| !model
)
13182 ctx
= isl_space_get_ctx(model
);
13183 if (!isl_space_has_named_params(model
))
13184 isl_die(ctx
, isl_error_invalid
,
13185 "model has unnamed parameters", goto error
);
13186 if (isl_basic_map_check_named_params(bmap
) < 0)
13188 bmap_space
= isl_basic_map_peek_space(bmap
);
13189 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
13190 if (equal_params
< 0)
13192 if (!equal_params
) {
13193 isl_reordering
*exp
;
13194 struct isl_dim_map
*dim_map
;
13196 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
13197 dim_map
= isl_dim_map_from_reordering(exp
);
13198 bmap
= isl_basic_map_realign(bmap
,
13199 isl_reordering_get_space(exp
),
13200 isl_dim_map_extend(dim_map
, bmap
));
13201 isl_reordering_free(exp
);
13202 isl_dim_map_free(dim_map
);
13205 isl_space_free(model
);
13208 isl_space_free(model
);
13209 isl_basic_map_free(bmap
);
13213 /* Do "bset" and "space" have the same parameters?
13215 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
13216 __isl_keep isl_space
*space
)
13218 isl_space
*bset_space
;
13220 bset_space
= isl_basic_set_peek_space(bset
);
13221 return isl_space_has_equal_params(bset_space
, space
);
13224 /* Do "map" and "space" have the same parameters?
13226 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
13227 __isl_keep isl_space
*space
)
13229 isl_space
*map_space
;
13231 map_space
= isl_map_peek_space(map
);
13232 return isl_space_has_equal_params(map_space
, space
);
13235 /* Do "set" and "space" have the same parameters?
13237 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
13238 __isl_keep isl_space
*space
)
13240 return isl_map_space_has_equal_params(set_to_map(set
), space
);
13243 /* Align the parameters of "bset" to those of "model", introducing
13244 * additional parameters if needed.
13246 __isl_give isl_basic_set
*isl_basic_set_align_params(
13247 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
13249 return isl_basic_map_align_params(bset
, model
);
13253 #define TYPE isl_map
13254 #define isl_map_drop_dims isl_map_drop
13255 #include "isl_drop_unused_params_templ.c"
13257 /* Drop all parameters not referenced by "set".
13259 __isl_give isl_set
*isl_set_drop_unused_params(
13260 __isl_take isl_set
*set
)
13262 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
13266 #define TYPE isl_basic_map
13267 #define isl_basic_map_drop_dims isl_basic_map_drop
13268 #include "isl_drop_unused_params_templ.c"
13270 /* Drop all parameters not referenced by "bset".
13272 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
13273 __isl_take isl_basic_set
*bset
)
13275 return bset_from_bmap(isl_basic_map_drop_unused_params(
13276 bset_to_bmap(bset
)));
13279 /* Given a tuple of identifiers "tuple" in a space that corresponds
13280 * to that of "set", if any of those identifiers appear as parameters
13281 * in "set", then equate those parameters with the corresponding
13282 * set dimensions and project out the parameters.
13283 * The result therefore has no such parameters.
13285 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
13286 __isl_keep isl_multi_id
*tuple
)
13290 isl_space
*set_space
, *tuple_space
;
13292 set_space
= isl_set_peek_space(set
);
13293 tuple_space
= isl_multi_id_peek_space(tuple
);
13294 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
13295 return isl_set_free(set
);
13296 n
= isl_multi_id_size(tuple
);
13298 return isl_set_free(set
);
13299 for (i
= 0; i
< n
; ++i
) {
13303 id
= isl_multi_id_get_at(tuple
, i
);
13305 return isl_set_free(set
);
13306 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
13310 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
13311 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
13316 /* Bind the set dimensions of "set" to parameters with identifiers
13317 * specified by "tuple", living in the same space as "set".
13319 * If no parameters with these identifiers appear in "set" already,
13320 * then the set dimensions are simply reinterpreted as parameters.
13321 * Otherwise, the parameters are first equated to the corresponding
13324 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
13325 __isl_take isl_multi_id
*tuple
)
13329 set
= equate_params(set
, tuple
);
13330 space
= isl_set_get_space(set
);
13331 space
= isl_space_bind_set(space
, tuple
);
13332 isl_multi_id_free(tuple
);
13333 set
= isl_set_reset_space(set
, space
);
13338 /* Given a tuple of identifiers "tuple" in a space that corresponds
13339 * to the domain of "map", if any of those identifiers appear as parameters
13340 * in "map", then equate those parameters with the corresponding
13341 * input dimensions and project out the parameters.
13342 * The result therefore has no such parameters.
13344 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
13345 __isl_keep isl_multi_id
*tuple
)
13349 isl_space
*map_space
, *tuple_space
;
13351 map_space
= isl_map_peek_space(map
);
13352 tuple_space
= isl_multi_id_peek_space(tuple
);
13353 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
13354 return isl_map_free(map
);
13355 n
= isl_multi_id_size(tuple
);
13357 return isl_map_free(map
);
13358 for (i
= 0; i
< n
; ++i
) {
13362 id
= isl_multi_id_get_at(tuple
, i
);
13364 return isl_map_free(map
);
13365 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
13369 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
13370 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
13375 /* Bind the input dimensions of "map" to parameters with identifiers
13376 * specified by "tuple", living in the domain space of "map".
13378 * If no parameters with these identifiers appear in "map" already,
13379 * then the input dimensions are simply reinterpreted as parameters.
13380 * Otherwise, the parameters are first equated to the corresponding
13381 * input dimensions.
13383 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
13384 __isl_take isl_multi_id
*tuple
)
13389 map
= map_equate_params(map
, tuple
);
13390 space
= isl_map_get_space(map
);
13391 space
= isl_space_bind_map_domain(space
, tuple
);
13392 isl_multi_id_free(tuple
);
13393 set
= set_from_map(isl_map_reset_space(map
, space
));
13398 /* Bind the output dimensions of "map" to parameters with identifiers
13399 * specified by "tuple", living in the range space of "map".
13401 * Since binding is more easily implemented on the domain,
13402 * bind the input dimensions of the inverse of "map".
13404 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
13405 __isl_take isl_multi_id
*tuple
)
13407 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
13410 /* Insert a domain corresponding to "tuple"
13411 * into the nullary or unary relation "set".
13412 * The result has an extra initial tuple and is therefore
13413 * either a unary or binary relation.
13414 * Any parameters with identifiers in "tuple" are reinterpreted
13415 * as the corresponding domain dimensions.
13417 static __isl_give isl_map
*unbind_params_insert_domain(
13418 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
13423 space
= isl_set_peek_space(set
);
13424 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
13425 isl_multi_id_free(tuple
);
13427 return isl_map_realign(set_to_map(set
), r
);
13430 /* Construct a set with "tuple" as domain from the parameter domain "set".
13431 * Any parameters with identifiers in "tuple" are reinterpreted
13432 * as the corresponding set dimensions.
13434 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
13435 __isl_take isl_multi_id
*tuple
)
13437 isl_bool is_params
;
13439 is_params
= isl_set_is_params(set
);
13441 set
= isl_set_free(set
);
13442 else if (!is_params
)
13443 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13444 "expecting parameter domain", set
= isl_set_free(set
));
13445 return set_from_map(unbind_params_insert_domain(set
, tuple
));
13448 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13450 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
13452 isl_bool is_params
;
13454 is_params
= isl_set_is_params(set
);
13456 return isl_stat_error
;
13457 else if (is_params
)
13458 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13459 "expecting proper set", return isl_stat_error
);
13461 return isl_stat_ok
;
13464 /* Construct a map with "domain" as domain and "set" as range.
13465 * Any parameters with identifiers in "domain" are reinterpreted
13466 * as the corresponding domain dimensions.
13468 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
13469 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
13471 if (isl_set_check_is_set(set
) < 0)
13472 set
= isl_set_free(set
);
13473 return unbind_params_insert_domain(set
, domain
);
13476 /* Construct a map with "domain" as domain and "set" as range.
13478 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
13479 __isl_take isl_space
*domain
)
13485 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
13486 domain
= isl_space_free(domain
);
13487 dim
= isl_space_dim(domain
, isl_dim_set
);
13489 domain
= isl_space_free(domain
);
13490 space
= isl_set_get_space(set
);
13491 domain
= isl_space_replace_params(domain
, space
);
13492 space
= isl_space_map_from_domain_and_range(domain
, space
);
13494 map
= isl_map_from_range(set
);
13495 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
13496 map
= isl_map_reset_space(map
, space
);
13501 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13502 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13503 enum isl_dim_type c2
, enum isl_dim_type c3
,
13504 enum isl_dim_type c4
, enum isl_dim_type c5
)
13506 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13507 struct isl_mat
*mat
;
13512 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13515 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13518 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13519 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13520 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13521 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13523 return isl_mat_free(mat
);
13524 for (k
= 0; k
< dim
; ++k
) {
13525 isl_int_set(mat
->row
[i
][pos
],
13526 bmap
->eq
[i
][off
+ k
]);
13534 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13535 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13536 enum isl_dim_type c2
, enum isl_dim_type c3
,
13537 enum isl_dim_type c4
, enum isl_dim_type c5
)
13539 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13540 struct isl_mat
*mat
;
13545 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13548 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13551 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13552 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13553 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13554 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13556 return isl_mat_free(mat
);
13557 for (k
= 0; k
< dim
; ++k
) {
13558 isl_int_set(mat
->row
[i
][pos
],
13559 bmap
->ineq
[i
][off
+ k
]);
13567 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13568 __isl_take isl_space
*space
,
13569 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13570 enum isl_dim_type c2
, enum isl_dim_type c3
,
13571 enum isl_dim_type c4
, enum isl_dim_type c5
)
13573 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13574 isl_basic_map
*bmap
= NULL
;
13581 dim
= isl_space_dim(space
, isl_dim_all
);
13582 if (dim
< 0 || !eq
|| !ineq
)
13585 if (eq
->n_col
!= ineq
->n_col
)
13586 isl_die(space
->ctx
, isl_error_invalid
,
13587 "equalities and inequalities matrices should have "
13588 "same number of columns", goto error
);
13592 if (eq
->n_col
< total
)
13593 isl_die(space
->ctx
, isl_error_invalid
,
13594 "number of columns too small", goto error
);
13596 extra
= eq
->n_col
- total
;
13598 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13599 eq
->n_row
, ineq
->n_row
);
13602 for (i
= 0; i
< extra
; ++i
) {
13603 k
= isl_basic_map_alloc_div(bmap
);
13606 isl_int_set_si(bmap
->div
[k
][0], 0);
13608 for (i
= 0; i
< eq
->n_row
; ++i
) {
13609 l
= isl_basic_map_alloc_equality(bmap
);
13612 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13613 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13614 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13617 for (k
= 0; k
< dim
; ++k
) {
13618 isl_int_set(bmap
->eq
[l
][off
+ k
],
13624 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13625 l
= isl_basic_map_alloc_inequality(bmap
);
13628 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13629 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13630 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13633 for (k
= 0; k
< dim
; ++k
) {
13634 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13635 ineq
->row
[i
][pos
]);
13641 isl_space_free(space
);
13643 isl_mat_free(ineq
);
13645 bmap
= isl_basic_map_simplify(bmap
);
13646 return isl_basic_map_finalize(bmap
);
13648 isl_space_free(space
);
13650 isl_mat_free(ineq
);
13651 isl_basic_map_free(bmap
);
13655 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13656 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13657 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13659 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13660 c1
, c2
, c3
, c4
, isl_dim_in
);
13663 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13664 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13665 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13667 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13668 c1
, c2
, c3
, c4
, isl_dim_in
);
13671 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13672 __isl_take isl_space
*space
,
13673 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13674 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13676 isl_basic_map
*bmap
;
13677 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13678 c1
, c2
, c3
, c4
, isl_dim_in
);
13679 return bset_from_bmap(bmap
);
13682 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13685 return isl_bool_error
;
13687 return isl_space_can_zip(bmap
->dim
);
13690 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13693 return isl_bool_error
;
13695 return isl_space_can_zip(map
->dim
);
13698 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13699 * (A -> C) -> (B -> D).
13701 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13711 if (!isl_basic_map_can_zip(bmap
))
13712 isl_die(bmap
->ctx
, isl_error_invalid
,
13713 "basic map cannot be zipped", goto error
);
13714 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13715 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13716 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13717 if (n_in
< 0 || n1
< 0 || n2
< 0)
13718 return isl_basic_map_free(bmap
);
13719 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13720 bmap
= isl_basic_map_cow(bmap
);
13721 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13724 bmap
->dim
= isl_space_zip(bmap
->dim
);
13727 bmap
= isl_basic_map_mark_final(bmap
);
13730 isl_basic_map_free(bmap
);
13734 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13735 * (A -> C) -> (B -> D).
13737 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13742 if (!isl_map_can_zip(map
))
13743 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13746 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13752 /* Can we apply isl_basic_map_curry to "bmap"?
13753 * That is, does it have a nested relation in its domain?
13755 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13758 return isl_bool_error
;
13760 return isl_space_can_curry(bmap
->dim
);
13763 /* Can we apply isl_map_curry to "map"?
13764 * That is, does it have a nested relation in its domain?
13766 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13769 return isl_bool_error
;
13771 return isl_space_can_curry(map
->dim
);
13774 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13777 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13783 if (!isl_basic_map_can_curry(bmap
))
13784 isl_die(bmap
->ctx
, isl_error_invalid
,
13785 "basic map cannot be curried", goto error
);
13786 bmap
= isl_basic_map_cow(bmap
);
13789 bmap
->dim
= isl_space_curry(bmap
->dim
);
13792 bmap
= isl_basic_map_mark_final(bmap
);
13795 isl_basic_map_free(bmap
);
13799 /* Given a map (A -> B) -> C, return the corresponding map
13802 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13804 return isl_map_change_space(map
, &isl_map_can_curry
,
13805 "map cannot be curried", &isl_space_curry
);
13808 /* Can isl_map_range_curry be applied to "map"?
13809 * That is, does it have a nested relation in its range,
13810 * the domain of which is itself a nested relation?
13812 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13815 return isl_bool_error
;
13817 return isl_space_can_range_curry(map
->dim
);
13820 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13821 * A -> (B -> (C -> D)).
13823 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13825 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13826 "map range cannot be curried",
13827 &isl_space_range_curry
);
13830 /* Can we apply isl_basic_map_uncurry to "bmap"?
13831 * That is, does it have a nested relation in its domain?
13833 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13836 return isl_bool_error
;
13838 return isl_space_can_uncurry(bmap
->dim
);
13841 /* Can we apply isl_map_uncurry to "map"?
13842 * That is, does it have a nested relation in its domain?
13844 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13847 return isl_bool_error
;
13849 return isl_space_can_uncurry(map
->dim
);
13852 /* Given a basic map A -> (B -> C), return the corresponding basic map
13855 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13861 if (!isl_basic_map_can_uncurry(bmap
))
13862 isl_die(bmap
->ctx
, isl_error_invalid
,
13863 "basic map cannot be uncurried",
13864 return isl_basic_map_free(bmap
));
13865 bmap
= isl_basic_map_cow(bmap
);
13868 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13870 return isl_basic_map_free(bmap
);
13871 bmap
= isl_basic_map_mark_final(bmap
);
13875 /* Given a map A -> (B -> C), return the corresponding map
13878 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13880 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13881 "map cannot be uncurried", &isl_space_uncurry
);
13884 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13885 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13887 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13890 /* Construct a basic map where the given dimensions are equal to each other.
13892 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13893 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13895 isl_basic_map
*bmap
= NULL
;
13899 total
= isl_space_dim(space
, isl_dim_all
);
13901 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13902 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13905 if (type1
== type2
&& pos1
== pos2
)
13906 return isl_basic_map_universe(space
);
13908 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13909 i
= isl_basic_map_alloc_equality(bmap
);
13912 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13913 pos1
+= isl_basic_map_offset(bmap
, type1
);
13914 pos2
+= isl_basic_map_offset(bmap
, type2
);
13915 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13916 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13917 bmap
= isl_basic_map_finalize(bmap
);
13918 isl_space_free(space
);
13921 isl_space_free(space
);
13922 isl_basic_map_free(bmap
);
13926 /* Add a constraint imposing that the given two dimensions are equal.
13928 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13929 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13933 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13935 bmap
= isl_basic_map_intersect(bmap
, eq
);
13940 /* Add a constraint imposing that the given two dimensions are equal.
13942 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13943 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13945 isl_basic_map
*bmap
;
13947 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13949 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13954 /* Add a constraint imposing that the given two dimensions have opposite values.
13956 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13957 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13959 isl_basic_map
*bmap
= NULL
;
13963 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13964 return isl_map_free(map
);
13965 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13966 return isl_map_free(map
);
13968 total
= isl_map_dim(map
, isl_dim_all
);
13970 return isl_map_free(map
);
13971 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13972 i
= isl_basic_map_alloc_equality(bmap
);
13975 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13976 pos1
+= isl_basic_map_offset(bmap
, type1
);
13977 pos2
+= isl_basic_map_offset(bmap
, type2
);
13978 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13979 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13980 bmap
= isl_basic_map_finalize(bmap
);
13982 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13986 isl_basic_map_free(bmap
);
13991 /* Construct a constraint imposing that the value of the first dimension is
13992 * greater than or equal to that of the second.
13994 static __isl_give isl_constraint
*constraint_order_ge(
13995 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13996 enum isl_dim_type type2
, int pos2
)
14000 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14001 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14002 space
= isl_space_free(space
);
14006 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
14008 if (type1
== type2
&& pos1
== pos2
)
14011 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
14012 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
14017 /* Add a constraint imposing that the value of the first dimension is
14018 * greater than or equal to that of the second.
14020 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
14021 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14026 if (type1
== type2
&& pos1
== pos2
)
14028 space
= isl_basic_map_get_space(bmap
);
14029 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14030 bmap
= isl_basic_map_add_constraint(bmap
, c
);
14035 /* Add a constraint imposing that the value of the first dimension is
14036 * greater than or equal to that of the second.
14038 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
14039 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14044 if (type1
== type2
&& pos1
== pos2
)
14046 space
= isl_map_get_space(map
);
14047 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14048 map
= isl_map_add_constraint(map
, c
);
14053 /* Add a constraint imposing that the value of the first dimension is
14054 * less than or equal to that of the second.
14056 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
14057 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14059 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
14062 /* Construct a basic map where the value of the first dimension is
14063 * greater than that of the second.
14065 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
14066 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14068 isl_basic_map
*bmap
= NULL
;
14072 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14073 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14076 if (type1
== type2
&& pos1
== pos2
)
14077 return isl_basic_map_empty(space
);
14079 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
14080 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14081 i
= isl_basic_map_alloc_inequality(bmap
);
14082 if (total
< 0 || i
< 0)
14083 return isl_basic_map_free(bmap
);
14084 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
14085 pos1
+= isl_basic_map_offset(bmap
, type1
);
14086 pos2
+= isl_basic_map_offset(bmap
, type2
);
14087 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
14088 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
14089 isl_int_set_si(bmap
->ineq
[i
][0], -1);
14090 bmap
= isl_basic_map_finalize(bmap
);
14094 isl_space_free(space
);
14095 isl_basic_map_free(bmap
);
14099 /* Add a constraint imposing that the value of the first dimension is
14100 * greater than that of the second.
14102 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
14103 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14107 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
14109 bmap
= isl_basic_map_intersect(bmap
, gt
);
14114 /* Add a constraint imposing that the value of the first dimension is
14115 * greater than that of the second.
14117 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
14118 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14120 isl_basic_map
*bmap
;
14122 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
14124 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
14129 /* Add a constraint imposing that the value of the first dimension is
14130 * smaller than that of the second.
14132 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
14133 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14135 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
14138 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
14142 isl_local_space
*ls
;
14147 if (!isl_basic_map_divs_known(bmap
))
14148 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14149 "some divs are unknown", return NULL
);
14151 ls
= isl_basic_map_get_local_space(bmap
);
14152 div
= isl_local_space_get_div(ls
, pos
);
14153 isl_local_space_free(ls
);
14158 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
14161 return isl_basic_map_get_div(bset
, pos
);
14164 /* Plug in "subs" for set dimension "pos" of "set".
14166 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
14167 unsigned pos
, __isl_keep isl_aff
*subs
)
14171 if (set
&& isl_set_plain_is_empty(set
))
14174 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
14175 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
14176 return isl_set_preimage_multi_aff(set
, ma
);
14179 /* Check if the range of "ma" is compatible with the domain or range
14180 * (depending on "type") of "bmap".
14182 static isl_stat
check_basic_map_compatible_range_multi_aff(
14183 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
14184 __isl_keep isl_multi_aff
*ma
)
14187 isl_space
*ma_space
;
14189 ma_space
= isl_multi_aff_get_space(ma
);
14191 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
14195 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14196 "parameters don't match", goto error
);
14197 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
14201 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14202 "spaces don't match", goto error
);
14204 isl_space_free(ma_space
);
14205 return isl_stat_ok
;
14207 isl_space_free(ma_space
);
14208 return isl_stat_error
;
14211 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14212 * coefficients before the transformed range of dimensions,
14213 * the "n_after" coefficients after the transformed range of dimensions
14214 * and the coefficients of the other divs in "bmap".
14216 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
14217 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
14222 isl_local_space
*ls
;
14227 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
14228 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
14229 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
14230 if (n_param
< 0 || n_set
< 0)
14231 return isl_basic_map_free(bmap
);
14233 for (i
= 0; i
< n_div
; ++i
) {
14234 int o_bmap
= 0, o_ls
= 0;
14236 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
14237 o_bmap
+= 1 + 1 + n_param
;
14238 o_ls
+= 1 + 1 + n_param
;
14239 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
14240 o_bmap
+= n_before
;
14241 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14242 ls
->div
->row
[i
] + o_ls
, n_set
);
14245 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
14247 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14248 ls
->div
->row
[i
] + o_ls
, n_div
);
14251 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
14252 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
14257 isl_local_space_free(ls
);
14260 isl_local_space_free(ls
);
14261 return isl_basic_map_free(bmap
);
14264 /* How many stride constraints does "ma" enforce?
14265 * That is, how many of the affine expressions have a denominator
14266 * different from one?
14268 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
14273 for (i
= 0; i
< ma
->n
; ++i
)
14274 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14280 /* For each affine expression in ma of the form
14282 * x_i = (f_i y + h_i)/m_i
14284 * with m_i different from one, add a constraint to "bmap"
14287 * f_i y + h_i = m_i alpha_i
14289 * with alpha_i an additional existentially quantified variable.
14291 * The input variables of "ma" correspond to a subset of the variables
14292 * of "bmap". There are "n_before" variables in "bmap" before this
14293 * subset and "n_after" variables after this subset.
14294 * The integer divisions of the affine expressions in "ma" are assumed
14295 * to have been aligned. There are "n_div_ma" of them and
14296 * they appear first in "bmap", straight after the "n_after" variables.
14298 static __isl_give isl_basic_map
*add_ma_strides(
14299 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
14300 int n_before
, int n_after
, int n_div_ma
)
14308 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14309 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
14310 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
14311 if (total
< 0 || n_param
< 0 || n_in
< 0)
14312 return isl_basic_map_free(bmap
);
14313 for (i
= 0; i
< ma
->n
; ++i
) {
14314 int o_bmap
= 0, o_ma
= 1;
14316 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14318 div
= isl_basic_map_alloc_div(bmap
);
14319 k
= isl_basic_map_alloc_equality(bmap
);
14320 if (div
< 0 || k
< 0)
14322 isl_int_set_si(bmap
->div
[div
][0], 0);
14323 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14324 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
14325 o_bmap
+= 1 + n_param
;
14326 o_ma
+= 1 + n_param
;
14327 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
14328 o_bmap
+= n_before
;
14329 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14330 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
14333 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
14335 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14336 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
14337 o_bmap
+= n_div_ma
;
14339 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
14340 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
14346 isl_basic_map_free(bmap
);
14350 /* Replace the domain or range space (depending on "type) of "space" by "set".
14352 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
14353 enum isl_dim_type type
, __isl_take isl_space
*set
)
14355 if (type
== isl_dim_in
) {
14356 space
= isl_space_range(space
);
14357 space
= isl_space_map_from_domain_and_range(set
, space
);
14359 space
= isl_space_domain(space
);
14360 space
= isl_space_map_from_domain_and_range(space
, set
);
14366 /* Compute the preimage of the domain or range (depending on "type")
14367 * of "bmap" under the function represented by "ma".
14368 * In other words, plug in "ma" in the domain or range of "bmap".
14369 * The result is a basic map that lives in the same space as "bmap"
14370 * except that the domain or range has been replaced by
14371 * the domain space of "ma".
14373 * If bmap is represented by
14375 * A(p) + S u + B x + T v + C(divs) >= 0,
14377 * where u and x are input and output dimensions if type == isl_dim_out
14378 * while x and v are input and output dimensions if type == isl_dim_in,
14379 * and ma is represented by
14381 * x = D(p) + F(y) + G(divs')
14383 * then the result is
14385 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14387 * The divs in the input set are similarly adjusted.
14390 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14394 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14395 * B_i G(divs') + c_i(divs))/n_i)
14397 * If bmap is not a rational map and if F(y) involves any denominators
14399 * x_i = (f_i y + h_i)/m_i
14401 * then additional constraints are added to ensure that we only
14402 * map back integer points. That is we enforce
14404 * f_i y + h_i = m_i alpha_i
14406 * with alpha_i an additional existentially quantified variable.
14408 * We first copy over the divs from "ma".
14409 * Then we add the modified constraints and divs from "bmap".
14410 * Finally, we add the stride constraints, if needed.
14412 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
14413 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
14414 __isl_take isl_multi_aff
*ma
)
14418 isl_basic_map
*res
= NULL
;
14419 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
14420 isl_int f
, c1
, c2
, g
;
14429 ma
= isl_multi_aff_align_divs(ma
);
14432 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
14435 if (type
== isl_dim_in
) {
14437 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
14439 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
14442 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
14443 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
14444 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
14447 space
= isl_multi_aff_get_domain_space(ma
);
14448 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
14449 rational
= isl_basic_map_is_rational(bmap
);
14450 strides
= rational
? 0 : multi_aff_strides(ma
);
14451 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
14452 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
14454 res
= isl_basic_map_set_rational(res
);
14456 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
14457 if (isl_basic_map_alloc_div(res
) < 0)
14460 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
14464 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
14465 k
= isl_basic_map_alloc_equality(res
);
14468 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
14469 n_after
, n_div_ma
, n_div_bmap
,
14470 f
, c1
, c2
, g
, 0) < 0)
14474 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
14475 k
= isl_basic_map_alloc_inequality(res
);
14478 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
14479 n_after
, n_div_ma
, n_div_bmap
,
14480 f
, c1
, c2
, g
, 0) < 0)
14484 for (i
= 0; i
< bmap
->n_div
; ++i
) {
14485 if (isl_int_is_zero(bmap
->div
[i
][0])) {
14486 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
14489 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
14490 n_before
, n_after
, n_div_ma
, n_div_bmap
,
14491 f
, c1
, c2
, g
, 1) < 0)
14496 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14502 isl_basic_map_free(bmap
);
14503 isl_multi_aff_free(ma
);
14504 res
= isl_basic_map_simplify(res
);
14505 return isl_basic_map_finalize(res
);
14511 isl_basic_map_free(bmap
);
14512 isl_multi_aff_free(ma
);
14513 isl_basic_map_free(res
);
14517 /* Compute the preimage of "bset" under the function represented by "ma".
14518 * In other words, plug in "ma" in "bset". The result is a basic set
14519 * that lives in the domain space of "ma".
14521 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14522 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14524 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14527 /* Compute the preimage of the domain of "bmap" under the function
14528 * represented by "ma".
14529 * In other words, plug in "ma" in the domain of "bmap".
14530 * The result is a basic map that lives in the same space as "bmap"
14531 * except that the domain has been replaced by the domain space of "ma".
14533 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14534 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14536 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14539 /* Compute the preimage of the range of "bmap" under the function
14540 * represented by "ma".
14541 * In other words, plug in "ma" in the range of "bmap".
14542 * The result is a basic map that lives in the same space as "bmap"
14543 * except that the range has been replaced by the domain space of "ma".
14545 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14546 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14548 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14551 /* Check if the range of "ma" is compatible with the domain or range
14552 * (depending on "type") of "map".
14553 * Return isl_stat_error if anything is wrong.
14555 static isl_stat
check_map_compatible_range_multi_aff(
14556 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14557 __isl_keep isl_multi_aff
*ma
)
14560 isl_space
*ma_space
;
14562 ma_space
= isl_multi_aff_get_space(ma
);
14563 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14564 isl_space_free(ma_space
);
14566 return isl_stat_error
;
14568 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14569 "spaces don't match", return isl_stat_error
);
14570 return isl_stat_ok
;
14573 /* Compute the preimage of the domain or range (depending on "type")
14574 * of "map" under the function represented by "ma".
14575 * In other words, plug in "ma" in the domain or range of "map".
14576 * The result is a map that lives in the same space as "map"
14577 * except that the domain or range has been replaced by
14578 * the domain space of "ma".
14580 * The parameters are assumed to have been aligned.
14582 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14583 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14588 map
= isl_map_cow(map
);
14589 ma
= isl_multi_aff_align_divs(ma
);
14592 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14595 for (i
= 0; i
< map
->n
; ++i
) {
14596 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14597 isl_multi_aff_copy(ma
));
14602 space
= isl_multi_aff_get_domain_space(ma
);
14603 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14605 isl_space_free(isl_map_take_space(map
));
14606 map
= isl_map_restore_space(map
, space
);
14610 isl_multi_aff_free(ma
);
14612 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14613 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14616 isl_multi_aff_free(ma
);
14621 /* Compute the preimage of the domain or range (depending on "type")
14622 * of "map" under the function represented by "ma".
14623 * In other words, plug in "ma" in the domain or range of "map".
14624 * The result is a map that lives in the same space as "map"
14625 * except that the domain or range has been replaced by
14626 * the domain space of "ma".
14628 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14629 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14636 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14640 return map_preimage_multi_aff(map
, type
, ma
);
14642 if (isl_map_check_named_params(map
) < 0)
14644 if (!isl_space_has_named_params(ma
->space
))
14645 isl_die(map
->ctx
, isl_error_invalid
,
14646 "unaligned unnamed parameters", goto error
);
14647 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14648 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14650 return map_preimage_multi_aff(map
, type
, ma
);
14652 isl_multi_aff_free(ma
);
14653 return isl_map_free(map
);
14656 /* Compute the preimage of "set" under the function represented by "ma".
14657 * In other words, plug in "ma" in "set". The result is a set
14658 * that lives in the domain space of "ma".
14660 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14661 __isl_take isl_multi_aff
*ma
)
14663 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14666 /* Compute the preimage of the domain of "map" under the function
14667 * represented by "ma".
14668 * In other words, plug in "ma" in the domain of "map".
14669 * The result is a map that lives in the same space as "map"
14670 * except that the domain has been replaced by the domain space of "ma".
14672 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14673 __isl_take isl_multi_aff
*ma
)
14675 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14678 /* Compute the preimage of the range of "map" under the function
14679 * represented by "ma".
14680 * In other words, plug in "ma" in the range of "map".
14681 * The result is a map that lives in the same space as "map"
14682 * except that the range has been replaced by the domain space of "ma".
14684 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14685 __isl_take isl_multi_aff
*ma
)
14687 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14690 /* Compute the preimage of "map" under the function represented by "pma".
14691 * In other words, plug in "pma" in the domain or range of "map".
14692 * The result is a map that lives in the same space as "map",
14693 * except that the space of type "type" has been replaced by
14694 * the domain space of "pma".
14696 * The parameters of "map" and "pma" are assumed to have been aligned.
14698 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14699 __isl_take isl_map
*map
, enum isl_dim_type type
,
14700 __isl_take isl_pw_multi_aff
*pma
)
14711 space
= isl_pw_multi_aff_get_domain_space(pma
);
14712 isl_pw_multi_aff_free(pma
);
14713 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14715 return isl_map_empty(space
);
14718 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14719 isl_multi_aff_copy(pma
->p
[0].maff
));
14720 if (type
== isl_dim_in
)
14721 res
= isl_map_intersect_domain(res
,
14722 isl_map_copy(pma
->p
[0].set
));
14724 res
= isl_map_intersect_range(res
,
14725 isl_map_copy(pma
->p
[0].set
));
14727 for (i
= 1; i
< pma
->n
; ++i
) {
14730 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14731 isl_multi_aff_copy(pma
->p
[i
].maff
));
14732 if (type
== isl_dim_in
)
14733 res_i
= isl_map_intersect_domain(res_i
,
14734 isl_map_copy(pma
->p
[i
].set
));
14736 res_i
= isl_map_intersect_range(res_i
,
14737 isl_map_copy(pma
->p
[i
].set
));
14738 res
= isl_map_union(res
, res_i
);
14741 isl_pw_multi_aff_free(pma
);
14745 isl_pw_multi_aff_free(pma
);
14750 /* Compute the preimage of "map" under the function represented by "pma".
14751 * In other words, plug in "pma" in the domain or range of "map".
14752 * The result is a map that lives in the same space as "map",
14753 * except that the space of type "type" has been replaced by
14754 * the domain space of "pma".
14756 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14757 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14764 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14768 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14770 if (isl_map_check_named_params(map
) < 0)
14772 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14774 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14775 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14777 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14779 isl_pw_multi_aff_free(pma
);
14780 return isl_map_free(map
);
14783 /* Compute the preimage of "set" under the function represented by "pma".
14784 * In other words, plug in "pma" in "set". The result is a set
14785 * that lives in the domain space of "pma".
14787 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14788 __isl_take isl_pw_multi_aff
*pma
)
14790 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14793 /* Compute the preimage of the domain of "map" under the function
14794 * represented by "pma".
14795 * In other words, plug in "pma" in the domain of "map".
14796 * The result is a map that lives in the same space as "map",
14797 * except that domain space has been replaced by the domain space of "pma".
14799 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14800 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14802 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14805 /* Compute the preimage of the range of "map" under the function
14806 * represented by "pma".
14807 * In other words, plug in "pma" in the range of "map".
14808 * The result is a map that lives in the same space as "map",
14809 * except that range space has been replaced by the domain space of "pma".
14811 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14812 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14814 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14817 /* Compute the preimage of "map" under the function represented by "mpa".
14818 * In other words, plug in "mpa" in the domain or range of "map".
14819 * The result is a map that lives in the same space as "map",
14820 * except that the space of type "type" has been replaced by
14821 * the domain space of "mpa".
14823 * If the map does not involve any constraints that refer to the
14824 * dimensions of the substituted space, then the only possible
14825 * effect of "mpa" on the map is to map the space to a different space.
14826 * We create a separate isl_multi_aff to effectuate this change
14827 * in order to avoid spurious splitting of the map along the pieces
14829 * If "mpa" has a non-trivial explicit domain, however,
14830 * then the full substitution should be performed.
14832 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14833 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14837 isl_pw_multi_aff
*pma
;
14839 n
= isl_map_dim(map
, type
);
14843 full
= isl_map_involves_dims(map
, type
, 0, n
);
14844 if (full
>= 0 && !full
)
14845 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14852 space
= isl_multi_pw_aff_get_space(mpa
);
14853 isl_multi_pw_aff_free(mpa
);
14854 ma
= isl_multi_aff_zero(space
);
14855 return isl_map_preimage_multi_aff(map
, type
, ma
);
14858 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14859 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14862 isl_multi_pw_aff_free(mpa
);
14866 /* Compute the preimage of "map" under the function represented by "mpa".
14867 * In other words, plug in "mpa" in the domain "map".
14868 * The result is a map that lives in the same space as "map",
14869 * except that domain space has been replaced by the domain space of "mpa".
14871 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14872 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14874 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14877 /* Compute the preimage of "set" by the function represented by "mpa".
14878 * In other words, plug in "mpa" in "set".
14880 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14881 __isl_take isl_multi_pw_aff
*mpa
)
14883 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14886 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14887 * on the output dimension "pos" in terms of the parameters,
14888 * the input dimensions and possibly some integer divisions,
14889 * but not any other output dimensions, extract this upper bound
14890 * as a function of all dimensions (with zero coefficients
14891 * for the output dimensions).
14893 * That is, the inequality is of the form
14895 * e(...) + c - m x >= 0
14897 * where e does not depend on any other output dimensions.
14898 * Return (e(...) + c) / m, with the denominator m in the first position.
14900 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14901 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14904 isl_size v_out
, total
;
14906 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14907 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14908 if (v_out
< 0 || total
< 0)
14910 ctx
= isl_basic_map_get_ctx(bmap
);
14911 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14912 total
, v_out
+ pos
);
14915 /* Is constraint "c" of "bmap" of the form
14917 * e(...) + c1 - m x >= 0
14921 * -e(...) + c2 + m x >= 0
14923 * where m > 1 and e does not involve any other output variables?
14925 * "v_out" is the offset to the output variables.
14926 * "d" is the position of x among the output variables.
14927 * "v_div" is the offset to the local variables.
14928 * "total" is the total number of variables.
14930 * Since the purpose of this function is to use the constraint
14931 * to express the output variable as an integer division,
14932 * do not allow the constraint to involve any local variables
14933 * that do not have an explicit representation.
14935 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14936 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14940 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14941 return isl_bool_false
;
14942 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14943 return isl_bool_false
;
14944 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14945 return isl_bool_false
;
14946 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14947 return isl_bool_false
;
14948 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14949 v_div
- (v_out
+ d
+ 1)) != -1)
14950 return isl_bool_false
;
14951 for (i
= 0; v_div
+ i
< total
; ++i
) {
14952 isl_bool known
, involves
;
14954 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14956 known
= isl_basic_map_div_is_known(bmap
, i
);
14957 if (known
< 0 || !known
)
14959 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14960 if (involves
< 0 || involves
)
14961 return isl_bool_not(involves
);
14963 return isl_bool_true
;
14966 /* Look for a pair of constraints
14968 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14972 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14974 * that express that the output dimension x at position "pos"
14975 * is some integer division of an expression in terms of the parameters,
14976 * input dimensions and integer divisions.
14977 * If such a pair can be found, then return the index
14978 * of the upper bound constraint, m x <= e(...) + c1.
14979 * Otherwise, return an index beyond the number of constraints.
14981 * The constraints of "bmap" are assumed to have been sorted.
14983 * In order for the constraints above to express an integer division,
14984 * m needs to be greater than 1 and such that
14986 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14988 * In particular, this ensures that
14990 * x = floor((e(...) + c1) / m)
14992 isl_size
isl_basic_map_find_output_upper_div_constraint(
14993 __isl_keep isl_basic_map
*bmap
, int pos
)
14998 isl_size v_out
, v_div
;
15002 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
15003 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15004 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15005 n_ineq
= isl_basic_map_n_inequality(bmap
);
15006 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
15007 return isl_size_error
;
15010 for (i
= 0; i
< n_ineq
; ++i
) {
15011 isl_bool potential
;
15013 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
15014 v_out
, pos
, v_div
, total
);
15020 j
= find_later_constraint_in_pair(bmap
, i
, v_out
+ pos
, total
,
15021 bmap
->ineq
[i
][1 + v_out
+ pos
], &sum
);
15027 isl_int_clear(sum
);
15031 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
15036 isl_int_clear(sum
);
15037 return isl_size_error
;
15040 /* Look for a pair of constraints
15043 * g(x) - i + d >= 0
15045 * in "bmap" on the output dimension at position "pos" such that
15046 * g(x) is an expression in the parameters and input dimensions, and
15048 * Return the index of the first constraint if such a pair can be found.
15049 * Otherwise, return an index beyond the number of inequality constraints.
15051 * The constraints are assumed to have been sorted.
15053 static isl_size
find_output_lower_mod_constraint(__isl_keep isl_basic_map
*bmap
,
15054 int pos
, isl_int n
)
15058 isl_size v_out
, n_out
;
15059 isl_size v_div
, n_div
;
15063 n_ineq
= isl_basic_map_n_inequality(bmap
);
15064 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15065 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15066 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15067 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15068 if (n_ineq
< 0 || v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15069 return isl_size_error
;
15072 for (i
= n_ineq
- 1; i
>= 0; --i
) {
15073 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]) &&
15074 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15076 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_div
,
15079 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_out
, pos
) != -1)
15081 if (isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_out
+ pos
+ 1,
15082 n_out
- (pos
+ 1)) != -1)
15084 j
= find_earlier_constraint_in_pair(bmap
, i
, v_out
+ pos
, v_div
,
15088 isl_int_clear(tmp
);
15091 if (isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15096 isl_int_clear(tmp
);
15100 /* Given that equality "eq" of "bset" expresses
15101 * the variable at position "pos" in terms of the other variables,
15102 * extract this expression as a function of those other variables,
15103 * excluding any local variables.
15105 static __isl_give isl_aff
*extract_expr_aff(
15106 __isl_keep isl_basic_set
*bset
, int eq
, int pos
)
15112 isl_local_space
*ls
;
15114 space
= isl_basic_set_peek_space(bset
);
15115 dim
= isl_space_dim(space
, isl_dim_all
);
15118 ctx
= isl_basic_set_get_ctx(bset
);
15119 v
= extract_bound_from_constraint(ctx
, bset
->eq
[eq
], dim
, pos
);
15120 ls
= isl_local_space_from_space(isl_space_copy(space
));
15121 return isl_aff_alloc_vec(ls
, v
);
15124 /* Given the presence in "bmap" of an equality constraint "eq"
15126 * f(x) + t m i + m n h(y) = 0
15128 * and a pair of inequality constraints
15130 * -g(x) + i >= 0 ("lower")
15131 * g(x) - i + d >= 0
15133 * where i is the output dimension at position "pos",
15134 * f(x) and g(x) are expressions in the parameters and
15135 * input dimensions (if any),
15136 * h(y) is an expression in the other output dimensions,
15137 * t is +1 or -1, and
15139 * return the expression
15141 * (-t f(x)/m - g(x)) mod n + g(x)
15143 * The expression is first constructed in terms of the wrapped space of "bmap".
15144 * Since this expression does not depend on the original output dimensions,
15145 * they can be removed, resulting in an expression in terms
15146 * of the input dimensions.
15147 * If "bmap" is actually a set, then the resulting expression has no domain.
15149 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15150 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15152 static __isl_give isl_aff
*construct_mod(__isl_keep isl_basic_map
*bmap
,
15153 int pos
, int eq
, int lower
, isl_int n
)
15156 isl_basic_set
*bset
;
15157 isl_aff
*f
, *g
, *mod
;
15161 is_set
= isl_basic_map_is_set(bmap
);
15164 ctx
= isl_basic_map_get_ctx(bmap
);
15167 bset
= bset_from_bmap(isl_basic_map_copy(bmap
));
15169 bset
= isl_basic_map_wrap(isl_basic_map_copy(bmap
));
15170 g
= extract_lower_bound_aff(bset
, lower
, pos
);
15171 f
= extract_expr_aff(bset
, eq
, pos
);
15172 isl_basic_set_free(bset
);
15174 mod
= isl_aff_sub(f
, isl_aff_copy(g
));
15175 v
= isl_val_int_from_isl_int(ctx
, n
);
15176 mod
= isl_aff_mod_val(mod
, v
);
15177 mod
= isl_aff_add(mod
, g
);
15179 mod
= isl_aff_project_domain_on_params(mod
);
15181 mod
= isl_aff_domain_factor_domain(mod
);
15186 /* Look for a combination of constraints in "bmap" that ensure
15187 * that output dimension "pos" is equal to some modulo expression
15188 * in the parameters and input dimensions and
15189 * return this expression if found.
15191 * The constraints are assumed to have been sorted.
15193 * In particular look for an equality constraint
15195 * f(x) + t m i + m n h(y) = 0
15197 * and a pair of inequality constraints
15200 * g(x) - i + d >= 0
15202 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15203 * h(y) is an expression in local variables and other output dimensions,
15204 * t is +1 or -1, and
15207 * If such a combination of constraints can be found
15210 * i = -t f(x)/m - t n h(y)
15211 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15213 * and so, because 0 <= i - g(x) <= d < n,
15215 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15216 * = (-t f(x)/m - g(x)) mod n
15220 * i = (-t f(x)/m - g(x)) mod n + g(x)
15222 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_mod(
15223 __isl_keep isl_basic_map
*bmap
, int pos
)
15226 isl_size n_eq
, n_ineq
;
15227 isl_size v_out
, n_out
;
15228 isl_size v_div
, n_div
;
15231 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15233 n_eq
= isl_basic_map_n_equality(bmap
);
15234 n_ineq
= isl_basic_map_n_inequality(bmap
);
15235 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15236 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15237 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15238 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15239 if (n_eq
< 0 || n_ineq
< 0 ||
15240 v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15242 if (n_eq
== 0 || n_ineq
< 2 || n_out
== 1)
15246 for (i
= 0; i
< n_eq
; ++i
) {
15247 if (isl_int_is_zero(bmap
->eq
[i
][1 + v_out
+ pos
]))
15249 isl_seq_gcd(bmap
->eq
[i
] + 1 + v_div
, n_div
, &gcd
);
15250 for (j
= 0; j
< n_out
; ++j
) {
15253 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ j
]);
15255 if (!isl_int_abs_gt(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15257 if (!isl_int_is_divisible_by(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15259 isl_int_divexact(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]);
15260 isl_int_abs(gcd
, gcd
);
15261 lower
= find_output_lower_mod_constraint(bmap
, pos
, gcd
);
15262 if (lower
>= n_ineq
)
15264 res
.valid
= isl_bool_true
;
15266 res
.value
= construct_mod(bmap
, v_out
+ pos
, i
,
15269 res
.valid
= isl_bool_error
;
15270 isl_int_clear(gcd
);
15274 isl_int_clear(gcd
);
15277 res
.valid
= isl_bool_error
;
15281 /* Construct an isl_aff from the given domain local space "ls" and
15282 * coefficients "v", where the local space may involve
15283 * local variables without a known expression, as long as these
15284 * do not have a non-zero coefficient in "v".
15285 * These need to be pruned away first since an isl_aff cannot
15286 * reference any local variables without a known expression.
15287 * For simplicity, remove all local variables that have a zero coefficient and
15288 * that are not used in other local variables with a non-zero coefficient.
15290 static __isl_give isl_aff
*isl_aff_alloc_vec_prune(
15291 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
15294 isl_size n_div
, v_div
;
15296 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
15297 v_div
= isl_local_space_var_offset(ls
, isl_dim_div
);
15298 if (n_div
< 0 || v_div
< 0 || !v
)
15300 for (i
= n_div
- 1; i
>= 0; --i
) {
15303 if (!isl_int_is_zero(v
->el
[1 + 1 + v_div
+ i
]))
15305 involves
= isl_local_space_involves_dims(ls
, isl_dim_div
, i
, 1);
15310 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
15311 v
= isl_vec_drop_els(v
, 1 + 1 + v_div
+ i
, 1);
15316 return isl_aff_alloc_vec(ls
, v
);
15318 isl_local_space_free(ls
);
15323 /* Look for a pair of constraints in "bmap" that ensure
15324 * that output dimension "pos" is equal to some integer division expression
15325 * in the parameters and input dimensions and
15326 * return this expression if found.
15328 * In particular, looks for a pair of constraints
15330 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
15334 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
15336 * where m > 1 and e only depends on parameters and input dimensions,
15339 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15341 * If such a pair of constraints can be found
15344 * x = floor((e(...) + c1) / m)
15346 * with e(...) an expression that does not involve any other output dimensions.
15348 * Note that we know that
15352 * If c1 + c2 were 0, then we would have detected an equality during
15353 * simplification. If c1 + c2 were negative, then we would have detected
15356 * The constraint defining the integer division is guaranteed not to involve
15357 * any local variables without a known expression, but such local variables
15358 * may appear in other constraints. They therefore need to be removed
15359 * during the construction of the affine expression.
15361 static __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div(
15362 __isl_keep isl_basic_map
*bmap
, int pos
)
15366 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15367 isl_local_space
*ls
;
15372 n_ineq
= isl_basic_map_n_inequality(bmap
);
15376 i
= isl_basic_map_find_output_upper_div_constraint(bmap
, pos
);
15382 is_set
= isl_basic_map_is_set(bmap
);
15384 bmap
= isl_basic_map_free(bmap
);
15386 ls
= isl_basic_map_get_local_space(bmap
);
15388 ls
= isl_local_space_wrap(ls
);
15389 v
= isl_basic_map_inequality_extract_output_upper_bound(bmap
, i
, pos
);
15391 aff
= isl_aff_alloc_vec_prune(ls
, v
);
15392 aff
= isl_aff_floor(aff
);
15395 aff
= isl_aff_project_domain_on_params(aff
);
15397 aff
= isl_aff_domain_factor_domain(aff
);
15399 res
.valid
= isl_bool_true
;
15403 res
.valid
= isl_bool_error
;
15407 /* Look for a combination of constraints in "bmap" that ensure
15408 * that output dimension "pos" is equal to some integer division or
15409 * modulo expression in the parameters and input dimensions and
15410 * return this expression if found.
15412 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div_mod(
15413 __isl_keep isl_basic_map
*bmap
, int pos
)
15415 isl_maybe_isl_aff div
;
15417 div
= isl_basic_map_try_find_output_div(bmap
, pos
);
15418 if (div
.valid
< 0 || div
.valid
)
15420 return isl_basic_map_try_find_output_mod(bmap
, pos
);
15423 /* Look for a combination of constraints in "bmap" that ensure
15424 * that any output dimension is equal to some integer division or
15425 * modulo expression in the parameters and input dimensions and
15426 * return this expression if found.
15427 * The position of the output dimension (if any) is returned in "pos".
15429 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_any_output_div_mod(
15430 __isl_keep isl_basic_map
*bmap
, int *pos
)
15433 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15435 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
15439 for (*pos
= 0; *pos
< dim
; ++*pos
) {
15440 res
= isl_basic_map_try_find_output_div_mod(bmap
, *pos
);
15441 if (res
.valid
< 0 || res
.valid
)
15447 res
.valid
= isl_bool_error
;
15451 /* Return a copy of the equality constraints of "bset" as a matrix.
15453 __isl_give isl_mat
*isl_basic_set_extract_equalities(
15454 __isl_keep isl_basic_set
*bset
)
15459 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15463 ctx
= isl_basic_set_get_ctx(bset
);
15464 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
15467 /* Are the "n" "coefficients" starting at "first" of the integer division
15468 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15470 * The "coefficient" at position 0 is the denominator.
15471 * The "coefficient" at position 1 is the constant term.
15473 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
15474 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
15475 unsigned first
, unsigned n
)
15477 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
15478 return isl_bool_error
;
15479 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
15480 return isl_bool_error
;
15481 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
15482 bmap2
->div
[pos2
] + first
, n
);
15485 /* Are the integer division expressions at position "pos1" in "bmap1" and
15486 * "pos2" in "bmap2" equal to each other, except that the constant terms
15489 isl_bool
isl_basic_map_equal_div_expr_except_constant(
15490 __isl_keep isl_basic_map
*bmap1
, int pos1
,
15491 __isl_keep isl_basic_map
*bmap2
, int pos2
)
15494 isl_size total
, total2
;
15496 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
15497 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
15498 if (total
< 0 || total2
< 0)
15499 return isl_bool_error
;
15500 if (total
!= total2
)
15501 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
15502 "incomparable div expressions", return isl_bool_error
);
15503 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15505 if (equal
< 0 || !equal
)
15507 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15509 if (equal
< 0 || equal
)
15510 return isl_bool_not(equal
);
15511 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15515 /* Replace the numerator of the constant term of the integer division
15516 * expression at position "div" in "bmap" by "value".
15517 * The caller guarantees that this does not change the meaning
15520 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
15521 __isl_take isl_basic_map
*bmap
, int div
, int value
)
15523 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
15524 return isl_basic_map_free(bmap
);
15526 isl_int_set_si(bmap
->div
[div
][1], value
);
15531 /* Is the point "inner" internal to inequality constraint "ineq"
15533 * The point is considered to be internal to the inequality constraint,
15534 * if it strictly lies on the positive side of the inequality constraint,
15535 * or if it lies on the constraint and the constraint is lexico-positive.
15537 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
15538 __isl_keep isl_basic_set
*bset
, int ineq
)
15544 if (!inner
|| !bset
)
15545 return isl_bool_error
;
15547 ctx
= isl_basic_set_get_ctx(bset
);
15548 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
15549 &ctx
->normalize_gcd
);
15550 if (!isl_int_is_zero(ctx
->normalize_gcd
))
15551 return isl_int_is_nonneg(ctx
->normalize_gcd
);
15553 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15555 return isl_bool_error
;
15556 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
15557 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
15560 /* Tighten the inequality constraints of "bset" that are outward with respect
15561 * to the point "vec".
15562 * That is, tighten the constraints that are not satisfied by "vec".
15564 * "vec" is a point internal to some superset S of "bset" that is used
15565 * to make the subsets of S disjoint, by tightening one half of the constraints
15566 * that separate two subsets. In particular, the constraints of S
15567 * are all satisfied by "vec" and should not be tightened.
15568 * Of the internal constraints, those that have "vec" on the outside
15569 * are tightened. The shared facet is included in the adjacent subset
15570 * with the opposite constraint.
15571 * For constraints that saturate "vec", this criterion cannot be used
15572 * to determine which of the two sides should be tightened.
15573 * Instead, the sign of the first non-zero coefficient is used
15574 * to make this choice. Note that this second criterion is never used
15575 * on the constraints of S since "vec" is interior to "S".
15577 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
15578 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
15582 bset
= isl_basic_set_cow(bset
);
15585 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
15588 internal
= is_internal(vec
, bset
, j
);
15590 return isl_basic_set_free(bset
);
15593 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
15599 /* Replace the variables x of type "type" starting at "first" in "bmap"
15600 * by x' with x = M x' with M the matrix trans.
15601 * That is, replace the corresponding coefficients c by c M.
15603 * The transformation matrix should be a square matrix.
15605 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
15606 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
15607 __isl_take isl_mat
*trans
)
15611 bmap
= isl_basic_map_cow(bmap
);
15612 if (!bmap
|| !trans
)
15615 if (trans
->n_row
!= trans
->n_col
)
15616 isl_die(trans
->ctx
, isl_error_invalid
,
15617 "expecting square transformation matrix", goto error
);
15618 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
15621 pos
= isl_basic_map_offset(bmap
, type
) + first
;
15623 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
15624 isl_mat_copy(trans
)) < 0)
15626 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
15627 isl_mat_copy(trans
)) < 0)
15629 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
15630 isl_mat_copy(trans
)) < 0)
15633 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
15634 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
15635 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
15637 isl_mat_free(trans
);
15640 isl_mat_free(trans
);
15641 isl_basic_map_free(bmap
);
15645 /* Replace the variables x of type "type" starting at "first" in "bset"
15646 * by x' with x = M x' with M the matrix trans.
15647 * That is, replace the corresponding coefficients c by c M.
15649 * The transformation matrix should be a square matrix.
15651 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
15652 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
15653 __isl_take isl_mat
*trans
)
15655 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);