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 int 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
);
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 -1);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 int 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 int 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 int 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
)
5338 n
= isl_basic_map_list_n_basic_map(list
);
5339 for (i
= 0; i
< n
; ++i
) {
5340 isl_basic_map
*bmap
;
5341 isl_basic_set
*bset
;
5343 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5344 bset
= isl_basic_set_underlying_set(bmap
);
5345 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5351 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5352 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5354 struct isl_basic_map
*bmap
;
5355 struct isl_ctx
*ctx
;
5356 isl_size dim
, bmap_total
;
5363 if (isl_basic_set_check_no_params(bset
) < 0 ||
5364 isl_basic_set_check_no_locals(bset
) < 0)
5366 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5367 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5368 if (dim
< 0 || bmap_total
< 0)
5370 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5371 if (like
->n_div
== 0) {
5372 isl_space
*space
= isl_basic_map_get_space(like
);
5373 isl_basic_map_free(like
);
5374 return isl_basic_map_reset_space(bset
, space
);
5376 bset
= isl_basic_set_cow(bset
);
5379 total
= dim
+ bset
->extra
;
5380 bmap
= bset_to_bmap(bset
);
5381 isl_space_free(bmap
->dim
);
5382 bmap
->dim
= isl_space_copy(like
->dim
);
5385 bmap
->n_div
= like
->n_div
;
5386 bmap
->extra
+= like
->n_div
;
5390 ltotal
= total
- bmap
->extra
+ like
->extra
;
5393 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5394 bmap
->extra
* (1 + 1 + total
));
5395 if (isl_blk_is_error(bmap
->block2
))
5397 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5401 for (i
= 0; i
< bmap
->extra
; ++i
)
5402 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5403 for (i
= 0; i
< like
->n_div
; ++i
) {
5404 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5405 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5407 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5409 isl_basic_map_free(like
);
5410 bmap
= isl_basic_map_simplify(bmap
);
5411 bmap
= isl_basic_map_finalize(bmap
);
5414 isl_basic_map_free(like
);
5415 isl_basic_set_free(bset
);
5419 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5420 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5422 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5423 bset_to_bmap(like
)));
5426 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5430 map
= isl_map_cow(map
);
5433 map
->dim
= isl_space_cow(map
->dim
);
5437 for (i
= 1; i
< map
->n
; ++i
)
5438 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5440 for (i
= 0; i
< map
->n
; ++i
) {
5441 map
->p
[i
] = bset_to_bmap(
5442 isl_basic_map_underlying_set(map
->p
[i
]));
5447 map
->dim
= isl_space_underlying(map
->dim
, 0);
5449 isl_space_free(map
->dim
);
5450 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5454 return set_from_map(map
);
5460 /* Replace the space of "bmap" by "space".
5462 * If the space of "bmap" is identical to "space" (including the identifiers
5463 * of the input and output dimensions), then simply return the original input.
5465 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5466 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5469 isl_space
*bmap_space
;
5471 bmap_space
= isl_basic_map_peek_space(bmap
);
5472 equal
= isl_space_is_equal(bmap_space
, space
);
5473 if (equal
>= 0 && equal
)
5474 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5478 isl_space_free(space
);
5481 bmap
= isl_basic_map_cow(bmap
);
5482 if (!bmap
|| !space
)
5485 isl_space_free(bmap
->dim
);
5488 bmap
= isl_basic_map_finalize(bmap
);
5492 isl_basic_map_free(bmap
);
5493 isl_space_free(space
);
5497 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5498 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5500 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5504 /* Check that the total dimensions of "map" and "space" are the same.
5506 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5507 __isl_keep isl_space
*space
)
5509 isl_size dim1
, dim2
;
5511 dim1
= isl_map_dim(map
, isl_dim_all
);
5512 dim2
= isl_space_dim(space
, isl_dim_all
);
5513 if (dim1
< 0 || dim2
< 0)
5514 return isl_stat_error
;
5517 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5518 "total dimensions do not match", return isl_stat_error
);
5521 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5522 __isl_take isl_space
*space
)
5526 map
= isl_map_cow(map
);
5530 for (i
= 0; i
< map
->n
; ++i
) {
5531 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5532 isl_space_copy(space
));
5536 isl_space_free(map
->dim
);
5542 isl_space_free(space
);
5546 /* Replace the space of "map" by "space", without modifying
5547 * the dimension of "map".
5549 * If the space of "map" is identical to "space" (including the identifiers
5550 * of the input and output dimensions), then simply return the original input.
5552 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5553 __isl_take isl_space
*space
)
5556 isl_space
*map_space
;
5558 map_space
= isl_map_peek_space(map
);
5559 equal
= isl_space_is_equal(map_space
, space
);
5560 if (equal
>= 0 && equal
)
5561 equal
= isl_space_has_equal_ids(map_space
, space
);
5565 isl_space_free(space
);
5568 if (check_map_space_equal_total_dim(map
, space
) < 0)
5570 return isl_map_reset_space(map
, space
);
5573 isl_space_free(space
);
5577 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5578 __isl_take isl_space
*dim
)
5580 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5583 /* Compute the parameter domain of the given basic set.
5585 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5591 is_params
= isl_basic_set_is_params(bset
);
5593 return isl_basic_set_free(bset
);
5597 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5599 return isl_basic_set_free(bset
);
5600 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5601 space
= isl_basic_set_get_space(bset
);
5602 space
= isl_space_params(space
);
5603 bset
= isl_basic_set_reset_space(bset
, space
);
5607 /* Construct a zero-dimensional basic set with the given parameter domain.
5609 __isl_give isl_basic_set
*isl_basic_set_from_params(
5610 __isl_take isl_basic_set
*bset
)
5613 space
= isl_basic_set_get_space(bset
);
5614 space
= isl_space_set_from_params(space
);
5615 bset
= isl_basic_set_reset_space(bset
, space
);
5619 /* Compute the parameter domain of the given set.
5621 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5623 return isl_map_params(set_to_map(set
));
5626 /* Construct a zero-dimensional set with the given parameter domain.
5628 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5631 space
= isl_set_get_space(set
);
5632 space
= isl_space_set_from_params(space
);
5633 set
= isl_set_reset_space(set
, space
);
5637 /* Compute the parameter domain of the given map.
5639 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5642 isl_size n_in
, n_out
;
5644 n_in
= isl_map_dim(map
, isl_dim_in
);
5645 n_out
= isl_map_dim(map
, isl_dim_out
);
5646 if (n_in
< 0 || n_out
< 0)
5647 return isl_map_free(map
);
5648 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5649 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5650 space
= isl_map_get_space(map
);
5651 space
= isl_space_params(space
);
5652 map
= isl_map_reset_space(map
, space
);
5656 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5661 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5663 return isl_basic_map_free(bmap
);
5664 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5666 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5668 return isl_basic_map_reset_space(bmap
, space
);
5671 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5674 return isl_bool_error
;
5675 return isl_space_may_be_set(bmap
->dim
);
5678 /* Is this basic map actually a set?
5679 * Users should never call this function. Outside of isl,
5680 * the type should indicate whether something is a set or a map.
5682 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5685 return isl_bool_error
;
5686 return isl_space_is_set(bmap
->dim
);
5689 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5693 is_set
= isl_basic_map_is_set(bmap
);
5698 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5700 isl_basic_map_free(bmap
);
5704 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5705 __isl_take isl_basic_map
*bmap
)
5709 isl_basic_map
*domain
;
5710 isl_size nparam
, n_in
, n_out
;
5712 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5713 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5714 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5715 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5716 return isl_basic_map_free(bmap
);
5718 space
= isl_basic_map_get_space(bmap
);
5719 space
= isl_space_from_range(isl_space_domain(space
));
5720 domain
= isl_basic_map_universe(space
);
5722 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5723 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5724 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5726 for (i
= 0; i
< n_in
; ++i
)
5727 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5730 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5731 return isl_basic_map_finalize(bmap
);
5734 __isl_give isl_basic_map
*isl_basic_map_range_map(
5735 __isl_take isl_basic_map
*bmap
)
5739 isl_basic_map
*range
;
5740 isl_size nparam
, n_in
, n_out
;
5742 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5743 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5744 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5745 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5746 return isl_basic_map_free(bmap
);
5748 space
= isl_basic_map_get_space(bmap
);
5749 space
= isl_space_from_range(isl_space_range(space
));
5750 range
= isl_basic_map_universe(space
);
5752 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5753 bmap
= isl_basic_map_apply_range(bmap
, range
);
5754 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5756 for (i
= 0; i
< n_out
; ++i
)
5757 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5760 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5761 return isl_basic_map_finalize(bmap
);
5764 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5768 return isl_space_may_be_set(map
->dim
);
5771 /* Is this map actually a set?
5772 * Users should never call this function. Outside of isl,
5773 * the type should indicate whether something is a set or a map.
5775 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5778 return isl_bool_error
;
5779 return isl_space_is_set(map
->dim
);
5782 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5786 struct isl_set
*set
;
5788 is_set
= isl_map_is_set(map
);
5792 return set_from_map(map
);
5794 map
= isl_map_cow(map
);
5798 set
= set_from_map(map
);
5799 set
->dim
= isl_space_range(set
->dim
);
5802 for (i
= 0; i
< map
->n
; ++i
) {
5803 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5807 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5808 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5815 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5819 map
= isl_map_cow(map
);
5823 map
->dim
= isl_space_domain_map(map
->dim
);
5826 for (i
= 0; i
< map
->n
; ++i
) {
5827 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5831 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5832 map
= isl_map_unmark_normalized(map
);
5839 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5842 isl_space
*range_dim
;
5844 map
= isl_map_cow(map
);
5848 range_dim
= isl_space_range(isl_map_get_space(map
));
5849 range_dim
= isl_space_from_range(range_dim
);
5850 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5851 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5854 for (i
= 0; i
< map
->n
; ++i
) {
5855 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5859 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5860 map
= isl_map_unmark_normalized(map
);
5867 /* Given a wrapped map of the form A[B -> C],
5868 * return the map A[B -> C] -> B.
5870 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5877 if (!isl_set_has_tuple_id(set
))
5878 return isl_map_domain_map(isl_set_unwrap(set
));
5880 id
= isl_set_get_tuple_id(set
);
5881 map
= isl_map_domain_map(isl_set_unwrap(set
));
5882 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5887 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5888 __isl_take isl_basic_set
*bset
)
5890 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5893 __isl_give isl_basic_map
*isl_basic_map_from_range(
5894 __isl_take isl_basic_set
*bset
)
5897 space
= isl_basic_set_get_space(bset
);
5898 space
= isl_space_from_range(space
);
5899 bset
= isl_basic_set_reset_space(bset
, space
);
5900 return bset_to_bmap(bset
);
5903 /* Create a relation with the given set as range.
5904 * The domain of the created relation is a zero-dimensional
5905 * flat anonymous space.
5907 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5910 space
= isl_set_get_space(set
);
5911 space
= isl_space_from_range(space
);
5912 set
= isl_set_reset_space(set
, space
);
5913 return set_to_map(set
);
5916 /* Create a relation with the given set as domain.
5917 * The range of the created relation is a zero-dimensional
5918 * flat anonymous space.
5920 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5922 return isl_map_reverse(isl_map_from_range(set
));
5925 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5926 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5928 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5931 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5932 __isl_take isl_set
*range
)
5934 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5937 /* Return a newly allocated isl_map with given space and flags and
5938 * room for "n" basic maps.
5939 * Make sure that all cached information is cleared.
5941 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5944 struct isl_map
*map
;
5949 isl_die(space
->ctx
, isl_error_internal
,
5950 "negative number of basic maps", goto error
);
5951 map
= isl_calloc(space
->ctx
, struct isl_map
,
5952 sizeof(struct isl_map
) +
5953 (n
- 1) * sizeof(struct isl_basic_map
*));
5957 map
->ctx
= space
->ctx
;
5958 isl_ctx_ref(map
->ctx
);
5966 isl_space_free(space
);
5970 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5972 struct isl_basic_map
*bmap
;
5973 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5974 bmap
= isl_basic_map_set_to_empty(bmap
);
5978 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5980 struct isl_basic_set
*bset
;
5981 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5982 bset
= isl_basic_set_set_to_empty(bset
);
5986 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5988 struct isl_basic_map
*bmap
;
5989 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5990 bmap
= isl_basic_map_finalize(bmap
);
5994 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5996 struct isl_basic_set
*bset
;
5997 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5998 bset
= isl_basic_set_finalize(bset
);
6002 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6003 __isl_take isl_space
*space
)
6006 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6007 isl_basic_map
*bmap
;
6010 space
= isl_space_free(space
);
6011 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6012 for (i
= 0; i
< total
; ++i
) {
6013 int k
= isl_basic_map_alloc_inequality(bmap
);
6016 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6017 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6021 isl_basic_map_free(bmap
);
6025 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6026 __isl_take isl_space
*space
)
6028 return isl_basic_map_nat_universe(space
);
6031 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6033 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6036 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6038 return isl_map_nat_universe(dim
);
6041 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6043 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6046 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6048 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6051 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6053 struct isl_map
*map
;
6056 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6057 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6061 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6063 struct isl_set
*set
;
6066 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6067 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6071 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6074 struct isl_map
*dup
;
6078 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6079 for (i
= 0; i
< map
->n
; ++i
)
6080 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6084 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6085 __isl_take isl_basic_map
*bmap
)
6089 if (isl_basic_map_plain_is_empty(bmap
)) {
6090 isl_basic_map_free(bmap
);
6093 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6094 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6095 map
->p
[map
->n
] = bmap
;
6097 map
= isl_map_unmark_normalized(map
);
6103 isl_basic_map_free(bmap
);
6107 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6118 isl_ctx_deref(map
->ctx
);
6119 for (i
= 0; i
< map
->n
; ++i
)
6120 isl_basic_map_free(map
->p
[i
]);
6121 isl_space_free(map
->dim
);
6127 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6128 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6133 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6135 return isl_basic_map_free(bmap
);
6137 bmap
= isl_basic_map_cow(bmap
);
6138 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6139 j
= isl_basic_map_alloc_equality(bmap
);
6142 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6143 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6144 isl_int_set_si(bmap
->eq
[j
][0], value
);
6145 bmap
= isl_basic_map_simplify(bmap
);
6146 return isl_basic_map_finalize(bmap
);
6148 isl_basic_map_free(bmap
);
6152 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6153 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6158 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6160 return isl_basic_map_free(bmap
);
6162 bmap
= isl_basic_map_cow(bmap
);
6163 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6164 j
= isl_basic_map_alloc_equality(bmap
);
6167 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6168 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6169 isl_int_set(bmap
->eq
[j
][0], value
);
6170 bmap
= isl_basic_map_simplify(bmap
);
6171 return isl_basic_map_finalize(bmap
);
6173 isl_basic_map_free(bmap
);
6177 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6178 enum isl_dim_type type
, unsigned pos
, int value
)
6180 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6181 return isl_basic_map_free(bmap
);
6182 return isl_basic_map_fix_pos_si(bmap
,
6183 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6186 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6187 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6189 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6190 return isl_basic_map_free(bmap
);
6191 return isl_basic_map_fix_pos(bmap
,
6192 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6195 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6196 * to be equal to "v".
6198 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6199 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6203 if (!isl_val_is_int(v
))
6204 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6205 "expecting integer value", goto error
);
6206 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6208 pos
+= isl_basic_map_offset(bmap
, type
);
6209 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6213 isl_basic_map_free(bmap
);
6218 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6219 * to be equal to "v".
6221 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6222 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6224 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6227 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6228 enum isl_dim_type type
, unsigned pos
, int value
)
6230 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6234 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6235 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6237 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6241 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6242 unsigned input
, int value
)
6244 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6247 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6248 unsigned dim
, int value
)
6250 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6251 isl_dim_set
, dim
, value
));
6254 /* Remove the basic map at position "i" from "map" if this basic map
6255 * is (obviously) empty.
6257 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6264 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6266 return isl_map_free(map
);
6270 isl_basic_map_free(map
->p
[i
]);
6273 map
->p
[i
] = map
->p
[map
->n
];
6274 map
= isl_map_unmark_normalized(map
);
6281 /* Perform "fn" on each basic map of "map", where we may not be holding
6282 * the only reference to "map".
6283 * In particular, "fn" should be a semantics preserving operation
6284 * that we want to apply to all copies of "map". We therefore need
6285 * to be careful not to modify "map" in a way that breaks "map"
6286 * in case anything goes wrong.
6288 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6289 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6291 struct isl_basic_map
*bmap
;
6297 for (i
= map
->n
- 1; i
>= 0; --i
) {
6298 bmap
= isl_basic_map_copy(map
->p
[i
]);
6302 isl_basic_map_free(map
->p
[i
]);
6304 map
= remove_if_empty(map
, i
);
6315 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6316 enum isl_dim_type type
, unsigned pos
, int value
)
6320 map
= isl_map_cow(map
);
6321 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6322 return isl_map_free(map
);
6323 for (i
= map
->n
- 1; i
>= 0; --i
) {
6324 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6325 map
= remove_if_empty(map
, i
);
6329 map
= isl_map_unmark_normalized(map
);
6333 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6334 enum isl_dim_type type
, unsigned pos
, int value
)
6336 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6339 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6340 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6344 map
= isl_map_cow(map
);
6345 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6346 return isl_map_free(map
);
6347 for (i
= 0; i
< map
->n
; ++i
) {
6348 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6352 map
= isl_map_unmark_normalized(map
);
6359 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6360 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6362 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6365 /* Fix the value of the variable at position "pos" of type "type" of "map"
6366 * to be equal to "v".
6368 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6369 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6373 map
= isl_map_cow(map
);
6377 if (!isl_val_is_int(v
))
6378 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6379 "expecting integer value", goto error
);
6380 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6382 for (i
= map
->n
- 1; i
>= 0; --i
) {
6383 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6385 map
= remove_if_empty(map
, i
);
6389 map
= isl_map_unmark_normalized(map
);
6398 /* Fix the value of the variable at position "pos" of type "type" of "set"
6399 * to be equal to "v".
6401 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6402 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6404 return isl_map_fix_val(set
, type
, pos
, v
);
6407 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6408 unsigned input
, int value
)
6410 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6413 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6415 return set_from_map(isl_map_fix_si(set_to_map(set
),
6416 isl_dim_set
, dim
, value
));
6419 static __isl_give isl_basic_map
*basic_map_bound_si(
6420 __isl_take isl_basic_map
*bmap
,
6421 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6426 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6427 return isl_basic_map_free(bmap
);
6428 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6430 return isl_basic_map_free(bmap
);
6431 pos
+= isl_basic_map_offset(bmap
, type
);
6432 bmap
= isl_basic_map_cow(bmap
);
6433 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6434 j
= isl_basic_map_alloc_inequality(bmap
);
6437 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6439 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6440 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6442 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6443 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6445 bmap
= isl_basic_map_simplify(bmap
);
6446 return isl_basic_map_finalize(bmap
);
6448 isl_basic_map_free(bmap
);
6452 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6453 __isl_take isl_basic_map
*bmap
,
6454 enum isl_dim_type type
, unsigned pos
, int value
)
6456 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6459 /* Constrain the values of the given dimension to be no greater than "value".
6461 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6462 __isl_take isl_basic_map
*bmap
,
6463 enum isl_dim_type type
, unsigned pos
, int value
)
6465 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6468 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6469 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6473 map
= isl_map_cow(map
);
6474 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6475 return isl_map_free(map
);
6476 for (i
= 0; i
< map
->n
; ++i
) {
6477 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6478 type
, pos
, value
, upper
);
6482 map
= isl_map_unmark_normalized(map
);
6489 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6490 enum isl_dim_type type
, unsigned pos
, int value
)
6492 return map_bound_si(map
, type
, pos
, value
, 0);
6495 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6496 enum isl_dim_type type
, unsigned pos
, int value
)
6498 return map_bound_si(map
, type
, pos
, value
, 1);
6501 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6502 enum isl_dim_type type
, unsigned pos
, int value
)
6504 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6508 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6509 enum isl_dim_type type
, unsigned pos
, int value
)
6511 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6514 /* Bound the given variable of "bmap" from below (or above is "upper"
6515 * is set) to "value".
6517 static __isl_give isl_basic_map
*basic_map_bound(
6518 __isl_take isl_basic_map
*bmap
,
6519 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6524 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6525 return isl_basic_map_free(bmap
);
6526 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6528 return isl_basic_map_free(bmap
);
6529 pos
+= isl_basic_map_offset(bmap
, type
);
6530 bmap
= isl_basic_map_cow(bmap
);
6531 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6532 j
= isl_basic_map_alloc_inequality(bmap
);
6535 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6537 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6538 isl_int_set(bmap
->ineq
[j
][0], value
);
6540 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6541 isl_int_neg(bmap
->ineq
[j
][0], value
);
6543 bmap
= isl_basic_map_simplify(bmap
);
6544 return isl_basic_map_finalize(bmap
);
6546 isl_basic_map_free(bmap
);
6550 /* Bound the given variable of "map" from below (or above is "upper"
6551 * is set) to "value".
6553 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6554 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6558 map
= isl_map_cow(map
);
6559 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6560 return isl_map_free(map
);
6561 for (i
= map
->n
- 1; i
>= 0; --i
) {
6562 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6563 map
= remove_if_empty(map
, i
);
6567 map
= isl_map_unmark_normalized(map
);
6571 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6572 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6574 return map_bound(map
, type
, pos
, value
, 0);
6577 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6578 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6580 return map_bound(map
, type
, pos
, value
, 1);
6583 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6584 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6586 return isl_map_lower_bound(set
, type
, pos
, value
);
6589 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6590 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6592 return isl_map_upper_bound(set
, type
, pos
, value
);
6595 /* Force the values of the variable at position "pos" of type "type" of "set"
6596 * to be no smaller than "value".
6598 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6599 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6603 if (!isl_val_is_int(value
))
6604 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6605 "expecting integer value", goto error
);
6606 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6607 isl_val_free(value
);
6610 isl_val_free(value
);
6615 /* Force the values of the variable at position "pos" of type "type" of "set"
6616 * to be no greater than "value".
6618 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6619 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6623 if (!isl_val_is_int(value
))
6624 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6625 "expecting integer value", goto error
);
6626 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6627 isl_val_free(value
);
6630 isl_val_free(value
);
6635 /* Bound the given variable of "bset" from below (or above is "upper"
6636 * is set) to "value".
6638 static __isl_give isl_basic_set
*isl_basic_set_bound(
6639 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6640 isl_int value
, int upper
)
6642 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6643 type
, pos
, value
, upper
));
6646 /* Bound the given variable of "bset" from below (or above is "upper"
6647 * is set) to "value".
6649 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6650 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6651 __isl_take isl_val
*value
, int upper
)
6655 if (!isl_val_is_int(value
))
6656 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6657 "expecting integer value", goto error
);
6658 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6659 isl_val_free(value
);
6662 isl_val_free(value
);
6663 isl_basic_set_free(bset
);
6667 /* Bound the given variable of "bset" from below to "value".
6669 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6670 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6671 __isl_take isl_val
*value
)
6673 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6676 /* Bound the given variable of "bset" from above to "value".
6678 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6679 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6680 __isl_take isl_val
*value
)
6682 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6685 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6689 map
= isl_map_cow(map
);
6693 map
->dim
= isl_space_reverse(map
->dim
);
6696 for (i
= 0; i
< map
->n
; ++i
) {
6697 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6701 map
= isl_map_unmark_normalized(map
);
6709 #define TYPE isl_pw_multi_aff
6711 #define SUFFIX _pw_multi_aff
6713 #define EMPTY isl_pw_multi_aff_empty
6715 #define ADD isl_pw_multi_aff_union_add
6716 #include "isl_map_lexopt_templ.c"
6718 /* Given a map "map", compute the lexicographically minimal
6719 * (or maximal) image element for each domain element in dom,
6720 * in the form of an isl_pw_multi_aff.
6721 * If "empty" is not NULL, then set *empty to those elements in dom that
6722 * do not have an image element.
6723 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6724 * should be computed over the domain of "map". "empty" is also NULL
6727 * We first compute the lexicographically minimal or maximal element
6728 * in the first basic map. This results in a partial solution "res"
6729 * and a subset "todo" of dom that still need to be handled.
6730 * We then consider each of the remaining maps in "map" and successively
6731 * update both "res" and "todo".
6732 * If "empty" is NULL, then the todo sets are not needed and therefore
6733 * also not computed.
6735 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6736 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6737 __isl_give isl_set
**empty
, unsigned flags
)
6741 isl_pw_multi_aff
*res
;
6744 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6745 if (!map
|| (!full
&& !dom
))
6748 if (isl_map_plain_is_empty(map
)) {
6753 return isl_pw_multi_aff_from_map(map
);
6756 res
= basic_map_partial_lexopt_pw_multi_aff(
6757 isl_basic_map_copy(map
->p
[0]),
6758 isl_set_copy(dom
), empty
, flags
);
6762 for (i
= 1; i
< map
->n
; ++i
) {
6763 isl_pw_multi_aff
*res_i
;
6765 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6766 isl_basic_map_copy(map
->p
[i
]),
6767 isl_set_copy(dom
), empty
, flags
);
6769 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6770 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6772 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6775 todo
= isl_set_intersect(todo
, *empty
);
6794 #define TYPE isl_map
6798 #define EMPTY isl_map_empty
6800 #define ADD isl_map_union_disjoint
6801 #include "isl_map_lexopt_templ.c"
6803 /* Given a map "map", compute the lexicographically minimal
6804 * (or maximal) image element for each domain element in "dom",
6805 * in the form of an isl_map.
6806 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6807 * do not have an image element.
6808 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6809 * should be computed over the domain of "map". "empty" is also NULL
6812 * If the input consists of more than one disjunct, then first
6813 * compute the desired result in the form of an isl_pw_multi_aff and
6814 * then convert that into an isl_map.
6816 * This function used to have an explicit implementation in terms
6817 * of isl_maps, but it would continually intersect the domains of
6818 * partial results with the complement of the domain of the next
6819 * partial solution, potentially leading to an explosion in the number
6820 * of disjuncts if there are several disjuncts in the input.
6821 * An even earlier implementation of this function would look for
6822 * better results in the domain of the partial result and for extra
6823 * results in the complement of this domain, which would lead to
6824 * even more splintering.
6826 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6827 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6828 __isl_give isl_set
**empty
, unsigned flags
)
6831 struct isl_map
*res
;
6832 isl_pw_multi_aff
*pma
;
6834 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6835 if (!map
|| (!full
&& !dom
))
6838 if (isl_map_plain_is_empty(map
)) {
6847 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6853 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6855 return isl_map_from_pw_multi_aff(pma
);
6864 __isl_give isl_map
*isl_map_partial_lexmax(
6865 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6866 __isl_give isl_set
**empty
)
6868 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6871 __isl_give isl_map
*isl_map_partial_lexmin(
6872 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6873 __isl_give isl_set
**empty
)
6875 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6878 __isl_give isl_set
*isl_set_partial_lexmin(
6879 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6880 __isl_give isl_set
**empty
)
6882 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6886 __isl_give isl_set
*isl_set_partial_lexmax(
6887 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6888 __isl_give isl_set
**empty
)
6890 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6894 /* Compute the lexicographic minimum (or maximum if "flags" includes
6895 * ISL_OPT_MAX) of "bset" over its parametric domain.
6897 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6900 return isl_basic_map_lexopt(bset
, flags
);
6903 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6905 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6908 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6910 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6913 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6915 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6918 /* Compute the lexicographic minimum of "bset" over its parametric domain
6919 * for the purpose of quantifier elimination.
6920 * That is, find an explicit representation for all the existentially
6921 * quantified variables in "bset" by computing their lexicographic
6924 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6925 __isl_take isl_basic_set
*bset
)
6927 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6930 /* Given a basic map with one output dimension, compute the minimum or
6931 * maximum of that dimension as an isl_pw_aff.
6933 * Compute the optimum as a lexicographic optimum over the single
6934 * output dimension and extract the single isl_pw_aff from the result.
6936 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6939 isl_pw_multi_aff
*pma
;
6942 bmap
= isl_basic_map_copy(bmap
);
6943 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6944 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6945 isl_pw_multi_aff_free(pma
);
6950 /* Compute the minimum or maximum of the given output dimension
6951 * as a function of the parameters and the input dimensions,
6952 * but independently of the other output dimensions.
6954 * We first project out the other output dimension and then compute
6955 * the "lexicographic" maximum in each basic map, combining the results
6956 * using isl_pw_aff_union_max.
6958 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6965 n_out
= isl_map_dim(map
, isl_dim_out
);
6967 map
= isl_map_free(map
);
6968 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6969 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6974 isl_space
*space
= isl_map_get_space(map
);
6976 return isl_pw_aff_empty(space
);
6979 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6980 for (i
= 1; i
< map
->n
; ++i
) {
6981 isl_pw_aff
*pwaff_i
;
6983 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6984 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6992 /* Compute the minimum of the given output dimension as a function of the
6993 * parameters and input dimensions, but independently of
6994 * the other output dimensions.
6996 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6998 return map_dim_opt(map
, pos
, 0);
7001 /* Compute the maximum of the given output dimension as a function of the
7002 * parameters and input dimensions, but independently of
7003 * the other output dimensions.
7005 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7007 return map_dim_opt(map
, pos
, 1);
7010 /* Compute the minimum or maximum of the given set dimension
7011 * as a function of the parameters,
7012 * but independently of the other set dimensions.
7014 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7017 return map_dim_opt(set
, pos
, max
);
7020 /* Compute the maximum of the given set dimension as a function of the
7021 * parameters, but independently of the other set dimensions.
7023 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7025 return set_dim_opt(set
, pos
, 1);
7028 /* Compute the minimum of the given set dimension as a function of the
7029 * parameters, but independently of the other set dimensions.
7031 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7033 return set_dim_opt(set
, pos
, 0);
7036 /* Apply a preimage specified by "mat" on the parameters of "bset".
7037 * bset is assumed to have only parameters and divs.
7039 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7040 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7044 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7045 if (nparam
< 0 || !mat
)
7048 bset
->dim
= isl_space_cow(bset
->dim
);
7052 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7054 bset
->dim
->nparam
= 0;
7055 bset
->dim
->n_out
= nparam
;
7056 bset
= isl_basic_set_preimage(bset
, mat
);
7058 bset
->dim
->nparam
= bset
->dim
->n_out
;
7059 bset
->dim
->n_out
= 0;
7064 isl_basic_set_free(bset
);
7068 /* Apply a preimage specified by "mat" on the parameters of "set".
7069 * set is assumed to have only parameters and divs.
7071 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7072 __isl_take isl_mat
*mat
)
7077 nparam
= isl_set_dim(set
, isl_dim_param
);
7078 if (nparam
< 0 || !mat
)
7081 if (mat
->n_row
!= 1 + nparam
)
7082 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7083 "unexpected number of rows", goto error
);
7085 space
= isl_set_get_space(set
);
7086 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7087 isl_dim_param
, 0, nparam
);
7088 set
= isl_set_reset_space(set
, space
);
7089 set
= isl_set_preimage(set
, mat
);
7090 nparam
= isl_set_dim(set
, isl_dim_out
);
7092 set
= isl_set_free(set
);
7093 space
= isl_set_get_space(set
);
7094 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7095 isl_dim_out
, 0, nparam
);
7096 set
= isl_set_reset_space(set
, space
);
7104 /* Intersect the basic set "bset" with the affine space specified by the
7105 * equalities in "eq".
7107 static __isl_give isl_basic_set
*basic_set_append_equalities(
7108 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7116 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7121 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7122 for (i
= 0; i
< eq
->n_row
; ++i
) {
7123 k
= isl_basic_set_alloc_equality(bset
);
7126 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7127 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7131 bset
= isl_basic_set_gauss(bset
, NULL
);
7132 bset
= isl_basic_set_finalize(bset
);
7137 isl_basic_set_free(bset
);
7141 /* Intersect the set "set" with the affine space specified by the
7142 * equalities in "eq".
7144 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7152 for (i
= 0; i
< set
->n
; ++i
) {
7153 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7166 /* Given a basic set "bset" that only involves parameters and existentially
7167 * quantified variables, return the index of the first equality
7168 * that only involves parameters. If there is no such equality then
7169 * return bset->n_eq.
7171 * This function assumes that isl_basic_set_gauss has been called on "bset".
7173 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7176 isl_size nparam
, n_div
;
7178 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7179 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7180 if (nparam
< 0 || n_div
< 0)
7183 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7184 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7191 /* Compute an explicit representation for the existentially quantified
7192 * variables in "bset" by computing the "minimal value" of the set
7193 * variables. Since there are no set variables, the computation of
7194 * the minimal value essentially computes an explicit representation
7195 * of the non-empty part(s) of "bset".
7197 * The input only involves parameters and existentially quantified variables.
7198 * All equalities among parameters have been removed.
7200 * Since the existentially quantified variables in the result are in general
7201 * going to be different from those in the input, we first replace
7202 * them by the minimal number of variables based on their equalities.
7203 * This should simplify the parametric integer programming.
7205 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7207 isl_morph
*morph1
, *morph2
;
7213 if (bset
->n_eq
== 0)
7214 return isl_basic_set_lexmin_compute_divs(bset
);
7216 morph1
= isl_basic_set_parameter_compression(bset
);
7217 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7218 bset
= isl_basic_set_lift(bset
);
7219 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7220 bset
= isl_morph_basic_set(morph2
, bset
);
7221 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7223 bset
= isl_basic_set_free(bset
);
7224 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7226 set
= isl_basic_set_lexmin_compute_divs(bset
);
7228 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7233 /* Project the given basic set onto its parameter domain, possibly introducing
7234 * new, explicit, existential variables in the constraints.
7235 * The input has parameters and (possibly implicit) existential variables.
7236 * The output has the same parameters, but only
7237 * explicit existentially quantified variables.
7239 * The actual projection is performed by pip, but pip doesn't seem
7240 * to like equalities very much, so we first remove the equalities
7241 * among the parameters by performing a variable compression on
7242 * the parameters. Afterward, an inverse transformation is performed
7243 * and the equalities among the parameters are inserted back in.
7245 * The variable compression on the parameters may uncover additional
7246 * equalities that were only implicit before. We therefore check
7247 * if there are any new parameter equalities in the result and
7248 * if so recurse. The removal of parameter equalities is required
7249 * for the parameter compression performed by base_compute_divs.
7251 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7255 struct isl_mat
*T
, *T2
;
7256 struct isl_set
*set
;
7259 bset
= isl_basic_set_cow(bset
);
7263 if (bset
->n_eq
== 0)
7264 return base_compute_divs(bset
);
7266 bset
= isl_basic_set_gauss(bset
, NULL
);
7269 if (isl_basic_set_plain_is_empty(bset
))
7270 return isl_set_from_basic_set(bset
);
7272 i
= first_parameter_equality(bset
);
7273 if (i
== bset
->n_eq
)
7274 return base_compute_divs(bset
);
7276 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7278 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7279 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7281 eq
= isl_mat_cow(eq
);
7282 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7283 if (T
&& T
->n_col
== 0) {
7287 bset
= isl_basic_set_set_to_empty(bset
);
7288 return isl_set_from_basic_set(bset
);
7290 bset
= basic_set_parameter_preimage(bset
, T
);
7292 i
= first_parameter_equality(bset
);
7295 else if (i
== bset
->n_eq
)
7296 set
= base_compute_divs(bset
);
7298 set
= parameter_compute_divs(bset
);
7299 set
= set_parameter_preimage(set
, T2
);
7300 set
= set_append_equalities(set
, eq
);
7304 /* Insert the divs from "ls" before those of "bmap".
7306 * The number of columns is not changed, which means that the last
7307 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7308 * The caller is responsible for removing the same number of dimensions
7309 * from the space of "bmap".
7311 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7312 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7318 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7320 return isl_basic_map_free(bmap
);
7324 old_n_div
= bmap
->n_div
;
7325 bmap
= insert_div_rows(bmap
, n_div
);
7329 for (i
= 0; i
< n_div
; ++i
) {
7330 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7331 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7337 /* Replace the space of "bmap" by the space and divs of "ls".
7339 * If "ls" has any divs, then we simplify the result since we may
7340 * have discovered some additional equalities that could simplify
7341 * the div expressions.
7343 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7344 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7348 bmap
= isl_basic_map_cow(bmap
);
7349 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7350 if (!bmap
|| n_div
< 0)
7353 bmap
= insert_divs_from_local_space(bmap
, ls
);
7357 isl_space_free(bmap
->dim
);
7358 bmap
->dim
= isl_local_space_get_space(ls
);
7362 isl_local_space_free(ls
);
7364 bmap
= isl_basic_map_simplify(bmap
);
7365 bmap
= isl_basic_map_finalize(bmap
);
7368 isl_basic_map_free(bmap
);
7369 isl_local_space_free(ls
);
7373 /* Replace the space of "map" by the space and divs of "ls".
7375 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7376 __isl_take isl_local_space
*ls
)
7380 map
= isl_map_cow(map
);
7384 for (i
= 0; i
< map
->n
; ++i
) {
7385 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7386 isl_local_space_copy(ls
));
7390 isl_space_free(map
->dim
);
7391 map
->dim
= isl_local_space_get_space(ls
);
7395 isl_local_space_free(ls
);
7398 isl_local_space_free(ls
);
7403 /* Compute an explicit representation for the existentially
7404 * quantified variables for which do not know any explicit representation yet.
7406 * We first sort the existentially quantified variables so that the
7407 * existentially quantified variables for which we already have an explicit
7408 * representation are placed before those for which we do not.
7409 * The input dimensions, the output dimensions and the existentially
7410 * quantified variables for which we already have an explicit
7411 * representation are then turned into parameters.
7412 * compute_divs returns a map with the same parameters and
7413 * no input or output dimensions and the dimension specification
7414 * is reset to that of the input, including the existentially quantified
7415 * variables for which we already had an explicit representation.
7417 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7419 struct isl_basic_set
*bset
;
7420 struct isl_set
*set
;
7421 struct isl_map
*map
;
7423 isl_local_space
*ls
;
7430 bmap
= isl_basic_map_sort_divs(bmap
);
7431 bmap
= isl_basic_map_cow(bmap
);
7435 n_known
= isl_basic_map_first_unknown_div(bmap
);
7436 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7437 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7438 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7439 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7440 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7442 space
= isl_space_set_alloc(bmap
->ctx
,
7443 nparam
+ n_in
+ n_out
+ n_known
, 0);
7447 ls
= isl_basic_map_get_local_space(bmap
);
7448 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7449 n_known
, bmap
->n_div
- n_known
);
7451 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7452 swap_div(bmap
, i
- n_known
, i
);
7453 bmap
->n_div
-= n_known
;
7454 bmap
->extra
-= n_known
;
7456 bmap
= isl_basic_map_reset_space(bmap
, space
);
7457 bset
= bset_from_bmap(bmap
);
7459 set
= parameter_compute_divs(bset
);
7460 map
= set_to_map(set
);
7461 map
= replace_space_by_local_space(map
, ls
);
7465 isl_basic_map_free(bmap
);
7469 /* Remove the explicit representation of local variable "div",
7472 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7473 __isl_take isl_basic_map
*bmap
, int div
)
7477 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7479 return isl_basic_map_free(bmap
);
7483 bmap
= isl_basic_map_cow(bmap
);
7486 isl_int_set_si(bmap
->div
[div
][0], 0);
7490 /* Is local variable "div" of "bmap" marked as not having an explicit
7492 * Note that even if "div" is not marked in this way and therefore
7493 * has an explicit representation, this representation may still
7494 * depend (indirectly) on other local variables that do not
7495 * have an explicit representation.
7497 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7500 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7501 return isl_bool_error
;
7502 return isl_int_is_zero(bmap
->div
[div
][0]);
7505 /* Return the position of the first local variable that does not
7506 * have an explicit representation.
7507 * Return the total number of local variables if they all have
7508 * an explicit representation.
7509 * Return -1 on error.
7511 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7518 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7519 if (!isl_basic_map_div_is_known(bmap
, i
))
7525 /* Return the position of the first local variable that does not
7526 * have an explicit representation.
7527 * Return the total number of local variables if they all have
7528 * an explicit representation.
7529 * Return -1 on error.
7531 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7533 return isl_basic_map_first_unknown_div(bset
);
7536 /* Does "bmap" have an explicit representation for all local variables?
7538 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7543 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7544 first
= isl_basic_map_first_unknown_div(bmap
);
7545 if (n
< 0 || first
< 0)
7546 return isl_bool_error
;
7550 /* Do all basic maps in "map" have an explicit representation
7551 * for all local variables?
7553 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7558 return isl_bool_error
;
7560 for (i
= 0; i
< map
->n
; ++i
) {
7561 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7566 return isl_bool_true
;
7569 /* If bmap contains any unknown divs, then compute explicit
7570 * expressions for them. However, this computation may be
7571 * quite expensive, so first try to remove divs that aren't
7574 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7577 struct isl_map
*map
;
7579 known
= isl_basic_map_divs_known(bmap
);
7583 return isl_map_from_basic_map(bmap
);
7585 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7587 known
= isl_basic_map_divs_known(bmap
);
7591 return isl_map_from_basic_map(bmap
);
7593 map
= compute_divs(bmap
);
7596 isl_basic_map_free(bmap
);
7600 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7604 struct isl_map
*res
;
7611 known
= isl_map_divs_known(map
);
7619 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7620 for (i
= 1 ; i
< map
->n
; ++i
) {
7622 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7623 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7624 res
= isl_map_union_disjoint(res
, r2
);
7626 res
= isl_map_union(res
, r2
);
7633 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7635 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7638 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7640 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7643 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7646 struct isl_set
*set
;
7651 map
= isl_map_cow(map
);
7655 set
= set_from_map(map
);
7656 set
->dim
= isl_space_domain(set
->dim
);
7659 for (i
= 0; i
< map
->n
; ++i
) {
7660 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7664 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7665 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7672 /* Return the union of "map1" and "map2", where we assume for now that
7673 * "map1" and "map2" are disjoint. Note that the basic maps inside
7674 * "map1" or "map2" may not be disjoint from each other.
7675 * Also note that this function is also called from isl_map_union,
7676 * which takes care of handling the situation where "map1" and "map2"
7677 * may not be disjoint.
7679 * If one of the inputs is empty, we can simply return the other input.
7680 * Similarly, if one of the inputs is universal, then it is equal to the union.
7682 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7683 __isl_take isl_map
*map2
)
7687 struct isl_map
*map
= NULL
;
7693 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7694 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7695 "spaces don't match", goto error
);
7706 is_universe
= isl_map_plain_is_universe(map1
);
7707 if (is_universe
< 0)
7714 is_universe
= isl_map_plain_is_universe(map2
);
7715 if (is_universe
< 0)
7722 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7723 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7724 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7726 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7727 map1
->n
+ map2
->n
, flags
);
7730 for (i
= 0; i
< map1
->n
; ++i
) {
7731 map
= isl_map_add_basic_map(map
,
7732 isl_basic_map_copy(map1
->p
[i
]));
7736 for (i
= 0; i
< map2
->n
; ++i
) {
7737 map
= isl_map_add_basic_map(map
,
7738 isl_basic_map_copy(map2
->p
[i
]));
7752 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7753 * guaranteed to be disjoint by the caller.
7755 * Note that this functions is called from within isl_map_make_disjoint,
7756 * so we have to be careful not to touch the constraints of the inputs
7759 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7760 __isl_take isl_map
*map2
)
7762 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7765 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7766 * not be disjoint. The parameters are assumed to have been aligned.
7768 * We currently simply call map_union_disjoint, the internal operation
7769 * of which does not really depend on the inputs being disjoint.
7770 * If the result contains more than one basic map, then we clear
7771 * the disjoint flag since the result may contain basic maps from
7772 * both inputs and these are not guaranteed to be disjoint.
7774 * As a special case, if "map1" and "map2" are obviously equal,
7775 * then we simply return "map1".
7777 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7778 __isl_take isl_map
*map2
)
7785 equal
= isl_map_plain_is_equal(map1
, map2
);
7793 map1
= map_union_disjoint(map1
, map2
);
7797 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7805 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7808 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7809 __isl_take isl_map
*map2
)
7811 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7814 __isl_give isl_set
*isl_set_union_disjoint(
7815 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7817 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7821 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7823 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7826 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7829 * "map" and "set" are assumed to be compatible and non-NULL.
7831 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7832 __isl_take isl_set
*set
,
7833 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7834 __isl_take isl_basic_set
*bset
))
7837 struct isl_map
*result
;
7840 if (isl_set_plain_is_universe(set
)) {
7845 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7846 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7847 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7849 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7850 map
->n
* set
->n
, flags
);
7851 for (i
= 0; result
&& i
< map
->n
; ++i
)
7852 for (j
= 0; j
< set
->n
; ++j
) {
7853 result
= isl_map_add_basic_map(result
,
7854 fn(isl_basic_map_copy(map
->p
[i
]),
7855 isl_basic_set_copy(set
->p
[j
])));
7865 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7866 __isl_take isl_set
*set
)
7870 ok
= isl_map_compatible_range(map
, set
);
7874 isl_die(set
->ctx
, isl_error_invalid
,
7875 "incompatible spaces", goto error
);
7877 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7884 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7885 __isl_take isl_set
*set
)
7887 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7890 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7891 __isl_take isl_set
*set
)
7895 ok
= isl_map_compatible_domain(map
, set
);
7899 isl_die(set
->ctx
, isl_error_invalid
,
7900 "incompatible spaces", goto error
);
7902 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7909 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7910 __isl_take isl_set
*set
)
7912 return isl_map_align_params_map_map_and(map
, set
,
7913 &map_intersect_domain
);
7916 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7917 * in the space B -> C, return the intersection.
7918 * The parameters are assumed to have been aligned.
7920 * The map "factor" is first extended to a map living in the space
7921 * [A -> B] -> C and then a regular intersection is computed.
7923 static __isl_give isl_map
*map_intersect_domain_factor_range(
7924 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7927 isl_map
*ext_factor
;
7929 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7930 ext_factor
= isl_map_universe(space
);
7931 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7932 return map_intersect(map
, ext_factor
);
7935 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7936 * in the space B -> C, return the intersection.
7938 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7939 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7941 return isl_map_align_params_map_map_and(map
, factor
,
7942 &map_intersect_domain_factor_range
);
7945 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7946 * in the space A -> C, return the intersection.
7948 * The map "factor" is first extended to a map living in the space
7949 * A -> [B -> C] and then a regular intersection is computed.
7951 static __isl_give isl_map
*map_intersect_range_factor_range(
7952 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7955 isl_map
*ext_factor
;
7957 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7958 ext_factor
= isl_map_universe(space
);
7959 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7960 return isl_map_intersect(map
, ext_factor
);
7963 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7964 * in the space A -> C, return the intersection.
7966 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7967 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7969 return isl_map_align_params_map_map_and(map
, factor
,
7970 &map_intersect_range_factor_range
);
7973 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7974 __isl_take isl_map
*map2
)
7978 map1
= isl_map_reverse(map1
);
7979 map1
= isl_map_apply_range(map1
, map2
);
7980 return isl_map_reverse(map1
);
7987 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7988 __isl_take isl_map
*map2
)
7990 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7993 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7994 __isl_take isl_map
*map2
)
7997 struct isl_map
*result
;
8003 space
= isl_space_join(isl_space_copy(map1
->dim
),
8004 isl_space_copy(map2
->dim
));
8006 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8009 for (i
= 0; i
< map1
->n
; ++i
)
8010 for (j
= 0; j
< map2
->n
; ++j
) {
8011 result
= isl_map_add_basic_map(result
,
8012 isl_basic_map_apply_range(
8013 isl_basic_map_copy(map1
->p
[i
]),
8014 isl_basic_map_copy(map2
->p
[j
])));
8020 if (result
&& result
->n
<= 1)
8021 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8029 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8030 __isl_take isl_map
*map2
)
8032 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8036 * returns range - domain
8038 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8040 isl_space
*target_space
;
8041 struct isl_basic_set
*bset
;
8049 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8050 bmap
->dim
, isl_dim_out
),
8052 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8053 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8054 if (dim
< 0 || nparam
< 0)
8056 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8057 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8058 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8059 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8061 bmap
= isl_basic_map_free(bmap
);
8062 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8063 for (i
= 0; i
< dim
; ++i
) {
8064 int j
= isl_basic_map_alloc_equality(bmap
);
8066 bmap
= isl_basic_map_free(bmap
);
8069 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8070 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8071 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8072 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8074 bset
= isl_basic_map_domain(bmap
);
8075 bset
= isl_basic_set_reset_space(bset
, target_space
);
8078 isl_basic_map_free(bmap
);
8082 /* Check that domain and range of "map" are the same.
8084 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
8089 space
= isl_map_peek_space(map
);
8090 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8092 return isl_stat_error
;
8094 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8095 "domain and range don't match", return isl_stat_error
);
8100 * returns range - domain
8102 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8106 struct isl_set
*result
;
8111 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8112 map
->dim
, isl_dim_out
),
8114 dim
= isl_map_get_space(map
);
8115 dim
= isl_space_domain(dim
);
8116 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8119 for (i
= 0; i
< map
->n
; ++i
)
8120 result
= isl_set_add_basic_set(result
,
8121 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8130 * returns [domain -> range] -> range - domain
8132 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8133 __isl_take isl_basic_map
*bmap
)
8137 isl_basic_map
*domain
;
8141 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8142 bmap
->dim
, isl_dim_out
))
8143 isl_die(bmap
->ctx
, isl_error_invalid
,
8144 "domain and range don't match", goto error
);
8146 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8147 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8148 if (nparam
< 0 || n
)
8149 return isl_basic_map_free(bmap
);
8151 space
= isl_basic_map_get_space(bmap
);
8152 space
= isl_space_from_range(isl_space_domain(space
));
8153 domain
= isl_basic_map_universe(space
);
8155 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8156 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8157 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8159 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8161 return isl_basic_map_free(bmap
);
8163 for (i
= 0; i
< n
; ++i
) {
8164 k
= isl_basic_map_alloc_equality(bmap
);
8167 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8168 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8169 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8170 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8173 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8174 return isl_basic_map_finalize(bmap
);
8176 isl_basic_map_free(bmap
);
8181 * returns [domain -> range] -> range - domain
8183 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8186 isl_space
*domain_space
;
8188 if (isl_map_check_equal_tuples(map
) < 0)
8189 return isl_map_free(map
);
8191 map
= isl_map_cow(map
);
8195 domain_space
= isl_space_domain(isl_map_get_space(map
));
8196 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8197 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8200 for (i
= 0; i
< map
->n
; ++i
) {
8201 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8205 map
= isl_map_unmark_normalized(map
);
8212 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8214 isl_size n_in
, n_out
;
8216 n_in
= isl_space_dim(space
, isl_dim_in
);
8217 n_out
= isl_space_dim(space
, isl_dim_out
);
8218 if (n_in
< 0 || n_out
< 0)
8221 isl_die(space
->ctx
, isl_error_invalid
,
8222 "number of input and output dimensions needs to be "
8223 "the same", goto error
);
8224 return isl_basic_map_equal(space
, n_in
);
8226 isl_space_free(space
);
8230 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8232 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8235 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8237 isl_space
*dim
= isl_set_get_space(set
);
8239 id
= isl_map_identity(isl_space_map_from_set(dim
));
8240 return isl_map_intersect_range(id
, set
);
8243 /* Construct a basic set with all set dimensions having only non-negative
8246 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8247 __isl_take isl_space
*space
)
8253 struct isl_basic_set
*bset
;
8255 nparam
= isl_space_dim(space
, isl_dim_param
);
8256 dim
= isl_space_dim(space
, isl_dim_set
);
8257 total
= isl_space_dim(space
, isl_dim_all
);
8258 if (nparam
< 0 || dim
< 0 || total
< 0)
8259 space
= isl_space_free(space
);
8260 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8263 for (i
= 0; i
< dim
; ++i
) {
8264 int k
= isl_basic_set_alloc_inequality(bset
);
8267 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8268 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8272 isl_basic_set_free(bset
);
8276 /* Construct the half-space x_pos >= 0.
8278 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8283 isl_basic_set
*nonneg
;
8285 total
= isl_space_dim(space
, isl_dim_all
);
8287 space
= isl_space_free(space
);
8288 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8289 k
= isl_basic_set_alloc_inequality(nonneg
);
8292 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8293 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8295 return isl_basic_set_finalize(nonneg
);
8297 isl_basic_set_free(nonneg
);
8301 /* Construct the half-space x_pos <= -1.
8303 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8310 total
= isl_space_dim(space
, isl_dim_all
);
8312 space
= isl_space_free(space
);
8313 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8314 k
= isl_basic_set_alloc_inequality(neg
);
8317 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8318 isl_int_set_si(neg
->ineq
[k
][0], -1);
8319 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8321 return isl_basic_set_finalize(neg
);
8323 isl_basic_set_free(neg
);
8327 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8328 enum isl_dim_type type
, unsigned first
, unsigned n
)
8332 isl_basic_set
*nonneg
;
8338 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8339 return isl_set_free(set
);
8341 offset
= pos(set
->dim
, type
);
8342 for (i
= 0; i
< n
; ++i
) {
8343 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8344 offset
+ first
+ i
);
8345 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8347 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8353 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8355 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8361 return isl_stat_error
;
8362 if (isl_set_plain_is_empty(set
)) {
8367 return fn(set
, signs
, user
);
8370 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8372 half
= isl_set_intersect(half
, isl_set_copy(set
));
8373 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8377 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8379 half
= isl_set_intersect(half
, set
);
8380 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8383 return isl_stat_error
;
8386 /* Call "fn" on the intersections of "set" with each of the orthants
8387 * (except for obviously empty intersections). The orthant is identified
8388 * by the signs array, with each entry having value 1 or -1 according
8389 * to the sign of the corresponding variable.
8391 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8392 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8401 return isl_stat_error
;
8402 if (isl_set_plain_is_empty(set
))
8405 nparam
= isl_set_dim(set
, isl_dim_param
);
8406 nvar
= isl_set_dim(set
, isl_dim_set
);
8407 if (nparam
< 0 || nvar
< 0)
8408 return isl_stat_error
;
8410 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8412 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8420 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8422 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8425 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8426 __isl_keep isl_basic_map
*bmap2
)
8429 struct isl_map
*map1
;
8430 struct isl_map
*map2
;
8432 if (!bmap1
|| !bmap2
)
8433 return isl_bool_error
;
8435 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8436 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8438 is_subset
= isl_map_is_subset(map1
, map2
);
8446 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8447 __isl_keep isl_basic_set
*bset2
)
8449 return isl_basic_map_is_subset(bset1
, bset2
);
8452 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8453 __isl_keep isl_basic_map
*bmap2
)
8457 if (!bmap1
|| !bmap2
)
8458 return isl_bool_error
;
8459 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8460 if (is_subset
!= isl_bool_true
)
8462 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8466 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8467 __isl_keep isl_basic_set
*bset2
)
8469 return isl_basic_map_is_equal(
8470 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8473 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8479 return isl_bool_error
;
8480 for (i
= 0; i
< map
->n
; ++i
) {
8481 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8483 return isl_bool_error
;
8485 return isl_bool_false
;
8487 return isl_bool_true
;
8490 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8492 return map
? map
->n
== 0 : isl_bool_error
;
8495 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8497 return set
? set
->n
== 0 : isl_bool_error
;
8500 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8502 return isl_map_is_empty(set_to_map(set
));
8505 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8506 __isl_keep isl_map
*map2
)
8509 return isl_bool_error
;
8511 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8514 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8515 __isl_keep isl_set
*set2
)
8518 return isl_bool_error
;
8520 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8523 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8528 return isl_bool_error
;
8529 is_subset
= isl_map_is_subset(map1
, map2
);
8530 if (is_subset
!= isl_bool_true
)
8532 is_subset
= isl_map_is_subset(map2
, map1
);
8536 /* Is "map1" equal to "map2"?
8538 * First check if they are obviously equal.
8539 * If not, then perform a more detailed analysis.
8541 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8545 equal
= isl_map_plain_is_equal(map1
, map2
);
8546 if (equal
< 0 || equal
)
8548 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8551 isl_bool
isl_basic_map_is_strict_subset(
8552 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8556 if (!bmap1
|| !bmap2
)
8557 return isl_bool_error
;
8558 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8559 if (is_subset
!= isl_bool_true
)
8561 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8562 return isl_bool_not(is_subset
);
8565 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8566 __isl_keep isl_map
*map2
)
8571 return isl_bool_error
;
8572 is_subset
= isl_map_is_subset(map1
, map2
);
8573 if (is_subset
!= isl_bool_true
)
8575 is_subset
= isl_map_is_subset(map2
, map1
);
8576 return isl_bool_not(is_subset
);
8579 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8580 __isl_keep isl_set
*set2
)
8582 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8585 /* Is "bmap" obviously equal to the universe with the same space?
8587 * That is, does it not have any constraints?
8589 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8592 return isl_bool_error
;
8593 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8596 /* Is "bset" obviously equal to the universe with the same space?
8598 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8600 return isl_basic_map_plain_is_universe(bset
);
8603 /* If "c" does not involve any existentially quantified variables,
8604 * then set *univ to false and abort
8606 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8608 isl_bool
*univ
= user
;
8611 n
= isl_constraint_dim(c
, isl_dim_div
);
8613 c
= isl_constraint_free(c
);
8614 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8615 isl_constraint_free(c
);
8616 if (*univ
< 0 || !*univ
)
8617 return isl_stat_error
;
8621 /* Is "bmap" equal to the universe with the same space?
8623 * First check if it is obviously equal to the universe.
8624 * If not and if there are any constraints not involving
8625 * existentially quantified variables, then it is certainly
8626 * not equal to the universe.
8627 * Otherwise, check if the universe is a subset of "bmap".
8629 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8633 isl_basic_map
*test
;
8635 univ
= isl_basic_map_plain_is_universe(bmap
);
8636 if (univ
< 0 || univ
)
8638 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
8640 return isl_bool_error
;
8642 return isl_bool_false
;
8643 univ
= isl_bool_true
;
8644 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8646 return isl_bool_error
;
8647 if (univ
< 0 || !univ
)
8649 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8650 univ
= isl_basic_map_is_subset(test
, bmap
);
8651 isl_basic_map_free(test
);
8655 /* Is "bset" equal to the universe with the same space?
8657 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8659 return isl_basic_map_is_universe(bset
);
8662 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8667 return isl_bool_error
;
8669 for (i
= 0; i
< map
->n
; ++i
) {
8670 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8675 return isl_bool_false
;
8678 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8680 return isl_map_plain_is_universe(set_to_map(set
));
8683 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8685 struct isl_basic_set
*bset
= NULL
;
8686 struct isl_vec
*sample
= NULL
;
8687 isl_bool empty
, non_empty
;
8690 return isl_bool_error
;
8692 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8693 return isl_bool_true
;
8695 if (isl_basic_map_plain_is_universe(bmap
))
8696 return isl_bool_false
;
8698 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8699 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8700 copy
= isl_basic_map_remove_redundancies(copy
);
8701 empty
= isl_basic_map_plain_is_empty(copy
);
8702 isl_basic_map_free(copy
);
8706 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8708 return isl_bool_error
;
8710 return isl_bool_false
;
8711 isl_vec_free(bmap
->sample
);
8712 bmap
->sample
= NULL
;
8713 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8715 return isl_bool_error
;
8716 sample
= isl_basic_set_sample_vec(bset
);
8718 return isl_bool_error
;
8719 empty
= sample
->size
== 0;
8720 isl_vec_free(bmap
->sample
);
8721 bmap
->sample
= sample
;
8723 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8728 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8731 return isl_bool_error
;
8732 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8735 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8738 return isl_bool_error
;
8739 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8742 /* Is "bmap" known to be non-empty?
8744 * That is, is the cached sample still valid?
8746 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8751 return isl_bool_error
;
8753 return isl_bool_false
;
8754 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8756 return isl_bool_error
;
8757 if (bmap
->sample
->size
!= 1 + total
)
8758 return isl_bool_false
;
8759 return isl_basic_map_contains(bmap
, bmap
->sample
);
8762 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8764 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8767 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8768 __isl_take isl_basic_map
*bmap2
)
8770 struct isl_map
*map
;
8771 if (!bmap1
|| !bmap2
)
8774 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8776 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8779 map
= isl_map_add_basic_map(map
, bmap1
);
8780 map
= isl_map_add_basic_map(map
, bmap2
);
8783 isl_basic_map_free(bmap1
);
8784 isl_basic_map_free(bmap2
);
8788 struct isl_set
*isl_basic_set_union(
8789 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8791 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8792 bset_to_bmap(bset2
)));
8795 /* Order divs such that any div only depends on previous divs */
8796 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8797 __isl_take isl_basic_map
*bmap
)
8802 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8804 return isl_basic_map_free(bmap
);
8806 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8808 if (isl_int_is_zero(bmap
->div
[i
][0]))
8810 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8815 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8816 "integer division depends on itself",
8817 return isl_basic_map_free(bmap
));
8818 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8826 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8828 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8831 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8838 for (i
= 0; i
< map
->n
; ++i
) {
8839 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8850 /* Sort the local variables of "bset".
8852 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8853 __isl_take isl_basic_set
*bset
)
8855 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8858 /* Apply the expansion computed by isl_merge_divs.
8859 * The expansion itself is given by "exp" while the resulting
8860 * list of divs is given by "div".
8862 * Move the integer divisions of "bmap" into the right position
8863 * according to "exp" and then introduce the additional integer
8864 * divisions, adding div constraints.
8865 * The moving should be done first to avoid moving coefficients
8866 * in the definitions of the extra integer divisions.
8868 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8869 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8874 bmap
= isl_basic_map_cow(bmap
);
8878 if (div
->n_row
< bmap
->n_div
)
8879 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8880 "not an expansion", goto error
);
8882 n_div
= bmap
->n_div
;
8883 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8884 div
->n_row
- n_div
, 0,
8885 2 * (div
->n_row
- n_div
));
8887 for (i
= n_div
; i
< div
->n_row
; ++i
)
8888 if (isl_basic_map_alloc_div(bmap
) < 0)
8891 for (j
= n_div
- 1; j
>= 0; --j
) {
8894 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8899 for (i
= 0; i
< div
->n_row
; ++i
) {
8900 if (j
< n_div
&& exp
[j
] == i
) {
8903 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8904 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8906 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8915 isl_basic_map_free(bmap
);
8920 /* Apply the expansion computed by isl_merge_divs.
8921 * The expansion itself is given by "exp" while the resulting
8922 * list of divs is given by "div".
8924 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8925 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8927 return isl_basic_map_expand_divs(bset
, div
, exp
);
8930 /* Look for a div in dst that corresponds to the div "div" in src.
8931 * The divs before "div" in src and dst are assumed to be the same.
8933 * Return the position of the corresponding div in dst
8934 * if there is one. Otherwise, return a position beyond the integer divisions.
8935 * Return -1 on error.
8937 static int find_div(__isl_keep isl_basic_map
*dst
,
8938 __isl_keep isl_basic_map
*src
, unsigned div
)
8944 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8945 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8946 if (n_div
< 0 || v_div
< 0)
8948 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8949 for (i
= div
; i
< n_div
; ++i
)
8950 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8951 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8957 /* Align the divs of "dst" to those of "src", adding divs from "src"
8958 * if needed. That is, make sure that the first src->n_div divs
8959 * of the result are equal to those of src.
8961 * The result is not finalized as by design it will have redundant
8962 * divs if any divs from "src" were copied.
8964 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8965 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8974 return isl_basic_map_free(dst
);
8976 if (src
->n_div
== 0)
8979 known
= isl_basic_map_divs_known(src
);
8981 return isl_basic_map_free(dst
);
8983 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8984 "some src divs are unknown",
8985 return isl_basic_map_free(dst
));
8987 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8989 return isl_basic_map_free(dst
);
8991 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8993 return isl_basic_map_free(dst
);
8996 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8998 dst
= isl_basic_map_free(dst
);
8999 for (i
= 0; i
< src
->n_div
; ++i
) {
9000 int j
= find_div(dst
, src
, i
);
9002 dst
= isl_basic_map_free(dst
);
9003 if (j
== dst_n_div
) {
9005 int extra
= src
->n_div
- i
;
9006 dst
= isl_basic_map_cow(dst
);
9009 dst
= isl_basic_map_extend_space(dst
,
9010 isl_space_copy(dst
->dim
),
9011 extra
, 0, 2 * extra
);
9014 j
= isl_basic_map_alloc_div(dst
);
9017 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9018 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9020 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9025 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9029 isl_basic_map_free(src
);
9032 isl_basic_map_free(src
);
9033 isl_basic_map_free(dst
);
9037 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9045 map
= isl_map_compute_divs(map
);
9046 map
= isl_map_cow(map
);
9050 for (i
= 1; i
< map
->n
; ++i
)
9051 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9052 for (i
= 1; i
< map
->n
; ++i
) {
9053 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9055 return isl_map_free(map
);
9058 map
= isl_map_unmark_normalized(map
);
9062 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9064 return isl_map_align_divs_internal(map
);
9067 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9069 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9072 /* Align the divs of the basic maps in "map" to those
9073 * of the basic maps in "list", as well as to the other basic maps in "map".
9074 * The elements in "list" are assumed to have known divs.
9076 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9077 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9081 map
= isl_map_compute_divs(map
);
9082 map
= isl_map_cow(map
);
9084 return isl_map_free(map
);
9088 n
= isl_basic_map_list_n_basic_map(list
);
9089 for (i
= 0; i
< n
; ++i
) {
9090 isl_basic_map
*bmap
;
9092 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9093 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9094 isl_basic_map_free(bmap
);
9097 return isl_map_free(map
);
9099 return isl_map_align_divs_internal(map
);
9102 /* Align the divs of each element of "list" to those of "bmap".
9103 * Both "bmap" and the elements of "list" are assumed to have known divs.
9105 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9106 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9111 return isl_basic_map_list_free(list
);
9113 n
= isl_basic_map_list_n_basic_map(list
);
9114 for (i
= 0; i
< n
; ++i
) {
9115 isl_basic_map
*bmap_i
;
9117 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9118 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9119 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9125 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9126 __isl_take isl_map
*map
)
9130 ok
= isl_map_compatible_domain(map
, set
);
9134 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9135 "incompatible spaces", goto error
);
9136 map
= isl_map_intersect_domain(map
, set
);
9137 set
= isl_map_range(map
);
9145 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9146 __isl_take isl_map
*map
)
9148 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9151 /* There is no need to cow as removing empty parts doesn't change
9152 * the meaning of the set.
9154 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9161 for (i
= map
->n
- 1; i
>= 0; --i
)
9162 map
= remove_if_empty(map
, i
);
9167 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9169 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9172 /* Create a binary relation that maps the shared initial "pos" dimensions
9173 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9175 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9176 __isl_keep isl_basic_set
*bset2
, int pos
)
9178 isl_basic_map
*bmap1
;
9179 isl_basic_map
*bmap2
;
9181 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9182 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9183 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9184 isl_dim_out
, 0, pos
);
9185 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9186 isl_dim_out
, 0, pos
);
9187 return isl_basic_map_range_product(bmap1
, bmap2
);
9190 /* Given two basic sets bset1 and bset2, compute the maximal difference
9191 * between the values of dimension pos in bset1 and those in bset2
9192 * for any common value of the parameters and dimensions preceding pos.
9194 static enum isl_lp_result
basic_set_maximal_difference_at(
9195 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9196 int pos
, isl_int
*opt
)
9198 isl_basic_map
*bmap1
;
9199 struct isl_ctx
*ctx
;
9200 struct isl_vec
*obj
;
9204 enum isl_lp_result res
;
9206 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9207 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9208 if (nparam
< 0 || dim1
< 0 || !bset2
)
9209 return isl_lp_error
;
9211 bmap1
= join_initial(bset1
, bset2
, pos
);
9212 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9214 return isl_lp_error
;
9217 obj
= isl_vec_alloc(ctx
, 1 + total
);
9220 isl_seq_clr(obj
->block
.data
, 1 + total
);
9221 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9222 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9223 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9225 isl_basic_map_free(bmap1
);
9229 isl_basic_map_free(bmap1
);
9230 return isl_lp_error
;
9233 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9234 * for any common value of the parameters and dimensions preceding pos
9235 * in both basic sets, the values of dimension pos in bset1 are
9236 * smaller or larger than those in bset2.
9239 * 1 if bset1 follows bset2
9240 * -1 if bset1 precedes bset2
9241 * 0 if bset1 and bset2 are incomparable
9242 * -2 if some error occurred.
9244 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9245 __isl_keep isl_basic_set
*bset2
, int pos
)
9248 enum isl_lp_result res
;
9253 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9255 if (res
== isl_lp_empty
)
9257 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9258 res
== isl_lp_unbounded
)
9260 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9269 /* Given two basic sets bset1 and bset2, check whether
9270 * for any common value of the parameters and dimensions preceding pos
9271 * there is a value of dimension pos in bset1 that is larger
9272 * than a value of the same dimension in bset2.
9275 * 1 if there exists such a pair
9276 * 0 if there is no such pair, but there is a pair of equal values
9278 * -2 if some error occurred.
9280 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9281 __isl_keep isl_basic_set
*bset2
, int pos
)
9284 isl_basic_map
*bmap
;
9287 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9290 bmap
= join_initial(bset1
, bset2
, pos
);
9291 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9292 isl_dim_out
, dim1
- pos
);
9293 empty
= isl_basic_map_is_empty(bmap
);
9297 isl_basic_map_free(bmap
);
9300 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9301 isl_dim_out
, dim1
- pos
);
9302 empty
= isl_basic_map_is_empty(bmap
);
9305 isl_basic_map_free(bmap
);
9310 isl_basic_map_free(bmap
);
9314 /* Given two sets set1 and set2, check whether
9315 * for any common value of the parameters and dimensions preceding pos
9316 * there is a value of dimension pos in set1 that is larger
9317 * than a value of the same dimension in set2.
9320 * 1 if there exists such a pair
9321 * 0 if there is no such pair, but there is a pair of equal values
9323 * -2 if some error occurred.
9325 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9326 __isl_keep isl_set
*set2
, int pos
)
9334 for (i
= 0; i
< set1
->n
; ++i
)
9335 for (j
= 0; j
< set2
->n
; ++j
) {
9337 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9338 if (f
== 1 || f
== -2)
9347 static isl_bool
isl_basic_map_plain_has_fixed_var(
9348 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9354 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9356 return isl_bool_error
;
9357 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9358 for (; d
+1 > pos
; --d
)
9359 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9363 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9364 return isl_bool_false
;
9365 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9366 return isl_bool_false
;
9367 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9368 return isl_bool_false
;
9370 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9371 return isl_bool_true
;
9373 return isl_bool_false
;
9376 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9377 unsigned pos
, isl_int
*val
)
9385 return isl_bool_error
;
9387 return isl_bool_false
;
9389 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9392 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9393 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9394 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9395 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9396 fixed
= isl_bool_false
;
9399 isl_int_set(*val
, v
);
9405 static isl_bool
isl_basic_set_plain_has_fixed_var(
9406 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9408 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9412 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9413 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9415 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9416 return isl_bool_error
;
9417 return isl_basic_map_plain_has_fixed_var(bmap
,
9418 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9421 /* If "bmap" obviously lies on a hyperplane where the given dimension
9422 * has a fixed value, then return that value.
9423 * Otherwise return NaN.
9425 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9426 __isl_keep isl_basic_map
*bmap
,
9427 enum isl_dim_type type
, unsigned pos
)
9435 ctx
= isl_basic_map_get_ctx(bmap
);
9436 v
= isl_val_alloc(ctx
);
9439 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9441 return isl_val_free(v
);
9443 isl_int_set_si(v
->d
, 1);
9447 return isl_val_nan(ctx
);
9450 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9451 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9453 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9454 return isl_bool_error
;
9455 return isl_map_plain_has_fixed_var(map
,
9456 map_offset(map
, type
) - 1 + pos
, val
);
9459 /* If "map" obviously lies on a hyperplane where the given dimension
9460 * has a fixed value, then return that value.
9461 * Otherwise return NaN.
9463 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9464 enum isl_dim_type type
, unsigned pos
)
9472 ctx
= isl_map_get_ctx(map
);
9473 v
= isl_val_alloc(ctx
);
9476 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9478 return isl_val_free(v
);
9480 isl_int_set_si(v
->d
, 1);
9484 return isl_val_nan(ctx
);
9487 /* If "set" obviously lies on a hyperplane where the given dimension
9488 * has a fixed value, then return that value.
9489 * Otherwise return NaN.
9491 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9492 enum isl_dim_type type
, unsigned pos
)
9494 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9497 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9498 * then return this fixed value in *val.
9500 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9501 unsigned dim
, isl_int
*val
)
9505 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
9507 return isl_bool_error
;
9508 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
9511 /* Return -1 if the constraint "c1" should be sorted before "c2"
9512 * and 1 if it should be sorted after "c2".
9513 * Return 0 if the two constraints are the same (up to the constant term).
9515 * In particular, if a constraint involves later variables than another
9516 * then it is sorted after this other constraint.
9517 * uset_gist depends on constraints without existentially quantified
9518 * variables sorting first.
9520 * For constraints that have the same latest variable, those
9521 * with the same coefficient for this latest variable (first in absolute value
9522 * and then in actual value) are grouped together.
9523 * This is useful for detecting pairs of constraints that can
9524 * be chained in their printed representation.
9526 * Finally, within a group, constraints are sorted according to
9527 * their coefficients (excluding the constant term).
9529 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9531 isl_int
**c1
= (isl_int
**) p1
;
9532 isl_int
**c2
= (isl_int
**) p2
;
9534 unsigned size
= *(unsigned *) arg
;
9537 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9538 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9543 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9546 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9550 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9553 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9554 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9555 * and 0 if the two constraints are the same (up to the constant term).
9557 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9558 isl_int
*c1
, isl_int
*c2
)
9563 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9567 return sort_constraint_cmp(&c1
, &c2
, &size
);
9570 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9571 __isl_take isl_basic_map
*bmap
)
9578 if (bmap
->n_ineq
== 0)
9580 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9582 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9584 return isl_basic_map_free(bmap
);
9586 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9587 &sort_constraint_cmp
, &size
) < 0)
9588 return isl_basic_map_free(bmap
);
9589 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9593 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9594 __isl_take isl_basic_set
*bset
)
9596 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9597 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9600 __isl_give isl_basic_map
*isl_basic_map_normalize(
9601 __isl_take isl_basic_map
*bmap
)
9603 bmap
= isl_basic_map_remove_redundancies(bmap
);
9604 bmap
= isl_basic_map_sort_constraints(bmap
);
9607 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9608 __isl_keep isl_basic_map
*bmap2
)
9612 isl_space
*space1
, *space2
;
9614 if (!bmap1
|| !bmap2
)
9619 space1
= isl_basic_map_peek_space(bmap1
);
9620 space2
= isl_basic_map_peek_space(bmap2
);
9621 cmp
= isl_space_cmp(space1
, space2
);
9624 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9625 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9626 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9627 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9628 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9630 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9632 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9634 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9635 return bmap1
->n_eq
- bmap2
->n_eq
;
9636 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9637 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9638 if (bmap1
->n_div
!= bmap2
->n_div
)
9639 return bmap1
->n_div
- bmap2
->n_div
;
9640 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9643 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9644 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9648 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9649 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9653 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9654 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9661 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9662 __isl_keep isl_basic_set
*bset2
)
9664 return isl_basic_map_plain_cmp(bset1
, bset2
);
9667 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9673 if (set1
->n
!= set2
->n
)
9674 return set1
->n
- set2
->n
;
9676 for (i
= 0; i
< set1
->n
; ++i
) {
9677 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9685 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9686 __isl_keep isl_basic_map
*bmap2
)
9688 if (!bmap1
|| !bmap2
)
9689 return isl_bool_error
;
9690 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9693 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9694 __isl_keep isl_basic_set
*bset2
)
9696 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9697 bset_to_bmap(bset2
));
9700 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9702 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9703 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9705 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9708 /* Sort the basic maps of "map" and remove duplicate basic maps.
9710 * While removing basic maps, we make sure that the basic maps remain
9711 * sorted because isl_map_normalize expects the basic maps of the result
9714 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9718 map
= isl_map_remove_empty_parts(map
);
9721 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9722 for (i
= map
->n
- 1; i
>= 1; --i
) {
9723 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9725 isl_basic_map_free(map
->p
[i
-1]);
9726 for (j
= i
; j
< map
->n
; ++j
)
9727 map
->p
[j
- 1] = map
->p
[j
];
9734 /* Remove obvious duplicates among the basic maps of "map".
9736 * Unlike isl_map_normalize, this function does not remove redundant
9737 * constraints and only removes duplicates that have exactly the same
9738 * constraints in the input. It does sort the constraints and
9739 * the basic maps to ease the detection of duplicates.
9741 * If "map" has already been normalized or if the basic maps are
9742 * disjoint, then there can be no duplicates.
9744 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9747 isl_basic_map
*bmap
;
9753 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9755 for (i
= 0; i
< map
->n
; ++i
) {
9756 bmap
= isl_basic_map_copy(map
->p
[i
]);
9757 bmap
= isl_basic_map_sort_constraints(bmap
);
9759 return isl_map_free(map
);
9760 isl_basic_map_free(map
->p
[i
]);
9764 map
= sort_and_remove_duplicates(map
);
9768 /* We normalize in place, but if anything goes wrong we need
9769 * to return NULL, so we need to make sure we don't change the
9770 * meaning of any possible other copies of map.
9772 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9775 struct isl_basic_map
*bmap
;
9779 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9781 for (i
= 0; i
< map
->n
; ++i
) {
9782 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9785 isl_basic_map_free(map
->p
[i
]);
9789 map
= sort_and_remove_duplicates(map
);
9791 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9798 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9800 return set_from_map(isl_map_normalize(set_to_map(set
)));
9803 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9804 __isl_keep isl_map
*map2
)
9810 return isl_bool_error
;
9813 return isl_bool_true
;
9814 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9815 return isl_bool_false
;
9817 map1
= isl_map_copy(map1
);
9818 map2
= isl_map_copy(map2
);
9819 map1
= isl_map_normalize(map1
);
9820 map2
= isl_map_normalize(map2
);
9823 equal
= map1
->n
== map2
->n
;
9824 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9825 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9835 return isl_bool_error
;
9838 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9839 __isl_keep isl_set
*set2
)
9841 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9844 /* Return the basic maps in "map" as a list.
9846 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9847 __isl_keep isl_map
*map
)
9851 isl_basic_map_list
*list
;
9855 ctx
= isl_map_get_ctx(map
);
9856 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9858 for (i
= 0; i
< map
->n
; ++i
) {
9859 isl_basic_map
*bmap
;
9861 bmap
= isl_basic_map_copy(map
->p
[i
]);
9862 list
= isl_basic_map_list_add(list
, bmap
);
9868 /* Return the intersection of the elements in the non-empty list "list".
9869 * All elements are assumed to live in the same space.
9871 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9872 __isl_take isl_basic_map_list
*list
)
9875 isl_basic_map
*bmap
;
9879 n
= isl_basic_map_list_n_basic_map(list
);
9881 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9882 "expecting non-empty list", goto error
);
9884 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9885 for (i
= 1; i
< n
; ++i
) {
9886 isl_basic_map
*bmap_i
;
9888 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9889 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9892 isl_basic_map_list_free(list
);
9895 isl_basic_map_list_free(list
);
9899 /* Return the intersection of the elements in the non-empty list "list".
9900 * All elements are assumed to live in the same space.
9902 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9903 __isl_take isl_basic_set_list
*list
)
9905 return isl_basic_map_list_intersect(list
);
9908 /* Return the union of the elements of "list".
9909 * The list is required to have at least one element.
9911 __isl_give isl_set
*isl_basic_set_list_union(
9912 __isl_take isl_basic_set_list
*list
)
9916 isl_basic_set
*bset
;
9921 n
= isl_basic_set_list_n_basic_set(list
);
9923 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9924 "expecting non-empty list", goto error
);
9926 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9927 space
= isl_basic_set_get_space(bset
);
9928 isl_basic_set_free(bset
);
9930 set
= isl_set_alloc_space(space
, n
, 0);
9931 for (i
= 0; i
< n
; ++i
) {
9932 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9933 set
= isl_set_add_basic_set(set
, bset
);
9936 isl_basic_set_list_free(list
);
9939 isl_basic_set_list_free(list
);
9943 /* Return the union of the elements in the non-empty list "list".
9944 * All elements are assumed to live in the same space.
9946 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9953 n
= isl_set_list_n_set(list
);
9955 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9956 "expecting non-empty list", goto error
);
9958 set
= isl_set_list_get_set(list
, 0);
9959 for (i
= 1; i
< n
; ++i
) {
9962 set_i
= isl_set_list_get_set(list
, i
);
9963 set
= isl_set_union(set
, set_i
);
9966 isl_set_list_free(list
);
9969 isl_set_list_free(list
);
9973 __isl_give isl_basic_map
*isl_basic_map_product(
9974 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9976 isl_space
*space_result
= NULL
;
9977 struct isl_basic_map
*bmap
;
9978 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9979 struct isl_dim_map
*dim_map1
, *dim_map2
;
9981 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9983 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9984 isl_space_copy(bmap2
->dim
));
9986 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9987 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9988 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9989 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9990 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9992 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9993 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9994 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9995 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9996 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9997 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9998 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9999 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10000 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10001 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10002 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10004 bmap
= isl_basic_map_alloc_space(space_result
,
10005 bmap1
->n_div
+ bmap2
->n_div
,
10006 bmap1
->n_eq
+ bmap2
->n_eq
,
10007 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10008 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10009 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10010 bmap
= isl_basic_map_simplify(bmap
);
10011 return isl_basic_map_finalize(bmap
);
10013 isl_basic_map_free(bmap1
);
10014 isl_basic_map_free(bmap2
);
10018 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10019 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10021 isl_basic_map
*prod
;
10023 prod
= isl_basic_map_product(bmap1
, bmap2
);
10024 prod
= isl_basic_map_flatten(prod
);
10028 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10029 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10031 return isl_basic_map_flat_range_product(bset1
, bset2
);
10034 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10035 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10037 isl_space
*space1
, *space2
;
10038 isl_space
*space_result
= NULL
;
10039 isl_basic_map
*bmap
;
10040 isl_size in1
, in2
, out
, nparam
;
10041 unsigned total
, pos
;
10042 struct isl_dim_map
*dim_map1
, *dim_map2
;
10044 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10045 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10046 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10047 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10048 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10051 space1
= isl_basic_map_get_space(bmap1
);
10052 space2
= isl_basic_map_get_space(bmap2
);
10053 space_result
= isl_space_domain_product(space1
, space2
);
10055 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10056 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10057 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10058 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10059 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10060 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10061 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10062 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10063 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10064 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10065 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10067 bmap
= isl_basic_map_alloc_space(space_result
,
10068 bmap1
->n_div
+ bmap2
->n_div
,
10069 bmap1
->n_eq
+ bmap2
->n_eq
,
10070 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10071 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10072 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10073 bmap
= isl_basic_map_simplify(bmap
);
10074 return isl_basic_map_finalize(bmap
);
10076 isl_basic_map_free(bmap1
);
10077 isl_basic_map_free(bmap2
);
10081 __isl_give isl_basic_map
*isl_basic_map_range_product(
10082 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10085 isl_space
*space_result
= NULL
;
10086 isl_basic_map
*bmap
;
10087 isl_size in
, out1
, out2
, nparam
;
10088 unsigned total
, pos
;
10089 struct isl_dim_map
*dim_map1
, *dim_map2
;
10091 rational
= isl_basic_map_is_rational(bmap1
);
10092 if (rational
>= 0 && rational
)
10093 rational
= isl_basic_map_is_rational(bmap2
);
10094 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10095 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10096 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10097 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10098 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10101 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10104 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10105 isl_space_copy(bmap2
->dim
));
10107 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10108 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10109 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10110 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10111 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10112 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10113 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10114 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10115 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10116 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10117 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10119 bmap
= isl_basic_map_alloc_space(space_result
,
10120 bmap1
->n_div
+ bmap2
->n_div
,
10121 bmap1
->n_eq
+ bmap2
->n_eq
,
10122 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10123 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10124 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10126 bmap
= isl_basic_map_set_rational(bmap
);
10127 bmap
= isl_basic_map_simplify(bmap
);
10128 return isl_basic_map_finalize(bmap
);
10130 isl_basic_map_free(bmap1
);
10131 isl_basic_map_free(bmap2
);
10135 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10136 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10138 isl_basic_map
*prod
;
10140 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10141 prod
= isl_basic_map_flatten_range(prod
);
10145 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10146 * and collect the results.
10147 * The result live in the space obtained by calling "space_product"
10148 * on the spaces of "map1" and "map2".
10149 * If "remove_duplicates" is set then the result may contain duplicates
10150 * (even if the inputs do not) and so we try and remove the obvious
10153 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10154 __isl_take isl_map
*map2
,
10155 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10156 __isl_take isl_space
*right
),
10157 __isl_give isl_basic_map
*(*basic_map_product
)(
10158 __isl_take isl_basic_map
*left
,
10159 __isl_take isl_basic_map
*right
),
10160 int remove_duplicates
)
10162 unsigned flags
= 0;
10163 struct isl_map
*result
;
10167 m
= isl_map_has_equal_params(map1
, map2
);
10171 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10172 "parameters don't match", goto error
);
10174 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10175 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10176 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10178 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10179 isl_space_copy(map2
->dim
)),
10180 map1
->n
* map2
->n
, flags
);
10183 for (i
= 0; i
< map1
->n
; ++i
)
10184 for (j
= 0; j
< map2
->n
; ++j
) {
10185 struct isl_basic_map
*part
;
10186 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10187 isl_basic_map_copy(map2
->p
[j
]));
10188 if (isl_basic_map_is_empty(part
))
10189 isl_basic_map_free(part
);
10191 result
= isl_map_add_basic_map(result
, part
);
10195 if (remove_duplicates
)
10196 result
= isl_map_remove_obvious_duplicates(result
);
10197 isl_map_free(map1
);
10198 isl_map_free(map2
);
10201 isl_map_free(map1
);
10202 isl_map_free(map2
);
10206 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10208 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10209 __isl_take isl_map
*map2
)
10211 return map_product(map1
, map2
, &isl_space_product
,
10212 &isl_basic_map_product
, 0);
10215 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10216 __isl_take isl_map
*map2
)
10218 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10221 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10223 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10224 __isl_take isl_map
*map2
)
10228 prod
= isl_map_product(map1
, map2
);
10229 prod
= isl_map_flatten(prod
);
10233 /* Given two set A and B, construct its Cartesian product A x B.
10235 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10237 return isl_map_range_product(set1
, set2
);
10240 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10241 __isl_take isl_set
*set2
)
10243 return isl_map_flat_range_product(set1
, set2
);
10246 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10248 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10249 __isl_take isl_map
*map2
)
10251 return map_product(map1
, map2
, &isl_space_domain_product
,
10252 &isl_basic_map_domain_product
, 1);
10255 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10257 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10258 __isl_take isl_map
*map2
)
10260 return map_product(map1
, map2
, &isl_space_range_product
,
10261 &isl_basic_map_range_product
, 1);
10264 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10265 __isl_take isl_map
*map2
)
10267 return isl_map_align_params_map_map_and(map1
, map2
,
10268 &map_domain_product_aligned
);
10271 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10272 __isl_take isl_map
*map2
)
10274 return isl_map_align_params_map_map_and(map1
, map2
,
10275 &map_range_product_aligned
);
10278 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10280 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10283 isl_size total1
, keep1
, total2
, keep2
;
10285 total1
= isl_map_dim(map
, isl_dim_in
);
10286 total2
= isl_map_dim(map
, isl_dim_out
);
10287 if (total1
< 0 || total2
< 0)
10288 return isl_map_free(map
);
10289 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10290 !isl_space_range_is_wrapping(map
->dim
))
10291 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10292 "not a product", return isl_map_free(map
));
10294 space
= isl_map_get_space(map
);
10295 space
= isl_space_factor_domain(space
);
10296 keep1
= isl_space_dim(space
, isl_dim_in
);
10297 keep2
= isl_space_dim(space
, isl_dim_out
);
10298 if (keep1
< 0 || keep2
< 0)
10299 map
= isl_map_free(map
);
10300 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10301 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10302 map
= isl_map_reset_space(map
, space
);
10307 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10309 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10312 isl_size total1
, keep1
, total2
, keep2
;
10314 total1
= isl_map_dim(map
, isl_dim_in
);
10315 total2
= isl_map_dim(map
, isl_dim_out
);
10316 if (total1
< 0 || total2
< 0)
10317 return isl_map_free(map
);
10318 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10319 !isl_space_range_is_wrapping(map
->dim
))
10320 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10321 "not a product", return isl_map_free(map
));
10323 space
= isl_map_get_space(map
);
10324 space
= isl_space_factor_range(space
);
10325 keep1
= isl_space_dim(space
, isl_dim_in
);
10326 keep2
= isl_space_dim(space
, isl_dim_out
);
10327 if (keep1
< 0 || keep2
< 0)
10328 map
= isl_map_free(map
);
10329 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10330 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10331 map
= isl_map_reset_space(map
, space
);
10336 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10338 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10341 isl_size total
, keep
;
10343 total
= isl_map_dim(map
, isl_dim_in
);
10345 return isl_map_free(map
);
10346 if (!isl_space_domain_is_wrapping(map
->dim
))
10347 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10348 "domain is not a product", return isl_map_free(map
));
10350 space
= isl_map_get_space(map
);
10351 space
= isl_space_domain_factor_domain(space
);
10352 keep
= isl_space_dim(space
, isl_dim_in
);
10354 map
= isl_map_free(map
);
10355 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10356 map
= isl_map_reset_space(map
, space
);
10361 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10363 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10366 isl_size total
, keep
;
10368 total
= isl_map_dim(map
, isl_dim_in
);
10370 return isl_map_free(map
);
10371 if (!isl_space_domain_is_wrapping(map
->dim
))
10372 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10373 "domain is not a product", return isl_map_free(map
));
10375 space
= isl_map_get_space(map
);
10376 space
= isl_space_domain_factor_range(space
);
10377 keep
= isl_space_dim(space
, isl_dim_in
);
10379 map
= isl_map_free(map
);
10380 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10381 map
= isl_map_reset_space(map
, space
);
10386 /* Given a map A -> [B -> C], extract the map A -> B.
10388 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10391 isl_size total
, keep
;
10393 total
= isl_map_dim(map
, isl_dim_out
);
10395 return isl_map_free(map
);
10396 if (!isl_space_range_is_wrapping(map
->dim
))
10397 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10398 "range is not a product", return isl_map_free(map
));
10400 space
= isl_map_get_space(map
);
10401 space
= isl_space_range_factor_domain(space
);
10402 keep
= isl_space_dim(space
, isl_dim_out
);
10404 map
= isl_map_free(map
);
10405 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10406 map
= isl_map_reset_space(map
, space
);
10411 /* Given a map A -> [B -> C], extract the map A -> C.
10413 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10416 isl_size total
, keep
;
10418 total
= isl_map_dim(map
, isl_dim_out
);
10420 return isl_map_free(map
);
10421 if (!isl_space_range_is_wrapping(map
->dim
))
10422 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10423 "range is not a product", return isl_map_free(map
));
10425 space
= isl_map_get_space(map
);
10426 space
= isl_space_range_factor_range(space
);
10427 keep
= isl_space_dim(space
, isl_dim_out
);
10429 map
= isl_map_free(map
);
10430 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10431 map
= isl_map_reset_space(map
, space
);
10436 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10438 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10439 __isl_take isl_map
*map2
)
10443 prod
= isl_map_domain_product(map1
, map2
);
10444 prod
= isl_map_flatten_domain(prod
);
10448 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10450 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10451 __isl_take isl_map
*map2
)
10455 prod
= isl_map_range_product(map1
, map2
);
10456 prod
= isl_map_flatten_range(prod
);
10460 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10463 uint32_t hash
= isl_hash_init();
10468 bmap
= isl_basic_map_copy(bmap
);
10469 bmap
= isl_basic_map_normalize(bmap
);
10470 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10473 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10474 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10476 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10477 isl_hash_hash(hash
, c_hash
);
10479 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10480 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10482 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10483 isl_hash_hash(hash
, c_hash
);
10485 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10486 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10488 if (isl_int_is_zero(bmap
->div
[i
][0]))
10490 isl_hash_byte(hash
, i
& 0xFF);
10491 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10492 isl_hash_hash(hash
, c_hash
);
10494 isl_basic_map_free(bmap
);
10498 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10500 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10503 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10510 map
= isl_map_copy(map
);
10511 map
= isl_map_normalize(map
);
10515 hash
= isl_hash_init();
10516 for (i
= 0; i
< map
->n
; ++i
) {
10517 uint32_t bmap_hash
;
10518 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10519 isl_hash_hash(hash
, bmap_hash
);
10527 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10529 return isl_map_get_hash(set_to_map(set
));
10532 /* Return the number of basic maps in the (current) representation of "map".
10534 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10536 return map
? map
->n
: 0;
10539 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10541 return set
? set
->n
: 0;
10544 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10545 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10550 return isl_stat_error
;
10552 for (i
= 0; i
< map
->n
; ++i
)
10553 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10554 return isl_stat_error
;
10556 return isl_stat_ok
;
10559 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10560 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10565 return isl_stat_error
;
10567 for (i
= 0; i
< set
->n
; ++i
)
10568 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10569 return isl_stat_error
;
10571 return isl_stat_ok
;
10574 /* Return a list of basic sets, the union of which is equal to "set".
10576 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10577 __isl_keep isl_set
*set
)
10580 isl_basic_set_list
*list
;
10585 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10586 for (i
= 0; i
< set
->n
; ++i
) {
10587 isl_basic_set
*bset
;
10589 bset
= isl_basic_set_copy(set
->p
[i
]);
10590 list
= isl_basic_set_list_add(list
, bset
);
10596 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10603 bset
= isl_basic_set_cow(bset
);
10607 space
= isl_basic_set_get_space(bset
);
10608 space
= isl_space_lift(space
, bset
->n_div
);
10611 isl_space_free(bset
->dim
);
10613 bset
->extra
-= bset
->n_div
;
10616 bset
= isl_basic_set_finalize(bset
);
10620 isl_basic_set_free(bset
);
10624 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10630 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10635 set
= isl_set_cow(set
);
10639 n_div
= set
->p
[0]->n_div
;
10640 space
= isl_set_get_space(set
);
10641 space
= isl_space_lift(space
, n_div
);
10644 isl_space_free(set
->dim
);
10647 for (i
= 0; i
< set
->n
; ++i
) {
10648 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10659 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10664 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
10667 size
+= bset
->n_eq
* (1 + dim
);
10668 size
+= bset
->n_ineq
* (1 + dim
);
10669 size
+= bset
->n_div
* (2 + dim
);
10674 int isl_set_size(__isl_keep isl_set
*set
)
10682 for (i
= 0; i
< set
->n
; ++i
)
10683 size
+= isl_basic_set_size(set
->p
[i
]);
10688 /* Check if there is any lower bound (if lower == 0) and/or upper
10689 * bound (if upper == 0) on the specified dim.
10691 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10692 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10696 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10697 return isl_bool_error
;
10699 pos
+= isl_basic_map_offset(bmap
, type
);
10701 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10702 if (isl_int_is_zero(bmap
->div
[i
][0]))
10704 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10705 return isl_bool_true
;
10708 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10709 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10710 return isl_bool_true
;
10712 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10713 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10720 return lower
&& upper
;
10723 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10724 enum isl_dim_type type
, unsigned pos
)
10726 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10729 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10730 enum isl_dim_type type
, unsigned pos
)
10732 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10735 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10736 enum isl_dim_type type
, unsigned pos
)
10738 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10741 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10742 enum isl_dim_type type
, unsigned pos
)
10747 return isl_bool_error
;
10749 for (i
= 0; i
< map
->n
; ++i
) {
10751 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10752 if (bounded
< 0 || !bounded
)
10756 return isl_bool_true
;
10759 /* Return true if the specified dim is involved in both an upper bound
10760 * and a lower bound.
10762 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10763 enum isl_dim_type type
, unsigned pos
)
10765 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10768 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10770 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10771 enum isl_dim_type type
, unsigned pos
,
10772 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10773 enum isl_dim_type type
, unsigned pos
))
10778 return isl_bool_error
;
10780 for (i
= 0; i
< map
->n
; ++i
) {
10782 bounded
= fn(map
->p
[i
], type
, pos
);
10783 if (bounded
< 0 || bounded
)
10787 return isl_bool_false
;
10790 /* Return 1 if the specified dim is involved in any lower bound.
10792 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10793 enum isl_dim_type type
, unsigned pos
)
10795 return has_any_bound(set
, type
, pos
,
10796 &isl_basic_map_dim_has_lower_bound
);
10799 /* Return 1 if the specified dim is involved in any upper bound.
10801 isl_bool
isl_set_dim_has_any_upper_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_upper_bound
);
10808 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10810 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10811 enum isl_dim_type type
, unsigned pos
,
10812 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10813 enum isl_dim_type type
, unsigned pos
))
10818 return isl_bool_error
;
10820 for (i
= 0; i
< map
->n
; ++i
) {
10822 bounded
= fn(map
->p
[i
], type
, pos
);
10823 if (bounded
< 0 || !bounded
)
10827 return isl_bool_true
;
10830 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10832 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10833 enum isl_dim_type type
, unsigned pos
)
10835 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10838 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10840 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10841 enum isl_dim_type type
, unsigned pos
)
10843 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10846 /* For each of the "n" variables starting at "first", determine
10847 * the sign of the variable and put the results in the first "n"
10848 * elements of the array "signs".
10850 * 1 means that the variable is non-negative
10851 * -1 means that the variable is non-positive
10852 * 0 means the variable attains both positive and negative values.
10854 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10855 unsigned first
, unsigned n
, int *signs
)
10857 isl_vec
*bound
= NULL
;
10858 struct isl_tab
*tab
= NULL
;
10859 struct isl_tab_undo
*snap
;
10863 total
= isl_basic_set_dim(bset
, isl_dim_all
);
10864 if (total
< 0 || !signs
)
10865 return isl_stat_error
;
10867 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
10868 tab
= isl_tab_from_basic_set(bset
, 0);
10869 if (!bound
|| !tab
)
10872 isl_seq_clr(bound
->el
, bound
->size
);
10873 isl_int_set_si(bound
->el
[0], -1);
10875 snap
= isl_tab_snap(tab
);
10876 for (i
= 0; i
< n
; ++i
) {
10879 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10880 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10882 empty
= tab
->empty
;
10883 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10884 if (isl_tab_rollback(tab
, snap
) < 0)
10892 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10893 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10895 empty
= tab
->empty
;
10896 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10897 if (isl_tab_rollback(tab
, snap
) < 0)
10900 signs
[i
] = empty
? -1 : 0;
10904 isl_vec_free(bound
);
10905 return isl_stat_ok
;
10908 isl_vec_free(bound
);
10909 return isl_stat_error
;
10912 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10913 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10915 if (!bset
|| !signs
)
10916 return isl_stat_error
;
10917 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10918 return isl_stat_error
;
10920 first
+= pos(bset
->dim
, type
) - 1;
10921 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10924 /* Is it possible for the integer division "div" to depend (possibly
10925 * indirectly) on any output dimensions?
10927 * If the div is undefined, then we conservatively assume that it
10928 * may depend on them.
10929 * Otherwise, we check if it actually depends on them or on any integer
10930 * divisions that may depend on them.
10932 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10935 isl_size n_out
, n_div
;
10936 unsigned o_out
, o_div
;
10938 if (isl_int_is_zero(bmap
->div
[div
][0]))
10939 return isl_bool_true
;
10941 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10943 return isl_bool_error
;
10944 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10946 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10947 return isl_bool_true
;
10949 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10951 return isl_bool_error
;
10952 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10954 for (i
= 0; i
< n_div
; ++i
) {
10955 isl_bool may_involve
;
10957 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10959 may_involve
= div_may_involve_output(bmap
, i
);
10960 if (may_involve
< 0 || may_involve
)
10961 return may_involve
;
10964 return isl_bool_false
;
10967 /* Return the first integer division of "bmap" in the range
10968 * [first, first + n[ that may depend on any output dimensions and
10969 * that has a non-zero coefficient in "c" (where the first coefficient
10970 * in "c" corresponds to integer division "first").
10972 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10973 isl_int
*c
, int first
, int n
)
10980 for (k
= first
; k
< first
+ n
; ++k
) {
10981 isl_bool may_involve
;
10983 if (isl_int_is_zero(c
[k
]))
10985 may_involve
= div_may_involve_output(bmap
, k
);
10986 if (may_involve
< 0)
10995 /* Look for a pair of inequality constraints in "bmap" of the form
10997 * -l + i >= 0 or i >= l
10999 * n + l - i >= 0 or i <= l + n
11001 * with n < "m" and i the output dimension at position "pos".
11002 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11003 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11004 * and earlier output dimensions, as well as integer divisions that do
11005 * not involve any of the output dimensions.
11007 * Return the index of the first inequality constraint or bmap->n_ineq
11008 * if no such pair can be found.
11010 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11011 int pos
, isl_int m
)
11016 isl_size n_div
, n_out
;
11017 unsigned o_div
, o_out
;
11020 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11021 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11022 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11023 if (total
< 0 || n_out
< 0 || n_div
< 0)
11026 ctx
= isl_basic_map_get_ctx(bmap
);
11027 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11028 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11029 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11030 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11032 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11033 n_out
- (pos
+ 1)) != -1)
11035 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11038 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11039 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11042 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11043 bmap
->ineq
[j
] + 1, total
))
11047 if (j
>= bmap
->n_ineq
)
11049 isl_int_add(bmap
->ineq
[i
][0],
11050 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11051 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11052 isl_int_sub(bmap
->ineq
[i
][0],
11053 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11056 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11062 return bmap
->n_ineq
;
11065 /* Return the index of the equality of "bmap" that defines
11066 * the output dimension "pos" in terms of earlier dimensions.
11067 * The equality may also involve integer divisions, as long
11068 * as those integer divisions are defined in terms of
11069 * parameters or input dimensions.
11070 * In this case, *div is set to the number of integer divisions and
11071 * *ineq is set to the number of inequality constraints (provided
11072 * div and ineq are not NULL).
11074 * The equality may also involve a single integer division involving
11075 * the output dimensions (typically only output dimension "pos") as
11076 * long as the coefficient of output dimension "pos" is 1 or -1 and
11077 * there is a pair of constraints i >= l and i <= l + n, with i referring
11078 * to output dimension "pos", l an expression involving only earlier
11079 * dimensions and n smaller than the coefficient of the integer division
11080 * in the equality. In this case, the output dimension can be defined
11081 * in terms of a modulo expression that does not involve the integer division.
11082 * *div is then set to this single integer division and
11083 * *ineq is set to the index of constraint i >= l.
11085 * Return bmap->n_eq if there is no such equality.
11086 * Return -1 on error.
11088 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11089 int pos
, int *div
, int *ineq
)
11092 isl_size n_div
, n_out
;
11093 unsigned o_div
, o_out
;
11095 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11096 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11097 if (n_out
< 0 || n_div
< 0)
11100 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11101 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11104 *ineq
= bmap
->n_ineq
;
11107 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11108 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11110 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11111 n_out
- (pos
+ 1)) != -1)
11113 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11117 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11118 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11120 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11121 k
+ 1, n_div
- (k
+1)) < n_div
)
11123 l
= find_modulo_constraint_pair(bmap
, pos
,
11124 bmap
->eq
[j
][o_div
+ k
]);
11127 if (l
>= bmap
->n_ineq
)
11139 /* Check if the given basic map is obviously single-valued.
11140 * In particular, for each output dimension, check that there is
11141 * an equality that defines the output dimension in terms of
11142 * earlier dimensions.
11144 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11149 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11151 return isl_bool_error
;
11153 for (i
= 0; i
< n_out
; ++i
) {
11156 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11159 return isl_bool_error
;
11160 if (eq
>= bmap
->n_eq
)
11161 return isl_bool_false
;
11164 return isl_bool_true
;
11167 /* Check if the given basic map is single-valued.
11168 * We simply compute
11172 * and check if the result is a subset of the identity mapping.
11174 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11177 isl_basic_map
*test
;
11181 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11185 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11186 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11188 space
= isl_basic_map_get_space(bmap
);
11189 space
= isl_space_map_from_set(isl_space_range(space
));
11190 id
= isl_basic_map_identity(space
);
11192 sv
= isl_basic_map_is_subset(test
, id
);
11194 isl_basic_map_free(test
);
11195 isl_basic_map_free(id
);
11200 /* Check if the given map is obviously single-valued.
11202 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11205 return isl_bool_error
;
11207 return isl_bool_true
;
11209 return isl_bool_false
;
11211 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11214 /* Check if the given map is single-valued.
11215 * We simply compute
11219 * and check if the result is a subset of the identity mapping.
11221 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11228 sv
= isl_map_plain_is_single_valued(map
);
11232 test
= isl_map_reverse(isl_map_copy(map
));
11233 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11235 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11236 id
= isl_map_identity(dim
);
11238 sv
= isl_map_is_subset(test
, id
);
11240 isl_map_free(test
);
11246 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11250 map
= isl_map_copy(map
);
11251 map
= isl_map_reverse(map
);
11252 in
= isl_map_is_single_valued(map
);
11258 /* Check if the given map is obviously injective.
11260 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11264 map
= isl_map_copy(map
);
11265 map
= isl_map_reverse(map
);
11266 in
= isl_map_plain_is_single_valued(map
);
11272 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11276 sv
= isl_map_is_single_valued(map
);
11280 return isl_map_is_injective(map
);
11283 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11285 return isl_map_is_single_valued(set_to_map(set
));
11288 /* Does "map" only map elements to themselves?
11290 * If the domain and range spaces are different, then "map"
11291 * is considered not to be an identity relation, even if it is empty.
11292 * Otherwise, construct the maximal identity relation and
11293 * check whether "map" is a subset of this relation.
11295 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11299 isl_bool equal
, is_identity
;
11301 space
= isl_map_get_space(map
);
11302 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11303 isl_space_free(space
);
11304 if (equal
< 0 || !equal
)
11307 id
= isl_map_identity(isl_map_get_space(map
));
11308 is_identity
= isl_map_is_subset(map
, id
);
11311 return is_identity
;
11314 int isl_map_is_translation(__isl_keep isl_map
*map
)
11319 delta
= isl_map_deltas(isl_map_copy(map
));
11320 ok
= isl_set_is_singleton(delta
);
11321 isl_set_free(delta
);
11326 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11328 if (isl_seq_first_non_zero(p
, pos
) != -1)
11330 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11335 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11338 isl_size nvar
, n_div
;
11341 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11343 return isl_bool_error
;
11345 return isl_bool_false
;
11347 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11349 return isl_bool_error
;
11350 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11351 for (j
= 0; j
< nvar
; ++j
) {
11352 int lower
= 0, upper
= 0;
11353 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11354 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11356 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11357 return isl_bool_false
;
11360 if (i
< bset
->n_eq
)
11362 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11363 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11365 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11366 return isl_bool_false
;
11367 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11372 if (!lower
|| !upper
)
11373 return isl_bool_false
;
11376 return isl_bool_true
;
11379 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11382 return isl_bool_error
;
11384 return isl_bool_false
;
11386 return isl_basic_set_is_box(set
->p
[0]);
11389 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11392 return isl_bool_error
;
11394 return isl_space_is_wrapping(bset
->dim
);
11397 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11400 return isl_bool_error
;
11402 return isl_space_is_wrapping(set
->dim
);
11405 /* Modify the space of "map" through a call to "change".
11406 * If "can_change" is set (not NULL), then first call it to check
11407 * if the modification is allowed, printing the error message "cannot_change"
11410 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11411 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11412 const char *cannot_change
,
11413 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11421 ok
= can_change
? can_change(map
) : isl_bool_true
;
11423 return isl_map_free(map
);
11425 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11426 return isl_map_free(map
));
11428 space
= change(isl_map_get_space(map
));
11429 map
= isl_map_reset_space(map
, space
);
11434 /* Is the domain of "map" a wrapped relation?
11436 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11439 return isl_bool_error
;
11441 return isl_space_domain_is_wrapping(map
->dim
);
11444 /* Does "map" have a wrapped relation in both domain and range?
11446 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11448 return isl_space_is_product(isl_map_peek_space(map
));
11451 /* Is the range of "map" a wrapped relation?
11453 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11456 return isl_bool_error
;
11458 return isl_space_range_is_wrapping(map
->dim
);
11461 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11463 bmap
= isl_basic_map_cow(bmap
);
11467 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11471 bmap
= isl_basic_map_finalize(bmap
);
11473 return bset_from_bmap(bmap
);
11475 isl_basic_map_free(bmap
);
11479 /* Given a map A -> B, return the set (A -> B).
11481 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11483 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11486 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11488 bset
= isl_basic_set_cow(bset
);
11492 bset
->dim
= isl_space_unwrap(bset
->dim
);
11496 bset
= isl_basic_set_finalize(bset
);
11498 return bset_to_bmap(bset
);
11500 isl_basic_set_free(bset
);
11504 /* Given a set (A -> B), return the map A -> B.
11505 * Error out if "set" is not of the form (A -> B).
11507 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11509 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11510 "not a wrapping set", &isl_space_unwrap
);
11513 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11514 enum isl_dim_type type
)
11519 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11522 bmap
= isl_basic_map_cow(bmap
);
11526 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11530 bmap
= isl_basic_map_finalize(bmap
);
11534 isl_basic_map_free(bmap
);
11538 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11539 enum isl_dim_type type
)
11546 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11549 map
= isl_map_cow(map
);
11553 for (i
= 0; i
< map
->n
; ++i
) {
11554 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11558 map
->dim
= isl_space_reset(map
->dim
, type
);
11568 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11573 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11576 bmap
= isl_basic_map_cow(bmap
);
11580 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11584 bmap
= isl_basic_map_finalize(bmap
);
11588 isl_basic_map_free(bmap
);
11592 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11594 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11597 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11598 __isl_take isl_basic_map
*bmap
)
11603 if (!bmap
->dim
->nested
[0])
11606 bmap
= isl_basic_map_cow(bmap
);
11610 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11614 bmap
= isl_basic_map_finalize(bmap
);
11618 isl_basic_map_free(bmap
);
11622 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11623 __isl_take isl_basic_map
*bmap
)
11628 if (!bmap
->dim
->nested
[1])
11631 bmap
= isl_basic_map_cow(bmap
);
11635 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11639 bmap
= isl_basic_map_finalize(bmap
);
11643 isl_basic_map_free(bmap
);
11647 /* Remove any internal structure from the spaces of domain and range of "map".
11649 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11654 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11657 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11660 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11662 return set_from_map(isl_map_flatten(set_to_map(set
)));
11665 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11667 isl_space
*space
, *flat_space
;
11670 space
= isl_set_get_space(set
);
11671 flat_space
= isl_space_flatten(isl_space_copy(space
));
11672 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11674 map
= isl_map_intersect_domain(map
, set
);
11679 /* Remove any internal structure from the space of the domain of "map".
11681 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11686 if (!map
->dim
->nested
[0])
11689 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11692 /* Remove any internal structure from the space of the range of "map".
11694 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11699 if (!map
->dim
->nested
[1])
11702 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11705 /* Reorder the dimensions of "bmap" according to the given dim_map
11706 * and set the dimension specification to "space" and
11707 * perform Gaussian elimination on the result.
11709 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11710 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11712 isl_basic_map
*res
;
11716 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11717 if (n_div
< 0 || !space
|| !dim_map
)
11720 flags
= bmap
->flags
;
11721 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11722 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11723 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11724 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11725 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11727 res
->flags
= flags
;
11728 res
= isl_basic_map_gauss(res
, NULL
);
11729 res
= isl_basic_map_finalize(res
);
11732 isl_dim_map_free(dim_map
);
11733 isl_basic_map_free(bmap
);
11734 isl_space_free(space
);
11738 /* Reorder the dimensions of "map" according to given reordering.
11740 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11741 __isl_take isl_reordering
*r
)
11744 struct isl_dim_map
*dim_map
;
11746 map
= isl_map_cow(map
);
11747 dim_map
= isl_dim_map_from_reordering(r
);
11748 if (!map
|| !r
|| !dim_map
)
11751 for (i
= 0; i
< map
->n
; ++i
) {
11752 struct isl_dim_map
*dim_map_i
;
11755 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11757 space
= isl_reordering_get_space(r
);
11758 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11764 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11765 map
= isl_map_unmark_normalized(map
);
11767 isl_reordering_free(r
);
11768 isl_dim_map_free(dim_map
);
11771 isl_dim_map_free(dim_map
);
11773 isl_reordering_free(r
);
11777 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11778 __isl_take isl_reordering
*r
)
11780 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11783 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11784 __isl_take isl_space
*model
)
11789 if (!map
|| !model
)
11792 ctx
= isl_space_get_ctx(model
);
11793 if (!isl_space_has_named_params(model
))
11794 isl_die(ctx
, isl_error_invalid
,
11795 "model has unnamed parameters", goto error
);
11796 if (isl_map_check_named_params(map
) < 0)
11798 aligned
= isl_map_space_has_equal_params(map
, model
);
11802 isl_reordering
*exp
;
11804 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11805 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11806 map
= isl_map_realign(map
, exp
);
11809 isl_space_free(model
);
11812 isl_space_free(model
);
11817 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11818 __isl_take isl_space
*model
)
11820 return isl_map_align_params(set
, model
);
11823 /* Align the parameters of "bmap" to those of "model", introducing
11824 * additional parameters if needed.
11826 __isl_give isl_basic_map
*isl_basic_map_align_params(
11827 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11830 isl_bool equal_params
;
11832 if (!bmap
|| !model
)
11835 ctx
= isl_space_get_ctx(model
);
11836 if (!isl_space_has_named_params(model
))
11837 isl_die(ctx
, isl_error_invalid
,
11838 "model has unnamed parameters", goto error
);
11839 if (isl_basic_map_check_named_params(bmap
) < 0)
11841 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11842 if (equal_params
< 0)
11844 if (!equal_params
) {
11845 isl_reordering
*exp
;
11846 struct isl_dim_map
*dim_map
;
11848 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11849 exp
= isl_reordering_extend_space(exp
,
11850 isl_basic_map_get_space(bmap
));
11851 dim_map
= isl_dim_map_from_reordering(exp
);
11852 bmap
= isl_basic_map_realign(bmap
,
11853 isl_reordering_get_space(exp
),
11854 isl_dim_map_extend(dim_map
, bmap
));
11855 isl_reordering_free(exp
);
11856 isl_dim_map_free(dim_map
);
11859 isl_space_free(model
);
11862 isl_space_free(model
);
11863 isl_basic_map_free(bmap
);
11867 /* Do "bset" and "space" have the same parameters?
11869 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11870 __isl_keep isl_space
*space
)
11872 isl_space
*bset_space
;
11874 bset_space
= isl_basic_set_peek_space(bset
);
11875 return isl_space_has_equal_params(bset_space
, space
);
11878 /* Do "map" and "space" have the same parameters?
11880 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11881 __isl_keep isl_space
*space
)
11883 isl_space
*map_space
;
11885 map_space
= isl_map_peek_space(map
);
11886 return isl_space_has_equal_params(map_space
, space
);
11889 /* Do "set" and "space" have the same parameters?
11891 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11892 __isl_keep isl_space
*space
)
11894 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11897 /* Align the parameters of "bset" to those of "model", introducing
11898 * additional parameters if needed.
11900 __isl_give isl_basic_set
*isl_basic_set_align_params(
11901 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11903 return isl_basic_map_align_params(bset
, model
);
11906 /* Drop all parameters not referenced by "map".
11908 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11913 n
= isl_map_dim(map
, isl_dim_param
);
11914 if (isl_map_check_named_params(map
) < 0 || n
< 0)
11915 return isl_map_free(map
);
11917 for (i
= n
- 1; i
>= 0; i
--) {
11920 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11922 return isl_map_free(map
);
11924 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11930 /* Drop all parameters not referenced by "set".
11932 __isl_give isl_set
*isl_set_drop_unused_params(
11933 __isl_take isl_set
*set
)
11935 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11938 /* Drop all parameters not referenced by "bmap".
11940 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11941 __isl_take isl_basic_map
*bmap
)
11946 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
11947 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
11948 return isl_basic_map_free(bmap
);
11950 for (i
= nparam
- 1; i
>= 0; i
--) {
11953 involves
= isl_basic_map_involves_dims(bmap
,
11954 isl_dim_param
, i
, 1);
11956 return isl_basic_map_free(bmap
);
11958 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11964 /* Drop all parameters not referenced by "bset".
11966 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11967 __isl_take isl_basic_set
*bset
)
11969 return bset_from_bmap(isl_basic_map_drop_unused_params(
11970 bset_to_bmap(bset
)));
11973 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11974 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11975 enum isl_dim_type c2
, enum isl_dim_type c3
,
11976 enum isl_dim_type c4
, enum isl_dim_type c5
)
11978 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11979 struct isl_mat
*mat
;
11984 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11987 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
11990 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11991 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11992 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11993 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
11995 return isl_mat_free(mat
);
11996 for (k
= 0; k
< dim
; ++k
) {
11997 isl_int_set(mat
->row
[i
][pos
],
11998 bmap
->eq
[i
][off
+ k
]);
12006 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12007 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12008 enum isl_dim_type c2
, enum isl_dim_type c3
,
12009 enum isl_dim_type c4
, enum isl_dim_type c5
)
12011 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12012 struct isl_mat
*mat
;
12017 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12020 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12023 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12024 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12025 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12026 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12028 return isl_mat_free(mat
);
12029 for (k
= 0; k
< dim
; ++k
) {
12030 isl_int_set(mat
->row
[i
][pos
],
12031 bmap
->ineq
[i
][off
+ k
]);
12039 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12040 __isl_take isl_space
*space
,
12041 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12042 enum isl_dim_type c2
, enum isl_dim_type c3
,
12043 enum isl_dim_type c4
, enum isl_dim_type c5
)
12045 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12046 isl_basic_map
*bmap
= NULL
;
12053 dim
= isl_space_dim(space
, isl_dim_all
);
12054 if (dim
< 0 || !eq
|| !ineq
)
12057 if (eq
->n_col
!= ineq
->n_col
)
12058 isl_die(space
->ctx
, isl_error_invalid
,
12059 "equalities and inequalities matrices should have "
12060 "same number of columns", goto error
);
12064 if (eq
->n_col
< total
)
12065 isl_die(space
->ctx
, isl_error_invalid
,
12066 "number of columns too small", goto error
);
12068 extra
= eq
->n_col
- total
;
12070 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12071 eq
->n_row
, ineq
->n_row
);
12074 for (i
= 0; i
< extra
; ++i
) {
12075 k
= isl_basic_map_alloc_div(bmap
);
12078 isl_int_set_si(bmap
->div
[k
][0], 0);
12080 for (i
= 0; i
< eq
->n_row
; ++i
) {
12081 l
= isl_basic_map_alloc_equality(bmap
);
12084 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12085 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12086 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12089 for (k
= 0; k
< dim
; ++k
) {
12090 isl_int_set(bmap
->eq
[l
][off
+ k
],
12096 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12097 l
= isl_basic_map_alloc_inequality(bmap
);
12100 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12101 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12102 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12105 for (k
= 0; k
< dim
; ++k
) {
12106 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12107 ineq
->row
[i
][pos
]);
12113 isl_space_free(space
);
12115 isl_mat_free(ineq
);
12117 bmap
= isl_basic_map_simplify(bmap
);
12118 return isl_basic_map_finalize(bmap
);
12120 isl_space_free(space
);
12122 isl_mat_free(ineq
);
12123 isl_basic_map_free(bmap
);
12127 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12128 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12129 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12131 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12132 c1
, c2
, c3
, c4
, isl_dim_in
);
12135 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12136 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12137 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12139 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12140 c1
, c2
, c3
, c4
, isl_dim_in
);
12143 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12144 __isl_take isl_space
*dim
,
12145 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12146 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12148 isl_basic_map
*bmap
;
12149 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12150 c1
, c2
, c3
, c4
, isl_dim_in
);
12151 return bset_from_bmap(bmap
);
12154 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12157 return isl_bool_error
;
12159 return isl_space_can_zip(bmap
->dim
);
12162 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12165 return isl_bool_error
;
12167 return isl_space_can_zip(map
->dim
);
12170 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12171 * (A -> C) -> (B -> D).
12173 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12183 if (!isl_basic_map_can_zip(bmap
))
12184 isl_die(bmap
->ctx
, isl_error_invalid
,
12185 "basic map cannot be zipped", goto error
);
12186 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12187 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12188 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12189 if (n_in
< 0 || n1
< 0 || n2
< 0)
12190 return isl_basic_map_free(bmap
);
12191 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12192 bmap
= isl_basic_map_cow(bmap
);
12193 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12196 bmap
->dim
= isl_space_zip(bmap
->dim
);
12199 bmap
= isl_basic_map_mark_final(bmap
);
12202 isl_basic_map_free(bmap
);
12206 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12207 * (A -> C) -> (B -> D).
12209 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12216 if (!isl_map_can_zip(map
))
12217 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12220 map
= isl_map_cow(map
);
12224 for (i
= 0; i
< map
->n
; ++i
) {
12225 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12230 map
->dim
= isl_space_zip(map
->dim
);
12240 /* Can we apply isl_basic_map_curry to "bmap"?
12241 * That is, does it have a nested relation in its domain?
12243 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12246 return isl_bool_error
;
12248 return isl_space_can_curry(bmap
->dim
);
12251 /* Can we apply isl_map_curry to "map"?
12252 * That is, does it have a nested relation in its domain?
12254 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12257 return isl_bool_error
;
12259 return isl_space_can_curry(map
->dim
);
12262 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12265 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12271 if (!isl_basic_map_can_curry(bmap
))
12272 isl_die(bmap
->ctx
, isl_error_invalid
,
12273 "basic map cannot be curried", goto error
);
12274 bmap
= isl_basic_map_cow(bmap
);
12277 bmap
->dim
= isl_space_curry(bmap
->dim
);
12280 bmap
= isl_basic_map_mark_final(bmap
);
12283 isl_basic_map_free(bmap
);
12287 /* Given a map (A -> B) -> C, return the corresponding map
12290 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12292 return isl_map_change_space(map
, &isl_map_can_curry
,
12293 "map cannot be curried", &isl_space_curry
);
12296 /* Can isl_map_range_curry be applied to "map"?
12297 * That is, does it have a nested relation in its range,
12298 * the domain of which is itself a nested relation?
12300 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12303 return isl_bool_error
;
12305 return isl_space_can_range_curry(map
->dim
);
12308 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12309 * A -> (B -> (C -> D)).
12311 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12313 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12314 "map range cannot be curried",
12315 &isl_space_range_curry
);
12318 /* Can we apply isl_basic_map_uncurry to "bmap"?
12319 * That is, does it have a nested relation in its domain?
12321 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12324 return isl_bool_error
;
12326 return isl_space_can_uncurry(bmap
->dim
);
12329 /* Can we apply isl_map_uncurry to "map"?
12330 * That is, does it have a nested relation in its domain?
12332 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12335 return isl_bool_error
;
12337 return isl_space_can_uncurry(map
->dim
);
12340 /* Given a basic map A -> (B -> C), return the corresponding basic map
12343 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12349 if (!isl_basic_map_can_uncurry(bmap
))
12350 isl_die(bmap
->ctx
, isl_error_invalid
,
12351 "basic map cannot be uncurried",
12352 return isl_basic_map_free(bmap
));
12353 bmap
= isl_basic_map_cow(bmap
);
12356 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12358 return isl_basic_map_free(bmap
);
12359 bmap
= isl_basic_map_mark_final(bmap
);
12363 /* Given a map A -> (B -> C), return the corresponding map
12366 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12368 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12369 "map cannot be uncurried", &isl_space_uncurry
);
12372 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12373 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12375 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12378 /* Construct a basic map where the given dimensions are equal to each other.
12380 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12381 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12383 isl_basic_map
*bmap
= NULL
;
12387 total
= isl_space_dim(space
, isl_dim_all
);
12389 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12390 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12393 if (type1
== type2
&& pos1
== pos2
)
12394 return isl_basic_map_universe(space
);
12396 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12397 i
= isl_basic_map_alloc_equality(bmap
);
12400 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12401 pos1
+= isl_basic_map_offset(bmap
, type1
);
12402 pos2
+= isl_basic_map_offset(bmap
, type2
);
12403 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12404 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12405 bmap
= isl_basic_map_finalize(bmap
);
12406 isl_space_free(space
);
12409 isl_space_free(space
);
12410 isl_basic_map_free(bmap
);
12414 /* Add a constraint imposing that the given two dimensions are equal.
12416 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12417 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12421 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12423 bmap
= isl_basic_map_intersect(bmap
, eq
);
12428 /* Add a constraint imposing that the given two dimensions are equal.
12430 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12431 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12433 isl_basic_map
*bmap
;
12435 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12437 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12442 /* Add a constraint imposing that the given two dimensions have opposite values.
12444 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12445 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12447 isl_basic_map
*bmap
= NULL
;
12451 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12452 return isl_map_free(map
);
12453 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12454 return isl_map_free(map
);
12456 total
= isl_map_dim(map
, isl_dim_all
);
12458 return isl_map_free(map
);
12459 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12460 i
= isl_basic_map_alloc_equality(bmap
);
12463 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12464 pos1
+= isl_basic_map_offset(bmap
, type1
);
12465 pos2
+= isl_basic_map_offset(bmap
, type2
);
12466 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12467 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12468 bmap
= isl_basic_map_finalize(bmap
);
12470 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12474 isl_basic_map_free(bmap
);
12479 /* Construct a constraint imposing that the value of the first dimension is
12480 * greater than or equal to that of the second.
12482 static __isl_give isl_constraint
*constraint_order_ge(
12483 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12484 enum isl_dim_type type2
, int pos2
)
12488 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12489 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12490 space
= isl_space_free(space
);
12494 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12496 if (type1
== type2
&& pos1
== pos2
)
12499 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12500 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12505 /* Add a constraint imposing that the value of the first dimension is
12506 * greater than or equal to that of the second.
12508 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12509 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12514 if (type1
== type2
&& pos1
== pos2
)
12516 space
= isl_basic_map_get_space(bmap
);
12517 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12518 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12523 /* Add a constraint imposing that the value of the first dimension is
12524 * greater than or equal to that of the second.
12526 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12527 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12532 if (type1
== type2
&& pos1
== pos2
)
12534 space
= isl_map_get_space(map
);
12535 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12536 map
= isl_map_add_constraint(map
, c
);
12541 /* Add a constraint imposing that the value of the first dimension is
12542 * less than or equal to that of the second.
12544 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12545 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12547 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12550 /* Construct a basic map where the value of the first dimension is
12551 * greater than that of the second.
12553 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12554 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12556 isl_basic_map
*bmap
= NULL
;
12560 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12561 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12564 if (type1
== type2
&& pos1
== pos2
)
12565 return isl_basic_map_empty(space
);
12567 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12568 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12569 i
= isl_basic_map_alloc_inequality(bmap
);
12570 if (total
< 0 || i
< 0)
12571 return isl_basic_map_free(bmap
);
12572 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
12573 pos1
+= isl_basic_map_offset(bmap
, type1
);
12574 pos2
+= isl_basic_map_offset(bmap
, type2
);
12575 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12576 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12577 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12578 bmap
= isl_basic_map_finalize(bmap
);
12582 isl_space_free(space
);
12583 isl_basic_map_free(bmap
);
12587 /* Add a constraint imposing that the value of the first dimension is
12588 * greater than that of the second.
12590 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12591 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12595 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12597 bmap
= isl_basic_map_intersect(bmap
, gt
);
12602 /* Add a constraint imposing that the value of the first dimension is
12603 * greater than that of the second.
12605 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12606 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12608 isl_basic_map
*bmap
;
12610 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12612 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12617 /* Add a constraint imposing that the value of the first dimension is
12618 * smaller than that of the second.
12620 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12621 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12623 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12626 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12630 isl_local_space
*ls
;
12635 if (!isl_basic_map_divs_known(bmap
))
12636 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12637 "some divs are unknown", return NULL
);
12639 ls
= isl_basic_map_get_local_space(bmap
);
12640 div
= isl_local_space_get_div(ls
, pos
);
12641 isl_local_space_free(ls
);
12646 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12649 return isl_basic_map_get_div(bset
, pos
);
12652 /* Plug in "subs" for dimension "type", "pos" of "bset".
12654 * Let i be the dimension to replace and let "subs" be of the form
12658 * Any integer division with a non-zero coefficient for i,
12660 * floor((a i + g)/m)
12664 * floor((a f + d g)/(m d))
12666 * Constraints of the form
12674 * We currently require that "subs" is an integral expression.
12675 * Handling rational expressions may require us to add stride constraints
12676 * as we do in isl_basic_set_preimage_multi_aff.
12678 __isl_give isl_basic_set
*isl_basic_set_substitute(
12679 __isl_take isl_basic_set
*bset
,
12680 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12687 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12690 bset
= isl_basic_set_cow(bset
);
12691 if (!bset
|| !subs
)
12694 ctx
= isl_basic_set_get_ctx(bset
);
12695 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12696 isl_die(ctx
, isl_error_invalid
,
12697 "spaces don't match", goto error
);
12698 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
12702 isl_die(ctx
, isl_error_unsupported
,
12703 "cannot handle divs yet", goto error
);
12704 if (!isl_int_is_one(subs
->v
->el
[0]))
12705 isl_die(ctx
, isl_error_invalid
,
12706 "can only substitute integer expressions", goto error
);
12708 pos
+= isl_basic_set_offset(bset
, type
);
12712 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12713 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12715 isl_int_set(v
, bset
->eq
[i
][pos
]);
12716 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12717 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12718 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12721 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12722 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12724 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12725 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12726 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12727 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12730 for (i
= 0; i
< bset
->n_div
; ++i
) {
12731 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12733 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12734 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12735 isl_seq_combine(bset
->div
[i
] + 1,
12736 subs
->v
->el
[0], bset
->div
[i
] + 1,
12737 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12738 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12743 bset
= isl_basic_set_simplify(bset
);
12744 return isl_basic_set_finalize(bset
);
12746 isl_basic_set_free(bset
);
12750 /* Plug in "subs" for dimension "type", "pos" of "set".
12752 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12753 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12757 if (set
&& isl_set_plain_is_empty(set
))
12760 set
= isl_set_cow(set
);
12764 for (i
= set
->n
- 1; i
>= 0; --i
) {
12765 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12766 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12777 /* Check if the range of "ma" is compatible with the domain or range
12778 * (depending on "type") of "bmap".
12780 static isl_stat
check_basic_map_compatible_range_multi_aff(
12781 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12782 __isl_keep isl_multi_aff
*ma
)
12785 isl_space
*ma_space
;
12787 ma_space
= isl_multi_aff_get_space(ma
);
12789 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12793 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12794 "parameters don't match", goto error
);
12795 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12799 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12800 "spaces don't match", goto error
);
12802 isl_space_free(ma_space
);
12803 return isl_stat_ok
;
12805 isl_space_free(ma_space
);
12806 return isl_stat_error
;
12809 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12810 * coefficients before the transformed range of dimensions,
12811 * the "n_after" coefficients after the transformed range of dimensions
12812 * and the coefficients of the other divs in "bmap".
12814 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12815 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12820 isl_local_space
*ls
;
12825 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12826 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12827 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12828 if (n_param
< 0 || n_set
< 0)
12829 return isl_basic_map_free(bmap
);
12831 for (i
= 0; i
< n_div
; ++i
) {
12832 int o_bmap
= 0, o_ls
= 0;
12834 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12835 o_bmap
+= 1 + 1 + n_param
;
12836 o_ls
+= 1 + 1 + n_param
;
12837 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12838 o_bmap
+= n_before
;
12839 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12840 ls
->div
->row
[i
] + o_ls
, n_set
);
12843 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12845 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12846 ls
->div
->row
[i
] + o_ls
, n_div
);
12849 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12850 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12855 isl_local_space_free(ls
);
12858 isl_local_space_free(ls
);
12859 return isl_basic_map_free(bmap
);
12862 /* How many stride constraints does "ma" enforce?
12863 * That is, how many of the affine expressions have a denominator
12864 * different from one?
12866 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12871 for (i
= 0; i
< ma
->n
; ++i
)
12872 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12878 /* For each affine expression in ma of the form
12880 * x_i = (f_i y + h_i)/m_i
12882 * with m_i different from one, add a constraint to "bmap"
12885 * f_i y + h_i = m_i alpha_i
12887 * with alpha_i an additional existentially quantified variable.
12889 * The input variables of "ma" correspond to a subset of the variables
12890 * of "bmap". There are "n_before" variables in "bmap" before this
12891 * subset and "n_after" variables after this subset.
12892 * The integer divisions of the affine expressions in "ma" are assumed
12893 * to have been aligned. There are "n_div_ma" of them and
12894 * they appear first in "bmap", straight after the "n_after" variables.
12896 static __isl_give isl_basic_map
*add_ma_strides(
12897 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12898 int n_before
, int n_after
, int n_div_ma
)
12906 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12907 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12908 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12909 if (total
< 0 || n_param
< 0 || n_in
< 0)
12910 return isl_basic_map_free(bmap
);
12911 for (i
= 0; i
< ma
->n
; ++i
) {
12912 int o_bmap
= 0, o_ma
= 1;
12914 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12916 div
= isl_basic_map_alloc_div(bmap
);
12917 k
= isl_basic_map_alloc_equality(bmap
);
12918 if (div
< 0 || k
< 0)
12920 isl_int_set_si(bmap
->div
[div
][0], 0);
12921 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12922 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12923 o_bmap
+= 1 + n_param
;
12924 o_ma
+= 1 + n_param
;
12925 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12926 o_bmap
+= n_before
;
12927 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12928 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12931 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12933 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12934 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12935 o_bmap
+= n_div_ma
;
12937 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12938 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12944 isl_basic_map_free(bmap
);
12948 /* Replace the domain or range space (depending on "type) of "space" by "set".
12950 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12951 enum isl_dim_type type
, __isl_take isl_space
*set
)
12953 if (type
== isl_dim_in
) {
12954 space
= isl_space_range(space
);
12955 space
= isl_space_map_from_domain_and_range(set
, space
);
12957 space
= isl_space_domain(space
);
12958 space
= isl_space_map_from_domain_and_range(space
, set
);
12964 /* Compute the preimage of the domain or range (depending on "type")
12965 * of "bmap" under the function represented by "ma".
12966 * In other words, plug in "ma" in the domain or range of "bmap".
12967 * The result is a basic map that lives in the same space as "bmap"
12968 * except that the domain or range has been replaced by
12969 * the domain space of "ma".
12971 * If bmap is represented by
12973 * A(p) + S u + B x + T v + C(divs) >= 0,
12975 * where u and x are input and output dimensions if type == isl_dim_out
12976 * while x and v are input and output dimensions if type == isl_dim_in,
12977 * and ma is represented by
12979 * x = D(p) + F(y) + G(divs')
12981 * then the result is
12983 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12985 * The divs in the input set are similarly adjusted.
12988 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12992 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12993 * B_i G(divs') + c_i(divs))/n_i)
12995 * If bmap is not a rational map and if F(y) involves any denominators
12997 * x_i = (f_i y + h_i)/m_i
12999 * then additional constraints are added to ensure that we only
13000 * map back integer points. That is we enforce
13002 * f_i y + h_i = m_i alpha_i
13004 * with alpha_i an additional existentially quantified variable.
13006 * We first copy over the divs from "ma".
13007 * Then we add the modified constraints and divs from "bmap".
13008 * Finally, we add the stride constraints, if needed.
13010 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13011 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13012 __isl_take isl_multi_aff
*ma
)
13016 isl_basic_map
*res
= NULL
;
13017 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13018 isl_int f
, c1
, c2
, g
;
13027 ma
= isl_multi_aff_align_divs(ma
);
13030 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13033 if (type
== isl_dim_in
) {
13035 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13037 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13040 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13041 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13042 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13045 space
= isl_multi_aff_get_domain_space(ma
);
13046 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13047 rational
= isl_basic_map_is_rational(bmap
);
13048 strides
= rational
? 0 : multi_aff_strides(ma
);
13049 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13050 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13052 res
= isl_basic_map_set_rational(res
);
13054 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13055 if (isl_basic_map_alloc_div(res
) < 0)
13058 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13062 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13063 k
= isl_basic_map_alloc_equality(res
);
13066 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13067 n_after
, n_div_ma
, n_div_bmap
,
13068 f
, c1
, c2
, g
, 0) < 0)
13072 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13073 k
= isl_basic_map_alloc_inequality(res
);
13076 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13077 n_after
, n_div_ma
, n_div_bmap
,
13078 f
, c1
, c2
, g
, 0) < 0)
13082 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13083 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13084 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13087 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13088 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13089 f
, c1
, c2
, g
, 1) < 0)
13094 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13100 isl_basic_map_free(bmap
);
13101 isl_multi_aff_free(ma
);
13102 res
= isl_basic_map_simplify(res
);
13103 return isl_basic_map_finalize(res
);
13109 isl_basic_map_free(bmap
);
13110 isl_multi_aff_free(ma
);
13111 isl_basic_map_free(res
);
13115 /* Compute the preimage of "bset" under the function represented by "ma".
13116 * In other words, plug in "ma" in "bset". The result is a basic set
13117 * that lives in the domain space of "ma".
13119 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13120 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13122 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13125 /* Compute the preimage of the domain of "bmap" under the function
13126 * represented by "ma".
13127 * In other words, plug in "ma" in the domain of "bmap".
13128 * The result is a basic map that lives in the same space as "bmap"
13129 * except that the domain has been replaced by the domain space of "ma".
13131 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13132 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13134 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13137 /* Compute the preimage of the range of "bmap" under the function
13138 * represented by "ma".
13139 * In other words, plug in "ma" in the range of "bmap".
13140 * The result is a basic map that lives in the same space as "bmap"
13141 * except that the range has been replaced by the domain space of "ma".
13143 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13144 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13146 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13149 /* Check if the range of "ma" is compatible with the domain or range
13150 * (depending on "type") of "map".
13151 * Return isl_stat_error if anything is wrong.
13153 static isl_stat
check_map_compatible_range_multi_aff(
13154 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13155 __isl_keep isl_multi_aff
*ma
)
13158 isl_space
*ma_space
;
13160 ma_space
= isl_multi_aff_get_space(ma
);
13161 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13162 isl_space_free(ma_space
);
13164 return isl_stat_error
;
13166 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13167 "spaces don't match", return isl_stat_error
);
13168 return isl_stat_ok
;
13171 /* Compute the preimage of the domain or range (depending on "type")
13172 * of "map" under the function represented by "ma".
13173 * In other words, plug in "ma" in the domain or range of "map".
13174 * The result is a map that lives in the same space as "map"
13175 * except that the domain or range has been replaced by
13176 * the domain space of "ma".
13178 * The parameters are assumed to have been aligned.
13180 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13181 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13186 map
= isl_map_cow(map
);
13187 ma
= isl_multi_aff_align_divs(ma
);
13190 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13193 for (i
= 0; i
< map
->n
; ++i
) {
13194 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13195 isl_multi_aff_copy(ma
));
13200 space
= isl_multi_aff_get_domain_space(ma
);
13201 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13203 isl_space_free(map
->dim
);
13208 isl_multi_aff_free(ma
);
13210 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13211 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13214 isl_multi_aff_free(ma
);
13219 /* Compute the preimage of the domain or range (depending on "type")
13220 * of "map" under the function represented by "ma".
13221 * In other words, plug in "ma" in the domain or range of "map".
13222 * The result is a map that lives in the same space as "map"
13223 * except that the domain or range has been replaced by
13224 * the domain space of "ma".
13226 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13227 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13234 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13238 return map_preimage_multi_aff(map
, type
, ma
);
13240 if (isl_map_check_named_params(map
) < 0)
13242 if (!isl_space_has_named_params(ma
->space
))
13243 isl_die(map
->ctx
, isl_error_invalid
,
13244 "unaligned unnamed parameters", goto error
);
13245 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13246 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13248 return map_preimage_multi_aff(map
, type
, ma
);
13250 isl_multi_aff_free(ma
);
13251 return isl_map_free(map
);
13254 /* Compute the preimage of "set" under the function represented by "ma".
13255 * In other words, plug in "ma" in "set". The result is a set
13256 * that lives in the domain space of "ma".
13258 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13259 __isl_take isl_multi_aff
*ma
)
13261 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13264 /* Compute the preimage of the domain of "map" under the function
13265 * represented by "ma".
13266 * In other words, plug in "ma" in the domain of "map".
13267 * The result is a map that lives in the same space as "map"
13268 * except that the domain has been replaced by the domain space of "ma".
13270 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13271 __isl_take isl_multi_aff
*ma
)
13273 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13276 /* Compute the preimage of the range of "map" under the function
13277 * represented by "ma".
13278 * In other words, plug in "ma" in the range of "map".
13279 * The result is a map that lives in the same space as "map"
13280 * except that the range has been replaced by the domain space of "ma".
13282 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13283 __isl_take isl_multi_aff
*ma
)
13285 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13288 /* Compute the preimage of "map" under the function represented by "pma".
13289 * In other words, plug in "pma" in the domain or range of "map".
13290 * The result is a map that lives in the same space as "map",
13291 * except that the space of type "type" has been replaced by
13292 * the domain space of "pma".
13294 * The parameters of "map" and "pma" are assumed to have been aligned.
13296 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13297 __isl_take isl_map
*map
, enum isl_dim_type type
,
13298 __isl_take isl_pw_multi_aff
*pma
)
13307 isl_pw_multi_aff_free(pma
);
13308 res
= isl_map_empty(isl_map_get_space(map
));
13313 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13314 isl_multi_aff_copy(pma
->p
[0].maff
));
13315 if (type
== isl_dim_in
)
13316 res
= isl_map_intersect_domain(res
,
13317 isl_map_copy(pma
->p
[0].set
));
13319 res
= isl_map_intersect_range(res
,
13320 isl_map_copy(pma
->p
[0].set
));
13322 for (i
= 1; i
< pma
->n
; ++i
) {
13325 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13326 isl_multi_aff_copy(pma
->p
[i
].maff
));
13327 if (type
== isl_dim_in
)
13328 res_i
= isl_map_intersect_domain(res_i
,
13329 isl_map_copy(pma
->p
[i
].set
));
13331 res_i
= isl_map_intersect_range(res_i
,
13332 isl_map_copy(pma
->p
[i
].set
));
13333 res
= isl_map_union(res
, res_i
);
13336 isl_pw_multi_aff_free(pma
);
13340 isl_pw_multi_aff_free(pma
);
13345 /* Compute the preimage of "map" under the function represented by "pma".
13346 * In other words, plug in "pma" in the domain or range of "map".
13347 * The result is a map that lives in the same space as "map",
13348 * except that the space of type "type" has been replaced by
13349 * the domain space of "pma".
13351 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13352 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13359 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13363 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13365 if (isl_map_check_named_params(map
) < 0)
13367 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13369 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13370 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13372 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13374 isl_pw_multi_aff_free(pma
);
13375 return isl_map_free(map
);
13378 /* Compute the preimage of "set" under the function represented by "pma".
13379 * In other words, plug in "pma" in "set". The result is a set
13380 * that lives in the domain space of "pma".
13382 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13383 __isl_take isl_pw_multi_aff
*pma
)
13385 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13388 /* Compute the preimage of the domain of "map" under the function
13389 * represented by "pma".
13390 * In other words, plug in "pma" in the domain of "map".
13391 * The result is a map that lives in the same space as "map",
13392 * except that domain space has been replaced by the domain space of "pma".
13394 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13395 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13397 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13400 /* Compute the preimage of the range of "map" under the function
13401 * represented by "pma".
13402 * In other words, plug in "pma" in the range of "map".
13403 * The result is a map that lives in the same space as "map",
13404 * except that range space has been replaced by the domain space of "pma".
13406 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13407 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13409 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13412 /* Compute the preimage of "map" under the function represented by "mpa".
13413 * In other words, plug in "mpa" in the domain or range of "map".
13414 * The result is a map that lives in the same space as "map",
13415 * except that the space of type "type" has been replaced by
13416 * the domain space of "mpa".
13418 * If the map does not involve any constraints that refer to the
13419 * dimensions of the substituted space, then the only possible
13420 * effect of "mpa" on the map is to map the space to a different space.
13421 * We create a separate isl_multi_aff to effectuate this change
13422 * in order to avoid spurious splitting of the map along the pieces
13424 * If "mpa" has a non-trivial explicit domain, however,
13425 * then the full substitution should be performed.
13427 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13428 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13432 isl_pw_multi_aff
*pma
;
13434 n
= isl_map_dim(map
, type
);
13438 full
= isl_map_involves_dims(map
, type
, 0, n
);
13439 if (full
>= 0 && !full
)
13440 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13447 space
= isl_multi_pw_aff_get_space(mpa
);
13448 isl_multi_pw_aff_free(mpa
);
13449 ma
= isl_multi_aff_zero(space
);
13450 return isl_map_preimage_multi_aff(map
, type
, ma
);
13453 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13454 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13457 isl_multi_pw_aff_free(mpa
);
13461 /* Compute the preimage of "map" under the function represented by "mpa".
13462 * In other words, plug in "mpa" in the domain "map".
13463 * The result is a map that lives in the same space as "map",
13464 * except that domain space has been replaced by the domain space of "mpa".
13466 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13467 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13469 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13472 /* Compute the preimage of "set" by the function represented by "mpa".
13473 * In other words, plug in "mpa" in "set".
13475 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13476 __isl_take isl_multi_pw_aff
*mpa
)
13478 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13481 /* Return a copy of the equality constraints of "bset" as a matrix.
13483 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13484 __isl_keep isl_basic_set
*bset
)
13489 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13493 ctx
= isl_basic_set_get_ctx(bset
);
13494 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
13497 /* Are the "n" "coefficients" starting at "first" of the integer division
13498 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13500 * The "coefficient" at position 0 is the denominator.
13501 * The "coefficient" at position 1 is the constant term.
13503 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13504 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13505 unsigned first
, unsigned n
)
13507 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13508 return isl_bool_error
;
13509 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13510 return isl_bool_error
;
13511 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13512 bmap2
->div
[pos2
] + first
, n
);
13515 /* Are the integer division expressions at position "pos1" in "bmap1" and
13516 * "pos2" in "bmap2" equal to each other, except that the constant terms
13519 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13520 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13521 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13524 isl_size total
, total2
;
13526 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
13527 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
13528 if (total
< 0 || total2
< 0)
13529 return isl_bool_error
;
13530 if (total
!= total2
)
13531 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13532 "incomparable div expressions", return isl_bool_error
);
13533 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13535 if (equal
< 0 || !equal
)
13537 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13539 if (equal
< 0 || equal
)
13540 return isl_bool_not(equal
);
13541 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13545 /* Replace the numerator of the constant term of the integer division
13546 * expression at position "div" in "bmap" by "value".
13547 * The caller guarantees that this does not change the meaning
13550 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13551 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13553 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13554 return isl_basic_map_free(bmap
);
13556 isl_int_set_si(bmap
->div
[div
][1], value
);
13561 /* Is the point "inner" internal to inequality constraint "ineq"
13563 * The point is considered to be internal to the inequality constraint,
13564 * if it strictly lies on the positive side of the inequality constraint,
13565 * or if it lies on the constraint and the constraint is lexico-positive.
13567 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13568 __isl_keep isl_basic_set
*bset
, int ineq
)
13574 if (!inner
|| !bset
)
13575 return isl_bool_error
;
13577 ctx
= isl_basic_set_get_ctx(bset
);
13578 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13579 &ctx
->normalize_gcd
);
13580 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13581 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13583 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13585 return isl_bool_error
;
13586 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13587 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13590 /* Tighten the inequality constraints of "bset" that are outward with respect
13591 * to the point "vec".
13592 * That is, tighten the constraints that are not satisfied by "vec".
13594 * "vec" is a point internal to some superset S of "bset" that is used
13595 * to make the subsets of S disjoint, by tightening one half of the constraints
13596 * that separate two subsets. In particular, the constraints of S
13597 * are all satisfied by "vec" and should not be tightened.
13598 * Of the internal constraints, those that have "vec" on the outside
13599 * are tightened. The shared facet is included in the adjacent subset
13600 * with the opposite constraint.
13601 * For constraints that saturate "vec", this criterion cannot be used
13602 * to determine which of the two sides should be tightened.
13603 * Instead, the sign of the first non-zero coefficient is used
13604 * to make this choice. Note that this second criterion is never used
13605 * on the constraints of S since "vec" is interior to "S".
13607 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13608 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13612 bset
= isl_basic_set_cow(bset
);
13615 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13618 internal
= is_internal(vec
, bset
, j
);
13620 return isl_basic_set_free(bset
);
13623 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13629 /* Replace the variables x of type "type" starting at "first" in "bmap"
13630 * by x' with x = M x' with M the matrix trans.
13631 * That is, replace the corresponding coefficients c by c M.
13633 * The transformation matrix should be a square matrix.
13635 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13636 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13637 __isl_take isl_mat
*trans
)
13641 bmap
= isl_basic_map_cow(bmap
);
13642 if (!bmap
|| !trans
)
13645 if (trans
->n_row
!= trans
->n_col
)
13646 isl_die(trans
->ctx
, isl_error_invalid
,
13647 "expecting square transformation matrix", goto error
);
13648 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13651 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13653 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13654 isl_mat_copy(trans
)) < 0)
13656 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13657 isl_mat_copy(trans
)) < 0)
13659 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13660 isl_mat_copy(trans
)) < 0)
13663 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13664 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13666 isl_mat_free(trans
);
13669 isl_mat_free(trans
);
13670 isl_basic_map_free(bmap
);
13674 /* Replace the variables x of type "type" starting at "first" in "bset"
13675 * by x' with x = M x' with M the matrix trans.
13676 * That is, replace the corresponding coefficients c by c M.
13678 * The transformation matrix should be a square matrix.
13680 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13681 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13682 __isl_take isl_mat
*trans
)
13684 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);