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_any_non_zero(bset
->eq
[i
] + 1, nparam
))
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_any_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 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_any_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 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 if (isl_seq_any_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1227 if (i
== bmap
->n_eq
)
1228 has_rational
= isl_bool_false
;
1230 isl_basic_map_free(bmap
);
1232 return has_rational
;
1235 /* Does "map" contain any rational points?
1237 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1240 isl_bool has_rational
;
1243 return isl_bool_error
;
1244 for (i
= 0; i
< map
->n
; ++i
) {
1245 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1246 if (has_rational
< 0 || has_rational
)
1247 return has_rational
;
1249 return isl_bool_false
;
1252 /* Does "set" contain any rational points?
1254 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1256 return isl_map_has_rational(set
);
1259 /* Is this basic set a parameter domain?
1261 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1264 return isl_bool_error
;
1265 return isl_space_is_params(bset
->dim
);
1268 /* Is this set a parameter domain?
1270 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1273 return isl_bool_error
;
1274 return isl_space_is_params(set
->dim
);
1277 /* Is this map actually a parameter domain?
1278 * Users should never call this function. Outside of isl,
1279 * a map can never be a parameter domain.
1281 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1284 return isl_bool_error
;
1285 return isl_space_is_params(map
->dim
);
1288 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1289 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1290 unsigned n_eq
, unsigned n_ineq
)
1293 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1294 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1295 size_t row_size
= 1 + n_var
+ extra
;
1301 return isl_basic_map_free(bmap
);
1303 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1304 if (isl_blk_is_error(bmap
->block
))
1307 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1308 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1312 bmap
->block2
= isl_blk_empty();
1315 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1316 if (isl_blk_is_error(bmap
->block2
))
1319 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1324 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1325 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1327 for (i
= 0; i
< extra
; ++i
)
1328 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1332 bmap
->c_size
= n_eq
+ n_ineq
;
1333 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1334 bmap
->extra
= extra
;
1338 bmap
->sample
= NULL
;
1342 isl_basic_map_free(bmap
);
1346 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1347 unsigned nparam
, unsigned dim
, unsigned extra
,
1348 unsigned n_eq
, unsigned n_ineq
)
1350 struct isl_basic_map
*bmap
;
1353 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1357 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1358 return bset_from_bmap(bmap
);
1361 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1362 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1364 struct isl_basic_map
*bmap
;
1367 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1368 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1369 return bset_from_bmap(bmap
);
1371 isl_space_free(space
);
1375 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1376 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1378 struct isl_basic_map
*bmap
;
1382 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1387 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1389 isl_space_free(space
);
1393 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1394 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1395 unsigned n_eq
, unsigned n_ineq
)
1397 struct isl_basic_map
*bmap
;
1400 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1404 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1408 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1409 __isl_keep isl_basic_map
*src
)
1412 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1414 if (!dst
|| total
< 0)
1415 return isl_basic_map_free(dst
);
1417 for (i
= 0; i
< src
->n_eq
; ++i
) {
1418 int j
= isl_basic_map_alloc_equality(dst
);
1420 return isl_basic_map_free(dst
);
1421 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1424 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1425 int j
= isl_basic_map_alloc_inequality(dst
);
1427 return isl_basic_map_free(dst
);
1428 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1431 for (i
= 0; i
< src
->n_div
; ++i
) {
1432 int j
= isl_basic_map_alloc_div(dst
);
1434 return isl_basic_map_free(dst
);
1435 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1437 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1441 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1443 struct isl_basic_map
*dup
;
1447 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1448 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1449 dup
= dup_constraints(dup
, bmap
);
1452 dup
->flags
= bmap
->flags
;
1453 dup
->sample
= isl_vec_copy(bmap
->sample
);
1457 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1459 struct isl_basic_map
*dup
;
1461 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1462 return bset_from_bmap(dup
);
1465 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1467 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset
)));
1470 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1479 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1484 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1488 bmap
= isl_basic_map_dup(bmap
);
1490 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1494 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1503 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1508 if (--bmap
->ref
> 0)
1511 isl_ctx_deref(bmap
->ctx
);
1513 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1515 isl_blk_free(bmap
->ctx
, bmap
->block
);
1516 isl_vec_free(bmap
->sample
);
1517 isl_space_free(bmap
->dim
);
1523 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1525 return isl_basic_map_free(bset_to_bmap(bset
));
1528 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1530 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1533 /* Check that "bset" does not involve any parameters.
1535 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1539 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1541 return isl_stat_error
;
1543 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1544 "basic set should not have any parameters",
1545 return isl_stat_error
);
1549 /* Check that "bset" does not involve any local variables.
1551 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1555 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1557 return isl_stat_error
;
1559 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1560 "basic set should not have any local variables",
1561 return isl_stat_error
);
1566 #define TYPE isl_map
1568 #include "isl_check_named_params_templ.c"
1571 #define TYPE isl_basic_map
1574 #include "isl_check_named_params_templ.c"
1576 /* Check that "bmap1" and "bmap2" have the same parameters,
1577 * reporting an error if they do not.
1579 static isl_stat
isl_basic_map_check_equal_params(
1580 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1584 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1586 return isl_stat_error
;
1588 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1589 "parameters don't match", return isl_stat_error
);
1594 #define TYPE isl_map
1596 #include "isl_align_params_bin_templ.c"
1601 #define ARG1 isl_map
1603 #define ARG2 isl_set
1605 #include "isl_align_params_templ.c"
1607 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1608 __isl_keep isl_map
*map2
,
1609 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1614 return isl_bool_error
;
1615 if (isl_map_has_equal_params(map1
, map2
))
1616 return fn(map1
, map2
);
1617 if (isl_map_check_named_params(map1
) < 0)
1618 return isl_bool_error
;
1619 if (isl_map_check_named_params(map2
) < 0)
1620 return isl_bool_error
;
1621 map1
= isl_map_copy(map1
);
1622 map2
= isl_map_copy(map2
);
1623 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1624 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1631 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1634 struct isl_ctx
*ctx
;
1636 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1640 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1641 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1643 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1644 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1645 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1646 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1647 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1649 int j
= isl_basic_map_alloc_inequality(bmap
);
1653 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1654 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1661 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1662 bmap
->extra
- bmap
->n_div
);
1663 return bmap
->n_eq
++;
1666 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1668 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1671 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1672 __isl_take isl_basic_map
*bmap
, unsigned n
)
1677 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1678 "invalid number of equalities",
1679 isl_basic_map_free(bmap
));
1684 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1685 __isl_take isl_basic_set
*bset
, unsigned n
)
1687 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1691 /* Drop the equality constraint at position "pos",
1692 * preserving the order of the other equality constraints.
1694 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1701 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1705 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1706 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1707 bmap
->eq
[bmap
->n_eq
] = t
;
1712 /* Turn inequality "pos" of "bmap" into an equality.
1714 * In particular, we move the inequality in front of the equalities
1715 * and move the last inequality in the position of the moved inequality.
1716 * Note that isl_tab_make_equalities_explicit depends on this particular
1717 * change in the ordering of the constraints.
1719 void isl_basic_map_inequality_to_equality(
1720 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1724 t
= bmap
->ineq
[pos
];
1725 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1726 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1731 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1737 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1739 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1742 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1745 struct isl_ctx
*ctx
;
1747 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1751 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1752 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1753 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1754 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1755 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1756 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1757 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1758 bmap
->extra
- bmap
->n_div
);
1759 return bmap
->n_ineq
++;
1762 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1764 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1767 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1768 __isl_take isl_basic_map
*bmap
, unsigned n
)
1772 if (n
> bmap
->n_ineq
)
1773 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1774 "invalid number of inequalities",
1775 return isl_basic_map_free(bmap
));
1780 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1781 __isl_take isl_basic_set
*bset
, unsigned n
)
1783 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1787 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1792 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1794 if (pos
!= bmap
->n_ineq
- 1) {
1795 t
= bmap
->ineq
[pos
];
1796 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1797 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1798 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1804 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1806 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1809 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1816 empty
= isl_basic_map_plain_is_empty(bmap
);
1818 return isl_basic_map_free(bmap
);
1822 bmap
= isl_basic_map_cow(bmap
);
1823 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1824 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1826 return isl_basic_map_free(bmap
);
1827 k
= isl_basic_map_alloc_equality(bmap
);
1830 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1833 isl_basic_map_free(bmap
);
1837 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1840 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1843 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1849 bmap
= isl_basic_map_cow(bmap
);
1850 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1851 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1853 return isl_basic_map_free(bmap
);
1854 k
= isl_basic_map_alloc_inequality(bmap
);
1857 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1860 isl_basic_map_free(bmap
);
1864 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1867 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1870 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1874 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1877 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1878 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1879 bmap
->extra
- bmap
->n_div
);
1880 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1881 return bmap
->n_div
++;
1884 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1886 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1890 #define TYPE isl_basic_map
1891 #include "check_type_range_templ.c"
1893 /* Check that there are "n" dimensions of type "type" starting at "first"
1896 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1897 enum isl_dim_type type
, unsigned first
, unsigned n
)
1899 return isl_basic_map_check_range(bset_to_bmap(bset
),
1903 /* Insert an extra integer division, prescribed by "div", to "bmap"
1904 * at (integer division) position "pos".
1906 * The integer division is first added at the end and then moved
1907 * into the right position.
1909 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1910 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1915 bmap
= isl_basic_map_cow(bmap
);
1916 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1917 if (total
< 0 || !div
)
1918 return isl_basic_map_free(bmap
);
1920 if (div
->size
!= 1 + 1 + total
)
1921 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1922 "unexpected size", return isl_basic_map_free(bmap
));
1923 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1924 return isl_basic_map_free(bmap
);
1926 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1927 k
= isl_basic_map_alloc_div(bmap
);
1929 return isl_basic_map_free(bmap
);
1930 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1931 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1933 for (i
= k
; i
> pos
; --i
)
1934 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1939 /* Insert an extra integer division, prescribed by "div", to "bset"
1940 * at (integer division) position "pos".
1942 __isl_give isl_basic_set
*isl_basic_set_insert_div(
1943 __isl_take isl_basic_set
*bset
, int pos
, __isl_keep isl_vec
*div
)
1945 isl_basic_map
*bmap
= bset_to_bmap(bset
);
1946 bmap
= isl_basic_map_insert_div(bmap
, pos
, div
);
1947 return bset_from_bmap(bmap
);
1950 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1953 return isl_stat_error
;
1954 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1959 static __isl_give isl_basic_map
*add_constraints(
1960 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1961 unsigned i_pos
, unsigned o_pos
)
1963 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1964 unsigned o_in
, o_out
;
1967 struct isl_dim_map
*dim_map
;
1969 space
= isl_basic_map_peek_space(bmap2
);
1970 if (!bmap1
|| !space
)
1973 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1974 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1975 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1976 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1977 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1978 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1979 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1980 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1982 ctx
= isl_basic_map_get_ctx(bmap1
);
1983 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1984 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1985 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1986 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1987 isl_dim_map_div(dim_map
, bmap2
, total
);
1989 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1991 isl_basic_map_free(bmap1
);
1992 isl_basic_map_free(bmap2
);
1996 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1997 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2000 struct isl_basic_map
*ext
;
2007 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
2009 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
2010 room_for_ineq(base
, n_ineq
))
2013 extra
+= base
->extra
;
2015 n_ineq
+= base
->n_ineq
;
2017 space
= isl_basic_map_get_space(base
);
2018 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2023 ext
->sample
= isl_vec_copy(base
->sample
);
2024 flags
= base
->flags
;
2025 ext
= add_constraints(ext
, base
, 0, 0);
2028 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2034 isl_basic_map_free(base
);
2038 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2039 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2041 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2042 extra
, n_eq
, n_ineq
));
2045 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2046 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2048 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2051 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2052 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2054 isl_basic_map
*bmap
= bset_to_bmap(base
);
2055 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2056 return bset_from_bmap(bmap
);
2059 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2061 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2064 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2069 if (bmap
->ref
> 1) {
2071 bmap
= isl_basic_map_dup(bmap
);
2074 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2078 /* Clear all cached information in "map", either because it is about
2079 * to be modified or because it is being freed.
2080 * Always return the same pointer that is passed in.
2081 * This is needed for the use in isl_map_free.
2083 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2085 isl_basic_map_free(map
->cached_simple_hull
[0]);
2086 isl_basic_map_free(map
->cached_simple_hull
[1]);
2087 map
->cached_simple_hull
[0] = NULL
;
2088 map
->cached_simple_hull
[1] = NULL
;
2092 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2094 return isl_map_cow(set
);
2097 /* Return an isl_map that is equal to "map" and that has only
2098 * a single reference.
2100 * If the original input already has only one reference, then
2101 * simply return it, but clear all cached information, since
2102 * it may be rendered invalid by the operations that will be
2103 * performed on the result.
2105 * Otherwise, create a duplicate (without any cached information).
2107 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2113 return clear_caches(map
);
2115 return isl_map_dup(map
);
2118 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2119 unsigned a_len
, unsigned b_len
)
2121 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2122 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2123 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2126 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2127 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2132 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2135 if (n1
== 0 || n2
== 0)
2138 bmap
= isl_basic_map_cow(bmap
);
2142 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2143 if (isl_blk_is_error(blk
))
2146 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2148 bmap
->eq
[i
] + pos
, n1
, n2
);
2150 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2152 bmap
->ineq
[i
] + pos
, n1
, n2
);
2154 for (i
= 0; i
< bmap
->n_div
; ++i
)
2156 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2158 isl_blk_free(bmap
->ctx
, blk
);
2160 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2161 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2162 return isl_basic_map_finalize(bmap
);
2164 isl_basic_map_free(bmap
);
2168 /* The given basic map has turned out to be empty.
2169 * Explicitly mark it as such and change the representation
2170 * to a canonical representation of the empty basic map.
2171 * Since the basic map has conflicting constraints,
2172 * it must have at least one constraint, except perhaps
2173 * if it was already explicitly marked as being empty.
2174 * Do nothing in the latter case, i.e., if it has been marked empty and
2175 * has no constraints.
2177 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2178 __isl_take isl_basic_map
*bmap
)
2185 n
= isl_basic_map_n_constraint(bmap
);
2186 empty
= isl_basic_map_plain_is_empty(bmap
);
2187 if (n
< 0 || empty
< 0)
2188 return isl_basic_map_free(bmap
);
2189 if (n
== 0 && empty
)
2191 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2193 return isl_basic_map_free(bmap
);
2194 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2195 return isl_basic_map_free(bmap
);
2196 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2199 if (bmap
->n_eq
> 0) {
2200 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2204 i
= isl_basic_map_alloc_equality(bmap
);
2208 isl_int_set_si(bmap
->eq
[i
][0], 1);
2209 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2210 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2211 isl_vec_free(bmap
->sample
);
2212 bmap
->sample
= NULL
;
2213 return isl_basic_map_finalize(bmap
);
2215 isl_basic_map_free(bmap
);
2219 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2220 __isl_take isl_basic_set
*bset
)
2222 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2225 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2226 __isl_take isl_basic_map
*bmap
)
2231 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2234 bmap
= isl_basic_map_cow(bmap
);
2238 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2240 return isl_basic_map_finalize(bmap
);
2243 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2244 __isl_take isl_basic_set
*bset
)
2246 return isl_basic_map_set_rational(bset
);
2249 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2250 __isl_take isl_basic_set
*bset
)
2255 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2258 bset
= isl_basic_set_cow(bset
);
2262 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2264 return isl_basic_set_finalize(bset
);
2267 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2271 map
= isl_map_cow(map
);
2274 for (i
= 0; i
< map
->n
; ++i
) {
2275 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2285 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2287 return isl_map_set_rational(set
);
2290 /* Given a constraint "c" that expresses a bound
2291 * on the variable at position "pos" in terms of the first "len" variables
2292 * (other than the variable itself if pos < len), extract this bound
2293 * as a function of those first "len" variables.
2295 * That is, the constraint is of one of the following forms
2297 * -e(...) + m x >= 0
2302 * Return (e(...)) / m, with the denominator m in the first position.
2304 static __isl_give isl_vec
*extract_bound_from_constraint(isl_ctx
*ctx
,
2305 isl_int
*c
, int len
, int pos
)
2309 v
= isl_vec_alloc(ctx
, 1 + 1 + len
);
2312 if (isl_int_is_pos(c
[1 + pos
])) {
2313 isl_int_set(v
->el
[0], c
[1 + pos
]);
2314 isl_seq_neg(v
->el
+ 1, c
, 1 + len
);
2316 isl_int_neg(v
->el
[0], c
[1 + pos
]);
2317 isl_seq_cpy(v
->el
+ 1, c
, 1 + len
);
2320 isl_int_set_si(v
->el
[1 + 1 + pos
], 0);
2325 /* Return the position of the last non-zero coefficient of
2326 * inequality constraint "ineq" of "bmap" or length "len",
2327 * given that the coefficient at position "first" is non-zero,
2328 * or that it is known that there is at least one coefficient
2329 * after "first" that is non-zero.
2331 static int extend_last_non_zero(__isl_keep isl_basic_map
*bmap
, int ineq
,
2332 int first
, unsigned len
)
2336 last
= isl_seq_last_non_zero(bmap
->ineq
[ineq
] + 1 + first
+ 1,
2341 return first
+ 1 + last
;
2344 /* Do the inequality constraints "i" and "j" of "bmap"
2345 * form a pair of opposite constraints, in the (first) "len" coefficients?
2347 static int is_constraint_pair(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
2350 return isl_seq_is_neg(bmap
->ineq
[i
] + 1, bmap
->ineq
[j
] + 1, len
);
2353 /* Given that inequality constraints "i" and "j" of "bmap"
2354 * form a pair of opposite constraints
2363 * do they allow for at most "bound" values in that direction?
2364 * That is, is the sum of their constant terms smaller than "bound"?
2366 * "tmp" is a temporary location that can be used to store the sum.
2368 static int constraint_pair_has_bound(__isl_keep isl_basic_map
*bmap
,
2369 int i
, int j
, isl_int bound
, isl_int
*tmp
)
2371 isl_int_add(*tmp
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
2372 return isl_int_abs_lt(*tmp
, bound
);
2375 /* Return the position of an inequality constraint in "bmap"
2376 * that together with inequality constraint "ineq" forms
2377 * a pair of opposite constraints that allow at most "bound" values
2378 * in their shared direction and that appears before "ineq".
2379 * Return a position beyond the number of inequality constraints
2380 * if no such constraint can be found.
2382 * The constraints of "bmap" are assumed to have been sorted.
2383 * This means that as soon as a constraint is found where the value
2384 * of the last coefficient (in absolute value) is different from that of "ineq",
2385 * no opposite constraint can be found.
2386 * It also means that only the coefficients up to this last coefficient
2387 * need to be compared.
2389 * "pos" is the position of a coefficient that is known to be non-zero.
2390 * If no such position is known a priori, then the value 0 can be passed in.
2391 * "len" is the number of (relevant) coefficients in the constraints.
2392 * "tmp" is a temporary location that can be used to store the sum.
2394 static isl_size
find_earlier_constraint_in_pair(__isl_keep isl_basic_map
*bmap
,
2395 int ineq
, int pos
, int len
, isl_int bound
, isl_int
*tmp
)
2401 n_ineq
= isl_basic_map_n_inequality(bmap
);
2403 return isl_size_error
;
2405 last
= extend_last_non_zero(bmap
, ineq
, pos
, len
);
2407 for (j
= ineq
- 1; j
>= 0; --j
) {
2408 if (!isl_int_abs_eq(bmap
->ineq
[ineq
][1 + last
],
2409 bmap
->ineq
[j
][1 + last
]))
2411 if (!is_constraint_pair(bmap
, ineq
, j
, last
+ 1))
2413 if (constraint_pair_has_bound(bmap
, ineq
, j
, bound
, tmp
))
2421 /* Return the position of an inequality constraint in "bmap"
2422 * that together with inequality constraint "ineq" forms
2423 * a pair of opposite constraints that allow at most "bound" values
2424 * in their shared direction and that appears after "ineq".
2425 * Return a position beyond the number of inequality constraints
2426 * if no such constraint can be found.
2428 * The constraints of "bmap" are assumed to have been sorted.
2429 * This means that as soon as a constraint is found where the value
2430 * of the last coefficient (in absolute value) is different from that of "ineq",
2431 * no opposite constraint can be found.
2432 * It also means that only the coefficients up to this last coefficient
2433 * need to be compared.
2435 * "pos" is the position of a coefficient that is known to be non-zero.
2436 * If no such position is known a priori, then the value 0 can be passed in.
2437 * "len" is the number of (relevant) coefficients in the constraints.
2438 * "tmp" is a temporary location that can be used to store the sum.
2440 static isl_size
find_later_constraint_in_pair(__isl_keep isl_basic_map
*bmap
,
2441 int ineq
, int pos
, int len
, isl_int bound
, isl_int
*tmp
)
2447 n_ineq
= isl_basic_map_n_inequality(bmap
);
2449 return isl_size_error
;
2451 last
= extend_last_non_zero(bmap
, ineq
, pos
, len
);
2453 for (j
= ineq
+ 1; j
< n_ineq
; ++j
) {
2454 if (!isl_int_abs_eq(bmap
->ineq
[ineq
][1 + last
],
2455 bmap
->ineq
[j
][1 + last
]))
2457 if (isl_seq_any_non_zero(bmap
->ineq
[j
] + 1 + last
+ 1,
2460 if (!is_constraint_pair(bmap
, ineq
, j
, last
+ 1))
2462 if (constraint_pair_has_bound(bmap
, ineq
, j
, bound
, tmp
))
2470 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2473 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2475 isl_int
*t
= bmap
->div
[a
];
2476 bmap
->div
[a
] = bmap
->div
[b
];
2480 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2481 * div definitions accordingly.
2483 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2489 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2491 return isl_basic_map_free(bmap
);
2493 swap_div(bmap
, a
, b
);
2495 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2496 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2498 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2499 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2501 for (i
= 0; i
< bmap
->n_div
; ++i
)
2502 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2508 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2510 isl_seq_cpy(c
, c
+ n
, rem
);
2511 isl_seq_clr(c
+ rem
, n
);
2514 /* Drop n dimensions starting at first.
2516 * In principle, this frees up some extra variables as the number
2517 * of columns remains constant, but we would have to extend
2518 * the div array too as the number of rows in this array is assumed
2519 * to be equal to extra.
2521 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2522 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2524 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2527 /* Move "n" divs starting at "first" to the end of the list of divs.
2529 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2530 unsigned first
, unsigned n
)
2535 if (first
+ n
== bmap
->n_div
)
2538 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2541 for (i
= 0; i
< n
; ++i
)
2542 div
[i
] = bmap
->div
[first
+ i
];
2543 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2544 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2545 for (i
= 0; i
< n
; ++i
)
2546 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2550 isl_basic_map_free(bmap
);
2555 #define TYPE isl_map
2557 #include "check_type_range_templ.c"
2559 /* Check that there are "n" dimensions of type "type" starting at "first"
2562 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2563 enum isl_dim_type type
, unsigned first
, unsigned n
)
2565 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2568 /* Drop "n" dimensions of type "type" starting at "first".
2569 * Perform the core computation, without cowing or
2570 * simplifying and finalizing the result.
2572 * In principle, this frees up some extra variables as the number
2573 * of columns remains constant, but we would have to extend
2574 * the div array too as the number of rows in this array is assumed
2575 * to be equal to extra.
2577 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2578 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2579 unsigned first
, unsigned n
)
2586 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2587 return isl_basic_map_free(bmap
);
2589 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2591 return isl_basic_map_free(bmap
);
2593 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2594 left
= total
- (offset
- 1) - n
;
2595 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2596 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2598 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2599 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2601 for (i
= 0; i
< bmap
->n_div
; ++i
)
2602 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2604 if (type
== isl_dim_div
) {
2605 bmap
= move_divs_last(bmap
, first
, n
);
2608 if (isl_basic_map_free_div(bmap
, n
) < 0)
2609 return isl_basic_map_free(bmap
);
2611 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2613 return isl_basic_map_free(bmap
);
2615 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2616 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2617 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2621 /* Drop "n" dimensions of type "type" starting at "first".
2623 * In principle, this frees up some extra variables as the number
2624 * of columns remains constant, but we would have to extend
2625 * the div array too as the number of rows in this array is assumed
2626 * to be equal to extra.
2628 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2629 enum isl_dim_type type
, unsigned first
, unsigned n
)
2633 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2636 bmap
= isl_basic_map_cow(bmap
);
2640 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2642 bmap
= isl_basic_map_simplify(bmap
);
2643 return isl_basic_map_finalize(bmap
);
2646 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2647 enum isl_dim_type type
, unsigned first
, unsigned n
)
2649 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2653 /* No longer consider "map" to be normalized.
2655 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2659 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2663 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2664 enum isl_dim_type type
, unsigned first
, unsigned n
)
2669 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2670 return isl_map_free(map
);
2672 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2674 map
= isl_map_cow(map
);
2678 for (i
= 0; i
< map
->n
; ++i
) {
2679 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2683 map
= isl_map_unmark_normalized(map
);
2685 space
= isl_map_take_space(map
);
2686 space
= isl_space_drop_dims(space
, type
, first
, n
);
2687 map
= isl_map_restore_space(map
, space
);
2695 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2696 enum isl_dim_type type
, unsigned first
, unsigned n
)
2698 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2701 /* Drop the integer division at position "div", which is assumed
2702 * not to appear in any of the constraints or
2703 * in any of the other integer divisions.
2705 * Since the integer division is redundant, there is no need to cow.
2707 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2708 __isl_take isl_basic_map
*bmap
, unsigned div
)
2710 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2713 /* Eliminate the specified n dimensions starting at first from the
2714 * constraints, without removing the dimensions from the space.
2715 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2717 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2718 enum isl_dim_type type
, unsigned first
, unsigned n
)
2725 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2726 return isl_map_free(map
);
2728 map
= isl_map_cow(map
);
2732 for (i
= 0; i
< map
->n
; ++i
) {
2733 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2743 /* Eliminate the specified n dimensions starting at first from the
2744 * constraints, without removing the dimensions from the space.
2745 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2747 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2748 enum isl_dim_type type
, unsigned first
, unsigned n
)
2750 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2753 /* Eliminate the specified n dimensions starting at first from the
2754 * constraints, without removing the dimensions from the space.
2755 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2757 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2758 unsigned first
, unsigned n
)
2760 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2763 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2764 __isl_take isl_basic_map
*bmap
)
2768 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2770 return isl_basic_map_free(bmap
);
2771 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2775 return isl_basic_map_finalize(bmap
);
2778 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2779 __isl_take isl_basic_set
*bset
)
2781 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2784 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2793 map
= isl_map_cow(map
);
2797 for (i
= 0; i
< map
->n
; ++i
) {
2798 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2808 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2810 return isl_map_remove_divs(set
);
2813 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2814 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2815 unsigned first
, unsigned n
)
2817 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2818 return isl_basic_map_free(bmap
);
2819 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2821 bmap
= isl_basic_map_eliminate_vars(bmap
,
2822 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2825 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2827 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2831 /* Does the local variable "div" of "bmap" have a known expression
2832 * that involves the "n" variables starting at "first"?
2834 isl_bool
isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map
*bmap
,
2835 int div
, unsigned first
, unsigned n
)
2839 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2840 if (unknown
< 0 || unknown
)
2841 return isl_bool_not(unknown
);
2842 if (isl_seq_any_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
))
2843 return isl_bool_true
;
2845 return isl_bool_false
;
2848 /* Return true if the definition of the given div (recursively) involves
2849 * any of the given variables.
2851 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2852 unsigned first
, unsigned n
)
2856 isl_size n_div
, v_div
;
2858 involves
= isl_basic_map_div_expr_involves_vars(bmap
, div
, first
, n
);
2859 if (involves
< 0 || involves
)
2862 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2863 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2864 if (n_div
< 0 || v_div
< 0)
2865 return isl_bool_error
;
2866 for (i
= n_div
- 1; i
>= 0; --i
) {
2869 if (isl_int_is_zero(bmap
->div
[div
][1 + 1 + v_div
+ i
]))
2871 involves
= div_involves_vars(bmap
, i
, first
, n
);
2872 if (involves
< 0 || involves
)
2876 return isl_bool_false
;
2879 /* Does the definition of any integer division involve
2880 * any of the given variables?
2882 isl_bool
isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map
*bmap
,
2883 unsigned first
, unsigned n
)
2888 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2890 return isl_bool_error
;
2892 for (i
= 0; i
< n_div
; ++i
) {
2895 has
= isl_basic_map_div_expr_involves_vars(bmap
, i
, first
, n
);
2900 return isl_bool_false
;
2903 /* Try and add a lower and/or upper bound on "div" to "bmap"
2904 * based on inequality "i".
2905 * "total" is the total number of variables (excluding the divs).
2906 * "v" is a temporary object that can be used during the calculations.
2907 * If "lb" is set, then a lower bound should be constructed.
2908 * If "ub" is set, then an upper bound should be constructed.
2910 * The calling function has already checked that the inequality does not
2911 * reference "div", but we still need to check that the inequality is
2912 * of the right form. We'll consider the case where we want to construct
2913 * a lower bound. The construction of upper bounds is similar.
2915 * Let "div" be of the form
2917 * q = floor((a + f(x))/d)
2919 * We essentially check if constraint "i" is of the form
2923 * so that we can use it to derive a lower bound on "div".
2924 * However, we allow a slightly more general form
2928 * with the condition that the coefficients of g(x) - f(x) are all
2930 * Rewriting this constraint as
2934 * adding a + f(x) to both sides and dividing by d, we obtain
2936 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2938 * Taking the floor on both sides, we obtain
2940 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2944 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2946 * In the case of an upper bound, we construct the constraint
2948 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2951 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2952 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2953 unsigned total
, isl_int v
, int lb
, int ub
)
2957 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2959 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2960 bmap
->div
[div
][1 + 1 + j
]);
2961 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2964 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2965 bmap
->div
[div
][1 + 1 + j
]);
2966 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2972 bmap
= isl_basic_map_cow(bmap
);
2973 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2975 int k
= isl_basic_map_alloc_inequality(bmap
);
2978 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2979 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2980 bmap
->div
[div
][1 + j
]);
2981 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2982 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2984 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2987 int k
= isl_basic_map_alloc_inequality(bmap
);
2990 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2991 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2992 bmap
->div
[div
][1 + j
]);
2993 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2994 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2996 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
3001 isl_basic_map_free(bmap
);
3005 /* This function is called right before "div" is eliminated from "bmap"
3006 * using Fourier-Motzkin.
3007 * Look through the constraints of "bmap" for constraints on the argument
3008 * of the integer division and use them to construct constraints on the
3009 * integer division itself. These constraints can then be combined
3010 * during the Fourier-Motzkin elimination.
3011 * Note that it is only useful to introduce lower bounds on "div"
3012 * if "bmap" already contains upper bounds on "div" as the newly
3013 * introduce lower bounds can then be combined with the pre-existing
3014 * upper bounds. Similarly for upper bounds.
3015 * We therefore first check if "bmap" contains any lower and/or upper bounds
3018 * It is interesting to note that the introduction of these constraints
3019 * can indeed lead to more accurate results, even when compared to
3020 * deriving constraints on the argument of "div" from constraints on "div".
3021 * Consider, for example, the set
3023 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
3025 * The second constraint can be rewritten as
3027 * 2 * [(-i-2j+3)/4] + k >= 0
3029 * from which we can derive
3031 * -i - 2j + 3 >= -2k
3037 * Combined with the first constraint, we obtain
3039 * -3 <= 3 + 2k or k >= -3
3041 * If, on the other hand we derive a constraint on [(i+2j)/4] from
3042 * the first constraint, we obtain
3044 * [(i + 2j)/4] >= [-3/4] = -1
3046 * Combining this constraint with the second constraint, we obtain
3050 static __isl_give isl_basic_map
*insert_bounds_on_div(
3051 __isl_take isl_basic_map
*bmap
, int div
)
3054 int check_lb
, check_ub
;
3061 if (isl_int_is_zero(bmap
->div
[div
][0]))
3064 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
3066 return isl_basic_map_free(bmap
);
3070 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
3071 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
3078 if (!check_lb
&& !check_ub
)
3083 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
3084 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
3087 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
3088 check_lb
, check_ub
);
3096 /* Remove all divs (recursively) involving any of the given variables
3097 * in their definitions.
3099 static __isl_give isl_basic_map
*remove_divs_involving_vars(
3100 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3104 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3107 involves
= div_involves_vars(bmap
, i
, first
, n
);
3109 return isl_basic_map_free(bmap
);
3112 bmap
= insert_bounds_on_div(bmap
, i
);
3113 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3122 /* Data structure for communicating data between detect_mod and
3123 * substitute_div_mod.
3125 * "pos" is the position of the variable that is being examined.
3127 * "lower_f" is the index of the constraint
3128 * -f(x) + m i + t m n h(alpha) >= 0
3129 * "lower_g" is the index of the constraint
3131 * "m" and "n" correspond to the values in the first constraint.
3132 * "sum" is a temporary variable that is used internally inside detect_mod.
3134 struct isl_detect_mod_data
{
3146 /* Initialize "data".
3148 static void isl_detect_mod_data_init(struct isl_detect_mod_data
*data
)
3150 isl_int_init(data
->m
);
3151 isl_int_init(data
->n
);
3152 isl_int_init(data
->sum
);
3155 /* Free any memory allocated by "data".
3157 static void isl_detect_mod_data_clear(struct isl_detect_mod_data
*data
)
3159 isl_int_clear(data
->sum
);
3160 isl_int_clear(data
->n
);
3161 isl_int_clear(data
->m
);
3164 /* Is the variable at position data->pos
3165 * equal to a specific case of a nested modulo?
3167 * In particular, look for two pairs of constraints
3169 * -f(x) + m i + t m n h(alpha) >= 0
3170 * f(x) - m i - t m n h(alpha) + c >= 0
3177 * where f(x) and g(x) are expressions in the other variables,
3178 * excluding local variables,
3179 * h(alpha) is a non-zero expression in the local variable,
3184 * If these pairs of constraints are found,
3185 * then store the constraint index of the first f-constraint in data->lower_f,
3186 * the index of the first g-constraint in data->lower_g and
3187 * the values m and n in data->m and data->n.
3189 * The constraints are assumed to have been sorted,
3190 * which means that the constraints in a pair are close to each other.
3191 * The sorting also means that the f-constraints appear
3192 * after the g-constraints.
3194 static isl_bool
detect_mod(__isl_keep isl_basic_map
*bmap
,
3195 struct isl_detect_mod_data
*data
)
3200 isl_size v_div
, n_div
, total
;
3202 n_ineq
= isl_basic_map_n_inequality(bmap
);
3203 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
3204 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3205 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3206 if (n_ineq
< 0 || v_div
< 0 || n_div
< 0 || total
< 0)
3207 return isl_bool_error
;
3209 return isl_bool_false
;
3211 for (i
= n_ineq
- 1; i
>= 1; --i
) {
3212 isl_seq_gcd(bmap
->ineq
[i
] + 1 + v_div
, n_div
, &data
->n
);
3213 if (isl_int_is_zero(data
->n
))
3214 return isl_bool_false
;
3215 isl_int_abs(data
->m
, bmap
->ineq
[i
][1 + data
->pos
]);
3216 j
= find_earlier_constraint_in_pair(bmap
, i
, data
->pos
, total
,
3217 data
->m
, &data
->sum
);
3219 return isl_bool_error
;
3222 if (!isl_int_is_divisible_by(data
->n
, data
->m
))
3224 if (isl_int_is_pos(bmap
->ineq
[i
][1 + data
->pos
]))
3228 isl_int_divexact(data
->n
, data
->n
, data
->m
);
3229 isl_int_abs(data
->n
, data
->n
);
3233 return isl_bool_false
;
3234 for (i
= j
- 1; i
>= 1; --i
) {
3235 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + v_div
, n_div
))
3237 if (!isl_int_is_one(bmap
->ineq
[i
][1 + data
->pos
]) &&
3238 !isl_int_is_negone(bmap
->ineq
[i
][1 + data
->pos
]))
3240 j
= find_earlier_constraint_in_pair(bmap
, i
, data
->pos
, v_div
,
3241 data
->m
, &data
->sum
);
3243 return isl_bool_error
;
3246 if (isl_int_is_pos(bmap
->ineq
[i
][1 + data
->pos
]))
3253 return isl_bool_false
;
3254 return isl_bool_true
;
3257 /* Given an affine expression "aff" in the variables of "bset" that expresses
3258 * a bound on the variable at position "pos" in terms of the other variables,
3259 * extract this expression as a function of those other variables,
3260 * excluding any local variables.
3262 static __isl_give isl_aff
*extract_aff(
3263 __isl_keep isl_basic_set
*bset
, isl_int
*aff
, int pos
)
3269 isl_local_space
*ls
;
3271 space
= isl_basic_set_peek_space(bset
);
3272 dim
= isl_space_dim(space
, isl_dim_all
);
3275 ls
= isl_local_space_from_space(isl_space_copy(space
));
3276 ctx
= isl_basic_set_get_ctx(bset
);
3277 v
= extract_bound_from_constraint(ctx
, aff
, dim
, pos
);
3278 return isl_aff_alloc_vec(ls
, v
);
3281 /* Given that inequality "ineq" of "bset" expresses a lower bound
3282 * on the variable at position "pos" in terms of the other variables,
3283 * extract this lower bound as a function of those other variables,
3284 * excluding any local variables.
3286 static __isl_give isl_aff
*extract_lower_bound_aff(
3287 __isl_keep isl_basic_set
*bset
, int ineq
, int pos
)
3291 return extract_aff(bset
, bset
->ineq
[ineq
], pos
);
3294 /* Given that there are two pairs of constraints
3296 * -f(x) + m i + t m n h(alpha) >= 0
3297 * f(x) - m i - t m n h(alpha) + c >= 0
3304 * where f(x) and g(x) are expressions in the other variables,
3305 * excluding local variables,
3306 * h(alpha) is a non-zero expression in the local variable,
3310 * derive an expression for i and plug that into "bmap".
3312 * In particular, the first pair of constraints implies
3314 * 0 <= (-f(x) + m i) % (m n) <= c
3315 * 0 <= (m floor(-f(x)/m) + (-f(x)) mod m + m i) % (m n) <= c
3316 * 0 <= (m floor(-f(x)/m) + m i) % (m n) + (-f(x)) mod m <= c
3317 * 0 <= m ((floor(-f(x)/m) + i) % n) + (-f(x)) mod m <= c
3321 * (floor(-f(x)/m) + i) % n = 0
3323 * because c < m, which can be rewritten to
3325 * (floor(-f(x)/m) + g(x) - g(x) + i) % n = 0
3329 * (-g(x) + i) % n = (-floor(-f(x)/m) - g(x)) % n
3331 * Since 0 <= -g(x) + i <= d < n,
3333 * -g(x) + i = (-floor(-f(x)/m) - g(x)) % n
3337 * i = (-floor(-f(x)/m) - g(x)) % n + g(x)
3339 * Note that the lower bounds on i derived from the original constraints are
3344 * (ignoring local variables).
3346 * The expression for i is plugged into "bmap", which results
3347 * in a basic map equivalent to "bmap" given i is equal to the expression,
3348 * so an equality constraint is added to ensure the meaning is preserved.
3350 static __isl_give isl_basic_map
*substitute_div_mod(
3351 __isl_take isl_basic_map
*bmap
, unsigned pos
,
3352 struct isl_detect_mod_data
*data
)
3355 isl_basic_set
*bset
, *eq
;
3357 isl_aff
*g
, *f
, *aff
, *it
;
3362 v_in
= isl_basic_map_var_offset(bmap
, isl_dim_in
);
3364 return isl_basic_map_free(bmap
);
3366 ctx
= isl_basic_map_get_ctx(bmap
);
3367 bset
= isl_basic_map_wrap(bmap
);
3368 space
= isl_basic_set_get_space(bset
);
3369 g
= extract_lower_bound_aff(bset
, data
->lower_g
, pos
);
3370 f
= extract_lower_bound_aff(bset
, data
->lower_f
, pos
);
3371 ma
= isl_space_identity_multi_aff_on_domain(space
);
3372 aff
= isl_aff_neg(isl_aff_floor(isl_aff_neg(f
)));
3373 aff
= isl_aff_sub(aff
, isl_aff_copy(g
));
3374 v
= isl_val_int_from_isl_int(ctx
, data
->n
);
3375 aff
= isl_aff_mod_val(aff
, v
);
3376 aff
= isl_aff_add(aff
, g
);
3377 it
= isl_multi_aff_get_at(ma
, pos
- v_in
);
3378 eq
= isl_aff_eq_basic_set(it
, isl_aff_copy(aff
));
3379 ma
= isl_multi_aff_set_at(ma
, pos
- v_in
, aff
);
3380 bset
= isl_basic_set_preimage_multi_aff(bset
, ma
);
3381 bset
= isl_basic_set_intersect(bset
, eq
);
3382 return isl_basic_set_unwrap(bset
);
3385 /* Remove all divs (recursively) involving any of the given dimensions
3386 * in their definitions.
3388 * If it is a single input or output dimension that
3389 * should not appear in any integer division expression and
3390 * if there is indeed an integer division expression
3391 * involving that dimension, then check if there are any constraints
3392 * that imply that the given dimension is equal to some expression
3393 * in the other dimensions.
3394 * If so, plug in that expression for the given dimension
3395 * so that the integer division expressions no longer
3396 * involve that dimension.
3398 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
3399 __isl_take isl_basic_map
*bmap
,
3400 enum isl_dim_type type
, unsigned first
, unsigned n
)
3405 struct isl_detect_mod_data data
;
3407 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3408 return isl_basic_map_free(bmap
);
3409 off
= isl_basic_map_var_offset(bmap
, type
);
3411 return isl_basic_map_free(bmap
);
3414 if (type
== isl_dim_param
|| type
== isl_dim_div
|| n
!= 1)
3415 return remove_divs_involving_vars(bmap
, first
, n
);
3416 used
= isl_basic_map_any_div_involves_vars(bmap
, first
, n
);
3418 return isl_basic_map_free(bmap
);
3421 bmap
= isl_basic_map_sort_constraints(bmap
);
3422 isl_detect_mod_data_init(&data
);
3424 found
= detect_mod(bmap
, &data
);
3426 bmap
= remove_divs_involving_vars(bmap
, first
, n
);
3428 bmap
= substitute_div_mod(bmap
, first
, &data
);
3429 isl_detect_mod_data_clear(&data
);
3433 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
3434 __isl_take isl_basic_set
*bset
,
3435 enum isl_dim_type type
, unsigned first
, unsigned n
)
3437 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
3440 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
3441 enum isl_dim_type type
, unsigned first
, unsigned n
)
3450 map
= isl_map_cow(map
);
3454 for (i
= 0; i
< map
->n
; ++i
) {
3455 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
3466 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
3467 enum isl_dim_type type
, unsigned first
, unsigned n
)
3469 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
3473 /* Does the description of "bmap" depend on the specified dimensions?
3474 * We also check whether the dimensions appear in any of the div definitions.
3475 * In principle there is no need for this check. If the dimensions appear
3476 * in a div definition, they also appear in the defining constraints of that
3479 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
3480 enum isl_dim_type type
, unsigned first
, unsigned n
)
3484 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3485 return isl_bool_error
;
3487 first
+= isl_basic_map_offset(bmap
, type
);
3488 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3489 if (isl_seq_any_non_zero(bmap
->eq
[i
] + first
, n
))
3490 return isl_bool_true
;
3491 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3492 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + first
, n
))
3493 return isl_bool_true
;
3494 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3495 if (isl_int_is_zero(bmap
->div
[i
][0]))
3497 if (isl_seq_any_non_zero(bmap
->div
[i
] + 1 + first
, n
))
3498 return isl_bool_true
;
3501 return isl_bool_false
;
3504 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
3505 enum isl_dim_type type
, unsigned first
, unsigned n
)
3509 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3510 return isl_bool_error
;
3512 for (i
= 0; i
< map
->n
; ++i
) {
3513 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
3515 if (involves
< 0 || involves
)
3519 return isl_bool_false
;
3522 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
3523 enum isl_dim_type type
, unsigned first
, unsigned n
)
3525 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
3528 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
3529 enum isl_dim_type type
, unsigned first
, unsigned n
)
3531 return isl_map_involves_dims(set
, type
, first
, n
);
3534 /* Does "bset" involve any local variables, i.e., integer divisions?
3536 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3540 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3542 return isl_bool_error
;
3543 return isl_bool_ok(n
> 0);
3546 /* isl_set_every_basic_set callback that checks whether "bset"
3547 * is free of local variables.
3549 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3551 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3554 /* Does "set" involve any local variables, i.e., integer divisions?
3556 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3560 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3561 return isl_bool_not(no_locals
);
3564 /* Drop all constraints in bmap that involve any of the dimensions
3565 * first to first+n-1.
3566 * This function only performs the actual removal of constraints.
3568 * This function should not call finalize since it is used by
3569 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3571 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3572 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3579 bmap
= isl_basic_map_cow(bmap
);
3584 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3585 if (!isl_seq_any_non_zero(bmap
->eq
[i
] + 1 + first
, n
))
3587 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3588 return isl_basic_map_free(bmap
);
3591 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3592 if (!isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + first
, n
))
3594 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3595 return isl_basic_map_free(bmap
);
3601 /* Drop all constraints in bset that involve any of the dimensions
3602 * first to first+n-1.
3603 * This function only performs the actual removal of constraints.
3605 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3606 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3608 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3611 /* Drop all constraints in bmap that do not involve any of the dimensions
3612 * first to first + n - 1 of the given type.
3614 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3615 __isl_take isl_basic_map
*bmap
,
3616 enum isl_dim_type type
, unsigned first
, unsigned n
)
3621 isl_space
*space
= isl_basic_map_get_space(bmap
);
3622 isl_basic_map_free(bmap
);
3623 return isl_basic_map_universe(space
);
3625 bmap
= isl_basic_map_cow(bmap
);
3629 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3630 return isl_basic_map_free(bmap
);
3632 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3634 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3635 if (isl_seq_any_non_zero(bmap
->eq
[i
] + 1 + first
, n
))
3637 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3638 return isl_basic_map_free(bmap
);
3641 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3642 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + first
, n
))
3644 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3645 return isl_basic_map_free(bmap
);
3648 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3652 /* Drop all constraints in bset that do not involve any of the dimensions
3653 * first to first + n - 1 of the given type.
3655 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3656 __isl_take isl_basic_set
*bset
,
3657 enum isl_dim_type type
, unsigned first
, unsigned n
)
3659 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3663 /* Drop all constraints in bmap that involve any of the dimensions
3664 * first to first + n - 1 of the given type.
3666 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3667 __isl_take isl_basic_map
*bmap
,
3668 enum isl_dim_type type
, unsigned first
, unsigned n
)
3675 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3676 return isl_basic_map_free(bmap
);
3678 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3679 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3680 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3681 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3685 /* Drop all constraints in bset that involve any of the dimensions
3686 * first to first + n - 1 of the given type.
3688 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3689 __isl_take isl_basic_set
*bset
,
3690 enum isl_dim_type type
, unsigned first
, unsigned n
)
3692 return isl_basic_map_drop_constraints_involving_dims(bset
,
3696 /* Drop constraints from "map" by applying "drop" to each basic map.
3698 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3699 enum isl_dim_type type
, unsigned first
, unsigned n
,
3700 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3701 enum isl_dim_type type
, unsigned first
, unsigned n
))
3705 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3706 return isl_map_free(map
);
3708 map
= isl_map_cow(map
);
3712 for (i
= 0; i
< map
->n
; ++i
) {
3713 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3715 return isl_map_free(map
);
3719 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3724 /* Drop all constraints in map that involve any of the dimensions
3725 * first to first + n - 1 of the given type.
3727 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3728 __isl_take isl_map
*map
,
3729 enum isl_dim_type type
, unsigned first
, unsigned n
)
3733 return drop_constraints(map
, type
, first
, n
,
3734 &isl_basic_map_drop_constraints_involving_dims
);
3737 /* Drop all constraints in "map" that do not involve any of the dimensions
3738 * first to first + n - 1 of the given type.
3740 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3741 __isl_take isl_map
*map
,
3742 enum isl_dim_type type
, unsigned first
, unsigned n
)
3745 isl_space
*space
= isl_map_get_space(map
);
3747 return isl_map_universe(space
);
3749 return drop_constraints(map
, type
, first
, n
,
3750 &isl_basic_map_drop_constraints_not_involving_dims
);
3753 /* Drop all constraints in set that involve any of the dimensions
3754 * first to first + n - 1 of the given type.
3756 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3757 __isl_take isl_set
*set
,
3758 enum isl_dim_type type
, unsigned first
, unsigned n
)
3760 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3763 /* Drop all constraints in "set" that do not involve any of the dimensions
3764 * first to first + n - 1 of the given type.
3766 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3767 __isl_take isl_set
*set
,
3768 enum isl_dim_type type
, unsigned first
, unsigned n
)
3770 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3773 /* Does local variable "div" of "bmap" have a complete explicit representation?
3774 * Having a complete explicit representation requires not only
3775 * an explicit representation, but also that all local variables
3776 * that appear in this explicit representation in turn have
3777 * a complete explicit representation.
3779 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3782 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3785 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3786 if (marked
< 0 || marked
)
3787 return isl_bool_not(marked
);
3789 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3792 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3794 known
= isl_basic_map_div_is_known(bmap
, i
);
3795 if (known
< 0 || !known
)
3799 return isl_bool_true
;
3802 /* Remove all divs that are unknown or defined in terms of unknown divs.
3804 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3805 __isl_take isl_basic_map
*bmap
)
3812 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3813 if (isl_basic_map_div_is_known(bmap
, i
))
3815 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3824 /* Remove all divs that are unknown or defined in terms of unknown divs.
3826 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3827 __isl_take isl_basic_set
*bset
)
3829 return isl_basic_map_remove_unknown_divs(bset
);
3832 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3841 map
= isl_map_cow(map
);
3845 for (i
= 0; i
< map
->n
; ++i
) {
3846 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3856 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3858 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3861 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3862 __isl_take isl_basic_set
*bset
,
3863 enum isl_dim_type type
, unsigned first
, unsigned n
)
3865 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3866 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3867 return bset_from_bmap(bmap
);
3870 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3871 enum isl_dim_type type
, unsigned first
, unsigned n
)
3878 map
= isl_map_cow(map
);
3879 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3880 return isl_map_free(map
);
3882 for (i
= 0; i
< map
->n
; ++i
) {
3883 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3884 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3888 map
= isl_map_drop(map
, type
, first
, n
);
3895 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3896 enum isl_dim_type type
, unsigned first
, unsigned n
)
3898 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3902 /* Project out n inputs starting at first using Fourier-Motzkin */
3903 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3904 unsigned first
, unsigned n
)
3906 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3909 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3910 FILE *out
, int indent
)
3915 fprintf(out
, "null basic set\n");
3919 fprintf(out
, "%*s", indent
, "");
3920 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3921 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3922 bset
->extra
, bset
->flags
);
3924 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3925 p
= isl_printer_set_dump(p
, 1);
3926 p
= isl_printer_set_indent(p
, indent
);
3927 p
= isl_printer_start_line(p
);
3928 p
= isl_printer_print_basic_set(p
, bset
);
3929 p
= isl_printer_end_line(p
);
3930 isl_printer_free(p
);
3933 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3934 FILE *out
, int indent
)
3939 fprintf(out
, "null basic map\n");
3943 fprintf(out
, "%*s", indent
, "");
3944 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3945 "flags: %x, n_name: %d\n",
3947 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3948 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3950 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3951 p
= isl_printer_set_dump(p
, 1);
3952 p
= isl_printer_set_indent(p
, indent
);
3953 p
= isl_printer_start_line(p
);
3954 p
= isl_printer_print_basic_map(p
, bmap
);
3955 p
= isl_printer_end_line(p
);
3956 isl_printer_free(p
);
3959 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3964 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3966 return isl_basic_map_free(bmap
);
3967 if (pos
>= bmap
->n_ineq
)
3968 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3969 "invalid position", return isl_basic_map_free(bmap
));
3970 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3971 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3972 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3973 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3977 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3980 if (isl_space_check_is_set(space
) < 0)
3982 return isl_map_alloc_space(space
, n
, flags
);
3984 isl_space_free(space
);
3988 /* Make sure "map" has room for at least "n" more basic maps.
3990 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3993 struct isl_map
*grown
= NULL
;
3997 isl_assert(map
->ctx
, n
>= 0, goto error
);
3998 if (map
->n
+ n
<= map
->size
)
4000 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
4003 for (i
= 0; i
< map
->n
; ++i
) {
4004 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
4012 isl_map_free(grown
);
4017 /* Make sure "set" has room for at least "n" more basic sets.
4019 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
4021 return set_from_map(isl_map_grow(set_to_map(set
), n
));
4024 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
4026 return isl_map_from_basic_map(bset
);
4029 /* This function performs the same operation as isl_set_from_basic_set,
4030 * but is considered as a function on an isl_basic_set when exported.
4032 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
4034 return isl_set_from_basic_set(bset
);
4037 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
4039 struct isl_map
*map
;
4044 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
4045 return isl_map_add_basic_map(map
, bmap
);
4048 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
4049 __isl_take isl_basic_set
*bset
)
4051 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
4052 bset_to_bmap(bset
)));
4055 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
4057 return isl_map_free(set
);
4060 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
4065 fprintf(out
, "null set\n");
4069 fprintf(out
, "%*s", indent
, "");
4070 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
4071 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
4073 for (i
= 0; i
< set
->n
; ++i
) {
4074 fprintf(out
, "%*s", indent
, "");
4075 fprintf(out
, "basic set %d:\n", i
);
4076 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
4080 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
4085 fprintf(out
, "null map\n");
4089 fprintf(out
, "%*s", indent
, "");
4090 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
4091 "flags: %x, n_name: %d\n",
4092 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
4093 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
4094 for (i
= 0; i
< map
->n
; ++i
) {
4095 fprintf(out
, "%*s", indent
, "");
4096 fprintf(out
, "basic map %d:\n", i
);
4097 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
4101 /* Check that the space of "bset" is the same as that of the domain of "bmap".
4103 static isl_stat
isl_basic_map_check_compatible_domain(
4104 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
4108 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
4110 return isl_stat_error
;
4112 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
4113 "incompatible spaces", return isl_stat_error
);
4118 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
4119 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4121 struct isl_basic_map
*bmap_domain
;
4124 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
4127 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4131 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4134 bmap
= isl_basic_map_cow(bmap
);
4137 bmap
= isl_basic_map_extend(bmap
,
4138 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
4139 bmap_domain
= isl_basic_map_from_domain(bset
);
4140 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
4142 bmap
= isl_basic_map_simplify(bmap
);
4143 return isl_basic_map_finalize(bmap
);
4145 isl_basic_map_free(bmap
);
4146 isl_basic_set_free(bset
);
4150 /* Check that the space of "bset" is the same as that of the range of "bmap".
4152 static isl_stat
isl_basic_map_check_compatible_range(
4153 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
4157 ok
= isl_basic_map_compatible_range(bmap
, bset
);
4159 return isl_stat_error
;
4161 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
4162 "incompatible spaces", return isl_stat_error
);
4167 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
4168 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4170 struct isl_basic_map
*bmap_range
;
4173 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
4176 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4179 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
4182 if (isl_basic_set_plain_is_universe(bset
)) {
4183 isl_basic_set_free(bset
);
4187 bmap
= isl_basic_map_cow(bmap
);
4190 bmap
= isl_basic_map_extend(bmap
,
4191 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
4192 bmap_range
= bset_to_bmap(bset
);
4193 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
4195 bmap
= isl_basic_map_simplify(bmap
);
4196 return isl_basic_map_finalize(bmap
);
4198 isl_basic_map_free(bmap
);
4199 isl_basic_set_free(bset
);
4203 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
4204 __isl_keep isl_vec
*vec
)
4210 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4211 if (total
< 0 || !vec
)
4212 return isl_bool_error
;
4214 if (1 + total
!= vec
->size
)
4215 return isl_bool_false
;
4219 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4220 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
4221 if (!isl_int_is_zero(s
)) {
4223 return isl_bool_false
;
4227 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4228 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
4229 if (isl_int_is_neg(s
)) {
4231 return isl_bool_false
;
4237 return isl_bool_true
;
4240 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
4241 __isl_keep isl_vec
*vec
)
4243 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
4246 __isl_give isl_basic_map
*isl_basic_map_intersect(
4247 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4249 struct isl_vec
*sample
= NULL
;
4250 isl_space
*space1
, *space2
;
4251 isl_size dim1
, dim2
, nparam1
, nparam2
;
4253 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4255 space1
= isl_basic_map_peek_space(bmap1
);
4256 space2
= isl_basic_map_peek_space(bmap2
);
4257 dim1
= isl_space_dim(space1
, isl_dim_all
);
4258 dim2
= isl_space_dim(space2
, isl_dim_all
);
4259 nparam1
= isl_space_dim(space1
, isl_dim_param
);
4260 nparam2
= isl_space_dim(space2
, isl_dim_param
);
4261 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
4263 if (dim1
== nparam1
&& dim2
!= nparam2
)
4264 return isl_basic_map_intersect(bmap2
, bmap1
);
4266 if (dim2
!= nparam2
&&
4267 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4270 if (isl_basic_map_plain_is_empty(bmap1
)) {
4271 isl_basic_map_free(bmap2
);
4274 if (isl_basic_map_plain_is_empty(bmap2
)) {
4275 isl_basic_map_free(bmap1
);
4279 if (bmap1
->sample
&&
4280 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
4281 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
4282 sample
= isl_vec_copy(bmap1
->sample
);
4283 else if (bmap2
->sample
&&
4284 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
4285 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
4286 sample
= isl_vec_copy(bmap2
->sample
);
4288 bmap1
= isl_basic_map_cow(bmap1
);
4291 bmap1
= isl_basic_map_extend(bmap1
,
4292 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4293 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
4296 isl_vec_free(sample
);
4298 isl_vec_free(bmap1
->sample
);
4299 bmap1
->sample
= sample
;
4302 bmap1
= isl_basic_map_simplify(bmap1
);
4303 return isl_basic_map_finalize(bmap1
);
4306 isl_vec_free(sample
);
4307 isl_basic_map_free(bmap1
);
4308 isl_basic_map_free(bmap2
);
4312 __isl_give isl_basic_set
*isl_basic_set_intersect(
4313 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
4315 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
4316 bset_to_bmap(bset2
)));
4319 /* Intersect the parameter domain of "bmap" with "bset".
4321 * isl_basic_map_intersect handles this as a special case.
4323 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
4324 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
4326 return isl_basic_map_intersect(bmap
, bset
);
4329 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
4330 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
4332 isl_basic_map
*bmap
;
4334 bmap
= bset_to_bmap(bset1
);
4335 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
4336 return bset_from_bmap(bmap
);
4339 /* Does "map" consist of a single disjunct, without any local variables?
4341 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
4346 return isl_bool_error
;
4348 return isl_bool_false
;
4349 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
4351 return isl_bool_error
;
4353 return isl_bool_false
;
4354 return isl_bool_true
;
4357 /* Check that "map" consists of a single disjunct, without any local variables.
4359 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
4363 ok
= is_convex_no_locals(map
);
4365 return isl_stat_error
;
4369 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
4370 "unexpectedly not convex or involving local variables",
4371 return isl_stat_error
);
4374 /* Special case of isl_map_intersect, where both map1 and map2
4375 * are convex, without any divs and such that either map1 or map2
4376 * contains a single constraint. This constraint is then simply
4377 * added to the other map.
4379 static __isl_give isl_map
*map_intersect_add_constraint(
4380 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
4382 if (check_convex_no_locals(map1
) < 0 ||
4383 check_convex_no_locals(map2
) < 0)
4386 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
4387 return isl_map_intersect(map2
, map1
);
4389 map1
= isl_map_cow(map1
);
4392 if (isl_map_plain_is_empty(map1
)) {
4396 if (map2
->p
[0]->n_eq
== 1)
4397 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
4399 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
4400 map2
->p
[0]->ineq
[0]);
4402 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
4403 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
4407 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
4408 isl_basic_map_free(map1
->p
[0]);
4414 map1
= isl_map_unmark_normalized(map1
);
4422 /* map2 may be either a parameter domain or a map living in the same
4425 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
4426 __isl_take isl_map
*map2
)
4432 isl_size dim2
, nparam2
;
4437 if ((isl_map_plain_is_empty(map1
) ||
4438 isl_map_plain_is_universe(map2
)) &&
4439 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4443 if ((isl_map_plain_is_empty(map2
) ||
4444 isl_map_plain_is_universe(map1
)) &&
4445 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4450 if (is_convex_no_locals(map1
) == isl_bool_true
&&
4451 is_convex_no_locals(map2
) == isl_bool_true
&&
4452 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
4453 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
4454 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
4455 return map_intersect_add_constraint(map1
, map2
);
4457 equal
= isl_map_plain_is_equal(map1
, map2
);
4465 dim2
= isl_map_dim(map2
, isl_dim_all
);
4466 nparam2
= isl_map_dim(map2
, isl_dim_param
);
4467 if (dim2
< 0 || nparam2
< 0)
4469 if (dim2
!= nparam2
)
4470 isl_assert(map1
->ctx
,
4471 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4473 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4474 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4475 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4477 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4478 map1
->n
* map2
->n
, flags
);
4481 for (i
= 0; i
< map1
->n
; ++i
)
4482 for (j
= 0; j
< map2
->n
; ++j
) {
4483 struct isl_basic_map
*part
;
4484 part
= isl_basic_map_intersect(
4485 isl_basic_map_copy(map1
->p
[i
]),
4486 isl_basic_map_copy(map2
->p
[j
]));
4487 if (isl_basic_map_is_empty(part
) < 0)
4488 part
= isl_basic_map_free(part
);
4489 result
= isl_map_add_basic_map(result
, part
);
4502 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
4503 __isl_take isl_map
*map2
)
4505 if (isl_map_check_equal_space(map1
, map2
) < 0)
4507 return map_intersect_internal(map1
, map2
);
4514 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
4515 __isl_take isl_map
*map2
)
4517 isl_map_align_params_bin(&map1
, &map2
);
4518 return map_intersect(map1
, map2
);
4521 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
4522 __isl_take isl_set
*set2
)
4524 return set_from_map(isl_map_intersect(set_to_map(set1
),
4528 /* map_intersect_internal accepts intersections
4529 * with parameter domains, so we can just call that function.
4531 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
4532 __isl_take isl_set
*params
)
4534 isl_map_align_params_set(&map
, ¶ms
);
4535 return map_intersect_internal(map
, params
);
4538 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4539 __isl_take isl_set
*params
)
4541 return isl_map_intersect_params(set
, params
);
4544 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4552 bmap
= isl_basic_map_cow(bmap
);
4555 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4556 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4557 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4558 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4559 if (n1
< 0 || n2
< 0)
4560 bmap
= isl_basic_map_free(bmap
);
4561 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4562 return isl_basic_map_reset_space(bmap
, space
);
4565 /* Given a basic map where the tuple of type "type" is a wrapped map,
4566 * swap domain and range of that wrapped map.
4568 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4569 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4572 isl_size offset
, n1
, n2
;
4574 space
= isl_basic_map_peek_space(bmap
);
4575 offset
= isl_basic_map_var_offset(bmap
, type
);
4576 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4577 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4578 if (offset
< 0 || n1
< 0 || n2
< 0)
4579 return isl_basic_map_free(bmap
);
4581 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4583 space
= isl_basic_map_take_space(bmap
);
4584 space
= isl_space_reverse_wrapped(space
, type
);
4585 bmap
= isl_basic_map_restore_space(bmap
, space
);
4590 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4593 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4594 __isl_take isl_basic_map
*bmap
)
4598 space
= isl_basic_map_peek_space(bmap
);
4599 if (isl_space_check_domain_is_wrapping(space
) < 0)
4600 return isl_basic_map_free(bmap
);
4601 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4606 /* Given a basic map A -> (B -> C), return the corresponding basic map
4609 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4610 __isl_take isl_basic_map
*bmap
)
4614 space
= isl_basic_map_peek_space(bmap
);
4615 if (isl_space_check_range_is_wrapping(space
) < 0)
4616 return isl_basic_map_free(bmap
);
4617 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4622 /* Given a basic map that is actually a basic set (A -> B),
4623 * return the corresponding basic set (B -> A) as a basic map.
4625 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4626 __isl_take isl_basic_map
*bmap
)
4630 space
= isl_basic_map_peek_space(bmap
);
4631 if (isl_space_check_is_wrapping(space
) < 0)
4632 return isl_basic_map_free(bmap
);
4633 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4638 static __isl_give isl_basic_map
*basic_map_space_reset(
4639 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4645 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4648 space
= isl_basic_map_get_space(bmap
);
4649 space
= isl_space_reset(space
, type
);
4650 bmap
= isl_basic_map_reset_space(bmap
, space
);
4654 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4655 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4656 unsigned pos
, unsigned n
)
4658 isl_bool rational
, is_empty
;
4659 isl_space
*res_space
;
4660 struct isl_basic_map
*res
;
4661 struct isl_dim_map
*dim_map
;
4664 enum isl_dim_type t
;
4667 return basic_map_space_reset(bmap
, type
);
4669 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4670 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4671 if (is_empty
< 0 || total
< 0)
4672 return isl_basic_map_free(bmap
);
4673 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4676 return isl_basic_map_free(bmap
);
4678 isl_basic_map_free(bmap
);
4679 return isl_basic_map_empty(res_space
);
4682 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4684 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4688 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4690 isl_size size
= isl_basic_map_dim(bmap
, t
);
4692 dim_map
= isl_dim_map_free(dim_map
);
4693 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4695 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4696 pos
, size
- pos
, off
+ pos
+ n
);
4698 dim
= isl_space_dim(res_space
, t
);
4700 dim_map
= isl_dim_map_free(dim_map
);
4703 isl_dim_map_div(dim_map
, bmap
, off
);
4705 res
= isl_basic_map_alloc_space(res_space
,
4706 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4707 rational
= isl_basic_map_is_rational(bmap
);
4709 res
= isl_basic_map_free(res
);
4711 res
= isl_basic_map_set_rational(res
);
4712 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4713 return isl_basic_map_finalize(res
);
4716 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4717 __isl_take isl_basic_set
*bset
,
4718 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4720 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4723 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4724 enum isl_dim_type type
, unsigned n
)
4728 dim
= isl_basic_map_dim(bmap
, type
);
4730 return isl_basic_map_free(bmap
);
4731 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4734 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4735 enum isl_dim_type type
, unsigned n
)
4739 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4740 return isl_basic_map_add_dims(bset
, type
, n
);
4742 isl_basic_set_free(bset
);
4746 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4747 enum isl_dim_type type
)
4751 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4754 space
= isl_map_get_space(map
);
4755 space
= isl_space_reset(space
, type
);
4756 map
= isl_map_reset_space(map
, space
);
4760 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4761 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4767 return map_space_reset(map
, type
);
4769 map
= isl_map_cow(map
);
4773 for (i
= 0; i
< map
->n
; ++i
) {
4774 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4779 space
= isl_map_take_space(map
);
4780 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4781 map
= isl_map_restore_space(map
, space
);
4789 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4790 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4792 return isl_map_insert_dims(set
, type
, pos
, n
);
4795 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4796 enum isl_dim_type type
, unsigned n
)
4800 dim
= isl_map_dim(map
, type
);
4802 return isl_map_free(map
);
4803 return isl_map_insert_dims(map
, type
, dim
, n
);
4806 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4807 enum isl_dim_type type
, unsigned n
)
4811 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4812 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4818 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4819 __isl_take isl_basic_map
*bmap
,
4820 enum isl_dim_type dst_type
, unsigned dst_pos
,
4821 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4824 struct isl_dim_map
*dim_map
;
4825 struct isl_basic_map
*res
;
4826 enum isl_dim_type t
;
4833 bmap
= isl_basic_map_reset(bmap
, src_type
);
4834 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4838 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4839 return isl_basic_map_free(bmap
);
4841 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4844 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4846 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4847 pos(bmap
->dim
, src_type
) + src_pos
+
4848 ((src_type
< dst_type
) ? n
: 0)) {
4849 space
= isl_basic_map_take_space(bmap
);
4850 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4851 src_type
, src_pos
, n
);
4852 bmap
= isl_basic_map_restore_space(bmap
, space
);
4853 bmap
= isl_basic_map_finalize(bmap
);
4858 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4860 return isl_basic_map_free(bmap
);
4861 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4864 space
= isl_basic_map_peek_space(bmap
);
4865 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4866 isl_size size
= isl_space_dim(space
, t
);
4868 dim_map
= isl_dim_map_free(dim_map
);
4869 if (t
== dst_type
) {
4870 isl_dim_map_dim_range(dim_map
, space
, t
,
4873 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4876 isl_dim_map_dim_range(dim_map
, space
, t
,
4877 dst_pos
, size
- dst_pos
, off
);
4878 off
+= size
- dst_pos
;
4879 } else if (t
== src_type
) {
4880 isl_dim_map_dim_range(dim_map
, space
, t
,
4883 isl_dim_map_dim_range(dim_map
, space
, t
,
4884 src_pos
+ n
, size
- src_pos
- n
, off
);
4885 off
+= size
- src_pos
- n
;
4887 isl_dim_map_dim(dim_map
, space
, t
, off
);
4891 isl_dim_map_div(dim_map
, bmap
, off
);
4893 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4894 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4895 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4896 space
= isl_basic_map_take_space(bmap
);
4897 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4898 src_type
, src_pos
, n
);
4899 bmap
= isl_basic_map_restore_space(bmap
, space
);
4903 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4904 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4905 bmap
= isl_basic_map_finalize(bmap
);
4909 isl_basic_map_free(bmap
);
4913 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4914 enum isl_dim_type dst_type
, unsigned dst_pos
,
4915 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4917 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4918 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4919 src_type
, src_pos
, n
);
4920 return bset_from_bmap(bmap
);
4923 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4924 enum isl_dim_type dst_type
, unsigned dst_pos
,
4925 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4929 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4930 return set_from_map(isl_map_move_dims(set_to_map(set
),
4931 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4937 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4938 enum isl_dim_type dst_type
, unsigned dst_pos
,
4939 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4945 map
= isl_map_reset(map
, src_type
);
4946 map
= isl_map_reset(map
, dst_type
);
4950 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4951 return isl_map_free(map
);
4953 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4956 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4958 map
= isl_map_cow(map
);
4962 for (i
= 0; i
< map
->n
; ++i
) {
4963 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4965 src_type
, src_pos
, n
);
4970 space
= isl_map_take_space(map
);
4971 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4972 src_type
, src_pos
, n
);
4973 map
= isl_map_restore_space(map
, space
);
4981 /* Move the specified dimensions to the last columns right before
4982 * the divs. Don't change the dimension specification of bmap.
4983 * That's the responsibility of the caller.
4985 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4986 enum isl_dim_type type
, unsigned first
, unsigned n
)
4989 struct isl_dim_map
*dim_map
;
4990 struct isl_basic_map
*res
;
4991 enum isl_dim_type t
;
4997 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4998 isl_basic_map_offset(bmap
, isl_dim_div
))
5001 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5003 return isl_basic_map_free(bmap
);
5004 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5007 space
= isl_basic_map_peek_space(bmap
);
5008 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
5009 isl_size size
= isl_space_dim(space
, t
);
5011 dim_map
= isl_dim_map_free(dim_map
);
5013 isl_dim_map_dim_range(dim_map
, space
, t
,
5016 isl_dim_map_dim_range(dim_map
, space
, t
,
5017 first
, n
, total
- bmap
->n_div
- n
);
5018 isl_dim_map_dim_range(dim_map
, space
, t
,
5019 first
+ n
, size
- (first
+ n
), off
);
5020 off
+= size
- (first
+ n
);
5022 isl_dim_map_dim(dim_map
, space
, t
, off
);
5026 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
5028 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
5029 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
5030 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
5034 /* Insert "n" rows in the divs of "bmap".
5036 * The number of columns is not changed, which means that the last
5037 * dimensions of "bmap" are being reintepreted as the new divs.
5038 * The space of "bmap" is not adjusted, however, which means
5039 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
5040 * from the space of "bmap" is the responsibility of the caller.
5042 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
5050 bmap
= isl_basic_map_cow(bmap
);
5054 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
5055 old
= bmap
->block2
.data
;
5056 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
5057 (bmap
->extra
+ n
) * (1 + row_size
));
5058 if (!bmap
->block2
.data
)
5059 return isl_basic_map_free(bmap
);
5060 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
5062 return isl_basic_map_free(bmap
);
5063 for (i
= 0; i
< n
; ++i
) {
5064 new_div
[i
] = bmap
->block2
.data
+
5065 (bmap
->extra
+ i
) * (1 + row_size
);
5066 isl_seq_clr(new_div
[i
], 1 + row_size
);
5068 for (i
= 0; i
< bmap
->extra
; ++i
)
5069 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
5071 bmap
->div
= new_div
;
5078 /* Drop constraints from "bmap" that only involve the variables
5079 * of "type" in the range [first, first + n] that are not related
5080 * to any of the variables outside that interval.
5081 * These constraints cannot influence the values for the variables
5082 * outside the interval, except in case they cause "bmap" to be empty.
5083 * Only drop the constraints if "bmap" is known to be non-empty.
5085 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
5086 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
5087 unsigned first
, unsigned n
)
5091 isl_size dim
, n_div
;
5094 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
5096 return isl_basic_map_free(bmap
);
5100 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
5101 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5102 if (dim
< 0 || n_div
< 0)
5103 return isl_basic_map_free(bmap
);
5104 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
5106 return isl_basic_map_free(bmap
);
5107 first
+= isl_basic_map_offset(bmap
, type
) - 1;
5108 for (i
= 0; i
< first
; ++i
)
5110 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
5113 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
5118 /* Turn the n dimensions of type type, starting at first
5119 * into existentially quantified variables.
5121 * If a subset of the projected out variables are unrelated
5122 * to any of the variables that remain, then the constraints
5123 * involving this subset are simply dropped first.
5125 __isl_give isl_basic_map
*isl_basic_map_project_out(
5126 __isl_take isl_basic_map
*bmap
,
5127 enum isl_dim_type type
, unsigned first
, unsigned n
)
5133 return basic_map_space_reset(bmap
, type
);
5134 if (type
== isl_dim_div
)
5135 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5136 "cannot project out existentially quantified variables",
5137 return isl_basic_map_free(bmap
));
5139 empty
= isl_basic_map_plain_is_empty(bmap
);
5141 return isl_basic_map_free(bmap
);
5143 bmap
= isl_basic_map_set_to_empty(bmap
);
5145 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
5149 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
5150 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
5152 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
5153 return isl_basic_map_free(bmap
);
5155 bmap
= move_last(bmap
, type
, first
, n
);
5156 bmap
= isl_basic_map_cow(bmap
);
5157 bmap
= insert_div_rows(bmap
, n
);
5159 space
= isl_basic_map_take_space(bmap
);
5160 space
= isl_space_drop_dims(space
, type
, first
, n
);
5161 bmap
= isl_basic_map_restore_space(bmap
, space
);
5162 bmap
= isl_basic_map_simplify(bmap
);
5163 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5164 return isl_basic_map_finalize(bmap
);
5167 /* Turn the n dimensions of type type, starting at first
5168 * into existentially quantified variables.
5170 __isl_give isl_basic_set
*isl_basic_set_project_out(
5171 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
5172 unsigned first
, unsigned n
)
5174 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
5178 /* Turn the n dimensions of type type, starting at first
5179 * into existentially quantified variables.
5181 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
5182 enum isl_dim_type type
, unsigned first
, unsigned n
)
5188 return map_space_reset(map
, type
);
5190 if (isl_map_check_range(map
, type
, first
, n
) < 0)
5191 return isl_map_free(map
);
5193 map
= isl_map_cow(map
);
5197 for (i
= 0; i
< map
->n
; ++i
) {
5198 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
5204 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5205 map
= isl_map_unmark_normalized(map
);
5207 space
= isl_map_take_space(map
);
5208 space
= isl_space_drop_dims(space
, type
, first
, n
);
5209 map
= isl_map_restore_space(map
, space
);
5218 #define TYPE isl_map
5219 #include "isl_project_out_all_params_templ.c"
5220 #include "isl_project_out_param_templ.c"
5222 /* Turn all the dimensions of type "type", except the "n" starting at "first"
5223 * into existentially quantified variables.
5225 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
5226 enum isl_dim_type type
, unsigned first
, unsigned n
)
5230 dim
= isl_map_dim(map
, type
);
5231 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
5232 return isl_map_free(map
);
5233 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
5234 map
= isl_map_project_out(map
, type
, 0, first
);
5238 /* Turn the n dimensions of type type, starting at first
5239 * into existentially quantified variables.
5241 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
5242 enum isl_dim_type type
, unsigned first
, unsigned n
)
5244 return set_from_map(isl_map_project_out(set_to_map(set
),
5248 /* If "set" involves a parameter with identifier "id",
5249 * then turn it into an existentially quantified variable.
5251 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
5252 __isl_take isl_id
*id
)
5254 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
5257 /* If "set" involves any of the parameters with identifiers in "list",
5258 * then turn them into existentially quantified variables.
5260 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
5261 __isl_take isl_id_list
*list
)
5265 map
= set_to_map(set
);
5266 map
= isl_map_project_out_param_id_list(map
, list
);
5267 return set_from_map(map
);
5270 /* Project out all parameters from "set" by existentially quantifying
5273 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
5275 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
5278 /* Return a map that projects the elements in "set" onto their
5279 * "n" set dimensions starting at "first".
5280 * "type" should be equal to isl_dim_set.
5282 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
5283 enum isl_dim_type type
, unsigned first
, unsigned n
)
5288 if (type
!= isl_dim_set
)
5289 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5290 "only set dimensions can be projected out", goto error
);
5291 if (isl_set_check_range(set
, type
, first
, n
) < 0)
5292 return isl_set_free(set
);
5294 map
= isl_map_from_domain(set
);
5295 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
5296 for (i
= 0; i
< n
; ++i
)
5297 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
5305 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
5311 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5313 return isl_basic_map_free(bmap
);
5314 for (i
= 0; i
< n
; ++i
) {
5315 j
= isl_basic_map_alloc_div(bmap
);
5318 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
5322 isl_basic_map_free(bmap
);
5326 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
5328 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
5329 __isl_keep isl_basic_map
*bmap2
)
5331 isl_space
*space1
, *space2
;
5333 space1
= isl_basic_map_peek_space(bmap1
);
5334 space2
= isl_basic_map_peek_space(bmap2
);
5335 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
5336 space2
, isl_dim_in
);
5339 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
5341 static isl_stat
isl_basic_map_check_applies_range(
5342 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
5346 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
5348 return isl_stat_error
;
5350 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
5351 "spaces don't match", return isl_stat_error
);
5355 __isl_give isl_basic_map
*isl_basic_map_apply_range(
5356 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
5358 isl_space
*space_result
= NULL
;
5359 struct isl_basic_map
*bmap
;
5360 isl_size n_in
, n_out
, n
, nparam
;
5361 unsigned total
, pos
;
5362 struct isl_dim_map
*dim_map1
, *dim_map2
;
5364 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
5366 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
5369 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
5370 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
5371 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
5372 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
5373 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
5376 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
5377 isl_basic_map_get_space(bmap2
));
5379 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
5380 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5381 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5382 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5383 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
5384 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5385 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
5386 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
5387 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5388 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
5389 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
5391 bmap
= isl_basic_map_alloc_space(space_result
,
5392 bmap1
->n_div
+ bmap2
->n_div
+ n
,
5393 bmap1
->n_eq
+ bmap2
->n_eq
,
5394 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5395 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5396 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5397 bmap
= add_divs(bmap
, n
);
5398 bmap
= isl_basic_map_simplify(bmap
);
5399 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5400 return isl_basic_map_finalize(bmap
);
5402 isl_basic_map_free(bmap1
);
5403 isl_basic_map_free(bmap2
);
5407 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
5408 __isl_take isl_basic_map
*bmap
)
5410 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
5413 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
5416 isl_basic_set_free(bset
);
5417 isl_basic_map_free(bmap
);
5421 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
5422 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
5424 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
5426 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
5427 bmap2
->dim
, isl_dim_in
))
5428 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
5429 "spaces don't match", goto error
);
5431 bmap1
= isl_basic_map_reverse(bmap1
);
5432 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
5433 return isl_basic_map_reverse(bmap1
);
5435 isl_basic_map_free(bmap1
);
5436 isl_basic_map_free(bmap2
);
5440 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5441 * A \cap B -> f(A) + f(B)
5443 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
5444 __isl_take isl_basic_map
*bmap2
)
5446 isl_size n_in
, n_out
, nparam
;
5447 unsigned total
, pos
;
5448 struct isl_basic_map
*bmap
= NULL
;
5449 struct isl_dim_map
*dim_map1
, *dim_map2
;
5452 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
5455 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
5456 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
5457 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
5458 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5461 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
5462 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5463 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
5464 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5465 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
5466 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5467 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
5468 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
5469 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5470 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
5471 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
5473 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
5474 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
5475 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
5476 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5477 for (i
= 0; i
< n_out
; ++i
) {
5478 int j
= isl_basic_map_alloc_equality(bmap
);
5481 isl_seq_clr(bmap
->eq
[j
], 1+total
);
5482 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
5483 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
5484 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
5486 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5487 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5488 bmap
= add_divs(bmap
, 2 * n_out
);
5490 bmap
= isl_basic_map_simplify(bmap
);
5491 return isl_basic_map_finalize(bmap
);
5493 isl_basic_map_free(bmap
);
5494 isl_basic_map_free(bmap1
);
5495 isl_basic_map_free(bmap2
);
5499 /* Given two maps A -> f(A) and B -> g(B), construct a map
5500 * A \cap B -> f(A) + f(B)
5502 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
5503 __isl_take isl_map
*map2
)
5505 struct isl_map
*result
;
5508 if (isl_map_check_equal_space(map1
, map2
) < 0)
5511 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5512 map1
->n
* map2
->n
, 0);
5515 for (i
= 0; i
< map1
->n
; ++i
)
5516 for (j
= 0; j
< map2
->n
; ++j
) {
5517 struct isl_basic_map
*part
;
5518 part
= isl_basic_map_sum(
5519 isl_basic_map_copy(map1
->p
[i
]),
5520 isl_basic_map_copy(map2
->p
[j
]));
5521 if (isl_basic_map_is_empty(part
))
5522 isl_basic_map_free(part
);
5524 result
= isl_map_add_basic_map(result
, part
);
5537 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
5538 __isl_take isl_set
*set2
)
5540 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5543 /* Given a basic map A -> f(A), construct A -> -f(A).
5545 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5551 bmap
= isl_basic_map_cow(bmap
);
5552 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5554 return isl_basic_map_free(bmap
);
5556 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5557 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5558 for (j
= 0; j
< n
; ++j
)
5559 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5560 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5561 for (j
= 0; j
< n
; ++j
)
5562 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5563 for (i
= 0; i
< bmap
->n_div
; ++i
)
5564 for (j
= 0; j
< n
; ++j
)
5565 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5566 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5567 return isl_basic_map_finalize(bmap
);
5570 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5572 return isl_basic_map_neg(bset
);
5575 /* Given a map A -> f(A), construct A -> -f(A).
5577 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5581 map
= isl_map_cow(map
);
5585 for (i
= 0; i
< map
->n
; ++i
) {
5586 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5597 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5599 return set_from_map(isl_map_neg(set_to_map(set
)));
5602 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5603 * A -> floor(f(A)/d).
5605 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5608 isl_size n_in
, n_out
, nparam
;
5609 unsigned total
, pos
;
5610 struct isl_basic_map
*result
= NULL
;
5611 struct isl_dim_map
*dim_map
;
5614 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5615 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5616 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5617 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5618 return isl_basic_map_free(bmap
);
5620 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5621 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5622 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5623 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5624 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5625 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5627 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5628 bmap
->n_div
+ n_out
,
5629 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5630 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5631 result
= add_divs(result
, n_out
);
5632 for (i
= 0; i
< n_out
; ++i
) {
5634 j
= isl_basic_map_alloc_inequality(result
);
5637 isl_seq_clr(result
->ineq
[j
], 1+total
);
5638 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5639 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5640 j
= isl_basic_map_alloc_inequality(result
);
5643 isl_seq_clr(result
->ineq
[j
], 1+total
);
5644 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5645 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5646 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5649 result
= isl_basic_map_simplify(result
);
5650 return isl_basic_map_finalize(result
);
5652 isl_basic_map_free(result
);
5656 /* Given a map A -> f(A) and an integer d, construct a map
5657 * A -> floor(f(A)/d).
5659 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5663 map
= isl_map_cow(map
);
5667 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5668 for (i
= 0; i
< map
->n
; ++i
) {
5669 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5673 map
= isl_map_unmark_normalized(map
);
5681 /* Given a map A -> f(A) and an integer d, construct a map
5682 * A -> floor(f(A)/d).
5684 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5685 __isl_take isl_val
*d
)
5689 if (!isl_val_is_int(d
))
5690 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5691 "expecting integer denominator", goto error
);
5692 map
= isl_map_floordiv(map
, d
->n
);
5701 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5709 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5710 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5711 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5712 if (total
< 0 || nparam
< 0 || n_in
< 0)
5713 return isl_basic_map_free(bmap
);
5714 i
= isl_basic_map_alloc_equality(bmap
);
5717 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5718 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5719 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5720 return isl_basic_map_finalize(bmap
);
5722 isl_basic_map_free(bmap
);
5726 /* Add a constraint to "bmap" expressing i_pos < o_pos
5728 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5736 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5737 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5738 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5739 if (total
< 0 || nparam
< 0 || n_in
< 0)
5740 return isl_basic_map_free(bmap
);
5741 i
= isl_basic_map_alloc_inequality(bmap
);
5744 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5745 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5746 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5747 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5748 return isl_basic_map_finalize(bmap
);
5750 isl_basic_map_free(bmap
);
5754 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5756 static __isl_give isl_basic_map
*var_less_or_equal(
5757 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5764 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5765 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5766 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5767 if (total
< 0 || nparam
< 0 || n_in
< 0)
5768 return isl_basic_map_free(bmap
);
5769 i
= isl_basic_map_alloc_inequality(bmap
);
5772 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5773 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5774 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5775 return isl_basic_map_finalize(bmap
);
5777 isl_basic_map_free(bmap
);
5781 /* Add a constraint to "bmap" expressing i_pos > o_pos
5783 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5791 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5792 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5793 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5794 if (total
< 0 || nparam
< 0 || n_in
< 0)
5795 return isl_basic_map_free(bmap
);
5796 i
= isl_basic_map_alloc_inequality(bmap
);
5799 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5800 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5801 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5802 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5803 return isl_basic_map_finalize(bmap
);
5805 isl_basic_map_free(bmap
);
5809 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5811 static __isl_give isl_basic_map
*var_more_or_equal(
5812 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5819 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5820 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5821 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5822 if (total
< 0 || nparam
< 0 || n_in
< 0)
5823 return isl_basic_map_free(bmap
);
5824 i
= isl_basic_map_alloc_inequality(bmap
);
5827 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5828 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5829 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5830 return isl_basic_map_finalize(bmap
);
5832 isl_basic_map_free(bmap
);
5836 __isl_give isl_basic_map
*isl_basic_map_equal(
5837 __isl_take isl_space
*space
, unsigned n_equal
)
5840 struct isl_basic_map
*bmap
;
5841 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5844 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5845 bmap
= var_equal(bmap
, i
);
5846 return isl_basic_map_finalize(bmap
);
5849 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5851 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5855 struct isl_basic_map
*bmap
;
5856 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5859 for (i
= 0; i
< pos
&& bmap
; ++i
)
5860 bmap
= var_equal(bmap
, i
);
5862 bmap
= var_less(bmap
, pos
);
5863 return isl_basic_map_finalize(bmap
);
5866 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5868 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5869 __isl_take isl_space
*space
, unsigned pos
)
5872 isl_basic_map
*bmap
;
5874 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5875 for (i
= 0; i
< pos
; ++i
)
5876 bmap
= var_equal(bmap
, i
);
5877 bmap
= var_less_or_equal(bmap
, pos
);
5878 return isl_basic_map_finalize(bmap
);
5881 /* Return a relation on "space" expressing i_pos > o_pos
5883 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5887 struct isl_basic_map
*bmap
;
5888 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5891 for (i
= 0; i
< pos
&& bmap
; ++i
)
5892 bmap
= var_equal(bmap
, i
);
5894 bmap
= var_more(bmap
, pos
);
5895 return isl_basic_map_finalize(bmap
);
5898 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5900 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5901 __isl_take isl_space
*space
, unsigned pos
)
5904 isl_basic_map
*bmap
;
5906 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5907 for (i
= 0; i
< pos
; ++i
)
5908 bmap
= var_equal(bmap
, i
);
5909 bmap
= var_more_or_equal(bmap
, pos
);
5910 return isl_basic_map_finalize(bmap
);
5913 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5914 unsigned n
, int equal
)
5916 struct isl_map
*map
;
5919 if (n
== 0 && equal
)
5920 return isl_map_universe(space
);
5922 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5924 for (i
= 0; i
+ 1 < n
; ++i
)
5925 map
= isl_map_add_basic_map(map
,
5926 isl_basic_map_less_at(isl_space_copy(space
), i
));
5929 map
= isl_map_add_basic_map(map
,
5930 isl_basic_map_less_or_equal_at(space
, n
- 1));
5932 map
= isl_map_add_basic_map(map
,
5933 isl_basic_map_less_at(space
, n
- 1));
5935 isl_space_free(space
);
5940 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5944 return map_lex_lte_first(space
, space
->n_out
, equal
);
5947 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5950 return map_lex_lte_first(space
, n
, 0);
5953 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5956 return map_lex_lte_first(space
, n
, 1);
5959 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5961 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5964 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5966 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5969 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5970 unsigned n
, int equal
)
5972 struct isl_map
*map
;
5975 if (n
== 0 && equal
)
5976 return isl_map_universe(space
);
5978 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5980 for (i
= 0; i
+ 1 < n
; ++i
)
5981 map
= isl_map_add_basic_map(map
,
5982 isl_basic_map_more_at(isl_space_copy(space
), i
));
5985 map
= isl_map_add_basic_map(map
,
5986 isl_basic_map_more_or_equal_at(space
, n
- 1));
5988 map
= isl_map_add_basic_map(map
,
5989 isl_basic_map_more_at(space
, n
- 1));
5991 isl_space_free(space
);
5996 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
6000 return map_lex_gte_first(space
, space
->n_out
, equal
);
6003 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
6006 return map_lex_gte_first(space
, n
, 0);
6009 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
6012 return map_lex_gte_first(space
, n
, 1);
6015 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
6017 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
6020 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
6022 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
6025 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
6026 __isl_take isl_set
*set2
)
6029 map
= isl_map_lex_le(isl_set_get_space(set1
));
6030 map
= isl_map_intersect_domain(map
, set1
);
6031 map
= isl_map_intersect_range(map
, set2
);
6035 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
6036 __isl_take isl_set
*set2
)
6039 map
= isl_map_lex_lt(isl_set_get_space(set1
));
6040 map
= isl_map_intersect_domain(map
, set1
);
6041 map
= isl_map_intersect_range(map
, set2
);
6045 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
6046 __isl_take isl_set
*set2
)
6049 map
= isl_map_lex_ge(isl_set_get_space(set1
));
6050 map
= isl_map_intersect_domain(map
, set1
);
6051 map
= isl_map_intersect_range(map
, set2
);
6055 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
6056 __isl_take isl_set
*set2
)
6059 map
= isl_map_lex_gt(isl_set_get_space(set1
));
6060 map
= isl_map_intersect_domain(map
, set1
);
6061 map
= isl_map_intersect_range(map
, set2
);
6065 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
6066 __isl_take isl_map
*map2
)
6069 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
6070 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6071 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6075 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
6076 __isl_take isl_map
*map2
)
6079 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
6080 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6081 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6085 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
6086 __isl_take isl_map
*map2
)
6089 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
6090 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6091 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6095 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
6096 __isl_take isl_map
*map2
)
6099 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
6100 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
6101 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
6105 /* For the div d = floor(f/m) at position "div", add the constraint
6109 static __isl_give isl_basic_map
*add_upper_div_constraint(
6110 __isl_take isl_basic_map
*bmap
, unsigned div
)
6113 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
6117 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6118 if (v_div
< 0 || n_div
< 0)
6119 return isl_basic_map_free(bmap
);
6121 i
= isl_basic_map_alloc_inequality(bmap
);
6123 return isl_basic_map_free(bmap
);
6124 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
6125 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
6130 /* For the div d = floor(f/m) at position "div", add the constraint
6132 * -(f-(m-1)) + m d >= 0
6134 static __isl_give isl_basic_map
*add_lower_div_constraint(
6135 __isl_take isl_basic_map
*bmap
, unsigned div
)
6138 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
6142 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6143 if (v_div
< 0 || n_div
< 0)
6144 return isl_basic_map_free(bmap
);
6146 i
= isl_basic_map_alloc_inequality(bmap
);
6148 return isl_basic_map_free(bmap
);
6149 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
6150 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
6151 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
6152 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
6157 /* For the div d = floor(f/m) at position "pos", add the constraints
6160 * -(f-(m-1)) + m d >= 0
6162 * Note that the second constraint is the negation of
6166 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
6167 __isl_take isl_basic_map
*bmap
, unsigned pos
)
6169 bmap
= add_upper_div_constraint(bmap
, pos
);
6170 bmap
= add_lower_div_constraint(bmap
, pos
);
6174 /* For the div d = floor(f/m) at position "pos", add the constraints
6177 * -(f-(m-1)) + m d >= 0
6179 * Note that the second constraint is the negation of
6183 __isl_give isl_basic_set
*isl_basic_set_add_div_constraints(
6184 __isl_take isl_basic_set
*bset
, unsigned pos
)
6186 isl_basic_map
*bmap
= bset_to_bmap(bset
);
6187 bmap
= isl_basic_map_add_div_constraints(bmap
, pos
);
6188 return bset_from_bmap(bmap
);
6191 /* For each known div d = floor(f/m), add the constraints
6194 * -(f-(m-1)) + m d >= 0
6196 * Remove duplicate constraints in case of some these div constraints
6197 * already appear in "bmap".
6199 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
6200 __isl_take isl_basic_map
*bmap
)
6204 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6206 return isl_basic_map_free(bmap
);
6210 bmap
= add_known_div_constraints(bmap
);
6211 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
6212 bmap
= isl_basic_map_finalize(bmap
);
6216 /* Add the div constraint of sign "sign" for div "div" of "bmap".
6218 * In particular, if this div is of the form d = floor(f/m),
6219 * then add the constraint
6223 * if sign < 0 or the constraint
6225 * -(f-(m-1)) + m d >= 0
6229 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
6230 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
6233 return add_upper_div_constraint(bmap
, div
);
6235 return add_lower_div_constraint(bmap
, div
);
6238 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
6239 __isl_take isl_basic_map
*bmap
)
6245 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
6247 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
6248 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
6249 return bset_from_bmap(bmap
);
6250 bmap
= isl_basic_map_cow(bmap
);
6253 space
= isl_basic_map_take_space(bmap
);
6254 space
= isl_space_underlying(space
, bmap
->n_div
);
6255 bmap
= isl_basic_map_restore_space(bmap
, space
);
6258 bmap
->extra
-= bmap
->n_div
;
6260 bmap
= isl_basic_map_finalize(bmap
);
6261 return bset_from_bmap(bmap
);
6263 isl_basic_map_free(bmap
);
6267 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
6268 __isl_take isl_basic_set
*bset
)
6270 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
6273 /* Replace each element in "list" by the result of applying
6274 * isl_basic_map_underlying_set to the element.
6276 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
6277 __isl_take isl_basic_map_list
*list
)
6282 n
= isl_basic_map_list_n_basic_map(list
);
6286 for (i
= 0; i
< n
; ++i
) {
6287 isl_basic_map
*bmap
;
6288 isl_basic_set
*bset
;
6290 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
6291 bset
= isl_basic_set_underlying_set(bmap
);
6292 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
6297 isl_basic_map_list_free(list
);
6301 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
6302 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
6304 struct isl_basic_map
*bmap
;
6305 struct isl_ctx
*ctx
;
6306 isl_size dim
, bmap_total
;
6313 if (isl_basic_set_check_no_params(bset
) < 0 ||
6314 isl_basic_set_check_no_locals(bset
) < 0)
6316 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
6317 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
6318 if (dim
< 0 || bmap_total
< 0)
6320 isl_assert(ctx
, dim
== bmap_total
, goto error
);
6321 if (like
->n_div
== 0) {
6322 isl_space
*space
= isl_basic_map_get_space(like
);
6323 isl_basic_map_free(like
);
6324 return isl_basic_map_reset_space(bset
, space
);
6326 bset
= isl_basic_set_cow(bset
);
6329 total
= dim
+ bset
->extra
;
6330 bmap
= bset_to_bmap(bset
);
6331 isl_space_free(isl_basic_map_take_space(bmap
));
6332 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
6335 bmap
->n_div
= like
->n_div
;
6336 bmap
->extra
+= like
->n_div
;
6340 ltotal
= total
- bmap
->extra
+ like
->extra
;
6343 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
6344 bmap
->extra
* (1 + 1 + total
));
6345 if (isl_blk_is_error(bmap
->block2
))
6347 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
6351 for (i
= 0; i
< bmap
->extra
; ++i
)
6352 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
6353 for (i
= 0; i
< like
->n_div
; ++i
) {
6354 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
6355 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
6357 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
6359 isl_basic_map_free(like
);
6360 bmap
= isl_basic_map_simplify(bmap
);
6361 bmap
= isl_basic_map_finalize(bmap
);
6364 isl_basic_map_free(like
);
6365 isl_basic_set_free(bset
);
6369 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
6370 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
6372 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
6373 bset_to_bmap(like
)));
6376 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
6380 map
= isl_map_cow(map
);
6383 map
->dim
= isl_space_cow(map
->dim
);
6387 for (i
= 1; i
< map
->n
; ++i
)
6388 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
6390 for (i
= 0; i
< map
->n
; ++i
) {
6391 map
->p
[i
] = bset_to_bmap(
6392 isl_basic_map_underlying_set(map
->p
[i
]));
6397 map
->dim
= isl_space_underlying(map
->dim
, 0);
6399 isl_space_free(map
->dim
);
6400 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
6404 return set_from_map(map
);
6410 /* Replace the space of "bmap" by "space".
6412 * If the space of "bmap" is identical to "space" (including the identifiers
6413 * of the input and output dimensions), then simply return the original input.
6415 __isl_give isl_basic_map
*isl_basic_map_reset_space(
6416 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
6419 isl_space
*bmap_space
;
6421 bmap_space
= isl_basic_map_peek_space(bmap
);
6422 equal
= isl_space_is_equal(bmap_space
, space
);
6423 if (equal
>= 0 && equal
)
6424 equal
= isl_space_has_equal_ids(bmap_space
, space
);
6428 isl_space_free(space
);
6431 isl_space_free(isl_basic_map_take_space(bmap
));
6432 bmap
= isl_basic_map_restore_space(bmap
, space
);
6434 bmap
= isl_basic_map_finalize(bmap
);
6438 isl_basic_map_free(bmap
);
6439 isl_space_free(space
);
6443 __isl_give isl_basic_set
*isl_basic_set_reset_space(
6444 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
6446 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
6450 /* Check that the total dimensions of "map" and "space" are the same.
6452 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
6453 __isl_keep isl_space
*space
)
6455 isl_size dim1
, dim2
;
6457 dim1
= isl_map_dim(map
, isl_dim_all
);
6458 dim2
= isl_space_dim(space
, isl_dim_all
);
6459 if (dim1
< 0 || dim2
< 0)
6460 return isl_stat_error
;
6463 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6464 "total dimensions do not match", return isl_stat_error
);
6467 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
6468 __isl_take isl_space
*space
)
6472 map
= isl_map_cow(map
);
6476 for (i
= 0; i
< map
->n
; ++i
) {
6477 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
6478 isl_space_copy(space
));
6482 isl_space_free(isl_map_take_space(map
));
6483 map
= isl_map_restore_space(map
, space
);
6488 isl_space_free(space
);
6492 /* Replace the space of "map" by "space", without modifying
6493 * the dimension of "map".
6495 * If the space of "map" is identical to "space" (including the identifiers
6496 * of the input and output dimensions), then simply return the original input.
6498 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
6499 __isl_take isl_space
*space
)
6502 isl_space
*map_space
;
6504 map_space
= isl_map_peek_space(map
);
6505 equal
= isl_space_is_equal(map_space
, space
);
6506 if (equal
>= 0 && equal
)
6507 equal
= isl_space_has_equal_ids(map_space
, space
);
6511 isl_space_free(space
);
6514 if (check_map_space_equal_total_dim(map
, space
) < 0)
6516 return isl_map_reset_space(map
, space
);
6519 isl_space_free(space
);
6523 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
6524 __isl_take isl_space
*space
)
6526 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
6529 /* Compute the parameter domain of the given basic set.
6531 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
6537 is_params
= isl_basic_set_is_params(bset
);
6539 return isl_basic_set_free(bset
);
6543 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6545 return isl_basic_set_free(bset
);
6546 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6547 space
= isl_basic_set_get_space(bset
);
6548 space
= isl_space_params(space
);
6549 bset
= isl_basic_set_reset_space(bset
, space
);
6553 /* Construct a zero-dimensional basic set with the given parameter domain.
6555 __isl_give isl_basic_set
*isl_basic_set_from_params(
6556 __isl_take isl_basic_set
*bset
)
6559 space
= isl_basic_set_get_space(bset
);
6560 space
= isl_space_set_from_params(space
);
6561 bset
= isl_basic_set_reset_space(bset
, space
);
6565 /* Compute the parameter domain of the given set.
6567 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6569 return isl_map_params(set_to_map(set
));
6572 /* Construct a zero-dimensional set with the given parameter domain.
6574 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6577 space
= isl_set_get_space(set
);
6578 space
= isl_space_set_from_params(space
);
6579 set
= isl_set_reset_space(set
, space
);
6583 /* Compute the parameter domain of the given map.
6585 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6588 isl_size n_in
, n_out
;
6590 n_in
= isl_map_dim(map
, isl_dim_in
);
6591 n_out
= isl_map_dim(map
, isl_dim_out
);
6592 if (n_in
< 0 || n_out
< 0)
6593 return isl_map_free(map
);
6594 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6595 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6596 space
= isl_map_get_space(map
);
6597 space
= isl_space_params(space
);
6598 map
= isl_map_reset_space(map
, space
);
6602 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6607 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6609 return isl_basic_map_free(bmap
);
6610 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6612 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6614 return isl_basic_map_reset_space(bmap
, space
);
6617 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6620 return isl_bool_error
;
6621 return isl_space_may_be_set(bmap
->dim
);
6624 /* Is this basic map actually a set?
6625 * Users should never call this function. Outside of isl,
6626 * the type should indicate whether something is a set or a map.
6628 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6631 return isl_bool_error
;
6632 return isl_space_is_set(bmap
->dim
);
6635 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6639 is_set
= isl_basic_map_is_set(bmap
);
6644 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6646 isl_basic_map_free(bmap
);
6650 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6651 __isl_take isl_basic_map
*bmap
)
6655 isl_basic_map
*domain
;
6656 isl_size nparam
, n_in
, n_out
;
6658 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6659 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6660 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6661 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6662 return isl_basic_map_free(bmap
);
6664 space
= isl_basic_map_get_space(bmap
);
6665 space
= isl_space_from_range(isl_space_domain(space
));
6666 domain
= isl_basic_map_universe(space
);
6668 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6669 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6670 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6672 for (i
= 0; i
< n_in
; ++i
)
6673 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6676 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6677 return isl_basic_map_finalize(bmap
);
6680 __isl_give isl_basic_map
*isl_basic_map_range_map(
6681 __isl_take isl_basic_map
*bmap
)
6685 isl_basic_map
*range
;
6686 isl_size nparam
, n_in
, n_out
;
6688 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6689 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6690 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6691 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6692 return isl_basic_map_free(bmap
);
6694 space
= isl_basic_map_get_space(bmap
);
6695 space
= isl_space_from_range(isl_space_range(space
));
6696 range
= isl_basic_map_universe(space
);
6698 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6699 bmap
= isl_basic_map_apply_range(bmap
, range
);
6700 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6702 for (i
= 0; i
< n_out
; ++i
)
6703 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6706 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6707 return isl_basic_map_finalize(bmap
);
6710 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6714 return isl_space_may_be_set(map
->dim
);
6717 /* Is this map actually a set?
6718 * Users should never call this function. Outside of isl,
6719 * the type should indicate whether something is a set or a map.
6721 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6724 return isl_bool_error
;
6725 return isl_space_is_set(map
->dim
);
6728 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6733 n_in
= isl_map_dim(map
, isl_dim_in
);
6735 return set_from_map(isl_map_free(map
));
6736 space
= isl_space_range(isl_map_get_space(map
));
6738 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6740 return set_from_map(isl_map_reset_space(map
, space
));
6743 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6744 * to each of its basic maps.
6746 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6747 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6748 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6753 map
= isl_map_cow(map
);
6757 for (i
= 0; i
< map
->n
; ++i
) {
6758 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6760 return isl_map_free(map
);
6762 map
= isl_map_unmark_normalized(map
);
6764 space
= isl_map_take_space(map
);
6765 space
= fn_space(space
);
6766 map
= isl_map_restore_space(map
, space
);
6771 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6773 return isl_map_transform(map
, &isl_space_domain_map
,
6774 &isl_basic_map_domain_map
);
6777 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6779 return isl_map_transform(map
, &isl_space_range_map
,
6780 &isl_basic_map_range_map
);
6783 /* Given a wrapped map of the form A[B -> C],
6784 * return the map A[B -> C] -> B.
6786 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6793 if (!isl_set_has_tuple_id(set
))
6794 return isl_map_domain_map(isl_set_unwrap(set
));
6796 id
= isl_set_get_tuple_id(set
);
6797 map
= isl_map_domain_map(isl_set_unwrap(set
));
6798 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6803 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6804 __isl_take isl_basic_set
*bset
)
6806 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6809 __isl_give isl_basic_map
*isl_basic_map_from_range(
6810 __isl_take isl_basic_set
*bset
)
6813 space
= isl_basic_set_get_space(bset
);
6814 space
= isl_space_from_range(space
);
6815 bset
= isl_basic_set_reset_space(bset
, space
);
6816 return bset_to_bmap(bset
);
6819 /* Create a relation with the given set as range.
6820 * The domain of the created relation is a zero-dimensional
6821 * flat anonymous space.
6823 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6826 space
= isl_set_get_space(set
);
6827 space
= isl_space_from_range(space
);
6828 set
= isl_set_reset_space(set
, space
);
6829 return set_to_map(set
);
6832 /* Create a relation with the given set as domain.
6833 * The range of the created relation is a zero-dimensional
6834 * flat anonymous space.
6836 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6838 return isl_map_reverse(isl_map_from_range(set
));
6841 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6842 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6844 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6847 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6848 __isl_take isl_set
*range
)
6850 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6853 /* Return a newly allocated isl_map with given space and flags and
6854 * room for "n" basic maps.
6855 * Make sure that all cached information is cleared.
6857 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6860 struct isl_map
*map
;
6865 isl_die(space
->ctx
, isl_error_internal
,
6866 "negative number of basic maps", goto error
);
6867 map
= isl_calloc(space
->ctx
, struct isl_map
,
6868 sizeof(struct isl_map
) +
6869 (n
- 1) * sizeof(struct isl_basic_map
*));
6873 map
->ctx
= space
->ctx
;
6874 isl_ctx_ref(map
->ctx
);
6882 isl_space_free(space
);
6886 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6888 struct isl_basic_map
*bmap
;
6889 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6890 bmap
= isl_basic_map_set_to_empty(bmap
);
6894 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6896 struct isl_basic_set
*bset
;
6897 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6898 bset
= isl_basic_set_set_to_empty(bset
);
6902 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6904 struct isl_basic_map
*bmap
;
6905 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6906 bmap
= isl_basic_map_finalize(bmap
);
6910 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6912 struct isl_basic_set
*bset
;
6913 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6914 bset
= isl_basic_set_finalize(bset
);
6918 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6919 __isl_take isl_space
*space
)
6922 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6923 isl_basic_map
*bmap
;
6926 space
= isl_space_free(space
);
6927 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6928 for (i
= 0; i
< total
; ++i
) {
6929 int k
= isl_basic_map_alloc_inequality(bmap
);
6932 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6933 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6937 isl_basic_map_free(bmap
);
6941 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6942 __isl_take isl_space
*space
)
6944 return isl_basic_map_nat_universe(space
);
6947 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6949 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6952 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6954 return isl_map_nat_universe(space
);
6957 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6959 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6962 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6964 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6967 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6969 struct isl_map
*map
;
6972 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6973 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6977 /* This function performs the same operation as isl_map_universe,
6978 * but is considered as a function on an isl_space when exported.
6980 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6982 return isl_map_universe(space
);
6985 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6987 struct isl_set
*set
;
6990 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6991 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6995 /* This function performs the same operation as isl_set_universe,
6996 * but is considered as a function on an isl_space when exported.
6998 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
7000 return isl_set_universe(space
);
7003 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
7006 struct isl_map
*dup
;
7010 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
7011 for (i
= 0; i
< map
->n
; ++i
)
7012 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
7016 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
7017 __isl_take isl_basic_map
*bmap
)
7021 if (isl_basic_map_plain_is_empty(bmap
)) {
7022 isl_basic_map_free(bmap
);
7025 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
7027 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
7028 map
->p
[map
->n
] = bmap
;
7030 map
= isl_map_unmark_normalized(map
);
7036 isl_basic_map_free(bmap
);
7040 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
7051 isl_ctx_deref(map
->ctx
);
7052 for (i
= 0; i
< map
->n
; ++i
)
7053 isl_basic_map_free(map
->p
[i
]);
7054 isl_space_free(map
->dim
);
7060 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
7061 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
7066 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7068 return isl_basic_map_free(bmap
);
7070 bmap
= isl_basic_map_cow(bmap
);
7071 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
7072 j
= isl_basic_map_alloc_equality(bmap
);
7075 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
7076 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
7077 isl_int_set_si(bmap
->eq
[j
][0], value
);
7078 bmap
= isl_basic_map_simplify(bmap
);
7079 return isl_basic_map_finalize(bmap
);
7081 isl_basic_map_free(bmap
);
7085 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
7086 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
7091 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7093 return isl_basic_map_free(bmap
);
7095 bmap
= isl_basic_map_cow(bmap
);
7096 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
7097 j
= isl_basic_map_alloc_equality(bmap
);
7100 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
7101 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
7102 isl_int_set(bmap
->eq
[j
][0], value
);
7103 bmap
= isl_basic_map_simplify(bmap
);
7104 return isl_basic_map_finalize(bmap
);
7106 isl_basic_map_free(bmap
);
7110 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
7111 enum isl_dim_type type
, unsigned pos
, 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_si(bmap
,
7116 isl_basic_map_offset(bmap
, type
) + pos
, value
);
7119 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
7120 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7122 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7123 return isl_basic_map_free(bmap
);
7124 return isl_basic_map_fix_pos(bmap
,
7125 isl_basic_map_offset(bmap
, type
) + pos
, value
);
7128 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
7129 * to be equal to "v".
7131 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
7132 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7136 if (!isl_val_is_int(v
))
7137 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
7138 "expecting integer value", goto error
);
7139 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7141 pos
+= isl_basic_map_offset(bmap
, type
);
7142 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
7146 isl_basic_map_free(bmap
);
7151 /* Fix the value of the variable at position "pos" of type "type" of "bset"
7152 * to be equal to "v".
7154 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
7155 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7157 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
7160 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
7161 enum isl_dim_type type
, unsigned pos
, int value
)
7163 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
7167 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
7168 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7170 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
7174 /* Remove the basic map at position "i" from "map" if this basic map
7175 * is (obviously) empty.
7177 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
7184 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
7186 return isl_map_free(map
);
7190 isl_basic_map_free(map
->p
[i
]);
7193 map
->p
[i
] = map
->p
[map
->n
];
7194 map
= isl_map_unmark_normalized(map
);
7201 /* Perform "fn" on each basic map of "map", where we may not be holding
7202 * the only reference to "map".
7203 * In particular, "fn" should be a semantics preserving operation
7204 * that we want to apply to all copies of "map". We therefore need
7205 * to be careful not to modify "map" in a way that breaks "map"
7206 * in case anything goes wrong.
7208 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
7209 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
7211 struct isl_basic_map
*bmap
;
7217 for (i
= map
->n
- 1; i
>= 0; --i
) {
7218 bmap
= isl_basic_map_copy(map
->p
[i
]);
7222 isl_basic_map_free(map
->p
[i
]);
7224 map
= remove_if_empty(map
, i
);
7235 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
7236 enum isl_dim_type type
, unsigned pos
, int value
)
7240 map
= isl_map_cow(map
);
7241 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7242 return isl_map_free(map
);
7243 for (i
= map
->n
- 1; i
>= 0; --i
) {
7244 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
7245 map
= remove_if_empty(map
, i
);
7249 map
= isl_map_unmark_normalized(map
);
7253 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
7254 enum isl_dim_type type
, unsigned pos
, int value
)
7256 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
7259 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
7260 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7264 map
= isl_map_cow(map
);
7265 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7266 return isl_map_free(map
);
7267 for (i
= 0; i
< map
->n
; ++i
) {
7268 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
7272 map
= isl_map_unmark_normalized(map
);
7279 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
7280 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7282 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
7285 /* Fix the value of the variable at position "pos" of type "type" of "map"
7286 * to be equal to "v".
7288 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
7289 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7293 map
= isl_map_cow(map
);
7297 if (!isl_val_is_int(v
))
7298 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7299 "expecting integer value", goto error
);
7300 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7302 for (i
= map
->n
- 1; i
>= 0; --i
) {
7303 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
7305 map
= remove_if_empty(map
, i
);
7309 map
= isl_map_unmark_normalized(map
);
7318 /* Fix the value of the variable at position "pos" of type "type" of "set"
7319 * to be equal to "v".
7321 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
7322 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
7324 return isl_map_fix_val(set
, type
, pos
, v
);
7327 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
7328 unsigned input
, int value
)
7330 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
7333 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
7336 return set_from_map(isl_map_fix_si(set_to_map(set
),
7337 isl_dim_set
, dim
, value
));
7340 static __isl_give isl_basic_map
*basic_map_bound_si(
7341 __isl_take isl_basic_map
*bmap
,
7342 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
7347 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7348 return isl_basic_map_free(bmap
);
7349 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7351 return isl_basic_map_free(bmap
);
7352 pos
+= isl_basic_map_offset(bmap
, type
);
7353 bmap
= isl_basic_map_cow(bmap
);
7354 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
7355 j
= isl_basic_map_alloc_inequality(bmap
);
7358 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
7360 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
7361 isl_int_set_si(bmap
->ineq
[j
][0], value
);
7363 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
7364 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
7366 bmap
= isl_basic_map_simplify(bmap
);
7367 return isl_basic_map_finalize(bmap
);
7369 isl_basic_map_free(bmap
);
7373 __isl_give isl_basic_map
*isl_basic_map_lower_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
, 0);
7380 /* Constrain the values of the given dimension to be no greater than "value".
7382 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
7383 __isl_take isl_basic_map
*bmap
,
7384 enum isl_dim_type type
, unsigned pos
, int value
)
7386 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
7389 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
7390 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
7394 map
= isl_map_cow(map
);
7395 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7396 return isl_map_free(map
);
7397 for (i
= 0; i
< map
->n
; ++i
) {
7398 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
7399 type
, pos
, value
, upper
);
7403 map
= isl_map_unmark_normalized(map
);
7410 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
7411 enum isl_dim_type type
, unsigned pos
, int value
)
7413 return map_bound_si(map
, type
, pos
, value
, 0);
7416 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
7417 enum isl_dim_type type
, unsigned pos
, int value
)
7419 return map_bound_si(map
, type
, pos
, value
, 1);
7422 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
7423 enum isl_dim_type type
, unsigned pos
, int value
)
7425 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
7429 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
7430 enum isl_dim_type type
, unsigned pos
, int value
)
7432 return isl_map_upper_bound_si(set
, type
, pos
, value
);
7435 /* Bound the given variable of "bmap" from below (or above is "upper"
7436 * is set) to "value".
7438 static __isl_give isl_basic_map
*basic_map_bound(
7439 __isl_take isl_basic_map
*bmap
,
7440 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7445 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7446 return isl_basic_map_free(bmap
);
7447 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7449 return isl_basic_map_free(bmap
);
7450 pos
+= isl_basic_map_offset(bmap
, type
);
7451 bmap
= isl_basic_map_cow(bmap
);
7452 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
7453 j
= isl_basic_map_alloc_inequality(bmap
);
7456 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
7458 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
7459 isl_int_set(bmap
->ineq
[j
][0], value
);
7461 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
7462 isl_int_neg(bmap
->ineq
[j
][0], value
);
7464 bmap
= isl_basic_map_simplify(bmap
);
7465 return isl_basic_map_finalize(bmap
);
7467 isl_basic_map_free(bmap
);
7471 /* Bound the given variable of "map" from below (or above is "upper"
7472 * is set) to "value".
7474 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7475 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7479 map
= isl_map_cow(map
);
7480 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7481 return isl_map_free(map
);
7482 for (i
= map
->n
- 1; i
>= 0; --i
) {
7483 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
7484 map
= remove_if_empty(map
, i
);
7488 map
= isl_map_unmark_normalized(map
);
7492 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
7493 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7495 return map_bound(map
, type
, pos
, value
, 0);
7498 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
7499 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7501 return map_bound(map
, type
, pos
, value
, 1);
7504 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
7505 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7507 return isl_map_lower_bound(set
, type
, pos
, value
);
7510 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
7511 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7513 return isl_map_upper_bound(set
, type
, pos
, value
);
7516 /* Force the values of the variable at position "pos" of type "type" of "map"
7517 * to be no smaller than "value".
7519 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
7520 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7524 if (!isl_val_is_int(value
))
7525 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7526 "expecting integer value", goto error
);
7527 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
7528 isl_val_free(value
);
7531 isl_val_free(value
);
7536 /* Force the values of the variable at position "pos" of type "type" of "set"
7537 * to be no smaller than "value".
7539 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
7540 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7544 map
= set_to_map(set
);
7545 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
7548 /* Force the values of the variable at position "pos" of type "type" of "map"
7549 * to be no greater than "value".
7551 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
7552 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7556 if (!isl_val_is_int(value
))
7557 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7558 "expecting integer value", goto error
);
7559 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7560 isl_val_free(value
);
7563 isl_val_free(value
);
7568 /* Force the values of the variable at position "pos" of type "type" of "set"
7569 * to be no greater than "value".
7571 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7572 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7576 map
= set_to_map(set
);
7577 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7580 /* If "mv" has an explicit domain, then intersect the domain of "map"
7581 * with this explicit domain.
7583 * An isl_multi_val object never has an explicit domain,
7584 * so simply return "map".
7586 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7587 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7594 #include "isl_map_bound_templ.c"
7596 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7597 * for each set dimension, by treating the set as a map.
7599 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7600 __isl_take isl_multi_val
*bound
,
7601 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7602 unsigned pos
, __isl_take isl_val
*value
))
7606 map
= set_to_map(set
);
7607 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7612 #include "isl_map_bound_templ.c"
7614 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7615 * for each set dimension, by converting the set and the bound
7616 * to objects living in a map space.
7618 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7619 __isl_take isl_multi_pw_aff
*bound
,
7620 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7621 unsigned pos
, __isl_take TYPE
*value
))
7625 map
= isl_map_from_range(set
);
7626 bound
= isl_multi_pw_aff_from_range(bound
);
7627 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7628 return isl_map_range(map
);
7631 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7632 * setting a bound on the given output dimension.
7634 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7635 unsigned pos
, __isl_take isl_val
*v
)
7637 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7640 /* Force the values of the set dimensions of "set"
7641 * to be no smaller than the corresponding values in "lower".
7643 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7644 __isl_take isl_multi_val
*lower
)
7646 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7649 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7650 * setting a bound on the given output dimension.
7652 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7653 unsigned pos
, __isl_take isl_val
*v
)
7655 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7658 /* Force the values of the set dimensions of "set"
7659 * to be no greater than the corresponding values in "upper".
7661 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7662 __isl_take isl_multi_val
*upper
)
7664 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7667 /* Force the symbolic constant expression "bound"
7668 * to satisfy the relation "order" with respect to
7669 * the output variable at position "pos" of "map".
7671 * Create an affine expression representing the output variable
7672 * in terms of the range and
7673 * compare it using "order" to "bound" (defined on the domain).
7674 * The result is a relation between elements in domain and range that
7675 * can be intersected with "map".
7677 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7678 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7679 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7680 __isl_take isl_pw_aff
*pa2
))
7683 isl_local_space
*ls
;
7686 space
= isl_space_range(isl_map_get_space(map
));
7687 ls
= isl_local_space_from_space(space
);
7688 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7689 map
= isl_map_intersect(map
, order(bound
, var
));
7693 /* Force the values of the output variable at position "pos" of "map"
7694 * to be no smaller than the symbolic constant expression "lower".
7696 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7697 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7699 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7702 /* Force the values of the output variable at position "pos" of "map"
7703 * to be no greater than the symbolic constant expression "upper".
7705 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7706 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7708 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7711 /* Force the values of the set dimensions of "set"
7712 * to be no smaller than the corresponding constant symbolic expressions
7715 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7716 __isl_take isl_multi_pw_aff
*lower
)
7718 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7721 /* Force the values of the set dimensions of "set"
7722 * to be no greater than the corresponding constant symbolic expressions
7725 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7726 __isl_take isl_multi_pw_aff
*upper
)
7728 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7731 /* Force the values of the output dimensions of "map"
7732 * to be no smaller than the corresponding constant symbolic expressions
7735 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7736 __isl_take isl_multi_pw_aff
*lower
)
7738 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7741 /* Force the values of the output dimensions of "map"
7742 * to be no greater than the corresponding constant symbolic expressions
7745 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7746 __isl_take isl_multi_pw_aff
*upper
)
7748 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7751 /* Bound the given variable of "bset" from below (or above is "upper"
7752 * is set) to "value".
7754 static __isl_give isl_basic_set
*isl_basic_set_bound(
7755 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7756 isl_int value
, int upper
)
7758 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7759 type
, pos
, value
, upper
));
7762 /* Bound the given variable of "bset" from below (or above is "upper"
7763 * is set) to "value".
7765 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7766 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7767 __isl_take isl_val
*value
, int upper
)
7771 if (!isl_val_is_int(value
))
7772 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7773 "expecting integer value", goto error
);
7774 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7775 isl_val_free(value
);
7778 isl_val_free(value
);
7779 isl_basic_set_free(bset
);
7783 /* Bound the given variable of "bset" from below to "value".
7785 __isl_give isl_basic_set
*isl_basic_set_lower_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
, 0);
7792 /* Bound the given variable of "bset" from above to "value".
7794 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7795 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7796 __isl_take isl_val
*value
)
7798 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7801 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7803 return isl_map_transform(map
, &isl_space_reverse
,
7804 &isl_basic_map_reverse
);
7807 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7809 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7811 return isl_map_transform(map
, &isl_space_domain_reverse
,
7812 &isl_basic_map_domain_reverse
);
7815 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7817 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7819 return isl_map_transform(map
, &isl_space_range_reverse
,
7820 &isl_basic_map_range_reverse
);
7823 /* Given a set (A -> B), return the corresponding set (B -> A).
7825 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7827 isl_map
*map
= set_to_map(set
);
7829 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7830 &isl_basic_map_set_reverse
);
7831 return set_from_map(map
);
7835 #define TYPE isl_pw_multi_aff
7837 #define SUFFIX _pw_multi_aff
7839 #define EMPTY isl_pw_multi_aff_empty
7841 #define ADD isl_pw_multi_aff_union_add
7842 #include "isl_map_lexopt_templ.c"
7844 /* Given a map "map", compute the lexicographically minimal
7845 * (or maximal) image element for each domain element in dom,
7846 * in the form of an isl_pw_multi_aff.
7847 * If "empty" is not NULL, then set *empty to those elements in dom that
7848 * do not have an image element.
7849 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7850 * should be computed over the domain of "map". "empty" is also NULL
7853 * We first compute the lexicographically minimal or maximal element
7854 * in the first basic map. This results in a partial solution "res"
7855 * and a subset "todo" of dom that still need to be handled.
7856 * We then consider each of the remaining maps in "map" and successively
7857 * update both "res" and "todo".
7858 * If "empty" is NULL, then the todo sets are not needed and therefore
7859 * also not computed.
7861 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7862 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7863 __isl_give isl_set
**empty
, unsigned flags
)
7867 isl_pw_multi_aff
*res
;
7870 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7871 if (!map
|| (!full
&& !dom
))
7874 if (isl_map_plain_is_empty(map
)) {
7879 return isl_pw_multi_aff_from_map(map
);
7882 res
= basic_map_partial_lexopt_pw_multi_aff(
7883 isl_basic_map_copy(map
->p
[0]),
7884 isl_set_copy(dom
), empty
, flags
);
7888 for (i
= 1; i
< map
->n
; ++i
) {
7889 isl_pw_multi_aff
*res_i
;
7891 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7892 isl_basic_map_copy(map
->p
[i
]),
7893 isl_set_copy(dom
), empty
, flags
);
7895 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7896 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7898 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7901 todo
= isl_set_intersect(todo
, *empty
);
7920 #define TYPE isl_map
7924 #define EMPTY isl_map_empty
7926 #define ADD isl_map_union_disjoint
7927 #include "isl_map_lexopt_templ.c"
7929 /* Given a map "map", compute the lexicographically minimal
7930 * (or maximal) image element for each domain element in "dom",
7931 * in the form of an isl_map.
7932 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7933 * do not have an image element.
7934 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7935 * should be computed over the domain of "map". "empty" is also NULL
7938 * If the input consists of more than one disjunct, then first
7939 * compute the desired result in the form of an isl_pw_multi_aff and
7940 * then convert that into an isl_map.
7942 * This function used to have an explicit implementation in terms
7943 * of isl_maps, but it would continually intersect the domains of
7944 * partial results with the complement of the domain of the next
7945 * partial solution, potentially leading to an explosion in the number
7946 * of disjuncts if there are several disjuncts in the input.
7947 * An even earlier implementation of this function would look for
7948 * better results in the domain of the partial result and for extra
7949 * results in the complement of this domain, which would lead to
7950 * even more splintering.
7952 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7953 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7954 __isl_give isl_set
**empty
, unsigned flags
)
7957 struct isl_map
*res
;
7958 isl_pw_multi_aff
*pma
;
7960 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7961 if (!map
|| (!full
&& !dom
))
7964 if (isl_map_plain_is_empty(map
)) {
7973 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7979 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7981 return isl_map_from_pw_multi_aff_internal(pma
);
7990 __isl_give isl_map
*isl_map_partial_lexmax(
7991 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7992 __isl_give isl_set
**empty
)
7994 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7997 __isl_give isl_map
*isl_map_partial_lexmin(
7998 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7999 __isl_give isl_set
**empty
)
8001 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
8004 __isl_give isl_set
*isl_set_partial_lexmin(
8005 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
8006 __isl_give isl_set
**empty
)
8008 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
8012 __isl_give isl_set
*isl_set_partial_lexmax(
8013 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
8014 __isl_give isl_set
**empty
)
8016 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
8020 /* Compute the lexicographic minimum (or maximum if "flags" includes
8021 * ISL_OPT_MAX) of "bset" over its parametric domain.
8023 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
8026 return isl_basic_map_lexopt(bset
, flags
);
8029 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
8031 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
8034 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
8036 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
8039 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
8041 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
8044 /* Compute the lexicographic minimum of "bset" over its parametric domain
8045 * for the purpose of quantifier elimination.
8046 * That is, find an explicit representation for all the existentially
8047 * quantified variables in "bset" by computing their lexicographic
8050 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
8051 __isl_take isl_basic_set
*bset
)
8053 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
8056 /* Given a basic map with one output dimension, compute the minimum or
8057 * maximum of that dimension as an isl_pw_aff.
8059 * Compute the optimum as a lexicographic optimum over the single
8060 * output dimension and extract the single isl_pw_aff from the result.
8062 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
8065 isl_pw_multi_aff
*pma
;
8068 bmap
= isl_basic_map_copy(bmap
);
8069 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
8070 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
8071 isl_pw_multi_aff_free(pma
);
8076 /* Compute the minimum or maximum of the given output dimension
8077 * as a function of the parameters and the input dimensions,
8078 * but independently of the other output dimensions.
8080 * We first project out the other output dimension and then compute
8081 * the "lexicographic" maximum in each basic map, combining the results
8082 * using isl_pw_aff_union_max.
8084 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
8091 n_out
= isl_map_dim(map
, isl_dim_out
);
8093 map
= isl_map_free(map
);
8094 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
8095 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
8100 isl_space
*space
= isl_map_get_space(map
);
8102 return isl_pw_aff_empty(space
);
8105 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
8106 for (i
= 1; i
< map
->n
; ++i
) {
8107 isl_pw_aff
*pwaff_i
;
8109 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
8110 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
8118 /* Compute the minimum 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_min(__isl_take isl_map
*map
, int pos
)
8124 return map_dim_opt(map
, pos
, 0);
8127 /* Compute the maximum of the given output dimension as a function of the
8128 * parameters and input dimensions, but independently of
8129 * the other output dimensions.
8131 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
8133 return map_dim_opt(map
, pos
, 1);
8136 /* Compute the minimum or maximum of the given set dimension
8137 * as a function of the parameters,
8138 * but independently of the other set dimensions.
8140 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
8143 return map_dim_opt(set
, pos
, max
);
8146 /* Compute the maximum of the given set dimension as a function of the
8147 * parameters, but independently of the other set dimensions.
8149 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
8151 return set_dim_opt(set
, pos
, 1);
8154 /* Compute the minimum of the given set dimension as a function of the
8155 * parameters, but independently of the other set dimensions.
8157 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
8159 return set_dim_opt(set
, pos
, 0);
8162 /* Apply a preimage specified by "mat" on the parameters of "bset".
8163 * bset is assumed to have only parameters and divs.
8165 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
8166 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
8170 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8171 if (nparam
< 0 || !mat
)
8174 bset
->dim
= isl_space_cow(bset
->dim
);
8178 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
8180 bset
->dim
->nparam
= 0;
8181 bset
->dim
->n_out
= nparam
;
8182 bset
= isl_basic_set_preimage(bset
, mat
);
8184 bset
->dim
->nparam
= bset
->dim
->n_out
;
8185 bset
->dim
->n_out
= 0;
8190 isl_basic_set_free(bset
);
8194 /* Apply a preimage specified by "mat" on the parameters of "set".
8195 * set is assumed to have only parameters and divs.
8197 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
8198 __isl_take isl_mat
*mat
)
8203 nparam
= isl_set_dim(set
, isl_dim_param
);
8204 if (nparam
< 0 || !mat
)
8207 if (mat
->n_row
!= 1 + nparam
)
8208 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
8209 "unexpected number of rows", goto error
);
8211 space
= isl_set_get_space(set
);
8212 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
8213 isl_dim_param
, 0, nparam
);
8214 set
= isl_set_reset_space(set
, space
);
8215 set
= isl_set_preimage(set
, mat
);
8216 nparam
= isl_set_dim(set
, isl_dim_out
);
8218 set
= isl_set_free(set
);
8219 space
= isl_set_get_space(set
);
8220 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
8221 isl_dim_out
, 0, nparam
);
8222 set
= isl_set_reset_space(set
, space
);
8230 /* Intersect the basic set "bset" with the affine space specified by the
8231 * equalities in "eq".
8233 static __isl_give isl_basic_set
*basic_set_append_equalities(
8234 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
8242 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
8246 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
8247 for (i
= 0; i
< eq
->n_row
; ++i
) {
8248 k
= isl_basic_set_alloc_equality(bset
);
8251 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
8252 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
8256 bset
= isl_basic_set_gauss(bset
, NULL
);
8257 bset
= isl_basic_set_finalize(bset
);
8262 isl_basic_set_free(bset
);
8266 /* Intersect the set "set" with the affine space specified by the
8267 * equalities in "eq".
8269 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
8270 __isl_take isl_mat
*eq
)
8277 for (i
= 0; i
< set
->n
; ++i
) {
8278 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
8291 /* Given a basic set "bset" that only involves parameters and existentially
8292 * quantified variables, return the index of the first equality
8293 * that only involves parameters. If there is no such equality then
8294 * return bset->n_eq.
8296 * This function assumes that isl_basic_set_gauss has been called on "bset".
8298 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
8301 isl_size nparam
, n_div
;
8303 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8304 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
8305 if (nparam
< 0 || n_div
< 0)
8308 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
8309 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
8316 /* Compute an explicit representation for the existentially quantified
8317 * variables in "bset" by computing the "minimal value" of the set
8318 * variables. Since there are no set variables, the computation of
8319 * the minimal value essentially computes an explicit representation
8320 * of the non-empty part(s) of "bset".
8322 * The input only involves parameters and existentially quantified variables.
8323 * All equalities among parameters have been removed.
8325 * Since the existentially quantified variables in the result are in general
8326 * going to be different from those in the input, we first replace
8327 * them by the minimal number of variables based on their equalities.
8328 * This should simplify the parametric integer programming.
8330 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
8332 isl_morph
*morph1
, *morph2
;
8338 if (bset
->n_eq
== 0)
8339 return isl_basic_set_lexmin_compute_divs(bset
);
8341 morph1
= isl_basic_set_parameter_compression(bset
);
8342 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
8343 bset
= isl_basic_set_lift(bset
);
8344 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
8345 bset
= isl_morph_basic_set(morph2
, bset
);
8346 n
= isl_basic_set_dim(bset
, isl_dim_set
);
8348 bset
= isl_basic_set_free(bset
);
8349 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
8351 set
= isl_basic_set_lexmin_compute_divs(bset
);
8353 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
8358 /* Project the given basic set onto its parameter domain, possibly introducing
8359 * new, explicit, existential variables in the constraints.
8360 * The input has parameters and (possibly implicit) existential variables.
8361 * The output has the same parameters, but only
8362 * explicit existentially quantified variables.
8364 * The actual projection is performed by pip, but pip doesn't seem
8365 * to like equalities very much, so we first remove the equalities
8366 * among the parameters by performing a variable compression on
8367 * the parameters. Afterward, an inverse transformation is performed
8368 * and the equalities among the parameters are inserted back in.
8370 * The variable compression on the parameters may uncover additional
8371 * equalities that were only implicit before. We therefore check
8372 * if there are any new parameter equalities in the result and
8373 * if so recurse. The removal of parameter equalities is required
8374 * for the parameter compression performed by base_compute_divs.
8376 static __isl_give isl_set
*parameter_compute_divs(
8377 __isl_take isl_basic_set
*bset
)
8381 struct isl_mat
*T
, *T2
;
8382 struct isl_set
*set
;
8385 bset
= isl_basic_set_cow(bset
);
8389 if (bset
->n_eq
== 0)
8390 return base_compute_divs(bset
);
8392 bset
= isl_basic_set_gauss(bset
, NULL
);
8395 if (isl_basic_set_plain_is_empty(bset
))
8396 return isl_set_from_basic_set(bset
);
8398 i
= first_parameter_equality(bset
);
8399 if (i
== bset
->n_eq
)
8400 return base_compute_divs(bset
);
8402 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
8404 return isl_set_from_basic_set(isl_basic_set_free(bset
));
8405 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
8407 eq
= isl_mat_cow(eq
);
8408 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
8409 if (T
&& T
->n_col
== 0) {
8413 bset
= isl_basic_set_set_to_empty(bset
);
8414 return isl_set_from_basic_set(bset
);
8416 bset
= basic_set_parameter_preimage(bset
, T
);
8418 i
= first_parameter_equality(bset
);
8421 else if (i
== bset
->n_eq
)
8422 set
= base_compute_divs(bset
);
8424 set
= parameter_compute_divs(bset
);
8425 set
= set_parameter_preimage(set
, T2
);
8426 set
= set_append_equalities(set
, eq
);
8430 /* Insert the divs from "ls" before those of "bmap".
8432 * The number of columns is not changed, which means that the last
8433 * dimensions of "bmap" are being reintepreted as the divs from "ls".
8434 * The caller is responsible for removing the same number of dimensions
8435 * from the space of "bmap".
8437 static __isl_give isl_basic_map
*insert_divs_from_local_space(
8438 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
8444 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8446 return isl_basic_map_free(bmap
);
8450 old_n_div
= bmap
->n_div
;
8451 bmap
= insert_div_rows(bmap
, n_div
);
8455 for (i
= 0; i
< n_div
; ++i
) {
8456 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
8457 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
8463 /* Replace the space of "bmap" by the space and divs of "ls".
8465 * If "ls" has any divs, then we simplify the result since we may
8466 * have discovered some additional equalities that could simplify
8467 * the div expressions.
8469 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
8470 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
8474 bmap
= isl_basic_map_cow(bmap
);
8475 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8476 if (!bmap
|| n_div
< 0)
8479 bmap
= insert_divs_from_local_space(bmap
, ls
);
8483 isl_space_free(bmap
->dim
);
8484 bmap
->dim
= isl_local_space_get_space(ls
);
8488 isl_local_space_free(ls
);
8490 bmap
= isl_basic_map_simplify(bmap
);
8491 bmap
= isl_basic_map_finalize(bmap
);
8494 isl_basic_map_free(bmap
);
8495 isl_local_space_free(ls
);
8499 /* Replace the space of "map" by the space and divs of "ls".
8501 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
8502 __isl_take isl_local_space
*ls
)
8506 map
= isl_map_cow(map
);
8510 for (i
= 0; i
< map
->n
; ++i
) {
8511 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
8512 isl_local_space_copy(ls
));
8516 isl_space_free(isl_map_take_space(map
));
8517 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
8519 isl_local_space_free(ls
);
8522 isl_local_space_free(ls
);
8527 /* Compute an explicit representation for the existentially
8528 * quantified variables for which do not know any explicit representation yet.
8530 * We first sort the existentially quantified variables so that the
8531 * existentially quantified variables for which we already have an explicit
8532 * representation are placed before those for which we do not.
8533 * The input dimensions, the output dimensions and the existentially
8534 * quantified variables for which we already have an explicit
8535 * representation are then turned into parameters.
8536 * compute_divs returns a map with the same parameters and
8537 * no input or output dimensions and the dimension specification
8538 * is reset to that of the input, including the existentially quantified
8539 * variables for which we already had an explicit representation.
8541 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
8543 struct isl_basic_set
*bset
;
8544 struct isl_set
*set
;
8545 struct isl_map
*map
;
8547 isl_local_space
*ls
;
8554 bmap
= isl_basic_map_sort_divs(bmap
);
8555 bmap
= isl_basic_map_cow(bmap
);
8559 n_known
= isl_basic_map_first_unknown_div(bmap
);
8560 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8561 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8562 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8563 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8564 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8566 space
= isl_space_set_alloc(bmap
->ctx
,
8567 nparam
+ n_in
+ n_out
+ n_known
, 0);
8571 ls
= isl_basic_map_get_local_space(bmap
);
8572 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8573 n_known
, bmap
->n_div
- n_known
);
8575 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8576 swap_div(bmap
, i
- n_known
, i
);
8577 bmap
->n_div
-= n_known
;
8578 bmap
->extra
-= n_known
;
8580 bmap
= isl_basic_map_reset_space(bmap
, space
);
8581 bset
= bset_from_bmap(bmap
);
8583 set
= parameter_compute_divs(bset
);
8584 map
= set_to_map(set
);
8585 map
= replace_space_by_local_space(map
, ls
);
8589 isl_basic_map_free(bmap
);
8593 /* Is the integer division at position "div" of "bmap" integral?
8594 * That is, does it have denominator 1?
8596 isl_bool
isl_basic_map_div_is_integral(__isl_keep isl_basic_map
*bmap
, int div
)
8598 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8599 return isl_bool_error
;
8600 return isl_int_is_one(bmap
->div
[div
][0]);
8603 /* Remove the explicit representation of local variable "div",
8606 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8607 __isl_take isl_basic_map
*bmap
, int div
)
8611 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8613 return isl_basic_map_free(bmap
);
8617 bmap
= isl_basic_map_cow(bmap
);
8620 isl_int_set_si(bmap
->div
[div
][0], 0);
8624 /* Is local variable "div" of "bmap" marked as not having an explicit
8626 * Note that even if "div" is not marked in this way and therefore
8627 * has an explicit representation, this representation may still
8628 * depend (indirectly) on other local variables that do not
8629 * have an explicit representation.
8631 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8634 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8635 return isl_bool_error
;
8636 return isl_int_is_zero(bmap
->div
[div
][0]);
8639 /* Return the position of the first local variable that does not
8640 * have an explicit representation.
8641 * Return the total number of local variables if they all have
8642 * an explicit representation.
8643 * Return -1 on error.
8645 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8652 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8653 if (!isl_basic_map_div_is_known(bmap
, i
))
8659 /* Return the position of the first local variable that does not
8660 * have an explicit representation.
8661 * Return the total number of local variables if they all have
8662 * an explicit representation.
8663 * Return -1 on error.
8665 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8667 return isl_basic_map_first_unknown_div(bset
);
8670 /* Does "bmap" have an explicit representation for all local variables?
8672 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8677 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8678 first
= isl_basic_map_first_unknown_div(bmap
);
8679 if (n
< 0 || first
< 0)
8680 return isl_bool_error
;
8684 /* Do all basic maps in "map" have an explicit representation
8685 * for all local variables?
8687 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8692 return isl_bool_error
;
8694 for (i
= 0; i
< map
->n
; ++i
) {
8695 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8700 return isl_bool_true
;
8703 /* If bmap contains any unknown divs, then compute explicit
8704 * expressions for them. However, this computation may be
8705 * quite expensive, so first try to remove divs that aren't
8708 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8711 struct isl_map
*map
;
8713 known
= isl_basic_map_divs_known(bmap
);
8717 return isl_map_from_basic_map(bmap
);
8719 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8721 known
= isl_basic_map_divs_known(bmap
);
8725 return isl_map_from_basic_map(bmap
);
8727 map
= compute_divs(bmap
);
8730 isl_basic_map_free(bmap
);
8734 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8738 struct isl_map
*res
;
8745 known
= isl_map_divs_known(map
);
8753 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8754 for (i
= 1 ; i
< map
->n
; ++i
) {
8756 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8757 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8758 res
= isl_map_union_disjoint(res
, r2
);
8760 res
= isl_map_union(res
, r2
);
8767 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8769 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8772 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8774 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8777 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8782 n_out
= isl_map_dim(map
, isl_dim_out
);
8784 return set_from_map(isl_map_free(map
));
8785 space
= isl_space_domain(isl_map_get_space(map
));
8787 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8789 return set_from_map(isl_map_reset_space(map
, space
));
8792 /* Return the union of "map1" and "map2", where we assume for now that
8793 * "map1" and "map2" are disjoint. Note that the basic maps inside
8794 * "map1" or "map2" may not be disjoint from each other.
8795 * Also note that this function is also called from isl_map_union,
8796 * which takes care of handling the situation where "map1" and "map2"
8797 * may not be disjoint.
8799 * If one of the inputs is empty, we can simply return the other input.
8800 * Similarly, if one of the inputs is universal, then it is equal to the union.
8802 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8803 __isl_take isl_map
*map2
)
8807 struct isl_map
*map
= NULL
;
8810 if (isl_map_check_equal_space(map1
, map2
) < 0)
8822 is_universe
= isl_map_plain_is_universe(map1
);
8823 if (is_universe
< 0)
8830 is_universe
= isl_map_plain_is_universe(map2
);
8831 if (is_universe
< 0)
8838 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8839 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8840 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8842 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8843 map1
->n
+ map2
->n
, flags
);
8846 for (i
= 0; i
< map1
->n
; ++i
) {
8847 map
= isl_map_add_basic_map(map
,
8848 isl_basic_map_copy(map1
->p
[i
]));
8852 for (i
= 0; i
< map2
->n
; ++i
) {
8853 map
= isl_map_add_basic_map(map
,
8854 isl_basic_map_copy(map2
->p
[i
]));
8868 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8869 * guaranteed to be disjoint by the caller.
8871 * Note that this functions is called from within isl_map_make_disjoint,
8872 * so we have to be careful not to touch the constraints of the inputs
8875 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8876 __isl_take isl_map
*map2
)
8878 isl_map_align_params_bin(&map1
, &map2
);
8879 return map_union_disjoint(map1
, map2
);
8882 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8885 * We currently simply call map_union_disjoint, the internal operation
8886 * of which does not really depend on the inputs being disjoint.
8887 * If the result contains more than one basic map, then we clear
8888 * the disjoint flag since the result may contain basic maps from
8889 * both inputs and these are not guaranteed to be disjoint.
8891 * As a special case, if "map1" and "map2" are obviously equal,
8892 * then we simply return "map1".
8894 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8895 __isl_take isl_map
*map2
)
8899 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8902 equal
= isl_map_plain_is_equal(map1
, map2
);
8910 map1
= map_union_disjoint(map1
, map2
);
8914 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8922 __isl_give isl_set
*isl_set_union_disjoint(
8923 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8925 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8929 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8930 __isl_take isl_set
*set2
)
8932 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8935 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8936 * the results in a map living in "space".
8938 * "map" and "set" are assumed to be compatible and non-NULL.
8940 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8941 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8942 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8943 __isl_take isl_basic_set
*bset
))
8946 struct isl_map
*result
;
8949 if (isl_set_plain_is_universe(set
)) {
8951 return isl_map_reset_equal_dim_space(map
, space
);
8954 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8955 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8956 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8958 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8959 for (i
= 0; result
&& i
< map
->n
; ++i
)
8960 for (j
= 0; j
< set
->n
; ++j
) {
8961 result
= isl_map_add_basic_map(result
,
8962 fn(isl_basic_map_copy(map
->p
[i
]),
8963 isl_basic_set_copy(set
->p
[j
])));
8973 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8974 __isl_take isl_set
*set
)
8979 isl_map_align_params_set(&map
, &set
);
8980 ok
= isl_map_compatible_range(map
, set
);
8984 isl_die(set
->ctx
, isl_error_invalid
,
8985 "incompatible spaces", goto error
);
8987 space
= isl_map_get_space(map
);
8988 return map_intersect_set(map
, space
, set
,
8989 &isl_basic_map_intersect_range
);
8996 /* Intersect the domain of "map" with "set".
8998 * If the domain dimensions of "map" do not have any identifiers,
8999 * then copy them over from "set".
9001 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
9002 __isl_take isl_set
*set
)
9007 isl_map_align_params_set(&map
, &set
);
9008 ok
= isl_map_compatible_domain(map
, set
);
9012 isl_die(set
->ctx
, isl_error_invalid
,
9013 "incompatible spaces", goto error
);
9015 space
= isl_map_get_space(map
);
9016 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
9017 isl_set_peek_space(set
), isl_dim_set
);
9018 return map_intersect_set(map
, space
, set
,
9019 &isl_basic_map_intersect_domain
);
9029 #include "isl_copy_tuple_id_templ.c"
9031 /* Data structure that specifies how isl_map_intersect_factor
9034 * "preserve_type" is the tuple where the factor differs from
9035 * the input map and of which the identifiers needs
9036 * to be preserved explicitly.
9037 * "other_factor" is used to extract the space of the other factor
9038 * from the space of the product ("map").
9039 * "product" is used to combine the given factor and a universe map
9040 * in the space returned by "other_factor" to produce a map
9041 * that lives in the same space as the input map.
9043 struct isl_intersect_factor_control
{
9044 enum isl_dim_type preserve_type
;
9045 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
9046 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
9047 __isl_take isl_map
*other
);
9050 /* Given a map "map" in some product space and a map "factor"
9051 * living in some factor space, return the intersection.
9053 * After aligning the parameters,
9054 * the map "factor" is first extended to a map living in the same space
9055 * as "map" and then a regular intersection is computed.
9057 * Note that the extension is computed as a product, which is anonymous
9058 * by default. If "map" has an identifier on the corresponding tuple,
9059 * then this identifier needs to be set on the product
9060 * before the intersection is computed.
9062 static __isl_give isl_map
*isl_map_intersect_factor(
9063 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
9064 struct isl_intersect_factor_control
*control
)
9068 isl_map
*other
, *product
;
9070 equal
= isl_map_has_equal_params(map
, factor
);
9074 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
9075 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
9078 space
= isl_map_get_space(map
);
9079 other
= isl_map_universe(control
->other_factor(space
));
9080 product
= control
->product(factor
, other
);
9082 space
= isl_map_peek_space(map
);
9083 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
9084 space
, control
->preserve_type
);
9085 return map_intersect(map
, product
);
9088 isl_map_free(factor
);
9092 /* Return the domain product of "map2" and "map1".
9094 static __isl_give isl_map
*isl_map_reverse_domain_product(
9095 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
9097 return isl_map_domain_product(map2
, map1
);
9100 /* Return the range product of "map2" and "map1".
9102 static __isl_give isl_map
*isl_map_reverse_range_product(
9103 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
9105 return isl_map_range_product(map2
, map1
);
9108 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9109 * in the space A -> C, return the intersection.
9111 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
9112 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9114 struct isl_intersect_factor_control control
= {
9115 .preserve_type
= isl_dim_in
,
9116 .other_factor
= isl_space_domain_factor_range
,
9117 .product
= isl_map_domain_product
,
9120 return isl_map_intersect_factor(map
, factor
, &control
);
9123 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9124 * in the space B -> C, return the intersection.
9126 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
9127 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9129 struct isl_intersect_factor_control control
= {
9130 .preserve_type
= isl_dim_in
,
9131 .other_factor
= isl_space_domain_factor_domain
,
9132 .product
= isl_map_reverse_domain_product
,
9135 return isl_map_intersect_factor(map
, factor
, &control
);
9138 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9139 * in the space A -> B, return the intersection.
9141 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
9142 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9144 struct isl_intersect_factor_control control
= {
9145 .preserve_type
= isl_dim_out
,
9146 .other_factor
= isl_space_range_factor_range
,
9147 .product
= isl_map_range_product
,
9150 return isl_map_intersect_factor(map
, factor
, &control
);
9153 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9154 * in the space A -> C, return the intersection.
9156 __isl_give isl_map
*isl_map_intersect_range_factor_range(
9157 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
9159 struct isl_intersect_factor_control control
= {
9160 .preserve_type
= isl_dim_out
,
9161 .other_factor
= isl_space_range_factor_domain
,
9162 .product
= isl_map_reverse_range_product
,
9165 return isl_map_intersect_factor(map
, factor
, &control
);
9168 /* Given a set "set" in a space [A -> B] and a set "domain"
9169 * in the space A, return the intersection.
9171 * The set "domain" is first extended to a set living in the space
9172 * [A -> B] and then a regular intersection is computed.
9174 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
9175 __isl_take isl_set
*domain
)
9177 struct isl_intersect_factor_control control
= {
9178 .preserve_type
= isl_dim_set
,
9179 .other_factor
= isl_space_factor_range
,
9180 .product
= isl_map_range_product
,
9183 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
9184 set_to_map(domain
), &control
));
9187 /* Given a set "set" in a space [A -> B] and a set "range"
9188 * in the space B, return the intersection.
9190 * The set "range" is first extended to a set living in the space
9191 * [A -> B] and then a regular intersection is computed.
9193 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
9194 __isl_take isl_set
*range
)
9196 struct isl_intersect_factor_control control
= {
9197 .preserve_type
= isl_dim_set
,
9198 .other_factor
= isl_space_factor_domain
,
9199 .product
= isl_map_reverse_range_product
,
9202 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
9203 set_to_map(range
), &control
));
9209 #include "isl_copy_tuple_id_templ.c"
9211 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
9212 * in the space A, return the intersection.
9214 * The set "domain" is extended to a set living in the space [A -> B] and
9215 * the domain of "map" is intersected with this set.
9217 * If "map" has an identifier on the domain tuple,
9218 * then this identifier needs to be set on this product
9219 * before the intersection is computed.
9221 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
9222 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
9227 isl_map_align_params_set(&map
, &domain
);
9228 space
= isl_map_get_space(map
);
9229 space
= isl_space_domain_wrapped_range(space
);
9230 factor
= isl_set_universe(space
);
9231 domain
= isl_set_product(domain
, factor
);
9232 space
= isl_map_peek_space(map
);
9233 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
9234 return isl_map_intersect_domain(map
, domain
);
9237 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
9238 * in the space B, return the intersection.
9240 * The set "domain" is extended to a set living in the space [B -> C] and
9241 * the range of "map" is intersected with this set.
9243 * If "map" has an identifier on the range tuple,
9244 * then this identifier needs to be set on this product
9245 * before the intersection is computed.
9247 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
9248 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
9253 isl_map_align_params_set(&map
, &domain
);
9254 space
= isl_map_get_space(map
);
9255 space
= isl_space_range_wrapped_range(space
);
9256 factor
= isl_set_universe(space
);
9257 domain
= isl_set_product(domain
, factor
);
9258 space
= isl_map_peek_space(map
);
9259 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
9260 return isl_map_intersect_range(map
, domain
);
9263 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
9264 __isl_take isl_map
*map2
)
9266 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
9268 map1
= isl_map_reverse(map1
);
9269 map1
= isl_map_apply_range(map1
, map2
);
9270 return isl_map_reverse(map1
);
9277 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
9278 __isl_take isl_map
*map2
)
9281 struct isl_map
*result
;
9284 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
9287 space
= isl_space_join(isl_space_copy(map1
->dim
),
9288 isl_space_copy(map2
->dim
));
9290 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
9293 for (i
= 0; i
< map1
->n
; ++i
)
9294 for (j
= 0; j
< map2
->n
; ++j
) {
9295 result
= isl_map_add_basic_map(result
,
9296 isl_basic_map_apply_range(
9297 isl_basic_map_copy(map1
->p
[i
]),
9298 isl_basic_map_copy(map2
->p
[j
])));
9304 if (result
&& result
->n
<= 1)
9305 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
9313 /* Is "bmap" a transformation, i.e.,
9314 * does it relate elements from the same space.
9316 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
9320 space
= isl_basic_map_peek_space(bmap
);
9321 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
9324 /* Check that "bmap" is a transformation, i.e.,
9325 * that it relates elements from the same space.
9327 static isl_stat
isl_basic_map_check_transformation(
9328 __isl_keep isl_basic_map
*bmap
)
9332 equal
= isl_basic_map_is_transformation(bmap
);
9334 return isl_stat_error
;
9336 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9337 "domain and range don't match", return isl_stat_error
);
9342 * returns range - domain
9344 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
9346 isl_space
*target_space
;
9347 struct isl_basic_set
*bset
;
9353 if (isl_basic_map_check_transformation(bmap
) < 0)
9354 return isl_basic_map_free(bmap
);
9355 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
9356 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
9357 if (dim
< 0 || nparam
< 0)
9359 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
9360 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
9361 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
9362 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9364 bmap
= isl_basic_map_free(bmap
);
9365 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
9366 for (i
= 0; i
< dim
; ++i
) {
9367 int j
= isl_basic_map_alloc_equality(bmap
);
9369 bmap
= isl_basic_map_free(bmap
);
9372 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
9373 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
9374 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
9375 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
9377 bset
= isl_basic_map_domain(bmap
);
9378 bset
= isl_basic_set_reset_space(bset
, target_space
);
9381 isl_basic_map_free(bmap
);
9385 /* Is the tuple of type "type1" of "map" the same as
9386 * the tuple of type "type2" of "space"?
9388 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
9389 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
9390 enum isl_dim_type type2
)
9392 isl_space
*map_space
;
9394 map_space
= isl_map_peek_space(map
);
9395 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
9398 /* Is the tuple of type "type1" of "map1" the same as
9399 * the tuple of type "type2" of "map2"?
9401 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
9402 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
9403 enum isl_dim_type type2
)
9405 isl_space
*space1
, *space2
;
9407 space1
= isl_map_peek_space(map1
);
9408 space2
= isl_map_peek_space(map2
);
9409 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
9412 /* Is the space of "obj" equal to "space", ignoring parameters?
9414 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
9415 __isl_keep isl_space
*space
)
9417 isl_space
*map_space
;
9419 map_space
= isl_map_peek_space(map
);
9420 return isl_space_has_equal_tuples(map_space
, space
);
9423 /* Check that "map" is a transformation, i.e.,
9424 * that it relates elements from the same space.
9426 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
9430 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
9432 return isl_stat_error
;
9434 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9435 "domain and range don't match", return isl_stat_error
);
9440 * returns range - domain
9442 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
9446 struct isl_set
*result
;
9448 if (isl_map_check_transformation(map
) < 0)
9450 space
= isl_map_get_space(map
);
9451 space
= isl_space_domain(space
);
9452 result
= isl_set_alloc_space(space
, map
->n
, 0);
9455 for (i
= 0; i
< map
->n
; ++i
)
9456 result
= isl_set_add_basic_set(result
,
9457 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
9466 * returns [domain -> range] -> range - domain
9468 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
9469 __isl_take isl_basic_map
*bmap
)
9473 isl_basic_map
*domain
;
9477 if (isl_basic_map_check_transformation(bmap
) < 0)
9478 return isl_basic_map_free(bmap
);
9480 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
9481 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
9482 if (nparam
< 0 || n
< 0)
9483 return isl_basic_map_free(bmap
);
9485 space
= isl_basic_map_get_space(bmap
);
9486 space
= isl_space_from_range(isl_space_domain(space
));
9487 domain
= isl_basic_map_universe(space
);
9489 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
9490 bmap
= isl_basic_map_apply_range(bmap
, domain
);
9491 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
9493 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9495 return isl_basic_map_free(bmap
);
9497 for (i
= 0; i
< n
; ++i
) {
9498 k
= isl_basic_map_alloc_equality(bmap
);
9501 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
9502 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
9503 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
9504 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
9507 bmap
= isl_basic_map_gauss(bmap
, NULL
);
9508 return isl_basic_map_finalize(bmap
);
9510 isl_basic_map_free(bmap
);
9515 * returns [domain -> range] -> range - domain
9517 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
9519 if (isl_map_check_transformation(map
) < 0)
9520 return isl_map_free(map
);
9522 return isl_map_transform(map
, &isl_space_range_map
,
9523 &isl_basic_map_deltas_map
);
9526 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9528 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
9533 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
9534 map
= isl_map_deltas_map(isl_map_universe(space
));
9535 map
= isl_map_intersect_range(map
, deltas
);
9537 return isl_set_unwrap(isl_map_domain(map
));
9540 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
9542 isl_size n_in
, n_out
;
9544 n_in
= isl_space_dim(space
, isl_dim_in
);
9545 n_out
= isl_space_dim(space
, isl_dim_out
);
9546 if (n_in
< 0 || n_out
< 0)
9549 isl_die(space
->ctx
, isl_error_invalid
,
9550 "number of input and output dimensions needs to be "
9551 "the same", goto error
);
9552 return isl_basic_map_equal(space
, n_in
);
9554 isl_space_free(space
);
9558 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
9560 return isl_map_from_basic_map(isl_basic_map_identity(space
));
9563 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
9565 isl_space
*space
= isl_set_get_space(set
);
9567 id
= isl_map_identity(isl_space_map_from_set(space
));
9568 return isl_map_intersect_range(id
, set
);
9571 /* Construct a basic set with all set dimensions having only non-negative
9574 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9575 __isl_take isl_space
*space
)
9581 struct isl_basic_set
*bset
;
9583 nparam
= isl_space_dim(space
, isl_dim_param
);
9584 dim
= isl_space_dim(space
, isl_dim_set
);
9585 total
= isl_space_dim(space
, isl_dim_all
);
9586 if (nparam
< 0 || dim
< 0 || total
< 0)
9587 space
= isl_space_free(space
);
9588 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9591 for (i
= 0; i
< dim
; ++i
) {
9592 int k
= isl_basic_set_alloc_inequality(bset
);
9595 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9596 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9600 isl_basic_set_free(bset
);
9604 /* Construct the half-space x_pos >= 0.
9606 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9611 isl_basic_set
*nonneg
;
9613 total
= isl_space_dim(space
, isl_dim_all
);
9615 space
= isl_space_free(space
);
9616 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9617 k
= isl_basic_set_alloc_inequality(nonneg
);
9620 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9621 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9623 return isl_basic_set_finalize(nonneg
);
9625 isl_basic_set_free(nonneg
);
9629 /* Construct the half-space x_pos <= -1.
9631 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9638 total
= isl_space_dim(space
, isl_dim_all
);
9640 space
= isl_space_free(space
);
9641 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9642 k
= isl_basic_set_alloc_inequality(neg
);
9645 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9646 isl_int_set_si(neg
->ineq
[k
][0], -1);
9647 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9649 return isl_basic_set_finalize(neg
);
9651 isl_basic_set_free(neg
);
9655 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9656 enum isl_dim_type type
, unsigned first
, unsigned n
)
9660 isl_basic_set
*nonneg
;
9666 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9667 return isl_set_free(set
);
9669 offset
= pos(set
->dim
, type
);
9670 for (i
= 0; i
< n
; ++i
) {
9671 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9672 offset
+ first
+ i
);
9673 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9675 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9681 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9683 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9689 return isl_stat_error
;
9690 if (isl_set_plain_is_empty(set
)) {
9695 return fn(set
, signs
, user
);
9698 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9700 half
= isl_set_intersect(half
, isl_set_copy(set
));
9701 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9705 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9707 half
= isl_set_intersect(half
, set
);
9708 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9711 return isl_stat_error
;
9714 /* Call "fn" on the intersections of "set" with each of the orthants
9715 * (except for obviously empty intersections). The orthant is identified
9716 * by the signs array, with each entry having value 1 or -1 according
9717 * to the sign of the corresponding variable.
9719 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9720 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9729 return isl_stat_error
;
9730 if (isl_set_plain_is_empty(set
))
9733 nparam
= isl_set_dim(set
, isl_dim_param
);
9734 nvar
= isl_set_dim(set
, isl_dim_set
);
9735 if (nparam
< 0 || nvar
< 0)
9736 return isl_stat_error
;
9738 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9740 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9748 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9750 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9753 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9754 __isl_keep isl_basic_map
*bmap2
)
9757 struct isl_map
*map1
;
9758 struct isl_map
*map2
;
9760 if (!bmap1
|| !bmap2
)
9761 return isl_bool_error
;
9763 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9764 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9766 is_subset
= isl_map_is_subset(map1
, map2
);
9774 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9775 __isl_keep isl_basic_set
*bset2
)
9777 return isl_basic_map_is_subset(bset1
, bset2
);
9780 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9781 __isl_keep isl_basic_map
*bmap2
)
9785 if (!bmap1
|| !bmap2
)
9786 return isl_bool_error
;
9787 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9788 if (is_subset
!= isl_bool_true
)
9790 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9794 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9795 __isl_keep isl_basic_set
*bset2
)
9797 return isl_basic_map_is_equal(
9798 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9801 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9807 return isl_bool_error
;
9808 for (i
= 0; i
< map
->n
; ++i
) {
9809 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9811 return isl_bool_error
;
9813 return isl_bool_false
;
9815 return isl_bool_true
;
9818 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9820 return map
? map
->n
== 0 : isl_bool_error
;
9823 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9825 return set
? set
->n
== 0 : isl_bool_error
;
9828 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9830 return isl_map_is_empty(set_to_map(set
));
9834 #define TYPE isl_basic_map
9837 #include "isl_type_has_equal_space_bin_templ.c"
9838 #include "isl_type_check_equal_space_templ.c"
9840 /* Check that "bset1" and "bset2" live in the same space,
9841 * reporting an error if they do not.
9843 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9844 __isl_keep isl_basic_set
*bset2
)
9846 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9847 bset_to_bmap(bset1
));
9851 #define TYPE isl_map
9853 #include "isl_type_has_equal_space_bin_templ.c"
9854 #include "isl_type_check_equal_space_templ.c"
9855 #include "isl_type_has_space_templ.c"
9857 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9858 __isl_keep isl_set
*set2
)
9860 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9864 #define TYPE1 isl_map
9866 #define TYPE2 isl_basic_map
9868 #define TYPE_PAIR isl_map_basic_map
9871 #include "isl_type_has_equal_space_templ.c"
9872 #include "isl_type_check_equal_space_templ.c"
9874 /* Check that "set" and "bset" live in the same space,
9875 * reporting an error if they do not.
9877 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9878 __isl_keep isl_basic_set
*bset
)
9880 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9881 bset_to_bmap(bset
));
9884 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9889 return isl_bool_error
;
9890 is_subset
= isl_map_is_subset(map1
, map2
);
9891 if (is_subset
!= isl_bool_true
)
9893 is_subset
= isl_map_is_subset(map2
, map1
);
9897 /* Is "map1" equal to "map2"?
9899 * First check if they are obviously equal.
9900 * If not, then perform a more detailed analysis.
9902 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9906 equal
= isl_map_plain_is_equal(map1
, map2
);
9907 if (equal
< 0 || equal
)
9909 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9912 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9913 __isl_keep isl_basic_map
*bmap2
)
9917 if (!bmap1
|| !bmap2
)
9918 return isl_bool_error
;
9919 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9920 if (is_subset
!= isl_bool_true
)
9922 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9923 return isl_bool_not(is_subset
);
9926 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9927 __isl_keep isl_map
*map2
)
9932 return isl_bool_error
;
9933 is_subset
= isl_map_is_subset(map1
, map2
);
9934 if (is_subset
!= isl_bool_true
)
9936 is_subset
= isl_map_is_subset(map2
, map1
);
9937 return isl_bool_not(is_subset
);
9940 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9941 __isl_keep isl_set
*set2
)
9943 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9946 /* Is "bmap" obviously equal to the universe with the same space?
9948 * That is, does it not have any constraints?
9950 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9953 return isl_bool_error
;
9954 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9957 /* Is "bset" obviously equal to the universe with the same space?
9959 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9961 return isl_basic_map_plain_is_universe(bset
);
9964 /* If "c" does not involve any existentially quantified variables,
9965 * then set *univ to false and abort
9967 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9969 isl_bool
*univ
= user
;
9972 n
= isl_constraint_dim(c
, isl_dim_div
);
9974 c
= isl_constraint_free(c
);
9975 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9976 isl_constraint_free(c
);
9977 if (*univ
< 0 || !*univ
)
9978 return isl_stat_error
;
9982 /* Is "bmap" equal to the universe with the same space?
9984 * First check if it is obviously equal to the universe.
9985 * If not and if there are any constraints not involving
9986 * existentially quantified variables, then it is certainly
9987 * not equal to the universe.
9988 * Otherwise, check if the universe is a subset of "bmap".
9990 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9994 isl_basic_map
*test
;
9996 univ
= isl_basic_map_plain_is_universe(bmap
);
9997 if (univ
< 0 || univ
)
9999 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10001 return isl_bool_error
;
10003 return isl_bool_false
;
10004 univ
= isl_bool_true
;
10005 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
10007 return isl_bool_error
;
10008 if (univ
< 0 || !univ
)
10010 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
10011 univ
= isl_basic_map_is_subset(test
, bmap
);
10012 isl_basic_map_free(test
);
10016 /* Is "bset" equal to the universe with the same space?
10018 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
10020 return isl_basic_map_is_universe(bset
);
10023 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
10028 return isl_bool_error
;
10030 for (i
= 0; i
< map
->n
; ++i
) {
10031 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
10036 return isl_bool_false
;
10039 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
10041 return isl_map_plain_is_universe(set_to_map(set
));
10044 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
10046 struct isl_basic_set
*bset
= NULL
;
10047 struct isl_vec
*sample
= NULL
;
10048 isl_bool empty
, non_empty
;
10051 return isl_bool_error
;
10053 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
10054 return isl_bool_true
;
10056 if (isl_basic_map_plain_is_universe(bmap
))
10057 return isl_bool_false
;
10059 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
10060 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
10061 copy
= isl_basic_map_remove_redundancies(copy
);
10062 empty
= isl_basic_map_plain_is_empty(copy
);
10063 isl_basic_map_free(copy
);
10067 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
10069 return isl_bool_error
;
10071 return isl_bool_false
;
10072 isl_vec_free(bmap
->sample
);
10073 bmap
->sample
= NULL
;
10074 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
10076 return isl_bool_error
;
10077 sample
= isl_basic_set_sample_vec(bset
);
10079 return isl_bool_error
;
10080 empty
= sample
->size
== 0;
10081 isl_vec_free(bmap
->sample
);
10082 bmap
->sample
= sample
;
10084 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
10089 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
10092 return isl_bool_error
;
10093 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
10096 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
10099 return isl_bool_error
;
10100 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
10103 /* Is "bmap" known to be non-empty?
10105 * That is, is the cached sample still valid?
10107 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
10112 return isl_bool_error
;
10114 return isl_bool_false
;
10115 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10117 return isl_bool_error
;
10118 if (bmap
->sample
->size
!= 1 + total
)
10119 return isl_bool_false
;
10120 return isl_basic_map_contains(bmap
, bmap
->sample
);
10123 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
10125 return isl_basic_map_is_empty(bset_to_bmap(bset
));
10128 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
10129 __isl_take isl_basic_map
*bmap2
)
10131 struct isl_map
*map
;
10133 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
10136 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
10139 map
= isl_map_add_basic_map(map
, bmap1
);
10140 map
= isl_map_add_basic_map(map
, bmap2
);
10143 isl_basic_map_free(bmap1
);
10144 isl_basic_map_free(bmap2
);
10148 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
10149 __isl_take isl_basic_set
*bset2
)
10151 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
10152 bset_to_bmap(bset2
)));
10155 /* Order divs such that any div only depends on previous divs */
10156 __isl_give isl_basic_map
*isl_basic_map_order_divs(
10157 __isl_take isl_basic_map
*bmap
)
10162 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
10164 return isl_basic_map_free(bmap
);
10166 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10168 if (isl_int_is_zero(bmap
->div
[i
][0]))
10170 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
10175 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
10176 "integer division depends on itself",
10177 return isl_basic_map_free(bmap
));
10178 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
10186 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
10193 for (i
= 0; i
< map
->n
; ++i
) {
10194 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
10205 /* Sort the local variables of "bset".
10207 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
10208 __isl_take isl_basic_set
*bset
)
10210 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
10213 /* Apply the expansion computed by isl_merge_divs.
10214 * The expansion itself is given by "exp" while the resulting
10215 * list of divs is given by "div".
10217 * Move the integer divisions of "bmap" into the right position
10218 * according to "exp" and then introduce the additional integer
10219 * divisions, adding div constraints.
10220 * The moving should be done first to avoid moving coefficients
10221 * in the definitions of the extra integer divisions.
10223 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
10224 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
10229 bmap
= isl_basic_map_cow(bmap
);
10233 if (div
->n_row
< bmap
->n_div
)
10234 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
10235 "not an expansion", goto error
);
10237 n_div
= bmap
->n_div
;
10238 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
10239 2 * (div
->n_row
- n_div
));
10241 for (i
= n_div
; i
< div
->n_row
; ++i
)
10242 if (isl_basic_map_alloc_div(bmap
) < 0)
10245 for (j
= n_div
- 1; j
>= 0; --j
) {
10248 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
10253 for (i
= 0; i
< div
->n_row
; ++i
) {
10254 if (j
< n_div
&& exp
[j
] == i
) {
10257 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
10258 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
10260 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
10269 isl_basic_map_free(bmap
);
10274 /* Apply the expansion computed by isl_merge_divs.
10275 * The expansion itself is given by "exp" while the resulting
10276 * list of divs is given by "div".
10278 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
10279 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
10281 return isl_basic_map_expand_divs(bset
, div
, exp
);
10284 /* Look for a div in dst that corresponds to the div "div" in src.
10285 * The divs before "div" in src and dst are assumed to be the same.
10287 * Return the position of the corresponding div in dst
10288 * if there is one. Otherwise, return a position beyond the integer divisions.
10289 * Return isl_size_error on error.
10291 static isl_size
find_div(__isl_keep isl_basic_map
*dst
,
10292 __isl_keep isl_basic_map
*src
, unsigned div
)
10298 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
10299 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
10300 if (n_div
< 0 || v_div
< 0)
10301 return isl_size_error
;
10302 isl_assert(dst
->ctx
, div
<= n_div
, return isl_size_error
);
10303 for (i
= div
; i
< n_div
; ++i
)
10304 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
10305 !isl_seq_any_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
10311 /* Align the local variables of "dst" to those of "src",
10312 * adding local variables from "src" if needed.
10313 * That is, make sure that the first src->n_div local variables
10314 * of the result correspond to those of src.
10315 * For any integer division that is copied to "dst",
10316 * the defining constraints are also introduced to "dst".
10317 * For an existentially quantified variable (without an explicit definition)
10318 * only an unconstrained existentially quantified variable
10319 * in the same positions is introduced.
10320 * The integer division of "src" are assumed to be ordered.
10322 * The integer divisions are swapped into the right position
10323 * (possibly after adding them first). This may result
10324 * in the remaining integer divisions appearing in the wrong order,
10325 * i.e., with some integer division appearing before
10326 * some other integer division on which it depends.
10327 * The integer divisions therefore need to be ordered.
10328 * This will not affect the integer divisions aligned to those of "src",
10329 * since "src" is assumed to have ordered integer divisions.
10331 * The result is not finalized as by design it will have redundant
10332 * divs if any divs from "src" were copied.
10334 __isl_give isl_basic_map
*isl_basic_map_align_divs(
10335 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
10340 isl_size dst_n_div
, src_n_div
;
10342 src_n_div
= isl_basic_map_dim(src
, isl_dim_div
);
10343 if (!dst
|| src_n_div
< 0)
10344 return isl_basic_map_free(dst
);
10346 if (src_n_div
== 0)
10349 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
10351 return isl_basic_map_free(dst
);
10354 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
10356 dst
= isl_basic_map_free(dst
);
10357 for (i
= 0; i
< src_n_div
; ++i
) {
10361 known
= isl_basic_map_div_is_known(src
, i
);
10363 return isl_basic_map_free(dst
);
10364 j
= known
? find_div(dst
, src
, i
) : dst_n_div
;
10366 dst
= isl_basic_map_free(dst
);
10367 if (j
== dst_n_div
) {
10369 int extra
= src_n_div
- i
;
10370 dst
= isl_basic_map_cow(dst
);
10372 return isl_basic_map_free(dst
);
10373 dst
= isl_basic_map_extend(dst
,
10374 extra
, 0, 2 * extra
);
10377 j
= isl_basic_map_alloc_div(dst
);
10379 return isl_basic_map_free(dst
);
10383 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
10384 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst_n_div
- i
);
10385 dst
= isl_basic_map_add_div_constraints(dst
, j
);
10387 return isl_basic_map_free(dst
);
10390 dst
= isl_basic_map_swap_div(dst
, i
, j
);
10392 return isl_basic_map_free(dst
);
10394 return isl_basic_map_order_divs(dst
);
10397 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
10405 map
= isl_map_compute_divs(map
);
10406 map
= isl_map_order_divs(map
);
10407 map
= isl_map_cow(map
);
10411 for (i
= 1; i
< map
->n
; ++i
)
10412 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
10413 for (i
= 1; i
< map
->n
; ++i
) {
10414 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
10416 return isl_map_free(map
);
10419 map
= isl_map_unmark_normalized(map
);
10423 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
10425 return isl_map_align_divs_internal(map
);
10428 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
10430 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10433 /* Align the divs of the basic maps in "map" to those
10434 * of the basic maps in "list", as well as to the other basic maps in "map".
10435 * The elements in "list" are assumed to have known divs.
10437 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
10438 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
10443 n
= isl_basic_map_list_n_basic_map(list
);
10444 map
= isl_map_compute_divs(map
);
10445 map
= isl_map_cow(map
);
10447 return isl_map_free(map
);
10451 for (i
= 0; i
< n
; ++i
) {
10452 isl_basic_map
*bmap
;
10454 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
10455 bmap
= isl_basic_map_order_divs(bmap
);
10456 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
10457 isl_basic_map_free(bmap
);
10460 return isl_map_free(map
);
10462 return isl_map_align_divs_internal(map
);
10465 /* Align the divs of each element of "list" to those of "bmap".
10466 * Both "bmap" and the elements of "list" are assumed to have known divs.
10468 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
10469 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
10474 n
= isl_basic_map_list_n_basic_map(list
);
10475 if (n
< 0 || !bmap
)
10476 return isl_basic_map_list_free(list
);
10478 for (i
= 0; i
< n
; ++i
) {
10479 isl_basic_map
*bmap_i
;
10481 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10482 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
10483 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
10489 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
10490 __isl_take isl_map
*map
)
10494 isl_map_align_params_set(&map
, &set
);
10495 ok
= isl_map_compatible_domain(map
, set
);
10499 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
10500 "incompatible spaces", goto error
);
10501 map
= isl_map_intersect_domain(map
, set
);
10502 set
= isl_map_range(map
);
10510 /* There is no need to cow as removing empty parts doesn't change
10511 * the meaning of the set.
10513 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10520 for (i
= map
->n
- 1; i
>= 0; --i
)
10521 map
= remove_if_empty(map
, i
);
10526 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10528 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10531 /* Create a binary relation that maps the shared initial "pos" dimensions
10532 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10534 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10535 __isl_keep isl_basic_set
*bset2
, int pos
)
10537 isl_basic_map
*bmap1
;
10538 isl_basic_map
*bmap2
;
10540 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10541 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10542 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10543 isl_dim_out
, 0, pos
);
10544 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10545 isl_dim_out
, 0, pos
);
10546 return isl_basic_map_range_product(bmap1
, bmap2
);
10549 /* Given two basic sets bset1 and bset2, compute the maximal difference
10550 * between the values of dimension pos in bset1 and those in bset2
10551 * for any common value of the parameters and dimensions preceding pos.
10553 static enum isl_lp_result
basic_set_maximal_difference_at(
10554 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10555 int pos
, isl_int
*opt
)
10557 isl_basic_map
*bmap1
;
10558 struct isl_ctx
*ctx
;
10559 struct isl_vec
*obj
;
10563 enum isl_lp_result res
;
10565 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10566 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10567 if (nparam
< 0 || dim1
< 0 || !bset2
)
10568 return isl_lp_error
;
10570 bmap1
= join_initial(bset1
, bset2
, pos
);
10571 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10573 return isl_lp_error
;
10576 obj
= isl_vec_alloc(ctx
, 1 + total
);
10579 isl_seq_clr(obj
->block
.data
, 1 + total
);
10580 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10581 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10582 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10584 isl_basic_map_free(bmap1
);
10588 isl_basic_map_free(bmap1
);
10589 return isl_lp_error
;
10592 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10593 * for any common value of the parameters and dimensions preceding pos
10594 * in both basic sets, the values of dimension pos in bset1 are
10595 * smaller or larger than those in bset2.
10598 * 1 if bset1 follows bset2
10599 * -1 if bset1 precedes bset2
10600 * 0 if bset1 and bset2 are incomparable
10601 * -2 if some error occurred.
10603 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10604 __isl_keep isl_basic_set
*bset2
, int pos
)
10607 enum isl_lp_result res
;
10612 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10614 if (res
== isl_lp_empty
)
10616 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10617 res
== isl_lp_unbounded
)
10619 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10624 isl_int_clear(opt
);
10628 /* Given two basic sets bset1 and bset2, check whether
10629 * for any common value of the parameters and dimensions preceding pos
10630 * there is a value of dimension pos in bset1 that is larger
10631 * than a value of the same dimension in bset2.
10634 * 1 if there exists such a pair
10635 * 0 if there is no such pair, but there is a pair of equal values
10637 * -2 if some error occurred.
10639 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10640 __isl_keep isl_basic_set
*bset2
, int pos
)
10643 isl_basic_map
*bmap
;
10646 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10649 bmap
= join_initial(bset1
, bset2
, pos
);
10650 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10651 isl_dim_out
, dim1
- pos
);
10652 empty
= isl_basic_map_is_empty(bmap
);
10656 isl_basic_map_free(bmap
);
10659 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10660 isl_dim_out
, dim1
- pos
);
10661 empty
= isl_basic_map_is_empty(bmap
);
10664 isl_basic_map_free(bmap
);
10669 isl_basic_map_free(bmap
);
10673 /* Given two sets set1 and set2, check whether
10674 * for any common value of the parameters and dimensions preceding pos
10675 * there is a value of dimension pos in set1 that is larger
10676 * than a value of the same dimension in set2.
10679 * 1 if there exists such a pair
10680 * 0 if there is no such pair, but there is a pair of equal values
10682 * -2 if some error occurred.
10684 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10685 __isl_keep isl_set
*set2
, int pos
)
10690 if (!set1
|| !set2
)
10693 for (i
= 0; i
< set1
->n
; ++i
)
10694 for (j
= 0; j
< set2
->n
; ++j
) {
10696 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10697 if (f
== 1 || f
== -2)
10706 static isl_bool
isl_basic_map_plain_has_fixed_var(
10707 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10713 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10715 return isl_bool_error
;
10716 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10717 for (; d
+1 > pos
; --d
)
10718 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10722 if (isl_seq_any_non_zero(bmap
->eq
[i
]+1, d
))
10723 return isl_bool_false
;
10724 if (isl_seq_any_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1))
10725 return isl_bool_false
;
10726 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10727 return isl_bool_false
;
10729 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10730 return isl_bool_true
;
10732 return isl_bool_false
;
10735 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10736 unsigned pos
, isl_int
*val
)
10744 return isl_bool_error
;
10746 return isl_bool_false
;
10748 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10751 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10752 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10753 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10754 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10755 fixed
= isl_bool_false
;
10758 isl_int_set(*val
, v
);
10759 isl_int_clear(tmp
);
10764 static isl_bool
isl_basic_set_plain_has_fixed_var(
10765 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10767 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10771 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10772 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10774 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10775 return isl_bool_error
;
10776 return isl_basic_map_plain_has_fixed_var(bmap
,
10777 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10780 /* If "bmap" obviously lies on a hyperplane where the given dimension
10781 * has a fixed value, then return that value.
10782 * Otherwise return NaN.
10784 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10785 __isl_keep isl_basic_map
*bmap
,
10786 enum isl_dim_type type
, unsigned pos
)
10794 ctx
= isl_basic_map_get_ctx(bmap
);
10795 v
= isl_val_alloc(ctx
);
10798 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10800 return isl_val_free(v
);
10802 isl_int_set_si(v
->d
, 1);
10806 return isl_val_nan(ctx
);
10809 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10810 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10812 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10813 return isl_bool_error
;
10814 return isl_map_plain_has_fixed_var(map
,
10815 map_offset(map
, type
) - 1 + pos
, val
);
10818 /* If "map" obviously lies on a hyperplane where the given dimension
10819 * has a fixed value, then return that value.
10820 * Otherwise return NaN.
10822 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10823 enum isl_dim_type type
, unsigned pos
)
10831 ctx
= isl_map_get_ctx(map
);
10832 v
= isl_val_alloc(ctx
);
10835 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10837 return isl_val_free(v
);
10839 isl_int_set_si(v
->d
, 1);
10843 return isl_val_nan(ctx
);
10846 /* If "set" obviously lies on a hyperplane where the given dimension
10847 * has a fixed value, then return that value.
10848 * Otherwise return NaN.
10850 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10851 enum isl_dim_type type
, unsigned pos
)
10853 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10856 /* Return a sequence of values in the same space as "set"
10857 * that are equal to the corresponding set dimensions of "set"
10858 * for those set dimensions that obviously lie on a hyperplane
10859 * where the dimension has a fixed value.
10860 * The other elements are set to NaN.
10862 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10863 __isl_keep isl_set
*set
)
10870 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10871 mv
= isl_multi_val_alloc(space
);
10872 n
= isl_multi_val_size(mv
);
10874 return isl_multi_val_free(mv
);
10876 for (i
= 0; i
< n
; ++i
) {
10879 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10880 mv
= isl_multi_val_set_val(mv
, i
, v
);
10886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10887 * then return this fixed value in *val.
10889 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10890 unsigned dim
, isl_int
*val
)
10894 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10896 return isl_bool_error
;
10897 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10900 /* Return -1 if the constraint "c1" should be sorted before "c2"
10901 * and 1 if it should be sorted after "c2".
10902 * Return 0 if the two constraints are the same (up to the constant term).
10904 * In particular, if a constraint involves later variables than another
10905 * then it is sorted after this other constraint.
10906 * uset_gist depends on constraints without existentially quantified
10907 * variables sorting first.
10909 * For constraints that have the same latest variable, those
10910 * with the same coefficient for this latest variable (first in absolute value
10911 * and then in actual value) are grouped together.
10912 * This is useful for detecting pairs of constraints that can
10913 * be chained in their printed representation.
10915 * Finally, within a group, constraints are sorted according to
10916 * their coefficients (excluding the constant term).
10918 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10920 isl_int
**c1
= (isl_int
**) p1
;
10921 isl_int
**c2
= (isl_int
**) p2
;
10923 unsigned size
= *(unsigned *) arg
;
10926 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10927 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10932 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10935 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10939 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10942 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10943 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10944 * and 0 if the two constraints are the same (up to the constant term).
10946 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10947 isl_int
*c1
, isl_int
*c2
)
10952 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10956 return sort_constraint_cmp(&c1
, &c2
, &size
);
10959 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10960 __isl_take isl_basic_map
*bmap
)
10967 if (bmap
->n_ineq
== 0)
10969 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10971 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10973 return isl_basic_map_free(bmap
);
10975 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10976 &sort_constraint_cmp
, &size
) < 0)
10977 return isl_basic_map_free(bmap
);
10978 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10982 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10983 __isl_take isl_basic_set
*bset
)
10985 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10986 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10989 __isl_give isl_basic_map
*isl_basic_map_normalize(
10990 __isl_take isl_basic_map
*bmap
)
10992 bmap
= isl_basic_map_remove_redundancies(bmap
);
10993 bmap
= isl_basic_map_sort_constraints(bmap
);
10996 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10997 __isl_keep isl_basic_map
*bmap2
)
11001 isl_space
*space1
, *space2
;
11003 if (!bmap1
|| !bmap2
)
11006 if (bmap1
== bmap2
)
11008 space1
= isl_basic_map_peek_space(bmap1
);
11009 space2
= isl_basic_map_peek_space(bmap2
);
11010 cmp
= isl_space_cmp(space1
, space2
);
11013 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
11014 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
11015 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
11016 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
11017 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11019 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
11021 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
11023 if (bmap1
->n_eq
!= bmap2
->n_eq
)
11024 return bmap1
->n_eq
- bmap2
->n_eq
;
11025 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
11026 return bmap1
->n_ineq
- bmap2
->n_ineq
;
11027 if (bmap1
->n_div
!= bmap2
->n_div
)
11028 return bmap1
->n_div
- bmap2
->n_div
;
11029 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
11032 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
11033 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
11037 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
11038 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
11042 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
11043 isl_bool unknown1
, unknown2
;
11045 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
11046 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
11047 if (unknown1
< 0 || unknown2
< 0)
11049 if (unknown1
&& unknown2
)
11055 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
11062 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
11063 __isl_keep isl_basic_set
*bset2
)
11065 return isl_basic_map_plain_cmp(bset1
, bset2
);
11068 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
11074 if (set1
->n
!= set2
->n
)
11075 return set1
->n
- set2
->n
;
11077 for (i
= 0; i
< set1
->n
; ++i
) {
11078 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
11086 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
11087 __isl_keep isl_basic_map
*bmap2
)
11089 if (!bmap1
|| !bmap2
)
11090 return isl_bool_error
;
11091 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
11094 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
11095 __isl_keep isl_basic_set
*bset2
)
11097 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
11098 bset_to_bmap(bset2
));
11101 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
11103 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
11104 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
11106 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
11109 /* Sort the basic maps of "map" and remove duplicate basic maps.
11111 * While removing basic maps, we make sure that the basic maps remain
11112 * sorted because isl_map_normalize expects the basic maps of the result
11115 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
11119 map
= isl_map_remove_empty_parts(map
);
11122 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
11123 for (i
= map
->n
- 1; i
>= 1; --i
) {
11124 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
11126 isl_basic_map_free(map
->p
[i
-1]);
11127 for (j
= i
; j
< map
->n
; ++j
)
11128 map
->p
[j
- 1] = map
->p
[j
];
11135 /* Remove obvious duplicates among the basic maps of "map".
11137 * Unlike isl_map_normalize, this function does not remove redundant
11138 * constraints and only removes duplicates that have exactly the same
11139 * constraints in the input. It does sort the constraints and
11140 * the basic maps to ease the detection of duplicates.
11142 * If "map" has already been normalized or if the basic maps are
11143 * disjoint, then there can be no duplicates.
11145 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
11148 isl_basic_map
*bmap
;
11154 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
11156 for (i
= 0; i
< map
->n
; ++i
) {
11157 bmap
= isl_basic_map_copy(map
->p
[i
]);
11158 bmap
= isl_basic_map_sort_constraints(bmap
);
11160 return isl_map_free(map
);
11161 isl_basic_map_free(map
->p
[i
]);
11165 map
= sort_and_remove_duplicates(map
);
11169 /* We normalize in place, but if anything goes wrong we need
11170 * to return NULL, so we need to make sure we don't change the
11171 * meaning of any possible other copies of map.
11173 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
11176 struct isl_basic_map
*bmap
;
11180 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
11182 for (i
= 0; i
< map
->n
; ++i
) {
11183 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
11186 isl_basic_map_free(map
->p
[i
]);
11190 map
= sort_and_remove_duplicates(map
);
11192 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
11199 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
11201 return set_from_map(isl_map_normalize(set_to_map(set
)));
11204 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
11205 __isl_keep isl_map
*map2
)
11210 if (!map1
|| !map2
)
11211 return isl_bool_error
;
11214 return isl_bool_true
;
11215 equal
= isl_map_has_equal_space(map1
, map2
);
11216 if (equal
< 0 || !equal
)
11219 map1
= isl_map_copy(map1
);
11220 map2
= isl_map_copy(map2
);
11221 map1
= isl_map_normalize(map1
);
11222 map2
= isl_map_normalize(map2
);
11223 if (!map1
|| !map2
)
11225 equal
= map1
->n
== map2
->n
;
11226 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
11227 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
11231 isl_map_free(map1
);
11232 isl_map_free(map2
);
11235 isl_map_free(map1
);
11236 isl_map_free(map2
);
11237 return isl_bool_error
;
11240 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
11241 __isl_keep isl_set
*set2
)
11243 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
11246 /* Return the basic maps in "map" as a list.
11248 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
11249 __isl_keep isl_map
*map
)
11253 isl_basic_map_list
*list
;
11257 ctx
= isl_map_get_ctx(map
);
11258 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
11260 for (i
= 0; i
< map
->n
; ++i
) {
11261 isl_basic_map
*bmap
;
11263 bmap
= isl_basic_map_copy(map
->p
[i
]);
11264 list
= isl_basic_map_list_add(list
, bmap
);
11270 /* Return the intersection of the elements in the non-empty list "list".
11271 * All elements are assumed to live in the same space.
11273 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
11274 __isl_take isl_basic_map_list
*list
)
11278 isl_basic_map
*bmap
;
11280 n
= isl_basic_map_list_n_basic_map(list
);
11284 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
11285 "expecting non-empty list", goto error
);
11287 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
11288 for (i
= 1; i
< n
; ++i
) {
11289 isl_basic_map
*bmap_i
;
11291 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
11292 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
11295 isl_basic_map_list_free(list
);
11298 isl_basic_map_list_free(list
);
11302 /* Return the intersection of the elements in the non-empty list "list".
11303 * All elements are assumed to live in the same space.
11305 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
11306 __isl_take isl_basic_set_list
*list
)
11308 return isl_basic_map_list_intersect(list
);
11311 /* Return the union of the elements of "list".
11312 * The list is required to have at least one element.
11314 __isl_give isl_set
*isl_basic_set_list_union(
11315 __isl_take isl_basic_set_list
*list
)
11320 isl_basic_set
*bset
;
11323 n
= isl_basic_set_list_n_basic_set(list
);
11327 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
11328 "expecting non-empty list", goto error
);
11330 bset
= isl_basic_set_list_get_basic_set(list
, 0);
11331 space
= isl_basic_set_get_space(bset
);
11332 isl_basic_set_free(bset
);
11334 set
= isl_set_alloc_space(space
, n
, 0);
11335 for (i
= 0; i
< n
; ++i
) {
11336 bset
= isl_basic_set_list_get_basic_set(list
, i
);
11337 set
= isl_set_add_basic_set(set
, bset
);
11340 isl_basic_set_list_free(list
);
11343 isl_basic_set_list_free(list
);
11347 /* Return the union of the elements in the non-empty list "list".
11348 * All elements are assumed to live in the same space.
11350 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
11356 n
= isl_set_list_n_set(list
);
11360 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
11361 "expecting non-empty list", goto error
);
11363 set
= isl_set_list_get_set(list
, 0);
11364 for (i
= 1; i
< n
; ++i
) {
11367 set_i
= isl_set_list_get_set(list
, i
);
11368 set
= isl_set_union(set
, set_i
);
11371 isl_set_list_free(list
);
11374 isl_set_list_free(list
);
11378 __isl_give isl_basic_map
*isl_basic_map_product(
11379 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11381 isl_space
*space_result
= NULL
;
11382 struct isl_basic_map
*bmap
;
11383 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
11384 struct isl_dim_map
*dim_map1
, *dim_map2
;
11386 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11388 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
11389 isl_space_copy(bmap2
->dim
));
11391 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11392 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11393 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11394 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11395 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11397 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11398 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11399 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11400 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11401 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11402 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11403 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11404 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11405 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11406 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11407 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11409 bmap
= isl_basic_map_alloc_space(space_result
,
11410 bmap1
->n_div
+ bmap2
->n_div
,
11411 bmap1
->n_eq
+ bmap2
->n_eq
,
11412 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11413 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11414 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11415 bmap
= isl_basic_map_simplify(bmap
);
11416 return isl_basic_map_finalize(bmap
);
11418 isl_basic_map_free(bmap1
);
11419 isl_basic_map_free(bmap2
);
11423 __isl_give isl_basic_map
*isl_basic_map_flat_product(
11424 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11426 isl_basic_map
*prod
;
11428 prod
= isl_basic_map_product(bmap1
, bmap2
);
11429 prod
= isl_basic_map_flatten(prod
);
11433 __isl_give isl_basic_set
*isl_basic_set_flat_product(
11434 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
11436 return isl_basic_map_flat_range_product(bset1
, bset2
);
11439 __isl_give isl_basic_map
*isl_basic_map_domain_product(
11440 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11442 isl_space
*space1
, *space2
;
11443 isl_space
*space_result
= NULL
;
11444 isl_basic_map
*bmap
;
11445 isl_size in1
, in2
, out
, nparam
;
11446 unsigned total
, pos
;
11447 struct isl_dim_map
*dim_map1
, *dim_map2
;
11449 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11450 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11451 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11452 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11453 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
11456 space1
= isl_basic_map_get_space(bmap1
);
11457 space2
= isl_basic_map_get_space(bmap2
);
11458 space_result
= isl_space_domain_product(space1
, space2
);
11460 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
11461 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11462 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11463 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11464 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11465 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11466 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11467 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11468 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
11469 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
11470 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11472 bmap
= isl_basic_map_alloc_space(space_result
,
11473 bmap1
->n_div
+ bmap2
->n_div
,
11474 bmap1
->n_eq
+ bmap2
->n_eq
,
11475 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11476 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11477 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11478 bmap
= isl_basic_map_simplify(bmap
);
11479 return isl_basic_map_finalize(bmap
);
11481 isl_basic_map_free(bmap1
);
11482 isl_basic_map_free(bmap2
);
11486 __isl_give isl_basic_map
*isl_basic_map_range_product(
11487 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11490 isl_space
*space_result
= NULL
;
11491 isl_basic_map
*bmap
;
11492 isl_size in
, out1
, out2
, nparam
;
11493 unsigned total
, pos
;
11494 struct isl_dim_map
*dim_map1
, *dim_map2
;
11496 rational
= isl_basic_map_is_rational(bmap1
);
11497 if (rational
>= 0 && rational
)
11498 rational
= isl_basic_map_is_rational(bmap2
);
11499 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11500 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11501 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11502 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11503 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
11506 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11509 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
11510 isl_space_copy(bmap2
->dim
));
11512 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11513 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11514 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11515 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11516 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11517 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11518 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11519 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11520 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11521 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11522 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11524 bmap
= isl_basic_map_alloc_space(space_result
,
11525 bmap1
->n_div
+ bmap2
->n_div
,
11526 bmap1
->n_eq
+ bmap2
->n_eq
,
11527 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11528 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11529 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11531 bmap
= isl_basic_map_set_rational(bmap
);
11532 bmap
= isl_basic_map_simplify(bmap
);
11533 return isl_basic_map_finalize(bmap
);
11535 isl_basic_map_free(bmap1
);
11536 isl_basic_map_free(bmap2
);
11540 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11541 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11543 isl_basic_map
*prod
;
11545 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11546 prod
= isl_basic_map_flatten_range(prod
);
11550 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11551 * and collect the results.
11552 * The result live in the space obtained by calling "space_product"
11553 * on the spaces of "map1" and "map2".
11554 * If "remove_duplicates" is set then the result may contain duplicates
11555 * (even if the inputs do not) and so we try and remove the obvious
11558 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11559 __isl_take isl_map
*map2
,
11560 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11561 __isl_take isl_space
*right
),
11562 __isl_give isl_basic_map
*(*basic_map_product
)(
11563 __isl_take isl_basic_map
*left
,
11564 __isl_take isl_basic_map
*right
),
11565 int remove_duplicates
)
11567 unsigned flags
= 0;
11568 struct isl_map
*result
;
11572 m
= isl_map_has_equal_params(map1
, map2
);
11576 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11577 "parameters don't match", goto error
);
11579 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11580 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11581 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11583 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11584 isl_space_copy(map2
->dim
)),
11585 map1
->n
* map2
->n
, flags
);
11588 for (i
= 0; i
< map1
->n
; ++i
)
11589 for (j
= 0; j
< map2
->n
; ++j
) {
11590 struct isl_basic_map
*part
;
11591 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11592 isl_basic_map_copy(map2
->p
[j
]));
11593 if (isl_basic_map_is_empty(part
))
11594 isl_basic_map_free(part
);
11596 result
= isl_map_add_basic_map(result
, part
);
11600 if (remove_duplicates
)
11601 result
= isl_map_remove_obvious_duplicates(result
);
11602 isl_map_free(map1
);
11603 isl_map_free(map2
);
11606 isl_map_free(map1
);
11607 isl_map_free(map2
);
11611 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11613 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11614 __isl_take isl_map
*map2
)
11616 isl_map_align_params_bin(&map1
, &map2
);
11617 return map_product(map1
, map2
, &isl_space_product
,
11618 &isl_basic_map_product
, 0);
11621 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11623 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11624 __isl_take isl_map
*map2
)
11628 prod
= isl_map_product(map1
, map2
);
11629 prod
= isl_map_flatten(prod
);
11633 /* Given two set A and B, construct its Cartesian product A x B.
11635 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11636 __isl_take isl_set
*set2
)
11638 return isl_map_range_product(set1
, set2
);
11641 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11642 __isl_take isl_set
*set2
)
11644 return isl_map_flat_range_product(set1
, set2
);
11647 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11649 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11650 __isl_take isl_map
*map2
)
11652 isl_map_align_params_bin(&map1
, &map2
);
11653 return map_product(map1
, map2
, &isl_space_domain_product
,
11654 &isl_basic_map_domain_product
, 1);
11657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11659 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11660 __isl_take isl_map
*map2
)
11662 isl_map_align_params_bin(&map1
, &map2
);
11663 return map_product(map1
, map2
, &isl_space_range_product
,
11664 &isl_basic_map_range_product
, 1);
11667 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11669 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11672 isl_size total1
, keep1
, total2
, keep2
;
11674 total1
= isl_map_dim(map
, isl_dim_in
);
11675 total2
= isl_map_dim(map
, isl_dim_out
);
11676 if (total1
< 0 || total2
< 0)
11677 return isl_map_free(map
);
11678 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11679 !isl_space_range_is_wrapping(map
->dim
))
11680 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11681 "not a product", return isl_map_free(map
));
11683 space
= isl_map_get_space(map
);
11684 space
= isl_space_factor_domain(space
);
11685 keep1
= isl_space_dim(space
, isl_dim_in
);
11686 keep2
= isl_space_dim(space
, isl_dim_out
);
11687 if (keep1
< 0 || keep2
< 0)
11688 map
= isl_map_free(map
);
11689 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11690 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11691 map
= isl_map_reset_space(map
, space
);
11696 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11698 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11701 isl_size total1
, keep1
, total2
, keep2
;
11703 total1
= isl_map_dim(map
, isl_dim_in
);
11704 total2
= isl_map_dim(map
, isl_dim_out
);
11705 if (total1
< 0 || total2
< 0)
11706 return isl_map_free(map
);
11707 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11708 !isl_space_range_is_wrapping(map
->dim
))
11709 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11710 "not a product", return isl_map_free(map
));
11712 space
= isl_map_get_space(map
);
11713 space
= isl_space_factor_range(space
);
11714 keep1
= isl_space_dim(space
, isl_dim_in
);
11715 keep2
= isl_space_dim(space
, isl_dim_out
);
11716 if (keep1
< 0 || keep2
< 0)
11717 map
= isl_map_free(map
);
11718 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11719 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11720 map
= isl_map_reset_space(map
, space
);
11725 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11727 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11730 isl_size total
, keep
;
11732 total
= isl_map_dim(map
, isl_dim_in
);
11734 return isl_map_free(map
);
11735 if (!isl_space_domain_is_wrapping(map
->dim
))
11736 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11737 "domain is not a product", return isl_map_free(map
));
11739 space
= isl_map_get_space(map
);
11740 space
= isl_space_domain_factor_domain(space
);
11741 keep
= isl_space_dim(space
, isl_dim_in
);
11743 map
= isl_map_free(map
);
11744 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11745 map
= isl_map_reset_space(map
, space
);
11750 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11752 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11755 isl_size total
, keep
;
11757 total
= isl_map_dim(map
, isl_dim_in
);
11759 return isl_map_free(map
);
11760 if (!isl_space_domain_is_wrapping(map
->dim
))
11761 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11762 "domain is not a product", return isl_map_free(map
));
11764 space
= isl_map_get_space(map
);
11765 space
= isl_space_domain_factor_range(space
);
11766 keep
= isl_space_dim(space
, isl_dim_in
);
11768 map
= isl_map_free(map
);
11769 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11770 map
= isl_map_reset_space(map
, space
);
11775 /* Given a map A -> [B -> C], extract the map A -> B.
11777 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11780 isl_size total
, keep
;
11782 total
= isl_map_dim(map
, isl_dim_out
);
11784 return isl_map_free(map
);
11785 if (!isl_space_range_is_wrapping(map
->dim
))
11786 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11787 "range is not a product", return isl_map_free(map
));
11789 space
= isl_map_get_space(map
);
11790 space
= isl_space_range_factor_domain(space
);
11791 keep
= isl_space_dim(space
, isl_dim_out
);
11793 map
= isl_map_free(map
);
11794 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11795 map
= isl_map_reset_space(map
, space
);
11800 /* Given a map A -> [B -> C], extract the map A -> C.
11802 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11805 isl_size total
, keep
;
11807 total
= isl_map_dim(map
, isl_dim_out
);
11809 return isl_map_free(map
);
11810 if (!isl_space_range_is_wrapping(map
->dim
))
11811 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11812 "range is not a product", return isl_map_free(map
));
11814 space
= isl_map_get_space(map
);
11815 space
= isl_space_range_factor_range(space
);
11816 keep
= isl_space_dim(space
, isl_dim_out
);
11818 map
= isl_map_free(map
);
11819 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11820 map
= isl_map_reset_space(map
, space
);
11825 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11827 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11828 __isl_take isl_map
*map2
)
11832 prod
= isl_map_domain_product(map1
, map2
);
11833 prod
= isl_map_flatten_domain(prod
);
11837 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11839 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11840 __isl_take isl_map
*map2
)
11844 prod
= isl_map_range_product(map1
, map2
);
11845 prod
= isl_map_flatten_range(prod
);
11849 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11852 uint32_t hash
= isl_hash_init();
11857 bmap
= isl_basic_map_copy(bmap
);
11858 bmap
= isl_basic_map_normalize(bmap
);
11859 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11862 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11863 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11865 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11866 isl_hash_hash(hash
, c_hash
);
11868 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11869 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11871 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11872 isl_hash_hash(hash
, c_hash
);
11874 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11875 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11877 if (isl_int_is_zero(bmap
->div
[i
][0]))
11879 isl_hash_byte(hash
, i
& 0xFF);
11880 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11881 isl_hash_hash(hash
, c_hash
);
11883 isl_basic_map_free(bmap
);
11887 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11889 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11892 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11899 map
= isl_map_copy(map
);
11900 map
= isl_map_normalize(map
);
11904 hash
= isl_hash_init();
11905 for (i
= 0; i
< map
->n
; ++i
) {
11906 uint32_t bmap_hash
;
11907 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11908 isl_hash_hash(hash
, bmap_hash
);
11916 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11918 return isl_map_get_hash(set_to_map(set
));
11921 /* Return the number of basic maps in the (current) representation of "map".
11923 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11925 return map
? map
->n
: isl_size_error
;
11928 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11930 return set
? set
->n
: isl_size_error
;
11933 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11934 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11939 return isl_stat_error
;
11941 for (i
= 0; i
< map
->n
; ++i
)
11942 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11943 return isl_stat_error
;
11945 return isl_stat_ok
;
11948 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11949 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11954 return isl_stat_error
;
11956 for (i
= 0; i
< set
->n
; ++i
)
11957 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11958 return isl_stat_error
;
11960 return isl_stat_ok
;
11963 /* Does "test" succeed on every basic set in "set"?
11965 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11966 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11972 return isl_bool_error
;
11974 for (i
= 0; i
< set
->n
; ++i
) {
11977 r
= test(set
->p
[i
], user
);
11982 return isl_bool_true
;
11985 /* Return a list of basic sets, the union of which is equal to "set".
11987 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11988 __isl_keep isl_set
*set
)
11991 isl_basic_set_list
*list
;
11996 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11997 for (i
= 0; i
< set
->n
; ++i
) {
11998 isl_basic_set
*bset
;
12000 bset
= isl_basic_set_copy(set
->p
[i
]);
12001 list
= isl_basic_set_list_add(list
, bset
);
12007 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
12014 bset
= isl_basic_set_cow(bset
);
12018 space
= isl_basic_set_get_space(bset
);
12019 space
= isl_space_lift(space
, bset
->n_div
);
12022 isl_space_free(bset
->dim
);
12024 bset
->extra
-= bset
->n_div
;
12027 bset
= isl_basic_set_finalize(bset
);
12031 isl_basic_set_free(bset
);
12035 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
12041 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
12046 set
= isl_set_cow(set
);
12050 n_div
= set
->p
[0]->n_div
;
12051 space
= isl_set_get_space(set
);
12052 space
= isl_space_lift(space
, n_div
);
12055 isl_space_free(set
->dim
);
12058 for (i
= 0; i
< set
->n
; ++i
) {
12059 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
12070 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
12075 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
12078 size
+= bset
->n_eq
* (1 + dim
);
12079 size
+= bset
->n_ineq
* (1 + dim
);
12080 size
+= bset
->n_div
* (2 + dim
);
12085 int isl_set_size(__isl_keep isl_set
*set
)
12093 for (i
= 0; i
< set
->n
; ++i
)
12094 size
+= isl_basic_set_size(set
->p
[i
]);
12099 /* Check if there is any lower bound (if lower == 0) and/or upper
12100 * bound (if upper == 0) on the specified dim.
12102 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12103 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
12109 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
12110 return isl_bool_error
;
12112 off
= isl_basic_map_var_offset(bmap
, type
);
12114 return isl_bool_error
;
12115 involves
= isl_basic_map_any_div_involves_vars(bmap
, off
+ pos
, 1);
12116 if (involves
< 0 || involves
)
12119 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12120 if (!isl_int_is_zero(bmap
->eq
[i
][1 + off
+ pos
]))
12121 return isl_bool_true
;
12123 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12124 int sgn
= isl_int_sgn(bmap
->ineq
[i
][1 + off
+ pos
]);
12131 return lower
&& upper
;
12134 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
12135 enum isl_dim_type type
, unsigned pos
)
12137 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
12140 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
12141 enum isl_dim_type type
, unsigned pos
)
12143 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
12146 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
12147 enum isl_dim_type type
, unsigned pos
)
12149 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
12152 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
12153 enum isl_dim_type type
, unsigned pos
)
12158 return isl_bool_error
;
12160 for (i
= 0; i
< map
->n
; ++i
) {
12162 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
12163 if (bounded
< 0 || !bounded
)
12167 return isl_bool_true
;
12170 /* Return true if the specified dim is involved in both an upper bound
12171 * and a lower bound.
12173 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
12174 enum isl_dim_type type
, unsigned pos
)
12176 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
12179 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12181 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
12182 enum isl_dim_type type
, unsigned pos
,
12183 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12184 enum isl_dim_type type
, unsigned pos
))
12189 return isl_bool_error
;
12191 for (i
= 0; i
< map
->n
; ++i
) {
12193 bounded
= fn(map
->p
[i
], type
, pos
);
12194 if (bounded
< 0 || bounded
)
12198 return isl_bool_false
;
12201 /* Return 1 if the specified dim is involved in any lower bound.
12203 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
12204 enum isl_dim_type type
, unsigned pos
)
12206 return has_any_bound(set
, type
, pos
,
12207 &isl_basic_map_dim_has_lower_bound
);
12210 /* Return 1 if the specified dim is involved in any upper bound.
12212 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
12213 enum isl_dim_type type
, unsigned pos
)
12215 return has_any_bound(set
, type
, pos
,
12216 &isl_basic_map_dim_has_upper_bound
);
12219 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12221 static isl_bool
has_bound(__isl_keep isl_map
*map
,
12222 enum isl_dim_type type
, unsigned pos
,
12223 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
12224 enum isl_dim_type type
, unsigned pos
))
12229 return isl_bool_error
;
12231 for (i
= 0; i
< map
->n
; ++i
) {
12233 bounded
= fn(map
->p
[i
], type
, pos
);
12234 if (bounded
< 0 || !bounded
)
12238 return isl_bool_true
;
12241 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12243 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
12244 enum isl_dim_type type
, unsigned pos
)
12246 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
12249 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12251 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
12252 enum isl_dim_type type
, unsigned pos
)
12254 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
12257 /* For each of the "n" variables starting at "first", determine
12258 * the sign of the variable and put the results in the first "n"
12259 * elements of the array "signs".
12261 * 1 means that the variable is non-negative
12262 * -1 means that the variable is non-positive
12263 * 0 means the variable attains both positive and negative values.
12265 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
12266 unsigned first
, unsigned n
, int *signs
)
12268 isl_vec
*bound
= NULL
;
12269 struct isl_tab
*tab
= NULL
;
12270 struct isl_tab_undo
*snap
;
12274 total
= isl_basic_set_dim(bset
, isl_dim_all
);
12275 if (total
< 0 || !signs
)
12276 return isl_stat_error
;
12278 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
12279 tab
= isl_tab_from_basic_set(bset
, 0);
12280 if (!bound
|| !tab
)
12283 isl_seq_clr(bound
->el
, bound
->size
);
12284 isl_int_set_si(bound
->el
[0], -1);
12286 snap
= isl_tab_snap(tab
);
12287 for (i
= 0; i
< n
; ++i
) {
12290 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
12291 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12293 empty
= tab
->empty
;
12294 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12295 if (isl_tab_rollback(tab
, snap
) < 0)
12303 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
12304 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
12306 empty
= tab
->empty
;
12307 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
12308 if (isl_tab_rollback(tab
, snap
) < 0)
12311 signs
[i
] = empty
? -1 : 0;
12315 isl_vec_free(bound
);
12316 return isl_stat_ok
;
12319 isl_vec_free(bound
);
12320 return isl_stat_error
;
12323 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
12324 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
12326 if (!bset
|| !signs
)
12327 return isl_stat_error
;
12328 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
12329 return isl_stat_error
;
12331 first
+= pos(bset
->dim
, type
) - 1;
12332 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
12335 /* Is it possible for the integer division "div" to depend (possibly
12336 * indirectly) on any output dimensions?
12338 * If the div is undefined, then we conservatively assume that it
12339 * may depend on them.
12340 * Otherwise, we check if it actually depends on them or on any integer
12341 * divisions that may depend on them.
12343 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
12346 isl_size n_out
, n_div
;
12347 unsigned o_out
, o_div
;
12349 if (isl_int_is_zero(bmap
->div
[div
][0]))
12350 return isl_bool_true
;
12352 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12354 return isl_bool_error
;
12355 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12357 if (isl_seq_any_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
))
12358 return isl_bool_true
;
12360 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12362 return isl_bool_error
;
12363 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12365 for (i
= 0; i
< n_div
; ++i
) {
12366 isl_bool may_involve
;
12368 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
12370 may_involve
= div_may_involve_output(bmap
, i
);
12371 if (may_involve
< 0 || may_involve
)
12372 return may_involve
;
12375 return isl_bool_false
;
12378 /* Return the first integer division of "bmap" in the range
12379 * [first, first + n[ that may depend on any output dimensions and
12380 * that has a non-zero coefficient in "c" (where the first coefficient
12381 * in "c" corresponds to integer division "first").
12383 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
12384 isl_int
*c
, int first
, int n
)
12391 for (k
= first
; k
< first
+ n
; ++k
) {
12392 isl_bool may_involve
;
12394 if (isl_int_is_zero(c
[k
]))
12396 may_involve
= div_may_involve_output(bmap
, k
);
12397 if (may_involve
< 0)
12406 /* Look for a pair of inequality constraints in "bmap" of the form
12408 * -l + i >= 0 or i >= l
12410 * n + l - i >= 0 or i <= l + n
12412 * with n < "m" and i the output dimension at position "pos".
12413 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12414 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12415 * and earlier output dimensions, as well as integer divisions that do
12416 * not involve any of the output dimensions.
12418 * Return the index of the first inequality constraint or bmap->n_ineq
12419 * if no such pair can be found.
12421 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
12422 int pos
, isl_int m
)
12427 isl_size n_div
, n_out
;
12428 unsigned o_div
, o_out
;
12431 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12432 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12433 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12434 if (total
< 0 || n_out
< 0 || n_div
< 0)
12437 ctx
= isl_basic_map_get_ctx(bmap
);
12438 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12439 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12440 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12441 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
12443 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
12444 n_out
- (pos
+ 1)))
12446 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
12449 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
12450 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
12453 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
12454 bmap
->ineq
[j
] + 1, total
))
12458 if (j
>= bmap
->n_ineq
)
12460 isl_int_add(bmap
->ineq
[i
][0],
12461 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12462 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
12463 isl_int_sub(bmap
->ineq
[i
][0],
12464 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12467 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
12473 return bmap
->n_ineq
;
12476 /* Return the index of the equality of "bmap" that defines
12477 * the output dimension "pos" in terms of earlier dimensions.
12478 * The equality may also involve integer divisions, as long
12479 * as those integer divisions are defined in terms of
12480 * parameters or input dimensions.
12481 * In this case, *div is set to the number of integer divisions and
12482 * *ineq is set to the number of inequality constraints (provided
12483 * div and ineq are not NULL).
12485 * The equality may also involve a single integer division involving
12486 * the output dimensions (typically only output dimension "pos") as
12487 * long as the coefficient of output dimension "pos" is 1 or -1 and
12488 * there is a pair of constraints i >= l and i <= l + n, with i referring
12489 * to output dimension "pos", l an expression involving only earlier
12490 * dimensions and n smaller than the coefficient of the integer division
12491 * in the equality. In this case, the output dimension can be defined
12492 * in terms of a modulo expression that does not involve the integer division.
12493 * *div is then set to this single integer division and
12494 * *ineq is set to the index of constraint i >= l.
12496 * Return bmap->n_eq if there is no such equality.
12497 * Return -1 on error.
12499 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
12500 int pos
, int *div
, int *ineq
)
12503 isl_size n_div
, n_out
;
12504 unsigned o_div
, o_out
;
12506 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12507 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12508 if (n_out
< 0 || n_div
< 0)
12511 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12512 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12515 *ineq
= bmap
->n_ineq
;
12518 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12519 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12521 if (isl_seq_any_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12522 n_out
- (pos
+ 1)))
12524 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12528 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12529 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12531 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12532 k
+ 1, n_div
- (k
+1)) < n_div
)
12534 l
= find_modulo_constraint_pair(bmap
, pos
,
12535 bmap
->eq
[j
][o_div
+ k
]);
12538 if (l
>= bmap
->n_ineq
)
12550 /* Check if the given basic map is obviously single-valued.
12551 * In particular, for each output dimension, check that there is
12552 * an equality that defines the output dimension in terms of
12553 * earlier dimensions.
12555 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12560 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12562 return isl_bool_error
;
12564 for (i
= 0; i
< n_out
; ++i
) {
12567 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12570 return isl_bool_error
;
12571 if (eq
>= bmap
->n_eq
)
12572 return isl_bool_false
;
12575 return isl_bool_true
;
12578 /* Check if the given basic map is single-valued.
12579 * We simply compute
12583 * and check if the result is a subset of the identity mapping.
12585 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12588 isl_basic_map
*test
;
12592 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12596 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12597 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12599 space
= isl_basic_map_get_space(bmap
);
12600 space
= isl_space_map_from_set(isl_space_range(space
));
12601 id
= isl_basic_map_identity(space
);
12603 sv
= isl_basic_map_is_subset(test
, id
);
12605 isl_basic_map_free(test
);
12606 isl_basic_map_free(id
);
12611 /* Check if the given map is obviously single-valued.
12613 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12616 return isl_bool_error
;
12618 return isl_bool_true
;
12620 return isl_bool_false
;
12622 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12625 /* Check if the given map is single-valued.
12626 * We simply compute
12630 * and check if the result is a subset of the identity mapping.
12632 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12639 sv
= isl_map_plain_is_single_valued(map
);
12643 test
= isl_map_reverse(isl_map_copy(map
));
12644 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12646 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12647 id
= isl_map_identity(space
);
12649 sv
= isl_map_is_subset(test
, id
);
12651 isl_map_free(test
);
12657 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12661 map
= isl_map_copy(map
);
12662 map
= isl_map_reverse(map
);
12663 in
= isl_map_is_single_valued(map
);
12669 /* Check if the given map is obviously injective.
12671 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12675 map
= isl_map_copy(map
);
12676 map
= isl_map_reverse(map
);
12677 in
= isl_map_plain_is_single_valued(map
);
12683 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12687 sv
= isl_map_is_single_valued(map
);
12691 return isl_map_is_injective(map
);
12694 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12696 return isl_map_is_single_valued(set_to_map(set
));
12699 /* Does "map" only map elements to themselves?
12701 * If the domain and range spaces are different, then "map"
12702 * is considered not to be an identity relation, even if it is empty.
12703 * Otherwise, construct the maximal identity relation and
12704 * check whether "map" is a subset of this relation.
12706 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12709 isl_bool equal
, is_identity
;
12711 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12712 if (equal
< 0 || !equal
)
12715 id
= isl_map_identity(isl_map_get_space(map
));
12716 is_identity
= isl_map_is_subset(map
, id
);
12719 return is_identity
;
12722 int isl_map_is_translation(__isl_keep isl_map
*map
)
12727 delta
= isl_map_deltas(isl_map_copy(map
));
12728 ok
= isl_set_is_singleton(delta
);
12729 isl_set_free(delta
);
12734 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12736 if (isl_seq_any_non_zero(p
, pos
))
12738 if (isl_seq_any_non_zero(p
+ pos
+ 1, len
- pos
- 1))
12743 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12746 isl_size nvar
, ovar
, n_div
;
12748 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12750 return isl_bool_error
;
12752 return isl_bool_false
;
12754 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12755 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12756 if (nvar
< 0 || ovar
< 0)
12757 return isl_bool_error
;
12758 for (j
= 0; j
< nvar
; ++j
) {
12759 int lower
= 0, upper
= 0;
12760 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12761 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12763 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12764 return isl_bool_false
;
12767 if (i
< bset
->n_eq
)
12769 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12770 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12772 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12773 return isl_bool_false
;
12774 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12779 if (!lower
|| !upper
)
12780 return isl_bool_false
;
12783 return isl_bool_true
;
12786 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12789 return isl_bool_error
;
12791 return isl_bool_false
;
12793 return isl_basic_set_is_box(set
->p
[0]);
12796 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12799 return isl_bool_error
;
12801 return isl_space_is_wrapping(bset
->dim
);
12804 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12807 return isl_bool_error
;
12809 return isl_space_is_wrapping(set
->dim
);
12812 /* Modify the space of "map" through a call to "change".
12813 * If "can_change" is set (not NULL), then first call it to check
12814 * if the modification is allowed, printing the error message "cannot_change"
12817 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12818 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12819 const char *cannot_change
,
12820 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12828 ok
= can_change
? can_change(map
) : isl_bool_true
;
12830 return isl_map_free(map
);
12832 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12833 return isl_map_free(map
));
12835 space
= change(isl_map_get_space(map
));
12836 map
= isl_map_reset_space(map
, space
);
12841 /* Is the domain of "map" a wrapped relation?
12843 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12846 return isl_bool_error
;
12848 return isl_space_domain_is_wrapping(map
->dim
);
12851 /* Does "map" have a wrapped relation in both domain and range?
12853 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12855 return isl_space_is_product(isl_map_peek_space(map
));
12858 /* Is the range of "map" a wrapped relation?
12860 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12863 return isl_bool_error
;
12865 return isl_space_range_is_wrapping(map
->dim
);
12868 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12872 space
= isl_basic_map_take_space(bmap
);
12873 space
= isl_space_wrap(space
);
12874 bmap
= isl_basic_map_restore_space(bmap
, space
);
12876 bmap
= isl_basic_map_finalize(bmap
);
12878 return bset_from_bmap(bmap
);
12881 /* Given a map A -> B, return the set (A -> B).
12883 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12885 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12888 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12890 bset
= isl_basic_set_cow(bset
);
12894 bset
->dim
= isl_space_unwrap(bset
->dim
);
12898 bset
= isl_basic_set_finalize(bset
);
12900 return bset_to_bmap(bset
);
12902 isl_basic_set_free(bset
);
12906 /* Given a set (A -> B), return the map A -> B.
12907 * Error out if "set" is not of the form (A -> B).
12909 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12911 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12912 "not a wrapping set", &isl_space_unwrap
);
12915 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12916 enum isl_dim_type type
)
12920 space
= isl_basic_map_take_space(bmap
);
12921 space
= isl_space_reset(space
, type
);
12922 bmap
= isl_basic_map_restore_space(bmap
, space
);
12924 bmap
= isl_basic_map_mark_final(bmap
);
12929 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12930 enum isl_dim_type type
)
12938 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12941 map
= isl_map_cow(map
);
12945 for (i
= 0; i
< map
->n
; ++i
) {
12946 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12951 space
= isl_map_take_space(map
);
12952 space
= isl_space_reset(space
, type
);
12953 map
= isl_map_restore_space(map
, space
);
12961 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12965 space
= isl_basic_map_take_space(bmap
);
12966 space
= isl_space_flatten(space
);
12967 bmap
= isl_basic_map_restore_space(bmap
, space
);
12969 bmap
= isl_basic_map_mark_final(bmap
);
12974 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12976 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12979 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12980 __isl_take isl_basic_map
*bmap
)
12984 space
= isl_basic_map_take_space(bmap
);
12985 space
= isl_space_flatten_domain(space
);
12986 bmap
= isl_basic_map_restore_space(bmap
, space
);
12988 bmap
= isl_basic_map_mark_final(bmap
);
12993 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12994 __isl_take isl_basic_map
*bmap
)
12998 space
= isl_basic_map_take_space(bmap
);
12999 space
= isl_space_flatten_range(space
);
13000 bmap
= isl_basic_map_restore_space(bmap
, space
);
13002 bmap
= isl_basic_map_mark_final(bmap
);
13007 /* Remove any internal structure from the spaces of domain and range of "map".
13009 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
13014 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
13017 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
13020 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
13022 return set_from_map(isl_map_flatten(set_to_map(set
)));
13025 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
13027 isl_space
*space
, *flat_space
;
13030 space
= isl_set_get_space(set
);
13031 flat_space
= isl_space_flatten(isl_space_copy(space
));
13032 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
13034 map
= isl_map_intersect_domain(map
, set
);
13039 /* Remove any internal structure from the space of the domain of "map".
13041 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
13046 if (!map
->dim
->nested
[0])
13049 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
13052 /* Remove any internal structure from the space of the range of "map".
13054 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
13059 if (!map
->dim
->nested
[1])
13062 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
13065 /* Reorder the dimensions of "bmap" according to the given dim_map
13066 * and set the dimension specification to "space" and
13067 * perform Gaussian elimination on the result.
13069 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
13070 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
13072 isl_basic_map
*res
;
13076 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
13077 if (n_div
< 0 || !space
|| !dim_map
)
13080 flags
= bmap
->flags
;
13081 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
13082 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
13083 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13084 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
13085 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
13087 res
->flags
= flags
;
13088 res
= isl_basic_map_gauss(res
, NULL
);
13089 res
= isl_basic_map_finalize(res
);
13092 isl_dim_map_free(dim_map
);
13093 isl_basic_map_free(bmap
);
13094 isl_space_free(space
);
13098 /* Reorder the dimensions of "map" according to given reordering.
13100 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
13101 __isl_take isl_reordering
*r
)
13104 struct isl_dim_map
*dim_map
;
13106 map
= isl_map_cow(map
);
13107 dim_map
= isl_dim_map_from_reordering(r
);
13108 if (!map
|| !r
|| !dim_map
)
13111 for (i
= 0; i
< map
->n
; ++i
) {
13112 struct isl_dim_map
*dim_map_i
;
13115 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
13117 space
= isl_reordering_get_space(r
);
13118 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
13124 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
13125 map
= isl_map_unmark_normalized(map
);
13127 isl_reordering_free(r
);
13128 isl_dim_map_free(dim_map
);
13131 isl_dim_map_free(dim_map
);
13133 isl_reordering_free(r
);
13137 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
13138 __isl_take isl_reordering
*r
)
13140 return set_from_map(isl_map_realign(set_to_map(set
), r
));
13143 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
13144 __isl_take isl_space
*model
)
13149 if (!map
|| !model
)
13152 ctx
= isl_space_get_ctx(model
);
13153 if (!isl_space_has_named_params(model
))
13154 isl_die(ctx
, isl_error_invalid
,
13155 "model has unnamed parameters", goto error
);
13156 if (isl_map_check_named_params(map
) < 0)
13158 aligned
= isl_map_space_has_equal_params(map
, model
);
13163 isl_reordering
*exp
;
13165 space
= isl_map_peek_space(map
);
13166 exp
= isl_parameter_alignment_reordering(space
, model
);
13167 map
= isl_map_realign(map
, exp
);
13170 isl_space_free(model
);
13173 isl_space_free(model
);
13178 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
13179 __isl_take isl_space
*model
)
13181 return isl_map_align_params(set
, model
);
13184 /* Align the parameters of "bmap" to those of "model", introducing
13185 * additional parameters if needed.
13187 __isl_give isl_basic_map
*isl_basic_map_align_params(
13188 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
13191 isl_bool equal_params
;
13192 isl_space
*bmap_space
;
13194 if (!bmap
|| !model
)
13197 ctx
= isl_space_get_ctx(model
);
13198 if (!isl_space_has_named_params(model
))
13199 isl_die(ctx
, isl_error_invalid
,
13200 "model has unnamed parameters", goto error
);
13201 if (isl_basic_map_check_named_params(bmap
) < 0)
13203 bmap_space
= isl_basic_map_peek_space(bmap
);
13204 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
13205 if (equal_params
< 0)
13207 if (!equal_params
) {
13208 isl_reordering
*exp
;
13209 struct isl_dim_map
*dim_map
;
13211 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
13212 dim_map
= isl_dim_map_from_reordering(exp
);
13213 bmap
= isl_basic_map_realign(bmap
,
13214 isl_reordering_get_space(exp
),
13215 isl_dim_map_extend(dim_map
, bmap
));
13216 isl_reordering_free(exp
);
13217 isl_dim_map_free(dim_map
);
13220 isl_space_free(model
);
13223 isl_space_free(model
);
13224 isl_basic_map_free(bmap
);
13228 /* Do "bset" and "space" have the same parameters?
13230 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
13231 __isl_keep isl_space
*space
)
13233 isl_space
*bset_space
;
13235 bset_space
= isl_basic_set_peek_space(bset
);
13236 return isl_space_has_equal_params(bset_space
, space
);
13239 /* Do "map" and "space" have the same parameters?
13241 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
13242 __isl_keep isl_space
*space
)
13244 isl_space
*map_space
;
13246 map_space
= isl_map_peek_space(map
);
13247 return isl_space_has_equal_params(map_space
, space
);
13250 /* Do "set" and "space" have the same parameters?
13252 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
13253 __isl_keep isl_space
*space
)
13255 return isl_map_space_has_equal_params(set_to_map(set
), space
);
13258 /* Align the parameters of "bset" to those of "model", introducing
13259 * additional parameters if needed.
13261 __isl_give isl_basic_set
*isl_basic_set_align_params(
13262 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
13264 return isl_basic_map_align_params(bset
, model
);
13268 #define TYPE isl_map
13269 #define isl_map_drop_dims isl_map_drop
13270 #include "isl_drop_unused_params_templ.c"
13272 /* Drop all parameters not referenced by "set".
13274 __isl_give isl_set
*isl_set_drop_unused_params(
13275 __isl_take isl_set
*set
)
13277 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
13281 #define TYPE isl_basic_map
13282 #define isl_basic_map_drop_dims isl_basic_map_drop
13283 #include "isl_drop_unused_params_templ.c"
13285 /* Drop all parameters not referenced by "bset".
13287 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
13288 __isl_take isl_basic_set
*bset
)
13290 return bset_from_bmap(isl_basic_map_drop_unused_params(
13291 bset_to_bmap(bset
)));
13294 /* Given a tuple of identifiers "tuple" in a space that corresponds
13295 * to that of "set", if any of those identifiers appear as parameters
13296 * in "set", then equate those parameters with the corresponding
13297 * set dimensions and project out the parameters.
13298 * The result therefore has no such parameters.
13300 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
13301 __isl_keep isl_multi_id
*tuple
)
13305 isl_space
*set_space
, *tuple_space
;
13307 set_space
= isl_set_peek_space(set
);
13308 tuple_space
= isl_multi_id_peek_space(tuple
);
13309 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
13310 return isl_set_free(set
);
13311 n
= isl_multi_id_size(tuple
);
13313 return isl_set_free(set
);
13314 for (i
= 0; i
< n
; ++i
) {
13318 id
= isl_multi_id_get_at(tuple
, i
);
13320 return isl_set_free(set
);
13321 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
13325 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
13326 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
13331 /* Bind the set dimensions of "set" to parameters with identifiers
13332 * specified by "tuple", living in the same space as "set".
13334 * If no parameters with these identifiers appear in "set" already,
13335 * then the set dimensions are simply reinterpreted as parameters.
13336 * Otherwise, the parameters are first equated to the corresponding
13339 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
13340 __isl_take isl_multi_id
*tuple
)
13344 set
= equate_params(set
, tuple
);
13345 space
= isl_set_get_space(set
);
13346 space
= isl_space_bind_set(space
, tuple
);
13347 isl_multi_id_free(tuple
);
13348 set
= isl_set_reset_space(set
, space
);
13353 /* Given a tuple of identifiers "tuple" in a space that corresponds
13354 * to the domain of "map", if any of those identifiers appear as parameters
13355 * in "map", then equate those parameters with the corresponding
13356 * input dimensions and project out the parameters.
13357 * The result therefore has no such parameters.
13359 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
13360 __isl_keep isl_multi_id
*tuple
)
13364 isl_space
*map_space
, *tuple_space
;
13366 map_space
= isl_map_peek_space(map
);
13367 tuple_space
= isl_multi_id_peek_space(tuple
);
13368 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
13369 return isl_map_free(map
);
13370 n
= isl_multi_id_size(tuple
);
13372 return isl_map_free(map
);
13373 for (i
= 0; i
< n
; ++i
) {
13377 id
= isl_multi_id_get_at(tuple
, i
);
13379 return isl_map_free(map
);
13380 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
13384 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
13385 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
13390 /* Bind the input dimensions of "map" to parameters with identifiers
13391 * specified by "tuple", living in the domain space of "map".
13393 * If no parameters with these identifiers appear in "map" already,
13394 * then the input dimensions are simply reinterpreted as parameters.
13395 * Otherwise, the parameters are first equated to the corresponding
13396 * input dimensions.
13398 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
13399 __isl_take isl_multi_id
*tuple
)
13404 map
= map_equate_params(map
, tuple
);
13405 space
= isl_map_get_space(map
);
13406 space
= isl_space_bind_map_domain(space
, tuple
);
13407 isl_multi_id_free(tuple
);
13408 set
= set_from_map(isl_map_reset_space(map
, space
));
13413 /* Bind the output dimensions of "map" to parameters with identifiers
13414 * specified by "tuple", living in the range space of "map".
13416 * Since binding is more easily implemented on the domain,
13417 * bind the input dimensions of the inverse of "map".
13419 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
13420 __isl_take isl_multi_id
*tuple
)
13422 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
13425 /* Insert a domain corresponding to "tuple"
13426 * into the nullary or unary relation "set".
13427 * The result has an extra initial tuple and is therefore
13428 * either a unary or binary relation.
13429 * Any parameters with identifiers in "tuple" are reinterpreted
13430 * as the corresponding domain dimensions.
13432 static __isl_give isl_map
*unbind_params_insert_domain(
13433 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
13438 space
= isl_set_peek_space(set
);
13439 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
13440 isl_multi_id_free(tuple
);
13442 return isl_map_realign(set_to_map(set
), r
);
13445 /* Construct a set with "tuple" as domain from the parameter domain "set".
13446 * Any parameters with identifiers in "tuple" are reinterpreted
13447 * as the corresponding set dimensions.
13449 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
13450 __isl_take isl_multi_id
*tuple
)
13452 isl_bool is_params
;
13454 is_params
= isl_set_is_params(set
);
13456 set
= isl_set_free(set
);
13457 else if (!is_params
)
13458 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13459 "expecting parameter domain", set
= isl_set_free(set
));
13460 return set_from_map(unbind_params_insert_domain(set
, tuple
));
13463 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13465 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
13467 isl_bool is_params
;
13469 is_params
= isl_set_is_params(set
);
13471 return isl_stat_error
;
13472 else if (is_params
)
13473 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13474 "expecting proper set", return isl_stat_error
);
13476 return isl_stat_ok
;
13479 /* Construct a map with "domain" as domain and "set" as range.
13480 * Any parameters with identifiers in "domain" are reinterpreted
13481 * as the corresponding domain dimensions.
13483 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
13484 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
13486 if (isl_set_check_is_set(set
) < 0)
13487 set
= isl_set_free(set
);
13488 return unbind_params_insert_domain(set
, domain
);
13491 /* Construct a map with "domain" as domain and "set" as range.
13493 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
13494 __isl_take isl_space
*domain
)
13500 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
13501 domain
= isl_space_free(domain
);
13502 dim
= isl_space_dim(domain
, isl_dim_set
);
13504 domain
= isl_space_free(domain
);
13505 space
= isl_set_get_space(set
);
13506 domain
= isl_space_replace_params(domain
, space
);
13507 space
= isl_space_map_from_domain_and_range(domain
, space
);
13509 map
= isl_map_from_range(set
);
13510 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
13511 map
= isl_map_reset_space(map
, space
);
13516 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13517 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13518 enum isl_dim_type c2
, enum isl_dim_type c3
,
13519 enum isl_dim_type c4
, enum isl_dim_type c5
)
13521 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13522 struct isl_mat
*mat
;
13527 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13530 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13533 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13534 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13535 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13536 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13538 return isl_mat_free(mat
);
13539 for (k
= 0; k
< dim
; ++k
) {
13540 isl_int_set(mat
->row
[i
][pos
],
13541 bmap
->eq
[i
][off
+ k
]);
13549 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13550 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13551 enum isl_dim_type c2
, enum isl_dim_type c3
,
13552 enum isl_dim_type c4
, enum isl_dim_type c5
)
13554 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13555 struct isl_mat
*mat
;
13560 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13563 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13566 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13567 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13568 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13569 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13571 return isl_mat_free(mat
);
13572 for (k
= 0; k
< dim
; ++k
) {
13573 isl_int_set(mat
->row
[i
][pos
],
13574 bmap
->ineq
[i
][off
+ k
]);
13582 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13583 __isl_take isl_space
*space
,
13584 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13585 enum isl_dim_type c2
, enum isl_dim_type c3
,
13586 enum isl_dim_type c4
, enum isl_dim_type c5
)
13588 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13589 isl_basic_map
*bmap
= NULL
;
13596 dim
= isl_space_dim(space
, isl_dim_all
);
13597 if (dim
< 0 || !eq
|| !ineq
)
13600 if (eq
->n_col
!= ineq
->n_col
)
13601 isl_die(space
->ctx
, isl_error_invalid
,
13602 "equalities and inequalities matrices should have "
13603 "same number of columns", goto error
);
13607 if (eq
->n_col
< total
)
13608 isl_die(space
->ctx
, isl_error_invalid
,
13609 "number of columns too small", goto error
);
13611 extra
= eq
->n_col
- total
;
13613 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13614 eq
->n_row
, ineq
->n_row
);
13617 for (i
= 0; i
< extra
; ++i
) {
13618 k
= isl_basic_map_alloc_div(bmap
);
13621 isl_int_set_si(bmap
->div
[k
][0], 0);
13623 for (i
= 0; i
< eq
->n_row
; ++i
) {
13624 l
= isl_basic_map_alloc_equality(bmap
);
13627 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13628 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13629 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13632 for (k
= 0; k
< dim
; ++k
) {
13633 isl_int_set(bmap
->eq
[l
][off
+ k
],
13639 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13640 l
= isl_basic_map_alloc_inequality(bmap
);
13643 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13644 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13645 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13648 for (k
= 0; k
< dim
; ++k
) {
13649 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13650 ineq
->row
[i
][pos
]);
13656 isl_space_free(space
);
13658 isl_mat_free(ineq
);
13660 bmap
= isl_basic_map_simplify(bmap
);
13661 return isl_basic_map_finalize(bmap
);
13663 isl_space_free(space
);
13665 isl_mat_free(ineq
);
13666 isl_basic_map_free(bmap
);
13670 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13671 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13672 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13674 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13675 c1
, c2
, c3
, c4
, isl_dim_in
);
13678 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13679 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13680 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13682 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13683 c1
, c2
, c3
, c4
, isl_dim_in
);
13686 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13687 __isl_take isl_space
*space
,
13688 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13689 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13691 isl_basic_map
*bmap
;
13692 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13693 c1
, c2
, c3
, c4
, isl_dim_in
);
13694 return bset_from_bmap(bmap
);
13697 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13700 return isl_bool_error
;
13702 return isl_space_can_zip(bmap
->dim
);
13705 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13708 return isl_bool_error
;
13710 return isl_space_can_zip(map
->dim
);
13713 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13714 * (A -> C) -> (B -> D).
13716 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13726 if (!isl_basic_map_can_zip(bmap
))
13727 isl_die(bmap
->ctx
, isl_error_invalid
,
13728 "basic map cannot be zipped", goto error
);
13729 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13730 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13731 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13732 if (n_in
< 0 || n1
< 0 || n2
< 0)
13733 return isl_basic_map_free(bmap
);
13734 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13735 bmap
= isl_basic_map_cow(bmap
);
13736 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13739 bmap
->dim
= isl_space_zip(bmap
->dim
);
13742 bmap
= isl_basic_map_mark_final(bmap
);
13745 isl_basic_map_free(bmap
);
13749 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13750 * (A -> C) -> (B -> D).
13752 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13757 if (!isl_map_can_zip(map
))
13758 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13761 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13767 /* Can we apply isl_basic_map_curry to "bmap"?
13768 * That is, does it have a nested relation in its domain?
13770 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13773 return isl_bool_error
;
13775 return isl_space_can_curry(bmap
->dim
);
13778 /* Can we apply isl_map_curry to "map"?
13779 * That is, does it have a nested relation in its domain?
13781 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13784 return isl_bool_error
;
13786 return isl_space_can_curry(map
->dim
);
13789 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13792 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13798 if (!isl_basic_map_can_curry(bmap
))
13799 isl_die(bmap
->ctx
, isl_error_invalid
,
13800 "basic map cannot be curried", goto error
);
13801 bmap
= isl_basic_map_cow(bmap
);
13804 bmap
->dim
= isl_space_curry(bmap
->dim
);
13807 bmap
= isl_basic_map_mark_final(bmap
);
13810 isl_basic_map_free(bmap
);
13814 /* Given a map (A -> B) -> C, return the corresponding map
13817 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13819 return isl_map_change_space(map
, &isl_map_can_curry
,
13820 "map cannot be curried", &isl_space_curry
);
13823 /* Can isl_map_range_curry be applied to "map"?
13824 * That is, does it have a nested relation in its range,
13825 * the domain of which is itself a nested relation?
13827 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13830 return isl_bool_error
;
13832 return isl_space_can_range_curry(map
->dim
);
13835 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13836 * A -> (B -> (C -> D)).
13838 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13840 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13841 "map range cannot be curried",
13842 &isl_space_range_curry
);
13845 /* Can we apply isl_basic_map_uncurry to "bmap"?
13846 * That is, does it have a nested relation in its domain?
13848 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13851 return isl_bool_error
;
13853 return isl_space_can_uncurry(bmap
->dim
);
13856 /* Can we apply isl_map_uncurry to "map"?
13857 * That is, does it have a nested relation in its domain?
13859 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13862 return isl_bool_error
;
13864 return isl_space_can_uncurry(map
->dim
);
13867 /* Given a basic map A -> (B -> C), return the corresponding basic map
13870 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13876 if (!isl_basic_map_can_uncurry(bmap
))
13877 isl_die(bmap
->ctx
, isl_error_invalid
,
13878 "basic map cannot be uncurried",
13879 return isl_basic_map_free(bmap
));
13880 bmap
= isl_basic_map_cow(bmap
);
13883 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13885 return isl_basic_map_free(bmap
);
13886 bmap
= isl_basic_map_mark_final(bmap
);
13890 /* Given a map A -> (B -> C), return the corresponding map
13893 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13895 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13896 "map cannot be uncurried", &isl_space_uncurry
);
13899 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13900 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13902 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13905 /* Construct a basic map where the given dimensions are equal to each other.
13907 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13908 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13910 isl_basic_map
*bmap
= NULL
;
13914 total
= isl_space_dim(space
, isl_dim_all
);
13916 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13917 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13920 if (type1
== type2
&& pos1
== pos2
)
13921 return isl_basic_map_universe(space
);
13923 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13924 i
= isl_basic_map_alloc_equality(bmap
);
13927 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13928 pos1
+= isl_basic_map_offset(bmap
, type1
);
13929 pos2
+= isl_basic_map_offset(bmap
, type2
);
13930 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13931 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13932 bmap
= isl_basic_map_finalize(bmap
);
13933 isl_space_free(space
);
13936 isl_space_free(space
);
13937 isl_basic_map_free(bmap
);
13941 /* Add a constraint imposing that the given two dimensions are equal.
13943 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13944 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13948 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13950 bmap
= isl_basic_map_intersect(bmap
, eq
);
13955 /* Add a constraint imposing that the given two dimensions are equal.
13957 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13958 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13960 isl_basic_map
*bmap
;
13962 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13964 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13969 /* Add a constraint imposing that the given two dimensions have opposite values.
13971 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13972 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13974 isl_basic_map
*bmap
= NULL
;
13978 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13979 return isl_map_free(map
);
13980 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13981 return isl_map_free(map
);
13983 total
= isl_map_dim(map
, isl_dim_all
);
13985 return isl_map_free(map
);
13986 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13987 i
= isl_basic_map_alloc_equality(bmap
);
13990 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13991 pos1
+= isl_basic_map_offset(bmap
, type1
);
13992 pos2
+= isl_basic_map_offset(bmap
, type2
);
13993 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13994 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13995 bmap
= isl_basic_map_finalize(bmap
);
13997 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
14001 isl_basic_map_free(bmap
);
14006 /* Construct a constraint imposing that the value of the first dimension is
14007 * greater than or equal to that of the second.
14009 static __isl_give isl_constraint
*constraint_order_ge(
14010 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
14011 enum isl_dim_type type2
, int pos2
)
14015 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14016 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14017 space
= isl_space_free(space
);
14021 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
14023 if (type1
== type2
&& pos1
== pos2
)
14026 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
14027 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
14032 /* Add a constraint imposing that the value of the first dimension is
14033 * greater than or equal to that of the second.
14035 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
14036 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14041 if (type1
== type2
&& pos1
== pos2
)
14043 space
= isl_basic_map_get_space(bmap
);
14044 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14045 bmap
= isl_basic_map_add_constraint(bmap
, c
);
14050 /* Add a constraint imposing that the value of the first dimension is
14051 * greater than or equal to that of the second.
14053 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
14054 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14059 if (type1
== type2
&& pos1
== pos2
)
14061 space
= isl_map_get_space(map
);
14062 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
14063 map
= isl_map_add_constraint(map
, c
);
14068 /* Add a constraint imposing that the value of the first dimension is
14069 * less than or equal to that of the second.
14071 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
14072 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14074 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
14077 /* Construct a basic map where the value of the first dimension is
14078 * greater than that of the second.
14080 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
14081 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14083 isl_basic_map
*bmap
= NULL
;
14087 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
14088 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
14091 if (type1
== type2
&& pos1
== pos2
)
14092 return isl_basic_map_empty(space
);
14094 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
14095 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14096 i
= isl_basic_map_alloc_inequality(bmap
);
14097 if (total
< 0 || i
< 0)
14098 return isl_basic_map_free(bmap
);
14099 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
14100 pos1
+= isl_basic_map_offset(bmap
, type1
);
14101 pos2
+= isl_basic_map_offset(bmap
, type2
);
14102 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
14103 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
14104 isl_int_set_si(bmap
->ineq
[i
][0], -1);
14105 bmap
= isl_basic_map_finalize(bmap
);
14109 isl_space_free(space
);
14110 isl_basic_map_free(bmap
);
14114 /* Add a constraint imposing that the value of the first dimension is
14115 * greater than that of the second.
14117 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
14118 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14122 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
14124 bmap
= isl_basic_map_intersect(bmap
, gt
);
14129 /* Add a constraint imposing that the value of the first dimension is
14130 * greater than that of the second.
14132 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
14133 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14135 isl_basic_map
*bmap
;
14137 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
14139 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
14144 /* Add a constraint imposing that the value of the first dimension is
14145 * smaller than that of the second.
14147 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
14148 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
14150 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
14153 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
14157 isl_local_space
*ls
;
14162 if (!isl_basic_map_divs_known(bmap
))
14163 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14164 "some divs are unknown", return NULL
);
14166 ls
= isl_basic_map_get_local_space(bmap
);
14167 div
= isl_local_space_get_div(ls
, pos
);
14168 isl_local_space_free(ls
);
14173 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
14176 return isl_basic_map_get_div(bset
, pos
);
14179 /* Plug in "subs" for set dimension "pos" of "set".
14181 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
14182 unsigned pos
, __isl_keep isl_aff
*subs
)
14186 if (set
&& isl_set_plain_is_empty(set
))
14189 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
14190 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
14191 return isl_set_preimage_multi_aff(set
, ma
);
14194 /* Check if the range of "ma" is compatible with the domain or range
14195 * (depending on "type") of "bmap".
14197 static isl_stat
check_basic_map_compatible_range_multi_aff(
14198 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
14199 __isl_keep isl_multi_aff
*ma
)
14202 isl_space
*ma_space
;
14204 ma_space
= isl_multi_aff_get_space(ma
);
14206 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
14210 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14211 "parameters don't match", goto error
);
14212 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
14216 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
14217 "spaces don't match", goto error
);
14219 isl_space_free(ma_space
);
14220 return isl_stat_ok
;
14222 isl_space_free(ma_space
);
14223 return isl_stat_error
;
14226 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14227 * coefficients before the transformed range of dimensions,
14228 * the "n_after" coefficients after the transformed range of dimensions
14229 * and the coefficients of the other divs in "bmap".
14231 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
14232 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
14237 isl_local_space
*ls
;
14242 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
14243 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
14244 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
14245 if (n_param
< 0 || n_set
< 0)
14246 return isl_basic_map_free(bmap
);
14248 for (i
= 0; i
< n_div
; ++i
) {
14249 int o_bmap
= 0, o_ls
= 0;
14251 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
14252 o_bmap
+= 1 + 1 + n_param
;
14253 o_ls
+= 1 + 1 + n_param
;
14254 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
14255 o_bmap
+= n_before
;
14256 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14257 ls
->div
->row
[i
] + o_ls
, n_set
);
14260 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
14262 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
14263 ls
->div
->row
[i
] + o_ls
, n_div
);
14266 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
14267 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
14272 isl_local_space_free(ls
);
14275 isl_local_space_free(ls
);
14276 return isl_basic_map_free(bmap
);
14279 /* How many stride constraints does "ma" enforce?
14280 * That is, how many of the affine expressions have a denominator
14281 * different from one?
14283 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
14288 for (i
= 0; i
< ma
->n
; ++i
)
14289 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14295 /* For each affine expression in ma of the form
14297 * x_i = (f_i y + h_i)/m_i
14299 * with m_i different from one, add a constraint to "bmap"
14302 * f_i y + h_i = m_i alpha_i
14304 * with alpha_i an additional existentially quantified variable.
14306 * The input variables of "ma" correspond to a subset of the variables
14307 * of "bmap". There are "n_before" variables in "bmap" before this
14308 * subset and "n_after" variables after this subset.
14309 * The integer divisions of the affine expressions in "ma" are assumed
14310 * to have been aligned. There are "n_div_ma" of them and
14311 * they appear first in "bmap", straight after the "n_after" variables.
14313 static __isl_give isl_basic_map
*add_ma_strides(
14314 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
14315 int n_before
, int n_after
, int n_div_ma
)
14323 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14324 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
14325 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
14326 if (total
< 0 || n_param
< 0 || n_in
< 0)
14327 return isl_basic_map_free(bmap
);
14328 for (i
= 0; i
< ma
->n
; ++i
) {
14329 int o_bmap
= 0, o_ma
= 1;
14331 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
14333 div
= isl_basic_map_alloc_div(bmap
);
14334 k
= isl_basic_map_alloc_equality(bmap
);
14335 if (div
< 0 || k
< 0)
14337 isl_int_set_si(bmap
->div
[div
][0], 0);
14338 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14339 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
14340 o_bmap
+= 1 + n_param
;
14341 o_ma
+= 1 + n_param
;
14342 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
14343 o_bmap
+= n_before
;
14344 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14345 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
14348 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
14350 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
14351 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
14352 o_bmap
+= n_div_ma
;
14354 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
14355 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
14361 isl_basic_map_free(bmap
);
14365 /* Replace the domain or range space (depending on "type) of "space" by "set".
14367 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
14368 enum isl_dim_type type
, __isl_take isl_space
*set
)
14370 if (type
== isl_dim_in
) {
14371 space
= isl_space_range(space
);
14372 space
= isl_space_map_from_domain_and_range(set
, space
);
14374 space
= isl_space_domain(space
);
14375 space
= isl_space_map_from_domain_and_range(space
, set
);
14381 /* Compute the preimage of the domain or range (depending on "type")
14382 * of "bmap" under the function represented by "ma".
14383 * In other words, plug in "ma" in the domain or range of "bmap".
14384 * The result is a basic map that lives in the same space as "bmap"
14385 * except that the domain or range has been replaced by
14386 * the domain space of "ma".
14388 * If bmap is represented by
14390 * A(p) + S u + B x + T v + C(divs) >= 0,
14392 * where u and x are input and output dimensions if type == isl_dim_out
14393 * while x and v are input and output dimensions if type == isl_dim_in,
14394 * and ma is represented by
14396 * x = D(p) + F(y) + G(divs')
14398 * then the result is
14400 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14402 * The divs in the input set are similarly adjusted.
14405 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14409 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14410 * B_i G(divs') + c_i(divs))/n_i)
14412 * If bmap is not a rational map and if F(y) involves any denominators
14414 * x_i = (f_i y + h_i)/m_i
14416 * then additional constraints are added to ensure that we only
14417 * map back integer points. That is we enforce
14419 * f_i y + h_i = m_i alpha_i
14421 * with alpha_i an additional existentially quantified variable.
14423 * We first copy over the divs from "ma".
14424 * Then we add the modified constraints and divs from "bmap".
14425 * Finally, we add the stride constraints, if needed.
14427 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
14428 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
14429 __isl_take isl_multi_aff
*ma
)
14433 isl_basic_map
*res
= NULL
;
14434 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
14435 isl_int f
, c1
, c2
, g
;
14444 ma
= isl_multi_aff_align_divs(ma
);
14447 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
14450 if (type
== isl_dim_in
) {
14452 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
14454 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
14457 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
14458 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
14459 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
14462 space
= isl_multi_aff_get_domain_space(ma
);
14463 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
14464 rational
= isl_basic_map_is_rational(bmap
);
14465 strides
= rational
? 0 : multi_aff_strides(ma
);
14466 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
14467 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
14469 res
= isl_basic_map_set_rational(res
);
14471 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
14472 if (isl_basic_map_alloc_div(res
) < 0)
14475 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
14479 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
14480 k
= isl_basic_map_alloc_equality(res
);
14483 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
14484 n_after
, n_div_ma
, n_div_bmap
,
14485 f
, c1
, c2
, g
, 0) < 0)
14489 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
14490 k
= isl_basic_map_alloc_inequality(res
);
14493 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
14494 n_after
, n_div_ma
, n_div_bmap
,
14495 f
, c1
, c2
, g
, 0) < 0)
14499 for (i
= 0; i
< bmap
->n_div
; ++i
) {
14500 if (isl_int_is_zero(bmap
->div
[i
][0])) {
14501 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
14504 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
14505 n_before
, n_after
, n_div_ma
, n_div_bmap
,
14506 f
, c1
, c2
, g
, 1) < 0)
14511 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14517 isl_basic_map_free(bmap
);
14518 isl_multi_aff_free(ma
);
14519 res
= isl_basic_map_simplify(res
);
14520 return isl_basic_map_finalize(res
);
14526 isl_basic_map_free(bmap
);
14527 isl_multi_aff_free(ma
);
14528 isl_basic_map_free(res
);
14532 /* Compute the preimage of "bset" under the function represented by "ma".
14533 * In other words, plug in "ma" in "bset". The result is a basic set
14534 * that lives in the domain space of "ma".
14536 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14537 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14539 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14542 /* Compute the preimage of the domain of "bmap" under the function
14543 * represented by "ma".
14544 * In other words, plug in "ma" in the domain of "bmap".
14545 * The result is a basic map that lives in the same space as "bmap"
14546 * except that the domain has been replaced by the domain space of "ma".
14548 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14549 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14551 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14554 /* Compute the preimage of the range of "bmap" under the function
14555 * represented by "ma".
14556 * In other words, plug in "ma" in the range of "bmap".
14557 * The result is a basic map that lives in the same space as "bmap"
14558 * except that the range has been replaced by the domain space of "ma".
14560 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14561 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14563 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14566 /* Check if the range of "ma" is compatible with the domain or range
14567 * (depending on "type") of "map".
14568 * Return isl_stat_error if anything is wrong.
14570 static isl_stat
check_map_compatible_range_multi_aff(
14571 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14572 __isl_keep isl_multi_aff
*ma
)
14575 isl_space
*ma_space
;
14577 ma_space
= isl_multi_aff_get_space(ma
);
14578 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14579 isl_space_free(ma_space
);
14581 return isl_stat_error
;
14583 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14584 "spaces don't match", return isl_stat_error
);
14585 return isl_stat_ok
;
14588 /* Compute the preimage of the domain or range (depending on "type")
14589 * of "map" under the function represented by "ma".
14590 * In other words, plug in "ma" in the domain or range of "map".
14591 * The result is a map that lives in the same space as "map"
14592 * except that the domain or range has been replaced by
14593 * the domain space of "ma".
14595 * The parameters are assumed to have been aligned.
14597 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14598 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14603 map
= isl_map_cow(map
);
14604 ma
= isl_multi_aff_align_divs(ma
);
14607 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14610 for (i
= 0; i
< map
->n
; ++i
) {
14611 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14612 isl_multi_aff_copy(ma
));
14617 space
= isl_multi_aff_get_domain_space(ma
);
14618 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14620 isl_space_free(isl_map_take_space(map
));
14621 map
= isl_map_restore_space(map
, space
);
14625 isl_multi_aff_free(ma
);
14627 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14628 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14631 isl_multi_aff_free(ma
);
14636 /* Compute the preimage of the domain or range (depending on "type")
14637 * of "map" under the function represented by "ma".
14638 * In other words, plug in "ma" in the domain or range of "map".
14639 * The result is a map that lives in the same space as "map"
14640 * except that the domain or range has been replaced by
14641 * the domain space of "ma".
14643 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14644 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14651 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14655 return map_preimage_multi_aff(map
, type
, ma
);
14657 if (isl_map_check_named_params(map
) < 0)
14659 if (!isl_space_has_named_params(ma
->space
))
14660 isl_die(map
->ctx
, isl_error_invalid
,
14661 "unaligned unnamed parameters", goto error
);
14662 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14663 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14665 return map_preimage_multi_aff(map
, type
, ma
);
14667 isl_multi_aff_free(ma
);
14668 return isl_map_free(map
);
14671 /* Compute the preimage of "set" under the function represented by "ma".
14672 * In other words, plug in "ma" in "set". The result is a set
14673 * that lives in the domain space of "ma".
14675 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14676 __isl_take isl_multi_aff
*ma
)
14678 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14681 /* Compute the preimage of the domain of "map" under the function
14682 * represented by "ma".
14683 * In other words, plug in "ma" in the domain of "map".
14684 * The result is a map that lives in the same space as "map"
14685 * except that the domain has been replaced by the domain space of "ma".
14687 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14688 __isl_take isl_multi_aff
*ma
)
14690 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14693 /* Compute the preimage of the range of "map" under the function
14694 * represented by "ma".
14695 * In other words, plug in "ma" in the range of "map".
14696 * The result is a map that lives in the same space as "map"
14697 * except that the range has been replaced by the domain space of "ma".
14699 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14700 __isl_take isl_multi_aff
*ma
)
14702 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14705 /* Compute the preimage of "map" under the function represented by "pma".
14706 * In other words, plug in "pma" in the domain or range of "map".
14707 * The result is a map that lives in the same space as "map",
14708 * except that the space of type "type" has been replaced by
14709 * the domain space of "pma".
14711 * The parameters of "map" and "pma" are assumed to have been aligned.
14713 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14714 __isl_take isl_map
*map
, enum isl_dim_type type
,
14715 __isl_take isl_pw_multi_aff
*pma
)
14726 space
= isl_pw_multi_aff_get_domain_space(pma
);
14727 isl_pw_multi_aff_free(pma
);
14728 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14730 return isl_map_empty(space
);
14733 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14734 isl_multi_aff_copy(pma
->p
[0].maff
));
14735 if (type
== isl_dim_in
)
14736 res
= isl_map_intersect_domain(res
,
14737 isl_map_copy(pma
->p
[0].set
));
14739 res
= isl_map_intersect_range(res
,
14740 isl_map_copy(pma
->p
[0].set
));
14742 for (i
= 1; i
< pma
->n
; ++i
) {
14745 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14746 isl_multi_aff_copy(pma
->p
[i
].maff
));
14747 if (type
== isl_dim_in
)
14748 res_i
= isl_map_intersect_domain(res_i
,
14749 isl_map_copy(pma
->p
[i
].set
));
14751 res_i
= isl_map_intersect_range(res_i
,
14752 isl_map_copy(pma
->p
[i
].set
));
14753 res
= isl_map_union(res
, res_i
);
14756 isl_pw_multi_aff_free(pma
);
14760 isl_pw_multi_aff_free(pma
);
14765 /* Compute the preimage of "map" under the function represented by "pma".
14766 * In other words, plug in "pma" in the domain or range of "map".
14767 * The result is a map that lives in the same space as "map",
14768 * except that the space of type "type" has been replaced by
14769 * the domain space of "pma".
14771 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14772 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14779 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14783 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14785 if (isl_map_check_named_params(map
) < 0)
14787 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14789 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14790 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14792 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14794 isl_pw_multi_aff_free(pma
);
14795 return isl_map_free(map
);
14798 /* Compute the preimage of "set" under the function represented by "pma".
14799 * In other words, plug in "pma" in "set". The result is a set
14800 * that lives in the domain space of "pma".
14802 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14803 __isl_take isl_pw_multi_aff
*pma
)
14805 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14808 /* Compute the preimage of the domain of "map" under the function
14809 * represented by "pma".
14810 * In other words, plug in "pma" in the domain of "map".
14811 * The result is a map that lives in the same space as "map",
14812 * except that domain space has been replaced by the domain space of "pma".
14814 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14815 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14817 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14820 /* Compute the preimage of the range of "map" under the function
14821 * represented by "pma".
14822 * In other words, plug in "pma" in the range of "map".
14823 * The result is a map that lives in the same space as "map",
14824 * except that range space has been replaced by the domain space of "pma".
14826 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14827 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14829 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14832 /* Compute the preimage of "map" under the function represented by "mpa".
14833 * In other words, plug in "mpa" in the domain or range of "map".
14834 * The result is a map that lives in the same space as "map",
14835 * except that the space of type "type" has been replaced by
14836 * the domain space of "mpa".
14838 * If the map does not involve any constraints that refer to the
14839 * dimensions of the substituted space, then the only possible
14840 * effect of "mpa" on the map is to map the space to a different space.
14841 * We create a separate isl_multi_aff to effectuate this change
14842 * in order to avoid spurious splitting of the map along the pieces
14844 * If "mpa" has a non-trivial explicit domain, however,
14845 * then the full substitution should be performed.
14847 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14848 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14852 isl_pw_multi_aff
*pma
;
14854 n
= isl_map_dim(map
, type
);
14858 full
= isl_map_involves_dims(map
, type
, 0, n
);
14859 if (full
>= 0 && !full
)
14860 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14867 space
= isl_multi_pw_aff_get_space(mpa
);
14868 isl_multi_pw_aff_free(mpa
);
14869 ma
= isl_multi_aff_zero(space
);
14870 return isl_map_preimage_multi_aff(map
, type
, ma
);
14873 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14874 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14877 isl_multi_pw_aff_free(mpa
);
14881 /* Compute the preimage of "map" under the function represented by "mpa".
14882 * In other words, plug in "mpa" in the domain "map".
14883 * The result is a map that lives in the same space as "map",
14884 * except that domain space has been replaced by the domain space of "mpa".
14886 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14887 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14889 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14892 /* Compute the preimage of "set" by the function represented by "mpa".
14893 * In other words, plug in "mpa" in "set".
14895 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14896 __isl_take isl_multi_pw_aff
*mpa
)
14898 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14901 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14902 * on the output dimension "pos" in terms of the parameters,
14903 * the input dimensions and possibly some integer divisions,
14904 * but not any other output dimensions, extract this upper bound
14905 * as a function of all dimensions (with zero coefficients
14906 * for the output dimensions).
14908 * That is, the inequality is of the form
14910 * e(...) + c - m x >= 0
14912 * where e does not depend on any other output dimensions.
14913 * Return (e(...) + c) / m, with the denominator m in the first position.
14915 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14916 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14919 isl_size v_out
, total
;
14921 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14922 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14923 if (v_out
< 0 || total
< 0)
14925 ctx
= isl_basic_map_get_ctx(bmap
);
14926 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14927 total
, v_out
+ pos
);
14930 /* Is constraint "c" of "bmap" of the form
14932 * e(...) + c1 - m x >= 0
14936 * -e(...) + c2 + m x >= 0
14938 * where m > 1 and e does not involve any other output variables?
14940 * "v_out" is the offset to the output variables.
14941 * "d" is the position of x among the output variables.
14942 * "v_div" is the offset to the local variables.
14943 * "total" is the total number of variables.
14945 * Since the purpose of this function is to use the constraint
14946 * to express the output variable as an integer division,
14947 * do not allow the constraint to involve any local variables
14948 * that do not have an explicit representation.
14950 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14951 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14955 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14956 return isl_bool_false
;
14957 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14958 return isl_bool_false
;
14959 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14960 return isl_bool_false
;
14961 if (isl_seq_any_non_zero(c
+ 1 + v_out
, d
))
14962 return isl_bool_false
;
14963 if (isl_seq_any_non_zero(c
+ 1 + v_out
+ d
+ 1, v_div
- (v_out
+ d
+ 1)))
14964 return isl_bool_false
;
14965 for (i
= 0; v_div
+ i
< total
; ++i
) {
14966 isl_bool known
, involves
;
14968 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14970 known
= isl_basic_map_div_is_known(bmap
, i
);
14971 if (known
< 0 || !known
)
14973 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14974 if (involves
< 0 || involves
)
14975 return isl_bool_not(involves
);
14977 return isl_bool_true
;
14980 /* Look for a pair of constraints
14982 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14986 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14988 * that express that the output dimension x at position "pos"
14989 * is some integer division of an expression in terms of the parameters,
14990 * input dimensions and integer divisions.
14991 * If such a pair can be found, then return the index
14992 * of the upper bound constraint, m x <= e(...) + c1.
14993 * Otherwise, return an index beyond the number of constraints.
14995 * The constraints of "bmap" are assumed to have been sorted.
14997 * In order for the constraints above to express an integer division,
14998 * m needs to be greater than 1 and such that
15000 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15002 * In particular, this ensures that
15004 * x = floor((e(...) + c1) / m)
15006 isl_size
isl_basic_map_find_output_upper_div_constraint(
15007 __isl_keep isl_basic_map
*bmap
, int pos
)
15012 isl_size v_out
, v_div
;
15016 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
15017 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15018 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15019 n_ineq
= isl_basic_map_n_inequality(bmap
);
15020 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
15021 return isl_size_error
;
15024 for (i
= 0; i
< n_ineq
; ++i
) {
15025 isl_bool potential
;
15027 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
15028 v_out
, pos
, v_div
, total
);
15034 j
= find_later_constraint_in_pair(bmap
, i
, v_out
+ pos
, total
,
15035 bmap
->ineq
[i
][1 + v_out
+ pos
], &sum
);
15041 isl_int_clear(sum
);
15045 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
15050 isl_int_clear(sum
);
15051 return isl_size_error
;
15054 /* Look for a pair of constraints
15057 * g(x) - i + d >= 0
15059 * in "bmap" on the output dimension at position "pos" such that
15060 * g(x) is an expression in the parameters and input dimensions, and
15062 * Return the index of the first constraint if such a pair can be found.
15063 * Otherwise, return an index beyond the number of inequality constraints.
15065 * The constraints are assumed to have been sorted.
15067 static isl_size
find_output_lower_mod_constraint(__isl_keep isl_basic_map
*bmap
,
15068 int pos
, isl_int n
)
15072 isl_size v_out
, n_out
;
15073 isl_size v_div
, n_div
;
15077 n_ineq
= isl_basic_map_n_inequality(bmap
);
15078 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15079 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15080 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15081 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15082 if (n_ineq
< 0 || v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15083 return isl_size_error
;
15086 for (i
= n_ineq
- 1; i
>= 0; --i
) {
15087 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]) &&
15088 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15090 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + v_div
, n_div
))
15092 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + v_out
, pos
))
15094 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + v_out
+ pos
+ 1,
15095 n_out
- (pos
+ 1)))
15097 j
= find_earlier_constraint_in_pair(bmap
, i
, v_out
+ pos
, v_div
,
15101 isl_int_clear(tmp
);
15104 if (isl_int_is_one(bmap
->ineq
[i
][1 + v_out
+ pos
]))
15109 isl_int_clear(tmp
);
15113 /* Turn "bmap" into a basic set for constructing an affine expression
15114 * that can later be plugged into an output dimension of "bmap".
15115 * "is_set" is set if "bmap" is actually a basic set already.
15117 * If "bmap" is a basic set already, then simply cast it.
15118 * Otherwise, wrap "bmap" into a basic set.
15120 static __isl_give isl_basic_set
*wrap_for_plug_in(
15121 __isl_take isl_basic_map
*bmap
, int is_set
)
15124 return bset_from_bmap(bmap
);
15126 return isl_basic_map_wrap(bmap
);
15129 /* Given an affine expression "aff" that was constructed on top of
15130 * the result of wrap_for_plug_in and that does not depend on the output space,
15131 * project out this output space.
15132 * "is_set" is the same as in the call to wrap_for_plug_in.
15134 * If "is_set" is set then the output space is actually the set space
15135 * of the original basic set and this appears as the domain of "aff".
15136 * Otherwise, the output space appears as the range in the nested domain.
15138 static __isl_give isl_aff
*unwrap_plug_in(__isl_take isl_aff
*aff
, int is_set
)
15141 return isl_aff_project_domain_on_params(aff
);
15143 return isl_aff_domain_factor_domain(aff
);
15146 /* Given that equality "eq" of "bset" expresses
15147 * the variable at position "pos" in terms of the other variables,
15148 * extract this expression as a function of those other variables,
15149 * excluding any local variables.
15151 static __isl_give isl_aff
*extract_expr_aff(
15152 __isl_keep isl_basic_set
*bset
, int eq
, int pos
)
15156 return extract_aff(bset
, bset
->eq
[eq
], pos
);
15159 /* Given an integer division "div" of the form
15163 * with i the variable at position "pos" and f not involving
15164 * any local variables, return the expression
15168 static __isl_give isl_aff
*extract_div_expr_aff(
15169 __isl_keep isl_basic_set
*bset
, int div
, int pos
)
15173 return extract_aff(bset
, bset
->div
[div
] + 1, pos
);
15176 /* Given the presence in "bmap" of an equality constraint "eq"
15178 * f(x) + t m i + m n h(y) = 0
15180 * and a pair of inequality constraints
15182 * -g(x) + i >= 0 ("lower")
15183 * g(x) - i + d >= 0
15185 * where i is the output dimension at position "pos",
15186 * f(x) and g(x) are expressions in the parameters and
15187 * input dimensions (if any),
15188 * h(y) is an expression in the other output dimensions,
15189 * t is +1 or -1, and
15191 * return the expression
15193 * (-t f(x)/m - g(x)) mod n + g(x)
15195 * The expression is first constructed in terms of the wrapped space of "bmap".
15196 * Since this expression does not depend on the original output dimensions,
15197 * they can be removed, resulting in an expression in terms
15198 * of the input dimensions.
15199 * If "bmap" is actually a set, then the resulting expression has no domain.
15201 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15202 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15204 static __isl_give isl_aff
*construct_mod(__isl_keep isl_basic_map
*bmap
,
15205 int pos
, int eq
, int lower
, isl_int n
)
15208 isl_basic_set
*bset
;
15209 isl_aff
*f
, *g
, *mod
;
15213 is_set
= isl_basic_map_is_set(bmap
);
15216 ctx
= isl_basic_map_get_ctx(bmap
);
15218 bset
= wrap_for_plug_in(isl_basic_map_copy(bmap
), is_set
);
15219 g
= extract_lower_bound_aff(bset
, lower
, pos
);
15220 f
= extract_expr_aff(bset
, eq
, pos
);
15221 isl_basic_set_free(bset
);
15223 mod
= isl_aff_sub(f
, isl_aff_copy(g
));
15224 v
= isl_val_int_from_isl_int(ctx
, n
);
15225 mod
= isl_aff_mod_val(mod
, v
);
15226 mod
= isl_aff_add(mod
, g
);
15227 mod
= unwrap_plug_in(mod
, is_set
);
15232 /* Look for a combination of constraints in "bmap" (including
15233 * an equality constraint) that ensure
15234 * that output dimension "pos" is equal to some modulo expression
15235 * in the parameters and input dimensions and
15236 * return this expression if found.
15238 * The constraints are assumed to have been sorted.
15240 * In particular look for an equality constraint
15242 * f(x) + t m i + m n h(y) = 0
15244 * and a pair of inequality constraints
15247 * g(x) - i + d >= 0
15249 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15250 * h(y) is an expression in local variables and other output dimensions,
15251 * t is +1 or -1, and
15254 * If such a combination of constraints can be found
15257 * i = -t f(x)/m - t n h(y)
15258 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15260 * and so, because 0 <= i - g(x) <= d < n,
15262 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15263 * = (-t f(x)/m - g(x)) mod n
15267 * i = (-t f(x)/m - g(x)) mod n + g(x)
15269 static __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_mod_eq(
15270 __isl_keep isl_basic_map
*bmap
, int pos
)
15273 isl_size n_eq
, n_ineq
;
15274 isl_size v_out
, n_out
;
15275 isl_size v_div
, n_div
;
15278 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15280 n_eq
= isl_basic_map_n_equality(bmap
);
15281 n_ineq
= isl_basic_map_n_inequality(bmap
);
15282 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15283 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15284 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15285 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15286 if (n_eq
< 0 || n_ineq
< 0 ||
15287 v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15289 if (n_eq
== 0 || n_ineq
< 2 || n_out
== 1)
15293 for (i
= 0; i
< n_eq
; ++i
) {
15294 if (isl_int_is_zero(bmap
->eq
[i
][1 + v_out
+ pos
]))
15296 isl_seq_gcd(bmap
->eq
[i
] + 1 + v_div
, n_div
, &gcd
);
15297 for (j
= 0; j
< n_out
; ++j
) {
15300 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ j
]);
15302 if (!isl_int_abs_gt(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15304 if (!isl_int_is_divisible_by(gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]))
15306 isl_int_divexact(gcd
, gcd
, bmap
->eq
[i
][1 + v_out
+ pos
]);
15307 isl_int_abs(gcd
, gcd
);
15308 lower
= find_output_lower_mod_constraint(bmap
, pos
, gcd
);
15309 if (lower
>= n_ineq
)
15311 res
.valid
= isl_bool_true
;
15313 res
.value
= construct_mod(bmap
, v_out
+ pos
, i
,
15316 res
.valid
= isl_bool_error
;
15317 isl_int_clear(gcd
);
15321 isl_int_clear(gcd
);
15324 res
.valid
= isl_bool_error
;
15328 /* Given that the integer division "div" of "bmap" is of the form
15330 * (f(x) + t m i)//(m n)
15332 * with t equal to 1 or -1 and n positive,
15333 * while the inequality constraint "lower" is of the form
15337 * with neither involving any local variables or output dimensions other than i,
15338 * construct the expression
15340 * (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15342 * "pos" is the position of the variable i.
15343 * "t" and "n" are the values t and n.
15345 * The expression is first constructed in terms of the wrapped space of "bmap".
15346 * Since this expression does not depend on the original output dimensions,
15347 * these can be removed, resulting in an expression in terms
15348 * of the input dimensions.
15349 * If "bmap" is actually a set, then the resulting expression has no domain.
15351 * First obtain the expressions
15356 * multiply the first with -t, take the floor and multiply with -t again,
15361 * Add t (n - 1) and subtract g(x) to obtain
15363 * t (n - 1) - t f(x)//m - g(x)
15365 * Finally, compute the modulo with respect to m and add g(x) back.
15367 static __isl_give isl_aff
*construct_mod_ineq(__isl_keep isl_basic_map
*bmap
,
15368 int pos
, int div
, int lower
, int t
, isl_int n
)
15371 isl_basic_set
*bset
;
15376 is_set
= isl_basic_map_is_set(bmap
);
15379 ctx
= isl_basic_map_get_ctx(bmap
);
15381 bset
= wrap_for_plug_in(isl_basic_map_copy(bmap
), is_set
);
15382 f
= extract_div_expr_aff(bset
, div
, pos
);
15383 g
= extract_lower_bound_aff(bset
, lower
, pos
);
15384 isl_basic_set_free(bset
);
15388 f
= isl_aff_floor(f
);
15392 v
= isl_val_int_from_isl_int(ctx
, n
);
15393 v
= isl_val_sub_ui(v
, 1);
15395 v
= isl_val_neg(v
);
15396 f
= isl_aff_add_constant_val(f
, v
);
15398 f
= isl_aff_sub(f
, isl_aff_copy(g
));
15399 v
= isl_val_int_from_isl_int(ctx
, n
);
15400 f
= isl_aff_mod_val(f
, v
);
15401 f
= isl_aff_add(f
, g
);
15403 f
= unwrap_plug_in(f
, is_set
);
15408 /* Given that inequality constraint "ineq" of "bmap" is of the form
15410 * f(x) + t m i - m n e + c >= 0
15412 * with integer division "e" of the form
15414 * e = (f(x) + t m i)//(m n)
15416 * f(x) an expression in the parameters and input dimensions and
15418 * check whether c <= -(n - 1) m and then look for
15419 * a pair of inequality constraints
15422 * g(x) - i + d >= 0
15424 * where g(x) is an expression in the parameters and input dimensions and
15426 * If successful, return the expression
15428 * (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15430 * "v_out" is the position of the output dimensions among the variables.
15431 * "pos" is the position of the output dimension i.
15432 * "n_ineq" is the number of inequality constraints.
15433 * "v_div" is the position of the local dimensions among the variables.
15436 * First extract the constant value "n".
15437 * If c' is the constant term of f(x), then the constant term
15438 * of the inequality constraint is c' + c.
15439 * Check that c' + c <= c' - (n - 1) m.
15440 * If so, and if the pair of inequality constraints can be found,
15441 * then extract the expression above.
15443 static __isl_give isl_maybe_isl_aff
try_find_output_mod_ineq_at(
15444 __isl_keep isl_basic_map
*bmap
, int v_out
, int pos
,
15445 int ineq
, int n_ineq
, int v_div
, int e
)
15449 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15454 isl_int_divexact(n
, bmap
->ineq
[ineq
][1 + v_div
+ e
],
15455 bmap
->ineq
[ineq
][1 + v_out
+ pos
]);
15457 isl_int_sub_ui(t
, n
, 1);
15458 isl_int_mul(t
, t
, bmap
->ineq
[ineq
][1 + v_out
+ pos
]);
15461 isl_int_add(t
, t
, bmap
->div
[e
][1]);
15462 if (isl_int_le(bmap
->ineq
[ineq
][0], t
)) {
15463 lower
= find_output_lower_mod_constraint(bmap
, pos
, n
);
15464 if (lower
< n_ineq
) {
15467 t
= isl_int_sgn(bmap
->ineq
[ineq
][1 + v_out
+ pos
]);
15468 res
.valid
= isl_bool_true
;
15469 res
.value
= construct_mod_ineq(bmap
, v_out
+ pos
,
15480 /* In the sequence of length "len" starting at "p",
15481 * is the element at "pos" the only non-zero element?
15483 static int only_non_zero(isl_int
*p
, unsigned pos
, unsigned len
)
15485 if (isl_int_is_zero(p
[pos
]))
15487 return unique(p
, pos
, len
);
15490 /* Look for a combination of inequality constraints in "bmap" that ensure
15491 * that output dimension "pos" is equal to some modulo expression
15492 * in the parameters and input dimensions and
15493 * return this expression if found.
15495 * The constraints are assumed to have been sorted.
15497 * In particular, look for an integer division of the form
15499 * e = (f(x) + t m i)//(m n)
15501 * with a corresponding inequality constraint
15503 * f(x) + t m i - m n e + c >= 0 (*)
15505 * and a pair of inequality constraints
15508 * g(x) - i + d >= 0
15510 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15512 * t is +1 or -1, and
15517 * e = (f(x) + t m i)//(m n) = (f(x)//m + t i)//n
15521 * f(x)//m + t i - (n - 1) <= n e <= f(x)//m + t i
15523 * and in particular
15525 * f(x)//m <= - t i + n e + (n - 1)
15527 * Constraint (*) implies
15529 * f(x) >= -t m i + m n e - c >= -t m i + m n e + (n - 1) m
15533 * f(x)//m >= - t i + n e + (n - 1)
15535 * Together, this implies
15537 * f(x)//m = -t i + n e + (n - 1)
15538 * t i = n e + (n - 1) - f(x)//m
15539 * i = t n e + t (n - 1) - t f(x)//m
15540 * i - g(x) = t n e + t (n - 1) - t f(x)//m - g(x)
15542 * and so, because 0 <= i - g(x) <= d < n,
15544 * i - g(x) = (t (n - 1) - t f(x)//m - g(x)) mod n
15548 * i = (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15551 * First look for a suitable inequality constraint for (*),
15552 * with the corresponding integer division.
15553 * If any such combination is found, look for an appropriate g(x) and
15554 * construct the corresponding expression in try_find_output_mod_ineq_at.
15556 static __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_mod_ineq(
15557 __isl_keep isl_basic_map
*bmap
, int pos
)
15561 isl_size v_out
, n_out
;
15562 isl_size v_div
, n_div
;
15563 isl_maybe_isl_aff no
= { isl_bool_false
, NULL
};
15564 isl_maybe_isl_aff error
= { isl_bool_error
, NULL
};
15566 n_ineq
= isl_basic_map_n_inequality(bmap
);
15567 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
15568 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
15569 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
15570 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
15571 if (n_ineq
< 0 || v_out
< 0 || n_out
< 0 || v_div
< 0 || n_div
< 0)
15573 if (n_div
< 1 || n_ineq
< 4)
15576 for (i
= 0; i
< n_ineq
; ++i
) {
15580 if (!only_non_zero(bmap
->ineq
[i
] + 1 + v_out
, pos
, n_out
))
15582 e
= isl_seq_last_non_zero(bmap
->ineq
[i
] + 1 + v_div
, n_div
);
15585 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, e
);
15590 if (isl_seq_any_non_zero(bmap
->ineq
[i
] + 1 + v_div
, e
))
15592 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ e
]))
15594 if (!isl_int_is_divisible_by(bmap
->ineq
[i
][1 + v_div
+ e
],
15595 bmap
->ineq
[i
][1 + v_out
+ pos
]))
15597 if (!isl_int_abs_eq(bmap
->ineq
[i
][1 + v_div
+ e
],
15600 if (!isl_seq_eq(bmap
->ineq
[i
] + 1, bmap
->div
[e
] + 2, v_div
+ e
))
15602 return try_find_output_mod_ineq_at(bmap
, v_out
, pos
, i
, n_ineq
,
15609 /* Look for a combination of constraints in "bmap" that ensure
15610 * that output dimension "pos" is equal to some modulo expression
15611 * in the parameters and input dimensions and
15612 * return this expression if found.
15614 * The constraints are assumed to have been sorted.
15616 * First look for a pattern involving an equality constraint and
15617 * then look for a pattern involving only inequality constraints.
15619 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_mod(
15620 __isl_keep isl_basic_map
*bmap
, int pos
)
15622 isl_maybe_isl_aff mod
;
15624 mod
= isl_basic_map_try_find_output_mod_eq(bmap
, pos
);
15625 if (mod
.valid
< 0 || mod
.valid
)
15627 return isl_basic_map_try_find_output_mod_ineq(bmap
, pos
);
15630 /* Construct an isl_aff from the given domain local space "ls" and
15631 * coefficients "v", where the local space may involve
15632 * local variables without a known expression, as long as these
15633 * do not have a non-zero coefficient in "v".
15634 * These need to be pruned away first since an isl_aff cannot
15635 * reference any local variables without a known expression.
15636 * For simplicity, remove all local variables that have a zero coefficient and
15637 * that are not used in other local variables with a non-zero coefficient.
15639 static __isl_give isl_aff
*isl_aff_alloc_vec_prune(
15640 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
15643 isl_size n_div
, v_div
;
15645 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
15646 v_div
= isl_local_space_var_offset(ls
, isl_dim_div
);
15647 if (n_div
< 0 || v_div
< 0 || !v
)
15649 for (i
= n_div
- 1; i
>= 0; --i
) {
15652 if (!isl_int_is_zero(v
->el
[1 + 1 + v_div
+ i
]))
15654 involves
= isl_local_space_involves_dims(ls
, isl_dim_div
, i
, 1);
15659 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
15660 v
= isl_vec_drop_els(v
, 1 + 1 + v_div
+ i
, 1);
15665 return isl_aff_alloc_vec(ls
, v
);
15667 isl_local_space_free(ls
);
15672 /* Look for a pair of constraints in "bmap" that ensure
15673 * that output dimension "pos" is equal to some integer division expression
15674 * in the parameters and input dimensions and
15675 * return this expression if found.
15677 * In particular, looks for a pair of constraints
15679 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
15683 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
15685 * where m > 1 and e only depends on parameters and input dimensions,
15688 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15690 * If such a pair of constraints can be found
15693 * x = floor((e(...) + c1) / m)
15695 * with e(...) an expression that does not involve any other output dimensions.
15697 * Note that we know that
15701 * If c1 + c2 were 0, then we would have detected an equality during
15702 * simplification. If c1 + c2 were negative, then we would have detected
15705 * The constraint defining the integer division is guaranteed not to involve
15706 * any local variables without a known expression, but such local variables
15707 * may appear in other constraints. They therefore need to be removed
15708 * during the construction of the affine expression.
15710 static __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div(
15711 __isl_keep isl_basic_map
*bmap
, int pos
)
15715 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15716 isl_local_space
*ls
;
15721 n_ineq
= isl_basic_map_n_inequality(bmap
);
15725 i
= isl_basic_map_find_output_upper_div_constraint(bmap
, pos
);
15731 is_set
= isl_basic_map_is_set(bmap
);
15733 bmap
= isl_basic_map_free(bmap
);
15735 ls
= isl_basic_map_get_local_space(bmap
);
15737 ls
= isl_local_space_wrap(ls
);
15738 v
= isl_basic_map_inequality_extract_output_upper_bound(bmap
, i
, pos
);
15740 aff
= isl_aff_alloc_vec_prune(ls
, v
);
15741 aff
= isl_aff_floor(aff
);
15744 aff
= isl_aff_project_domain_on_params(aff
);
15746 aff
= isl_aff_domain_factor_domain(aff
);
15748 res
.valid
= isl_bool_true
;
15752 res
.valid
= isl_bool_error
;
15756 /* Look for a combination of constraints in "bmap" that ensure
15757 * that output dimension "pos" is equal to some integer division or
15758 * modulo expression in the parameters and input dimensions and
15759 * return this expression if found.
15761 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_output_div_mod(
15762 __isl_keep isl_basic_map
*bmap
, int pos
)
15764 isl_maybe_isl_aff div
;
15766 div
= isl_basic_map_try_find_output_div(bmap
, pos
);
15767 if (div
.valid
< 0 || div
.valid
)
15769 return isl_basic_map_try_find_output_mod(bmap
, pos
);
15772 /* Look for a combination of constraints in "bmap" that ensure
15773 * that any output dimension is equal to some integer division or
15774 * modulo expression in the parameters and input dimensions and
15775 * return this expression if found.
15776 * The position of the output dimension (if any) is returned in "pos".
15778 __isl_give isl_maybe_isl_aff
isl_basic_map_try_find_any_output_div_mod(
15779 __isl_keep isl_basic_map
*bmap
, int *pos
)
15782 isl_maybe_isl_aff res
= { isl_bool_false
, NULL
};
15784 dim
= isl_basic_map_dim(bmap
, isl_dim_out
);
15788 for (*pos
= 0; *pos
< dim
; ++*pos
) {
15789 res
= isl_basic_map_try_find_output_div_mod(bmap
, *pos
);
15790 if (res
.valid
< 0 || res
.valid
)
15796 res
.valid
= isl_bool_error
;
15800 /* Return a copy of the equality constraints of "bset" as a matrix.
15802 __isl_give isl_mat
*isl_basic_set_extract_equalities(
15803 __isl_keep isl_basic_set
*bset
)
15808 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15812 ctx
= isl_basic_set_get_ctx(bset
);
15813 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
15816 /* Are the "n" "coefficients" starting at "first" of the integer division
15817 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15819 * The "coefficient" at position 0 is the denominator.
15820 * The "coefficient" at position 1 is the constant term.
15822 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
15823 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
15824 unsigned first
, unsigned n
)
15826 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
15827 return isl_bool_error
;
15828 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
15829 return isl_bool_error
;
15830 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
15831 bmap2
->div
[pos2
] + first
, n
);
15834 /* Are the integer division expressions at position "pos1" in "bmap1" and
15835 * "pos2" in "bmap2" equal to each other, except that the constant terms
15838 isl_bool
isl_basic_map_equal_div_expr_except_constant(
15839 __isl_keep isl_basic_map
*bmap1
, int pos1
,
15840 __isl_keep isl_basic_map
*bmap2
, int pos2
)
15843 isl_size total
, total2
;
15845 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
15846 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
15847 if (total
< 0 || total2
< 0)
15848 return isl_bool_error
;
15849 if (total
!= total2
)
15850 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
15851 "incomparable div expressions", return isl_bool_error
);
15852 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15854 if (equal
< 0 || !equal
)
15856 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15858 if (equal
< 0 || equal
)
15859 return isl_bool_not(equal
);
15860 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
15864 /* Replace the numerator of the constant term of the integer division
15865 * expression at position "div" in "bmap" by "value".
15866 * The caller guarantees that this does not change the meaning
15869 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
15870 __isl_take isl_basic_map
*bmap
, int div
, int value
)
15872 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
15873 return isl_basic_map_free(bmap
);
15875 isl_int_set_si(bmap
->div
[div
][1], value
);
15880 /* Is the point "inner" internal to inequality constraint "ineq"
15882 * The point is considered to be internal to the inequality constraint,
15883 * if it strictly lies on the positive side of the inequality constraint,
15884 * or if it lies on the constraint and the constraint is lexico-positive.
15886 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
15887 __isl_keep isl_basic_set
*bset
, int ineq
)
15893 if (!inner
|| !bset
)
15894 return isl_bool_error
;
15896 ctx
= isl_basic_set_get_ctx(bset
);
15897 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
15898 &ctx
->normalize_gcd
);
15899 if (!isl_int_is_zero(ctx
->normalize_gcd
))
15900 return isl_int_is_nonneg(ctx
->normalize_gcd
);
15902 total
= isl_basic_set_dim(bset
, isl_dim_all
);
15904 return isl_bool_error
;
15905 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
15906 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
15909 /* Tighten the inequality constraints of "bset" that are outward with respect
15910 * to the point "vec".
15911 * That is, tighten the constraints that are not satisfied by "vec".
15913 * "vec" is a point internal to some superset S of "bset" that is used
15914 * to make the subsets of S disjoint, by tightening one half of the constraints
15915 * that separate two subsets. In particular, the constraints of S
15916 * are all satisfied by "vec" and should not be tightened.
15917 * Of the internal constraints, those that have "vec" on the outside
15918 * are tightened. The shared facet is included in the adjacent subset
15919 * with the opposite constraint.
15920 * For constraints that saturate "vec", this criterion cannot be used
15921 * to determine which of the two sides should be tightened.
15922 * Instead, the sign of the first non-zero coefficient is used
15923 * to make this choice. Note that this second criterion is never used
15924 * on the constraints of S since "vec" is interior to "S".
15926 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
15927 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
15931 bset
= isl_basic_set_cow(bset
);
15934 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
15937 internal
= is_internal(vec
, bset
, j
);
15939 return isl_basic_set_free(bset
);
15942 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
15948 /* Replace the variables x of type "type" starting at "first" in "bmap"
15949 * by x' with x = M x' with M the matrix trans.
15950 * That is, replace the corresponding coefficients c by c M.
15952 * The transformation matrix should be a square matrix.
15954 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
15955 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
15956 __isl_take isl_mat
*trans
)
15960 bmap
= isl_basic_map_cow(bmap
);
15961 if (!bmap
|| !trans
)
15964 if (trans
->n_row
!= trans
->n_col
)
15965 isl_die(trans
->ctx
, isl_error_invalid
,
15966 "expecting square transformation matrix", goto error
);
15967 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
15970 pos
= isl_basic_map_offset(bmap
, type
) + first
;
15972 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
15973 isl_mat_copy(trans
)) < 0)
15975 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
15976 isl_mat_copy(trans
)) < 0)
15978 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
15979 isl_mat_copy(trans
)) < 0)
15982 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
15983 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
15984 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
15986 isl_mat_free(trans
);
15989 isl_mat_free(trans
);
15990 isl_basic_map_free(bmap
);
15994 /* Replace the variables x of type "type" starting at "first" in "bset"
15995 * by x' with x = M x' with M the matrix trans.
15996 * That is, replace the corresponding coefficients c by c M.
15998 * The transformation matrix should be a square matrix.
16000 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
16001 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
16002 __isl_take isl_mat
*trans
)
16004 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);