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
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
47 #include <isl_printer_private.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
59 __isl_keep
const isl_basic_set
*bset
)
61 return (const isl_basic_map
*) bset
;
64 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return 1;
68 case isl_dim_in
: return 1 + dim
->nparam
;
69 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
74 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
75 enum isl_dim_type type
)
78 return isl_size_error
;
80 case isl_dim_cst
: return 1;
83 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
84 case isl_dim_div
: return bmap
->n_div
;
85 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
90 /* Return the space of "map".
92 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
94 return map
? map
->dim
: NULL
;
97 /* Return the space of "set".
99 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
101 return isl_map_peek_space(set_to_map(set
));
104 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
106 return isl_space_dim(isl_map_peek_space(map
), type
);
109 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
111 return isl_map_dim(set_to_map(set
), type
);
114 /* Return the position of the variables of the given type
115 * within the sequence of variables of "bmap".
117 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
118 enum isl_dim_type type
)
122 space
= isl_basic_map_peek_space(bmap
);
124 return isl_size_error
;
129 case isl_dim_out
: return isl_space_offset(space
, type
);
130 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
133 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
134 "invalid dimension type", return isl_size_error
);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
142 enum isl_dim_type type
)
144 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
147 /* Return the position of the coefficients of the variables of the given type
148 * within the sequence of coefficients of "bmap".
150 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
151 enum isl_dim_type type
)
154 case isl_dim_cst
: return 0;
158 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
163 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
164 enum isl_dim_type type
)
166 return isl_basic_map_offset(bset
, type
);
169 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
171 return pos(map
->dim
, type
);
174 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
175 enum isl_dim_type type
)
177 return isl_basic_map_dim(bset
, type
);
180 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
182 return isl_basic_set_dim(bset
, isl_dim_set
);
185 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
187 return isl_basic_set_dim(bset
, isl_dim_param
);
190 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
192 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
195 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
197 return isl_set_dim(set
, isl_dim_set
);
200 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
202 return isl_set_dim(set
, isl_dim_param
);
205 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
210 return isl_size_error
;
211 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
213 return isl_size_error
;
214 return dim
+ bmap
->n_div
;
217 /* Return the number of equality constraints in the description of "bmap".
218 * Return -1 on error.
220 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
227 /* Return the number of equality constraints in the description of "bset".
228 * Return -1 on error.
230 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
232 return isl_basic_map_n_equality(bset_to_bmap(bset
));
235 /* Return the number of inequality constraints in the description of "bmap".
236 * Return -1 on error.
238 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
245 /* Return the number of inequality constraints in the description of "bset".
246 * Return -1 on error.
248 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
250 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
253 /* Do "bmap1" and "bmap2" have the same parameters?
255 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
256 __isl_keep isl_basic_map
*bmap2
)
258 isl_space
*space1
, *space2
;
260 space1
= isl_basic_map_peek_space(bmap1
);
261 space2
= isl_basic_map_peek_space(bmap2
);
262 return isl_space_has_equal_params(space1
, space2
);
265 /* Do "map1" and "map2" have the same parameters?
267 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
268 __isl_keep isl_map
*map2
)
270 isl_space
*space1
, *space2
;
272 space1
= isl_map_peek_space(map1
);
273 space2
= isl_map_peek_space(map2
);
274 return isl_space_has_equal_params(space1
, space2
);
277 /* Do "map" and "set" have the same parameters?
279 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
280 __isl_keep isl_set
*set
)
282 return isl_map_has_equal_params(map
, set_to_map(set
));
285 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
286 __isl_keep isl_set
*set
)
290 return isl_bool_error
;
291 m
= isl_map_has_equal_params(map
, set_to_map(set
));
294 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
295 set
->dim
, isl_dim_set
);
298 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
299 __isl_keep isl_basic_set
*bset
)
303 return isl_bool_error
;
304 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
307 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
308 bset
->dim
, isl_dim_set
);
311 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
312 __isl_keep isl_set
*set
)
316 return isl_bool_error
;
317 m
= isl_map_has_equal_params(map
, set_to_map(set
));
320 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
321 set
->dim
, isl_dim_set
);
324 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
325 __isl_keep isl_basic_set
*bset
)
329 return isl_bool_error
;
330 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
333 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
334 bset
->dim
, isl_dim_set
);
337 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
339 return bmap
? bmap
->ctx
: NULL
;
342 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
344 return bset
? bset
->ctx
: NULL
;
347 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
349 return map
? map
->ctx
: NULL
;
352 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
354 return set
? set
->ctx
: NULL
;
357 /* Return the space of "bmap".
359 __isl_keep isl_space
*isl_basic_map_peek_space(
360 __isl_keep
const isl_basic_map
*bmap
)
362 return bmap
? bmap
->dim
: NULL
;
365 /* Return the space of "bset".
367 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
369 return isl_basic_map_peek_space(bset_to_bmap(bset
));
372 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
374 return isl_space_copy(isl_basic_map_peek_space(bmap
));
377 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
379 return isl_basic_map_get_space(bset_to_bmap(bset
));
382 /* Extract the divs in "bmap" as a matrix.
384 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
392 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
396 ctx
= isl_basic_map_get_ctx(bmap
);
397 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
398 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
402 for (i
= 0; i
< bmap
->n_div
; ++i
)
403 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
408 /* Extract the divs in "bset" as a matrix.
410 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
412 return isl_basic_map_get_divs(bset
);
415 __isl_give isl_local_space
*isl_basic_map_get_local_space(
416 __isl_keep isl_basic_map
*bmap
)
423 div
= isl_basic_map_get_divs(bmap
);
424 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
427 __isl_give isl_local_space
*isl_basic_set_get_local_space(
428 __isl_keep isl_basic_set
*bset
)
430 return isl_basic_map_get_local_space(bset
);
433 /* For each known div d = floor(f/m), add the constraints
436 * -(f-(m-1)) + m d >= 0
438 * Do not finalize the result.
440 static __isl_give isl_basic_map
*add_known_div_constraints(
441 __isl_take isl_basic_map
*bmap
)
446 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
448 return isl_basic_map_free(bmap
);
451 bmap
= isl_basic_map_cow(bmap
);
452 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
455 for (i
= 0; i
< n_div
; ++i
) {
456 if (isl_int_is_zero(bmap
->div
[i
][0]))
458 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
464 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
465 __isl_take isl_local_space
*ls
)
471 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
473 ls
= isl_local_space_free(ls
);
477 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
478 n_div
, 0, 2 * n_div
);
480 for (i
= 0; i
< n_div
; ++i
)
481 if (isl_basic_map_alloc_div(bmap
) < 0)
484 for (i
= 0; i
< n_div
; ++i
)
485 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
486 bmap
= add_known_div_constraints(bmap
);
488 isl_local_space_free(ls
);
491 isl_local_space_free(ls
);
492 isl_basic_map_free(bmap
);
496 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
497 __isl_take isl_local_space
*ls
)
499 return isl_basic_map_from_local_space(ls
);
502 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
504 return isl_space_copy(isl_map_peek_space(map
));
507 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
511 return isl_space_copy(set
->dim
);
514 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
515 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
517 bmap
= isl_basic_map_cow(bmap
);
520 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
523 bmap
= isl_basic_map_finalize(bmap
);
526 isl_basic_map_free(bmap
);
530 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
531 __isl_take isl_basic_set
*bset
, const char *s
)
533 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
536 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
537 enum isl_dim_type type
)
539 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
542 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
543 enum isl_dim_type type
, const char *s
)
547 map
= isl_map_cow(map
);
551 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
555 for (i
= 0; i
< map
->n
; ++i
) {
556 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
567 /* Replace the identifier of the tuple of type "type" by "id".
569 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
570 __isl_take isl_basic_map
*bmap
,
571 enum isl_dim_type type
, __isl_take isl_id
*id
)
573 bmap
= isl_basic_map_cow(bmap
);
576 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
578 return isl_basic_map_free(bmap
);
579 bmap
= isl_basic_map_finalize(bmap
);
586 /* Replace the identifier of the tuple by "id".
588 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
589 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
591 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
594 /* Does the input or output tuple have a name?
596 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
598 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
601 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
602 enum isl_dim_type type
)
604 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
607 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
610 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
614 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
615 enum isl_dim_type type
, __isl_take isl_id
*id
)
617 map
= isl_map_cow(map
);
621 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
623 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
629 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
630 __isl_take isl_id
*id
)
632 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
635 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
636 enum isl_dim_type type
)
638 map
= isl_map_cow(map
);
642 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
644 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
647 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
649 return isl_map_reset_tuple_id(set
, isl_dim_set
);
652 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
654 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
657 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
658 enum isl_dim_type type
)
660 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
663 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
665 return isl_map_has_tuple_id(set
, isl_dim_set
);
668 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
670 return isl_map_get_tuple_id(set
, isl_dim_set
);
673 /* Does the set tuple have a name?
675 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
678 return isl_bool_error
;
679 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
683 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
685 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
688 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
690 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
693 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
694 enum isl_dim_type type
, unsigned pos
)
696 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
699 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
700 enum isl_dim_type type
, unsigned pos
)
702 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
705 /* Does the given dimension have a name?
707 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
708 enum isl_dim_type type
, unsigned pos
)
711 return isl_bool_error
;
712 return isl_space_has_dim_name(map
->dim
, type
, pos
);
715 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
716 enum isl_dim_type type
, unsigned pos
)
718 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
721 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
722 enum isl_dim_type type
, unsigned pos
)
724 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
727 /* Does the given dimension have a name?
729 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
730 enum isl_dim_type type
, unsigned pos
)
733 return isl_bool_error
;
734 return isl_space_has_dim_name(set
->dim
, type
, pos
);
737 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
738 __isl_take isl_basic_map
*bmap
,
739 enum isl_dim_type type
, unsigned pos
, const char *s
)
741 bmap
= isl_basic_map_cow(bmap
);
744 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
747 return isl_basic_map_finalize(bmap
);
749 isl_basic_map_free(bmap
);
753 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
754 enum isl_dim_type type
, unsigned pos
, const char *s
)
758 map
= isl_map_cow(map
);
762 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
766 for (i
= 0; i
< map
->n
; ++i
) {
767 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
778 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
779 __isl_take isl_basic_set
*bset
,
780 enum isl_dim_type type
, unsigned pos
, const char *s
)
782 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
786 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
787 enum isl_dim_type type
, unsigned pos
, const char *s
)
789 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
793 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
794 enum isl_dim_type type
, unsigned pos
)
797 return isl_bool_error
;
798 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
801 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
802 enum isl_dim_type type
, unsigned pos
)
804 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
807 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
808 enum isl_dim_type type
, unsigned pos
)
810 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
813 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
814 enum isl_dim_type type
, unsigned pos
)
816 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
819 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
820 enum isl_dim_type type
, unsigned pos
)
822 return isl_map_has_dim_id(set
, type
, pos
);
825 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
826 enum isl_dim_type type
, unsigned pos
)
828 return isl_map_get_dim_id(set
, type
, pos
);
831 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
832 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
834 map
= isl_map_cow(map
);
838 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
840 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
846 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
847 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
849 return isl_map_set_dim_id(set
, type
, pos
, id
);
852 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
853 __isl_keep isl_id
*id
)
857 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
860 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
861 __isl_keep isl_id
*id
)
863 return isl_map_find_dim_by_id(set
, type
, id
);
866 /* Return the position of the dimension of the given type and name
868 * Return -1 if no such dimension can be found.
870 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
871 enum isl_dim_type type
, const char *name
)
875 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
878 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
883 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
886 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
889 return isl_map_find_dim_by_name(set
, type
, name
);
892 /* Check whether equality i of bset is a pure stride constraint
893 * on a single dimension, i.e., of the form
897 * with k a constant and e an existentially quantified variable.
899 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
907 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
908 d
= isl_basic_set_dim(bset
, isl_dim_set
);
909 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
910 if (nparam
< 0 || d
< 0 || n_div
< 0)
911 return isl_bool_error
;
913 if (!isl_int_is_zero(bset
->eq
[i
][0]))
914 return isl_bool_false
;
916 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
917 return isl_bool_false
;
918 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
920 return isl_bool_false
;
921 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
923 return isl_bool_false
;
925 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
927 return isl_bool_false
;
928 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
929 n_div
- pos2
- 1) != -1)
930 return isl_bool_false
;
931 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
932 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
933 return isl_bool_false
;
935 return isl_bool_true
;
938 /* Reset the user pointer on all identifiers of parameters and tuples
939 * of the space of "map".
941 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
945 space
= isl_map_get_space(map
);
946 space
= isl_space_reset_user(space
);
947 map
= isl_map_reset_space(map
, space
);
952 /* Reset the user pointer on all identifiers of parameters and tuples
953 * of the space of "set".
955 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
957 return isl_map_reset_user(set
);
960 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
963 return isl_bool_error
;
964 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
967 /* Has "map" been marked as a rational map?
968 * In particular, have all basic maps in "map" been marked this way?
969 * An empty map is not considered to be rational.
970 * Maps where only some of the basic maps are marked rational
973 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
979 return isl_bool_error
;
981 return isl_bool_false
;
982 rational
= isl_basic_map_is_rational(map
->p
[0]);
985 for (i
= 1; i
< map
->n
; ++i
) {
988 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
991 if (rational
!= rational_i
)
992 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
993 "mixed rational and integer basic maps "
994 "not supported", return isl_bool_error
);
1000 /* Has "set" been marked as a rational set?
1001 * In particular, have all basic set in "set" been marked this way?
1002 * An empty set is not considered to be rational.
1003 * Sets where only some of the basic sets are marked rational
1006 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1008 return isl_map_is_rational(set
);
1011 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1013 return isl_basic_map_is_rational(bset
);
1016 /* Does "bmap" contain any rational points?
1018 * If "bmap" has an equality for each dimension, equating the dimension
1019 * to an integer constant, then it has no rational points, even if it
1020 * is marked as rational.
1022 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1024 isl_bool has_rational
= isl_bool_true
;
1028 return isl_bool_error
;
1029 if (isl_basic_map_plain_is_empty(bmap
))
1030 return isl_bool_false
;
1031 if (!isl_basic_map_is_rational(bmap
))
1032 return isl_bool_false
;
1033 bmap
= isl_basic_map_copy(bmap
);
1034 bmap
= isl_basic_map_implicit_equalities(bmap
);
1035 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1037 return isl_bool_error
;
1038 if (bmap
->n_eq
== total
) {
1040 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1041 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1044 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1045 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1047 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1052 if (i
== bmap
->n_eq
)
1053 has_rational
= isl_bool_false
;
1055 isl_basic_map_free(bmap
);
1057 return has_rational
;
1060 /* Does "map" contain any rational points?
1062 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1065 isl_bool has_rational
;
1068 return isl_bool_error
;
1069 for (i
= 0; i
< map
->n
; ++i
) {
1070 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1071 if (has_rational
< 0 || has_rational
)
1072 return has_rational
;
1074 return isl_bool_false
;
1077 /* Does "set" contain any rational points?
1079 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1081 return isl_map_has_rational(set
);
1084 /* Is this basic set a parameter domain?
1086 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1089 return isl_bool_error
;
1090 return isl_space_is_params(bset
->dim
);
1093 /* Is this set a parameter domain?
1095 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1098 return isl_bool_error
;
1099 return isl_space_is_params(set
->dim
);
1102 /* Is this map actually a parameter domain?
1103 * Users should never call this function. Outside of isl,
1104 * a map can never be a parameter domain.
1106 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1109 return isl_bool_error
;
1110 return isl_space_is_params(map
->dim
);
1113 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1114 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1115 unsigned n_eq
, unsigned n_ineq
)
1118 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1119 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1120 size_t row_size
= 1 + n_var
+ extra
;
1126 return isl_basic_map_free(bmap
);
1128 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1129 if (isl_blk_is_error(bmap
->block
))
1132 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1133 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1137 bmap
->block2
= isl_blk_empty();
1140 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1141 if (isl_blk_is_error(bmap
->block2
))
1144 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1149 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1150 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1152 for (i
= 0; i
< extra
; ++i
)
1153 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1157 bmap
->c_size
= n_eq
+ n_ineq
;
1158 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1159 bmap
->extra
= extra
;
1163 bmap
->sample
= NULL
;
1167 isl_basic_map_free(bmap
);
1171 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1172 unsigned nparam
, unsigned dim
, unsigned extra
,
1173 unsigned n_eq
, unsigned n_ineq
)
1175 struct isl_basic_map
*bmap
;
1178 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1182 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1183 return bset_from_bmap(bmap
);
1186 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1187 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1189 struct isl_basic_map
*bmap
;
1192 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1193 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1194 return bset_from_bmap(bmap
);
1196 isl_space_free(dim
);
1200 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1201 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1203 struct isl_basic_map
*bmap
;
1207 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1212 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1214 isl_space_free(space
);
1218 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1219 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1220 unsigned n_eq
, unsigned n_ineq
)
1222 struct isl_basic_map
*bmap
;
1225 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1229 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1233 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1234 __isl_keep isl_basic_map
*src
)
1237 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1239 if (!dst
|| total
< 0)
1240 return isl_basic_map_free(dst
);
1242 for (i
= 0; i
< src
->n_eq
; ++i
) {
1243 int j
= isl_basic_map_alloc_equality(dst
);
1245 return isl_basic_map_free(dst
);
1246 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1249 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1250 int j
= isl_basic_map_alloc_inequality(dst
);
1252 return isl_basic_map_free(dst
);
1253 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1256 for (i
= 0; i
< src
->n_div
; ++i
) {
1257 int j
= isl_basic_map_alloc_div(dst
);
1259 return isl_basic_map_free(dst
);
1260 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1262 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1266 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1268 struct isl_basic_map
*dup
;
1272 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1273 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1274 dup
= dup_constraints(dup
, bmap
);
1277 dup
->flags
= bmap
->flags
;
1278 dup
->sample
= isl_vec_copy(bmap
->sample
);
1282 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1284 struct isl_basic_map
*dup
;
1286 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1287 return bset_from_bmap(dup
);
1290 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1295 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1299 return isl_basic_set_dup(bset
);
1302 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1311 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1316 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1320 bmap
= isl_basic_map_dup(bmap
);
1322 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1326 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1335 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1340 if (--bmap
->ref
> 0)
1343 isl_ctx_deref(bmap
->ctx
);
1345 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1347 isl_blk_free(bmap
->ctx
, bmap
->block
);
1348 isl_vec_free(bmap
->sample
);
1349 isl_space_free(bmap
->dim
);
1355 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1357 return isl_basic_map_free(bset_to_bmap(bset
));
1360 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1362 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1365 /* Check that "bset" does not involve any parameters.
1367 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1371 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1373 return isl_stat_error
;
1375 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1376 "basic set should not have any parameters",
1377 return isl_stat_error
);
1381 /* Check that "bset" does not involve any local variables.
1383 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1387 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1389 return isl_stat_error
;
1391 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1392 "basic set should not have any local variables",
1393 return isl_stat_error
);
1397 /* Check that "map" has only named parameters, reporting an error
1400 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1402 return isl_space_check_named_params(isl_map_peek_space(map
));
1405 /* Check that "bmap" has only named parameters, reporting an error
1408 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1410 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1413 /* Check that "bmap1" and "bmap2" have the same parameters,
1414 * reporting an error if they do not.
1416 static isl_stat
isl_basic_map_check_equal_params(
1417 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1421 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1423 return isl_stat_error
;
1425 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1426 "parameters don't match", return isl_stat_error
);
1430 __isl_give isl_map
*isl_map_align_params_map_map_and(
1431 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1432 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1433 __isl_take isl_map
*map2
))
1437 if (isl_map_has_equal_params(map1
, map2
))
1438 return fn(map1
, map2
);
1439 if (isl_map_check_named_params(map1
) < 0)
1441 if (isl_map_check_named_params(map2
) < 0)
1443 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1444 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1445 return fn(map1
, map2
);
1452 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1453 __isl_keep isl_map
*map2
,
1454 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1459 return isl_bool_error
;
1460 if (isl_map_has_equal_params(map1
, map2
))
1461 return fn(map1
, map2
);
1462 if (isl_map_check_named_params(map1
) < 0)
1463 return isl_bool_error
;
1464 if (isl_map_check_named_params(map2
) < 0)
1465 return isl_bool_error
;
1466 map1
= isl_map_copy(map1
);
1467 map2
= isl_map_copy(map2
);
1468 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1469 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1476 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1479 struct isl_ctx
*ctx
;
1481 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1485 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1486 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1488 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1489 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1490 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1491 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1492 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1494 int j
= isl_basic_map_alloc_inequality(bmap
);
1498 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1499 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1506 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1507 bmap
->extra
- bmap
->n_div
);
1508 return bmap
->n_eq
++;
1511 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1513 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1516 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1520 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1525 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1527 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1530 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1535 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1537 if (pos
!= bmap
->n_eq
- 1) {
1539 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1540 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1546 /* Turn inequality "pos" of "bmap" into an equality.
1548 * In particular, we move the inequality in front of the equalities
1549 * and move the last inequality in the position of the moved inequality.
1550 * Note that isl_tab_make_equalities_explicit depends on this particular
1551 * change in the ordering of the constraints.
1553 void isl_basic_map_inequality_to_equality(
1554 struct isl_basic_map
*bmap
, unsigned pos
)
1558 t
= bmap
->ineq
[pos
];
1559 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1560 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1565 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1566 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1567 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1568 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1571 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1573 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1576 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1579 struct isl_ctx
*ctx
;
1581 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1585 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1586 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1587 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1588 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1589 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1590 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1591 bmap
->extra
- bmap
->n_div
);
1592 return bmap
->n_ineq
++;
1595 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1597 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1600 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1604 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1609 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1611 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1614 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1619 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1621 if (pos
!= bmap
->n_ineq
- 1) {
1622 t
= bmap
->ineq
[pos
];
1623 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1624 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1625 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1631 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1633 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1636 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1643 empty
= isl_basic_map_plain_is_empty(bmap
);
1645 return isl_basic_map_free(bmap
);
1649 bmap
= isl_basic_map_cow(bmap
);
1650 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1651 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1653 return isl_basic_map_free(bmap
);
1654 k
= isl_basic_map_alloc_equality(bmap
);
1657 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1660 isl_basic_map_free(bmap
);
1664 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1667 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1670 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1676 bmap
= isl_basic_map_cow(bmap
);
1677 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1678 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1680 return isl_basic_map_free(bmap
);
1681 k
= isl_basic_map_alloc_inequality(bmap
);
1684 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1687 isl_basic_map_free(bmap
);
1691 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1694 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1697 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1701 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1704 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1705 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1706 bmap
->extra
- bmap
->n_div
);
1707 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1708 return bmap
->n_div
++;
1711 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1713 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1717 #define TYPE isl_basic_map
1718 #include "check_type_range_templ.c"
1720 /* Check that there are "n" dimensions of type "type" starting at "first"
1723 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1724 enum isl_dim_type type
, unsigned first
, unsigned n
)
1726 return isl_basic_map_check_range(bset_to_bmap(bset
),
1730 /* Insert an extra integer division, prescribed by "div", to "bmap"
1731 * at (integer division) position "pos".
1733 * The integer division is first added at the end and then moved
1734 * into the right position.
1736 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1737 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1742 bmap
= isl_basic_map_cow(bmap
);
1743 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1744 if (total
< 0 || !div
)
1745 return isl_basic_map_free(bmap
);
1747 if (div
->size
!= 1 + 1 + total
)
1748 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1749 "unexpected size", return isl_basic_map_free(bmap
));
1750 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1751 return isl_basic_map_free(bmap
);
1753 bmap
= isl_basic_map_extend_space(bmap
,
1754 isl_basic_map_get_space(bmap
), 1, 0, 2);
1755 k
= isl_basic_map_alloc_div(bmap
);
1757 return isl_basic_map_free(bmap
);
1758 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1759 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1761 for (i
= k
; i
> pos
; --i
)
1762 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1767 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1770 return isl_stat_error
;
1771 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1776 static __isl_give isl_basic_map
*add_constraints(
1777 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1778 unsigned i_pos
, unsigned o_pos
)
1780 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1781 unsigned o_in
, o_out
;
1784 struct isl_dim_map
*dim_map
;
1786 space
= isl_basic_map_peek_space(bmap2
);
1787 if (!bmap1
|| !space
)
1790 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1791 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1792 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1793 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1794 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1795 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1796 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1797 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1799 ctx
= isl_basic_map_get_ctx(bmap1
);
1800 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1801 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1802 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1803 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1804 isl_dim_map_div(dim_map
, bmap2
, total
);
1806 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1808 isl_basic_map_free(bmap1
);
1809 isl_basic_map_free(bmap2
);
1813 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1814 struct isl_basic_set
*bset2
, unsigned pos
)
1816 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1817 bset_to_bmap(bset2
), 0, pos
));
1820 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1821 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1822 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1824 struct isl_basic_map
*ext
;
1834 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1835 base
->extra
>= base
->n_div
+ extra
;
1837 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1838 room_for_ineq(base
, n_ineq
)) {
1839 isl_space_free(space
);
1843 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1844 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1845 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1846 extra
+= base
->extra
;
1848 n_ineq
+= base
->n_ineq
;
1850 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1856 ext
->sample
= isl_vec_copy(base
->sample
);
1857 flags
= base
->flags
;
1858 ext
= add_constraints(ext
, base
, 0, 0);
1861 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1867 isl_space_free(space
);
1868 isl_basic_map_free(base
);
1872 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1873 __isl_take isl_basic_set
*base
,
1874 __isl_take isl_space
*dim
, unsigned extra
,
1875 unsigned n_eq
, unsigned n_ineq
)
1877 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1878 dim
, extra
, n_eq
, n_ineq
));
1881 struct isl_basic_map
*isl_basic_map_extend_constraints(
1882 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1886 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1890 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1891 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1892 unsigned n_eq
, unsigned n_ineq
)
1894 struct isl_basic_map
*bmap
;
1899 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1903 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1906 isl_basic_map_free(base
);
1910 struct isl_basic_set
*isl_basic_set_extend_constraints(
1911 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1913 isl_basic_map
*bmap
= bset_to_bmap(base
);
1914 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1915 return bset_from_bmap(bmap
);
1918 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1920 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1923 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1928 if (bmap
->ref
> 1) {
1930 bmap
= isl_basic_map_dup(bmap
);
1933 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1934 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1939 /* Clear all cached information in "map", either because it is about
1940 * to be modified or because it is being freed.
1941 * Always return the same pointer that is passed in.
1942 * This is needed for the use in isl_map_free.
1944 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1946 isl_basic_map_free(map
->cached_simple_hull
[0]);
1947 isl_basic_map_free(map
->cached_simple_hull
[1]);
1948 map
->cached_simple_hull
[0] = NULL
;
1949 map
->cached_simple_hull
[1] = NULL
;
1953 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1955 return isl_map_cow(set
);
1958 /* Return an isl_map that is equal to "map" and that has only
1959 * a single reference.
1961 * If the original input already has only one reference, then
1962 * simply return it, but clear all cached information, since
1963 * it may be rendered invalid by the operations that will be
1964 * performed on the result.
1966 * Otherwise, create a duplicate (without any cached information).
1968 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1974 return clear_caches(map
);
1976 return isl_map_dup(map
);
1979 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1980 unsigned a_len
, unsigned b_len
)
1982 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1983 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1984 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1987 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1988 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1993 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
1996 if (n1
== 0 || n2
== 0)
1999 bmap
= isl_basic_map_cow(bmap
);
2003 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2004 if (isl_blk_is_error(blk
))
2007 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2009 bmap
->eq
[i
] + pos
, n1
, n2
);
2011 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2013 bmap
->ineq
[i
] + pos
, n1
, n2
);
2015 for (i
= 0; i
< bmap
->n_div
; ++i
)
2017 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2019 isl_blk_free(bmap
->ctx
, blk
);
2021 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2022 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2023 return isl_basic_map_finalize(bmap
);
2025 isl_basic_map_free(bmap
);
2029 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2030 __isl_take isl_basic_map
*bmap
)
2035 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2037 return isl_basic_map_free(bmap
);
2038 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2039 return isl_basic_map_free(bmap
);
2040 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2042 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2044 i
= isl_basic_map_alloc_equality(bmap
);
2048 isl_int_set_si(bmap
->eq
[i
][0], 1);
2049 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2050 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2051 isl_vec_free(bmap
->sample
);
2052 bmap
->sample
= NULL
;
2053 return isl_basic_map_finalize(bmap
);
2055 isl_basic_map_free(bmap
);
2059 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2060 __isl_take isl_basic_set
*bset
)
2062 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2065 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2066 __isl_take isl_basic_map
*bmap
)
2071 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2074 bmap
= isl_basic_map_cow(bmap
);
2078 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2080 return isl_basic_map_finalize(bmap
);
2083 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2084 __isl_take isl_basic_set
*bset
)
2086 return isl_basic_map_set_rational(bset
);
2089 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2090 __isl_take isl_basic_set
*bset
)
2095 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2098 bset
= isl_basic_set_cow(bset
);
2102 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2104 return isl_basic_set_finalize(bset
);
2107 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2111 map
= isl_map_cow(map
);
2114 for (i
= 0; i
< map
->n
; ++i
) {
2115 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2125 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2127 return isl_map_set_rational(set
);
2130 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2133 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2135 isl_int
*t
= bmap
->div
[a
];
2136 bmap
->div
[a
] = bmap
->div
[b
];
2140 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2141 * div definitions accordingly.
2143 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2149 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2151 return isl_basic_map_free(bmap
);
2153 swap_div(bmap
, a
, b
);
2155 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2156 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2158 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2159 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2161 for (i
= 0; i
< bmap
->n_div
; ++i
)
2162 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2163 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2168 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2170 isl_seq_cpy(c
, c
+ n
, rem
);
2171 isl_seq_clr(c
+ rem
, n
);
2174 /* Drop n dimensions starting at first.
2176 * In principle, this frees up some extra variables as the number
2177 * of columns remains constant, but we would have to extend
2178 * the div array too as the number of rows in this array is assumed
2179 * to be equal to extra.
2181 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2182 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2184 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2187 /* Move "n" divs starting at "first" to the end of the list of divs.
2189 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2190 unsigned first
, unsigned n
)
2195 if (first
+ n
== bmap
->n_div
)
2198 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2201 for (i
= 0; i
< n
; ++i
)
2202 div
[i
] = bmap
->div
[first
+ i
];
2203 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2204 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2205 for (i
= 0; i
< n
; ++i
)
2206 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2210 isl_basic_map_free(bmap
);
2215 #define TYPE isl_map
2217 #include "check_type_range_templ.c"
2219 /* Check that there are "n" dimensions of type "type" starting at "first"
2222 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2223 enum isl_dim_type type
, unsigned first
, unsigned n
)
2225 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2228 /* Drop "n" dimensions of type "type" starting at "first".
2229 * Perform the core computation, without cowing or
2230 * simplifying and finalizing the result.
2232 * In principle, this frees up some extra variables as the number
2233 * of columns remains constant, but we would have to extend
2234 * the div array too as the number of rows in this array is assumed
2235 * to be equal to extra.
2237 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2238 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2239 unsigned first
, unsigned n
)
2246 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2247 return isl_basic_map_free(bmap
);
2249 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2251 return isl_basic_map_free(bmap
);
2253 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2254 left
= total
- (offset
- 1) - n
;
2255 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2256 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2258 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2259 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2261 for (i
= 0; i
< bmap
->n_div
; ++i
)
2262 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2264 if (type
== isl_dim_div
) {
2265 bmap
= move_divs_last(bmap
, first
, n
);
2268 if (isl_basic_map_free_div(bmap
, n
) < 0)
2269 return isl_basic_map_free(bmap
);
2271 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2273 return isl_basic_map_free(bmap
);
2275 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2276 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2280 /* Drop "n" dimensions of type "type" starting at "first".
2282 * In principle, this frees up some extra variables as the number
2283 * of columns remains constant, but we would have to extend
2284 * the div array too as the number of rows in this array is assumed
2285 * to be equal to extra.
2287 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2288 enum isl_dim_type type
, unsigned first
, unsigned n
)
2292 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2295 bmap
= isl_basic_map_cow(bmap
);
2299 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2301 bmap
= isl_basic_map_simplify(bmap
);
2302 return isl_basic_map_finalize(bmap
);
2305 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2306 enum isl_dim_type type
, unsigned first
, unsigned n
)
2308 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2312 /* No longer consider "map" to be normalized.
2314 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2318 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2322 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2323 enum isl_dim_type type
, unsigned first
, unsigned n
)
2327 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2328 return isl_map_free(map
);
2330 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2332 map
= isl_map_cow(map
);
2335 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2339 for (i
= 0; i
< map
->n
; ++i
) {
2340 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2344 map
= isl_map_unmark_normalized(map
);
2352 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2353 enum isl_dim_type type
, unsigned first
, unsigned n
)
2355 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2358 /* Drop the integer division at position "div", which is assumed
2359 * not to appear in any of the constraints or
2360 * in any of the other integer divisions.
2362 * Since the integer division is redundant, there is no need to cow.
2364 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2365 __isl_take isl_basic_map
*bmap
, unsigned div
)
2367 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2370 /* Eliminate the specified n dimensions starting at first from the
2371 * constraints, without removing the dimensions from the space.
2372 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2374 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2375 enum isl_dim_type type
, unsigned first
, unsigned n
)
2382 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2383 return isl_map_free(map
);
2385 map
= isl_map_cow(map
);
2389 for (i
= 0; i
< map
->n
; ++i
) {
2390 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2400 /* Eliminate the specified n dimensions starting at first from the
2401 * constraints, without removing the dimensions from the space.
2402 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2404 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2405 enum isl_dim_type type
, unsigned first
, unsigned n
)
2407 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2410 /* Eliminate the specified n dimensions starting at first from the
2411 * constraints, without removing the dimensions from the space.
2412 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2414 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2415 unsigned first
, unsigned n
)
2417 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2420 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2421 __isl_take isl_basic_map
*bmap
)
2425 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2427 return isl_basic_map_free(bmap
);
2428 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2432 return isl_basic_map_finalize(bmap
);
2435 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2436 __isl_take isl_basic_set
*bset
)
2438 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2441 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2450 map
= isl_map_cow(map
);
2454 for (i
= 0; i
< map
->n
; ++i
) {
2455 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2465 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2467 return isl_map_remove_divs(set
);
2470 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2471 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2472 unsigned first
, unsigned n
)
2474 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2475 return isl_basic_map_free(bmap
);
2476 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2478 bmap
= isl_basic_map_eliminate_vars(bmap
,
2479 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2482 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2484 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2488 /* Return true if the definition of the given div (recursively) involves
2489 * any of the given variables.
2491 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2492 unsigned first
, unsigned n
)
2495 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2497 if (isl_int_is_zero(bmap
->div
[div
][0]))
2498 return isl_bool_false
;
2499 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2500 return isl_bool_true
;
2502 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2505 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2507 involves
= div_involves_vars(bmap
, i
, first
, n
);
2508 if (involves
< 0 || involves
)
2512 return isl_bool_false
;
2515 /* Try and add a lower and/or upper bound on "div" to "bmap"
2516 * based on inequality "i".
2517 * "total" is the total number of variables (excluding the divs).
2518 * "v" is a temporary object that can be used during the calculations.
2519 * If "lb" is set, then a lower bound should be constructed.
2520 * If "ub" is set, then an upper bound should be constructed.
2522 * The calling function has already checked that the inequality does not
2523 * reference "div", but we still need to check that the inequality is
2524 * of the right form. We'll consider the case where we want to construct
2525 * a lower bound. The construction of upper bounds is similar.
2527 * Let "div" be of the form
2529 * q = floor((a + f(x))/d)
2531 * We essentially check if constraint "i" is of the form
2535 * so that we can use it to derive a lower bound on "div".
2536 * However, we allow a slightly more general form
2540 * with the condition that the coefficients of g(x) - f(x) are all
2542 * Rewriting this constraint as
2546 * adding a + f(x) to both sides and dividing by d, we obtain
2548 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2550 * Taking the floor on both sides, we obtain
2552 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2556 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2558 * In the case of an upper bound, we construct the constraint
2560 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2563 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2564 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2565 unsigned total
, isl_int v
, int lb
, int ub
)
2569 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2571 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2572 bmap
->div
[div
][1 + 1 + j
]);
2573 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2576 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2577 bmap
->div
[div
][1 + 1 + j
]);
2578 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2584 bmap
= isl_basic_map_cow(bmap
);
2585 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2587 int k
= isl_basic_map_alloc_inequality(bmap
);
2590 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2591 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2592 bmap
->div
[div
][1 + j
]);
2593 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2594 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2596 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2599 int k
= isl_basic_map_alloc_inequality(bmap
);
2602 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2603 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2604 bmap
->div
[div
][1 + j
]);
2605 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2606 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2608 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2613 isl_basic_map_free(bmap
);
2617 /* This function is called right before "div" is eliminated from "bmap"
2618 * using Fourier-Motzkin.
2619 * Look through the constraints of "bmap" for constraints on the argument
2620 * of the integer division and use them to construct constraints on the
2621 * integer division itself. These constraints can then be combined
2622 * during the Fourier-Motzkin elimination.
2623 * Note that it is only useful to introduce lower bounds on "div"
2624 * if "bmap" already contains upper bounds on "div" as the newly
2625 * introduce lower bounds can then be combined with the pre-existing
2626 * upper bounds. Similarly for upper bounds.
2627 * We therefore first check if "bmap" contains any lower and/or upper bounds
2630 * It is interesting to note that the introduction of these constraints
2631 * can indeed lead to more accurate results, even when compared to
2632 * deriving constraints on the argument of "div" from constraints on "div".
2633 * Consider, for example, the set
2635 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2637 * The second constraint can be rewritten as
2639 * 2 * [(-i-2j+3)/4] + k >= 0
2641 * from which we can derive
2643 * -i - 2j + 3 >= -2k
2649 * Combined with the first constraint, we obtain
2651 * -3 <= 3 + 2k or k >= -3
2653 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2654 * the first constraint, we obtain
2656 * [(i + 2j)/4] >= [-3/4] = -1
2658 * Combining this constraint with the second constraint, we obtain
2662 static __isl_give isl_basic_map
*insert_bounds_on_div(
2663 __isl_take isl_basic_map
*bmap
, int div
)
2666 int check_lb
, check_ub
;
2673 if (isl_int_is_zero(bmap
->div
[div
][0]))
2676 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2678 return isl_basic_map_free(bmap
);
2682 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2683 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2690 if (!check_lb
&& !check_ub
)
2695 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2696 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2699 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2700 check_lb
, check_ub
);
2708 /* Remove all divs (recursively) involving any of the given dimensions
2709 * in their definitions.
2711 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2712 __isl_take isl_basic_map
*bmap
,
2713 enum isl_dim_type type
, unsigned first
, unsigned n
)
2717 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2718 return isl_basic_map_free(bmap
);
2719 first
+= isl_basic_map_offset(bmap
, type
);
2721 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2724 involves
= div_involves_vars(bmap
, i
, first
, n
);
2726 return isl_basic_map_free(bmap
);
2729 bmap
= insert_bounds_on_div(bmap
, i
);
2730 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2739 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2740 __isl_take isl_basic_set
*bset
,
2741 enum isl_dim_type type
, unsigned first
, unsigned n
)
2743 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2746 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2747 enum isl_dim_type type
, unsigned first
, unsigned n
)
2756 map
= isl_map_cow(map
);
2760 for (i
= 0; i
< map
->n
; ++i
) {
2761 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2772 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2773 enum isl_dim_type type
, unsigned first
, unsigned n
)
2775 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2779 /* Does the description of "bmap" depend on the specified dimensions?
2780 * We also check whether the dimensions appear in any of the div definitions.
2781 * In principle there is no need for this check. If the dimensions appear
2782 * in a div definition, they also appear in the defining constraints of that
2785 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2786 enum isl_dim_type type
, unsigned first
, unsigned n
)
2790 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2791 return isl_bool_error
;
2793 first
+= isl_basic_map_offset(bmap
, type
);
2794 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2795 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2796 return isl_bool_true
;
2797 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2798 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2799 return isl_bool_true
;
2800 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2801 if (isl_int_is_zero(bmap
->div
[i
][0]))
2803 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2804 return isl_bool_true
;
2807 return isl_bool_false
;
2810 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2811 enum isl_dim_type type
, unsigned first
, unsigned n
)
2815 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2816 return isl_bool_error
;
2818 for (i
= 0; i
< map
->n
; ++i
) {
2819 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2821 if (involves
< 0 || involves
)
2825 return isl_bool_false
;
2828 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2829 enum isl_dim_type type
, unsigned first
, unsigned n
)
2831 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2834 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2835 enum isl_dim_type type
, unsigned first
, unsigned n
)
2837 return isl_map_involves_dims(set
, type
, first
, n
);
2840 /* Drop all constraints in bmap that involve any of the dimensions
2841 * first to first+n-1.
2842 * This function only performs the actual removal of constraints.
2844 * This function should not call finalize since it is used by
2845 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2847 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2848 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2855 bmap
= isl_basic_map_cow(bmap
);
2860 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2861 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2863 isl_basic_map_drop_equality(bmap
, i
);
2866 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2867 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2869 isl_basic_map_drop_inequality(bmap
, i
);
2875 /* Drop all constraints in bset that involve any of the dimensions
2876 * first to first+n-1.
2877 * This function only performs the actual removal of constraints.
2879 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2880 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2882 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2885 /* Drop all constraints in bmap that do not involve any of the dimensions
2886 * first to first + n - 1 of the given type.
2888 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2889 __isl_take isl_basic_map
*bmap
,
2890 enum isl_dim_type type
, unsigned first
, unsigned n
)
2895 isl_space
*space
= isl_basic_map_get_space(bmap
);
2896 isl_basic_map_free(bmap
);
2897 return isl_basic_map_universe(space
);
2899 bmap
= isl_basic_map_cow(bmap
);
2903 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2904 return isl_basic_map_free(bmap
);
2906 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2908 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2909 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2911 isl_basic_map_drop_equality(bmap
, i
);
2914 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2915 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2917 isl_basic_map_drop_inequality(bmap
, i
);
2920 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2924 /* Drop all constraints in bset that do not involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2928 __isl_take isl_basic_set
*bset
,
2929 enum isl_dim_type type
, unsigned first
, unsigned n
)
2931 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2935 /* Drop all constraints in bmap that involve any of the dimensions
2936 * first to first + n - 1 of the given type.
2938 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2939 __isl_take isl_basic_map
*bmap
,
2940 enum isl_dim_type type
, unsigned first
, unsigned n
)
2947 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2948 return isl_basic_map_free(bmap
);
2950 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2951 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2952 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2953 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2957 /* Drop all constraints in bset that involve any of the dimensions
2958 * first to first + n - 1 of the given type.
2960 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2961 __isl_take isl_basic_set
*bset
,
2962 enum isl_dim_type type
, unsigned first
, unsigned n
)
2964 return isl_basic_map_drop_constraints_involving_dims(bset
,
2968 /* Drop constraints from "map" by applying "drop" to each basic map.
2970 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2971 enum isl_dim_type type
, unsigned first
, unsigned n
,
2972 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2973 enum isl_dim_type type
, unsigned first
, unsigned n
))
2977 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2978 return isl_map_free(map
);
2980 map
= isl_map_cow(map
);
2984 for (i
= 0; i
< map
->n
; ++i
) {
2985 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2987 return isl_map_free(map
);
2991 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2996 /* Drop all constraints in map that involve any of the dimensions
2997 * first to first + n - 1 of the given type.
2999 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3000 __isl_take isl_map
*map
,
3001 enum isl_dim_type type
, unsigned first
, unsigned n
)
3005 return drop_constraints(map
, type
, first
, n
,
3006 &isl_basic_map_drop_constraints_involving_dims
);
3009 /* Drop all constraints in "map" that do not involve any of the dimensions
3010 * first to first + n - 1 of the given type.
3012 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3013 __isl_take isl_map
*map
,
3014 enum isl_dim_type type
, unsigned first
, unsigned n
)
3017 isl_space
*space
= isl_map_get_space(map
);
3019 return isl_map_universe(space
);
3021 return drop_constraints(map
, type
, first
, n
,
3022 &isl_basic_map_drop_constraints_not_involving_dims
);
3025 /* Drop all constraints in set that involve any of the dimensions
3026 * first to first + n - 1 of the given type.
3028 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3029 __isl_take isl_set
*set
,
3030 enum isl_dim_type type
, unsigned first
, unsigned n
)
3032 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3035 /* Drop all constraints in "set" that do not involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3039 __isl_take isl_set
*set
,
3040 enum isl_dim_type type
, unsigned first
, unsigned n
)
3042 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3045 /* Does local variable "div" of "bmap" have a complete explicit representation?
3046 * Having a complete explicit representation requires not only
3047 * an explicit representation, but also that all local variables
3048 * that appear in this explicit representation in turn have
3049 * a complete explicit representation.
3051 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3054 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3057 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3058 if (marked
< 0 || marked
)
3059 return isl_bool_not(marked
);
3061 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3064 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3066 known
= isl_basic_map_div_is_known(bmap
, i
);
3067 if (known
< 0 || !known
)
3071 return isl_bool_true
;
3074 /* Remove all divs that are unknown or defined in terms of unknown divs.
3076 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3077 __isl_take isl_basic_map
*bmap
)
3084 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3085 if (isl_basic_map_div_is_known(bmap
, i
))
3087 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3096 /* Remove all divs that are unknown or defined in terms of unknown divs.
3098 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3099 __isl_take isl_basic_set
*bset
)
3101 return isl_basic_map_remove_unknown_divs(bset
);
3104 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3113 map
= isl_map_cow(map
);
3117 for (i
= 0; i
< map
->n
; ++i
) {
3118 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3128 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3130 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3133 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3134 __isl_take isl_basic_set
*bset
,
3135 enum isl_dim_type type
, unsigned first
, unsigned n
)
3137 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3138 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3139 return bset_from_bmap(bmap
);
3142 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3143 enum isl_dim_type type
, unsigned first
, unsigned n
)
3150 map
= isl_map_cow(map
);
3151 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3152 return isl_map_free(map
);
3154 for (i
= 0; i
< map
->n
; ++i
) {
3155 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3156 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3160 map
= isl_map_drop(map
, type
, first
, n
);
3167 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3168 enum isl_dim_type type
, unsigned first
, unsigned n
)
3170 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3174 /* Project out n inputs starting at first using Fourier-Motzkin */
3175 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3176 unsigned first
, unsigned n
)
3178 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3181 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3182 FILE *out
, int indent
)
3187 fprintf(out
, "null basic set\n");
3191 fprintf(out
, "%*s", indent
, "");
3192 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3193 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3194 bset
->extra
, bset
->flags
);
3196 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3197 p
= isl_printer_set_dump(p
, 1);
3198 p
= isl_printer_set_indent(p
, indent
);
3199 p
= isl_printer_start_line(p
);
3200 p
= isl_printer_print_basic_set(p
, bset
);
3201 p
= isl_printer_end_line(p
);
3202 isl_printer_free(p
);
3205 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3206 FILE *out
, int indent
)
3211 fprintf(out
, "null basic map\n");
3215 fprintf(out
, "%*s", indent
, "");
3216 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3217 "flags: %x, n_name: %d\n",
3219 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3220 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3222 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3223 p
= isl_printer_set_dump(p
, 1);
3224 p
= isl_printer_set_indent(p
, indent
);
3225 p
= isl_printer_start_line(p
);
3226 p
= isl_printer_print_basic_map(p
, bmap
);
3227 p
= isl_printer_end_line(p
);
3228 isl_printer_free(p
);
3231 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3236 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3238 return isl_basic_map_free(bmap
);
3239 if (pos
>= bmap
->n_ineq
)
3240 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3241 "invalid position", return isl_basic_map_free(bmap
));
3242 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3243 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3244 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3245 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3249 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3252 if (isl_space_check_is_set(space
) < 0)
3254 return isl_map_alloc_space(space
, n
, flags
);
3256 isl_space_free(space
);
3260 /* Make sure "map" has room for at least "n" more basic maps.
3262 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3265 struct isl_map
*grown
= NULL
;
3269 isl_assert(map
->ctx
, n
>= 0, goto error
);
3270 if (map
->n
+ n
<= map
->size
)
3272 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3275 for (i
= 0; i
< map
->n
; ++i
) {
3276 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3284 isl_map_free(grown
);
3289 /* Make sure "set" has room for at least "n" more basic sets.
3291 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3293 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3296 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3298 return isl_map_from_basic_map(bset
);
3301 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3303 struct isl_map
*map
;
3308 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3309 return isl_map_add_basic_map(map
, bmap
);
3312 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3313 __isl_take isl_basic_set
*bset
)
3315 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3316 bset_to_bmap(bset
)));
3319 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3321 return isl_map_free(set
);
3324 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3329 fprintf(out
, "null set\n");
3333 fprintf(out
, "%*s", indent
, "");
3334 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3335 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3337 for (i
= 0; i
< set
->n
; ++i
) {
3338 fprintf(out
, "%*s", indent
, "");
3339 fprintf(out
, "basic set %d:\n", i
);
3340 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3344 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3349 fprintf(out
, "null map\n");
3353 fprintf(out
, "%*s", indent
, "");
3354 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3355 "flags: %x, n_name: %d\n",
3356 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3357 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3358 for (i
= 0; i
< map
->n
; ++i
) {
3359 fprintf(out
, "%*s", indent
, "");
3360 fprintf(out
, "basic map %d:\n", i
);
3361 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3365 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3366 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3368 struct isl_basic_map
*bmap_domain
;
3371 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3374 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3378 isl_assert(bset
->ctx
,
3379 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3381 bmap
= isl_basic_map_cow(bmap
);
3384 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3385 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3386 bmap_domain
= isl_basic_map_from_domain(bset
);
3387 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3389 bmap
= isl_basic_map_simplify(bmap
);
3390 return isl_basic_map_finalize(bmap
);
3392 isl_basic_map_free(bmap
);
3393 isl_basic_set_free(bset
);
3397 /* Check that the space of "bset" is the same as that of the range of "bmap".
3399 static isl_stat
isl_basic_map_check_compatible_range(
3400 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3404 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3406 return isl_stat_error
;
3408 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3409 "incompatible spaces", return isl_stat_error
);
3414 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3415 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3417 struct isl_basic_map
*bmap_range
;
3420 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3423 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3426 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3429 if (isl_basic_set_plain_is_universe(bset
)) {
3430 isl_basic_set_free(bset
);
3434 bmap
= isl_basic_map_cow(bmap
);
3437 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3438 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3439 bmap_range
= bset_to_bmap(bset
);
3440 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3442 bmap
= isl_basic_map_simplify(bmap
);
3443 return isl_basic_map_finalize(bmap
);
3445 isl_basic_map_free(bmap
);
3446 isl_basic_set_free(bset
);
3450 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3451 __isl_keep isl_vec
*vec
)
3457 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3458 if (total
< 0 || !vec
)
3459 return isl_bool_error
;
3461 if (1 + total
!= vec
->size
)
3462 return isl_bool_false
;
3466 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3467 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3468 if (!isl_int_is_zero(s
)) {
3470 return isl_bool_false
;
3474 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3475 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3476 if (isl_int_is_neg(s
)) {
3478 return isl_bool_false
;
3484 return isl_bool_true
;
3487 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3488 __isl_keep isl_vec
*vec
)
3490 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3493 __isl_give isl_basic_map
*isl_basic_map_intersect(
3494 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3496 struct isl_vec
*sample
= NULL
;
3497 isl_space
*space1
, *space2
;
3498 isl_size dim1
, dim2
, nparam1
, nparam2
;
3500 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3502 space1
= isl_basic_map_peek_space(bmap1
);
3503 space2
= isl_basic_map_peek_space(bmap2
);
3504 dim1
= isl_space_dim(space1
, isl_dim_all
);
3505 dim2
= isl_space_dim(space2
, isl_dim_all
);
3506 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3507 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3508 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3510 if (dim1
== nparam1
&& dim2
!= nparam2
)
3511 return isl_basic_map_intersect(bmap2
, bmap1
);
3513 if (dim2
!= nparam2
)
3514 isl_assert(bmap1
->ctx
,
3515 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3517 if (isl_basic_map_plain_is_empty(bmap1
)) {
3518 isl_basic_map_free(bmap2
);
3521 if (isl_basic_map_plain_is_empty(bmap2
)) {
3522 isl_basic_map_free(bmap1
);
3526 if (bmap1
->sample
&&
3527 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3528 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3529 sample
= isl_vec_copy(bmap1
->sample
);
3530 else if (bmap2
->sample
&&
3531 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3532 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3533 sample
= isl_vec_copy(bmap2
->sample
);
3535 bmap1
= isl_basic_map_cow(bmap1
);
3538 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3539 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3540 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3543 isl_vec_free(sample
);
3545 isl_vec_free(bmap1
->sample
);
3546 bmap1
->sample
= sample
;
3549 bmap1
= isl_basic_map_simplify(bmap1
);
3550 return isl_basic_map_finalize(bmap1
);
3553 isl_vec_free(sample
);
3554 isl_basic_map_free(bmap1
);
3555 isl_basic_map_free(bmap2
);
3559 struct isl_basic_set
*isl_basic_set_intersect(
3560 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3562 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3563 bset_to_bmap(bset2
)));
3566 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3567 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3569 return isl_basic_set_intersect(bset1
, bset2
);
3572 /* Special case of isl_map_intersect, where both map1 and map2
3573 * are convex, without any divs and such that either map1 or map2
3574 * contains a single constraint. This constraint is then simply
3575 * added to the other map.
3577 static __isl_give isl_map
*map_intersect_add_constraint(
3578 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3580 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3581 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3582 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3583 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3585 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3586 return isl_map_intersect(map2
, map1
);
3588 map1
= isl_map_cow(map1
);
3591 if (isl_map_plain_is_empty(map1
)) {
3595 if (map2
->p
[0]->n_eq
== 1)
3596 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3598 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3599 map2
->p
[0]->ineq
[0]);
3601 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3602 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3606 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3607 isl_basic_map_free(map1
->p
[0]);
3613 map1
= isl_map_unmark_normalized(map1
);
3621 /* map2 may be either a parameter domain or a map living in the same
3624 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3625 __isl_take isl_map
*map2
)
3631 isl_size dim2
, nparam2
;
3636 if ((isl_map_plain_is_empty(map1
) ||
3637 isl_map_plain_is_universe(map2
)) &&
3638 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3642 if ((isl_map_plain_is_empty(map2
) ||
3643 isl_map_plain_is_universe(map1
)) &&
3644 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3649 if (map1
->n
== 1 && map2
->n
== 1 &&
3650 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3651 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3652 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3653 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3654 return map_intersect_add_constraint(map1
, map2
);
3656 equal
= isl_map_plain_is_equal(map1
, map2
);
3664 dim2
= isl_map_dim(map2
, isl_dim_all
);
3665 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3666 if (dim2
< 0 || nparam2
< 0)
3668 if (dim2
!= nparam2
)
3669 isl_assert(map1
->ctx
,
3670 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3672 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3673 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3674 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3676 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3677 map1
->n
* map2
->n
, flags
);
3680 for (i
= 0; i
< map1
->n
; ++i
)
3681 for (j
= 0; j
< map2
->n
; ++j
) {
3682 struct isl_basic_map
*part
;
3683 part
= isl_basic_map_intersect(
3684 isl_basic_map_copy(map1
->p
[i
]),
3685 isl_basic_map_copy(map2
->p
[j
]));
3686 if (isl_basic_map_is_empty(part
) < 0)
3687 part
= isl_basic_map_free(part
);
3688 result
= isl_map_add_basic_map(result
, part
);
3701 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3702 __isl_take isl_map
*map2
)
3706 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3707 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3708 "spaces don't match", goto error
);
3709 return map_intersect_internal(map1
, map2
);
3716 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3717 __isl_take isl_map
*map2
)
3719 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3722 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3724 return set_from_map(isl_map_intersect(set_to_map(set1
),
3728 /* map_intersect_internal accepts intersections
3729 * with parameter domains, so we can just call that function.
3731 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3732 __isl_take isl_set
*params
)
3734 return map_intersect_internal(map
, params
);
3737 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3738 __isl_take isl_map
*map2
)
3740 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3743 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3744 __isl_take isl_set
*params
)
3746 return isl_map_intersect_params(set
, params
);
3749 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3757 bmap
= isl_basic_map_cow(bmap
);
3760 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3761 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3762 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3763 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3764 if (n1
< 0 || n2
< 0)
3765 bmap
= isl_basic_map_free(bmap
);
3766 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3767 return isl_basic_map_reset_space(bmap
, space
);
3770 static __isl_give isl_basic_map
*basic_map_space_reset(
3771 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3777 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3780 space
= isl_basic_map_get_space(bmap
);
3781 space
= isl_space_reset(space
, type
);
3782 bmap
= isl_basic_map_reset_space(bmap
, space
);
3786 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3787 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3788 unsigned pos
, unsigned n
)
3790 isl_bool rational
, is_empty
;
3791 isl_space
*res_space
;
3792 struct isl_basic_map
*res
;
3793 struct isl_dim_map
*dim_map
;
3796 enum isl_dim_type t
;
3799 return basic_map_space_reset(bmap
, type
);
3801 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3802 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3803 if (is_empty
< 0 || total
< 0)
3804 return isl_basic_map_free(bmap
);
3805 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3808 return isl_basic_map_free(bmap
);
3810 isl_basic_map_free(bmap
);
3811 return isl_basic_map_empty(res_space
);
3814 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
3816 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3820 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3822 isl_size size
= isl_basic_map_dim(bmap
, t
);
3824 dim_map
= isl_dim_map_free(dim_map
);
3825 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3827 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3828 pos
, size
- pos
, off
+ pos
+ n
);
3830 dim
= isl_space_dim(res_space
, t
);
3832 dim_map
= isl_dim_map_free(dim_map
);
3835 isl_dim_map_div(dim_map
, bmap
, off
);
3837 res
= isl_basic_map_alloc_space(res_space
,
3838 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3839 rational
= isl_basic_map_is_rational(bmap
);
3841 res
= isl_basic_map_free(res
);
3843 res
= isl_basic_map_set_rational(res
);
3844 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3845 return isl_basic_map_finalize(res
);
3848 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3849 __isl_take isl_basic_set
*bset
,
3850 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3852 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3855 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3856 enum isl_dim_type type
, unsigned n
)
3860 dim
= isl_basic_map_dim(bmap
, type
);
3862 return isl_basic_map_free(bmap
);
3863 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
3866 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3867 enum isl_dim_type type
, unsigned n
)
3871 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3872 return isl_basic_map_add_dims(bset
, type
, n
);
3874 isl_basic_set_free(bset
);
3878 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3879 enum isl_dim_type type
)
3883 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3886 space
= isl_map_get_space(map
);
3887 space
= isl_space_reset(space
, type
);
3888 map
= isl_map_reset_space(map
, space
);
3892 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3893 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3898 return map_space_reset(map
, type
);
3900 map
= isl_map_cow(map
);
3904 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3908 for (i
= 0; i
< map
->n
; ++i
) {
3909 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3920 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3921 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3923 return isl_map_insert_dims(set
, type
, pos
, n
);
3926 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3927 enum isl_dim_type type
, unsigned n
)
3931 dim
= isl_map_dim(map
, type
);
3933 return isl_map_free(map
);
3934 return isl_map_insert_dims(map
, type
, dim
, n
);
3937 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3938 enum isl_dim_type type
, unsigned n
)
3942 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3943 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3949 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3950 __isl_take isl_basic_map
*bmap
,
3951 enum isl_dim_type dst_type
, unsigned dst_pos
,
3952 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3955 struct isl_dim_map
*dim_map
;
3956 struct isl_basic_map
*res
;
3957 enum isl_dim_type t
;
3964 bmap
= isl_basic_map_reset(bmap
, src_type
);
3965 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3969 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3970 return isl_basic_map_free(bmap
);
3972 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3975 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3977 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3978 pos(bmap
->dim
, src_type
) + src_pos
+
3979 ((src_type
< dst_type
) ? n
: 0)) {
3980 bmap
= isl_basic_map_cow(bmap
);
3984 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3985 src_type
, src_pos
, n
);
3989 bmap
= isl_basic_map_finalize(bmap
);
3994 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3996 return isl_basic_map_free(bmap
);
3997 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4000 space
= isl_basic_map_peek_space(bmap
);
4001 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4002 isl_size size
= isl_space_dim(space
, t
);
4004 dim_map
= isl_dim_map_free(dim_map
);
4005 if (t
== dst_type
) {
4006 isl_dim_map_dim_range(dim_map
, space
, t
,
4009 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4012 isl_dim_map_dim_range(dim_map
, space
, t
,
4013 dst_pos
, size
- dst_pos
, off
);
4014 off
+= size
- dst_pos
;
4015 } else if (t
== src_type
) {
4016 isl_dim_map_dim_range(dim_map
, space
, t
,
4019 isl_dim_map_dim_range(dim_map
, space
, t
,
4020 src_pos
+ n
, size
- src_pos
- n
, off
);
4021 off
+= size
- src_pos
- n
;
4023 isl_dim_map_dim(dim_map
, space
, t
, off
);
4027 isl_dim_map_div(dim_map
, bmap
, off
);
4029 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4030 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4031 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4035 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4036 src_type
, src_pos
, n
);
4040 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4041 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4042 bmap
= isl_basic_map_finalize(bmap
);
4046 isl_basic_map_free(bmap
);
4050 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4051 enum isl_dim_type dst_type
, unsigned dst_pos
,
4052 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4054 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4055 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4056 src_type
, src_pos
, n
);
4057 return bset_from_bmap(bmap
);
4060 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4061 enum isl_dim_type dst_type
, unsigned dst_pos
,
4062 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4066 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4067 return set_from_map(isl_map_move_dims(set_to_map(set
),
4068 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4074 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4075 enum isl_dim_type dst_type
, unsigned dst_pos
,
4076 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4081 map
= isl_map_reset(map
, src_type
);
4082 map
= isl_map_reset(map
, dst_type
);
4086 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4087 return isl_map_free(map
);
4089 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4092 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4094 map
= isl_map_cow(map
);
4098 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4102 for (i
= 0; i
< map
->n
; ++i
) {
4103 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4105 src_type
, src_pos
, n
);
4116 /* Move the specified dimensions to the last columns right before
4117 * the divs. Don't change the dimension specification of bmap.
4118 * That's the responsibility of the caller.
4120 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4121 enum isl_dim_type type
, unsigned first
, unsigned n
)
4124 struct isl_dim_map
*dim_map
;
4125 struct isl_basic_map
*res
;
4126 enum isl_dim_type t
;
4132 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4133 isl_basic_map_offset(bmap
, isl_dim_div
))
4136 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4138 return isl_basic_map_free(bmap
);
4139 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4142 space
= isl_basic_map_peek_space(bmap
);
4143 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4144 isl_size size
= isl_space_dim(space
, t
);
4146 dim_map
= isl_dim_map_free(dim_map
);
4148 isl_dim_map_dim_range(dim_map
, space
, t
,
4151 isl_dim_map_dim_range(dim_map
, space
, t
,
4152 first
, n
, total
- bmap
->n_div
- n
);
4153 isl_dim_map_dim_range(dim_map
, space
, t
,
4154 first
+ n
, size
- (first
+ n
), off
);
4155 off
+= size
- (first
+ n
);
4157 isl_dim_map_dim(dim_map
, space
, t
, off
);
4161 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4163 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4164 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4165 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4169 /* Insert "n" rows in the divs of "bmap".
4171 * The number of columns is not changed, which means that the last
4172 * dimensions of "bmap" are being reintepreted as the new divs.
4173 * The space of "bmap" is not adjusted, however, which means
4174 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4175 * from the space of "bmap" is the responsibility of the caller.
4177 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4185 bmap
= isl_basic_map_cow(bmap
);
4189 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4190 old
= bmap
->block2
.data
;
4191 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4192 (bmap
->extra
+ n
) * (1 + row_size
));
4193 if (!bmap
->block2
.data
)
4194 return isl_basic_map_free(bmap
);
4195 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4197 return isl_basic_map_free(bmap
);
4198 for (i
= 0; i
< n
; ++i
) {
4199 new_div
[i
] = bmap
->block2
.data
+
4200 (bmap
->extra
+ i
) * (1 + row_size
);
4201 isl_seq_clr(new_div
[i
], 1 + row_size
);
4203 for (i
= 0; i
< bmap
->extra
; ++i
)
4204 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4206 bmap
->div
= new_div
;
4213 /* Drop constraints from "bmap" that only involve the variables
4214 * of "type" in the range [first, first + n] that are not related
4215 * to any of the variables outside that interval.
4216 * These constraints cannot influence the values for the variables
4217 * outside the interval, except in case they cause "bmap" to be empty.
4218 * Only drop the constraints if "bmap" is known to be non-empty.
4220 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4221 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4222 unsigned first
, unsigned n
)
4226 isl_size dim
, n_div
;
4229 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4231 return isl_basic_map_free(bmap
);
4235 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4236 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4237 if (dim
< 0 || n_div
< 0)
4238 return isl_basic_map_free(bmap
);
4239 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4241 return isl_basic_map_free(bmap
);
4242 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4243 for (i
= 0; i
< first
; ++i
)
4245 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4248 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4253 /* Turn the n dimensions of type type, starting at first
4254 * into existentially quantified variables.
4256 * If a subset of the projected out variables are unrelated
4257 * to any of the variables that remain, then the constraints
4258 * involving this subset are simply dropped first.
4260 __isl_give isl_basic_map
*isl_basic_map_project_out(
4261 __isl_take isl_basic_map
*bmap
,
4262 enum isl_dim_type type
, unsigned first
, unsigned n
)
4267 return basic_map_space_reset(bmap
, type
);
4268 if (type
== isl_dim_div
)
4269 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4270 "cannot project out existentially quantified variables",
4271 return isl_basic_map_free(bmap
));
4273 empty
= isl_basic_map_plain_is_empty(bmap
);
4275 return isl_basic_map_free(bmap
);
4277 bmap
= isl_basic_map_set_to_empty(bmap
);
4279 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4283 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4284 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4286 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4287 return isl_basic_map_free(bmap
);
4289 bmap
= move_last(bmap
, type
, first
, n
);
4290 bmap
= isl_basic_map_cow(bmap
);
4291 bmap
= insert_div_rows(bmap
, n
);
4295 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4298 bmap
= isl_basic_map_simplify(bmap
);
4299 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4300 return isl_basic_map_finalize(bmap
);
4302 isl_basic_map_free(bmap
);
4306 /* Turn the n dimensions of type type, starting at first
4307 * into existentially quantified variables.
4309 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4310 enum isl_dim_type type
, unsigned first
, unsigned n
)
4312 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4316 /* Turn the n dimensions of type type, starting at first
4317 * into existentially quantified variables.
4319 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4320 enum isl_dim_type type
, unsigned first
, unsigned n
)
4325 return map_space_reset(map
, type
);
4327 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4328 return isl_map_free(map
);
4330 map
= isl_map_cow(map
);
4334 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4338 for (i
= 0; i
< map
->n
; ++i
) {
4339 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4350 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4351 * into existentially quantified variables.
4353 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4354 enum isl_dim_type type
, unsigned first
, unsigned n
)
4358 dim
= isl_map_dim(map
, type
);
4359 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4360 return isl_map_free(map
);
4361 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4362 map
= isl_map_project_out(map
, type
, 0, first
);
4366 /* Turn the n dimensions of type type, starting at first
4367 * into existentially quantified variables.
4369 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4370 enum isl_dim_type type
, unsigned first
, unsigned n
)
4372 return set_from_map(isl_map_project_out(set_to_map(set
),
4376 /* Return a map that projects the elements in "set" onto their
4377 * "n" set dimensions starting at "first".
4378 * "type" should be equal to isl_dim_set.
4380 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4381 enum isl_dim_type type
, unsigned first
, unsigned n
)
4386 if (type
!= isl_dim_set
)
4387 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4388 "only set dimensions can be projected out", goto error
);
4389 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4390 return isl_set_free(set
);
4392 map
= isl_map_from_domain(set
);
4393 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4394 for (i
= 0; i
< n
; ++i
)
4395 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4403 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4409 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4411 return isl_basic_map_free(bmap
);
4412 for (i
= 0; i
< n
; ++i
) {
4413 j
= isl_basic_map_alloc_div(bmap
);
4416 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4420 isl_basic_map_free(bmap
);
4424 struct isl_basic_map
*isl_basic_map_apply_range(
4425 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4427 isl_space
*space_result
= NULL
;
4428 struct isl_basic_map
*bmap
;
4429 isl_size n_in
, n_out
, n
, nparam
;
4430 unsigned total
, pos
;
4431 struct isl_dim_map
*dim_map1
, *dim_map2
;
4433 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4435 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4436 bmap2
->dim
, isl_dim_in
))
4437 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4438 "spaces don't match", goto error
);
4440 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4441 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4442 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4443 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4444 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4447 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4448 isl_basic_map_get_space(bmap2
));
4450 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4451 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4452 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4453 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4454 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4455 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4456 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4457 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4458 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4459 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4460 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4462 bmap
= isl_basic_map_alloc_space(space_result
,
4463 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4464 bmap1
->n_eq
+ bmap2
->n_eq
,
4465 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4466 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4467 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4468 bmap
= add_divs(bmap
, n
);
4469 bmap
= isl_basic_map_simplify(bmap
);
4470 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4471 return isl_basic_map_finalize(bmap
);
4473 isl_basic_map_free(bmap1
);
4474 isl_basic_map_free(bmap2
);
4478 struct isl_basic_set
*isl_basic_set_apply(
4479 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4484 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4487 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4490 isl_basic_set_free(bset
);
4491 isl_basic_map_free(bmap
);
4495 struct isl_basic_map
*isl_basic_map_apply_domain(
4496 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4498 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4500 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4501 bmap2
->dim
, isl_dim_in
))
4502 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4503 "spaces don't match", goto error
);
4505 bmap1
= isl_basic_map_reverse(bmap1
);
4506 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4507 return isl_basic_map_reverse(bmap1
);
4509 isl_basic_map_free(bmap1
);
4510 isl_basic_map_free(bmap2
);
4514 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4515 * A \cap B -> f(A) + f(B)
4517 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4518 __isl_take isl_basic_map
*bmap2
)
4520 isl_size n_in
, n_out
, nparam
;
4521 unsigned total
, pos
;
4522 struct isl_basic_map
*bmap
= NULL
;
4523 struct isl_dim_map
*dim_map1
, *dim_map2
;
4526 if (!bmap1
|| !bmap2
)
4529 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4532 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4533 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4534 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4535 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4538 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4539 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4540 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4541 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4542 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4543 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4544 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4545 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4546 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4547 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4548 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4550 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4551 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4552 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4553 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4554 for (i
= 0; i
< n_out
; ++i
) {
4555 int j
= isl_basic_map_alloc_equality(bmap
);
4558 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4559 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4560 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4561 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4563 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4564 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4565 bmap
= add_divs(bmap
, 2 * n_out
);
4567 bmap
= isl_basic_map_simplify(bmap
);
4568 return isl_basic_map_finalize(bmap
);
4570 isl_basic_map_free(bmap
);
4571 isl_basic_map_free(bmap1
);
4572 isl_basic_map_free(bmap2
);
4576 /* Given two maps A -> f(A) and B -> g(B), construct a map
4577 * A \cap B -> f(A) + f(B)
4579 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4580 __isl_take isl_map
*map2
)
4582 struct isl_map
*result
;
4588 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4590 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4591 map1
->n
* map2
->n
, 0);
4594 for (i
= 0; i
< map1
->n
; ++i
)
4595 for (j
= 0; j
< map2
->n
; ++j
) {
4596 struct isl_basic_map
*part
;
4597 part
= isl_basic_map_sum(
4598 isl_basic_map_copy(map1
->p
[i
]),
4599 isl_basic_map_copy(map2
->p
[j
]));
4600 if (isl_basic_map_is_empty(part
))
4601 isl_basic_map_free(part
);
4603 result
= isl_map_add_basic_map(result
, part
);
4616 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4617 __isl_take isl_set
*set2
)
4619 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4622 /* Given a basic map A -> f(A), construct A -> -f(A).
4624 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4630 bmap
= isl_basic_map_cow(bmap
);
4631 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4633 return isl_basic_map_free(bmap
);
4635 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4636 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4637 for (j
= 0; j
< n
; ++j
)
4638 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4639 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4640 for (j
= 0; j
< n
; ++j
)
4641 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4642 for (i
= 0; i
< bmap
->n_div
; ++i
)
4643 for (j
= 0; j
< n
; ++j
)
4644 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4645 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4646 return isl_basic_map_finalize(bmap
);
4649 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4651 return isl_basic_map_neg(bset
);
4654 /* Given a map A -> f(A), construct A -> -f(A).
4656 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4660 map
= isl_map_cow(map
);
4664 for (i
= 0; i
< map
->n
; ++i
) {
4665 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4676 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4678 return set_from_map(isl_map_neg(set_to_map(set
)));
4681 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4682 * A -> floor(f(A)/d).
4684 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4687 isl_size n_in
, n_out
, nparam
;
4688 unsigned total
, pos
;
4689 struct isl_basic_map
*result
= NULL
;
4690 struct isl_dim_map
*dim_map
;
4693 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4694 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4695 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4696 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4697 return isl_basic_map_free(bmap
);
4699 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4700 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4701 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4702 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4703 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4704 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4706 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4707 bmap
->n_div
+ n_out
,
4708 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4709 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4710 result
= add_divs(result
, n_out
);
4711 for (i
= 0; i
< n_out
; ++i
) {
4713 j
= isl_basic_map_alloc_inequality(result
);
4716 isl_seq_clr(result
->ineq
[j
], 1+total
);
4717 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4718 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4719 j
= isl_basic_map_alloc_inequality(result
);
4722 isl_seq_clr(result
->ineq
[j
], 1+total
);
4723 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4724 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4725 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4728 result
= isl_basic_map_simplify(result
);
4729 return isl_basic_map_finalize(result
);
4731 isl_basic_map_free(result
);
4735 /* Given a map A -> f(A) and an integer d, construct a map
4736 * A -> floor(f(A)/d).
4738 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4742 map
= isl_map_cow(map
);
4746 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4747 for (i
= 0; i
< map
->n
; ++i
) {
4748 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4752 map
= isl_map_unmark_normalized(map
);
4760 /* Given a map A -> f(A) and an integer d, construct a map
4761 * A -> floor(f(A)/d).
4763 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4764 __isl_take isl_val
*d
)
4768 if (!isl_val_is_int(d
))
4769 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4770 "expecting integer denominator", goto error
);
4771 map
= isl_map_floordiv(map
, d
->n
);
4780 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4788 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4789 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4790 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4791 if (total
< 0 || nparam
< 0 || n_in
< 0)
4792 return isl_basic_map_free(bmap
);
4793 i
= isl_basic_map_alloc_equality(bmap
);
4796 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
4797 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4798 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4799 return isl_basic_map_finalize(bmap
);
4801 isl_basic_map_free(bmap
);
4805 /* Add a constraint to "bmap" expressing i_pos < o_pos
4807 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4815 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4816 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4817 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4818 if (total
< 0 || nparam
< 0 || n_in
< 0)
4819 return isl_basic_map_free(bmap
);
4820 i
= isl_basic_map_alloc_inequality(bmap
);
4823 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4824 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4825 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4826 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4827 return isl_basic_map_finalize(bmap
);
4829 isl_basic_map_free(bmap
);
4833 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4835 static __isl_give isl_basic_map
*var_less_or_equal(
4836 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4843 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4844 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4845 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4846 if (total
< 0 || nparam
< 0 || n_in
< 0)
4847 return isl_basic_map_free(bmap
);
4848 i
= isl_basic_map_alloc_inequality(bmap
);
4851 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4852 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4853 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4854 return isl_basic_map_finalize(bmap
);
4856 isl_basic_map_free(bmap
);
4860 /* Add a constraint to "bmap" expressing i_pos > o_pos
4862 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4870 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4871 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4872 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4873 if (total
< 0 || nparam
< 0 || n_in
< 0)
4874 return isl_basic_map_free(bmap
);
4875 i
= isl_basic_map_alloc_inequality(bmap
);
4878 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4879 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4880 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4881 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4882 return isl_basic_map_finalize(bmap
);
4884 isl_basic_map_free(bmap
);
4888 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4890 static __isl_give isl_basic_map
*var_more_or_equal(
4891 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4898 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4899 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4900 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4901 if (total
< 0 || nparam
< 0 || n_in
< 0)
4902 return isl_basic_map_free(bmap
);
4903 i
= isl_basic_map_alloc_inequality(bmap
);
4906 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4907 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4908 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4909 return isl_basic_map_finalize(bmap
);
4911 isl_basic_map_free(bmap
);
4915 __isl_give isl_basic_map
*isl_basic_map_equal(
4916 __isl_take isl_space
*space
, unsigned n_equal
)
4919 struct isl_basic_map
*bmap
;
4920 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4923 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4924 bmap
= var_equal(bmap
, i
);
4925 return isl_basic_map_finalize(bmap
);
4928 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4930 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4934 struct isl_basic_map
*bmap
;
4935 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4938 for (i
= 0; i
< pos
&& bmap
; ++i
)
4939 bmap
= var_equal(bmap
, i
);
4941 bmap
= var_less(bmap
, pos
);
4942 return isl_basic_map_finalize(bmap
);
4945 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4947 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4948 __isl_take isl_space
*space
, unsigned pos
)
4951 isl_basic_map
*bmap
;
4953 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4954 for (i
= 0; i
< pos
; ++i
)
4955 bmap
= var_equal(bmap
, i
);
4956 bmap
= var_less_or_equal(bmap
, pos
);
4957 return isl_basic_map_finalize(bmap
);
4960 /* Return a relation on "space" expressing i_pos > o_pos
4962 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4966 struct isl_basic_map
*bmap
;
4967 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4970 for (i
= 0; i
< pos
&& bmap
; ++i
)
4971 bmap
= var_equal(bmap
, i
);
4973 bmap
= var_more(bmap
, pos
);
4974 return isl_basic_map_finalize(bmap
);
4977 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4979 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4980 __isl_take isl_space
*space
, unsigned pos
)
4983 isl_basic_map
*bmap
;
4985 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4986 for (i
= 0; i
< pos
; ++i
)
4987 bmap
= var_equal(bmap
, i
);
4988 bmap
= var_more_or_equal(bmap
, pos
);
4989 return isl_basic_map_finalize(bmap
);
4992 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4993 unsigned n
, int equal
)
4995 struct isl_map
*map
;
4998 if (n
== 0 && equal
)
4999 return isl_map_universe(space
);
5001 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5003 for (i
= 0; i
+ 1 < n
; ++i
)
5004 map
= isl_map_add_basic_map(map
,
5005 isl_basic_map_less_at(isl_space_copy(space
), i
));
5008 map
= isl_map_add_basic_map(map
,
5009 isl_basic_map_less_or_equal_at(space
, n
- 1));
5011 map
= isl_map_add_basic_map(map
,
5012 isl_basic_map_less_at(space
, n
- 1));
5014 isl_space_free(space
);
5019 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5023 return map_lex_lte_first(space
, space
->n_out
, equal
);
5026 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5028 return map_lex_lte_first(dim
, n
, 0);
5031 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5033 return map_lex_lte_first(dim
, n
, 1);
5036 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5038 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5041 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5043 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5046 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5047 unsigned n
, int equal
)
5049 struct isl_map
*map
;
5052 if (n
== 0 && equal
)
5053 return isl_map_universe(space
);
5055 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5057 for (i
= 0; i
+ 1 < n
; ++i
)
5058 map
= isl_map_add_basic_map(map
,
5059 isl_basic_map_more_at(isl_space_copy(space
), i
));
5062 map
= isl_map_add_basic_map(map
,
5063 isl_basic_map_more_or_equal_at(space
, n
- 1));
5065 map
= isl_map_add_basic_map(map
,
5066 isl_basic_map_more_at(space
, n
- 1));
5068 isl_space_free(space
);
5073 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5077 return map_lex_gte_first(space
, space
->n_out
, equal
);
5080 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5082 return map_lex_gte_first(dim
, n
, 0);
5085 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5087 return map_lex_gte_first(dim
, n
, 1);
5090 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5092 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5095 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5097 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5100 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5101 __isl_take isl_set
*set2
)
5104 map
= isl_map_lex_le(isl_set_get_space(set1
));
5105 map
= isl_map_intersect_domain(map
, set1
);
5106 map
= isl_map_intersect_range(map
, set2
);
5110 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5111 __isl_take isl_set
*set2
)
5114 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5115 map
= isl_map_intersect_domain(map
, set1
);
5116 map
= isl_map_intersect_range(map
, set2
);
5120 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5121 __isl_take isl_set
*set2
)
5124 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5125 map
= isl_map_intersect_domain(map
, set1
);
5126 map
= isl_map_intersect_range(map
, set2
);
5130 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5131 __isl_take isl_set
*set2
)
5134 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5135 map
= isl_map_intersect_domain(map
, set1
);
5136 map
= isl_map_intersect_range(map
, set2
);
5140 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5141 __isl_take isl_map
*map2
)
5144 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5145 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5146 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5150 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5151 __isl_take isl_map
*map2
)
5154 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5155 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5156 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5160 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5161 __isl_take isl_map
*map2
)
5164 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5165 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5166 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5170 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5171 __isl_take isl_map
*map2
)
5174 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5175 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5176 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5180 /* For the div d = floor(f/m) at position "div", add the constraint
5184 static __isl_give isl_basic_map
*add_upper_div_constraint(
5185 __isl_take isl_basic_map
*bmap
, unsigned div
)
5188 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5192 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5193 if (v_div
< 0 || n_div
< 0)
5194 return isl_basic_map_free(bmap
);
5196 i
= isl_basic_map_alloc_inequality(bmap
);
5198 return isl_basic_map_free(bmap
);
5199 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5200 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5205 /* For the div d = floor(f/m) at position "div", add the constraint
5207 * -(f-(m-1)) + m d >= 0
5209 static __isl_give isl_basic_map
*add_lower_div_constraint(
5210 __isl_take isl_basic_map
*bmap
, unsigned div
)
5213 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5217 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5218 if (v_div
< 0 || n_div
< 0)
5219 return isl_basic_map_free(bmap
);
5221 i
= isl_basic_map_alloc_inequality(bmap
);
5223 return isl_basic_map_free(bmap
);
5224 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5225 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5226 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5227 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5232 /* For the div d = floor(f/m) at position "pos", add the constraints
5235 * -(f-(m-1)) + m d >= 0
5237 * Note that the second constraint is the negation of
5241 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5242 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5244 bmap
= add_upper_div_constraint(bmap
, pos
);
5245 bmap
= add_lower_div_constraint(bmap
, pos
);
5249 /* For each known div d = floor(f/m), add the constraints
5252 * -(f-(m-1)) + m d >= 0
5254 * Remove duplicate constraints in case of some these div constraints
5255 * already appear in "bmap".
5257 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5258 __isl_take isl_basic_map
*bmap
)
5262 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5264 return isl_basic_map_free(bmap
);
5268 bmap
= add_known_div_constraints(bmap
);
5269 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5270 bmap
= isl_basic_map_finalize(bmap
);
5274 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5276 * In particular, if this div is of the form d = floor(f/m),
5277 * then add the constraint
5281 * if sign < 0 or the constraint
5283 * -(f-(m-1)) + m d >= 0
5287 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5288 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5291 return add_upper_div_constraint(bmap
, div
);
5293 return add_lower_div_constraint(bmap
, div
);
5296 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5297 __isl_take isl_basic_map
*bmap
)
5301 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5303 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5304 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5305 return bset_from_bmap(bmap
);
5306 bmap
= isl_basic_map_cow(bmap
);
5309 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5312 bmap
->extra
-= bmap
->n_div
;
5314 bmap
= isl_basic_map_finalize(bmap
);
5315 return bset_from_bmap(bmap
);
5317 isl_basic_map_free(bmap
);
5321 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5322 __isl_take isl_basic_set
*bset
)
5324 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5327 /* Replace each element in "list" by the result of applying
5328 * isl_basic_map_underlying_set to the element.
5330 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5331 __isl_take isl_basic_map_list
*list
)
5336 n
= isl_basic_map_list_n_basic_map(list
);
5340 for (i
= 0; i
< n
; ++i
) {
5341 isl_basic_map
*bmap
;
5342 isl_basic_set
*bset
;
5344 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5345 bset
= isl_basic_set_underlying_set(bmap
);
5346 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5351 isl_basic_map_list_free(list
);
5355 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5356 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5358 struct isl_basic_map
*bmap
;
5359 struct isl_ctx
*ctx
;
5360 isl_size dim
, bmap_total
;
5367 if (isl_basic_set_check_no_params(bset
) < 0 ||
5368 isl_basic_set_check_no_locals(bset
) < 0)
5370 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5371 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5372 if (dim
< 0 || bmap_total
< 0)
5374 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5375 if (like
->n_div
== 0) {
5376 isl_space
*space
= isl_basic_map_get_space(like
);
5377 isl_basic_map_free(like
);
5378 return isl_basic_map_reset_space(bset
, space
);
5380 bset
= isl_basic_set_cow(bset
);
5383 total
= dim
+ bset
->extra
;
5384 bmap
= bset_to_bmap(bset
);
5385 isl_space_free(bmap
->dim
);
5386 bmap
->dim
= isl_space_copy(like
->dim
);
5389 bmap
->n_div
= like
->n_div
;
5390 bmap
->extra
+= like
->n_div
;
5394 ltotal
= total
- bmap
->extra
+ like
->extra
;
5397 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5398 bmap
->extra
* (1 + 1 + total
));
5399 if (isl_blk_is_error(bmap
->block2
))
5401 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5405 for (i
= 0; i
< bmap
->extra
; ++i
)
5406 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5407 for (i
= 0; i
< like
->n_div
; ++i
) {
5408 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5409 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5411 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5413 isl_basic_map_free(like
);
5414 bmap
= isl_basic_map_simplify(bmap
);
5415 bmap
= isl_basic_map_finalize(bmap
);
5418 isl_basic_map_free(like
);
5419 isl_basic_set_free(bset
);
5423 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5424 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5426 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5427 bset_to_bmap(like
)));
5430 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5434 map
= isl_map_cow(map
);
5437 map
->dim
= isl_space_cow(map
->dim
);
5441 for (i
= 1; i
< map
->n
; ++i
)
5442 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5444 for (i
= 0; i
< map
->n
; ++i
) {
5445 map
->p
[i
] = bset_to_bmap(
5446 isl_basic_map_underlying_set(map
->p
[i
]));
5451 map
->dim
= isl_space_underlying(map
->dim
, 0);
5453 isl_space_free(map
->dim
);
5454 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5458 return set_from_map(map
);
5464 /* Replace the space of "bmap" by "space".
5466 * If the space of "bmap" is identical to "space" (including the identifiers
5467 * of the input and output dimensions), then simply return the original input.
5469 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5470 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5473 isl_space
*bmap_space
;
5475 bmap_space
= isl_basic_map_peek_space(bmap
);
5476 equal
= isl_space_is_equal(bmap_space
, space
);
5477 if (equal
>= 0 && equal
)
5478 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5482 isl_space_free(space
);
5485 bmap
= isl_basic_map_cow(bmap
);
5486 if (!bmap
|| !space
)
5489 isl_space_free(bmap
->dim
);
5492 bmap
= isl_basic_map_finalize(bmap
);
5496 isl_basic_map_free(bmap
);
5497 isl_space_free(space
);
5501 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5502 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5504 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5508 /* Check that the total dimensions of "map" and "space" are the same.
5510 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5511 __isl_keep isl_space
*space
)
5513 isl_size dim1
, dim2
;
5515 dim1
= isl_map_dim(map
, isl_dim_all
);
5516 dim2
= isl_space_dim(space
, isl_dim_all
);
5517 if (dim1
< 0 || dim2
< 0)
5518 return isl_stat_error
;
5521 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5522 "total dimensions do not match", return isl_stat_error
);
5525 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5526 __isl_take isl_space
*space
)
5530 map
= isl_map_cow(map
);
5534 for (i
= 0; i
< map
->n
; ++i
) {
5535 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5536 isl_space_copy(space
));
5540 isl_space_free(map
->dim
);
5546 isl_space_free(space
);
5550 /* Replace the space of "map" by "space", without modifying
5551 * the dimension of "map".
5553 * If the space of "map" is identical to "space" (including the identifiers
5554 * of the input and output dimensions), then simply return the original input.
5556 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5557 __isl_take isl_space
*space
)
5560 isl_space
*map_space
;
5562 map_space
= isl_map_peek_space(map
);
5563 equal
= isl_space_is_equal(map_space
, space
);
5564 if (equal
>= 0 && equal
)
5565 equal
= isl_space_has_equal_ids(map_space
, space
);
5569 isl_space_free(space
);
5572 if (check_map_space_equal_total_dim(map
, space
) < 0)
5574 return isl_map_reset_space(map
, space
);
5577 isl_space_free(space
);
5581 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5582 __isl_take isl_space
*dim
)
5584 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5587 /* Compute the parameter domain of the given basic set.
5589 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5595 is_params
= isl_basic_set_is_params(bset
);
5597 return isl_basic_set_free(bset
);
5601 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5603 return isl_basic_set_free(bset
);
5604 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5605 space
= isl_basic_set_get_space(bset
);
5606 space
= isl_space_params(space
);
5607 bset
= isl_basic_set_reset_space(bset
, space
);
5611 /* Construct a zero-dimensional basic set with the given parameter domain.
5613 __isl_give isl_basic_set
*isl_basic_set_from_params(
5614 __isl_take isl_basic_set
*bset
)
5617 space
= isl_basic_set_get_space(bset
);
5618 space
= isl_space_set_from_params(space
);
5619 bset
= isl_basic_set_reset_space(bset
, space
);
5623 /* Compute the parameter domain of the given set.
5625 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5627 return isl_map_params(set_to_map(set
));
5630 /* Construct a zero-dimensional set with the given parameter domain.
5632 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5635 space
= isl_set_get_space(set
);
5636 space
= isl_space_set_from_params(space
);
5637 set
= isl_set_reset_space(set
, space
);
5641 /* Compute the parameter domain of the given map.
5643 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5646 isl_size n_in
, n_out
;
5648 n_in
= isl_map_dim(map
, isl_dim_in
);
5649 n_out
= isl_map_dim(map
, isl_dim_out
);
5650 if (n_in
< 0 || n_out
< 0)
5651 return isl_map_free(map
);
5652 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5653 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5654 space
= isl_map_get_space(map
);
5655 space
= isl_space_params(space
);
5656 map
= isl_map_reset_space(map
, space
);
5660 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5665 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5667 return isl_basic_map_free(bmap
);
5668 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5670 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5672 return isl_basic_map_reset_space(bmap
, space
);
5675 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5678 return isl_bool_error
;
5679 return isl_space_may_be_set(bmap
->dim
);
5682 /* Is this basic map actually a set?
5683 * Users should never call this function. Outside of isl,
5684 * the type should indicate whether something is a set or a map.
5686 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5689 return isl_bool_error
;
5690 return isl_space_is_set(bmap
->dim
);
5693 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5697 is_set
= isl_basic_map_is_set(bmap
);
5702 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5704 isl_basic_map_free(bmap
);
5708 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5709 __isl_take isl_basic_map
*bmap
)
5713 isl_basic_map
*domain
;
5714 isl_size nparam
, n_in
, n_out
;
5716 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5717 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5718 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5719 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5720 return isl_basic_map_free(bmap
);
5722 space
= isl_basic_map_get_space(bmap
);
5723 space
= isl_space_from_range(isl_space_domain(space
));
5724 domain
= isl_basic_map_universe(space
);
5726 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5727 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5728 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5730 for (i
= 0; i
< n_in
; ++i
)
5731 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5734 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5735 return isl_basic_map_finalize(bmap
);
5738 __isl_give isl_basic_map
*isl_basic_map_range_map(
5739 __isl_take isl_basic_map
*bmap
)
5743 isl_basic_map
*range
;
5744 isl_size nparam
, n_in
, n_out
;
5746 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5747 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5748 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5749 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5750 return isl_basic_map_free(bmap
);
5752 space
= isl_basic_map_get_space(bmap
);
5753 space
= isl_space_from_range(isl_space_range(space
));
5754 range
= isl_basic_map_universe(space
);
5756 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5757 bmap
= isl_basic_map_apply_range(bmap
, range
);
5758 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5760 for (i
= 0; i
< n_out
; ++i
)
5761 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5764 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5765 return isl_basic_map_finalize(bmap
);
5768 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5772 return isl_space_may_be_set(map
->dim
);
5775 /* Is this map actually a set?
5776 * Users should never call this function. Outside of isl,
5777 * the type should indicate whether something is a set or a map.
5779 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5782 return isl_bool_error
;
5783 return isl_space_is_set(map
->dim
);
5786 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5790 struct isl_set
*set
;
5792 is_set
= isl_map_is_set(map
);
5796 return set_from_map(map
);
5798 map
= isl_map_cow(map
);
5802 set
= set_from_map(map
);
5803 set
->dim
= isl_space_range(set
->dim
);
5806 for (i
= 0; i
< map
->n
; ++i
) {
5807 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5811 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5812 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5819 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5823 map
= isl_map_cow(map
);
5827 map
->dim
= isl_space_domain_map(map
->dim
);
5830 for (i
= 0; i
< map
->n
; ++i
) {
5831 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5835 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5836 map
= isl_map_unmark_normalized(map
);
5843 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5846 isl_space
*range_dim
;
5848 map
= isl_map_cow(map
);
5852 range_dim
= isl_space_range(isl_map_get_space(map
));
5853 range_dim
= isl_space_from_range(range_dim
);
5854 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5855 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5858 for (i
= 0; i
< map
->n
; ++i
) {
5859 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5863 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5864 map
= isl_map_unmark_normalized(map
);
5871 /* Given a wrapped map of the form A[B -> C],
5872 * return the map A[B -> C] -> B.
5874 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5881 if (!isl_set_has_tuple_id(set
))
5882 return isl_map_domain_map(isl_set_unwrap(set
));
5884 id
= isl_set_get_tuple_id(set
);
5885 map
= isl_map_domain_map(isl_set_unwrap(set
));
5886 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5891 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5892 __isl_take isl_basic_set
*bset
)
5894 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5897 __isl_give isl_basic_map
*isl_basic_map_from_range(
5898 __isl_take isl_basic_set
*bset
)
5901 space
= isl_basic_set_get_space(bset
);
5902 space
= isl_space_from_range(space
);
5903 bset
= isl_basic_set_reset_space(bset
, space
);
5904 return bset_to_bmap(bset
);
5907 /* Create a relation with the given set as range.
5908 * The domain of the created relation is a zero-dimensional
5909 * flat anonymous space.
5911 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5914 space
= isl_set_get_space(set
);
5915 space
= isl_space_from_range(space
);
5916 set
= isl_set_reset_space(set
, space
);
5917 return set_to_map(set
);
5920 /* Create a relation with the given set as domain.
5921 * The range of the created relation is a zero-dimensional
5922 * flat anonymous space.
5924 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5926 return isl_map_reverse(isl_map_from_range(set
));
5929 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5930 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5932 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5935 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5936 __isl_take isl_set
*range
)
5938 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5941 /* Return a newly allocated isl_map with given space and flags and
5942 * room for "n" basic maps.
5943 * Make sure that all cached information is cleared.
5945 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5948 struct isl_map
*map
;
5953 isl_die(space
->ctx
, isl_error_internal
,
5954 "negative number of basic maps", goto error
);
5955 map
= isl_calloc(space
->ctx
, struct isl_map
,
5956 sizeof(struct isl_map
) +
5957 (n
- 1) * sizeof(struct isl_basic_map
*));
5961 map
->ctx
= space
->ctx
;
5962 isl_ctx_ref(map
->ctx
);
5970 isl_space_free(space
);
5974 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5976 struct isl_basic_map
*bmap
;
5977 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5978 bmap
= isl_basic_map_set_to_empty(bmap
);
5982 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5984 struct isl_basic_set
*bset
;
5985 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5986 bset
= isl_basic_set_set_to_empty(bset
);
5990 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5992 struct isl_basic_map
*bmap
;
5993 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5994 bmap
= isl_basic_map_finalize(bmap
);
5998 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6000 struct isl_basic_set
*bset
;
6001 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6002 bset
= isl_basic_set_finalize(bset
);
6006 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6007 __isl_take isl_space
*space
)
6010 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6011 isl_basic_map
*bmap
;
6014 space
= isl_space_free(space
);
6015 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6016 for (i
= 0; i
< total
; ++i
) {
6017 int k
= isl_basic_map_alloc_inequality(bmap
);
6020 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6021 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6025 isl_basic_map_free(bmap
);
6029 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6030 __isl_take isl_space
*space
)
6032 return isl_basic_map_nat_universe(space
);
6035 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6037 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6040 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6042 return isl_map_nat_universe(dim
);
6045 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6047 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6050 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6052 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6055 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6057 struct isl_map
*map
;
6060 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6061 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6065 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6067 struct isl_set
*set
;
6070 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6071 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6075 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6078 struct isl_map
*dup
;
6082 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6083 for (i
= 0; i
< map
->n
; ++i
)
6084 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6088 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6089 __isl_take isl_basic_map
*bmap
)
6093 if (isl_basic_map_plain_is_empty(bmap
)) {
6094 isl_basic_map_free(bmap
);
6097 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6098 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6099 map
->p
[map
->n
] = bmap
;
6101 map
= isl_map_unmark_normalized(map
);
6107 isl_basic_map_free(bmap
);
6111 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6122 isl_ctx_deref(map
->ctx
);
6123 for (i
= 0; i
< map
->n
; ++i
)
6124 isl_basic_map_free(map
->p
[i
]);
6125 isl_space_free(map
->dim
);
6131 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6132 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6137 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6139 return isl_basic_map_free(bmap
);
6141 bmap
= isl_basic_map_cow(bmap
);
6142 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6143 j
= isl_basic_map_alloc_equality(bmap
);
6146 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6147 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6148 isl_int_set_si(bmap
->eq
[j
][0], value
);
6149 bmap
= isl_basic_map_simplify(bmap
);
6150 return isl_basic_map_finalize(bmap
);
6152 isl_basic_map_free(bmap
);
6156 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6157 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6162 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6164 return isl_basic_map_free(bmap
);
6166 bmap
= isl_basic_map_cow(bmap
);
6167 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6168 j
= isl_basic_map_alloc_equality(bmap
);
6171 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6172 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6173 isl_int_set(bmap
->eq
[j
][0], value
);
6174 bmap
= isl_basic_map_simplify(bmap
);
6175 return isl_basic_map_finalize(bmap
);
6177 isl_basic_map_free(bmap
);
6181 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6182 enum isl_dim_type type
, unsigned pos
, int value
)
6184 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6185 return isl_basic_map_free(bmap
);
6186 return isl_basic_map_fix_pos_si(bmap
,
6187 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6190 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6191 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6193 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6194 return isl_basic_map_free(bmap
);
6195 return isl_basic_map_fix_pos(bmap
,
6196 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6199 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6200 * to be equal to "v".
6202 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6203 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6207 if (!isl_val_is_int(v
))
6208 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6209 "expecting integer value", goto error
);
6210 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6212 pos
+= isl_basic_map_offset(bmap
, type
);
6213 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6217 isl_basic_map_free(bmap
);
6222 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6223 * to be equal to "v".
6225 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6226 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6228 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6231 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6232 enum isl_dim_type type
, unsigned pos
, int value
)
6234 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6238 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6239 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6241 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6245 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6246 unsigned input
, int value
)
6248 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6251 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6252 unsigned dim
, int value
)
6254 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6255 isl_dim_set
, dim
, value
));
6258 /* Remove the basic map at position "i" from "map" if this basic map
6259 * is (obviously) empty.
6261 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6268 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6270 return isl_map_free(map
);
6274 isl_basic_map_free(map
->p
[i
]);
6277 map
->p
[i
] = map
->p
[map
->n
];
6278 map
= isl_map_unmark_normalized(map
);
6285 /* Perform "fn" on each basic map of "map", where we may not be holding
6286 * the only reference to "map".
6287 * In particular, "fn" should be a semantics preserving operation
6288 * that we want to apply to all copies of "map". We therefore need
6289 * to be careful not to modify "map" in a way that breaks "map"
6290 * in case anything goes wrong.
6292 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6293 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6295 struct isl_basic_map
*bmap
;
6301 for (i
= map
->n
- 1; i
>= 0; --i
) {
6302 bmap
= isl_basic_map_copy(map
->p
[i
]);
6306 isl_basic_map_free(map
->p
[i
]);
6308 map
= remove_if_empty(map
, i
);
6319 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6320 enum isl_dim_type type
, unsigned pos
, int value
)
6324 map
= isl_map_cow(map
);
6325 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6326 return isl_map_free(map
);
6327 for (i
= map
->n
- 1; i
>= 0; --i
) {
6328 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6329 map
= remove_if_empty(map
, i
);
6333 map
= isl_map_unmark_normalized(map
);
6337 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6338 enum isl_dim_type type
, unsigned pos
, int value
)
6340 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6343 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6344 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6348 map
= isl_map_cow(map
);
6349 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6350 return isl_map_free(map
);
6351 for (i
= 0; i
< map
->n
; ++i
) {
6352 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6356 map
= isl_map_unmark_normalized(map
);
6363 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6364 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6366 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6369 /* Fix the value of the variable at position "pos" of type "type" of "map"
6370 * to be equal to "v".
6372 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6373 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6377 map
= isl_map_cow(map
);
6381 if (!isl_val_is_int(v
))
6382 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6383 "expecting integer value", goto error
);
6384 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6386 for (i
= map
->n
- 1; i
>= 0; --i
) {
6387 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6389 map
= remove_if_empty(map
, i
);
6393 map
= isl_map_unmark_normalized(map
);
6402 /* Fix the value of the variable at position "pos" of type "type" of "set"
6403 * to be equal to "v".
6405 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6406 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6408 return isl_map_fix_val(set
, type
, pos
, v
);
6411 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6412 unsigned input
, int value
)
6414 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6417 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6419 return set_from_map(isl_map_fix_si(set_to_map(set
),
6420 isl_dim_set
, dim
, value
));
6423 static __isl_give isl_basic_map
*basic_map_bound_si(
6424 __isl_take isl_basic_map
*bmap
,
6425 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6430 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6431 return isl_basic_map_free(bmap
);
6432 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6434 return isl_basic_map_free(bmap
);
6435 pos
+= isl_basic_map_offset(bmap
, type
);
6436 bmap
= isl_basic_map_cow(bmap
);
6437 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6438 j
= isl_basic_map_alloc_inequality(bmap
);
6441 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6443 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6444 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6446 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6447 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6449 bmap
= isl_basic_map_simplify(bmap
);
6450 return isl_basic_map_finalize(bmap
);
6452 isl_basic_map_free(bmap
);
6456 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6457 __isl_take isl_basic_map
*bmap
,
6458 enum isl_dim_type type
, unsigned pos
, int value
)
6460 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6463 /* Constrain the values of the given dimension to be no greater than "value".
6465 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6466 __isl_take isl_basic_map
*bmap
,
6467 enum isl_dim_type type
, unsigned pos
, int value
)
6469 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6472 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6473 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6477 map
= isl_map_cow(map
);
6478 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6479 return isl_map_free(map
);
6480 for (i
= 0; i
< map
->n
; ++i
) {
6481 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6482 type
, pos
, value
, upper
);
6486 map
= isl_map_unmark_normalized(map
);
6493 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6494 enum isl_dim_type type
, unsigned pos
, int value
)
6496 return map_bound_si(map
, type
, pos
, value
, 0);
6499 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6500 enum isl_dim_type type
, unsigned pos
, int value
)
6502 return map_bound_si(map
, type
, pos
, value
, 1);
6505 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6506 enum isl_dim_type type
, unsigned pos
, int value
)
6508 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6512 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6513 enum isl_dim_type type
, unsigned pos
, int value
)
6515 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6518 /* Bound the given variable of "bmap" from below (or above is "upper"
6519 * is set) to "value".
6521 static __isl_give isl_basic_map
*basic_map_bound(
6522 __isl_take isl_basic_map
*bmap
,
6523 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6528 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6529 return isl_basic_map_free(bmap
);
6530 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6532 return isl_basic_map_free(bmap
);
6533 pos
+= isl_basic_map_offset(bmap
, type
);
6534 bmap
= isl_basic_map_cow(bmap
);
6535 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6536 j
= isl_basic_map_alloc_inequality(bmap
);
6539 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6541 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6542 isl_int_set(bmap
->ineq
[j
][0], value
);
6544 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6545 isl_int_neg(bmap
->ineq
[j
][0], value
);
6547 bmap
= isl_basic_map_simplify(bmap
);
6548 return isl_basic_map_finalize(bmap
);
6550 isl_basic_map_free(bmap
);
6554 /* Bound the given variable of "map" from below (or above is "upper"
6555 * is set) to "value".
6557 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6558 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6562 map
= isl_map_cow(map
);
6563 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6564 return isl_map_free(map
);
6565 for (i
= map
->n
- 1; i
>= 0; --i
) {
6566 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6567 map
= remove_if_empty(map
, i
);
6571 map
= isl_map_unmark_normalized(map
);
6575 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6576 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6578 return map_bound(map
, type
, pos
, value
, 0);
6581 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6582 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6584 return map_bound(map
, type
, pos
, value
, 1);
6587 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6588 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6590 return isl_map_lower_bound(set
, type
, pos
, value
);
6593 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6594 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6596 return isl_map_upper_bound(set
, type
, pos
, value
);
6599 /* Force the values of the variable at position "pos" of type "type" of "set"
6600 * to be no smaller than "value".
6602 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6603 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6607 if (!isl_val_is_int(value
))
6608 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6609 "expecting integer value", goto error
);
6610 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6611 isl_val_free(value
);
6614 isl_val_free(value
);
6619 /* Force the values of the variable at position "pos" of type "type" of "set"
6620 * to be no greater than "value".
6622 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6623 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6627 if (!isl_val_is_int(value
))
6628 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6629 "expecting integer value", goto error
);
6630 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6631 isl_val_free(value
);
6634 isl_val_free(value
);
6639 /* Bound the given variable of "bset" from below (or above is "upper"
6640 * is set) to "value".
6642 static __isl_give isl_basic_set
*isl_basic_set_bound(
6643 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6644 isl_int value
, int upper
)
6646 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6647 type
, pos
, value
, upper
));
6650 /* Bound the given variable of "bset" from below (or above is "upper"
6651 * is set) to "value".
6653 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6654 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6655 __isl_take isl_val
*value
, int upper
)
6659 if (!isl_val_is_int(value
))
6660 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6661 "expecting integer value", goto error
);
6662 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6663 isl_val_free(value
);
6666 isl_val_free(value
);
6667 isl_basic_set_free(bset
);
6671 /* Bound the given variable of "bset" from below to "value".
6673 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6674 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6675 __isl_take isl_val
*value
)
6677 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6680 /* Bound the given variable of "bset" from above to "value".
6682 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6683 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6684 __isl_take isl_val
*value
)
6686 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6689 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6693 map
= isl_map_cow(map
);
6697 map
->dim
= isl_space_reverse(map
->dim
);
6700 for (i
= 0; i
< map
->n
; ++i
) {
6701 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6705 map
= isl_map_unmark_normalized(map
);
6713 #define TYPE isl_pw_multi_aff
6715 #define SUFFIX _pw_multi_aff
6717 #define EMPTY isl_pw_multi_aff_empty
6719 #define ADD isl_pw_multi_aff_union_add
6720 #include "isl_map_lexopt_templ.c"
6722 /* Given a map "map", compute the lexicographically minimal
6723 * (or maximal) image element for each domain element in dom,
6724 * in the form of an isl_pw_multi_aff.
6725 * If "empty" is not NULL, then set *empty to those elements in dom that
6726 * do not have an image element.
6727 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6728 * should be computed over the domain of "map". "empty" is also NULL
6731 * We first compute the lexicographically minimal or maximal element
6732 * in the first basic map. This results in a partial solution "res"
6733 * and a subset "todo" of dom that still need to be handled.
6734 * We then consider each of the remaining maps in "map" and successively
6735 * update both "res" and "todo".
6736 * If "empty" is NULL, then the todo sets are not needed and therefore
6737 * also not computed.
6739 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6740 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6741 __isl_give isl_set
**empty
, unsigned flags
)
6745 isl_pw_multi_aff
*res
;
6748 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6749 if (!map
|| (!full
&& !dom
))
6752 if (isl_map_plain_is_empty(map
)) {
6757 return isl_pw_multi_aff_from_map(map
);
6760 res
= basic_map_partial_lexopt_pw_multi_aff(
6761 isl_basic_map_copy(map
->p
[0]),
6762 isl_set_copy(dom
), empty
, flags
);
6766 for (i
= 1; i
< map
->n
; ++i
) {
6767 isl_pw_multi_aff
*res_i
;
6769 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6770 isl_basic_map_copy(map
->p
[i
]),
6771 isl_set_copy(dom
), empty
, flags
);
6773 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6774 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6776 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6779 todo
= isl_set_intersect(todo
, *empty
);
6798 #define TYPE isl_map
6802 #define EMPTY isl_map_empty
6804 #define ADD isl_map_union_disjoint
6805 #include "isl_map_lexopt_templ.c"
6807 /* Given a map "map", compute the lexicographically minimal
6808 * (or maximal) image element for each domain element in "dom",
6809 * in the form of an isl_map.
6810 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6811 * do not have an image element.
6812 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6813 * should be computed over the domain of "map". "empty" is also NULL
6816 * If the input consists of more than one disjunct, then first
6817 * compute the desired result in the form of an isl_pw_multi_aff and
6818 * then convert that into an isl_map.
6820 * This function used to have an explicit implementation in terms
6821 * of isl_maps, but it would continually intersect the domains of
6822 * partial results with the complement of the domain of the next
6823 * partial solution, potentially leading to an explosion in the number
6824 * of disjuncts if there are several disjuncts in the input.
6825 * An even earlier implementation of this function would look for
6826 * better results in the domain of the partial result and for extra
6827 * results in the complement of this domain, which would lead to
6828 * even more splintering.
6830 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6831 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6832 __isl_give isl_set
**empty
, unsigned flags
)
6835 struct isl_map
*res
;
6836 isl_pw_multi_aff
*pma
;
6838 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6839 if (!map
|| (!full
&& !dom
))
6842 if (isl_map_plain_is_empty(map
)) {
6851 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6857 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6859 return isl_map_from_pw_multi_aff(pma
);
6868 __isl_give isl_map
*isl_map_partial_lexmax(
6869 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6870 __isl_give isl_set
**empty
)
6872 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6875 __isl_give isl_map
*isl_map_partial_lexmin(
6876 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6877 __isl_give isl_set
**empty
)
6879 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6882 __isl_give isl_set
*isl_set_partial_lexmin(
6883 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6884 __isl_give isl_set
**empty
)
6886 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6890 __isl_give isl_set
*isl_set_partial_lexmax(
6891 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6892 __isl_give isl_set
**empty
)
6894 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6898 /* Compute the lexicographic minimum (or maximum if "flags" includes
6899 * ISL_OPT_MAX) of "bset" over its parametric domain.
6901 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6904 return isl_basic_map_lexopt(bset
, flags
);
6907 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6909 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6912 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6914 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6917 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6919 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6922 /* Compute the lexicographic minimum of "bset" over its parametric domain
6923 * for the purpose of quantifier elimination.
6924 * That is, find an explicit representation for all the existentially
6925 * quantified variables in "bset" by computing their lexicographic
6928 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6929 __isl_take isl_basic_set
*bset
)
6931 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6934 /* Given a basic map with one output dimension, compute the minimum or
6935 * maximum of that dimension as an isl_pw_aff.
6937 * Compute the optimum as a lexicographic optimum over the single
6938 * output dimension and extract the single isl_pw_aff from the result.
6940 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6943 isl_pw_multi_aff
*pma
;
6946 bmap
= isl_basic_map_copy(bmap
);
6947 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6948 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6949 isl_pw_multi_aff_free(pma
);
6954 /* Compute the minimum or maximum of the given output dimension
6955 * as a function of the parameters and the input dimensions,
6956 * but independently of the other output dimensions.
6958 * We first project out the other output dimension and then compute
6959 * the "lexicographic" maximum in each basic map, combining the results
6960 * using isl_pw_aff_union_max.
6962 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6969 n_out
= isl_map_dim(map
, isl_dim_out
);
6971 map
= isl_map_free(map
);
6972 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6973 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6978 isl_space
*space
= isl_map_get_space(map
);
6980 return isl_pw_aff_empty(space
);
6983 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6984 for (i
= 1; i
< map
->n
; ++i
) {
6985 isl_pw_aff
*pwaff_i
;
6987 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6988 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6996 /* Compute the minimum of the given output dimension as a function of the
6997 * parameters and input dimensions, but independently of
6998 * the other output dimensions.
7000 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7002 return map_dim_opt(map
, pos
, 0);
7005 /* Compute the maximum of the given output dimension as a function of the
7006 * parameters and input dimensions, but independently of
7007 * the other output dimensions.
7009 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7011 return map_dim_opt(map
, pos
, 1);
7014 /* Compute the minimum or maximum of the given set dimension
7015 * as a function of the parameters,
7016 * but independently of the other set dimensions.
7018 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7021 return map_dim_opt(set
, pos
, max
);
7024 /* Compute the maximum of the given set dimension as a function of the
7025 * parameters, but independently of the other set dimensions.
7027 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7029 return set_dim_opt(set
, pos
, 1);
7032 /* Compute the minimum of the given set dimension as a function of the
7033 * parameters, but independently of the other set dimensions.
7035 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7037 return set_dim_opt(set
, pos
, 0);
7040 /* Apply a preimage specified by "mat" on the parameters of "bset".
7041 * bset is assumed to have only parameters and divs.
7043 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7044 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7048 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7049 if (nparam
< 0 || !mat
)
7052 bset
->dim
= isl_space_cow(bset
->dim
);
7056 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7058 bset
->dim
->nparam
= 0;
7059 bset
->dim
->n_out
= nparam
;
7060 bset
= isl_basic_set_preimage(bset
, mat
);
7062 bset
->dim
->nparam
= bset
->dim
->n_out
;
7063 bset
->dim
->n_out
= 0;
7068 isl_basic_set_free(bset
);
7072 /* Apply a preimage specified by "mat" on the parameters of "set".
7073 * set is assumed to have only parameters and divs.
7075 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7076 __isl_take isl_mat
*mat
)
7081 nparam
= isl_set_dim(set
, isl_dim_param
);
7082 if (nparam
< 0 || !mat
)
7085 if (mat
->n_row
!= 1 + nparam
)
7086 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7087 "unexpected number of rows", goto error
);
7089 space
= isl_set_get_space(set
);
7090 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7091 isl_dim_param
, 0, nparam
);
7092 set
= isl_set_reset_space(set
, space
);
7093 set
= isl_set_preimage(set
, mat
);
7094 nparam
= isl_set_dim(set
, isl_dim_out
);
7096 set
= isl_set_free(set
);
7097 space
= isl_set_get_space(set
);
7098 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7099 isl_dim_out
, 0, nparam
);
7100 set
= isl_set_reset_space(set
, space
);
7108 /* Intersect the basic set "bset" with the affine space specified by the
7109 * equalities in "eq".
7111 static __isl_give isl_basic_set
*basic_set_append_equalities(
7112 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7120 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7125 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7126 for (i
= 0; i
< eq
->n_row
; ++i
) {
7127 k
= isl_basic_set_alloc_equality(bset
);
7130 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7131 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7135 bset
= isl_basic_set_gauss(bset
, NULL
);
7136 bset
= isl_basic_set_finalize(bset
);
7141 isl_basic_set_free(bset
);
7145 /* Intersect the set "set" with the affine space specified by the
7146 * equalities in "eq".
7148 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7156 for (i
= 0; i
< set
->n
; ++i
) {
7157 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7170 /* Given a basic set "bset" that only involves parameters and existentially
7171 * quantified variables, return the index of the first equality
7172 * that only involves parameters. If there is no such equality then
7173 * return bset->n_eq.
7175 * This function assumes that isl_basic_set_gauss has been called on "bset".
7177 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7180 isl_size nparam
, n_div
;
7182 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7183 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7184 if (nparam
< 0 || n_div
< 0)
7187 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7188 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7195 /* Compute an explicit representation for the existentially quantified
7196 * variables in "bset" by computing the "minimal value" of the set
7197 * variables. Since there are no set variables, the computation of
7198 * the minimal value essentially computes an explicit representation
7199 * of the non-empty part(s) of "bset".
7201 * The input only involves parameters and existentially quantified variables.
7202 * All equalities among parameters have been removed.
7204 * Since the existentially quantified variables in the result are in general
7205 * going to be different from those in the input, we first replace
7206 * them by the minimal number of variables based on their equalities.
7207 * This should simplify the parametric integer programming.
7209 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7211 isl_morph
*morph1
, *morph2
;
7217 if (bset
->n_eq
== 0)
7218 return isl_basic_set_lexmin_compute_divs(bset
);
7220 morph1
= isl_basic_set_parameter_compression(bset
);
7221 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7222 bset
= isl_basic_set_lift(bset
);
7223 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7224 bset
= isl_morph_basic_set(morph2
, bset
);
7225 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7227 bset
= isl_basic_set_free(bset
);
7228 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7230 set
= isl_basic_set_lexmin_compute_divs(bset
);
7232 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7237 /* Project the given basic set onto its parameter domain, possibly introducing
7238 * new, explicit, existential variables in the constraints.
7239 * The input has parameters and (possibly implicit) existential variables.
7240 * The output has the same parameters, but only
7241 * explicit existentially quantified variables.
7243 * The actual projection is performed by pip, but pip doesn't seem
7244 * to like equalities very much, so we first remove the equalities
7245 * among the parameters by performing a variable compression on
7246 * the parameters. Afterward, an inverse transformation is performed
7247 * and the equalities among the parameters are inserted back in.
7249 * The variable compression on the parameters may uncover additional
7250 * equalities that were only implicit before. We therefore check
7251 * if there are any new parameter equalities in the result and
7252 * if so recurse. The removal of parameter equalities is required
7253 * for the parameter compression performed by base_compute_divs.
7255 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7259 struct isl_mat
*T
, *T2
;
7260 struct isl_set
*set
;
7263 bset
= isl_basic_set_cow(bset
);
7267 if (bset
->n_eq
== 0)
7268 return base_compute_divs(bset
);
7270 bset
= isl_basic_set_gauss(bset
, NULL
);
7273 if (isl_basic_set_plain_is_empty(bset
))
7274 return isl_set_from_basic_set(bset
);
7276 i
= first_parameter_equality(bset
);
7277 if (i
== bset
->n_eq
)
7278 return base_compute_divs(bset
);
7280 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7282 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7283 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7285 eq
= isl_mat_cow(eq
);
7286 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7287 if (T
&& T
->n_col
== 0) {
7291 bset
= isl_basic_set_set_to_empty(bset
);
7292 return isl_set_from_basic_set(bset
);
7294 bset
= basic_set_parameter_preimage(bset
, T
);
7296 i
= first_parameter_equality(bset
);
7299 else if (i
== bset
->n_eq
)
7300 set
= base_compute_divs(bset
);
7302 set
= parameter_compute_divs(bset
);
7303 set
= set_parameter_preimage(set
, T2
);
7304 set
= set_append_equalities(set
, eq
);
7308 /* Insert the divs from "ls" before those of "bmap".
7310 * The number of columns is not changed, which means that the last
7311 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7312 * The caller is responsible for removing the same number of dimensions
7313 * from the space of "bmap".
7315 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7316 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7322 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7324 return isl_basic_map_free(bmap
);
7328 old_n_div
= bmap
->n_div
;
7329 bmap
= insert_div_rows(bmap
, n_div
);
7333 for (i
= 0; i
< n_div
; ++i
) {
7334 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7335 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7341 /* Replace the space of "bmap" by the space and divs of "ls".
7343 * If "ls" has any divs, then we simplify the result since we may
7344 * have discovered some additional equalities that could simplify
7345 * the div expressions.
7347 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7348 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7352 bmap
= isl_basic_map_cow(bmap
);
7353 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7354 if (!bmap
|| n_div
< 0)
7357 bmap
= insert_divs_from_local_space(bmap
, ls
);
7361 isl_space_free(bmap
->dim
);
7362 bmap
->dim
= isl_local_space_get_space(ls
);
7366 isl_local_space_free(ls
);
7368 bmap
= isl_basic_map_simplify(bmap
);
7369 bmap
= isl_basic_map_finalize(bmap
);
7372 isl_basic_map_free(bmap
);
7373 isl_local_space_free(ls
);
7377 /* Replace the space of "map" by the space and divs of "ls".
7379 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7380 __isl_take isl_local_space
*ls
)
7384 map
= isl_map_cow(map
);
7388 for (i
= 0; i
< map
->n
; ++i
) {
7389 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7390 isl_local_space_copy(ls
));
7394 isl_space_free(map
->dim
);
7395 map
->dim
= isl_local_space_get_space(ls
);
7399 isl_local_space_free(ls
);
7402 isl_local_space_free(ls
);
7407 /* Compute an explicit representation for the existentially
7408 * quantified variables for which do not know any explicit representation yet.
7410 * We first sort the existentially quantified variables so that the
7411 * existentially quantified variables for which we already have an explicit
7412 * representation are placed before those for which we do not.
7413 * The input dimensions, the output dimensions and the existentially
7414 * quantified variables for which we already have an explicit
7415 * representation are then turned into parameters.
7416 * compute_divs returns a map with the same parameters and
7417 * no input or output dimensions and the dimension specification
7418 * is reset to that of the input, including the existentially quantified
7419 * variables for which we already had an explicit representation.
7421 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7423 struct isl_basic_set
*bset
;
7424 struct isl_set
*set
;
7425 struct isl_map
*map
;
7427 isl_local_space
*ls
;
7434 bmap
= isl_basic_map_sort_divs(bmap
);
7435 bmap
= isl_basic_map_cow(bmap
);
7439 n_known
= isl_basic_map_first_unknown_div(bmap
);
7440 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7441 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7442 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7443 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7444 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7446 space
= isl_space_set_alloc(bmap
->ctx
,
7447 nparam
+ n_in
+ n_out
+ n_known
, 0);
7451 ls
= isl_basic_map_get_local_space(bmap
);
7452 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7453 n_known
, bmap
->n_div
- n_known
);
7455 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7456 swap_div(bmap
, i
- n_known
, i
);
7457 bmap
->n_div
-= n_known
;
7458 bmap
->extra
-= n_known
;
7460 bmap
= isl_basic_map_reset_space(bmap
, space
);
7461 bset
= bset_from_bmap(bmap
);
7463 set
= parameter_compute_divs(bset
);
7464 map
= set_to_map(set
);
7465 map
= replace_space_by_local_space(map
, ls
);
7469 isl_basic_map_free(bmap
);
7473 /* Remove the explicit representation of local variable "div",
7476 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7477 __isl_take isl_basic_map
*bmap
, int div
)
7481 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7483 return isl_basic_map_free(bmap
);
7487 bmap
= isl_basic_map_cow(bmap
);
7490 isl_int_set_si(bmap
->div
[div
][0], 0);
7494 /* Is local variable "div" of "bmap" marked as not having an explicit
7496 * Note that even if "div" is not marked in this way and therefore
7497 * has an explicit representation, this representation may still
7498 * depend (indirectly) on other local variables that do not
7499 * have an explicit representation.
7501 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7504 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7505 return isl_bool_error
;
7506 return isl_int_is_zero(bmap
->div
[div
][0]);
7509 /* Return the position of the first local variable that does not
7510 * have an explicit representation.
7511 * Return the total number of local variables if they all have
7512 * an explicit representation.
7513 * Return -1 on error.
7515 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7522 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7523 if (!isl_basic_map_div_is_known(bmap
, i
))
7529 /* Return the position of the first local variable that does not
7530 * have an explicit representation.
7531 * Return the total number of local variables if they all have
7532 * an explicit representation.
7533 * Return -1 on error.
7535 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7537 return isl_basic_map_first_unknown_div(bset
);
7540 /* Does "bmap" have an explicit representation for all local variables?
7542 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7547 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7548 first
= isl_basic_map_first_unknown_div(bmap
);
7549 if (n
< 0 || first
< 0)
7550 return isl_bool_error
;
7554 /* Do all basic maps in "map" have an explicit representation
7555 * for all local variables?
7557 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7562 return isl_bool_error
;
7564 for (i
= 0; i
< map
->n
; ++i
) {
7565 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7570 return isl_bool_true
;
7573 /* If bmap contains any unknown divs, then compute explicit
7574 * expressions for them. However, this computation may be
7575 * quite expensive, so first try to remove divs that aren't
7578 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7581 struct isl_map
*map
;
7583 known
= isl_basic_map_divs_known(bmap
);
7587 return isl_map_from_basic_map(bmap
);
7589 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7591 known
= isl_basic_map_divs_known(bmap
);
7595 return isl_map_from_basic_map(bmap
);
7597 map
= compute_divs(bmap
);
7600 isl_basic_map_free(bmap
);
7604 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7608 struct isl_map
*res
;
7615 known
= isl_map_divs_known(map
);
7623 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7624 for (i
= 1 ; i
< map
->n
; ++i
) {
7626 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7627 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7628 res
= isl_map_union_disjoint(res
, r2
);
7630 res
= isl_map_union(res
, r2
);
7637 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7639 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7642 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7644 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7647 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7650 struct isl_set
*set
;
7655 map
= isl_map_cow(map
);
7659 set
= set_from_map(map
);
7660 set
->dim
= isl_space_domain(set
->dim
);
7663 for (i
= 0; i
< map
->n
; ++i
) {
7664 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7668 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7669 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7676 /* Return the union of "map1" and "map2", where we assume for now that
7677 * "map1" and "map2" are disjoint. Note that the basic maps inside
7678 * "map1" or "map2" may not be disjoint from each other.
7679 * Also note that this function is also called from isl_map_union,
7680 * which takes care of handling the situation where "map1" and "map2"
7681 * may not be disjoint.
7683 * If one of the inputs is empty, we can simply return the other input.
7684 * Similarly, if one of the inputs is universal, then it is equal to the union.
7686 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7687 __isl_take isl_map
*map2
)
7691 struct isl_map
*map
= NULL
;
7697 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7698 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7699 "spaces don't match", goto error
);
7710 is_universe
= isl_map_plain_is_universe(map1
);
7711 if (is_universe
< 0)
7718 is_universe
= isl_map_plain_is_universe(map2
);
7719 if (is_universe
< 0)
7726 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7727 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7728 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7730 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7731 map1
->n
+ map2
->n
, flags
);
7734 for (i
= 0; i
< map1
->n
; ++i
) {
7735 map
= isl_map_add_basic_map(map
,
7736 isl_basic_map_copy(map1
->p
[i
]));
7740 for (i
= 0; i
< map2
->n
; ++i
) {
7741 map
= isl_map_add_basic_map(map
,
7742 isl_basic_map_copy(map2
->p
[i
]));
7756 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7757 * guaranteed to be disjoint by the caller.
7759 * Note that this functions is called from within isl_map_make_disjoint,
7760 * so we have to be careful not to touch the constraints of the inputs
7763 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7764 __isl_take isl_map
*map2
)
7766 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7769 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7770 * not be disjoint. The parameters are assumed to have been aligned.
7772 * We currently simply call map_union_disjoint, the internal operation
7773 * of which does not really depend on the inputs being disjoint.
7774 * If the result contains more than one basic map, then we clear
7775 * the disjoint flag since the result may contain basic maps from
7776 * both inputs and these are not guaranteed to be disjoint.
7778 * As a special case, if "map1" and "map2" are obviously equal,
7779 * then we simply return "map1".
7781 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7782 __isl_take isl_map
*map2
)
7789 equal
= isl_map_plain_is_equal(map1
, map2
);
7797 map1
= map_union_disjoint(map1
, map2
);
7801 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7809 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7812 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7813 __isl_take isl_map
*map2
)
7815 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7818 __isl_give isl_set
*isl_set_union_disjoint(
7819 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7821 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7825 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7827 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7830 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7833 * "map" and "set" are assumed to be compatible and non-NULL.
7835 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7836 __isl_take isl_set
*set
,
7837 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7838 __isl_take isl_basic_set
*bset
))
7841 struct isl_map
*result
;
7844 if (isl_set_plain_is_universe(set
)) {
7849 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7850 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7851 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7853 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7854 map
->n
* set
->n
, flags
);
7855 for (i
= 0; result
&& i
< map
->n
; ++i
)
7856 for (j
= 0; j
< set
->n
; ++j
) {
7857 result
= isl_map_add_basic_map(result
,
7858 fn(isl_basic_map_copy(map
->p
[i
]),
7859 isl_basic_set_copy(set
->p
[j
])));
7869 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7870 __isl_take isl_set
*set
)
7874 ok
= isl_map_compatible_range(map
, set
);
7878 isl_die(set
->ctx
, isl_error_invalid
,
7879 "incompatible spaces", goto error
);
7881 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7888 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7889 __isl_take isl_set
*set
)
7891 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7894 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7895 __isl_take isl_set
*set
)
7899 ok
= isl_map_compatible_domain(map
, set
);
7903 isl_die(set
->ctx
, isl_error_invalid
,
7904 "incompatible spaces", goto error
);
7906 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7913 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7914 __isl_take isl_set
*set
)
7916 return isl_map_align_params_map_map_and(map
, set
,
7917 &map_intersect_domain
);
7920 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7921 * in the space B -> C, return the intersection.
7922 * The parameters are assumed to have been aligned.
7924 * The map "factor" is first extended to a map living in the space
7925 * [A -> B] -> C and then a regular intersection is computed.
7927 static __isl_give isl_map
*map_intersect_domain_factor_range(
7928 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7931 isl_map
*ext_factor
;
7933 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7934 ext_factor
= isl_map_universe(space
);
7935 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7936 return map_intersect(map
, ext_factor
);
7939 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7940 * in the space B -> C, return the intersection.
7942 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7943 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7945 return isl_map_align_params_map_map_and(map
, factor
,
7946 &map_intersect_domain_factor_range
);
7949 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7950 * in the space A -> C, return the intersection.
7952 * The map "factor" is first extended to a map living in the space
7953 * A -> [B -> C] and then a regular intersection is computed.
7955 static __isl_give isl_map
*map_intersect_range_factor_range(
7956 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7959 isl_map
*ext_factor
;
7961 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7962 ext_factor
= isl_map_universe(space
);
7963 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7964 return isl_map_intersect(map
, ext_factor
);
7967 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7968 * in the space A -> C, return the intersection.
7970 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7971 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7973 return isl_map_align_params_map_map_and(map
, factor
,
7974 &map_intersect_range_factor_range
);
7977 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7978 __isl_take isl_map
*map2
)
7982 map1
= isl_map_reverse(map1
);
7983 map1
= isl_map_apply_range(map1
, map2
);
7984 return isl_map_reverse(map1
);
7991 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7992 __isl_take isl_map
*map2
)
7994 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7997 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7998 __isl_take isl_map
*map2
)
8001 struct isl_map
*result
;
8007 space
= isl_space_join(isl_space_copy(map1
->dim
),
8008 isl_space_copy(map2
->dim
));
8010 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8013 for (i
= 0; i
< map1
->n
; ++i
)
8014 for (j
= 0; j
< map2
->n
; ++j
) {
8015 result
= isl_map_add_basic_map(result
,
8016 isl_basic_map_apply_range(
8017 isl_basic_map_copy(map1
->p
[i
]),
8018 isl_basic_map_copy(map2
->p
[j
])));
8024 if (result
&& result
->n
<= 1)
8025 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8033 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8034 __isl_take isl_map
*map2
)
8036 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8040 * returns range - domain
8042 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8044 isl_space
*target_space
;
8045 struct isl_basic_set
*bset
;
8053 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8054 bmap
->dim
, isl_dim_out
),
8056 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8057 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8058 if (dim
< 0 || nparam
< 0)
8060 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8061 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8062 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8063 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8065 bmap
= isl_basic_map_free(bmap
);
8066 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8067 for (i
= 0; i
< dim
; ++i
) {
8068 int j
= isl_basic_map_alloc_equality(bmap
);
8070 bmap
= isl_basic_map_free(bmap
);
8073 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8074 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8075 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8076 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8078 bset
= isl_basic_map_domain(bmap
);
8079 bset
= isl_basic_set_reset_space(bset
, target_space
);
8082 isl_basic_map_free(bmap
);
8086 /* Check that domain and range of "map" are the same.
8088 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
8093 space
= isl_map_peek_space(map
);
8094 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8096 return isl_stat_error
;
8098 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8099 "domain and range don't match", return isl_stat_error
);
8104 * returns range - domain
8106 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8110 struct isl_set
*result
;
8115 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8116 map
->dim
, isl_dim_out
),
8118 dim
= isl_map_get_space(map
);
8119 dim
= isl_space_domain(dim
);
8120 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8123 for (i
= 0; i
< map
->n
; ++i
)
8124 result
= isl_set_add_basic_set(result
,
8125 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8134 * returns [domain -> range] -> range - domain
8136 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8137 __isl_take isl_basic_map
*bmap
)
8141 isl_basic_map
*domain
;
8145 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8146 bmap
->dim
, isl_dim_out
))
8147 isl_die(bmap
->ctx
, isl_error_invalid
,
8148 "domain and range don't match", goto error
);
8150 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8151 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8152 if (nparam
< 0 || n
)
8153 return isl_basic_map_free(bmap
);
8155 space
= isl_basic_map_get_space(bmap
);
8156 space
= isl_space_from_range(isl_space_domain(space
));
8157 domain
= isl_basic_map_universe(space
);
8159 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8160 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8161 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8163 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8165 return isl_basic_map_free(bmap
);
8167 for (i
= 0; i
< n
; ++i
) {
8168 k
= isl_basic_map_alloc_equality(bmap
);
8171 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8172 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8173 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8174 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8177 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8178 return isl_basic_map_finalize(bmap
);
8180 isl_basic_map_free(bmap
);
8185 * returns [domain -> range] -> range - domain
8187 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8190 isl_space
*domain_space
;
8192 if (isl_map_check_equal_tuples(map
) < 0)
8193 return isl_map_free(map
);
8195 map
= isl_map_cow(map
);
8199 domain_space
= isl_space_domain(isl_map_get_space(map
));
8200 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8201 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8204 for (i
= 0; i
< map
->n
; ++i
) {
8205 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8209 map
= isl_map_unmark_normalized(map
);
8216 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8218 isl_size n_in
, n_out
;
8220 n_in
= isl_space_dim(space
, isl_dim_in
);
8221 n_out
= isl_space_dim(space
, isl_dim_out
);
8222 if (n_in
< 0 || n_out
< 0)
8225 isl_die(space
->ctx
, isl_error_invalid
,
8226 "number of input and output dimensions needs to be "
8227 "the same", goto error
);
8228 return isl_basic_map_equal(space
, n_in
);
8230 isl_space_free(space
);
8234 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8236 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8239 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8241 isl_space
*dim
= isl_set_get_space(set
);
8243 id
= isl_map_identity(isl_space_map_from_set(dim
));
8244 return isl_map_intersect_range(id
, set
);
8247 /* Construct a basic set with all set dimensions having only non-negative
8250 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8251 __isl_take isl_space
*space
)
8257 struct isl_basic_set
*bset
;
8259 nparam
= isl_space_dim(space
, isl_dim_param
);
8260 dim
= isl_space_dim(space
, isl_dim_set
);
8261 total
= isl_space_dim(space
, isl_dim_all
);
8262 if (nparam
< 0 || dim
< 0 || total
< 0)
8263 space
= isl_space_free(space
);
8264 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8267 for (i
= 0; i
< dim
; ++i
) {
8268 int k
= isl_basic_set_alloc_inequality(bset
);
8271 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8272 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8276 isl_basic_set_free(bset
);
8280 /* Construct the half-space x_pos >= 0.
8282 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8287 isl_basic_set
*nonneg
;
8289 total
= isl_space_dim(space
, isl_dim_all
);
8291 space
= isl_space_free(space
);
8292 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8293 k
= isl_basic_set_alloc_inequality(nonneg
);
8296 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8297 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8299 return isl_basic_set_finalize(nonneg
);
8301 isl_basic_set_free(nonneg
);
8305 /* Construct the half-space x_pos <= -1.
8307 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8314 total
= isl_space_dim(space
, isl_dim_all
);
8316 space
= isl_space_free(space
);
8317 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8318 k
= isl_basic_set_alloc_inequality(neg
);
8321 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8322 isl_int_set_si(neg
->ineq
[k
][0], -1);
8323 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8325 return isl_basic_set_finalize(neg
);
8327 isl_basic_set_free(neg
);
8331 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8332 enum isl_dim_type type
, unsigned first
, unsigned n
)
8336 isl_basic_set
*nonneg
;
8342 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8343 return isl_set_free(set
);
8345 offset
= pos(set
->dim
, type
);
8346 for (i
= 0; i
< n
; ++i
) {
8347 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8348 offset
+ first
+ i
);
8349 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8351 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8357 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8359 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8365 return isl_stat_error
;
8366 if (isl_set_plain_is_empty(set
)) {
8371 return fn(set
, signs
, user
);
8374 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8376 half
= isl_set_intersect(half
, isl_set_copy(set
));
8377 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8381 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8383 half
= isl_set_intersect(half
, set
);
8384 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8387 return isl_stat_error
;
8390 /* Call "fn" on the intersections of "set" with each of the orthants
8391 * (except for obviously empty intersections). The orthant is identified
8392 * by the signs array, with each entry having value 1 or -1 according
8393 * to the sign of the corresponding variable.
8395 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8396 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8405 return isl_stat_error
;
8406 if (isl_set_plain_is_empty(set
))
8409 nparam
= isl_set_dim(set
, isl_dim_param
);
8410 nvar
= isl_set_dim(set
, isl_dim_set
);
8411 if (nparam
< 0 || nvar
< 0)
8412 return isl_stat_error
;
8414 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8416 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8424 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8426 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8429 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8430 __isl_keep isl_basic_map
*bmap2
)
8433 struct isl_map
*map1
;
8434 struct isl_map
*map2
;
8436 if (!bmap1
|| !bmap2
)
8437 return isl_bool_error
;
8439 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8440 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8442 is_subset
= isl_map_is_subset(map1
, map2
);
8450 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8451 __isl_keep isl_basic_set
*bset2
)
8453 return isl_basic_map_is_subset(bset1
, bset2
);
8456 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8457 __isl_keep isl_basic_map
*bmap2
)
8461 if (!bmap1
|| !bmap2
)
8462 return isl_bool_error
;
8463 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8464 if (is_subset
!= isl_bool_true
)
8466 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8470 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8471 __isl_keep isl_basic_set
*bset2
)
8473 return isl_basic_map_is_equal(
8474 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8477 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8483 return isl_bool_error
;
8484 for (i
= 0; i
< map
->n
; ++i
) {
8485 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8487 return isl_bool_error
;
8489 return isl_bool_false
;
8491 return isl_bool_true
;
8494 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8496 return map
? map
->n
== 0 : isl_bool_error
;
8499 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8501 return set
? set
->n
== 0 : isl_bool_error
;
8504 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8506 return isl_map_is_empty(set_to_map(set
));
8509 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8510 __isl_keep isl_map
*map2
)
8513 return isl_bool_error
;
8515 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8518 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8519 __isl_keep isl_set
*set2
)
8522 return isl_bool_error
;
8524 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8527 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8532 return isl_bool_error
;
8533 is_subset
= isl_map_is_subset(map1
, map2
);
8534 if (is_subset
!= isl_bool_true
)
8536 is_subset
= isl_map_is_subset(map2
, map1
);
8540 /* Is "map1" equal to "map2"?
8542 * First check if they are obviously equal.
8543 * If not, then perform a more detailed analysis.
8545 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8549 equal
= isl_map_plain_is_equal(map1
, map2
);
8550 if (equal
< 0 || equal
)
8552 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8555 isl_bool
isl_basic_map_is_strict_subset(
8556 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8560 if (!bmap1
|| !bmap2
)
8561 return isl_bool_error
;
8562 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8563 if (is_subset
!= isl_bool_true
)
8565 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8566 return isl_bool_not(is_subset
);
8569 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8570 __isl_keep isl_map
*map2
)
8575 return isl_bool_error
;
8576 is_subset
= isl_map_is_subset(map1
, map2
);
8577 if (is_subset
!= isl_bool_true
)
8579 is_subset
= isl_map_is_subset(map2
, map1
);
8580 return isl_bool_not(is_subset
);
8583 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8584 __isl_keep isl_set
*set2
)
8586 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8589 /* Is "bmap" obviously equal to the universe with the same space?
8591 * That is, does it not have any constraints?
8593 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8596 return isl_bool_error
;
8597 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8600 /* Is "bset" obviously equal to the universe with the same space?
8602 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8604 return isl_basic_map_plain_is_universe(bset
);
8607 /* If "c" does not involve any existentially quantified variables,
8608 * then set *univ to false and abort
8610 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8612 isl_bool
*univ
= user
;
8615 n
= isl_constraint_dim(c
, isl_dim_div
);
8617 c
= isl_constraint_free(c
);
8618 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8619 isl_constraint_free(c
);
8620 if (*univ
< 0 || !*univ
)
8621 return isl_stat_error
;
8625 /* Is "bmap" equal to the universe with the same space?
8627 * First check if it is obviously equal to the universe.
8628 * If not and if there are any constraints not involving
8629 * existentially quantified variables, then it is certainly
8630 * not equal to the universe.
8631 * Otherwise, check if the universe is a subset of "bmap".
8633 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8637 isl_basic_map
*test
;
8639 univ
= isl_basic_map_plain_is_universe(bmap
);
8640 if (univ
< 0 || univ
)
8642 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
8644 return isl_bool_error
;
8646 return isl_bool_false
;
8647 univ
= isl_bool_true
;
8648 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8650 return isl_bool_error
;
8651 if (univ
< 0 || !univ
)
8653 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8654 univ
= isl_basic_map_is_subset(test
, bmap
);
8655 isl_basic_map_free(test
);
8659 /* Is "bset" equal to the universe with the same space?
8661 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8663 return isl_basic_map_is_universe(bset
);
8666 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8671 return isl_bool_error
;
8673 for (i
= 0; i
< map
->n
; ++i
) {
8674 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8679 return isl_bool_false
;
8682 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8684 return isl_map_plain_is_universe(set_to_map(set
));
8687 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8689 struct isl_basic_set
*bset
= NULL
;
8690 struct isl_vec
*sample
= NULL
;
8691 isl_bool empty
, non_empty
;
8694 return isl_bool_error
;
8696 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8697 return isl_bool_true
;
8699 if (isl_basic_map_plain_is_universe(bmap
))
8700 return isl_bool_false
;
8702 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8703 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8704 copy
= isl_basic_map_remove_redundancies(copy
);
8705 empty
= isl_basic_map_plain_is_empty(copy
);
8706 isl_basic_map_free(copy
);
8710 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8712 return isl_bool_error
;
8714 return isl_bool_false
;
8715 isl_vec_free(bmap
->sample
);
8716 bmap
->sample
= NULL
;
8717 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8719 return isl_bool_error
;
8720 sample
= isl_basic_set_sample_vec(bset
);
8722 return isl_bool_error
;
8723 empty
= sample
->size
== 0;
8724 isl_vec_free(bmap
->sample
);
8725 bmap
->sample
= sample
;
8727 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8732 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8735 return isl_bool_error
;
8736 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8739 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8742 return isl_bool_error
;
8743 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8746 /* Is "bmap" known to be non-empty?
8748 * That is, is the cached sample still valid?
8750 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8755 return isl_bool_error
;
8757 return isl_bool_false
;
8758 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8760 return isl_bool_error
;
8761 if (bmap
->sample
->size
!= 1 + total
)
8762 return isl_bool_false
;
8763 return isl_basic_map_contains(bmap
, bmap
->sample
);
8766 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8768 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8771 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8772 __isl_take isl_basic_map
*bmap2
)
8774 struct isl_map
*map
;
8775 if (!bmap1
|| !bmap2
)
8778 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8780 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8783 map
= isl_map_add_basic_map(map
, bmap1
);
8784 map
= isl_map_add_basic_map(map
, bmap2
);
8787 isl_basic_map_free(bmap1
);
8788 isl_basic_map_free(bmap2
);
8792 struct isl_set
*isl_basic_set_union(
8793 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8795 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8796 bset_to_bmap(bset2
)));
8799 /* Order divs such that any div only depends on previous divs */
8800 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8801 __isl_take isl_basic_map
*bmap
)
8806 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8808 return isl_basic_map_free(bmap
);
8810 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8812 if (isl_int_is_zero(bmap
->div
[i
][0]))
8814 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8819 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8820 "integer division depends on itself",
8821 return isl_basic_map_free(bmap
));
8822 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8830 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8832 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8835 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8842 for (i
= 0; i
< map
->n
; ++i
) {
8843 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8854 /* Sort the local variables of "bset".
8856 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8857 __isl_take isl_basic_set
*bset
)
8859 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8862 /* Apply the expansion computed by isl_merge_divs.
8863 * The expansion itself is given by "exp" while the resulting
8864 * list of divs is given by "div".
8866 * Move the integer divisions of "bmap" into the right position
8867 * according to "exp" and then introduce the additional integer
8868 * divisions, adding div constraints.
8869 * The moving should be done first to avoid moving coefficients
8870 * in the definitions of the extra integer divisions.
8872 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8873 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8878 bmap
= isl_basic_map_cow(bmap
);
8882 if (div
->n_row
< bmap
->n_div
)
8883 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8884 "not an expansion", goto error
);
8886 n_div
= bmap
->n_div
;
8887 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8888 div
->n_row
- n_div
, 0,
8889 2 * (div
->n_row
- n_div
));
8891 for (i
= n_div
; i
< div
->n_row
; ++i
)
8892 if (isl_basic_map_alloc_div(bmap
) < 0)
8895 for (j
= n_div
- 1; j
>= 0; --j
) {
8898 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8903 for (i
= 0; i
< div
->n_row
; ++i
) {
8904 if (j
< n_div
&& exp
[j
] == i
) {
8907 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8908 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8910 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8919 isl_basic_map_free(bmap
);
8924 /* Apply the expansion computed by isl_merge_divs.
8925 * The expansion itself is given by "exp" while the resulting
8926 * list of divs is given by "div".
8928 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8929 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8931 return isl_basic_map_expand_divs(bset
, div
, exp
);
8934 /* Look for a div in dst that corresponds to the div "div" in src.
8935 * The divs before "div" in src and dst are assumed to be the same.
8937 * Return the position of the corresponding div in dst
8938 * if there is one. Otherwise, return a position beyond the integer divisions.
8939 * Return -1 on error.
8941 static int find_div(__isl_keep isl_basic_map
*dst
,
8942 __isl_keep isl_basic_map
*src
, unsigned div
)
8948 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8949 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8950 if (n_div
< 0 || v_div
< 0)
8952 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8953 for (i
= div
; i
< n_div
; ++i
)
8954 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8955 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8961 /* Align the divs of "dst" to those of "src", adding divs from "src"
8962 * if needed. That is, make sure that the first src->n_div divs
8963 * of the result are equal to those of src.
8965 * The result is not finalized as by design it will have redundant
8966 * divs if any divs from "src" were copied.
8968 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8969 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8978 return isl_basic_map_free(dst
);
8980 if (src
->n_div
== 0)
8983 known
= isl_basic_map_divs_known(src
);
8985 return isl_basic_map_free(dst
);
8987 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8988 "some src divs are unknown",
8989 return isl_basic_map_free(dst
));
8991 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8993 return isl_basic_map_free(dst
);
8995 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8997 return isl_basic_map_free(dst
);
9000 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9002 dst
= isl_basic_map_free(dst
);
9003 for (i
= 0; i
< src
->n_div
; ++i
) {
9004 int j
= find_div(dst
, src
, i
);
9006 dst
= isl_basic_map_free(dst
);
9007 if (j
== dst_n_div
) {
9009 int extra
= src
->n_div
- i
;
9010 dst
= isl_basic_map_cow(dst
);
9013 dst
= isl_basic_map_extend_space(dst
,
9014 isl_space_copy(dst
->dim
),
9015 extra
, 0, 2 * extra
);
9018 j
= isl_basic_map_alloc_div(dst
);
9021 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9022 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9024 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9029 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9033 isl_basic_map_free(src
);
9036 isl_basic_map_free(src
);
9037 isl_basic_map_free(dst
);
9041 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9049 map
= isl_map_compute_divs(map
);
9050 map
= isl_map_cow(map
);
9054 for (i
= 1; i
< map
->n
; ++i
)
9055 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9056 for (i
= 1; i
< map
->n
; ++i
) {
9057 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9059 return isl_map_free(map
);
9062 map
= isl_map_unmark_normalized(map
);
9066 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9068 return isl_map_align_divs_internal(map
);
9071 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9073 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9076 /* Align the divs of the basic maps in "map" to those
9077 * of the basic maps in "list", as well as to the other basic maps in "map".
9078 * The elements in "list" are assumed to have known divs.
9080 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9081 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9086 n
= isl_basic_map_list_n_basic_map(list
);
9087 map
= isl_map_compute_divs(map
);
9088 map
= isl_map_cow(map
);
9090 return isl_map_free(map
);
9094 for (i
= 0; i
< n
; ++i
) {
9095 isl_basic_map
*bmap
;
9097 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9098 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9099 isl_basic_map_free(bmap
);
9102 return isl_map_free(map
);
9104 return isl_map_align_divs_internal(map
);
9107 /* Align the divs of each element of "list" to those of "bmap".
9108 * Both "bmap" and the elements of "list" are assumed to have known divs.
9110 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9111 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9116 n
= isl_basic_map_list_n_basic_map(list
);
9118 return isl_basic_map_list_free(list
);
9120 for (i
= 0; i
< n
; ++i
) {
9121 isl_basic_map
*bmap_i
;
9123 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9124 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9125 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9131 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9132 __isl_take isl_map
*map
)
9136 ok
= isl_map_compatible_domain(map
, set
);
9140 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9141 "incompatible spaces", goto error
);
9142 map
= isl_map_intersect_domain(map
, set
);
9143 set
= isl_map_range(map
);
9151 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9152 __isl_take isl_map
*map
)
9154 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9157 /* There is no need to cow as removing empty parts doesn't change
9158 * the meaning of the set.
9160 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9167 for (i
= map
->n
- 1; i
>= 0; --i
)
9168 map
= remove_if_empty(map
, i
);
9173 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9175 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9178 /* Create a binary relation that maps the shared initial "pos" dimensions
9179 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9181 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9182 __isl_keep isl_basic_set
*bset2
, int pos
)
9184 isl_basic_map
*bmap1
;
9185 isl_basic_map
*bmap2
;
9187 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9188 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9189 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9190 isl_dim_out
, 0, pos
);
9191 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9192 isl_dim_out
, 0, pos
);
9193 return isl_basic_map_range_product(bmap1
, bmap2
);
9196 /* Given two basic sets bset1 and bset2, compute the maximal difference
9197 * between the values of dimension pos in bset1 and those in bset2
9198 * for any common value of the parameters and dimensions preceding pos.
9200 static enum isl_lp_result
basic_set_maximal_difference_at(
9201 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9202 int pos
, isl_int
*opt
)
9204 isl_basic_map
*bmap1
;
9205 struct isl_ctx
*ctx
;
9206 struct isl_vec
*obj
;
9210 enum isl_lp_result res
;
9212 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9213 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9214 if (nparam
< 0 || dim1
< 0 || !bset2
)
9215 return isl_lp_error
;
9217 bmap1
= join_initial(bset1
, bset2
, pos
);
9218 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9220 return isl_lp_error
;
9223 obj
= isl_vec_alloc(ctx
, 1 + total
);
9226 isl_seq_clr(obj
->block
.data
, 1 + total
);
9227 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9228 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9229 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9231 isl_basic_map_free(bmap1
);
9235 isl_basic_map_free(bmap1
);
9236 return isl_lp_error
;
9239 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9240 * for any common value of the parameters and dimensions preceding pos
9241 * in both basic sets, the values of dimension pos in bset1 are
9242 * smaller or larger than those in bset2.
9245 * 1 if bset1 follows bset2
9246 * -1 if bset1 precedes bset2
9247 * 0 if bset1 and bset2 are incomparable
9248 * -2 if some error occurred.
9250 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9251 __isl_keep isl_basic_set
*bset2
, int pos
)
9254 enum isl_lp_result res
;
9259 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9261 if (res
== isl_lp_empty
)
9263 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9264 res
== isl_lp_unbounded
)
9266 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9275 /* Given two basic sets bset1 and bset2, check whether
9276 * for any common value of the parameters and dimensions preceding pos
9277 * there is a value of dimension pos in bset1 that is larger
9278 * than a value of the same dimension in bset2.
9281 * 1 if there exists such a pair
9282 * 0 if there is no such pair, but there is a pair of equal values
9284 * -2 if some error occurred.
9286 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9287 __isl_keep isl_basic_set
*bset2
, int pos
)
9290 isl_basic_map
*bmap
;
9293 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9296 bmap
= join_initial(bset1
, bset2
, pos
);
9297 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9298 isl_dim_out
, dim1
- pos
);
9299 empty
= isl_basic_map_is_empty(bmap
);
9303 isl_basic_map_free(bmap
);
9306 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9307 isl_dim_out
, dim1
- pos
);
9308 empty
= isl_basic_map_is_empty(bmap
);
9311 isl_basic_map_free(bmap
);
9316 isl_basic_map_free(bmap
);
9320 /* Given two sets set1 and set2, check whether
9321 * for any common value of the parameters and dimensions preceding pos
9322 * there is a value of dimension pos in set1 that is larger
9323 * than a value of the same dimension in set2.
9326 * 1 if there exists such a pair
9327 * 0 if there is no such pair, but there is a pair of equal values
9329 * -2 if some error occurred.
9331 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9332 __isl_keep isl_set
*set2
, int pos
)
9340 for (i
= 0; i
< set1
->n
; ++i
)
9341 for (j
= 0; j
< set2
->n
; ++j
) {
9343 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9344 if (f
== 1 || f
== -2)
9353 static isl_bool
isl_basic_map_plain_has_fixed_var(
9354 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9360 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9362 return isl_bool_error
;
9363 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9364 for (; d
+1 > pos
; --d
)
9365 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9369 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9370 return isl_bool_false
;
9371 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9372 return isl_bool_false
;
9373 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9374 return isl_bool_false
;
9376 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9377 return isl_bool_true
;
9379 return isl_bool_false
;
9382 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9383 unsigned pos
, isl_int
*val
)
9391 return isl_bool_error
;
9393 return isl_bool_false
;
9395 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9398 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9399 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9400 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9401 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9402 fixed
= isl_bool_false
;
9405 isl_int_set(*val
, v
);
9411 static isl_bool
isl_basic_set_plain_has_fixed_var(
9412 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9414 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9418 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9419 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9421 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9422 return isl_bool_error
;
9423 return isl_basic_map_plain_has_fixed_var(bmap
,
9424 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9427 /* If "bmap" obviously lies on a hyperplane where the given dimension
9428 * has a fixed value, then return that value.
9429 * Otherwise return NaN.
9431 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9432 __isl_keep isl_basic_map
*bmap
,
9433 enum isl_dim_type type
, unsigned pos
)
9441 ctx
= isl_basic_map_get_ctx(bmap
);
9442 v
= isl_val_alloc(ctx
);
9445 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9447 return isl_val_free(v
);
9449 isl_int_set_si(v
->d
, 1);
9453 return isl_val_nan(ctx
);
9456 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9457 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9459 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9460 return isl_bool_error
;
9461 return isl_map_plain_has_fixed_var(map
,
9462 map_offset(map
, type
) - 1 + pos
, val
);
9465 /* If "map" obviously lies on a hyperplane where the given dimension
9466 * has a fixed value, then return that value.
9467 * Otherwise return NaN.
9469 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9470 enum isl_dim_type type
, unsigned pos
)
9478 ctx
= isl_map_get_ctx(map
);
9479 v
= isl_val_alloc(ctx
);
9482 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9484 return isl_val_free(v
);
9486 isl_int_set_si(v
->d
, 1);
9490 return isl_val_nan(ctx
);
9493 /* If "set" obviously lies on a hyperplane where the given dimension
9494 * has a fixed value, then return that value.
9495 * Otherwise return NaN.
9497 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9498 enum isl_dim_type type
, unsigned pos
)
9500 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9503 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9504 * then return this fixed value in *val.
9506 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9507 unsigned dim
, isl_int
*val
)
9511 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
9513 return isl_bool_error
;
9514 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
9517 /* Return -1 if the constraint "c1" should be sorted before "c2"
9518 * and 1 if it should be sorted after "c2".
9519 * Return 0 if the two constraints are the same (up to the constant term).
9521 * In particular, if a constraint involves later variables than another
9522 * then it is sorted after this other constraint.
9523 * uset_gist depends on constraints without existentially quantified
9524 * variables sorting first.
9526 * For constraints that have the same latest variable, those
9527 * with the same coefficient for this latest variable (first in absolute value
9528 * and then in actual value) are grouped together.
9529 * This is useful for detecting pairs of constraints that can
9530 * be chained in their printed representation.
9532 * Finally, within a group, constraints are sorted according to
9533 * their coefficients (excluding the constant term).
9535 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9537 isl_int
**c1
= (isl_int
**) p1
;
9538 isl_int
**c2
= (isl_int
**) p2
;
9540 unsigned size
= *(unsigned *) arg
;
9543 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9544 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9549 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9552 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9556 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9559 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9560 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9561 * and 0 if the two constraints are the same (up to the constant term).
9563 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9564 isl_int
*c1
, isl_int
*c2
)
9569 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9573 return sort_constraint_cmp(&c1
, &c2
, &size
);
9576 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9577 __isl_take isl_basic_map
*bmap
)
9584 if (bmap
->n_ineq
== 0)
9586 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9588 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9590 return isl_basic_map_free(bmap
);
9592 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9593 &sort_constraint_cmp
, &size
) < 0)
9594 return isl_basic_map_free(bmap
);
9595 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9599 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9600 __isl_take isl_basic_set
*bset
)
9602 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9603 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9606 __isl_give isl_basic_map
*isl_basic_map_normalize(
9607 __isl_take isl_basic_map
*bmap
)
9609 bmap
= isl_basic_map_remove_redundancies(bmap
);
9610 bmap
= isl_basic_map_sort_constraints(bmap
);
9613 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9614 __isl_keep isl_basic_map
*bmap2
)
9618 isl_space
*space1
, *space2
;
9620 if (!bmap1
|| !bmap2
)
9625 space1
= isl_basic_map_peek_space(bmap1
);
9626 space2
= isl_basic_map_peek_space(bmap2
);
9627 cmp
= isl_space_cmp(space1
, space2
);
9630 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9631 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9632 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9633 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9634 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9636 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9638 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9640 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9641 return bmap1
->n_eq
- bmap2
->n_eq
;
9642 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9643 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9644 if (bmap1
->n_div
!= bmap2
->n_div
)
9645 return bmap1
->n_div
- bmap2
->n_div
;
9646 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9649 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9650 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9654 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9655 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9659 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9660 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9667 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9668 __isl_keep isl_basic_set
*bset2
)
9670 return isl_basic_map_plain_cmp(bset1
, bset2
);
9673 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9679 if (set1
->n
!= set2
->n
)
9680 return set1
->n
- set2
->n
;
9682 for (i
= 0; i
< set1
->n
; ++i
) {
9683 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9691 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9692 __isl_keep isl_basic_map
*bmap2
)
9694 if (!bmap1
|| !bmap2
)
9695 return isl_bool_error
;
9696 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9699 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9700 __isl_keep isl_basic_set
*bset2
)
9702 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9703 bset_to_bmap(bset2
));
9706 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9708 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9709 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9711 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9714 /* Sort the basic maps of "map" and remove duplicate basic maps.
9716 * While removing basic maps, we make sure that the basic maps remain
9717 * sorted because isl_map_normalize expects the basic maps of the result
9720 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9724 map
= isl_map_remove_empty_parts(map
);
9727 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9728 for (i
= map
->n
- 1; i
>= 1; --i
) {
9729 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9731 isl_basic_map_free(map
->p
[i
-1]);
9732 for (j
= i
; j
< map
->n
; ++j
)
9733 map
->p
[j
- 1] = map
->p
[j
];
9740 /* Remove obvious duplicates among the basic maps of "map".
9742 * Unlike isl_map_normalize, this function does not remove redundant
9743 * constraints and only removes duplicates that have exactly the same
9744 * constraints in the input. It does sort the constraints and
9745 * the basic maps to ease the detection of duplicates.
9747 * If "map" has already been normalized or if the basic maps are
9748 * disjoint, then there can be no duplicates.
9750 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9753 isl_basic_map
*bmap
;
9759 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9761 for (i
= 0; i
< map
->n
; ++i
) {
9762 bmap
= isl_basic_map_copy(map
->p
[i
]);
9763 bmap
= isl_basic_map_sort_constraints(bmap
);
9765 return isl_map_free(map
);
9766 isl_basic_map_free(map
->p
[i
]);
9770 map
= sort_and_remove_duplicates(map
);
9774 /* We normalize in place, but if anything goes wrong we need
9775 * to return NULL, so we need to make sure we don't change the
9776 * meaning of any possible other copies of map.
9778 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9781 struct isl_basic_map
*bmap
;
9785 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9787 for (i
= 0; i
< map
->n
; ++i
) {
9788 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9791 isl_basic_map_free(map
->p
[i
]);
9795 map
= sort_and_remove_duplicates(map
);
9797 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9804 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9806 return set_from_map(isl_map_normalize(set_to_map(set
)));
9809 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9810 __isl_keep isl_map
*map2
)
9816 return isl_bool_error
;
9819 return isl_bool_true
;
9820 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9821 return isl_bool_false
;
9823 map1
= isl_map_copy(map1
);
9824 map2
= isl_map_copy(map2
);
9825 map1
= isl_map_normalize(map1
);
9826 map2
= isl_map_normalize(map2
);
9829 equal
= map1
->n
== map2
->n
;
9830 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9831 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9841 return isl_bool_error
;
9844 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9845 __isl_keep isl_set
*set2
)
9847 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9850 /* Return the basic maps in "map" as a list.
9852 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9853 __isl_keep isl_map
*map
)
9857 isl_basic_map_list
*list
;
9861 ctx
= isl_map_get_ctx(map
);
9862 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9864 for (i
= 0; i
< map
->n
; ++i
) {
9865 isl_basic_map
*bmap
;
9867 bmap
= isl_basic_map_copy(map
->p
[i
]);
9868 list
= isl_basic_map_list_add(list
, bmap
);
9874 /* Return the intersection of the elements in the non-empty list "list".
9875 * All elements are assumed to live in the same space.
9877 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9878 __isl_take isl_basic_map_list
*list
)
9882 isl_basic_map
*bmap
;
9884 n
= isl_basic_map_list_n_basic_map(list
);
9888 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9889 "expecting non-empty list", goto error
);
9891 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9892 for (i
= 1; i
< n
; ++i
) {
9893 isl_basic_map
*bmap_i
;
9895 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9896 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9899 isl_basic_map_list_free(list
);
9902 isl_basic_map_list_free(list
);
9906 /* Return the intersection of the elements in the non-empty list "list".
9907 * All elements are assumed to live in the same space.
9909 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9910 __isl_take isl_basic_set_list
*list
)
9912 return isl_basic_map_list_intersect(list
);
9915 /* Return the union of the elements of "list".
9916 * The list is required to have at least one element.
9918 __isl_give isl_set
*isl_basic_set_list_union(
9919 __isl_take isl_basic_set_list
*list
)
9924 isl_basic_set
*bset
;
9927 n
= isl_basic_set_list_n_basic_set(list
);
9931 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9932 "expecting non-empty list", goto error
);
9934 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9935 space
= isl_basic_set_get_space(bset
);
9936 isl_basic_set_free(bset
);
9938 set
= isl_set_alloc_space(space
, n
, 0);
9939 for (i
= 0; i
< n
; ++i
) {
9940 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9941 set
= isl_set_add_basic_set(set
, bset
);
9944 isl_basic_set_list_free(list
);
9947 isl_basic_set_list_free(list
);
9951 /* Return the union of the elements in the non-empty list "list".
9952 * All elements are assumed to live in the same space.
9954 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9960 n
= isl_set_list_n_set(list
);
9964 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9965 "expecting non-empty list", goto error
);
9967 set
= isl_set_list_get_set(list
, 0);
9968 for (i
= 1; i
< n
; ++i
) {
9971 set_i
= isl_set_list_get_set(list
, i
);
9972 set
= isl_set_union(set
, set_i
);
9975 isl_set_list_free(list
);
9978 isl_set_list_free(list
);
9982 __isl_give isl_basic_map
*isl_basic_map_product(
9983 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9985 isl_space
*space_result
= NULL
;
9986 struct isl_basic_map
*bmap
;
9987 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9988 struct isl_dim_map
*dim_map1
, *dim_map2
;
9990 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9992 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9993 isl_space_copy(bmap2
->dim
));
9995 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9996 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9997 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9998 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9999 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10001 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10002 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10003 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10004 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10005 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10006 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10007 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10008 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10009 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10010 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10011 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10013 bmap
= isl_basic_map_alloc_space(space_result
,
10014 bmap1
->n_div
+ bmap2
->n_div
,
10015 bmap1
->n_eq
+ bmap2
->n_eq
,
10016 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10017 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10018 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10019 bmap
= isl_basic_map_simplify(bmap
);
10020 return isl_basic_map_finalize(bmap
);
10022 isl_basic_map_free(bmap1
);
10023 isl_basic_map_free(bmap2
);
10027 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10028 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10030 isl_basic_map
*prod
;
10032 prod
= isl_basic_map_product(bmap1
, bmap2
);
10033 prod
= isl_basic_map_flatten(prod
);
10037 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10038 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10040 return isl_basic_map_flat_range_product(bset1
, bset2
);
10043 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10044 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10046 isl_space
*space1
, *space2
;
10047 isl_space
*space_result
= NULL
;
10048 isl_basic_map
*bmap
;
10049 isl_size in1
, in2
, out
, nparam
;
10050 unsigned total
, pos
;
10051 struct isl_dim_map
*dim_map1
, *dim_map2
;
10053 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10054 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10055 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10056 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10057 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10060 space1
= isl_basic_map_get_space(bmap1
);
10061 space2
= isl_basic_map_get_space(bmap2
);
10062 space_result
= isl_space_domain_product(space1
, space2
);
10064 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10065 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10066 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10067 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10068 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10069 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10070 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10071 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10072 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10073 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10074 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10076 bmap
= isl_basic_map_alloc_space(space_result
,
10077 bmap1
->n_div
+ bmap2
->n_div
,
10078 bmap1
->n_eq
+ bmap2
->n_eq
,
10079 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10080 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10081 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10082 bmap
= isl_basic_map_simplify(bmap
);
10083 return isl_basic_map_finalize(bmap
);
10085 isl_basic_map_free(bmap1
);
10086 isl_basic_map_free(bmap2
);
10090 __isl_give isl_basic_map
*isl_basic_map_range_product(
10091 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10094 isl_space
*space_result
= NULL
;
10095 isl_basic_map
*bmap
;
10096 isl_size in
, out1
, out2
, nparam
;
10097 unsigned total
, pos
;
10098 struct isl_dim_map
*dim_map1
, *dim_map2
;
10100 rational
= isl_basic_map_is_rational(bmap1
);
10101 if (rational
>= 0 && rational
)
10102 rational
= isl_basic_map_is_rational(bmap2
);
10103 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10104 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10105 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10106 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10107 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10110 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10113 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10114 isl_space_copy(bmap2
->dim
));
10116 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10117 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10118 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10119 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10120 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10121 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10122 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10123 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10124 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10125 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10126 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10128 bmap
= isl_basic_map_alloc_space(space_result
,
10129 bmap1
->n_div
+ bmap2
->n_div
,
10130 bmap1
->n_eq
+ bmap2
->n_eq
,
10131 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10132 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10133 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10135 bmap
= isl_basic_map_set_rational(bmap
);
10136 bmap
= isl_basic_map_simplify(bmap
);
10137 return isl_basic_map_finalize(bmap
);
10139 isl_basic_map_free(bmap1
);
10140 isl_basic_map_free(bmap2
);
10144 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10145 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10147 isl_basic_map
*prod
;
10149 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10150 prod
= isl_basic_map_flatten_range(prod
);
10154 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10155 * and collect the results.
10156 * The result live in the space obtained by calling "space_product"
10157 * on the spaces of "map1" and "map2".
10158 * If "remove_duplicates" is set then the result may contain duplicates
10159 * (even if the inputs do not) and so we try and remove the obvious
10162 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10163 __isl_take isl_map
*map2
,
10164 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10165 __isl_take isl_space
*right
),
10166 __isl_give isl_basic_map
*(*basic_map_product
)(
10167 __isl_take isl_basic_map
*left
,
10168 __isl_take isl_basic_map
*right
),
10169 int remove_duplicates
)
10171 unsigned flags
= 0;
10172 struct isl_map
*result
;
10176 m
= isl_map_has_equal_params(map1
, map2
);
10180 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10181 "parameters don't match", goto error
);
10183 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10184 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10185 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10187 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10188 isl_space_copy(map2
->dim
)),
10189 map1
->n
* map2
->n
, flags
);
10192 for (i
= 0; i
< map1
->n
; ++i
)
10193 for (j
= 0; j
< map2
->n
; ++j
) {
10194 struct isl_basic_map
*part
;
10195 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10196 isl_basic_map_copy(map2
->p
[j
]));
10197 if (isl_basic_map_is_empty(part
))
10198 isl_basic_map_free(part
);
10200 result
= isl_map_add_basic_map(result
, part
);
10204 if (remove_duplicates
)
10205 result
= isl_map_remove_obvious_duplicates(result
);
10206 isl_map_free(map1
);
10207 isl_map_free(map2
);
10210 isl_map_free(map1
);
10211 isl_map_free(map2
);
10215 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10217 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10218 __isl_take isl_map
*map2
)
10220 return map_product(map1
, map2
, &isl_space_product
,
10221 &isl_basic_map_product
, 0);
10224 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10225 __isl_take isl_map
*map2
)
10227 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10230 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10232 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10233 __isl_take isl_map
*map2
)
10237 prod
= isl_map_product(map1
, map2
);
10238 prod
= isl_map_flatten(prod
);
10242 /* Given two set A and B, construct its Cartesian product A x B.
10244 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10246 return isl_map_range_product(set1
, set2
);
10249 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10250 __isl_take isl_set
*set2
)
10252 return isl_map_flat_range_product(set1
, set2
);
10255 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10257 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10258 __isl_take isl_map
*map2
)
10260 return map_product(map1
, map2
, &isl_space_domain_product
,
10261 &isl_basic_map_domain_product
, 1);
10264 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10266 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10267 __isl_take isl_map
*map2
)
10269 return map_product(map1
, map2
, &isl_space_range_product
,
10270 &isl_basic_map_range_product
, 1);
10273 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10274 __isl_take isl_map
*map2
)
10276 return isl_map_align_params_map_map_and(map1
, map2
,
10277 &map_domain_product_aligned
);
10280 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10281 __isl_take isl_map
*map2
)
10283 return isl_map_align_params_map_map_and(map1
, map2
,
10284 &map_range_product_aligned
);
10287 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10289 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10292 isl_size total1
, keep1
, total2
, keep2
;
10294 total1
= isl_map_dim(map
, isl_dim_in
);
10295 total2
= isl_map_dim(map
, isl_dim_out
);
10296 if (total1
< 0 || total2
< 0)
10297 return isl_map_free(map
);
10298 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10299 !isl_space_range_is_wrapping(map
->dim
))
10300 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10301 "not a product", return isl_map_free(map
));
10303 space
= isl_map_get_space(map
);
10304 space
= isl_space_factor_domain(space
);
10305 keep1
= isl_space_dim(space
, isl_dim_in
);
10306 keep2
= isl_space_dim(space
, isl_dim_out
);
10307 if (keep1
< 0 || keep2
< 0)
10308 map
= isl_map_free(map
);
10309 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10310 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10311 map
= isl_map_reset_space(map
, space
);
10316 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10318 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10321 isl_size total1
, keep1
, total2
, keep2
;
10323 total1
= isl_map_dim(map
, isl_dim_in
);
10324 total2
= isl_map_dim(map
, isl_dim_out
);
10325 if (total1
< 0 || total2
< 0)
10326 return isl_map_free(map
);
10327 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10328 !isl_space_range_is_wrapping(map
->dim
))
10329 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10330 "not a product", return isl_map_free(map
));
10332 space
= isl_map_get_space(map
);
10333 space
= isl_space_factor_range(space
);
10334 keep1
= isl_space_dim(space
, isl_dim_in
);
10335 keep2
= isl_space_dim(space
, isl_dim_out
);
10336 if (keep1
< 0 || keep2
< 0)
10337 map
= isl_map_free(map
);
10338 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10339 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10340 map
= isl_map_reset_space(map
, space
);
10345 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10347 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10350 isl_size total
, keep
;
10352 total
= isl_map_dim(map
, isl_dim_in
);
10354 return isl_map_free(map
);
10355 if (!isl_space_domain_is_wrapping(map
->dim
))
10356 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10357 "domain is not a product", return isl_map_free(map
));
10359 space
= isl_map_get_space(map
);
10360 space
= isl_space_domain_factor_domain(space
);
10361 keep
= isl_space_dim(space
, isl_dim_in
);
10363 map
= isl_map_free(map
);
10364 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10365 map
= isl_map_reset_space(map
, space
);
10370 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10372 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10375 isl_size total
, keep
;
10377 total
= isl_map_dim(map
, isl_dim_in
);
10379 return isl_map_free(map
);
10380 if (!isl_space_domain_is_wrapping(map
->dim
))
10381 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10382 "domain is not a product", return isl_map_free(map
));
10384 space
= isl_map_get_space(map
);
10385 space
= isl_space_domain_factor_range(space
);
10386 keep
= isl_space_dim(space
, isl_dim_in
);
10388 map
= isl_map_free(map
);
10389 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10390 map
= isl_map_reset_space(map
, space
);
10395 /* Given a map A -> [B -> C], extract the map A -> B.
10397 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10400 isl_size total
, keep
;
10402 total
= isl_map_dim(map
, isl_dim_out
);
10404 return isl_map_free(map
);
10405 if (!isl_space_range_is_wrapping(map
->dim
))
10406 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10407 "range is not a product", return isl_map_free(map
));
10409 space
= isl_map_get_space(map
);
10410 space
= isl_space_range_factor_domain(space
);
10411 keep
= isl_space_dim(space
, isl_dim_out
);
10413 map
= isl_map_free(map
);
10414 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10415 map
= isl_map_reset_space(map
, space
);
10420 /* Given a map A -> [B -> C], extract the map A -> C.
10422 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10425 isl_size total
, keep
;
10427 total
= isl_map_dim(map
, isl_dim_out
);
10429 return isl_map_free(map
);
10430 if (!isl_space_range_is_wrapping(map
->dim
))
10431 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10432 "range is not a product", return isl_map_free(map
));
10434 space
= isl_map_get_space(map
);
10435 space
= isl_space_range_factor_range(space
);
10436 keep
= isl_space_dim(space
, isl_dim_out
);
10438 map
= isl_map_free(map
);
10439 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10440 map
= isl_map_reset_space(map
, space
);
10445 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10447 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10448 __isl_take isl_map
*map2
)
10452 prod
= isl_map_domain_product(map1
, map2
);
10453 prod
= isl_map_flatten_domain(prod
);
10457 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10459 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10460 __isl_take isl_map
*map2
)
10464 prod
= isl_map_range_product(map1
, map2
);
10465 prod
= isl_map_flatten_range(prod
);
10469 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10472 uint32_t hash
= isl_hash_init();
10477 bmap
= isl_basic_map_copy(bmap
);
10478 bmap
= isl_basic_map_normalize(bmap
);
10479 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10482 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10483 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10485 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10486 isl_hash_hash(hash
, c_hash
);
10488 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10489 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10491 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10492 isl_hash_hash(hash
, c_hash
);
10494 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10495 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10497 if (isl_int_is_zero(bmap
->div
[i
][0]))
10499 isl_hash_byte(hash
, i
& 0xFF);
10500 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10501 isl_hash_hash(hash
, c_hash
);
10503 isl_basic_map_free(bmap
);
10507 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10509 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10512 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10519 map
= isl_map_copy(map
);
10520 map
= isl_map_normalize(map
);
10524 hash
= isl_hash_init();
10525 for (i
= 0; i
< map
->n
; ++i
) {
10526 uint32_t bmap_hash
;
10527 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10528 isl_hash_hash(hash
, bmap_hash
);
10536 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10538 return isl_map_get_hash(set_to_map(set
));
10541 /* Return the number of basic maps in the (current) representation of "map".
10543 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
10545 return map
? map
->n
: isl_size_error
;
10548 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
10550 return set
? set
->n
: isl_size_error
;
10553 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10554 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10559 return isl_stat_error
;
10561 for (i
= 0; i
< map
->n
; ++i
)
10562 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10563 return isl_stat_error
;
10565 return isl_stat_ok
;
10568 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10569 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10574 return isl_stat_error
;
10576 for (i
= 0; i
< set
->n
; ++i
)
10577 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10578 return isl_stat_error
;
10580 return isl_stat_ok
;
10583 /* Return a list of basic sets, the union of which is equal to "set".
10585 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10586 __isl_keep isl_set
*set
)
10589 isl_basic_set_list
*list
;
10594 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10595 for (i
= 0; i
< set
->n
; ++i
) {
10596 isl_basic_set
*bset
;
10598 bset
= isl_basic_set_copy(set
->p
[i
]);
10599 list
= isl_basic_set_list_add(list
, bset
);
10605 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10612 bset
= isl_basic_set_cow(bset
);
10616 space
= isl_basic_set_get_space(bset
);
10617 space
= isl_space_lift(space
, bset
->n_div
);
10620 isl_space_free(bset
->dim
);
10622 bset
->extra
-= bset
->n_div
;
10625 bset
= isl_basic_set_finalize(bset
);
10629 isl_basic_set_free(bset
);
10633 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10639 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10644 set
= isl_set_cow(set
);
10648 n_div
= set
->p
[0]->n_div
;
10649 space
= isl_set_get_space(set
);
10650 space
= isl_space_lift(space
, n_div
);
10653 isl_space_free(set
->dim
);
10656 for (i
= 0; i
< set
->n
; ++i
) {
10657 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10668 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10673 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
10676 size
+= bset
->n_eq
* (1 + dim
);
10677 size
+= bset
->n_ineq
* (1 + dim
);
10678 size
+= bset
->n_div
* (2 + dim
);
10683 int isl_set_size(__isl_keep isl_set
*set
)
10691 for (i
= 0; i
< set
->n
; ++i
)
10692 size
+= isl_basic_set_size(set
->p
[i
]);
10697 /* Check if there is any lower bound (if lower == 0) and/or upper
10698 * bound (if upper == 0) on the specified dim.
10700 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10701 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10705 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10706 return isl_bool_error
;
10708 pos
+= isl_basic_map_offset(bmap
, type
);
10710 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10711 if (isl_int_is_zero(bmap
->div
[i
][0]))
10713 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10714 return isl_bool_true
;
10717 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10718 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10719 return isl_bool_true
;
10721 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10722 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10729 return lower
&& upper
;
10732 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10733 enum isl_dim_type type
, unsigned pos
)
10735 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10738 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10739 enum isl_dim_type type
, unsigned pos
)
10741 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10744 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10745 enum isl_dim_type type
, unsigned pos
)
10747 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10750 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10751 enum isl_dim_type type
, unsigned pos
)
10756 return isl_bool_error
;
10758 for (i
= 0; i
< map
->n
; ++i
) {
10760 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10761 if (bounded
< 0 || !bounded
)
10765 return isl_bool_true
;
10768 /* Return true if the specified dim is involved in both an upper bound
10769 * and a lower bound.
10771 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10772 enum isl_dim_type type
, unsigned pos
)
10774 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10777 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10779 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10780 enum isl_dim_type type
, unsigned pos
,
10781 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10782 enum isl_dim_type type
, unsigned pos
))
10787 return isl_bool_error
;
10789 for (i
= 0; i
< map
->n
; ++i
) {
10791 bounded
= fn(map
->p
[i
], type
, pos
);
10792 if (bounded
< 0 || bounded
)
10796 return isl_bool_false
;
10799 /* Return 1 if the specified dim is involved in any lower bound.
10801 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10802 enum isl_dim_type type
, unsigned pos
)
10804 return has_any_bound(set
, type
, pos
,
10805 &isl_basic_map_dim_has_lower_bound
);
10808 /* Return 1 if the specified dim is involved in any upper bound.
10810 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10811 enum isl_dim_type type
, unsigned pos
)
10813 return has_any_bound(set
, type
, pos
,
10814 &isl_basic_map_dim_has_upper_bound
);
10817 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10819 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10820 enum isl_dim_type type
, unsigned pos
,
10821 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10822 enum isl_dim_type type
, unsigned pos
))
10827 return isl_bool_error
;
10829 for (i
= 0; i
< map
->n
; ++i
) {
10831 bounded
= fn(map
->p
[i
], type
, pos
);
10832 if (bounded
< 0 || !bounded
)
10836 return isl_bool_true
;
10839 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10841 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10842 enum isl_dim_type type
, unsigned pos
)
10844 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10847 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10849 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10850 enum isl_dim_type type
, unsigned pos
)
10852 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10855 /* For each of the "n" variables starting at "first", determine
10856 * the sign of the variable and put the results in the first "n"
10857 * elements of the array "signs".
10859 * 1 means that the variable is non-negative
10860 * -1 means that the variable is non-positive
10861 * 0 means the variable attains both positive and negative values.
10863 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10864 unsigned first
, unsigned n
, int *signs
)
10866 isl_vec
*bound
= NULL
;
10867 struct isl_tab
*tab
= NULL
;
10868 struct isl_tab_undo
*snap
;
10872 total
= isl_basic_set_dim(bset
, isl_dim_all
);
10873 if (total
< 0 || !signs
)
10874 return isl_stat_error
;
10876 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
10877 tab
= isl_tab_from_basic_set(bset
, 0);
10878 if (!bound
|| !tab
)
10881 isl_seq_clr(bound
->el
, bound
->size
);
10882 isl_int_set_si(bound
->el
[0], -1);
10884 snap
= isl_tab_snap(tab
);
10885 for (i
= 0; i
< n
; ++i
) {
10888 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10889 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10891 empty
= tab
->empty
;
10892 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10893 if (isl_tab_rollback(tab
, snap
) < 0)
10901 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10902 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10904 empty
= tab
->empty
;
10905 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10906 if (isl_tab_rollback(tab
, snap
) < 0)
10909 signs
[i
] = empty
? -1 : 0;
10913 isl_vec_free(bound
);
10914 return isl_stat_ok
;
10917 isl_vec_free(bound
);
10918 return isl_stat_error
;
10921 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10922 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10924 if (!bset
|| !signs
)
10925 return isl_stat_error
;
10926 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10927 return isl_stat_error
;
10929 first
+= pos(bset
->dim
, type
) - 1;
10930 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10933 /* Is it possible for the integer division "div" to depend (possibly
10934 * indirectly) on any output dimensions?
10936 * If the div is undefined, then we conservatively assume that it
10937 * may depend on them.
10938 * Otherwise, we check if it actually depends on them or on any integer
10939 * divisions that may depend on them.
10941 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10944 isl_size n_out
, n_div
;
10945 unsigned o_out
, o_div
;
10947 if (isl_int_is_zero(bmap
->div
[div
][0]))
10948 return isl_bool_true
;
10950 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10952 return isl_bool_error
;
10953 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10955 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10956 return isl_bool_true
;
10958 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10960 return isl_bool_error
;
10961 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10963 for (i
= 0; i
< n_div
; ++i
) {
10964 isl_bool may_involve
;
10966 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10968 may_involve
= div_may_involve_output(bmap
, i
);
10969 if (may_involve
< 0 || may_involve
)
10970 return may_involve
;
10973 return isl_bool_false
;
10976 /* Return the first integer division of "bmap" in the range
10977 * [first, first + n[ that may depend on any output dimensions and
10978 * that has a non-zero coefficient in "c" (where the first coefficient
10979 * in "c" corresponds to integer division "first").
10981 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10982 isl_int
*c
, int first
, int n
)
10989 for (k
= first
; k
< first
+ n
; ++k
) {
10990 isl_bool may_involve
;
10992 if (isl_int_is_zero(c
[k
]))
10994 may_involve
= div_may_involve_output(bmap
, k
);
10995 if (may_involve
< 0)
11004 /* Look for a pair of inequality constraints in "bmap" of the form
11006 * -l + i >= 0 or i >= l
11008 * n + l - i >= 0 or i <= l + n
11010 * with n < "m" and i the output dimension at position "pos".
11011 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11012 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11013 * and earlier output dimensions, as well as integer divisions that do
11014 * not involve any of the output dimensions.
11016 * Return the index of the first inequality constraint or bmap->n_ineq
11017 * if no such pair can be found.
11019 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11020 int pos
, isl_int m
)
11025 isl_size n_div
, n_out
;
11026 unsigned o_div
, o_out
;
11029 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11030 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11031 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11032 if (total
< 0 || n_out
< 0 || n_div
< 0)
11035 ctx
= isl_basic_map_get_ctx(bmap
);
11036 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11037 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11038 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11039 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11041 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11042 n_out
- (pos
+ 1)) != -1)
11044 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11047 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11048 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11051 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11052 bmap
->ineq
[j
] + 1, total
))
11056 if (j
>= bmap
->n_ineq
)
11058 isl_int_add(bmap
->ineq
[i
][0],
11059 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11060 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11061 isl_int_sub(bmap
->ineq
[i
][0],
11062 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11065 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11071 return bmap
->n_ineq
;
11074 /* Return the index of the equality of "bmap" that defines
11075 * the output dimension "pos" in terms of earlier dimensions.
11076 * The equality may also involve integer divisions, as long
11077 * as those integer divisions are defined in terms of
11078 * parameters or input dimensions.
11079 * In this case, *div is set to the number of integer divisions and
11080 * *ineq is set to the number of inequality constraints (provided
11081 * div and ineq are not NULL).
11083 * The equality may also involve a single integer division involving
11084 * the output dimensions (typically only output dimension "pos") as
11085 * long as the coefficient of output dimension "pos" is 1 or -1 and
11086 * there is a pair of constraints i >= l and i <= l + n, with i referring
11087 * to output dimension "pos", l an expression involving only earlier
11088 * dimensions and n smaller than the coefficient of the integer division
11089 * in the equality. In this case, the output dimension can be defined
11090 * in terms of a modulo expression that does not involve the integer division.
11091 * *div is then set to this single integer division and
11092 * *ineq is set to the index of constraint i >= l.
11094 * Return bmap->n_eq if there is no such equality.
11095 * Return -1 on error.
11097 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11098 int pos
, int *div
, int *ineq
)
11101 isl_size n_div
, n_out
;
11102 unsigned o_div
, o_out
;
11104 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11105 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11106 if (n_out
< 0 || n_div
< 0)
11109 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11110 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11113 *ineq
= bmap
->n_ineq
;
11116 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11117 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11119 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11120 n_out
- (pos
+ 1)) != -1)
11122 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11126 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11127 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11129 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11130 k
+ 1, n_div
- (k
+1)) < n_div
)
11132 l
= find_modulo_constraint_pair(bmap
, pos
,
11133 bmap
->eq
[j
][o_div
+ k
]);
11136 if (l
>= bmap
->n_ineq
)
11148 /* Check if the given basic map is obviously single-valued.
11149 * In particular, for each output dimension, check that there is
11150 * an equality that defines the output dimension in terms of
11151 * earlier dimensions.
11153 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11158 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11160 return isl_bool_error
;
11162 for (i
= 0; i
< n_out
; ++i
) {
11165 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11168 return isl_bool_error
;
11169 if (eq
>= bmap
->n_eq
)
11170 return isl_bool_false
;
11173 return isl_bool_true
;
11176 /* Check if the given basic map is single-valued.
11177 * We simply compute
11181 * and check if the result is a subset of the identity mapping.
11183 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11186 isl_basic_map
*test
;
11190 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11194 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11195 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11197 space
= isl_basic_map_get_space(bmap
);
11198 space
= isl_space_map_from_set(isl_space_range(space
));
11199 id
= isl_basic_map_identity(space
);
11201 sv
= isl_basic_map_is_subset(test
, id
);
11203 isl_basic_map_free(test
);
11204 isl_basic_map_free(id
);
11209 /* Check if the given map is obviously single-valued.
11211 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11214 return isl_bool_error
;
11216 return isl_bool_true
;
11218 return isl_bool_false
;
11220 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11223 /* Check if the given map is single-valued.
11224 * We simply compute
11228 * and check if the result is a subset of the identity mapping.
11230 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11237 sv
= isl_map_plain_is_single_valued(map
);
11241 test
= isl_map_reverse(isl_map_copy(map
));
11242 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11244 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11245 id
= isl_map_identity(dim
);
11247 sv
= isl_map_is_subset(test
, id
);
11249 isl_map_free(test
);
11255 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11259 map
= isl_map_copy(map
);
11260 map
= isl_map_reverse(map
);
11261 in
= isl_map_is_single_valued(map
);
11267 /* Check if the given map is obviously injective.
11269 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11273 map
= isl_map_copy(map
);
11274 map
= isl_map_reverse(map
);
11275 in
= isl_map_plain_is_single_valued(map
);
11281 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11285 sv
= isl_map_is_single_valued(map
);
11289 return isl_map_is_injective(map
);
11292 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11294 return isl_map_is_single_valued(set_to_map(set
));
11297 /* Does "map" only map elements to themselves?
11299 * If the domain and range spaces are different, then "map"
11300 * is considered not to be an identity relation, even if it is empty.
11301 * Otherwise, construct the maximal identity relation and
11302 * check whether "map" is a subset of this relation.
11304 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11308 isl_bool equal
, is_identity
;
11310 space
= isl_map_get_space(map
);
11311 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11312 isl_space_free(space
);
11313 if (equal
< 0 || !equal
)
11316 id
= isl_map_identity(isl_map_get_space(map
));
11317 is_identity
= isl_map_is_subset(map
, id
);
11320 return is_identity
;
11323 int isl_map_is_translation(__isl_keep isl_map
*map
)
11328 delta
= isl_map_deltas(isl_map_copy(map
));
11329 ok
= isl_set_is_singleton(delta
);
11330 isl_set_free(delta
);
11335 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11337 if (isl_seq_first_non_zero(p
, pos
) != -1)
11339 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11344 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11347 isl_size nvar
, n_div
;
11350 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11352 return isl_bool_error
;
11354 return isl_bool_false
;
11356 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11358 return isl_bool_error
;
11359 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11360 for (j
= 0; j
< nvar
; ++j
) {
11361 int lower
= 0, upper
= 0;
11362 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11363 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11365 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11366 return isl_bool_false
;
11369 if (i
< bset
->n_eq
)
11371 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11372 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11374 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11375 return isl_bool_false
;
11376 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11381 if (!lower
|| !upper
)
11382 return isl_bool_false
;
11385 return isl_bool_true
;
11388 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11391 return isl_bool_error
;
11393 return isl_bool_false
;
11395 return isl_basic_set_is_box(set
->p
[0]);
11398 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11401 return isl_bool_error
;
11403 return isl_space_is_wrapping(bset
->dim
);
11406 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11409 return isl_bool_error
;
11411 return isl_space_is_wrapping(set
->dim
);
11414 /* Modify the space of "map" through a call to "change".
11415 * If "can_change" is set (not NULL), then first call it to check
11416 * if the modification is allowed, printing the error message "cannot_change"
11419 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11420 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11421 const char *cannot_change
,
11422 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11430 ok
= can_change
? can_change(map
) : isl_bool_true
;
11432 return isl_map_free(map
);
11434 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11435 return isl_map_free(map
));
11437 space
= change(isl_map_get_space(map
));
11438 map
= isl_map_reset_space(map
, space
);
11443 /* Is the domain of "map" a wrapped relation?
11445 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11448 return isl_bool_error
;
11450 return isl_space_domain_is_wrapping(map
->dim
);
11453 /* Does "map" have a wrapped relation in both domain and range?
11455 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11457 return isl_space_is_product(isl_map_peek_space(map
));
11460 /* Is the range of "map" a wrapped relation?
11462 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11465 return isl_bool_error
;
11467 return isl_space_range_is_wrapping(map
->dim
);
11470 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11472 bmap
= isl_basic_map_cow(bmap
);
11476 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11480 bmap
= isl_basic_map_finalize(bmap
);
11482 return bset_from_bmap(bmap
);
11484 isl_basic_map_free(bmap
);
11488 /* Given a map A -> B, return the set (A -> B).
11490 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11492 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11495 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11497 bset
= isl_basic_set_cow(bset
);
11501 bset
->dim
= isl_space_unwrap(bset
->dim
);
11505 bset
= isl_basic_set_finalize(bset
);
11507 return bset_to_bmap(bset
);
11509 isl_basic_set_free(bset
);
11513 /* Given a set (A -> B), return the map A -> B.
11514 * Error out if "set" is not of the form (A -> B).
11516 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11518 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11519 "not a wrapping set", &isl_space_unwrap
);
11522 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11523 enum isl_dim_type type
)
11528 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11531 bmap
= isl_basic_map_cow(bmap
);
11535 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11539 bmap
= isl_basic_map_finalize(bmap
);
11543 isl_basic_map_free(bmap
);
11547 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11548 enum isl_dim_type type
)
11555 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11558 map
= isl_map_cow(map
);
11562 for (i
= 0; i
< map
->n
; ++i
) {
11563 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11567 map
->dim
= isl_space_reset(map
->dim
, type
);
11577 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11582 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11585 bmap
= isl_basic_map_cow(bmap
);
11589 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11593 bmap
= isl_basic_map_finalize(bmap
);
11597 isl_basic_map_free(bmap
);
11601 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11603 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11606 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11607 __isl_take isl_basic_map
*bmap
)
11612 if (!bmap
->dim
->nested
[0])
11615 bmap
= isl_basic_map_cow(bmap
);
11619 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11623 bmap
= isl_basic_map_finalize(bmap
);
11627 isl_basic_map_free(bmap
);
11631 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11632 __isl_take isl_basic_map
*bmap
)
11637 if (!bmap
->dim
->nested
[1])
11640 bmap
= isl_basic_map_cow(bmap
);
11644 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11648 bmap
= isl_basic_map_finalize(bmap
);
11652 isl_basic_map_free(bmap
);
11656 /* Remove any internal structure from the spaces of domain and range of "map".
11658 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11663 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11666 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11669 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11671 return set_from_map(isl_map_flatten(set_to_map(set
)));
11674 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11676 isl_space
*space
, *flat_space
;
11679 space
= isl_set_get_space(set
);
11680 flat_space
= isl_space_flatten(isl_space_copy(space
));
11681 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11683 map
= isl_map_intersect_domain(map
, set
);
11688 /* Remove any internal structure from the space of the domain of "map".
11690 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11695 if (!map
->dim
->nested
[0])
11698 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11701 /* Remove any internal structure from the space of the range of "map".
11703 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11708 if (!map
->dim
->nested
[1])
11711 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11714 /* Reorder the dimensions of "bmap" according to the given dim_map
11715 * and set the dimension specification to "space" and
11716 * perform Gaussian elimination on the result.
11718 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11719 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11721 isl_basic_map
*res
;
11725 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11726 if (n_div
< 0 || !space
|| !dim_map
)
11729 flags
= bmap
->flags
;
11730 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11731 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11732 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11733 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11734 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11736 res
->flags
= flags
;
11737 res
= isl_basic_map_gauss(res
, NULL
);
11738 res
= isl_basic_map_finalize(res
);
11741 isl_dim_map_free(dim_map
);
11742 isl_basic_map_free(bmap
);
11743 isl_space_free(space
);
11747 /* Reorder the dimensions of "map" according to given reordering.
11749 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11750 __isl_take isl_reordering
*r
)
11753 struct isl_dim_map
*dim_map
;
11755 map
= isl_map_cow(map
);
11756 dim_map
= isl_dim_map_from_reordering(r
);
11757 if (!map
|| !r
|| !dim_map
)
11760 for (i
= 0; i
< map
->n
; ++i
) {
11761 struct isl_dim_map
*dim_map_i
;
11764 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11766 space
= isl_reordering_get_space(r
);
11767 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11773 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11774 map
= isl_map_unmark_normalized(map
);
11776 isl_reordering_free(r
);
11777 isl_dim_map_free(dim_map
);
11780 isl_dim_map_free(dim_map
);
11782 isl_reordering_free(r
);
11786 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11787 __isl_take isl_reordering
*r
)
11789 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11792 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11793 __isl_take isl_space
*model
)
11798 if (!map
|| !model
)
11801 ctx
= isl_space_get_ctx(model
);
11802 if (!isl_space_has_named_params(model
))
11803 isl_die(ctx
, isl_error_invalid
,
11804 "model has unnamed parameters", goto error
);
11805 if (isl_map_check_named_params(map
) < 0)
11807 aligned
= isl_map_space_has_equal_params(map
, model
);
11811 isl_reordering
*exp
;
11813 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11814 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11815 map
= isl_map_realign(map
, exp
);
11818 isl_space_free(model
);
11821 isl_space_free(model
);
11826 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11827 __isl_take isl_space
*model
)
11829 return isl_map_align_params(set
, model
);
11832 /* Align the parameters of "bmap" to those of "model", introducing
11833 * additional parameters if needed.
11835 __isl_give isl_basic_map
*isl_basic_map_align_params(
11836 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11839 isl_bool equal_params
;
11841 if (!bmap
|| !model
)
11844 ctx
= isl_space_get_ctx(model
);
11845 if (!isl_space_has_named_params(model
))
11846 isl_die(ctx
, isl_error_invalid
,
11847 "model has unnamed parameters", goto error
);
11848 if (isl_basic_map_check_named_params(bmap
) < 0)
11850 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11851 if (equal_params
< 0)
11853 if (!equal_params
) {
11854 isl_reordering
*exp
;
11855 struct isl_dim_map
*dim_map
;
11857 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11858 exp
= isl_reordering_extend_space(exp
,
11859 isl_basic_map_get_space(bmap
));
11860 dim_map
= isl_dim_map_from_reordering(exp
);
11861 bmap
= isl_basic_map_realign(bmap
,
11862 isl_reordering_get_space(exp
),
11863 isl_dim_map_extend(dim_map
, bmap
));
11864 isl_reordering_free(exp
);
11865 isl_dim_map_free(dim_map
);
11868 isl_space_free(model
);
11871 isl_space_free(model
);
11872 isl_basic_map_free(bmap
);
11876 /* Do "bset" and "space" have the same parameters?
11878 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11879 __isl_keep isl_space
*space
)
11881 isl_space
*bset_space
;
11883 bset_space
= isl_basic_set_peek_space(bset
);
11884 return isl_space_has_equal_params(bset_space
, space
);
11887 /* Do "map" and "space" have the same parameters?
11889 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11890 __isl_keep isl_space
*space
)
11892 isl_space
*map_space
;
11894 map_space
= isl_map_peek_space(map
);
11895 return isl_space_has_equal_params(map_space
, space
);
11898 /* Do "set" and "space" have the same parameters?
11900 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11901 __isl_keep isl_space
*space
)
11903 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11906 /* Align the parameters of "bset" to those of "model", introducing
11907 * additional parameters if needed.
11909 __isl_give isl_basic_set
*isl_basic_set_align_params(
11910 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11912 return isl_basic_map_align_params(bset
, model
);
11915 /* Drop all parameters not referenced by "map".
11917 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11922 n
= isl_map_dim(map
, isl_dim_param
);
11923 if (isl_map_check_named_params(map
) < 0 || n
< 0)
11924 return isl_map_free(map
);
11926 for (i
= n
- 1; i
>= 0; i
--) {
11929 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11931 return isl_map_free(map
);
11933 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11939 /* Drop all parameters not referenced by "set".
11941 __isl_give isl_set
*isl_set_drop_unused_params(
11942 __isl_take isl_set
*set
)
11944 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11947 /* Drop all parameters not referenced by "bmap".
11949 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11950 __isl_take isl_basic_map
*bmap
)
11955 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
11956 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
11957 return isl_basic_map_free(bmap
);
11959 for (i
= nparam
- 1; i
>= 0; i
--) {
11962 involves
= isl_basic_map_involves_dims(bmap
,
11963 isl_dim_param
, i
, 1);
11965 return isl_basic_map_free(bmap
);
11967 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11973 /* Drop all parameters not referenced by "bset".
11975 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11976 __isl_take isl_basic_set
*bset
)
11978 return bset_from_bmap(isl_basic_map_drop_unused_params(
11979 bset_to_bmap(bset
)));
11982 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11983 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11984 enum isl_dim_type c2
, enum isl_dim_type c3
,
11985 enum isl_dim_type c4
, enum isl_dim_type c5
)
11987 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11988 struct isl_mat
*mat
;
11993 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11996 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
11999 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12000 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12001 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12002 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12004 return isl_mat_free(mat
);
12005 for (k
= 0; k
< dim
; ++k
) {
12006 isl_int_set(mat
->row
[i
][pos
],
12007 bmap
->eq
[i
][off
+ k
]);
12015 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12016 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12017 enum isl_dim_type c2
, enum isl_dim_type c3
,
12018 enum isl_dim_type c4
, enum isl_dim_type c5
)
12020 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12021 struct isl_mat
*mat
;
12026 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12029 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12032 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12033 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12034 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12035 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12037 return isl_mat_free(mat
);
12038 for (k
= 0; k
< dim
; ++k
) {
12039 isl_int_set(mat
->row
[i
][pos
],
12040 bmap
->ineq
[i
][off
+ k
]);
12048 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12049 __isl_take isl_space
*space
,
12050 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12051 enum isl_dim_type c2
, enum isl_dim_type c3
,
12052 enum isl_dim_type c4
, enum isl_dim_type c5
)
12054 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12055 isl_basic_map
*bmap
= NULL
;
12062 dim
= isl_space_dim(space
, isl_dim_all
);
12063 if (dim
< 0 || !eq
|| !ineq
)
12066 if (eq
->n_col
!= ineq
->n_col
)
12067 isl_die(space
->ctx
, isl_error_invalid
,
12068 "equalities and inequalities matrices should have "
12069 "same number of columns", goto error
);
12073 if (eq
->n_col
< total
)
12074 isl_die(space
->ctx
, isl_error_invalid
,
12075 "number of columns too small", goto error
);
12077 extra
= eq
->n_col
- total
;
12079 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12080 eq
->n_row
, ineq
->n_row
);
12083 for (i
= 0; i
< extra
; ++i
) {
12084 k
= isl_basic_map_alloc_div(bmap
);
12087 isl_int_set_si(bmap
->div
[k
][0], 0);
12089 for (i
= 0; i
< eq
->n_row
; ++i
) {
12090 l
= isl_basic_map_alloc_equality(bmap
);
12093 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12094 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12095 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12098 for (k
= 0; k
< dim
; ++k
) {
12099 isl_int_set(bmap
->eq
[l
][off
+ k
],
12105 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12106 l
= isl_basic_map_alloc_inequality(bmap
);
12109 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12110 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12111 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12114 for (k
= 0; k
< dim
; ++k
) {
12115 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12116 ineq
->row
[i
][pos
]);
12122 isl_space_free(space
);
12124 isl_mat_free(ineq
);
12126 bmap
= isl_basic_map_simplify(bmap
);
12127 return isl_basic_map_finalize(bmap
);
12129 isl_space_free(space
);
12131 isl_mat_free(ineq
);
12132 isl_basic_map_free(bmap
);
12136 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12137 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12138 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12140 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12141 c1
, c2
, c3
, c4
, isl_dim_in
);
12144 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12145 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12146 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12148 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12149 c1
, c2
, c3
, c4
, isl_dim_in
);
12152 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12153 __isl_take isl_space
*dim
,
12154 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12155 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12157 isl_basic_map
*bmap
;
12158 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12159 c1
, c2
, c3
, c4
, isl_dim_in
);
12160 return bset_from_bmap(bmap
);
12163 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12166 return isl_bool_error
;
12168 return isl_space_can_zip(bmap
->dim
);
12171 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12174 return isl_bool_error
;
12176 return isl_space_can_zip(map
->dim
);
12179 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12180 * (A -> C) -> (B -> D).
12182 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12192 if (!isl_basic_map_can_zip(bmap
))
12193 isl_die(bmap
->ctx
, isl_error_invalid
,
12194 "basic map cannot be zipped", goto error
);
12195 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12196 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12197 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12198 if (n_in
< 0 || n1
< 0 || n2
< 0)
12199 return isl_basic_map_free(bmap
);
12200 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12201 bmap
= isl_basic_map_cow(bmap
);
12202 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12205 bmap
->dim
= isl_space_zip(bmap
->dim
);
12208 bmap
= isl_basic_map_mark_final(bmap
);
12211 isl_basic_map_free(bmap
);
12215 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12216 * (A -> C) -> (B -> D).
12218 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12225 if (!isl_map_can_zip(map
))
12226 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12229 map
= isl_map_cow(map
);
12233 for (i
= 0; i
< map
->n
; ++i
) {
12234 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12239 map
->dim
= isl_space_zip(map
->dim
);
12249 /* Can we apply isl_basic_map_curry to "bmap"?
12250 * That is, does it have a nested relation in its domain?
12252 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12255 return isl_bool_error
;
12257 return isl_space_can_curry(bmap
->dim
);
12260 /* Can we apply isl_map_curry to "map"?
12261 * That is, does it have a nested relation in its domain?
12263 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12266 return isl_bool_error
;
12268 return isl_space_can_curry(map
->dim
);
12271 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12274 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12280 if (!isl_basic_map_can_curry(bmap
))
12281 isl_die(bmap
->ctx
, isl_error_invalid
,
12282 "basic map cannot be curried", goto error
);
12283 bmap
= isl_basic_map_cow(bmap
);
12286 bmap
->dim
= isl_space_curry(bmap
->dim
);
12289 bmap
= isl_basic_map_mark_final(bmap
);
12292 isl_basic_map_free(bmap
);
12296 /* Given a map (A -> B) -> C, return the corresponding map
12299 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12301 return isl_map_change_space(map
, &isl_map_can_curry
,
12302 "map cannot be curried", &isl_space_curry
);
12305 /* Can isl_map_range_curry be applied to "map"?
12306 * That is, does it have a nested relation in its range,
12307 * the domain of which is itself a nested relation?
12309 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12312 return isl_bool_error
;
12314 return isl_space_can_range_curry(map
->dim
);
12317 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12318 * A -> (B -> (C -> D)).
12320 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12322 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12323 "map range cannot be curried",
12324 &isl_space_range_curry
);
12327 /* Can we apply isl_basic_map_uncurry to "bmap"?
12328 * That is, does it have a nested relation in its domain?
12330 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12333 return isl_bool_error
;
12335 return isl_space_can_uncurry(bmap
->dim
);
12338 /* Can we apply isl_map_uncurry to "map"?
12339 * That is, does it have a nested relation in its domain?
12341 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12344 return isl_bool_error
;
12346 return isl_space_can_uncurry(map
->dim
);
12349 /* Given a basic map A -> (B -> C), return the corresponding basic map
12352 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12358 if (!isl_basic_map_can_uncurry(bmap
))
12359 isl_die(bmap
->ctx
, isl_error_invalid
,
12360 "basic map cannot be uncurried",
12361 return isl_basic_map_free(bmap
));
12362 bmap
= isl_basic_map_cow(bmap
);
12365 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12367 return isl_basic_map_free(bmap
);
12368 bmap
= isl_basic_map_mark_final(bmap
);
12372 /* Given a map A -> (B -> C), return the corresponding map
12375 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12377 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12378 "map cannot be uncurried", &isl_space_uncurry
);
12381 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12382 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12384 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12387 /* Construct a basic map where the given dimensions are equal to each other.
12389 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12390 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12392 isl_basic_map
*bmap
= NULL
;
12396 total
= isl_space_dim(space
, isl_dim_all
);
12398 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12399 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12402 if (type1
== type2
&& pos1
== pos2
)
12403 return isl_basic_map_universe(space
);
12405 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12406 i
= isl_basic_map_alloc_equality(bmap
);
12409 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12410 pos1
+= isl_basic_map_offset(bmap
, type1
);
12411 pos2
+= isl_basic_map_offset(bmap
, type2
);
12412 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12413 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12414 bmap
= isl_basic_map_finalize(bmap
);
12415 isl_space_free(space
);
12418 isl_space_free(space
);
12419 isl_basic_map_free(bmap
);
12423 /* Add a constraint imposing that the given two dimensions are equal.
12425 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12426 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12430 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12432 bmap
= isl_basic_map_intersect(bmap
, eq
);
12437 /* Add a constraint imposing that the given two dimensions are equal.
12439 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12440 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12442 isl_basic_map
*bmap
;
12444 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12446 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12451 /* Add a constraint imposing that the given two dimensions have opposite values.
12453 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12454 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12456 isl_basic_map
*bmap
= NULL
;
12460 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12461 return isl_map_free(map
);
12462 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12463 return isl_map_free(map
);
12465 total
= isl_map_dim(map
, isl_dim_all
);
12467 return isl_map_free(map
);
12468 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12469 i
= isl_basic_map_alloc_equality(bmap
);
12472 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12473 pos1
+= isl_basic_map_offset(bmap
, type1
);
12474 pos2
+= isl_basic_map_offset(bmap
, type2
);
12475 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12476 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12477 bmap
= isl_basic_map_finalize(bmap
);
12479 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12483 isl_basic_map_free(bmap
);
12488 /* Construct a constraint imposing that the value of the first dimension is
12489 * greater than or equal to that of the second.
12491 static __isl_give isl_constraint
*constraint_order_ge(
12492 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12493 enum isl_dim_type type2
, int pos2
)
12497 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12498 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12499 space
= isl_space_free(space
);
12503 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12505 if (type1
== type2
&& pos1
== pos2
)
12508 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12509 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12514 /* Add a constraint imposing that the value of the first dimension is
12515 * greater than or equal to that of the second.
12517 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12518 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12523 if (type1
== type2
&& pos1
== pos2
)
12525 space
= isl_basic_map_get_space(bmap
);
12526 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12527 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12532 /* Add a constraint imposing that the value of the first dimension is
12533 * greater than or equal to that of the second.
12535 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12536 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12541 if (type1
== type2
&& pos1
== pos2
)
12543 space
= isl_map_get_space(map
);
12544 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12545 map
= isl_map_add_constraint(map
, c
);
12550 /* Add a constraint imposing that the value of the first dimension is
12551 * less than or equal to that of the second.
12553 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12554 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12556 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12559 /* Construct a basic map where the value of the first dimension is
12560 * greater than that of the second.
12562 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12563 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12565 isl_basic_map
*bmap
= NULL
;
12569 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12570 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12573 if (type1
== type2
&& pos1
== pos2
)
12574 return isl_basic_map_empty(space
);
12576 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12577 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12578 i
= isl_basic_map_alloc_inequality(bmap
);
12579 if (total
< 0 || i
< 0)
12580 return isl_basic_map_free(bmap
);
12581 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
12582 pos1
+= isl_basic_map_offset(bmap
, type1
);
12583 pos2
+= isl_basic_map_offset(bmap
, type2
);
12584 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12585 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12586 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12587 bmap
= isl_basic_map_finalize(bmap
);
12591 isl_space_free(space
);
12592 isl_basic_map_free(bmap
);
12596 /* Add a constraint imposing that the value of the first dimension is
12597 * greater than that of the second.
12599 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12600 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12604 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12606 bmap
= isl_basic_map_intersect(bmap
, gt
);
12611 /* Add a constraint imposing that the value of the first dimension is
12612 * greater than that of the second.
12614 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12615 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12617 isl_basic_map
*bmap
;
12619 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12621 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12626 /* Add a constraint imposing that the value of the first dimension is
12627 * smaller than that of the second.
12629 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12630 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12632 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12635 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12639 isl_local_space
*ls
;
12644 if (!isl_basic_map_divs_known(bmap
))
12645 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12646 "some divs are unknown", return NULL
);
12648 ls
= isl_basic_map_get_local_space(bmap
);
12649 div
= isl_local_space_get_div(ls
, pos
);
12650 isl_local_space_free(ls
);
12655 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12658 return isl_basic_map_get_div(bset
, pos
);
12661 /* Plug in "subs" for dimension "type", "pos" of "bset".
12663 * Let i be the dimension to replace and let "subs" be of the form
12667 * Any integer division with a non-zero coefficient for i,
12669 * floor((a i + g)/m)
12673 * floor((a f + d g)/(m d))
12675 * Constraints of the form
12683 * We currently require that "subs" is an integral expression.
12684 * Handling rational expressions may require us to add stride constraints
12685 * as we do in isl_basic_set_preimage_multi_aff.
12687 __isl_give isl_basic_set
*isl_basic_set_substitute(
12688 __isl_take isl_basic_set
*bset
,
12689 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12696 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12699 bset
= isl_basic_set_cow(bset
);
12700 if (!bset
|| !subs
)
12703 ctx
= isl_basic_set_get_ctx(bset
);
12704 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12705 isl_die(ctx
, isl_error_invalid
,
12706 "spaces don't match", goto error
);
12707 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
12711 isl_die(ctx
, isl_error_unsupported
,
12712 "cannot handle divs yet", goto error
);
12713 if (!isl_int_is_one(subs
->v
->el
[0]))
12714 isl_die(ctx
, isl_error_invalid
,
12715 "can only substitute integer expressions", goto error
);
12717 pos
+= isl_basic_set_offset(bset
, type
);
12721 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12722 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12724 isl_int_set(v
, bset
->eq
[i
][pos
]);
12725 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12726 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12727 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12730 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12731 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12733 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12734 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12735 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12736 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12739 for (i
= 0; i
< bset
->n_div
; ++i
) {
12740 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12742 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12743 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12744 isl_seq_combine(bset
->div
[i
] + 1,
12745 subs
->v
->el
[0], bset
->div
[i
] + 1,
12746 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12747 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12752 bset
= isl_basic_set_simplify(bset
);
12753 return isl_basic_set_finalize(bset
);
12755 isl_basic_set_free(bset
);
12759 /* Plug in "subs" for dimension "type", "pos" of "set".
12761 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12762 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12766 if (set
&& isl_set_plain_is_empty(set
))
12769 set
= isl_set_cow(set
);
12773 for (i
= set
->n
- 1; i
>= 0; --i
) {
12774 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12775 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12786 /* Check if the range of "ma" is compatible with the domain or range
12787 * (depending on "type") of "bmap".
12789 static isl_stat
check_basic_map_compatible_range_multi_aff(
12790 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12791 __isl_keep isl_multi_aff
*ma
)
12794 isl_space
*ma_space
;
12796 ma_space
= isl_multi_aff_get_space(ma
);
12798 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12802 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12803 "parameters don't match", goto error
);
12804 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12808 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12809 "spaces don't match", goto error
);
12811 isl_space_free(ma_space
);
12812 return isl_stat_ok
;
12814 isl_space_free(ma_space
);
12815 return isl_stat_error
;
12818 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12819 * coefficients before the transformed range of dimensions,
12820 * the "n_after" coefficients after the transformed range of dimensions
12821 * and the coefficients of the other divs in "bmap".
12823 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12824 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12829 isl_local_space
*ls
;
12834 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12835 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12836 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12837 if (n_param
< 0 || n_set
< 0)
12838 return isl_basic_map_free(bmap
);
12840 for (i
= 0; i
< n_div
; ++i
) {
12841 int o_bmap
= 0, o_ls
= 0;
12843 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12844 o_bmap
+= 1 + 1 + n_param
;
12845 o_ls
+= 1 + 1 + n_param
;
12846 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12847 o_bmap
+= n_before
;
12848 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12849 ls
->div
->row
[i
] + o_ls
, n_set
);
12852 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12854 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12855 ls
->div
->row
[i
] + o_ls
, n_div
);
12858 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12859 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12864 isl_local_space_free(ls
);
12867 isl_local_space_free(ls
);
12868 return isl_basic_map_free(bmap
);
12871 /* How many stride constraints does "ma" enforce?
12872 * That is, how many of the affine expressions have a denominator
12873 * different from one?
12875 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12880 for (i
= 0; i
< ma
->n
; ++i
)
12881 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12887 /* For each affine expression in ma of the form
12889 * x_i = (f_i y + h_i)/m_i
12891 * with m_i different from one, add a constraint to "bmap"
12894 * f_i y + h_i = m_i alpha_i
12896 * with alpha_i an additional existentially quantified variable.
12898 * The input variables of "ma" correspond to a subset of the variables
12899 * of "bmap". There are "n_before" variables in "bmap" before this
12900 * subset and "n_after" variables after this subset.
12901 * The integer divisions of the affine expressions in "ma" are assumed
12902 * to have been aligned. There are "n_div_ma" of them and
12903 * they appear first in "bmap", straight after the "n_after" variables.
12905 static __isl_give isl_basic_map
*add_ma_strides(
12906 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12907 int n_before
, int n_after
, int n_div_ma
)
12915 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12916 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12917 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12918 if (total
< 0 || n_param
< 0 || n_in
< 0)
12919 return isl_basic_map_free(bmap
);
12920 for (i
= 0; i
< ma
->n
; ++i
) {
12921 int o_bmap
= 0, o_ma
= 1;
12923 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12925 div
= isl_basic_map_alloc_div(bmap
);
12926 k
= isl_basic_map_alloc_equality(bmap
);
12927 if (div
< 0 || k
< 0)
12929 isl_int_set_si(bmap
->div
[div
][0], 0);
12930 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12931 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12932 o_bmap
+= 1 + n_param
;
12933 o_ma
+= 1 + n_param
;
12934 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12935 o_bmap
+= n_before
;
12936 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12937 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12940 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12942 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12943 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12944 o_bmap
+= n_div_ma
;
12946 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12947 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12953 isl_basic_map_free(bmap
);
12957 /* Replace the domain or range space (depending on "type) of "space" by "set".
12959 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12960 enum isl_dim_type type
, __isl_take isl_space
*set
)
12962 if (type
== isl_dim_in
) {
12963 space
= isl_space_range(space
);
12964 space
= isl_space_map_from_domain_and_range(set
, space
);
12966 space
= isl_space_domain(space
);
12967 space
= isl_space_map_from_domain_and_range(space
, set
);
12973 /* Compute the preimage of the domain or range (depending on "type")
12974 * of "bmap" under the function represented by "ma".
12975 * In other words, plug in "ma" in the domain or range of "bmap".
12976 * The result is a basic map that lives in the same space as "bmap"
12977 * except that the domain or range has been replaced by
12978 * the domain space of "ma".
12980 * If bmap is represented by
12982 * A(p) + S u + B x + T v + C(divs) >= 0,
12984 * where u and x are input and output dimensions if type == isl_dim_out
12985 * while x and v are input and output dimensions if type == isl_dim_in,
12986 * and ma is represented by
12988 * x = D(p) + F(y) + G(divs')
12990 * then the result is
12992 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12994 * The divs in the input set are similarly adjusted.
12997 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13001 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13002 * B_i G(divs') + c_i(divs))/n_i)
13004 * If bmap is not a rational map and if F(y) involves any denominators
13006 * x_i = (f_i y + h_i)/m_i
13008 * then additional constraints are added to ensure that we only
13009 * map back integer points. That is we enforce
13011 * f_i y + h_i = m_i alpha_i
13013 * with alpha_i an additional existentially quantified variable.
13015 * We first copy over the divs from "ma".
13016 * Then we add the modified constraints and divs from "bmap".
13017 * Finally, we add the stride constraints, if needed.
13019 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13020 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13021 __isl_take isl_multi_aff
*ma
)
13025 isl_basic_map
*res
= NULL
;
13026 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13027 isl_int f
, c1
, c2
, g
;
13036 ma
= isl_multi_aff_align_divs(ma
);
13039 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13042 if (type
== isl_dim_in
) {
13044 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13046 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13049 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13050 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13051 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13054 space
= isl_multi_aff_get_domain_space(ma
);
13055 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13056 rational
= isl_basic_map_is_rational(bmap
);
13057 strides
= rational
? 0 : multi_aff_strides(ma
);
13058 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13059 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13061 res
= isl_basic_map_set_rational(res
);
13063 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13064 if (isl_basic_map_alloc_div(res
) < 0)
13067 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13071 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13072 k
= isl_basic_map_alloc_equality(res
);
13075 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13076 n_after
, n_div_ma
, n_div_bmap
,
13077 f
, c1
, c2
, g
, 0) < 0)
13081 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13082 k
= isl_basic_map_alloc_inequality(res
);
13085 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13086 n_after
, n_div_ma
, n_div_bmap
,
13087 f
, c1
, c2
, g
, 0) < 0)
13091 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13092 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13093 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13096 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13097 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13098 f
, c1
, c2
, g
, 1) < 0)
13103 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13109 isl_basic_map_free(bmap
);
13110 isl_multi_aff_free(ma
);
13111 res
= isl_basic_map_simplify(res
);
13112 return isl_basic_map_finalize(res
);
13118 isl_basic_map_free(bmap
);
13119 isl_multi_aff_free(ma
);
13120 isl_basic_map_free(res
);
13124 /* Compute the preimage of "bset" under the function represented by "ma".
13125 * In other words, plug in "ma" in "bset". The result is a basic set
13126 * that lives in the domain space of "ma".
13128 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13129 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13131 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13134 /* Compute the preimage of the domain of "bmap" under the function
13135 * represented by "ma".
13136 * In other words, plug in "ma" in the domain of "bmap".
13137 * The result is a basic map that lives in the same space as "bmap"
13138 * except that the domain has been replaced by the domain space of "ma".
13140 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13141 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13143 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13146 /* Compute the preimage of the range of "bmap" under the function
13147 * represented by "ma".
13148 * In other words, plug in "ma" in the range of "bmap".
13149 * The result is a basic map that lives in the same space as "bmap"
13150 * except that the range has been replaced by the domain space of "ma".
13152 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13153 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13155 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13158 /* Check if the range of "ma" is compatible with the domain or range
13159 * (depending on "type") of "map".
13160 * Return isl_stat_error if anything is wrong.
13162 static isl_stat
check_map_compatible_range_multi_aff(
13163 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13164 __isl_keep isl_multi_aff
*ma
)
13167 isl_space
*ma_space
;
13169 ma_space
= isl_multi_aff_get_space(ma
);
13170 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13171 isl_space_free(ma_space
);
13173 return isl_stat_error
;
13175 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13176 "spaces don't match", return isl_stat_error
);
13177 return isl_stat_ok
;
13180 /* Compute the preimage of the domain or range (depending on "type")
13181 * of "map" under the function represented by "ma".
13182 * In other words, plug in "ma" in the domain or range of "map".
13183 * The result is a map that lives in the same space as "map"
13184 * except that the domain or range has been replaced by
13185 * the domain space of "ma".
13187 * The parameters are assumed to have been aligned.
13189 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13190 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13195 map
= isl_map_cow(map
);
13196 ma
= isl_multi_aff_align_divs(ma
);
13199 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13202 for (i
= 0; i
< map
->n
; ++i
) {
13203 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13204 isl_multi_aff_copy(ma
));
13209 space
= isl_multi_aff_get_domain_space(ma
);
13210 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13212 isl_space_free(map
->dim
);
13217 isl_multi_aff_free(ma
);
13219 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13220 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13223 isl_multi_aff_free(ma
);
13228 /* Compute the preimage of the domain or range (depending on "type")
13229 * of "map" under the function represented by "ma".
13230 * In other words, plug in "ma" in the domain or range of "map".
13231 * The result is a map that lives in the same space as "map"
13232 * except that the domain or range has been replaced by
13233 * the domain space of "ma".
13235 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13236 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13243 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13247 return map_preimage_multi_aff(map
, type
, ma
);
13249 if (isl_map_check_named_params(map
) < 0)
13251 if (!isl_space_has_named_params(ma
->space
))
13252 isl_die(map
->ctx
, isl_error_invalid
,
13253 "unaligned unnamed parameters", goto error
);
13254 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13255 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13257 return map_preimage_multi_aff(map
, type
, ma
);
13259 isl_multi_aff_free(ma
);
13260 return isl_map_free(map
);
13263 /* Compute the preimage of "set" under the function represented by "ma".
13264 * In other words, plug in "ma" in "set". The result is a set
13265 * that lives in the domain space of "ma".
13267 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13268 __isl_take isl_multi_aff
*ma
)
13270 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13273 /* Compute the preimage of the domain of "map" under the function
13274 * represented by "ma".
13275 * In other words, plug in "ma" in the domain of "map".
13276 * The result is a map that lives in the same space as "map"
13277 * except that the domain has been replaced by the domain space of "ma".
13279 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13280 __isl_take isl_multi_aff
*ma
)
13282 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13285 /* Compute the preimage of the range of "map" under the function
13286 * represented by "ma".
13287 * In other words, plug in "ma" in the range of "map".
13288 * The result is a map that lives in the same space as "map"
13289 * except that the range has been replaced by the domain space of "ma".
13291 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13292 __isl_take isl_multi_aff
*ma
)
13294 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13297 /* Compute the preimage of "map" under the function represented by "pma".
13298 * In other words, plug in "pma" in the domain or range of "map".
13299 * The result is a map that lives in the same space as "map",
13300 * except that the space of type "type" has been replaced by
13301 * the domain space of "pma".
13303 * The parameters of "map" and "pma" are assumed to have been aligned.
13305 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13306 __isl_take isl_map
*map
, enum isl_dim_type type
,
13307 __isl_take isl_pw_multi_aff
*pma
)
13316 isl_pw_multi_aff_free(pma
);
13317 res
= isl_map_empty(isl_map_get_space(map
));
13322 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13323 isl_multi_aff_copy(pma
->p
[0].maff
));
13324 if (type
== isl_dim_in
)
13325 res
= isl_map_intersect_domain(res
,
13326 isl_map_copy(pma
->p
[0].set
));
13328 res
= isl_map_intersect_range(res
,
13329 isl_map_copy(pma
->p
[0].set
));
13331 for (i
= 1; i
< pma
->n
; ++i
) {
13334 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13335 isl_multi_aff_copy(pma
->p
[i
].maff
));
13336 if (type
== isl_dim_in
)
13337 res_i
= isl_map_intersect_domain(res_i
,
13338 isl_map_copy(pma
->p
[i
].set
));
13340 res_i
= isl_map_intersect_range(res_i
,
13341 isl_map_copy(pma
->p
[i
].set
));
13342 res
= isl_map_union(res
, res_i
);
13345 isl_pw_multi_aff_free(pma
);
13349 isl_pw_multi_aff_free(pma
);
13354 /* Compute the preimage of "map" under the function represented by "pma".
13355 * In other words, plug in "pma" in the domain or range of "map".
13356 * The result is a map that lives in the same space as "map",
13357 * except that the space of type "type" has been replaced by
13358 * the domain space of "pma".
13360 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13361 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13368 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13372 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13374 if (isl_map_check_named_params(map
) < 0)
13376 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13378 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13379 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13381 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13383 isl_pw_multi_aff_free(pma
);
13384 return isl_map_free(map
);
13387 /* Compute the preimage of "set" under the function represented by "pma".
13388 * In other words, plug in "pma" in "set". The result is a set
13389 * that lives in the domain space of "pma".
13391 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13392 __isl_take isl_pw_multi_aff
*pma
)
13394 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13397 /* Compute the preimage of the domain of "map" under the function
13398 * represented by "pma".
13399 * In other words, plug in "pma" in the domain of "map".
13400 * The result is a map that lives in the same space as "map",
13401 * except that domain space has been replaced by the domain space of "pma".
13403 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13404 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13406 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13409 /* Compute the preimage of the range of "map" under the function
13410 * represented by "pma".
13411 * In other words, plug in "pma" in the range of "map".
13412 * The result is a map that lives in the same space as "map",
13413 * except that range space has been replaced by the domain space of "pma".
13415 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13416 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13418 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13421 /* Compute the preimage of "map" under the function represented by "mpa".
13422 * In other words, plug in "mpa" in the domain or range of "map".
13423 * The result is a map that lives in the same space as "map",
13424 * except that the space of type "type" has been replaced by
13425 * the domain space of "mpa".
13427 * If the map does not involve any constraints that refer to the
13428 * dimensions of the substituted space, then the only possible
13429 * effect of "mpa" on the map is to map the space to a different space.
13430 * We create a separate isl_multi_aff to effectuate this change
13431 * in order to avoid spurious splitting of the map along the pieces
13433 * If "mpa" has a non-trivial explicit domain, however,
13434 * then the full substitution should be performed.
13436 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13437 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13441 isl_pw_multi_aff
*pma
;
13443 n
= isl_map_dim(map
, type
);
13447 full
= isl_map_involves_dims(map
, type
, 0, n
);
13448 if (full
>= 0 && !full
)
13449 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13456 space
= isl_multi_pw_aff_get_space(mpa
);
13457 isl_multi_pw_aff_free(mpa
);
13458 ma
= isl_multi_aff_zero(space
);
13459 return isl_map_preimage_multi_aff(map
, type
, ma
);
13462 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13463 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13466 isl_multi_pw_aff_free(mpa
);
13470 /* Compute the preimage of "map" under the function represented by "mpa".
13471 * In other words, plug in "mpa" in the domain "map".
13472 * The result is a map that lives in the same space as "map",
13473 * except that domain space has been replaced by the domain space of "mpa".
13475 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13476 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13478 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13481 /* Compute the preimage of "set" by the function represented by "mpa".
13482 * In other words, plug in "mpa" in "set".
13484 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13485 __isl_take isl_multi_pw_aff
*mpa
)
13487 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13490 /* Return a copy of the equality constraints of "bset" as a matrix.
13492 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13493 __isl_keep isl_basic_set
*bset
)
13498 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13502 ctx
= isl_basic_set_get_ctx(bset
);
13503 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
13506 /* Are the "n" "coefficients" starting at "first" of the integer division
13507 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13509 * The "coefficient" at position 0 is the denominator.
13510 * The "coefficient" at position 1 is the constant term.
13512 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13513 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13514 unsigned first
, unsigned n
)
13516 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13517 return isl_bool_error
;
13518 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13519 return isl_bool_error
;
13520 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13521 bmap2
->div
[pos2
] + first
, n
);
13524 /* Are the integer division expressions at position "pos1" in "bmap1" and
13525 * "pos2" in "bmap2" equal to each other, except that the constant terms
13528 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13529 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13530 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13533 isl_size total
, total2
;
13535 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
13536 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
13537 if (total
< 0 || total2
< 0)
13538 return isl_bool_error
;
13539 if (total
!= total2
)
13540 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13541 "incomparable div expressions", return isl_bool_error
);
13542 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13544 if (equal
< 0 || !equal
)
13546 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13548 if (equal
< 0 || equal
)
13549 return isl_bool_not(equal
);
13550 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13554 /* Replace the numerator of the constant term of the integer division
13555 * expression at position "div" in "bmap" by "value".
13556 * The caller guarantees that this does not change the meaning
13559 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13560 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13562 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13563 return isl_basic_map_free(bmap
);
13565 isl_int_set_si(bmap
->div
[div
][1], value
);
13570 /* Is the point "inner" internal to inequality constraint "ineq"
13572 * The point is considered to be internal to the inequality constraint,
13573 * if it strictly lies on the positive side of the inequality constraint,
13574 * or if it lies on the constraint and the constraint is lexico-positive.
13576 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13577 __isl_keep isl_basic_set
*bset
, int ineq
)
13583 if (!inner
|| !bset
)
13584 return isl_bool_error
;
13586 ctx
= isl_basic_set_get_ctx(bset
);
13587 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13588 &ctx
->normalize_gcd
);
13589 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13590 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13592 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13594 return isl_bool_error
;
13595 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13596 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13599 /* Tighten the inequality constraints of "bset" that are outward with respect
13600 * to the point "vec".
13601 * That is, tighten the constraints that are not satisfied by "vec".
13603 * "vec" is a point internal to some superset S of "bset" that is used
13604 * to make the subsets of S disjoint, by tightening one half of the constraints
13605 * that separate two subsets. In particular, the constraints of S
13606 * are all satisfied by "vec" and should not be tightened.
13607 * Of the internal constraints, those that have "vec" on the outside
13608 * are tightened. The shared facet is included in the adjacent subset
13609 * with the opposite constraint.
13610 * For constraints that saturate "vec", this criterion cannot be used
13611 * to determine which of the two sides should be tightened.
13612 * Instead, the sign of the first non-zero coefficient is used
13613 * to make this choice. Note that this second criterion is never used
13614 * on the constraints of S since "vec" is interior to "S".
13616 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13617 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13621 bset
= isl_basic_set_cow(bset
);
13624 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13627 internal
= is_internal(vec
, bset
, j
);
13629 return isl_basic_set_free(bset
);
13632 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13638 /* Replace the variables x of type "type" starting at "first" in "bmap"
13639 * by x' with x = M x' with M the matrix trans.
13640 * That is, replace the corresponding coefficients c by c M.
13642 * The transformation matrix should be a square matrix.
13644 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13645 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13646 __isl_take isl_mat
*trans
)
13650 bmap
= isl_basic_map_cow(bmap
);
13651 if (!bmap
|| !trans
)
13654 if (trans
->n_row
!= trans
->n_col
)
13655 isl_die(trans
->ctx
, isl_error_invalid
,
13656 "expecting square transformation matrix", goto error
);
13657 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13660 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13662 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13663 isl_mat_copy(trans
)) < 0)
13665 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13666 isl_mat_copy(trans
)) < 0)
13668 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13669 isl_mat_copy(trans
)) < 0)
13672 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13673 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13675 isl_mat_free(trans
);
13678 isl_mat_free(trans
);
13679 isl_basic_map_free(bmap
);
13683 /* Replace the variables x of type "type" starting at "first" in "bset"
13684 * by x' with x = M x' with M the matrix trans.
13685 * That is, replace the corresponding coefficients c by c M.
13687 * The transformation matrix should be a square matrix.
13689 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13690 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13691 __isl_take isl_mat
*trans
)
13693 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);