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 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
75 enum isl_dim_type type
)
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 unsigned 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 unsigned 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 unsigned 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 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
182 return isl_basic_set_dim(bset
, isl_dim_set
);
185 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
187 return isl_basic_set_dim(bset
, isl_dim_param
);
190 unsigned 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 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
197 return isl_set_dim(set
, isl_dim_set
);
200 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
202 return isl_set_dim(set
, isl_dim_param
);
205 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
207 return bmap
? bmap
->dim
->n_in
: 0;
210 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
212 return bmap
? bmap
->dim
->n_out
: 0;
215 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
217 return bmap
? bmap
->dim
->nparam
: 0;
220 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
222 return bmap
? bmap
->n_div
: 0;
225 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
227 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
230 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
232 return map
? map
->dim
->n_out
: 0;
235 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
237 return map
? map
->dim
->nparam
: 0;
240 /* Return the number of equality constraints in the description of "bmap".
241 * Return -1 on error.
243 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
250 /* Return the number of equality constraints in the description of "bset".
251 * Return -1 on error.
253 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
255 return isl_basic_map_n_equality(bset_to_bmap(bset
));
258 /* Return the number of inequality constraints in the description of "bmap".
259 * Return -1 on error.
261 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
268 /* Return the number of inequality constraints in the description of "bset".
269 * Return -1 on error.
271 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
273 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
276 /* Do "bmap1" and "bmap2" have the same parameters?
278 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
279 __isl_keep isl_basic_map
*bmap2
)
281 isl_space
*space1
, *space2
;
283 space1
= isl_basic_map_peek_space(bmap1
);
284 space2
= isl_basic_map_peek_space(bmap2
);
285 return isl_space_has_equal_params(space1
, space2
);
288 /* Do "map1" and "map2" have the same parameters?
290 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
291 __isl_keep isl_map
*map2
)
293 isl_space
*space1
, *space2
;
295 space1
= isl_map_peek_space(map1
);
296 space2
= isl_map_peek_space(map2
);
297 return isl_space_has_equal_params(space1
, space2
);
300 /* Do "map" and "set" have the same parameters?
302 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
303 __isl_keep isl_set
*set
)
305 return isl_map_has_equal_params(map
, set_to_map(set
));
308 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
309 __isl_keep isl_set
*set
)
313 return isl_bool_error
;
314 m
= isl_map_has_equal_params(map
, set_to_map(set
));
317 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
318 set
->dim
, isl_dim_set
);
321 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
322 __isl_keep isl_basic_set
*bset
)
326 return isl_bool_error
;
327 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
330 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
331 bset
->dim
, isl_dim_set
);
334 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
335 __isl_keep isl_set
*set
)
339 return isl_bool_error
;
340 m
= isl_map_has_equal_params(map
, set_to_map(set
));
343 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
344 set
->dim
, isl_dim_set
);
347 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
348 __isl_keep isl_basic_set
*bset
)
352 return isl_bool_error
;
353 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
356 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
357 bset
->dim
, isl_dim_set
);
360 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
362 return bmap
? bmap
->ctx
: NULL
;
365 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
367 return bset
? bset
->ctx
: NULL
;
370 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
372 return map
? map
->ctx
: NULL
;
375 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
377 return set
? set
->ctx
: NULL
;
380 /* Return the space of "bmap".
382 __isl_keep isl_space
*isl_basic_map_peek_space(
383 __isl_keep
const isl_basic_map
*bmap
)
385 return bmap
? bmap
->dim
: NULL
;
388 /* Return the space of "bset".
390 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
392 return isl_basic_map_peek_space(bset_to_bmap(bset
));
395 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
397 return isl_space_copy(isl_basic_map_peek_space(bmap
));
400 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
402 return isl_basic_map_get_space(bset_to_bmap(bset
));
405 /* Extract the divs in "bmap" as a matrix.
407 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
415 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
419 ctx
= isl_basic_map_get_ctx(bmap
);
420 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
421 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
425 for (i
= 0; i
< bmap
->n_div
; ++i
)
426 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
431 /* Extract the divs in "bset" as a matrix.
433 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
435 return isl_basic_map_get_divs(bset
);
438 __isl_give isl_local_space
*isl_basic_map_get_local_space(
439 __isl_keep isl_basic_map
*bmap
)
446 div
= isl_basic_map_get_divs(bmap
);
447 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
450 __isl_give isl_local_space
*isl_basic_set_get_local_space(
451 __isl_keep isl_basic_set
*bset
)
453 return isl_basic_map_get_local_space(bset
);
456 /* For each known div d = floor(f/m), add the constraints
459 * -(f-(m-1)) + m d >= 0
461 * Do not finalize the result.
463 static __isl_give isl_basic_map
*add_known_div_constraints(
464 __isl_take isl_basic_map
*bmap
)
471 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
474 bmap
= isl_basic_map_cow(bmap
);
475 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
478 for (i
= 0; i
< n_div
; ++i
) {
479 if (isl_int_is_zero(bmap
->div
[i
][0]))
481 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
487 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
488 __isl_take isl_local_space
*ls
)
497 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
498 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
499 n_div
, 0, 2 * n_div
);
501 for (i
= 0; i
< n_div
; ++i
)
502 if (isl_basic_map_alloc_div(bmap
) < 0)
505 for (i
= 0; i
< n_div
; ++i
)
506 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
507 bmap
= add_known_div_constraints(bmap
);
509 isl_local_space_free(ls
);
512 isl_local_space_free(ls
);
513 isl_basic_map_free(bmap
);
517 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
518 __isl_take isl_local_space
*ls
)
520 return isl_basic_map_from_local_space(ls
);
523 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
525 return isl_space_copy(isl_map_peek_space(map
));
528 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
532 return isl_space_copy(set
->dim
);
535 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
536 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
538 bmap
= isl_basic_map_cow(bmap
);
541 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
544 bmap
= isl_basic_map_finalize(bmap
);
547 isl_basic_map_free(bmap
);
551 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
552 __isl_take isl_basic_set
*bset
, const char *s
)
554 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
557 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
558 enum isl_dim_type type
)
560 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
563 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
564 enum isl_dim_type type
, const char *s
)
568 map
= isl_map_cow(map
);
572 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
576 for (i
= 0; i
< map
->n
; ++i
) {
577 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
588 /* Replace the identifier of the tuple of type "type" by "id".
590 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
591 __isl_take isl_basic_map
*bmap
,
592 enum isl_dim_type type
, __isl_take isl_id
*id
)
594 bmap
= isl_basic_map_cow(bmap
);
597 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
599 return isl_basic_map_free(bmap
);
600 bmap
= isl_basic_map_finalize(bmap
);
607 /* Replace the identifier of the tuple by "id".
609 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
610 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
612 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
615 /* Does the input or output tuple have a name?
617 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
619 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
622 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
623 enum isl_dim_type type
)
625 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
628 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
631 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
635 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
636 enum isl_dim_type type
, __isl_take isl_id
*id
)
638 map
= isl_map_cow(map
);
642 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
644 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
650 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
651 __isl_take isl_id
*id
)
653 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
656 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
657 enum isl_dim_type type
)
659 map
= isl_map_cow(map
);
663 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
665 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
668 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
670 return isl_map_reset_tuple_id(set
, isl_dim_set
);
673 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
675 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
678 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
679 enum isl_dim_type type
)
681 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
684 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
686 return isl_map_has_tuple_id(set
, isl_dim_set
);
689 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
691 return isl_map_get_tuple_id(set
, isl_dim_set
);
694 /* Does the set tuple have a name?
696 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
699 return isl_bool_error
;
700 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
704 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
706 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
709 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
711 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
714 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
715 enum isl_dim_type type
, unsigned pos
)
717 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
720 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
721 enum isl_dim_type type
, unsigned pos
)
723 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
726 /* Does the given dimension have a name?
728 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
729 enum isl_dim_type type
, unsigned pos
)
732 return isl_bool_error
;
733 return isl_space_has_dim_name(map
->dim
, type
, pos
);
736 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
737 enum isl_dim_type type
, unsigned pos
)
739 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
742 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
743 enum isl_dim_type type
, unsigned pos
)
745 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
748 /* Does the given dimension have a name?
750 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
751 enum isl_dim_type type
, unsigned pos
)
754 return isl_bool_error
;
755 return isl_space_has_dim_name(set
->dim
, type
, pos
);
758 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
759 __isl_take isl_basic_map
*bmap
,
760 enum isl_dim_type type
, unsigned pos
, const char *s
)
762 bmap
= isl_basic_map_cow(bmap
);
765 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
768 return isl_basic_map_finalize(bmap
);
770 isl_basic_map_free(bmap
);
774 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
775 enum isl_dim_type type
, unsigned pos
, const char *s
)
779 map
= isl_map_cow(map
);
783 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
787 for (i
= 0; i
< map
->n
; ++i
) {
788 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
799 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
800 __isl_take isl_basic_set
*bset
,
801 enum isl_dim_type type
, unsigned pos
, const char *s
)
803 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
807 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
808 enum isl_dim_type type
, unsigned pos
, const char *s
)
810 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
814 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
815 enum isl_dim_type type
, unsigned pos
)
818 return isl_bool_error
;
819 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
822 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
823 enum isl_dim_type type
, unsigned pos
)
825 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
828 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
829 enum isl_dim_type type
, unsigned pos
)
831 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
834 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
835 enum isl_dim_type type
, unsigned pos
)
837 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
840 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
841 enum isl_dim_type type
, unsigned pos
)
843 return isl_map_has_dim_id(set
, type
, pos
);
846 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
847 enum isl_dim_type type
, unsigned pos
)
849 return isl_map_get_dim_id(set
, type
, pos
);
852 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
853 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
855 map
= isl_map_cow(map
);
859 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
861 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
867 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
868 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
870 return isl_map_set_dim_id(set
, type
, pos
, id
);
873 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
874 __isl_keep isl_id
*id
)
878 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
881 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
882 __isl_keep isl_id
*id
)
884 return isl_map_find_dim_by_id(set
, type
, id
);
887 /* Return the position of the dimension of the given type and name
889 * Return -1 if no such dimension can be found.
891 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
892 enum isl_dim_type type
, const char *name
)
896 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
899 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
904 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
907 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
910 return isl_map_find_dim_by_name(set
, type
, name
);
913 /* Check whether equality i of bset is a pure stride constraint
914 * on a single dimension, i.e., of the form
918 * with k a constant and e an existentially quantified variable.
920 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
929 return isl_bool_error
;
931 if (!isl_int_is_zero(bset
->eq
[i
][0]))
932 return isl_bool_false
;
934 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
935 d
= isl_basic_set_dim(bset
, isl_dim_set
);
936 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
938 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
939 return isl_bool_false
;
940 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
942 return isl_bool_false
;
943 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
945 return isl_bool_false
;
947 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
949 return isl_bool_false
;
950 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
951 n_div
- pos2
- 1) != -1)
952 return isl_bool_false
;
953 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
954 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
955 return isl_bool_false
;
957 return isl_bool_true
;
960 /* Reset the user pointer on all identifiers of parameters and tuples
961 * of the space of "map".
963 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
967 space
= isl_map_get_space(map
);
968 space
= isl_space_reset_user(space
);
969 map
= isl_map_reset_space(map
, space
);
974 /* Reset the user pointer on all identifiers of parameters and tuples
975 * of the space of "set".
977 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
979 return isl_map_reset_user(set
);
982 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
985 return isl_bool_error
;
986 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
989 /* Has "map" been marked as a rational map?
990 * In particular, have all basic maps in "map" been marked this way?
991 * An empty map is not considered to be rational.
992 * Maps where only some of the basic maps are marked rational
995 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1001 return isl_bool_error
;
1003 return isl_bool_false
;
1004 rational
= isl_basic_map_is_rational(map
->p
[0]);
1007 for (i
= 1; i
< map
->n
; ++i
) {
1008 isl_bool rational_i
;
1010 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1013 if (rational
!= rational_i
)
1014 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1015 "mixed rational and integer basic maps "
1016 "not supported", return isl_bool_error
);
1022 /* Has "set" been marked as a rational set?
1023 * In particular, have all basic set in "set" been marked this way?
1024 * An empty set is not considered to be rational.
1025 * Sets where only some of the basic sets are marked rational
1028 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1030 return isl_map_is_rational(set
);
1033 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1035 return isl_basic_map_is_rational(bset
);
1038 /* Does "bmap" contain any rational points?
1040 * If "bmap" has an equality for each dimension, equating the dimension
1041 * to an integer constant, then it has no rational points, even if it
1042 * is marked as rational.
1044 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1046 isl_bool has_rational
= isl_bool_true
;
1050 return isl_bool_error
;
1051 if (isl_basic_map_plain_is_empty(bmap
))
1052 return isl_bool_false
;
1053 if (!isl_basic_map_is_rational(bmap
))
1054 return isl_bool_false
;
1055 bmap
= isl_basic_map_copy(bmap
);
1056 bmap
= isl_basic_map_implicit_equalities(bmap
);
1058 return isl_bool_error
;
1059 total
= isl_basic_map_total_dim(bmap
);
1060 if (bmap
->n_eq
== total
) {
1062 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1063 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1066 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1067 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1069 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1074 if (i
== bmap
->n_eq
)
1075 has_rational
= isl_bool_false
;
1077 isl_basic_map_free(bmap
);
1079 return has_rational
;
1082 /* Does "map" contain any rational points?
1084 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1087 isl_bool has_rational
;
1090 return isl_bool_error
;
1091 for (i
= 0; i
< map
->n
; ++i
) {
1092 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1093 if (has_rational
< 0 || has_rational
)
1094 return has_rational
;
1096 return isl_bool_false
;
1099 /* Does "set" contain any rational points?
1101 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1103 return isl_map_has_rational(set
);
1106 /* Is this basic set a parameter domain?
1108 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1111 return isl_bool_error
;
1112 return isl_space_is_params(bset
->dim
);
1115 /* Is this set a parameter domain?
1117 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1120 return isl_bool_error
;
1121 return isl_space_is_params(set
->dim
);
1124 /* Is this map actually a parameter domain?
1125 * Users should never call this function. Outside of isl,
1126 * a map can never be a parameter domain.
1128 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1131 return isl_bool_error
;
1132 return isl_space_is_params(map
->dim
);
1135 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1136 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1137 unsigned n_eq
, unsigned n_ineq
)
1140 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1141 unsigned n_var
= isl_space_dim(space
, isl_dim_all
);
1142 size_t row_size
= 1 + n_var
+ extra
;
1148 return isl_basic_map_free(bmap
);
1150 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1151 if (isl_blk_is_error(bmap
->block
))
1154 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1155 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1159 bmap
->block2
= isl_blk_empty();
1162 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1163 if (isl_blk_is_error(bmap
->block2
))
1166 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1171 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1172 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1174 for (i
= 0; i
< extra
; ++i
)
1175 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1179 bmap
->c_size
= n_eq
+ n_ineq
;
1180 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1181 bmap
->extra
= extra
;
1185 bmap
->sample
= NULL
;
1189 isl_basic_map_free(bmap
);
1193 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1194 unsigned nparam
, unsigned dim
, unsigned extra
,
1195 unsigned n_eq
, unsigned n_ineq
)
1197 struct isl_basic_map
*bmap
;
1200 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1204 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1205 return bset_from_bmap(bmap
);
1208 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1209 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1211 struct isl_basic_map
*bmap
;
1214 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1215 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1216 return bset_from_bmap(bmap
);
1218 isl_space_free(dim
);
1222 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1223 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1225 struct isl_basic_map
*bmap
;
1229 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1234 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1236 isl_space_free(space
);
1240 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1241 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1242 unsigned n_eq
, unsigned n_ineq
)
1244 struct isl_basic_map
*bmap
;
1247 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1251 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1255 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1256 __isl_keep isl_basic_map
*src
)
1259 unsigned total
= isl_basic_map_total_dim(src
);
1264 for (i
= 0; i
< src
->n_eq
; ++i
) {
1265 int j
= isl_basic_map_alloc_equality(dst
);
1267 return isl_basic_map_free(dst
);
1268 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1271 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1272 int j
= isl_basic_map_alloc_inequality(dst
);
1274 return isl_basic_map_free(dst
);
1275 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1278 for (i
= 0; i
< src
->n_div
; ++i
) {
1279 int j
= isl_basic_map_alloc_div(dst
);
1281 return isl_basic_map_free(dst
);
1282 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1284 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1288 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1290 struct isl_basic_map
*dup
;
1294 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1295 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1296 dup
= dup_constraints(dup
, bmap
);
1299 dup
->flags
= bmap
->flags
;
1300 dup
->sample
= isl_vec_copy(bmap
->sample
);
1304 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1306 struct isl_basic_map
*dup
;
1308 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1309 return bset_from_bmap(dup
);
1312 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1317 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1321 return isl_basic_set_dup(bset
);
1324 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1333 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1338 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1342 bmap
= isl_basic_map_dup(bmap
);
1344 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1348 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1357 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1362 if (--bmap
->ref
> 0)
1365 isl_ctx_deref(bmap
->ctx
);
1367 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1369 isl_blk_free(bmap
->ctx
, bmap
->block
);
1370 isl_vec_free(bmap
->sample
);
1371 isl_space_free(bmap
->dim
);
1377 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1379 return isl_basic_map_free(bset_to_bmap(bset
));
1382 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1384 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1387 /* Check that "bset" does not involve any parameters.
1389 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1392 return isl_stat_error
;
1393 if (isl_basic_set_dim(bset
, isl_dim_param
) != 0)
1394 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1395 "basic set should not have any parameters",
1396 return isl_stat_error
);
1400 /* Check that "bset" does not involve any local variables.
1402 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1405 return isl_stat_error
;
1406 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
1407 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1408 "basic set should not have any local variables",
1409 return isl_stat_error
);
1413 /* Check that "map" has only named parameters, reporting an error
1416 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1418 return isl_space_check_named_params(isl_map_peek_space(map
));
1421 /* Check that "bmap" has only named parameters, reporting an error
1424 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1426 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1429 /* Check that "bmap1" and "bmap2" have the same parameters,
1430 * reporting an error if they do not.
1432 static isl_stat
isl_basic_map_check_equal_params(
1433 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1437 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1439 return isl_stat_error
;
1441 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1442 "parameters don't match", return isl_stat_error
);
1446 __isl_give isl_map
*isl_map_align_params_map_map_and(
1447 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1448 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1449 __isl_take isl_map
*map2
))
1453 if (isl_map_has_equal_params(map1
, map2
))
1454 return fn(map1
, map2
);
1455 if (isl_map_check_named_params(map1
) < 0)
1457 if (isl_map_check_named_params(map2
) < 0)
1459 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1460 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1461 return fn(map1
, map2
);
1468 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1469 __isl_keep isl_map
*map2
,
1470 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1475 return isl_bool_error
;
1476 if (isl_map_has_equal_params(map1
, map2
))
1477 return fn(map1
, map2
);
1478 if (isl_map_check_named_params(map1
) < 0)
1479 return isl_bool_error
;
1480 if (isl_map_check_named_params(map2
) < 0)
1481 return isl_bool_error
;
1482 map1
= isl_map_copy(map1
);
1483 map2
= isl_map_copy(map2
);
1484 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1485 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1492 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1494 struct isl_ctx
*ctx
;
1498 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1499 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1502 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1505 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1507 int j
= isl_basic_map_alloc_inequality(bmap
);
1511 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1512 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1519 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1520 bmap
->extra
- bmap
->n_div
);
1521 return bmap
->n_eq
++;
1524 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1526 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1529 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1533 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1538 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1540 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1543 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1548 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1550 if (pos
!= bmap
->n_eq
- 1) {
1552 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1553 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1559 /* Turn inequality "pos" of "bmap" into an equality.
1561 * In particular, we move the inequality in front of the equalities
1562 * and move the last inequality in the position of the moved inequality.
1563 * Note that isl_tab_make_equalities_explicit depends on this particular
1564 * change in the ordering of the constraints.
1566 void isl_basic_map_inequality_to_equality(
1567 struct isl_basic_map
*bmap
, unsigned pos
)
1571 t
= bmap
->ineq
[pos
];
1572 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1573 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1578 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1579 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1580 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1581 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1584 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1586 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1589 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1591 struct isl_ctx
*ctx
;
1595 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1596 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1597 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1598 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1599 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1600 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1601 1 + isl_basic_map_total_dim(bmap
),
1602 bmap
->extra
- bmap
->n_div
);
1603 return bmap
->n_ineq
++;
1606 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1608 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1611 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1615 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1620 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1622 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1625 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1630 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1632 if (pos
!= bmap
->n_ineq
- 1) {
1633 t
= bmap
->ineq
[pos
];
1634 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1635 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1636 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1642 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1644 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1647 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1653 empty
= isl_basic_map_plain_is_empty(bmap
);
1655 return isl_basic_map_free(bmap
);
1659 bmap
= isl_basic_map_cow(bmap
);
1660 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1663 k
= isl_basic_map_alloc_equality(bmap
);
1666 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1669 isl_basic_map_free(bmap
);
1673 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1676 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1679 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1684 bmap
= isl_basic_map_cow(bmap
);
1685 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1688 k
= isl_basic_map_alloc_inequality(bmap
);
1691 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1694 isl_basic_map_free(bmap
);
1698 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1701 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1704 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1708 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1709 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1710 1 + 1 + isl_basic_map_total_dim(bmap
),
1711 bmap
->extra
- bmap
->n_div
);
1712 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1713 return bmap
->n_div
++;
1716 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1718 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1722 #define TYPE isl_basic_map
1723 #include "check_type_range_templ.c"
1725 /* Check that there are "n" dimensions of type "type" starting at "first"
1728 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1729 enum isl_dim_type type
, unsigned first
, unsigned n
)
1731 return isl_basic_map_check_range(bset_to_bmap(bset
),
1735 /* Insert an extra integer division, prescribed by "div", to "bmap"
1736 * at (integer division) position "pos".
1738 * The integer division is first added at the end and then moved
1739 * into the right position.
1741 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1742 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1746 bmap
= isl_basic_map_cow(bmap
);
1748 return isl_basic_map_free(bmap
);
1750 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1751 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1752 "unexpected size", return isl_basic_map_free(bmap
));
1753 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1754 return isl_basic_map_free(bmap
);
1756 bmap
= isl_basic_map_extend_space(bmap
,
1757 isl_basic_map_get_space(bmap
), 1, 0, 2);
1758 k
= isl_basic_map_alloc_div(bmap
);
1760 return isl_basic_map_free(bmap
);
1761 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1762 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1764 for (i
= k
; i
> pos
; --i
)
1765 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1770 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1773 return isl_stat_error
;
1774 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1779 static __isl_give isl_basic_map
*add_constraints(
1780 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1781 unsigned i_pos
, unsigned o_pos
)
1783 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1786 struct isl_dim_map
*dim_map
;
1788 space
= isl_basic_map_peek_space(bmap2
);
1789 if (!bmap1
|| !space
)
1792 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1793 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1794 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1795 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1796 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1797 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1798 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
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
)
2036 total
= isl_basic_map_total_dim(bmap
);
2037 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2038 return isl_basic_map_free(bmap
);
2039 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2041 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2043 i
= isl_basic_map_alloc_equality(bmap
);
2047 isl_int_set_si(bmap
->eq
[i
][0], 1);
2048 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2049 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2050 isl_vec_free(bmap
->sample
);
2051 bmap
->sample
= NULL
;
2052 return isl_basic_map_finalize(bmap
);
2054 isl_basic_map_free(bmap
);
2058 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2059 __isl_take isl_basic_set
*bset
)
2061 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2064 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2065 __isl_take isl_basic_map
*bmap
)
2070 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2073 bmap
= isl_basic_map_cow(bmap
);
2077 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2079 return isl_basic_map_finalize(bmap
);
2082 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2083 __isl_take isl_basic_set
*bset
)
2085 return isl_basic_map_set_rational(bset
);
2088 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2089 __isl_take isl_basic_set
*bset
)
2094 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2097 bset
= isl_basic_set_cow(bset
);
2101 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2103 return isl_basic_set_finalize(bset
);
2106 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2110 map
= isl_map_cow(map
);
2113 for (i
= 0; i
< map
->n
; ++i
) {
2114 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2124 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2126 return isl_map_set_rational(set
);
2129 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2134 isl_int
*t
= bmap
->div
[a
];
2135 bmap
->div
[a
] = bmap
->div
[b
];
2139 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2140 * div definitions accordingly.
2142 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2148 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2150 return isl_basic_map_free(bmap
);
2152 swap_div(bmap
, a
, b
);
2154 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2155 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2157 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2158 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2160 for (i
= 0; i
< bmap
->n_div
; ++i
)
2161 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2162 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2167 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2169 isl_seq_cpy(c
, c
+ n
, rem
);
2170 isl_seq_clr(c
+ rem
, n
);
2173 /* Drop n dimensions starting at first.
2175 * In principle, this frees up some extra variables as the number
2176 * of columns remains constant, but we would have to extend
2177 * the div array too as the number of rows in this array is assumed
2178 * to be equal to extra.
2180 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2181 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2183 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2186 /* Move "n" divs starting at "first" to the end of the list of divs.
2188 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2189 unsigned first
, unsigned n
)
2194 if (first
+ n
== bmap
->n_div
)
2197 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2200 for (i
= 0; i
< n
; ++i
)
2201 div
[i
] = bmap
->div
[first
+ i
];
2202 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2203 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2204 for (i
= 0; i
< n
; ++i
)
2205 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2209 isl_basic_map_free(bmap
);
2214 #define TYPE isl_map
2216 #include "check_type_range_templ.c"
2218 /* Check that there are "n" dimensions of type "type" starting at "first"
2221 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2222 enum isl_dim_type type
, unsigned first
, unsigned n
)
2224 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2227 /* Drop "n" dimensions of type "type" starting at "first".
2228 * Perform the core computation, without cowing or
2229 * simplifying and finalizing the result.
2231 * In principle, this frees up some extra variables as the number
2232 * of columns remains constant, but we would have to extend
2233 * the div array too as the number of rows in this array is assumed
2234 * to be equal to extra.
2236 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2237 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2238 unsigned first
, unsigned n
)
2244 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2245 return isl_basic_map_free(bmap
);
2247 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2248 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2249 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2250 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2252 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2253 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2255 for (i
= 0; i
< bmap
->n_div
; ++i
)
2256 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2258 if (type
== isl_dim_div
) {
2259 bmap
= move_divs_last(bmap
, first
, n
);
2262 if (isl_basic_map_free_div(bmap
, n
) < 0)
2263 return isl_basic_map_free(bmap
);
2265 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2267 return isl_basic_map_free(bmap
);
2269 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2274 /* Drop "n" dimensions of type "type" starting at "first".
2276 * In principle, this frees up some extra variables as the number
2277 * of columns remains constant, but we would have to extend
2278 * the div array too as the number of rows in this array is assumed
2279 * to be equal to extra.
2281 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2282 enum isl_dim_type type
, unsigned first
, unsigned n
)
2286 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2289 bmap
= isl_basic_map_cow(bmap
);
2293 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2295 bmap
= isl_basic_map_simplify(bmap
);
2296 return isl_basic_map_finalize(bmap
);
2299 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2300 enum isl_dim_type type
, unsigned first
, unsigned n
)
2302 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2306 /* No longer consider "map" to be normalized.
2308 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2312 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2316 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2317 enum isl_dim_type type
, unsigned first
, unsigned n
)
2321 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2322 return isl_map_free(map
);
2324 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2326 map
= isl_map_cow(map
);
2329 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2333 for (i
= 0; i
< map
->n
; ++i
) {
2334 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2338 map
= isl_map_unmark_normalized(map
);
2346 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2347 enum isl_dim_type type
, unsigned first
, unsigned n
)
2349 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2352 /* Drop the integer division at position "div", which is assumed
2353 * not to appear in any of the constraints or
2354 * in any of the other integer divisions.
2356 * Since the integer division is redundant, there is no need to cow.
2358 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2359 __isl_take isl_basic_map
*bmap
, unsigned div
)
2361 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2364 /* Eliminate the specified n dimensions starting at first from the
2365 * constraints, without removing the dimensions from the space.
2366 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2368 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2369 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2377 return isl_map_free(map
);
2379 map
= isl_map_cow(map
);
2383 for (i
= 0; i
< map
->n
; ++i
) {
2384 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2394 /* Eliminate the specified n dimensions starting at first from the
2395 * constraints, without removing the dimensions from the space.
2396 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2398 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2399 enum isl_dim_type type
, unsigned first
, unsigned n
)
2401 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2404 /* Eliminate the specified n dimensions starting at first from the
2405 * constraints, without removing the dimensions from the space.
2406 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2409 unsigned first
, unsigned n
)
2411 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2414 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2415 __isl_take isl_basic_map
*bmap
)
2419 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2421 return isl_basic_map_free(bmap
);
2422 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2426 return isl_basic_map_finalize(bmap
);
2429 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set
*bset
)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2435 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2444 map
= isl_map_cow(map
);
2448 for (i
= 0; i
< map
->n
; ++i
) {
2449 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2459 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2461 return isl_map_remove_divs(set
);
2464 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2466 unsigned first
, unsigned n
)
2468 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2469 return isl_basic_map_free(bmap
);
2470 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2472 bmap
= isl_basic_map_eliminate_vars(bmap
,
2473 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2476 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2478 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2486 unsigned first
, unsigned n
)
2489 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2491 if (isl_int_is_zero(bmap
->div
[div
][0]))
2492 return isl_bool_false
;
2493 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2494 return isl_bool_true
;
2496 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2499 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2501 involves
= div_involves_vars(bmap
, i
, first
, n
);
2502 if (involves
< 0 || involves
)
2506 return isl_bool_false
;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2534 * with the condition that the coefficients of g(x) - f(x) are all
2536 * Rewriting this constraint as
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2559 unsigned total
, isl_int v
, int lb
, int ub
)
2563 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2565 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2566 bmap
->div
[div
][1 + 1 + j
]);
2567 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2570 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2571 bmap
->div
[div
][1 + 1 + j
]);
2572 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2578 bmap
= isl_basic_map_cow(bmap
);
2579 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2581 int k
= isl_basic_map_alloc_inequality(bmap
);
2584 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2585 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2586 bmap
->div
[div
][1 + j
]);
2587 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2588 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2590 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2593 int k
= isl_basic_map_alloc_inequality(bmap
);
2596 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2597 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2598 bmap
->div
[div
][1 + j
]);
2599 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2600 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2602 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2607 isl_basic_map_free(bmap
);
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2656 static __isl_give isl_basic_map
*insert_bounds_on_div(
2657 __isl_take isl_basic_map
*bmap
, int div
)
2660 int check_lb
, check_ub
;
2667 if (isl_int_is_zero(bmap
->div
[div
][0]))
2670 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2672 return isl_basic_map_free(bmap
);
2676 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2677 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2684 if (!check_lb
&& !check_ub
)
2689 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2690 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2693 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2694 check_lb
, check_ub
);
2702 /* Remove all divs (recursively) involving any of the given dimensions
2703 * in their definitions.
2705 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2706 __isl_take isl_basic_map
*bmap
,
2707 enum isl_dim_type type
, unsigned first
, unsigned n
)
2711 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2712 return isl_basic_map_free(bmap
);
2713 first
+= isl_basic_map_offset(bmap
, type
);
2715 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2718 involves
= div_involves_vars(bmap
, i
, first
, n
);
2720 return isl_basic_map_free(bmap
);
2723 bmap
= insert_bounds_on_div(bmap
, i
);
2724 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2733 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2734 __isl_take isl_basic_set
*bset
,
2735 enum isl_dim_type type
, unsigned first
, unsigned n
)
2737 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2740 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2741 enum isl_dim_type type
, unsigned first
, unsigned n
)
2750 map
= isl_map_cow(map
);
2754 for (i
= 0; i
< map
->n
; ++i
) {
2755 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2766 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2767 enum isl_dim_type type
, unsigned first
, unsigned n
)
2769 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2773 /* Does the description of "bmap" depend on the specified dimensions?
2774 * We also check whether the dimensions appear in any of the div definitions.
2775 * In principle there is no need for this check. If the dimensions appear
2776 * in a div definition, they also appear in the defining constraints of that
2779 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2780 enum isl_dim_type type
, unsigned first
, unsigned n
)
2784 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2785 return isl_bool_error
;
2787 first
+= isl_basic_map_offset(bmap
, type
);
2788 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2789 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2790 return isl_bool_true
;
2791 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2792 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2793 return isl_bool_true
;
2794 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2795 if (isl_int_is_zero(bmap
->div
[i
][0]))
2797 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2798 return isl_bool_true
;
2801 return isl_bool_false
;
2804 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2805 enum isl_dim_type type
, unsigned first
, unsigned n
)
2809 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2810 return isl_bool_error
;
2812 for (i
= 0; i
< map
->n
; ++i
) {
2813 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2815 if (involves
< 0 || involves
)
2819 return isl_bool_false
;
2822 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2823 enum isl_dim_type type
, unsigned first
, unsigned n
)
2825 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2828 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2829 enum isl_dim_type type
, unsigned first
, unsigned n
)
2831 return isl_map_involves_dims(set
, type
, first
, n
);
2834 /* Drop all constraints in bmap that involve any of the dimensions
2835 * first to first+n-1.
2836 * This function only performs the actual removal of constraints.
2838 * This function should not call finalize since it is used by
2839 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2841 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2842 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2849 bmap
= isl_basic_map_cow(bmap
);
2854 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2855 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2857 isl_basic_map_drop_equality(bmap
, i
);
2860 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2861 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2863 isl_basic_map_drop_inequality(bmap
, i
);
2869 /* Drop all constraints in bset that involve any of the dimensions
2870 * first to first+n-1.
2871 * This function only performs the actual removal of constraints.
2873 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2874 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2876 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2879 /* Drop all constraints in bmap that do not involve any of the dimensions
2880 * first to first + n - 1 of the given type.
2882 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2883 __isl_take isl_basic_map
*bmap
,
2884 enum isl_dim_type type
, unsigned first
, unsigned n
)
2889 isl_space
*space
= isl_basic_map_get_space(bmap
);
2890 isl_basic_map_free(bmap
);
2891 return isl_basic_map_universe(space
);
2893 bmap
= isl_basic_map_cow(bmap
);
2897 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2898 return isl_basic_map_free(bmap
);
2900 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2902 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2903 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2905 isl_basic_map_drop_equality(bmap
, i
);
2908 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2909 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2911 isl_basic_map_drop_inequality(bmap
, i
);
2914 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2918 /* Drop all constraints in bset that do not involve any of the dimensions
2919 * first to first + n - 1 of the given type.
2921 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2922 __isl_take isl_basic_set
*bset
,
2923 enum isl_dim_type type
, unsigned first
, unsigned n
)
2925 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2929 /* Drop all constraints in bmap that involve any of the dimensions
2930 * first to first + n - 1 of the given type.
2932 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2933 __isl_take isl_basic_map
*bmap
,
2934 enum isl_dim_type type
, unsigned first
, unsigned n
)
2941 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2942 return isl_basic_map_free(bmap
);
2944 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2945 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2946 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2947 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2951 /* Drop all constraints in bset that involve any of the dimensions
2952 * first to first + n - 1 of the given type.
2954 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2955 __isl_take isl_basic_set
*bset
,
2956 enum isl_dim_type type
, unsigned first
, unsigned n
)
2958 return isl_basic_map_drop_constraints_involving_dims(bset
,
2962 /* Drop constraints from "map" by applying "drop" to each basic map.
2964 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
,
2966 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2967 enum isl_dim_type type
, unsigned first
, unsigned n
))
2971 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2972 return isl_map_free(map
);
2974 map
= isl_map_cow(map
);
2978 for (i
= 0; i
< map
->n
; ++i
) {
2979 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2981 return isl_map_free(map
);
2985 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2990 /* Drop all constraints in map that involve any of the dimensions
2991 * first to first + n - 1 of the given type.
2993 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2994 __isl_take isl_map
*map
,
2995 enum isl_dim_type type
, unsigned first
, unsigned n
)
2999 return drop_constraints(map
, type
, first
, n
,
3000 &isl_basic_map_drop_constraints_involving_dims
);
3003 /* Drop all constraints in "map" that do not involve any of the dimensions
3004 * first to first + n - 1 of the given type.
3006 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3007 __isl_take isl_map
*map
,
3008 enum isl_dim_type type
, unsigned first
, unsigned n
)
3011 isl_space
*space
= isl_map_get_space(map
);
3013 return isl_map_universe(space
);
3015 return drop_constraints(map
, type
, first
, n
,
3016 &isl_basic_map_drop_constraints_not_involving_dims
);
3019 /* Drop all constraints in set that involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3023 __isl_take isl_set
*set
,
3024 enum isl_dim_type type
, unsigned first
, unsigned n
)
3026 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3029 /* Drop all constraints in "set" that do not involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3032 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3033 __isl_take isl_set
*set
,
3034 enum isl_dim_type type
, unsigned first
, unsigned n
)
3036 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3039 /* Does local variable "div" of "bmap" have a complete explicit representation?
3040 * Having a complete explicit representation requires not only
3041 * an explicit representation, but also that all local variables
3042 * that appear in this explicit representation in turn have
3043 * a complete explicit representation.
3045 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3048 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3051 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3052 if (marked
< 0 || marked
)
3053 return isl_bool_not(marked
);
3055 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3058 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3060 known
= isl_basic_map_div_is_known(bmap
, i
);
3061 if (known
< 0 || !known
)
3065 return isl_bool_true
;
3068 /* Remove all divs that are unknown or defined in terms of unknown divs.
3070 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3071 __isl_take isl_basic_map
*bmap
)
3078 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3079 if (isl_basic_map_div_is_known(bmap
, i
))
3081 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3090 /* Remove all divs that are unknown or defined in terms of unknown divs.
3092 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3093 __isl_take isl_basic_set
*bset
)
3095 return isl_basic_map_remove_unknown_divs(bset
);
3098 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3107 map
= isl_map_cow(map
);
3111 for (i
= 0; i
< map
->n
; ++i
) {
3112 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3122 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3124 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3127 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3128 __isl_take isl_basic_set
*bset
,
3129 enum isl_dim_type type
, unsigned first
, unsigned n
)
3131 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3132 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3133 return bset_from_bmap(bmap
);
3136 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3137 enum isl_dim_type type
, unsigned first
, unsigned n
)
3144 map
= isl_map_cow(map
);
3145 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3146 return isl_map_free(map
);
3148 for (i
= 0; i
< map
->n
; ++i
) {
3149 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3150 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3154 map
= isl_map_drop(map
, type
, first
, n
);
3161 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3162 enum isl_dim_type type
, unsigned first
, unsigned n
)
3164 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3168 /* Project out n inputs starting at first using Fourier-Motzkin */
3169 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3170 unsigned first
, unsigned n
)
3172 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3175 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3176 FILE *out
, int indent
)
3181 fprintf(out
, "null basic set\n");
3185 fprintf(out
, "%*s", indent
, "");
3186 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3187 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3188 bset
->extra
, bset
->flags
);
3190 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3191 p
= isl_printer_set_dump(p
, 1);
3192 p
= isl_printer_set_indent(p
, indent
);
3193 p
= isl_printer_start_line(p
);
3194 p
= isl_printer_print_basic_set(p
, bset
);
3195 p
= isl_printer_end_line(p
);
3196 isl_printer_free(p
);
3199 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3200 FILE *out
, int indent
)
3205 fprintf(out
, "null basic map\n");
3209 fprintf(out
, "%*s", indent
, "");
3210 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3211 "flags: %x, n_name: %d\n",
3213 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3214 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3216 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3217 p
= isl_printer_set_dump(p
, 1);
3218 p
= isl_printer_set_indent(p
, indent
);
3219 p
= isl_printer_start_line(p
);
3220 p
= isl_printer_print_basic_map(p
, bmap
);
3221 p
= isl_printer_end_line(p
);
3222 isl_printer_free(p
);
3225 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3232 total
= isl_basic_map_total_dim(bmap
);
3233 if (pos
>= bmap
->n_ineq
)
3234 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3235 "invalid position", return isl_basic_map_free(bmap
));
3236 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3237 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3238 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3239 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3243 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3246 if (isl_space_check_is_set(space
) < 0)
3248 return isl_map_alloc_space(space
, n
, flags
);
3250 isl_space_free(space
);
3254 /* Make sure "map" has room for at least "n" more basic maps.
3256 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3259 struct isl_map
*grown
= NULL
;
3263 isl_assert(map
->ctx
, n
>= 0, goto error
);
3264 if (map
->n
+ n
<= map
->size
)
3266 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3269 for (i
= 0; i
< map
->n
; ++i
) {
3270 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3278 isl_map_free(grown
);
3283 /* Make sure "set" has room for at least "n" more basic sets.
3285 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3287 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3290 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3292 return isl_map_from_basic_map(bset
);
3295 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3297 struct isl_map
*map
;
3302 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3303 return isl_map_add_basic_map(map
, bmap
);
3306 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3307 __isl_take isl_basic_set
*bset
)
3309 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3310 bset_to_bmap(bset
)));
3313 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3315 return isl_map_free(set
);
3318 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3323 fprintf(out
, "null set\n");
3327 fprintf(out
, "%*s", indent
, "");
3328 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3329 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3331 for (i
= 0; i
< set
->n
; ++i
) {
3332 fprintf(out
, "%*s", indent
, "");
3333 fprintf(out
, "basic set %d:\n", i
);
3334 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3338 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3343 fprintf(out
, "null map\n");
3347 fprintf(out
, "%*s", indent
, "");
3348 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3349 "flags: %x, n_name: %d\n",
3350 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3351 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3352 for (i
= 0; i
< map
->n
; ++i
) {
3353 fprintf(out
, "%*s", indent
, "");
3354 fprintf(out
, "basic map %d:\n", i
);
3355 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3359 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3360 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3362 struct isl_basic_map
*bmap_domain
;
3364 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3367 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0)
3368 isl_assert(bset
->ctx
,
3369 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3371 bmap
= isl_basic_map_cow(bmap
);
3374 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3375 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3376 bmap_domain
= isl_basic_map_from_domain(bset
);
3377 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3379 bmap
= isl_basic_map_simplify(bmap
);
3380 return isl_basic_map_finalize(bmap
);
3382 isl_basic_map_free(bmap
);
3383 isl_basic_set_free(bset
);
3387 /* Check that the space of "bset" is the same as that of the range of "bmap".
3389 static isl_stat
isl_basic_map_check_compatible_range(
3390 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3394 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3396 return isl_stat_error
;
3398 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3399 "incompatible spaces", return isl_stat_error
);
3404 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3405 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3407 struct isl_basic_map
*bmap_range
;
3409 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3412 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0 &&
3413 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3416 if (isl_basic_set_plain_is_universe(bset
)) {
3417 isl_basic_set_free(bset
);
3421 bmap
= isl_basic_map_cow(bmap
);
3424 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3425 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3426 bmap_range
= bset_to_bmap(bset
);
3427 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3429 bmap
= isl_basic_map_simplify(bmap
);
3430 return isl_basic_map_finalize(bmap
);
3432 isl_basic_map_free(bmap
);
3433 isl_basic_set_free(bset
);
3437 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3438 __isl_keep isl_vec
*vec
)
3445 return isl_bool_error
;
3447 total
= 1 + isl_basic_map_total_dim(bmap
);
3448 if (total
!= vec
->size
)
3449 return isl_bool_false
;
3453 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3454 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3455 if (!isl_int_is_zero(s
)) {
3457 return isl_bool_false
;
3461 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3462 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3463 if (isl_int_is_neg(s
)) {
3465 return isl_bool_false
;
3471 return isl_bool_true
;
3474 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3475 __isl_keep isl_vec
*vec
)
3477 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3480 __isl_give isl_basic_map
*isl_basic_map_intersect(
3481 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3483 struct isl_vec
*sample
= NULL
;
3484 isl_space
*space1
, *space2
;
3486 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3488 space1
= isl_basic_map_peek_space(bmap1
);
3489 space2
= isl_basic_map_peek_space(bmap2
);
3490 if (isl_space_dim(space1
, isl_dim_all
) ==
3491 isl_space_dim(space1
, isl_dim_param
) &&
3492 isl_space_dim(space2
, isl_dim_all
) !=
3493 isl_space_dim(space2
, isl_dim_param
))
3494 return isl_basic_map_intersect(bmap2
, bmap1
);
3496 if (isl_space_dim(space2
, isl_dim_all
) !=
3497 isl_space_dim(space2
, isl_dim_param
))
3498 isl_assert(bmap1
->ctx
,
3499 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3501 if (isl_basic_map_plain_is_empty(bmap1
)) {
3502 isl_basic_map_free(bmap2
);
3505 if (isl_basic_map_plain_is_empty(bmap2
)) {
3506 isl_basic_map_free(bmap1
);
3510 if (bmap1
->sample
&&
3511 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3512 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3513 sample
= isl_vec_copy(bmap1
->sample
);
3514 else if (bmap2
->sample
&&
3515 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3516 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3517 sample
= isl_vec_copy(bmap2
->sample
);
3519 bmap1
= isl_basic_map_cow(bmap1
);
3522 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3523 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3524 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3527 isl_vec_free(sample
);
3529 isl_vec_free(bmap1
->sample
);
3530 bmap1
->sample
= sample
;
3533 bmap1
= isl_basic_map_simplify(bmap1
);
3534 return isl_basic_map_finalize(bmap1
);
3537 isl_vec_free(sample
);
3538 isl_basic_map_free(bmap1
);
3539 isl_basic_map_free(bmap2
);
3543 struct isl_basic_set
*isl_basic_set_intersect(
3544 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3546 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3547 bset_to_bmap(bset2
)));
3550 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3551 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3553 return isl_basic_set_intersect(bset1
, bset2
);
3556 /* Special case of isl_map_intersect, where both map1 and map2
3557 * are convex, without any divs and such that either map1 or map2
3558 * contains a single constraint. This constraint is then simply
3559 * added to the other map.
3561 static __isl_give isl_map
*map_intersect_add_constraint(
3562 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3564 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3565 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3566 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3567 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3569 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3570 return isl_map_intersect(map2
, map1
);
3572 map1
= isl_map_cow(map1
);
3575 if (isl_map_plain_is_empty(map1
)) {
3579 if (map2
->p
[0]->n_eq
== 1)
3580 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3582 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3583 map2
->p
[0]->ineq
[0]);
3585 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3586 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3590 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3591 isl_basic_map_free(map1
->p
[0]);
3597 map1
= isl_map_unmark_normalized(map1
);
3605 /* map2 may be either a parameter domain or a map living in the same
3608 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3609 __isl_take isl_map
*map2
)
3619 if ((isl_map_plain_is_empty(map1
) ||
3620 isl_map_plain_is_universe(map2
)) &&
3621 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3625 if ((isl_map_plain_is_empty(map2
) ||
3626 isl_map_plain_is_universe(map1
)) &&
3627 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3632 if (map1
->n
== 1 && map2
->n
== 1 &&
3633 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3634 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3635 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3636 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3637 return map_intersect_add_constraint(map1
, map2
);
3639 equal
= isl_map_plain_is_equal(map1
, map2
);
3647 if (isl_map_dim(map2
, isl_dim_all
) != isl_map_dim(map2
, isl_dim_param
))
3648 isl_assert(map1
->ctx
,
3649 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3651 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3652 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3653 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3655 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3656 map1
->n
* map2
->n
, flags
);
3659 for (i
= 0; i
< map1
->n
; ++i
)
3660 for (j
= 0; j
< map2
->n
; ++j
) {
3661 struct isl_basic_map
*part
;
3662 part
= isl_basic_map_intersect(
3663 isl_basic_map_copy(map1
->p
[i
]),
3664 isl_basic_map_copy(map2
->p
[j
]));
3665 if (isl_basic_map_is_empty(part
) < 0)
3666 part
= isl_basic_map_free(part
);
3667 result
= isl_map_add_basic_map(result
, part
);
3680 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3681 __isl_take isl_map
*map2
)
3685 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3686 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3687 "spaces don't match", goto error
);
3688 return map_intersect_internal(map1
, map2
);
3695 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3696 __isl_take isl_map
*map2
)
3698 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3701 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3703 return set_from_map(isl_map_intersect(set_to_map(set1
),
3707 /* map_intersect_internal accepts intersections
3708 * with parameter domains, so we can just call that function.
3710 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3711 __isl_take isl_set
*params
)
3713 return map_intersect_internal(map
, params
);
3716 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3717 __isl_take isl_map
*map2
)
3719 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3722 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3723 __isl_take isl_set
*params
)
3725 return isl_map_intersect_params(set
, params
);
3728 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3731 unsigned pos
, n1
, n2
;
3735 bmap
= isl_basic_map_cow(bmap
);
3738 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3739 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3740 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3741 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3742 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3743 return isl_basic_map_reset_space(bmap
, space
);
3746 static __isl_give isl_basic_map
*basic_map_space_reset(
3747 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3753 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3756 space
= isl_basic_map_get_space(bmap
);
3757 space
= isl_space_reset(space
, type
);
3758 bmap
= isl_basic_map_reset_space(bmap
, space
);
3762 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3763 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3764 unsigned pos
, unsigned n
)
3766 isl_bool rational
, is_empty
;
3767 isl_space
*res_space
;
3768 struct isl_basic_map
*res
;
3769 struct isl_dim_map
*dim_map
;
3770 unsigned total
, off
;
3771 enum isl_dim_type t
;
3774 return basic_map_space_reset(bmap
, type
);
3776 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3778 return isl_basic_map_free(bmap
);
3779 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3782 return isl_basic_map_free(bmap
);
3784 isl_basic_map_free(bmap
);
3785 return isl_basic_map_empty(res_space
);
3788 total
= isl_basic_map_total_dim(bmap
) + n
;
3789 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3791 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3793 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3795 unsigned size
= isl_basic_map_dim(bmap
, t
);
3796 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3798 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3799 pos
, size
- pos
, off
+ pos
+ n
);
3801 off
+= isl_space_dim(res_space
, t
);
3803 isl_dim_map_div(dim_map
, bmap
, off
);
3805 res
= isl_basic_map_alloc_space(res_space
,
3806 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3807 rational
= isl_basic_map_is_rational(bmap
);
3809 res
= isl_basic_map_free(res
);
3811 res
= isl_basic_map_set_rational(res
);
3812 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3813 return isl_basic_map_finalize(res
);
3816 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3817 __isl_take isl_basic_set
*bset
,
3818 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3820 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3823 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3824 enum isl_dim_type type
, unsigned n
)
3828 return isl_basic_map_insert_dims(bmap
, type
,
3829 isl_basic_map_dim(bmap
, type
), n
);
3832 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3833 enum isl_dim_type type
, unsigned n
)
3837 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3838 return isl_basic_map_add_dims(bset
, type
, n
);
3840 isl_basic_set_free(bset
);
3844 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3845 enum isl_dim_type type
)
3849 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3852 space
= isl_map_get_space(map
);
3853 space
= isl_space_reset(space
, type
);
3854 map
= isl_map_reset_space(map
, space
);
3858 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3859 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3864 return map_space_reset(map
, type
);
3866 map
= isl_map_cow(map
);
3870 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3874 for (i
= 0; i
< map
->n
; ++i
) {
3875 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3886 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3887 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3889 return isl_map_insert_dims(set
, type
, pos
, n
);
3892 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3893 enum isl_dim_type type
, unsigned n
)
3897 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3900 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3901 enum isl_dim_type type
, unsigned n
)
3905 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3906 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3912 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3913 __isl_take isl_basic_map
*bmap
,
3914 enum isl_dim_type dst_type
, unsigned dst_pos
,
3915 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3918 struct isl_dim_map
*dim_map
;
3919 struct isl_basic_map
*res
;
3920 enum isl_dim_type t
;
3921 unsigned total
, off
;
3926 bmap
= isl_basic_map_reset(bmap
, src_type
);
3927 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3931 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3932 return isl_basic_map_free(bmap
);
3934 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3937 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3939 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3940 pos(bmap
->dim
, src_type
) + src_pos
+
3941 ((src_type
< dst_type
) ? n
: 0)) {
3942 bmap
= isl_basic_map_cow(bmap
);
3946 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3947 src_type
, src_pos
, n
);
3951 bmap
= isl_basic_map_finalize(bmap
);
3956 total
= isl_basic_map_total_dim(bmap
);
3957 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3960 space
= isl_basic_map_peek_space(bmap
);
3961 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3962 unsigned size
= isl_space_dim(space
, t
);
3963 if (t
== dst_type
) {
3964 isl_dim_map_dim_range(dim_map
, space
, t
,
3967 isl_dim_map_dim_range(dim_map
, space
, src_type
,
3970 isl_dim_map_dim_range(dim_map
, space
, t
,
3971 dst_pos
, size
- dst_pos
, off
);
3972 off
+= size
- dst_pos
;
3973 } else if (t
== src_type
) {
3974 isl_dim_map_dim_range(dim_map
, space
, t
,
3977 isl_dim_map_dim_range(dim_map
, space
, t
,
3978 src_pos
+ n
, size
- src_pos
- n
, off
);
3979 off
+= size
- src_pos
- n
;
3981 isl_dim_map_dim(dim_map
, space
, t
, off
);
3985 isl_dim_map_div(dim_map
, bmap
, off
);
3987 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3988 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3989 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3993 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3994 src_type
, src_pos
, n
);
3998 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3999 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4000 bmap
= isl_basic_map_finalize(bmap
);
4004 isl_basic_map_free(bmap
);
4008 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4009 enum isl_dim_type dst_type
, unsigned dst_pos
,
4010 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4012 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4013 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4014 src_type
, src_pos
, n
);
4015 return bset_from_bmap(bmap
);
4018 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4019 enum isl_dim_type dst_type
, unsigned dst_pos
,
4020 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4024 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4025 return set_from_map(isl_map_move_dims(set_to_map(set
),
4026 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4032 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4033 enum isl_dim_type dst_type
, unsigned dst_pos
,
4034 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4039 map
= isl_map_reset(map
, src_type
);
4040 map
= isl_map_reset(map
, dst_type
);
4044 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4045 return isl_map_free(map
);
4047 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4050 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4052 map
= isl_map_cow(map
);
4056 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4060 for (i
= 0; i
< map
->n
; ++i
) {
4061 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4063 src_type
, src_pos
, n
);
4074 /* Move the specified dimensions to the last columns right before
4075 * the divs. Don't change the dimension specification of bmap.
4076 * That's the responsibility of the caller.
4078 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4079 enum isl_dim_type type
, unsigned first
, unsigned n
)
4082 struct isl_dim_map
*dim_map
;
4083 struct isl_basic_map
*res
;
4084 enum isl_dim_type t
;
4085 unsigned total
, off
;
4089 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4090 isl_basic_map_offset(bmap
, isl_dim_div
))
4093 total
= isl_basic_map_total_dim(bmap
);
4094 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4097 space
= isl_basic_map_peek_space(bmap
);
4098 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4099 unsigned size
= isl_space_dim(space
, t
);
4101 isl_dim_map_dim_range(dim_map
, space
, t
,
4104 isl_dim_map_dim_range(dim_map
, space
, t
,
4105 first
, n
, total
- bmap
->n_div
- n
);
4106 isl_dim_map_dim_range(dim_map
, space
, t
,
4107 first
+ n
, size
- (first
+ n
), off
);
4108 off
+= size
- (first
+ n
);
4110 isl_dim_map_dim(dim_map
, space
, t
, off
);
4114 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4116 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4117 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4118 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4122 /* Insert "n" rows in the divs of "bmap".
4124 * The number of columns is not changed, which means that the last
4125 * dimensions of "bmap" are being reintepreted as the new divs.
4126 * The space of "bmap" is not adjusted, however, which means
4127 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4128 * from the space of "bmap" is the responsibility of the caller.
4130 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4138 bmap
= isl_basic_map_cow(bmap
);
4142 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4143 old
= bmap
->block2
.data
;
4144 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4145 (bmap
->extra
+ n
) * (1 + row_size
));
4146 if (!bmap
->block2
.data
)
4147 return isl_basic_map_free(bmap
);
4148 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4150 return isl_basic_map_free(bmap
);
4151 for (i
= 0; i
< n
; ++i
) {
4152 new_div
[i
] = bmap
->block2
.data
+
4153 (bmap
->extra
+ i
) * (1 + row_size
);
4154 isl_seq_clr(new_div
[i
], 1 + row_size
);
4156 for (i
= 0; i
< bmap
->extra
; ++i
)
4157 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4159 bmap
->div
= new_div
;
4166 /* Drop constraints from "bmap" that only involve the variables
4167 * of "type" in the range [first, first + n] that are not related
4168 * to any of the variables outside that interval.
4169 * These constraints cannot influence the values for the variables
4170 * outside the interval, except in case they cause "bmap" to be empty.
4171 * Only drop the constraints if "bmap" is known to be non-empty.
4173 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4174 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4175 unsigned first
, unsigned n
)
4179 unsigned dim
, n_div
;
4182 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4184 return isl_basic_map_free(bmap
);
4188 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4189 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4190 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4192 return isl_basic_map_free(bmap
);
4193 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4194 for (i
= 0; i
< first
; ++i
)
4196 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4199 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4204 /* Turn the n dimensions of type type, starting at first
4205 * into existentially quantified variables.
4207 * If a subset of the projected out variables are unrelated
4208 * to any of the variables that remain, then the constraints
4209 * involving this subset are simply dropped first.
4211 __isl_give isl_basic_map
*isl_basic_map_project_out(
4212 __isl_take isl_basic_map
*bmap
,
4213 enum isl_dim_type type
, unsigned first
, unsigned n
)
4218 return basic_map_space_reset(bmap
, type
);
4219 if (type
== isl_dim_div
)
4220 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4221 "cannot project out existentially quantified variables",
4222 return isl_basic_map_free(bmap
));
4224 empty
= isl_basic_map_plain_is_empty(bmap
);
4226 return isl_basic_map_free(bmap
);
4228 bmap
= isl_basic_map_set_to_empty(bmap
);
4230 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4234 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4235 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4237 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4238 return isl_basic_map_free(bmap
);
4240 bmap
= move_last(bmap
, type
, first
, n
);
4241 bmap
= isl_basic_map_cow(bmap
);
4242 bmap
= insert_div_rows(bmap
, n
);
4246 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4249 bmap
= isl_basic_map_simplify(bmap
);
4250 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4251 return isl_basic_map_finalize(bmap
);
4253 isl_basic_map_free(bmap
);
4257 /* Turn the n dimensions of type type, starting at first
4258 * into existentially quantified variables.
4260 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4261 enum isl_dim_type type
, unsigned first
, unsigned n
)
4263 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4267 /* Turn the n dimensions of type type, starting at first
4268 * into existentially quantified variables.
4270 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4271 enum isl_dim_type type
, unsigned first
, unsigned n
)
4276 return map_space_reset(map
, type
);
4278 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4279 return isl_map_free(map
);
4281 map
= isl_map_cow(map
);
4285 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4289 for (i
= 0; i
< map
->n
; ++i
) {
4290 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4301 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4302 * into existentially quantified variables.
4304 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4305 enum isl_dim_type type
, unsigned first
, unsigned n
)
4309 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4310 return isl_map_free(map
);
4311 dim
= isl_map_dim(map
, type
);
4312 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4313 map
= isl_map_project_out(map
, type
, 0, first
);
4317 /* Turn the n dimensions of type type, starting at first
4318 * into existentially quantified variables.
4320 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4321 enum isl_dim_type type
, unsigned first
, unsigned n
)
4323 return set_from_map(isl_map_project_out(set_to_map(set
),
4327 /* Return a map that projects the elements in "set" onto their
4328 * "n" set dimensions starting at "first".
4329 * "type" should be equal to isl_dim_set.
4331 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4332 enum isl_dim_type type
, unsigned first
, unsigned n
)
4337 if (type
!= isl_dim_set
)
4338 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4339 "only set dimensions can be projected out", goto error
);
4340 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4341 return isl_set_free(set
);
4343 map
= isl_map_from_domain(set
);
4344 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4345 for (i
= 0; i
< n
; ++i
)
4346 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4354 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4359 for (i
= 0; i
< n
; ++i
) {
4360 j
= isl_basic_map_alloc_div(bmap
);
4363 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4367 isl_basic_map_free(bmap
);
4371 struct isl_basic_map
*isl_basic_map_apply_range(
4372 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4374 isl_space
*space_result
= NULL
;
4375 struct isl_basic_map
*bmap
;
4376 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4377 struct isl_dim_map
*dim_map1
, *dim_map2
;
4379 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4381 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4382 bmap2
->dim
, isl_dim_in
))
4383 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4384 "spaces don't match", goto error
);
4386 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4387 isl_basic_map_get_space(bmap2
));
4389 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4390 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4391 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4392 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4394 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4395 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4396 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4397 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4398 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4399 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4400 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4401 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4402 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4403 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4404 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4406 bmap
= isl_basic_map_alloc_space(space_result
,
4407 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4408 bmap1
->n_eq
+ bmap2
->n_eq
,
4409 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4410 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4411 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4412 bmap
= add_divs(bmap
, n
);
4413 bmap
= isl_basic_map_simplify(bmap
);
4414 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4415 return isl_basic_map_finalize(bmap
);
4417 isl_basic_map_free(bmap1
);
4418 isl_basic_map_free(bmap2
);
4422 struct isl_basic_set
*isl_basic_set_apply(
4423 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4428 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4431 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4434 isl_basic_set_free(bset
);
4435 isl_basic_map_free(bmap
);
4439 struct isl_basic_map
*isl_basic_map_apply_domain(
4440 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4442 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4444 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4445 bmap2
->dim
, isl_dim_in
))
4446 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4447 "spaces don't match", goto error
);
4449 bmap1
= isl_basic_map_reverse(bmap1
);
4450 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4451 return isl_basic_map_reverse(bmap1
);
4453 isl_basic_map_free(bmap1
);
4454 isl_basic_map_free(bmap2
);
4458 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4459 * A \cap B -> f(A) + f(B)
4461 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4462 __isl_take isl_basic_map
*bmap2
)
4464 unsigned n_in
, n_out
, nparam
, total
, pos
;
4465 struct isl_basic_map
*bmap
= NULL
;
4466 struct isl_dim_map
*dim_map1
, *dim_map2
;
4469 if (!bmap1
|| !bmap2
)
4472 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4475 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4476 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4477 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4479 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4480 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4481 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4482 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4483 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4486 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4487 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4488 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4489 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4491 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4492 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4493 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4494 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4495 for (i
= 0; i
< n_out
; ++i
) {
4496 int j
= isl_basic_map_alloc_equality(bmap
);
4499 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4500 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4501 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4502 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4504 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4505 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4506 bmap
= add_divs(bmap
, 2 * n_out
);
4508 bmap
= isl_basic_map_simplify(bmap
);
4509 return isl_basic_map_finalize(bmap
);
4511 isl_basic_map_free(bmap
);
4512 isl_basic_map_free(bmap1
);
4513 isl_basic_map_free(bmap2
);
4517 /* Given two maps A -> f(A) and B -> g(B), construct a map
4518 * A \cap B -> f(A) + f(B)
4520 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4521 __isl_take isl_map
*map2
)
4523 struct isl_map
*result
;
4529 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4531 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4532 map1
->n
* map2
->n
, 0);
4535 for (i
= 0; i
< map1
->n
; ++i
)
4536 for (j
= 0; j
< map2
->n
; ++j
) {
4537 struct isl_basic_map
*part
;
4538 part
= isl_basic_map_sum(
4539 isl_basic_map_copy(map1
->p
[i
]),
4540 isl_basic_map_copy(map2
->p
[j
]));
4541 if (isl_basic_map_is_empty(part
))
4542 isl_basic_map_free(part
);
4544 result
= isl_map_add_basic_map(result
, part
);
4557 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4558 __isl_take isl_set
*set2
)
4560 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4563 /* Given a basic map A -> f(A), construct A -> -f(A).
4565 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4570 bmap
= isl_basic_map_cow(bmap
);
4574 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4575 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4576 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4577 for (j
= 0; j
< n
; ++j
)
4578 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4579 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4580 for (j
= 0; j
< n
; ++j
)
4581 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4582 for (i
= 0; i
< bmap
->n_div
; ++i
)
4583 for (j
= 0; j
< n
; ++j
)
4584 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4585 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4586 return isl_basic_map_finalize(bmap
);
4589 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4591 return isl_basic_map_neg(bset
);
4594 /* Given a map A -> f(A), construct A -> -f(A).
4596 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4600 map
= isl_map_cow(map
);
4604 for (i
= 0; i
< map
->n
; ++i
) {
4605 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4616 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4618 return set_from_map(isl_map_neg(set_to_map(set
)));
4621 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4622 * A -> floor(f(A)/d).
4624 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4627 unsigned n_in
, n_out
, nparam
, total
, pos
;
4628 struct isl_basic_map
*result
= NULL
;
4629 struct isl_dim_map
*dim_map
;
4635 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4636 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4637 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4639 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4640 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4641 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4642 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4643 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4644 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4646 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4647 bmap
->n_div
+ n_out
,
4648 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4649 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4650 result
= add_divs(result
, n_out
);
4651 for (i
= 0; i
< n_out
; ++i
) {
4653 j
= isl_basic_map_alloc_inequality(result
);
4656 isl_seq_clr(result
->ineq
[j
], 1+total
);
4657 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4658 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4659 j
= isl_basic_map_alloc_inequality(result
);
4662 isl_seq_clr(result
->ineq
[j
], 1+total
);
4663 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4664 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4665 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4668 result
= isl_basic_map_simplify(result
);
4669 return isl_basic_map_finalize(result
);
4671 isl_basic_map_free(result
);
4675 /* Given a map A -> f(A) and an integer d, construct a map
4676 * A -> floor(f(A)/d).
4678 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4682 map
= isl_map_cow(map
);
4686 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4687 for (i
= 0; i
< map
->n
; ++i
) {
4688 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4692 map
= isl_map_unmark_normalized(map
);
4700 /* Given a map A -> f(A) and an integer d, construct a map
4701 * A -> floor(f(A)/d).
4703 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4704 __isl_take isl_val
*d
)
4708 if (!isl_val_is_int(d
))
4709 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4710 "expecting integer denominator", goto error
);
4711 map
= isl_map_floordiv(map
, d
->n
);
4720 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4727 i
= isl_basic_map_alloc_equality(bmap
);
4730 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4731 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4732 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4733 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4734 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4735 return isl_basic_map_finalize(bmap
);
4737 isl_basic_map_free(bmap
);
4741 /* Add a constraint to "bmap" expressing i_pos < o_pos
4743 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4750 i
= isl_basic_map_alloc_inequality(bmap
);
4753 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4754 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4755 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4756 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4757 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4758 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4759 return isl_basic_map_finalize(bmap
);
4761 isl_basic_map_free(bmap
);
4765 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4767 static __isl_give isl_basic_map
*var_less_or_equal(
4768 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4774 i
= isl_basic_map_alloc_inequality(bmap
);
4777 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4778 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4779 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4780 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4781 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4782 return isl_basic_map_finalize(bmap
);
4784 isl_basic_map_free(bmap
);
4788 /* Add a constraint to "bmap" expressing i_pos > o_pos
4790 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4797 i
= isl_basic_map_alloc_inequality(bmap
);
4800 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4801 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4802 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4803 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4804 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4805 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4806 return isl_basic_map_finalize(bmap
);
4808 isl_basic_map_free(bmap
);
4812 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4814 static __isl_give isl_basic_map
*var_more_or_equal(
4815 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4821 i
= isl_basic_map_alloc_inequality(bmap
);
4824 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4825 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4826 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4827 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4828 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4829 return isl_basic_map_finalize(bmap
);
4831 isl_basic_map_free(bmap
);
4835 __isl_give isl_basic_map
*isl_basic_map_equal(
4836 __isl_take isl_space
*space
, unsigned n_equal
)
4839 struct isl_basic_map
*bmap
;
4840 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4843 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4844 bmap
= var_equal(bmap
, i
);
4845 return isl_basic_map_finalize(bmap
);
4848 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4850 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4854 struct isl_basic_map
*bmap
;
4855 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4858 for (i
= 0; i
< pos
&& bmap
; ++i
)
4859 bmap
= var_equal(bmap
, i
);
4861 bmap
= var_less(bmap
, pos
);
4862 return isl_basic_map_finalize(bmap
);
4865 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4867 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4868 __isl_take isl_space
*space
, unsigned pos
)
4871 isl_basic_map
*bmap
;
4873 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4874 for (i
= 0; i
< pos
; ++i
)
4875 bmap
= var_equal(bmap
, i
);
4876 bmap
= var_less_or_equal(bmap
, pos
);
4877 return isl_basic_map_finalize(bmap
);
4880 /* Return a relation on "space" expressing i_pos > o_pos
4882 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4886 struct isl_basic_map
*bmap
;
4887 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4890 for (i
= 0; i
< pos
&& bmap
; ++i
)
4891 bmap
= var_equal(bmap
, i
);
4893 bmap
= var_more(bmap
, pos
);
4894 return isl_basic_map_finalize(bmap
);
4897 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4899 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4900 __isl_take isl_space
*space
, unsigned pos
)
4903 isl_basic_map
*bmap
;
4905 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4906 for (i
= 0; i
< pos
; ++i
)
4907 bmap
= var_equal(bmap
, i
);
4908 bmap
= var_more_or_equal(bmap
, pos
);
4909 return isl_basic_map_finalize(bmap
);
4912 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4913 unsigned n
, int equal
)
4915 struct isl_map
*map
;
4918 if (n
== 0 && equal
)
4919 return isl_map_universe(space
);
4921 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4923 for (i
= 0; i
+ 1 < n
; ++i
)
4924 map
= isl_map_add_basic_map(map
,
4925 isl_basic_map_less_at(isl_space_copy(space
), i
));
4928 map
= isl_map_add_basic_map(map
,
4929 isl_basic_map_less_or_equal_at(space
, n
- 1));
4931 map
= isl_map_add_basic_map(map
,
4932 isl_basic_map_less_at(space
, n
- 1));
4934 isl_space_free(space
);
4939 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
4943 return map_lex_lte_first(space
, space
->n_out
, equal
);
4946 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4948 return map_lex_lte_first(dim
, n
, 0);
4951 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4953 return map_lex_lte_first(dim
, n
, 1);
4956 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4958 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4961 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4963 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4966 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
4967 unsigned n
, int equal
)
4969 struct isl_map
*map
;
4972 if (n
== 0 && equal
)
4973 return isl_map_universe(space
);
4975 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4977 for (i
= 0; i
+ 1 < n
; ++i
)
4978 map
= isl_map_add_basic_map(map
,
4979 isl_basic_map_more_at(isl_space_copy(space
), i
));
4982 map
= isl_map_add_basic_map(map
,
4983 isl_basic_map_more_or_equal_at(space
, n
- 1));
4985 map
= isl_map_add_basic_map(map
,
4986 isl_basic_map_more_at(space
, n
- 1));
4988 isl_space_free(space
);
4993 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
4997 return map_lex_gte_first(space
, space
->n_out
, equal
);
5000 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5002 return map_lex_gte_first(dim
, n
, 0);
5005 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5007 return map_lex_gte_first(dim
, n
, 1);
5010 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5012 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5015 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5017 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5020 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5021 __isl_take isl_set
*set2
)
5024 map
= isl_map_lex_le(isl_set_get_space(set1
));
5025 map
= isl_map_intersect_domain(map
, set1
);
5026 map
= isl_map_intersect_range(map
, set2
);
5030 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5031 __isl_take isl_set
*set2
)
5034 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5035 map
= isl_map_intersect_domain(map
, set1
);
5036 map
= isl_map_intersect_range(map
, set2
);
5040 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5041 __isl_take isl_set
*set2
)
5044 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5045 map
= isl_map_intersect_domain(map
, set1
);
5046 map
= isl_map_intersect_range(map
, set2
);
5050 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5051 __isl_take isl_set
*set2
)
5054 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5055 map
= isl_map_intersect_domain(map
, set1
);
5056 map
= isl_map_intersect_range(map
, set2
);
5060 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5061 __isl_take isl_map
*map2
)
5064 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5065 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5066 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5070 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5071 __isl_take isl_map
*map2
)
5074 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5075 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5076 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5080 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5081 __isl_take isl_map
*map2
)
5084 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5085 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5086 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5090 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5091 __isl_take isl_map
*map2
)
5094 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5095 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5096 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5100 /* For the div d = floor(f/m) at position "div", add the constraint
5104 static __isl_give isl_basic_map
*add_upper_div_constraint(
5105 __isl_take isl_basic_map
*bmap
, unsigned div
)
5108 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5109 unsigned n_div
, pos
;
5111 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5113 return isl_basic_map_free(bmap
);
5115 i
= isl_basic_map_alloc_inequality(bmap
);
5117 return isl_basic_map_free(bmap
);
5118 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5119 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5124 /* For the div d = floor(f/m) at position "div", add the constraint
5126 * -(f-(m-1)) + m d >= 0
5128 static __isl_give isl_basic_map
*add_lower_div_constraint(
5129 __isl_take isl_basic_map
*bmap
, unsigned div
)
5132 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5133 unsigned n_div
, pos
;
5135 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5137 return isl_basic_map_free(bmap
);
5139 i
= isl_basic_map_alloc_inequality(bmap
);
5141 return isl_basic_map_free(bmap
);
5142 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5143 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5144 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5145 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5150 /* For the div d = floor(f/m) at position "pos", add the constraints
5153 * -(f-(m-1)) + m d >= 0
5155 * Note that the second constraint is the negation of
5159 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5160 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5162 bmap
= add_upper_div_constraint(bmap
, pos
);
5163 bmap
= add_lower_div_constraint(bmap
, pos
);
5167 /* For each known div d = floor(f/m), add the constraints
5170 * -(f-(m-1)) + m d >= 0
5172 * Remove duplicate constraints in case of some these div constraints
5173 * already appear in "bmap".
5175 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5176 __isl_take isl_basic_map
*bmap
)
5182 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5186 bmap
= add_known_div_constraints(bmap
);
5187 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5188 bmap
= isl_basic_map_finalize(bmap
);
5192 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5194 * In particular, if this div is of the form d = floor(f/m),
5195 * then add the constraint
5199 * if sign < 0 or the constraint
5201 * -(f-(m-1)) + m d >= 0
5205 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5206 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5209 return add_upper_div_constraint(bmap
, div
);
5211 return add_lower_div_constraint(bmap
, div
);
5214 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5215 __isl_take isl_basic_map
*bmap
)
5219 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5221 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5222 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5223 return bset_from_bmap(bmap
);
5224 bmap
= isl_basic_map_cow(bmap
);
5227 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5230 bmap
->extra
-= bmap
->n_div
;
5232 bmap
= isl_basic_map_finalize(bmap
);
5233 return bset_from_bmap(bmap
);
5235 isl_basic_map_free(bmap
);
5239 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5240 __isl_take isl_basic_set
*bset
)
5242 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5245 /* Replace each element in "list" by the result of applying
5246 * isl_basic_map_underlying_set to the element.
5248 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5249 __isl_take isl_basic_map_list
*list
)
5256 n
= isl_basic_map_list_n_basic_map(list
);
5257 for (i
= 0; i
< n
; ++i
) {
5258 isl_basic_map
*bmap
;
5259 isl_basic_set
*bset
;
5261 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5262 bset
= isl_basic_set_underlying_set(bmap
);
5263 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5269 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5270 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5272 struct isl_basic_map
*bmap
;
5273 struct isl_ctx
*ctx
;
5274 unsigned dim
, total
;
5280 if (isl_basic_set_check_no_params(bset
) < 0 ||
5281 isl_basic_set_check_no_locals(bset
) < 0)
5283 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5284 isl_assert(ctx
, dim
== isl_basic_map_total_dim(like
), goto error
);
5285 if (like
->n_div
== 0) {
5286 isl_space
*space
= isl_basic_map_get_space(like
);
5287 isl_basic_map_free(like
);
5288 return isl_basic_map_reset_space(bset
, space
);
5290 bset
= isl_basic_set_cow(bset
);
5293 total
= dim
+ bset
->extra
;
5294 bmap
= bset_to_bmap(bset
);
5295 isl_space_free(bmap
->dim
);
5296 bmap
->dim
= isl_space_copy(like
->dim
);
5299 bmap
->n_div
= like
->n_div
;
5300 bmap
->extra
+= like
->n_div
;
5304 ltotal
= total
- bmap
->extra
+ like
->extra
;
5307 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5308 bmap
->extra
* (1 + 1 + total
));
5309 if (isl_blk_is_error(bmap
->block2
))
5311 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5315 for (i
= 0; i
< bmap
->extra
; ++i
)
5316 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5317 for (i
= 0; i
< like
->n_div
; ++i
) {
5318 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5319 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5321 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5323 isl_basic_map_free(like
);
5324 bmap
= isl_basic_map_simplify(bmap
);
5325 bmap
= isl_basic_map_finalize(bmap
);
5328 isl_basic_map_free(like
);
5329 isl_basic_set_free(bset
);
5333 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5334 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5336 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5337 bset_to_bmap(like
)));
5340 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5344 map
= isl_map_cow(map
);
5347 map
->dim
= isl_space_cow(map
->dim
);
5351 for (i
= 1; i
< map
->n
; ++i
)
5352 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5354 for (i
= 0; i
< map
->n
; ++i
) {
5355 map
->p
[i
] = bset_to_bmap(
5356 isl_basic_map_underlying_set(map
->p
[i
]));
5361 map
->dim
= isl_space_underlying(map
->dim
, 0);
5363 isl_space_free(map
->dim
);
5364 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5368 return set_from_map(map
);
5374 /* Replace the space of "bmap" by "space".
5376 * If the space of "bmap" is identical to "space" (including the identifiers
5377 * of the input and output dimensions), then simply return the original input.
5379 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5380 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5383 isl_space
*bmap_space
;
5385 bmap_space
= isl_basic_map_peek_space(bmap
);
5386 equal
= isl_space_is_equal(bmap_space
, space
);
5387 if (equal
>= 0 && equal
)
5388 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5392 isl_space_free(space
);
5395 bmap
= isl_basic_map_cow(bmap
);
5396 if (!bmap
|| !space
)
5399 isl_space_free(bmap
->dim
);
5402 bmap
= isl_basic_map_finalize(bmap
);
5406 isl_basic_map_free(bmap
);
5407 isl_space_free(space
);
5411 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5412 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5414 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5418 /* Check that the total dimensions of "map" and "space" are the same.
5420 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5421 __isl_keep isl_space
*space
)
5423 unsigned dim1
, dim2
;
5426 return isl_stat_error
;
5427 dim1
= isl_map_dim(map
, isl_dim_all
);
5428 dim2
= isl_space_dim(space
, isl_dim_all
);
5431 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5432 "total dimensions do not match", return isl_stat_error
);
5435 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5436 __isl_take isl_space
*space
)
5440 map
= isl_map_cow(map
);
5444 for (i
= 0; i
< map
->n
; ++i
) {
5445 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5446 isl_space_copy(space
));
5450 isl_space_free(map
->dim
);
5456 isl_space_free(space
);
5460 /* Replace the space of "map" by "space", without modifying
5461 * the dimension of "map".
5463 * If the space of "map" is identical to "space" (including the identifiers
5464 * of the input and output dimensions), then simply return the original input.
5466 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5467 __isl_take isl_space
*space
)
5470 isl_space
*map_space
;
5472 map_space
= isl_map_peek_space(map
);
5473 equal
= isl_space_is_equal(map_space
, space
);
5474 if (equal
>= 0 && equal
)
5475 equal
= isl_space_has_equal_ids(map_space
, space
);
5479 isl_space_free(space
);
5482 if (check_map_space_equal_total_dim(map
, space
) < 0)
5484 return isl_map_reset_space(map
, space
);
5487 isl_space_free(space
);
5491 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5492 __isl_take isl_space
*dim
)
5494 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5497 /* Compute the parameter domain of the given basic set.
5499 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5505 is_params
= isl_basic_set_is_params(bset
);
5507 return isl_basic_set_free(bset
);
5511 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5512 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5513 space
= isl_basic_set_get_space(bset
);
5514 space
= isl_space_params(space
);
5515 bset
= isl_basic_set_reset_space(bset
, space
);
5519 /* Construct a zero-dimensional basic set with the given parameter domain.
5521 __isl_give isl_basic_set
*isl_basic_set_from_params(
5522 __isl_take isl_basic_set
*bset
)
5525 space
= isl_basic_set_get_space(bset
);
5526 space
= isl_space_set_from_params(space
);
5527 bset
= isl_basic_set_reset_space(bset
, space
);
5531 /* Compute the parameter domain of the given set.
5533 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5535 return isl_map_params(set_to_map(set
));
5538 /* Construct a zero-dimensional set with the given parameter domain.
5540 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5543 space
= isl_set_get_space(set
);
5544 space
= isl_space_set_from_params(space
);
5545 set
= isl_set_reset_space(set
, space
);
5549 /* Compute the parameter domain of the given map.
5551 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5556 n
= isl_map_dim(map
, isl_dim_in
);
5557 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5558 n
= isl_map_dim(map
, isl_dim_out
);
5559 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5560 space
= isl_map_get_space(map
);
5561 space
= isl_space_params(space
);
5562 map
= isl_map_reset_space(map
, space
);
5566 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5573 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5575 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5576 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5578 return isl_basic_map_reset_space(bmap
, space
);
5581 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5584 return isl_bool_error
;
5585 return isl_space_may_be_set(bmap
->dim
);
5588 /* Is this basic map actually a set?
5589 * Users should never call this function. Outside of isl,
5590 * the type should indicate whether something is a set or a map.
5592 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5595 return isl_bool_error
;
5596 return isl_space_is_set(bmap
->dim
);
5599 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5603 is_set
= isl_basic_map_is_set(bmap
);
5608 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5610 isl_basic_map_free(bmap
);
5614 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5615 __isl_take isl_basic_map
*bmap
)
5619 isl_basic_map
*domain
;
5620 int nparam
, n_in
, n_out
;
5622 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5623 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5624 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5626 space
= isl_basic_map_get_space(bmap
);
5627 space
= isl_space_from_range(isl_space_domain(space
));
5628 domain
= isl_basic_map_universe(space
);
5630 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5631 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5632 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5634 for (i
= 0; i
< n_in
; ++i
)
5635 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5638 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5639 return isl_basic_map_finalize(bmap
);
5642 __isl_give isl_basic_map
*isl_basic_map_range_map(
5643 __isl_take isl_basic_map
*bmap
)
5647 isl_basic_map
*range
;
5648 int nparam
, n_in
, n_out
;
5650 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5651 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5652 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5654 space
= isl_basic_map_get_space(bmap
);
5655 space
= isl_space_from_range(isl_space_range(space
));
5656 range
= isl_basic_map_universe(space
);
5658 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5659 bmap
= isl_basic_map_apply_range(bmap
, range
);
5660 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5662 for (i
= 0; i
< n_out
; ++i
)
5663 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5666 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5667 return isl_basic_map_finalize(bmap
);
5670 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5674 return isl_space_may_be_set(map
->dim
);
5677 /* Is this map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5681 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5684 return isl_bool_error
;
5685 return isl_space_is_set(map
->dim
);
5688 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5692 struct isl_set
*set
;
5694 is_set
= isl_map_is_set(map
);
5698 return set_from_map(map
);
5700 map
= isl_map_cow(map
);
5704 set
= set_from_map(map
);
5705 set
->dim
= isl_space_range(set
->dim
);
5708 for (i
= 0; i
< map
->n
; ++i
) {
5709 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5713 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5714 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5721 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5725 map
= isl_map_cow(map
);
5729 map
->dim
= isl_space_domain_map(map
->dim
);
5732 for (i
= 0; i
< map
->n
; ++i
) {
5733 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5737 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5738 map
= isl_map_unmark_normalized(map
);
5745 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5748 isl_space
*range_dim
;
5750 map
= isl_map_cow(map
);
5754 range_dim
= isl_space_range(isl_map_get_space(map
));
5755 range_dim
= isl_space_from_range(range_dim
);
5756 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5757 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5760 for (i
= 0; i
< map
->n
; ++i
) {
5761 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5765 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5766 map
= isl_map_unmark_normalized(map
);
5773 /* Given a wrapped map of the form A[B -> C],
5774 * return the map A[B -> C] -> B.
5776 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5783 if (!isl_set_has_tuple_id(set
))
5784 return isl_map_domain_map(isl_set_unwrap(set
));
5786 id
= isl_set_get_tuple_id(set
);
5787 map
= isl_map_domain_map(isl_set_unwrap(set
));
5788 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5793 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5794 __isl_take isl_basic_set
*bset
)
5796 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5799 __isl_give isl_basic_map
*isl_basic_map_from_range(
5800 __isl_take isl_basic_set
*bset
)
5803 space
= isl_basic_set_get_space(bset
);
5804 space
= isl_space_from_range(space
);
5805 bset
= isl_basic_set_reset_space(bset
, space
);
5806 return bset_to_bmap(bset
);
5809 /* Create a relation with the given set as range.
5810 * The domain of the created relation is a zero-dimensional
5811 * flat anonymous space.
5813 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5816 space
= isl_set_get_space(set
);
5817 space
= isl_space_from_range(space
);
5818 set
= isl_set_reset_space(set
, space
);
5819 return set_to_map(set
);
5822 /* Create a relation with the given set as domain.
5823 * The range of the created relation is a zero-dimensional
5824 * flat anonymous space.
5826 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5828 return isl_map_reverse(isl_map_from_range(set
));
5831 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5832 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5834 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5837 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5838 __isl_take isl_set
*range
)
5840 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5843 /* Return a newly allocated isl_map with given space and flags and
5844 * room for "n" basic maps.
5845 * Make sure that all cached information is cleared.
5847 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5850 struct isl_map
*map
;
5855 isl_die(space
->ctx
, isl_error_internal
,
5856 "negative number of basic maps", goto error
);
5857 map
= isl_calloc(space
->ctx
, struct isl_map
,
5858 sizeof(struct isl_map
) +
5859 (n
- 1) * sizeof(struct isl_basic_map
*));
5863 map
->ctx
= space
->ctx
;
5864 isl_ctx_ref(map
->ctx
);
5872 isl_space_free(space
);
5876 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5878 struct isl_basic_map
*bmap
;
5879 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5880 bmap
= isl_basic_map_set_to_empty(bmap
);
5884 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5886 struct isl_basic_set
*bset
;
5887 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5888 bset
= isl_basic_set_set_to_empty(bset
);
5892 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5894 struct isl_basic_map
*bmap
;
5895 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5896 bmap
= isl_basic_map_finalize(bmap
);
5900 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5902 struct isl_basic_set
*bset
;
5903 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5904 bset
= isl_basic_set_finalize(bset
);
5908 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
5909 __isl_take isl_space
*space
)
5912 unsigned total
= isl_space_dim(space
, isl_dim_all
);
5913 isl_basic_map
*bmap
;
5915 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
5916 for (i
= 0; i
< total
; ++i
) {
5917 int k
= isl_basic_map_alloc_inequality(bmap
);
5920 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5921 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5925 isl_basic_map_free(bmap
);
5929 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
5930 __isl_take isl_space
*space
)
5932 return isl_basic_map_nat_universe(space
);
5935 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5937 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5940 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5942 return isl_map_nat_universe(dim
);
5945 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
5947 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5950 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
5952 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5955 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
5957 struct isl_map
*map
;
5960 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5961 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
5965 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
5967 struct isl_set
*set
;
5970 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5971 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
5975 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5978 struct isl_map
*dup
;
5982 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5983 for (i
= 0; i
< map
->n
; ++i
)
5984 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5988 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5989 __isl_take isl_basic_map
*bmap
)
5993 if (isl_basic_map_plain_is_empty(bmap
)) {
5994 isl_basic_map_free(bmap
);
5997 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5998 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5999 map
->p
[map
->n
] = bmap
;
6001 map
= isl_map_unmark_normalized(map
);
6007 isl_basic_map_free(bmap
);
6011 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6022 isl_ctx_deref(map
->ctx
);
6023 for (i
= 0; i
< map
->n
; ++i
)
6024 isl_basic_map_free(map
->p
[i
]);
6025 isl_space_free(map
->dim
);
6031 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6032 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6036 bmap
= isl_basic_map_cow(bmap
);
6037 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6038 j
= isl_basic_map_alloc_equality(bmap
);
6041 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6042 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6043 isl_int_set_si(bmap
->eq
[j
][0], value
);
6044 bmap
= isl_basic_map_simplify(bmap
);
6045 return isl_basic_map_finalize(bmap
);
6047 isl_basic_map_free(bmap
);
6051 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6052 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6056 bmap
= isl_basic_map_cow(bmap
);
6057 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6058 j
= isl_basic_map_alloc_equality(bmap
);
6061 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6062 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6063 isl_int_set(bmap
->eq
[j
][0], value
);
6064 bmap
= isl_basic_map_simplify(bmap
);
6065 return isl_basic_map_finalize(bmap
);
6067 isl_basic_map_free(bmap
);
6071 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6072 enum isl_dim_type type
, unsigned pos
, int value
)
6074 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6075 return isl_basic_map_free(bmap
);
6076 return isl_basic_map_fix_pos_si(bmap
,
6077 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6080 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6081 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6083 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6084 return isl_basic_map_free(bmap
);
6085 return isl_basic_map_fix_pos(bmap
,
6086 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6089 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6090 * to be equal to "v".
6092 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6093 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6097 if (!isl_val_is_int(v
))
6098 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6099 "expecting integer value", goto error
);
6100 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6102 pos
+= isl_basic_map_offset(bmap
, type
);
6103 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6107 isl_basic_map_free(bmap
);
6112 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6113 * to be equal to "v".
6115 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6116 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6118 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6121 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6122 enum isl_dim_type type
, unsigned pos
, int value
)
6124 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6128 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6129 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6131 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6135 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6136 unsigned input
, int value
)
6138 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6141 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6142 unsigned dim
, int value
)
6144 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6145 isl_dim_set
, dim
, value
));
6148 /* Remove the basic map at position "i" from "map" if this basic map
6149 * is (obviously) empty.
6151 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6158 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6160 return isl_map_free(map
);
6164 isl_basic_map_free(map
->p
[i
]);
6167 map
->p
[i
] = map
->p
[map
->n
];
6168 map
= isl_map_unmark_normalized(map
);
6175 /* Perform "fn" on each basic map of "map", where we may not be holding
6176 * the only reference to "map".
6177 * In particular, "fn" should be a semantics preserving operation
6178 * that we want to apply to all copies of "map". We therefore need
6179 * to be careful not to modify "map" in a way that breaks "map"
6180 * in case anything goes wrong.
6182 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6183 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6185 struct isl_basic_map
*bmap
;
6191 for (i
= map
->n
- 1; i
>= 0; --i
) {
6192 bmap
= isl_basic_map_copy(map
->p
[i
]);
6196 isl_basic_map_free(map
->p
[i
]);
6198 map
= remove_if_empty(map
, i
);
6209 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6210 enum isl_dim_type type
, unsigned pos
, int value
)
6214 map
= isl_map_cow(map
);
6215 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6216 return isl_map_free(map
);
6217 for (i
= map
->n
- 1; i
>= 0; --i
) {
6218 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6219 map
= remove_if_empty(map
, i
);
6223 map
= isl_map_unmark_normalized(map
);
6227 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6228 enum isl_dim_type type
, unsigned pos
, int value
)
6230 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6233 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6234 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6238 map
= isl_map_cow(map
);
6239 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6240 return isl_map_free(map
);
6241 for (i
= 0; i
< map
->n
; ++i
) {
6242 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6246 map
= isl_map_unmark_normalized(map
);
6253 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6254 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6256 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6259 /* Fix the value of the variable at position "pos" of type "type" of "map"
6260 * to be equal to "v".
6262 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6263 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6267 map
= isl_map_cow(map
);
6271 if (!isl_val_is_int(v
))
6272 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6273 "expecting integer value", goto error
);
6274 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6276 for (i
= map
->n
- 1; i
>= 0; --i
) {
6277 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6279 map
= remove_if_empty(map
, i
);
6283 map
= isl_map_unmark_normalized(map
);
6292 /* Fix the value of the variable at position "pos" of type "type" of "set"
6293 * to be equal to "v".
6295 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6296 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6298 return isl_map_fix_val(set
, type
, pos
, v
);
6301 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6302 unsigned input
, int value
)
6304 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6307 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6309 return set_from_map(isl_map_fix_si(set_to_map(set
),
6310 isl_dim_set
, dim
, value
));
6313 static __isl_give isl_basic_map
*basic_map_bound_si(
6314 __isl_take isl_basic_map
*bmap
,
6315 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6319 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6320 return isl_basic_map_free(bmap
);
6321 pos
+= isl_basic_map_offset(bmap
, type
);
6322 bmap
= isl_basic_map_cow(bmap
);
6323 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6324 j
= isl_basic_map_alloc_inequality(bmap
);
6327 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6329 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6330 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6332 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6333 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6335 bmap
= isl_basic_map_simplify(bmap
);
6336 return isl_basic_map_finalize(bmap
);
6338 isl_basic_map_free(bmap
);
6342 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6343 __isl_take isl_basic_map
*bmap
,
6344 enum isl_dim_type type
, unsigned pos
, int value
)
6346 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6349 /* Constrain the values of the given dimension to be no greater than "value".
6351 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6352 __isl_take isl_basic_map
*bmap
,
6353 enum isl_dim_type type
, unsigned pos
, int value
)
6355 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6358 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6359 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6363 map
= isl_map_cow(map
);
6364 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6365 return isl_map_free(map
);
6366 for (i
= 0; i
< map
->n
; ++i
) {
6367 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6368 type
, pos
, value
, upper
);
6372 map
= isl_map_unmark_normalized(map
);
6379 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6380 enum isl_dim_type type
, unsigned pos
, int value
)
6382 return map_bound_si(map
, type
, pos
, value
, 0);
6385 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6386 enum isl_dim_type type
, unsigned pos
, int value
)
6388 return map_bound_si(map
, type
, pos
, value
, 1);
6391 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6392 enum isl_dim_type type
, unsigned pos
, int value
)
6394 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6398 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6399 enum isl_dim_type type
, unsigned pos
, int value
)
6401 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6404 /* Bound the given variable of "bmap" from below (or above is "upper"
6405 * is set) to "value".
6407 static __isl_give isl_basic_map
*basic_map_bound(
6408 __isl_take isl_basic_map
*bmap
,
6409 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6413 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6414 return isl_basic_map_free(bmap
);
6415 pos
+= isl_basic_map_offset(bmap
, type
);
6416 bmap
= isl_basic_map_cow(bmap
);
6417 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6418 j
= isl_basic_map_alloc_inequality(bmap
);
6421 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6423 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6424 isl_int_set(bmap
->ineq
[j
][0], value
);
6426 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6427 isl_int_neg(bmap
->ineq
[j
][0], value
);
6429 bmap
= isl_basic_map_simplify(bmap
);
6430 return isl_basic_map_finalize(bmap
);
6432 isl_basic_map_free(bmap
);
6436 /* Bound the given variable of "map" from below (or above is "upper"
6437 * is set) to "value".
6439 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6440 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6444 map
= isl_map_cow(map
);
6445 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6446 return isl_map_free(map
);
6447 for (i
= map
->n
- 1; i
>= 0; --i
) {
6448 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6449 map
= remove_if_empty(map
, i
);
6453 map
= isl_map_unmark_normalized(map
);
6457 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6458 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6460 return map_bound(map
, type
, pos
, value
, 0);
6463 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6464 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6466 return map_bound(map
, type
, pos
, value
, 1);
6469 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6470 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6472 return isl_map_lower_bound(set
, type
, pos
, value
);
6475 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6476 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6478 return isl_map_upper_bound(set
, type
, pos
, value
);
6481 /* Force the values of the variable at position "pos" of type "type" of "set"
6482 * to be no smaller than "value".
6484 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6485 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6489 if (!isl_val_is_int(value
))
6490 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6491 "expecting integer value", goto error
);
6492 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6493 isl_val_free(value
);
6496 isl_val_free(value
);
6501 /* Force the values of the variable at position "pos" of type "type" of "set"
6502 * to be no greater than "value".
6504 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6505 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6509 if (!isl_val_is_int(value
))
6510 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6511 "expecting integer value", goto error
);
6512 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6513 isl_val_free(value
);
6516 isl_val_free(value
);
6521 /* Bound the given variable of "bset" from below (or above is "upper"
6522 * is set) to "value".
6524 static __isl_give isl_basic_set
*isl_basic_set_bound(
6525 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6526 isl_int value
, int upper
)
6528 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6529 type
, pos
, value
, upper
));
6532 /* Bound the given variable of "bset" from below (or above is "upper"
6533 * is set) to "value".
6535 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6536 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6537 __isl_take isl_val
*value
, int upper
)
6541 if (!isl_val_is_int(value
))
6542 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6543 "expecting integer value", goto error
);
6544 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6545 isl_val_free(value
);
6548 isl_val_free(value
);
6549 isl_basic_set_free(bset
);
6553 /* Bound the given variable of "bset" from below to "value".
6555 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6556 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6557 __isl_take isl_val
*value
)
6559 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6562 /* Bound the given variable of "bset" from above to "value".
6564 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6565 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6566 __isl_take isl_val
*value
)
6568 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6571 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6575 map
= isl_map_cow(map
);
6579 map
->dim
= isl_space_reverse(map
->dim
);
6582 for (i
= 0; i
< map
->n
; ++i
) {
6583 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6587 map
= isl_map_unmark_normalized(map
);
6595 #define TYPE isl_pw_multi_aff
6597 #define SUFFIX _pw_multi_aff
6599 #define EMPTY isl_pw_multi_aff_empty
6601 #define ADD isl_pw_multi_aff_union_add
6602 #include "isl_map_lexopt_templ.c"
6604 /* Given a map "map", compute the lexicographically minimal
6605 * (or maximal) image element for each domain element in dom,
6606 * in the form of an isl_pw_multi_aff.
6607 * If "empty" is not NULL, then set *empty to those elements in dom that
6608 * do not have an image element.
6609 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6610 * should be computed over the domain of "map". "empty" is also NULL
6613 * We first compute the lexicographically minimal or maximal element
6614 * in the first basic map. This results in a partial solution "res"
6615 * and a subset "todo" of dom that still need to be handled.
6616 * We then consider each of the remaining maps in "map" and successively
6617 * update both "res" and "todo".
6618 * If "empty" is NULL, then the todo sets are not needed and therefore
6619 * also not computed.
6621 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6622 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6623 __isl_give isl_set
**empty
, unsigned flags
)
6627 isl_pw_multi_aff
*res
;
6630 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6631 if (!map
|| (!full
&& !dom
))
6634 if (isl_map_plain_is_empty(map
)) {
6639 return isl_pw_multi_aff_from_map(map
);
6642 res
= basic_map_partial_lexopt_pw_multi_aff(
6643 isl_basic_map_copy(map
->p
[0]),
6644 isl_set_copy(dom
), empty
, flags
);
6648 for (i
= 1; i
< map
->n
; ++i
) {
6649 isl_pw_multi_aff
*res_i
;
6651 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6652 isl_basic_map_copy(map
->p
[i
]),
6653 isl_set_copy(dom
), empty
, flags
);
6655 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6656 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6658 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6661 todo
= isl_set_intersect(todo
, *empty
);
6680 #define TYPE isl_map
6684 #define EMPTY isl_map_empty
6686 #define ADD isl_map_union_disjoint
6687 #include "isl_map_lexopt_templ.c"
6689 /* Given a map "map", compute the lexicographically minimal
6690 * (or maximal) image element for each domain element in "dom",
6691 * in the form of an isl_map.
6692 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6693 * do not have an image element.
6694 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6695 * should be computed over the domain of "map". "empty" is also NULL
6698 * If the input consists of more than one disjunct, then first
6699 * compute the desired result in the form of an isl_pw_multi_aff and
6700 * then convert that into an isl_map.
6702 * This function used to have an explicit implementation in terms
6703 * of isl_maps, but it would continually intersect the domains of
6704 * partial results with the complement of the domain of the next
6705 * partial solution, potentially leading to an explosion in the number
6706 * of disjuncts if there are several disjuncts in the input.
6707 * An even earlier implementation of this function would look for
6708 * better results in the domain of the partial result and for extra
6709 * results in the complement of this domain, which would lead to
6710 * even more splintering.
6712 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6713 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6714 __isl_give isl_set
**empty
, unsigned flags
)
6717 struct isl_map
*res
;
6718 isl_pw_multi_aff
*pma
;
6720 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6721 if (!map
|| (!full
&& !dom
))
6724 if (isl_map_plain_is_empty(map
)) {
6733 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6739 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6741 return isl_map_from_pw_multi_aff(pma
);
6750 __isl_give isl_map
*isl_map_partial_lexmax(
6751 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6752 __isl_give isl_set
**empty
)
6754 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6757 __isl_give isl_map
*isl_map_partial_lexmin(
6758 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6759 __isl_give isl_set
**empty
)
6761 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6764 __isl_give isl_set
*isl_set_partial_lexmin(
6765 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6766 __isl_give isl_set
**empty
)
6768 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6772 __isl_give isl_set
*isl_set_partial_lexmax(
6773 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6774 __isl_give isl_set
**empty
)
6776 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6780 /* Compute the lexicographic minimum (or maximum if "flags" includes
6781 * ISL_OPT_MAX) of "bset" over its parametric domain.
6783 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6786 return isl_basic_map_lexopt(bset
, flags
);
6789 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6791 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6794 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6796 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6799 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6801 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6804 /* Compute the lexicographic minimum of "bset" over its parametric domain
6805 * for the purpose of quantifier elimination.
6806 * That is, find an explicit representation for all the existentially
6807 * quantified variables in "bset" by computing their lexicographic
6810 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6811 __isl_take isl_basic_set
*bset
)
6813 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6816 /* Given a basic map with one output dimension, compute the minimum or
6817 * maximum of that dimension as an isl_pw_aff.
6819 * Compute the optimum as a lexicographic optimum over the single
6820 * output dimension and extract the single isl_pw_aff from the result.
6822 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6825 isl_pw_multi_aff
*pma
;
6828 bmap
= isl_basic_map_copy(bmap
);
6829 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6830 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6831 isl_pw_multi_aff_free(pma
);
6836 /* Compute the minimum or maximum of the given output dimension
6837 * as a function of the parameters and the input dimensions,
6838 * but independently of the other output dimensions.
6840 * We first project out the other output dimension and then compute
6841 * the "lexicographic" maximum in each basic map, combining the results
6842 * using isl_pw_aff_union_max.
6844 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6851 n_out
= isl_map_dim(map
, isl_dim_out
);
6852 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6853 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6858 isl_space
*space
= isl_map_get_space(map
);
6860 return isl_pw_aff_empty(space
);
6863 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6864 for (i
= 1; i
< map
->n
; ++i
) {
6865 isl_pw_aff
*pwaff_i
;
6867 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6868 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6876 /* Compute the minimum of the given output dimension as a function of the
6877 * parameters and input dimensions, but independently of
6878 * the other output dimensions.
6880 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6882 return map_dim_opt(map
, pos
, 0);
6885 /* Compute the maximum of the given output dimension as a function of the
6886 * parameters and input dimensions, but independently of
6887 * the other output dimensions.
6889 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6891 return map_dim_opt(map
, pos
, 1);
6894 /* Compute the minimum or maximum of the given set dimension
6895 * as a function of the parameters,
6896 * but independently of the other set dimensions.
6898 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6901 return map_dim_opt(set
, pos
, max
);
6904 /* Compute the maximum of the given set dimension as a function of the
6905 * parameters, but independently of the other set dimensions.
6907 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6909 return set_dim_opt(set
, pos
, 1);
6912 /* Compute the minimum of the given set dimension as a function of the
6913 * parameters, but independently of the other set dimensions.
6915 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6917 return set_dim_opt(set
, pos
, 0);
6920 /* Apply a preimage specified by "mat" on the parameters of "bset".
6921 * bset is assumed to have only parameters and divs.
6923 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6924 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6931 bset
->dim
= isl_space_cow(bset
->dim
);
6935 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6937 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6939 bset
->dim
->nparam
= 0;
6940 bset
->dim
->n_out
= nparam
;
6941 bset
= isl_basic_set_preimage(bset
, mat
);
6943 bset
->dim
->nparam
= bset
->dim
->n_out
;
6944 bset
->dim
->n_out
= 0;
6949 isl_basic_set_free(bset
);
6953 /* Apply a preimage specified by "mat" on the parameters of "set".
6954 * set is assumed to have only parameters and divs.
6956 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6957 __isl_take isl_mat
*mat
)
6965 nparam
= isl_set_dim(set
, isl_dim_param
);
6967 if (mat
->n_row
!= 1 + nparam
)
6968 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6969 "unexpected number of rows", goto error
);
6971 space
= isl_set_get_space(set
);
6972 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6973 isl_dim_param
, 0, nparam
);
6974 set
= isl_set_reset_space(set
, space
);
6975 set
= isl_set_preimage(set
, mat
);
6976 nparam
= isl_set_dim(set
, isl_dim_out
);
6977 space
= isl_set_get_space(set
);
6978 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6979 isl_dim_out
, 0, nparam
);
6980 set
= isl_set_reset_space(set
, space
);
6988 /* Intersect the basic set "bset" with the affine space specified by the
6989 * equalities in "eq".
6991 static __isl_give isl_basic_set
*basic_set_append_equalities(
6992 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7000 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7005 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7006 for (i
= 0; i
< eq
->n_row
; ++i
) {
7007 k
= isl_basic_set_alloc_equality(bset
);
7010 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7011 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7015 bset
= isl_basic_set_gauss(bset
, NULL
);
7016 bset
= isl_basic_set_finalize(bset
);
7021 isl_basic_set_free(bset
);
7025 /* Intersect the set "set" with the affine space specified by the
7026 * equalities in "eq".
7028 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7036 for (i
= 0; i
< set
->n
; ++i
) {
7037 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7050 /* Given a basic set "bset" that only involves parameters and existentially
7051 * quantified variables, return the index of the first equality
7052 * that only involves parameters. If there is no such equality then
7053 * return bset->n_eq.
7055 * This function assumes that isl_basic_set_gauss has been called on "bset".
7057 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7060 unsigned nparam
, n_div
;
7065 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7066 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7068 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7069 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7076 /* Compute an explicit representation for the existentially quantified
7077 * variables in "bset" by computing the "minimal value" of the set
7078 * variables. Since there are no set variables, the computation of
7079 * the minimal value essentially computes an explicit representation
7080 * of the non-empty part(s) of "bset".
7082 * The input only involves parameters and existentially quantified variables.
7083 * All equalities among parameters have been removed.
7085 * Since the existentially quantified variables in the result are in general
7086 * going to be different from those in the input, we first replace
7087 * them by the minimal number of variables based on their equalities.
7088 * This should simplify the parametric integer programming.
7090 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7092 isl_morph
*morph1
, *morph2
;
7098 if (bset
->n_eq
== 0)
7099 return isl_basic_set_lexmin_compute_divs(bset
);
7101 morph1
= isl_basic_set_parameter_compression(bset
);
7102 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7103 bset
= isl_basic_set_lift(bset
);
7104 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7105 bset
= isl_morph_basic_set(morph2
, bset
);
7106 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7107 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7109 set
= isl_basic_set_lexmin_compute_divs(bset
);
7111 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7116 /* Project the given basic set onto its parameter domain, possibly introducing
7117 * new, explicit, existential variables in the constraints.
7118 * The input has parameters and (possibly implicit) existential variables.
7119 * The output has the same parameters, but only
7120 * explicit existentially quantified variables.
7122 * The actual projection is performed by pip, but pip doesn't seem
7123 * to like equalities very much, so we first remove the equalities
7124 * among the parameters by performing a variable compression on
7125 * the parameters. Afterward, an inverse transformation is performed
7126 * and the equalities among the parameters are inserted back in.
7128 * The variable compression on the parameters may uncover additional
7129 * equalities that were only implicit before. We therefore check
7130 * if there are any new parameter equalities in the result and
7131 * if so recurse. The removal of parameter equalities is required
7132 * for the parameter compression performed by base_compute_divs.
7134 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7138 struct isl_mat
*T
, *T2
;
7139 struct isl_set
*set
;
7142 bset
= isl_basic_set_cow(bset
);
7146 if (bset
->n_eq
== 0)
7147 return base_compute_divs(bset
);
7149 bset
= isl_basic_set_gauss(bset
, NULL
);
7152 if (isl_basic_set_plain_is_empty(bset
))
7153 return isl_set_from_basic_set(bset
);
7155 i
= first_parameter_equality(bset
);
7156 if (i
== bset
->n_eq
)
7157 return base_compute_divs(bset
);
7159 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7160 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7162 eq
= isl_mat_cow(eq
);
7163 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7164 if (T
&& T
->n_col
== 0) {
7168 bset
= isl_basic_set_set_to_empty(bset
);
7169 return isl_set_from_basic_set(bset
);
7171 bset
= basic_set_parameter_preimage(bset
, T
);
7173 i
= first_parameter_equality(bset
);
7176 else if (i
== bset
->n_eq
)
7177 set
= base_compute_divs(bset
);
7179 set
= parameter_compute_divs(bset
);
7180 set
= set_parameter_preimage(set
, T2
);
7181 set
= set_append_equalities(set
, eq
);
7185 /* Insert the divs from "ls" before those of "bmap".
7187 * The number of columns is not changed, which means that the last
7188 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7189 * The caller is responsible for removing the same number of dimensions
7190 * from the space of "bmap".
7192 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7193 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7199 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7203 old_n_div
= bmap
->n_div
;
7204 bmap
= insert_div_rows(bmap
, n_div
);
7208 for (i
= 0; i
< n_div
; ++i
) {
7209 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7210 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7216 /* Replace the space of "bmap" by the space and divs of "ls".
7218 * If "ls" has any divs, then we simplify the result since we may
7219 * have discovered some additional equalities that could simplify
7220 * the div expressions.
7222 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7223 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7227 bmap
= isl_basic_map_cow(bmap
);
7231 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7232 bmap
= insert_divs_from_local_space(bmap
, ls
);
7236 isl_space_free(bmap
->dim
);
7237 bmap
->dim
= isl_local_space_get_space(ls
);
7241 isl_local_space_free(ls
);
7243 bmap
= isl_basic_map_simplify(bmap
);
7244 bmap
= isl_basic_map_finalize(bmap
);
7247 isl_basic_map_free(bmap
);
7248 isl_local_space_free(ls
);
7252 /* Replace the space of "map" by the space and divs of "ls".
7254 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7255 __isl_take isl_local_space
*ls
)
7259 map
= isl_map_cow(map
);
7263 for (i
= 0; i
< map
->n
; ++i
) {
7264 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7265 isl_local_space_copy(ls
));
7269 isl_space_free(map
->dim
);
7270 map
->dim
= isl_local_space_get_space(ls
);
7274 isl_local_space_free(ls
);
7277 isl_local_space_free(ls
);
7282 /* Compute an explicit representation for the existentially
7283 * quantified variables for which do not know any explicit representation yet.
7285 * We first sort the existentially quantified variables so that the
7286 * existentially quantified variables for which we already have an explicit
7287 * representation are placed before those for which we do not.
7288 * The input dimensions, the output dimensions and the existentially
7289 * quantified variables for which we already have an explicit
7290 * representation are then turned into parameters.
7291 * compute_divs returns a map with the same parameters and
7292 * no input or output dimensions and the dimension specification
7293 * is reset to that of the input, including the existentially quantified
7294 * variables for which we already had an explicit representation.
7296 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7298 struct isl_basic_set
*bset
;
7299 struct isl_set
*set
;
7300 struct isl_map
*map
;
7302 isl_local_space
*ls
;
7309 bmap
= isl_basic_map_sort_divs(bmap
);
7310 bmap
= isl_basic_map_cow(bmap
);
7314 n_known
= isl_basic_map_first_unknown_div(bmap
);
7316 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7318 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7319 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7320 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7321 space
= isl_space_set_alloc(bmap
->ctx
,
7322 nparam
+ n_in
+ n_out
+ n_known
, 0);
7326 ls
= isl_basic_map_get_local_space(bmap
);
7327 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7328 n_known
, bmap
->n_div
- n_known
);
7330 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7331 swap_div(bmap
, i
- n_known
, i
);
7332 bmap
->n_div
-= n_known
;
7333 bmap
->extra
-= n_known
;
7335 bmap
= isl_basic_map_reset_space(bmap
, space
);
7336 bset
= bset_from_bmap(bmap
);
7338 set
= parameter_compute_divs(bset
);
7339 map
= set_to_map(set
);
7340 map
= replace_space_by_local_space(map
, ls
);
7344 isl_basic_map_free(bmap
);
7348 /* Remove the explicit representation of local variable "div",
7351 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7352 __isl_take isl_basic_map
*bmap
, int div
)
7356 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7358 return isl_basic_map_free(bmap
);
7362 bmap
= isl_basic_map_cow(bmap
);
7365 isl_int_set_si(bmap
->div
[div
][0], 0);
7369 /* Is local variable "div" of "bmap" marked as not having an explicit
7371 * Note that even if "div" is not marked in this way and therefore
7372 * has an explicit representation, this representation may still
7373 * depend (indirectly) on other local variables that do not
7374 * have an explicit representation.
7376 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7379 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7380 return isl_bool_error
;
7381 return isl_int_is_zero(bmap
->div
[div
][0]);
7384 /* Return the position of the first local variable that does not
7385 * have an explicit representation.
7386 * Return the total number of local variables if they all have
7387 * an explicit representation.
7388 * Return -1 on error.
7390 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7397 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7398 if (!isl_basic_map_div_is_known(bmap
, i
))
7404 /* Return the position of the first local variable that does not
7405 * have an explicit representation.
7406 * Return the total number of local variables if they all have
7407 * an explicit representation.
7408 * Return -1 on error.
7410 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7412 return isl_basic_map_first_unknown_div(bset
);
7415 /* Does "bmap" have an explicit representation for all local variables?
7417 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7421 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7422 first
= isl_basic_map_first_unknown_div(bmap
);
7424 return isl_bool_error
;
7428 /* Do all basic maps in "map" have an explicit representation
7429 * for all local variables?
7431 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7436 return isl_bool_error
;
7438 for (i
= 0; i
< map
->n
; ++i
) {
7439 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7444 return isl_bool_true
;
7447 /* If bmap contains any unknown divs, then compute explicit
7448 * expressions for them. However, this computation may be
7449 * quite expensive, so first try to remove divs that aren't
7452 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7455 struct isl_map
*map
;
7457 known
= isl_basic_map_divs_known(bmap
);
7461 return isl_map_from_basic_map(bmap
);
7463 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7465 known
= isl_basic_map_divs_known(bmap
);
7469 return isl_map_from_basic_map(bmap
);
7471 map
= compute_divs(bmap
);
7474 isl_basic_map_free(bmap
);
7478 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7482 struct isl_map
*res
;
7489 known
= isl_map_divs_known(map
);
7497 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7498 for (i
= 1 ; i
< map
->n
; ++i
) {
7500 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7501 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7502 res
= isl_map_union_disjoint(res
, r2
);
7504 res
= isl_map_union(res
, r2
);
7511 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7513 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7516 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7518 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7521 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7524 struct isl_set
*set
;
7529 map
= isl_map_cow(map
);
7533 set
= set_from_map(map
);
7534 set
->dim
= isl_space_domain(set
->dim
);
7537 for (i
= 0; i
< map
->n
; ++i
) {
7538 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7542 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7543 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7550 /* Return the union of "map1" and "map2", where we assume for now that
7551 * "map1" and "map2" are disjoint. Note that the basic maps inside
7552 * "map1" or "map2" may not be disjoint from each other.
7553 * Also note that this function is also called from isl_map_union,
7554 * which takes care of handling the situation where "map1" and "map2"
7555 * may not be disjoint.
7557 * If one of the inputs is empty, we can simply return the other input.
7558 * Similarly, if one of the inputs is universal, then it is equal to the union.
7560 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7561 __isl_take isl_map
*map2
)
7565 struct isl_map
*map
= NULL
;
7571 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7572 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7573 "spaces don't match", goto error
);
7584 is_universe
= isl_map_plain_is_universe(map1
);
7585 if (is_universe
< 0)
7592 is_universe
= isl_map_plain_is_universe(map2
);
7593 if (is_universe
< 0)
7600 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7601 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7602 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7604 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7605 map1
->n
+ map2
->n
, flags
);
7608 for (i
= 0; i
< map1
->n
; ++i
) {
7609 map
= isl_map_add_basic_map(map
,
7610 isl_basic_map_copy(map1
->p
[i
]));
7614 for (i
= 0; i
< map2
->n
; ++i
) {
7615 map
= isl_map_add_basic_map(map
,
7616 isl_basic_map_copy(map2
->p
[i
]));
7630 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7631 * guaranteed to be disjoint by the caller.
7633 * Note that this functions is called from within isl_map_make_disjoint,
7634 * so we have to be careful not to touch the constraints of the inputs
7637 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7638 __isl_take isl_map
*map2
)
7640 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7643 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7644 * not be disjoint. The parameters are assumed to have been aligned.
7646 * We currently simply call map_union_disjoint, the internal operation
7647 * of which does not really depend on the inputs being disjoint.
7648 * If the result contains more than one basic map, then we clear
7649 * the disjoint flag since the result may contain basic maps from
7650 * both inputs and these are not guaranteed to be disjoint.
7652 * As a special case, if "map1" and "map2" are obviously equal,
7653 * then we simply return "map1".
7655 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7656 __isl_take isl_map
*map2
)
7663 equal
= isl_map_plain_is_equal(map1
, map2
);
7671 map1
= map_union_disjoint(map1
, map2
);
7675 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7683 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7686 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7687 __isl_take isl_map
*map2
)
7689 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7692 __isl_give isl_set
*isl_set_union_disjoint(
7693 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7695 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7699 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7701 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7704 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7707 * "map" and "set" are assumed to be compatible and non-NULL.
7709 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7710 __isl_take isl_set
*set
,
7711 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7712 __isl_take isl_basic_set
*bset
))
7715 struct isl_map
*result
;
7718 if (isl_set_plain_is_universe(set
)) {
7723 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7724 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7725 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7727 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7728 map
->n
* set
->n
, flags
);
7729 for (i
= 0; result
&& i
< map
->n
; ++i
)
7730 for (j
= 0; j
< set
->n
; ++j
) {
7731 result
= isl_map_add_basic_map(result
,
7732 fn(isl_basic_map_copy(map
->p
[i
]),
7733 isl_basic_set_copy(set
->p
[j
])));
7743 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7744 __isl_take isl_set
*set
)
7748 ok
= isl_map_compatible_range(map
, set
);
7752 isl_die(set
->ctx
, isl_error_invalid
,
7753 "incompatible spaces", goto error
);
7755 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7762 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7763 __isl_take isl_set
*set
)
7765 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7768 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7769 __isl_take isl_set
*set
)
7773 ok
= isl_map_compatible_domain(map
, set
);
7777 isl_die(set
->ctx
, isl_error_invalid
,
7778 "incompatible spaces", goto error
);
7780 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7787 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7788 __isl_take isl_set
*set
)
7790 return isl_map_align_params_map_map_and(map
, set
,
7791 &map_intersect_domain
);
7794 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7795 * in the space B -> C, return the intersection.
7796 * The parameters are assumed to have been aligned.
7798 * The map "factor" is first extended to a map living in the space
7799 * [A -> B] -> C and then a regular intersection is computed.
7801 static __isl_give isl_map
*map_intersect_domain_factor_range(
7802 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7805 isl_map
*ext_factor
;
7807 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7808 ext_factor
= isl_map_universe(space
);
7809 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7810 return map_intersect(map
, ext_factor
);
7813 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7814 * in the space B -> C, return the intersection.
7816 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7817 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7819 return isl_map_align_params_map_map_and(map
, factor
,
7820 &map_intersect_domain_factor_range
);
7823 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7824 * in the space A -> C, return the intersection.
7826 * The map "factor" is first extended to a map living in the space
7827 * A -> [B -> C] and then a regular intersection is computed.
7829 static __isl_give isl_map
*map_intersect_range_factor_range(
7830 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7833 isl_map
*ext_factor
;
7835 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7836 ext_factor
= isl_map_universe(space
);
7837 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7838 return isl_map_intersect(map
, ext_factor
);
7841 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7842 * in the space A -> C, return the intersection.
7844 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7845 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7847 return isl_map_align_params_map_map_and(map
, factor
,
7848 &map_intersect_range_factor_range
);
7851 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7852 __isl_take isl_map
*map2
)
7856 map1
= isl_map_reverse(map1
);
7857 map1
= isl_map_apply_range(map1
, map2
);
7858 return isl_map_reverse(map1
);
7865 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7866 __isl_take isl_map
*map2
)
7868 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7871 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7872 __isl_take isl_map
*map2
)
7875 struct isl_map
*result
;
7881 space
= isl_space_join(isl_space_copy(map1
->dim
),
7882 isl_space_copy(map2
->dim
));
7884 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7887 for (i
= 0; i
< map1
->n
; ++i
)
7888 for (j
= 0; j
< map2
->n
; ++j
) {
7889 result
= isl_map_add_basic_map(result
,
7890 isl_basic_map_apply_range(
7891 isl_basic_map_copy(map1
->p
[i
]),
7892 isl_basic_map_copy(map2
->p
[j
])));
7898 if (result
&& result
->n
<= 1)
7899 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7907 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7908 __isl_take isl_map
*map2
)
7910 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7914 * returns range - domain
7916 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7918 isl_space
*target_space
;
7919 struct isl_basic_set
*bset
;
7926 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7927 bmap
->dim
, isl_dim_out
),
7929 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7930 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7931 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7932 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7933 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7934 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7935 for (i
= 0; i
< dim
; ++i
) {
7936 int j
= isl_basic_map_alloc_equality(bmap
);
7938 bmap
= isl_basic_map_free(bmap
);
7941 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7942 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7943 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7944 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7946 bset
= isl_basic_map_domain(bmap
);
7947 bset
= isl_basic_set_reset_space(bset
, target_space
);
7950 isl_basic_map_free(bmap
);
7954 /* Check that domain and range of "map" are the same.
7956 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
7961 space
= isl_map_peek_space(map
);
7962 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
7964 return isl_stat_error
;
7966 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7967 "domain and range don't match", return isl_stat_error
);
7972 * returns range - domain
7974 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7978 struct isl_set
*result
;
7983 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7984 map
->dim
, isl_dim_out
),
7986 dim
= isl_map_get_space(map
);
7987 dim
= isl_space_domain(dim
);
7988 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7991 for (i
= 0; i
< map
->n
; ++i
)
7992 result
= isl_set_add_basic_set(result
,
7993 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8002 * returns [domain -> range] -> range - domain
8004 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8005 __isl_take isl_basic_map
*bmap
)
8009 isl_basic_map
*domain
;
8013 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8014 bmap
->dim
, isl_dim_out
))
8015 isl_die(bmap
->ctx
, isl_error_invalid
,
8016 "domain and range don't match", goto error
);
8018 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8019 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8021 space
= isl_basic_map_get_space(bmap
);
8022 space
= isl_space_from_range(isl_space_domain(space
));
8023 domain
= isl_basic_map_universe(space
);
8025 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8026 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8027 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8029 total
= isl_basic_map_total_dim(bmap
);
8031 for (i
= 0; i
< n
; ++i
) {
8032 k
= isl_basic_map_alloc_equality(bmap
);
8035 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8036 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8037 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8038 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8041 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8042 return isl_basic_map_finalize(bmap
);
8044 isl_basic_map_free(bmap
);
8049 * returns [domain -> range] -> range - domain
8051 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8054 isl_space
*domain_space
;
8056 if (isl_map_check_equal_tuples(map
) < 0)
8057 return isl_map_free(map
);
8059 map
= isl_map_cow(map
);
8063 domain_space
= isl_space_domain(isl_map_get_space(map
));
8064 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8065 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8068 for (i
= 0; i
< map
->n
; ++i
) {
8069 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8073 map
= isl_map_unmark_normalized(map
);
8080 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8082 unsigned n_in
, n_out
;
8086 n_in
= isl_space_dim(space
, isl_dim_in
);
8087 n_out
= isl_space_dim(space
, isl_dim_out
);
8089 isl_die(space
->ctx
, isl_error_invalid
,
8090 "number of input and output dimensions needs to be "
8091 "the same", goto error
);
8092 return isl_basic_map_equal(space
, n_in
);
8094 isl_space_free(space
);
8098 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8100 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8103 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8105 isl_space
*dim
= isl_set_get_space(set
);
8107 id
= isl_map_identity(isl_space_map_from_set(dim
));
8108 return isl_map_intersect_range(id
, set
);
8111 /* Construct a basic set with all set dimensions having only non-negative
8114 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8115 __isl_take isl_space
*space
)
8120 struct isl_basic_set
*bset
;
8124 nparam
= isl_space_dim(space
, isl_dim_param
);
8125 dim
= isl_space_dim(space
, isl_dim_set
);
8126 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8129 for (i
= 0; i
< dim
; ++i
) {
8130 int k
= isl_basic_set_alloc_inequality(bset
);
8133 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8134 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8138 isl_basic_set_free(bset
);
8142 /* Construct the half-space x_pos >= 0.
8144 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8148 isl_basic_set
*nonneg
;
8150 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8151 k
= isl_basic_set_alloc_inequality(nonneg
);
8154 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8155 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8157 return isl_basic_set_finalize(nonneg
);
8159 isl_basic_set_free(nonneg
);
8163 /* Construct the half-space x_pos <= -1.
8165 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8171 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8172 k
= isl_basic_set_alloc_inequality(neg
);
8175 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8176 isl_int_set_si(neg
->ineq
[k
][0], -1);
8177 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8179 return isl_basic_set_finalize(neg
);
8181 isl_basic_set_free(neg
);
8185 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8186 enum isl_dim_type type
, unsigned first
, unsigned n
)
8190 isl_basic_set
*nonneg
;
8196 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8197 return isl_set_free(set
);
8199 offset
= pos(set
->dim
, type
);
8200 for (i
= 0; i
< n
; ++i
) {
8201 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8202 offset
+ first
+ i
);
8203 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8205 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8211 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8213 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8219 return isl_stat_error
;
8220 if (isl_set_plain_is_empty(set
)) {
8225 return fn(set
, signs
, user
);
8228 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8230 half
= isl_set_intersect(half
, isl_set_copy(set
));
8231 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8235 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8237 half
= isl_set_intersect(half
, set
);
8238 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8241 return isl_stat_error
;
8244 /* Call "fn" on the intersections of "set" with each of the orthants
8245 * (except for obviously empty intersections). The orthant is identified
8246 * by the signs array, with each entry having value 1 or -1 according
8247 * to the sign of the corresponding variable.
8249 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8250 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8259 return isl_stat_error
;
8260 if (isl_set_plain_is_empty(set
))
8263 nparam
= isl_set_dim(set
, isl_dim_param
);
8264 nvar
= isl_set_dim(set
, isl_dim_set
);
8266 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8268 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8276 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8278 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8281 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8282 __isl_keep isl_basic_map
*bmap2
)
8285 struct isl_map
*map1
;
8286 struct isl_map
*map2
;
8288 if (!bmap1
|| !bmap2
)
8289 return isl_bool_error
;
8291 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8292 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8294 is_subset
= isl_map_is_subset(map1
, map2
);
8302 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8303 __isl_keep isl_basic_set
*bset2
)
8305 return isl_basic_map_is_subset(bset1
, bset2
);
8308 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8309 __isl_keep isl_basic_map
*bmap2
)
8313 if (!bmap1
|| !bmap2
)
8314 return isl_bool_error
;
8315 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8316 if (is_subset
!= isl_bool_true
)
8318 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8322 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8323 __isl_keep isl_basic_set
*bset2
)
8325 return isl_basic_map_is_equal(
8326 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8329 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8335 return isl_bool_error
;
8336 for (i
= 0; i
< map
->n
; ++i
) {
8337 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8339 return isl_bool_error
;
8341 return isl_bool_false
;
8343 return isl_bool_true
;
8346 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8348 return map
? map
->n
== 0 : isl_bool_error
;
8351 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8353 return set
? set
->n
== 0 : isl_bool_error
;
8356 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8358 return isl_map_is_empty(set_to_map(set
));
8361 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8362 __isl_keep isl_map
*map2
)
8365 return isl_bool_error
;
8367 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8370 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8371 __isl_keep isl_set
*set2
)
8374 return isl_bool_error
;
8376 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8379 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8384 return isl_bool_error
;
8385 is_subset
= isl_map_is_subset(map1
, map2
);
8386 if (is_subset
!= isl_bool_true
)
8388 is_subset
= isl_map_is_subset(map2
, map1
);
8392 /* Is "map1" equal to "map2"?
8394 * First check if they are obviously equal.
8395 * If not, then perform a more detailed analysis.
8397 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8401 equal
= isl_map_plain_is_equal(map1
, map2
);
8402 if (equal
< 0 || equal
)
8404 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8407 isl_bool
isl_basic_map_is_strict_subset(
8408 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8412 if (!bmap1
|| !bmap2
)
8413 return isl_bool_error
;
8414 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8415 if (is_subset
!= isl_bool_true
)
8417 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8418 return isl_bool_not(is_subset
);
8421 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8422 __isl_keep isl_map
*map2
)
8427 return isl_bool_error
;
8428 is_subset
= isl_map_is_subset(map1
, map2
);
8429 if (is_subset
!= isl_bool_true
)
8431 is_subset
= isl_map_is_subset(map2
, map1
);
8432 return isl_bool_not(is_subset
);
8435 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8436 __isl_keep isl_set
*set2
)
8438 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8441 /* Is "bmap" obviously equal to the universe with the same space?
8443 * That is, does it not have any constraints?
8445 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8448 return isl_bool_error
;
8449 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8452 /* Is "bset" obviously equal to the universe with the same space?
8454 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8456 return isl_basic_map_plain_is_universe(bset
);
8459 /* If "c" does not involve any existentially quantified variables,
8460 * then set *univ to false and abort
8462 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8464 isl_bool
*univ
= user
;
8467 n
= isl_constraint_dim(c
, isl_dim_div
);
8468 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8469 isl_constraint_free(c
);
8470 if (*univ
< 0 || !*univ
)
8471 return isl_stat_error
;
8475 /* Is "bmap" equal to the universe with the same space?
8477 * First check if it is obviously equal to the universe.
8478 * If not and if there are any constraints not involving
8479 * existentially quantified variables, then it is certainly
8480 * not equal to the universe.
8481 * Otherwise, check if the universe is a subset of "bmap".
8483 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8486 isl_basic_map
*test
;
8488 univ
= isl_basic_map_plain_is_universe(bmap
);
8489 if (univ
< 0 || univ
)
8491 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8492 return isl_bool_false
;
8493 univ
= isl_bool_true
;
8494 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8496 return isl_bool_error
;
8497 if (univ
< 0 || !univ
)
8499 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8500 univ
= isl_basic_map_is_subset(test
, bmap
);
8501 isl_basic_map_free(test
);
8505 /* Is "bset" equal to the universe with the same space?
8507 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8509 return isl_basic_map_is_universe(bset
);
8512 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8517 return isl_bool_error
;
8519 for (i
= 0; i
< map
->n
; ++i
) {
8520 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8525 return isl_bool_false
;
8528 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8530 return isl_map_plain_is_universe(set_to_map(set
));
8533 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8535 struct isl_basic_set
*bset
= NULL
;
8536 struct isl_vec
*sample
= NULL
;
8537 isl_bool empty
, non_empty
;
8540 return isl_bool_error
;
8542 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8543 return isl_bool_true
;
8545 if (isl_basic_map_plain_is_universe(bmap
))
8546 return isl_bool_false
;
8548 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8549 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8550 copy
= isl_basic_map_remove_redundancies(copy
);
8551 empty
= isl_basic_map_plain_is_empty(copy
);
8552 isl_basic_map_free(copy
);
8556 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8558 return isl_bool_error
;
8560 return isl_bool_false
;
8561 isl_vec_free(bmap
->sample
);
8562 bmap
->sample
= NULL
;
8563 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8565 return isl_bool_error
;
8566 sample
= isl_basic_set_sample_vec(bset
);
8568 return isl_bool_error
;
8569 empty
= sample
->size
== 0;
8570 isl_vec_free(bmap
->sample
);
8571 bmap
->sample
= sample
;
8573 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8578 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8581 return isl_bool_error
;
8582 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8585 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8588 return isl_bool_error
;
8589 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8592 /* Is "bmap" known to be non-empty?
8594 * That is, is the cached sample still valid?
8596 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8601 return isl_bool_error
;
8603 return isl_bool_false
;
8604 total
= 1 + isl_basic_map_total_dim(bmap
);
8605 if (bmap
->sample
->size
!= total
)
8606 return isl_bool_false
;
8607 return isl_basic_map_contains(bmap
, bmap
->sample
);
8610 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8612 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8615 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8616 __isl_take isl_basic_map
*bmap2
)
8618 struct isl_map
*map
;
8619 if (!bmap1
|| !bmap2
)
8622 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8624 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8627 map
= isl_map_add_basic_map(map
, bmap1
);
8628 map
= isl_map_add_basic_map(map
, bmap2
);
8631 isl_basic_map_free(bmap1
);
8632 isl_basic_map_free(bmap2
);
8636 struct isl_set
*isl_basic_set_union(
8637 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8639 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8640 bset_to_bmap(bset2
)));
8643 /* Order divs such that any div only depends on previous divs */
8644 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8645 __isl_take isl_basic_map
*bmap
)
8650 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8652 return isl_basic_map_free(bmap
);
8654 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8656 if (isl_int_is_zero(bmap
->div
[i
][0]))
8658 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8663 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8664 "integer division depends on itself",
8665 return isl_basic_map_free(bmap
));
8666 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8674 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8676 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8679 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8686 for (i
= 0; i
< map
->n
; ++i
) {
8687 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8698 /* Sort the local variables of "bset".
8700 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8701 __isl_take isl_basic_set
*bset
)
8703 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8706 /* Apply the expansion computed by isl_merge_divs.
8707 * The expansion itself is given by "exp" while the resulting
8708 * list of divs is given by "div".
8710 * Move the integer divisions of "bmap" into the right position
8711 * according to "exp" and then introduce the additional integer
8712 * divisions, adding div constraints.
8713 * The moving should be done first to avoid moving coefficients
8714 * in the definitions of the extra integer divisions.
8716 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8717 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8722 bmap
= isl_basic_map_cow(bmap
);
8726 if (div
->n_row
< bmap
->n_div
)
8727 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8728 "not an expansion", goto error
);
8730 n_div
= bmap
->n_div
;
8731 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8732 div
->n_row
- n_div
, 0,
8733 2 * (div
->n_row
- n_div
));
8735 for (i
= n_div
; i
< div
->n_row
; ++i
)
8736 if (isl_basic_map_alloc_div(bmap
) < 0)
8739 for (j
= n_div
- 1; j
>= 0; --j
) {
8742 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8747 for (i
= 0; i
< div
->n_row
; ++i
) {
8748 if (j
< n_div
&& exp
[j
] == i
) {
8751 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8752 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8754 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8763 isl_basic_map_free(bmap
);
8768 /* Apply the expansion computed by isl_merge_divs.
8769 * The expansion itself is given by "exp" while the resulting
8770 * list of divs is given by "div".
8772 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8773 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8775 return isl_basic_map_expand_divs(bset
, div
, exp
);
8778 /* Look for a div in dst that corresponds to the div "div" in src.
8779 * The divs before "div" in src and dst are assumed to be the same.
8781 * Return the position of the corresponding div in dst
8782 * if there is one. Otherwise, return a position beyond the integer divisions.
8783 * Return -1 on error.
8785 static int find_div(__isl_keep isl_basic_map
*dst
,
8786 __isl_keep isl_basic_map
*src
, unsigned div
)
8792 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8793 if (!dst
|| v_div
< 0)
8796 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8797 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8798 for (i
= div
; i
< n_div
; ++i
)
8799 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8800 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8806 /* Align the divs of "dst" to those of "src", adding divs from "src"
8807 * if needed. That is, make sure that the first src->n_div divs
8808 * of the result are equal to those of src.
8810 * The result is not finalized as by design it will have redundant
8811 * divs if any divs from "src" were copied.
8813 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8814 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8823 return isl_basic_map_free(dst
);
8825 if (src
->n_div
== 0)
8828 known
= isl_basic_map_divs_known(src
);
8830 return isl_basic_map_free(dst
);
8832 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8833 "some src divs are unknown",
8834 return isl_basic_map_free(dst
));
8836 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8838 return isl_basic_map_free(dst
);
8840 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8842 return isl_basic_map_free(dst
);
8845 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8846 for (i
= 0; i
< src
->n_div
; ++i
) {
8847 int j
= find_div(dst
, src
, i
);
8849 dst
= isl_basic_map_free(dst
);
8850 if (j
== dst_n_div
) {
8852 int extra
= src
->n_div
- i
;
8853 dst
= isl_basic_map_cow(dst
);
8856 dst
= isl_basic_map_extend_space(dst
,
8857 isl_space_copy(dst
->dim
),
8858 extra
, 0, 2 * extra
);
8861 j
= isl_basic_map_alloc_div(dst
);
8864 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
8865 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
8867 dst
= isl_basic_map_add_div_constraints(dst
, j
);
8872 dst
= isl_basic_map_swap_div(dst
, i
, j
);
8876 isl_basic_map_free(src
);
8879 isl_basic_map_free(src
);
8880 isl_basic_map_free(dst
);
8884 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8892 map
= isl_map_compute_divs(map
);
8893 map
= isl_map_cow(map
);
8897 for (i
= 1; i
< map
->n
; ++i
)
8898 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8899 for (i
= 1; i
< map
->n
; ++i
) {
8900 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8902 return isl_map_free(map
);
8905 map
= isl_map_unmark_normalized(map
);
8909 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8911 return isl_map_align_divs_internal(map
);
8914 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8916 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8919 /* Align the divs of the basic maps in "map" to those
8920 * of the basic maps in "list", as well as to the other basic maps in "map".
8921 * The elements in "list" are assumed to have known divs.
8923 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8924 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8928 map
= isl_map_compute_divs(map
);
8929 map
= isl_map_cow(map
);
8931 return isl_map_free(map
);
8935 n
= isl_basic_map_list_n_basic_map(list
);
8936 for (i
= 0; i
< n
; ++i
) {
8937 isl_basic_map
*bmap
;
8939 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8940 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8941 isl_basic_map_free(bmap
);
8944 return isl_map_free(map
);
8946 return isl_map_align_divs_internal(map
);
8949 /* Align the divs of each element of "list" to those of "bmap".
8950 * Both "bmap" and the elements of "list" are assumed to have known divs.
8952 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8953 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8958 return isl_basic_map_list_free(list
);
8960 n
= isl_basic_map_list_n_basic_map(list
);
8961 for (i
= 0; i
< n
; ++i
) {
8962 isl_basic_map
*bmap_i
;
8964 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8965 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8966 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8972 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8973 __isl_take isl_map
*map
)
8977 ok
= isl_map_compatible_domain(map
, set
);
8981 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8982 "incompatible spaces", goto error
);
8983 map
= isl_map_intersect_domain(map
, set
);
8984 set
= isl_map_range(map
);
8992 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8993 __isl_take isl_map
*map
)
8995 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8998 /* There is no need to cow as removing empty parts doesn't change
8999 * the meaning of the set.
9001 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9008 for (i
= map
->n
- 1; i
>= 0; --i
)
9009 map
= remove_if_empty(map
, i
);
9014 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9016 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9019 /* Create a binary relation that maps the shared initial "pos" dimensions
9020 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9022 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9023 __isl_keep isl_basic_set
*bset2
, int pos
)
9025 isl_basic_map
*bmap1
;
9026 isl_basic_map
*bmap2
;
9028 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9029 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9030 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9031 isl_dim_out
, 0, pos
);
9032 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9033 isl_dim_out
, 0, pos
);
9034 return isl_basic_map_range_product(bmap1
, bmap2
);
9037 /* Given two basic sets bset1 and bset2, compute the maximal difference
9038 * between the values of dimension pos in bset1 and those in bset2
9039 * for any common value of the parameters and dimensions preceding pos.
9041 static enum isl_lp_result
basic_set_maximal_difference_at(
9042 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9043 int pos
, isl_int
*opt
)
9045 isl_basic_map
*bmap1
;
9046 struct isl_ctx
*ctx
;
9047 struct isl_vec
*obj
;
9051 enum isl_lp_result res
;
9053 if (!bset1
|| !bset2
)
9054 return isl_lp_error
;
9056 nparam
= isl_basic_set_n_param(bset1
);
9057 dim1
= isl_basic_set_n_dim(bset1
);
9059 bmap1
= join_initial(bset1
, bset2
, pos
);
9061 return isl_lp_error
;
9063 total
= isl_basic_map_total_dim(bmap1
);
9065 obj
= isl_vec_alloc(ctx
, 1 + total
);
9068 isl_seq_clr(obj
->block
.data
, 1 + total
);
9069 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9070 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9071 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9073 isl_basic_map_free(bmap1
);
9077 isl_basic_map_free(bmap1
);
9078 return isl_lp_error
;
9081 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9082 * for any common value of the parameters and dimensions preceding pos
9083 * in both basic sets, the values of dimension pos in bset1 are
9084 * smaller or larger than those in bset2.
9087 * 1 if bset1 follows bset2
9088 * -1 if bset1 precedes bset2
9089 * 0 if bset1 and bset2 are incomparable
9090 * -2 if some error occurred.
9092 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9093 __isl_keep isl_basic_set
*bset2
, int pos
)
9096 enum isl_lp_result res
;
9101 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9103 if (res
== isl_lp_empty
)
9105 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9106 res
== isl_lp_unbounded
)
9108 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9117 /* Given two basic sets bset1 and bset2, check whether
9118 * for any common value of the parameters and dimensions preceding pos
9119 * there is a value of dimension pos in bset1 that is larger
9120 * than a value of the same dimension in bset2.
9123 * 1 if there exists such a pair
9124 * 0 if there is no such pair, but there is a pair of equal values
9126 * -2 if some error occurred.
9128 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9129 __isl_keep isl_basic_set
*bset2
, int pos
)
9132 isl_basic_map
*bmap
;
9135 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9136 bmap
= join_initial(bset1
, bset2
, pos
);
9137 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9138 isl_dim_out
, dim1
- pos
);
9139 empty
= isl_basic_map_is_empty(bmap
);
9143 isl_basic_map_free(bmap
);
9146 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9147 isl_dim_out
, dim1
- pos
);
9148 empty
= isl_basic_map_is_empty(bmap
);
9151 isl_basic_map_free(bmap
);
9156 isl_basic_map_free(bmap
);
9160 /* Given two sets set1 and set2, check whether
9161 * for any common value of the parameters and dimensions preceding pos
9162 * there is a value of dimension pos in set1 that is larger
9163 * than a value of the same dimension in set2.
9166 * 1 if there exists such a pair
9167 * 0 if there is no such pair, but there is a pair of equal values
9169 * -2 if some error occurred.
9171 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9172 __isl_keep isl_set
*set2
, int pos
)
9180 for (i
= 0; i
< set1
->n
; ++i
)
9181 for (j
= 0; j
< set2
->n
; ++j
) {
9183 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9184 if (f
== 1 || f
== -2)
9193 static isl_bool
isl_basic_map_plain_has_fixed_var(
9194 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9201 return isl_bool_error
;
9202 total
= isl_basic_map_total_dim(bmap
);
9203 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9204 for (; d
+1 > pos
; --d
)
9205 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9209 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9210 return isl_bool_false
;
9211 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9212 return isl_bool_false
;
9213 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9214 return isl_bool_false
;
9216 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9217 return isl_bool_true
;
9219 return isl_bool_false
;
9222 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9223 unsigned pos
, isl_int
*val
)
9231 return isl_bool_error
;
9233 return isl_bool_false
;
9235 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9238 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9239 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9240 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9241 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9242 fixed
= isl_bool_false
;
9245 isl_int_set(*val
, v
);
9251 static isl_bool
isl_basic_set_plain_has_fixed_var(
9252 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9254 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9258 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9259 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9261 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9262 return isl_bool_error
;
9263 return isl_basic_map_plain_has_fixed_var(bmap
,
9264 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9267 /* If "bmap" obviously lies on a hyperplane where the given dimension
9268 * has a fixed value, then return that value.
9269 * Otherwise return NaN.
9271 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9272 __isl_keep isl_basic_map
*bmap
,
9273 enum isl_dim_type type
, unsigned pos
)
9281 ctx
= isl_basic_map_get_ctx(bmap
);
9282 v
= isl_val_alloc(ctx
);
9285 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9287 return isl_val_free(v
);
9289 isl_int_set_si(v
->d
, 1);
9293 return isl_val_nan(ctx
);
9296 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9297 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9299 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9300 return isl_bool_error
;
9301 return isl_map_plain_has_fixed_var(map
,
9302 map_offset(map
, type
) - 1 + pos
, val
);
9305 /* If "map" obviously lies on a hyperplane where the given dimension
9306 * has a fixed value, then return that value.
9307 * Otherwise return NaN.
9309 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9310 enum isl_dim_type type
, unsigned pos
)
9318 ctx
= isl_map_get_ctx(map
);
9319 v
= isl_val_alloc(ctx
);
9322 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9324 return isl_val_free(v
);
9326 isl_int_set_si(v
->d
, 1);
9330 return isl_val_nan(ctx
);
9333 /* If "set" obviously lies on a hyperplane where the given dimension
9334 * has a fixed value, then return that value.
9335 * Otherwise return NaN.
9337 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9338 enum isl_dim_type type
, unsigned pos
)
9340 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9343 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9344 * then return this fixed value in *val.
9346 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9347 unsigned dim
, isl_int
*val
)
9349 return isl_basic_set_plain_has_fixed_var(bset
,
9350 isl_basic_set_n_param(bset
) + dim
, val
);
9353 /* Return -1 if the constraint "c1" should be sorted before "c2"
9354 * and 1 if it should be sorted after "c2".
9355 * Return 0 if the two constraints are the same (up to the constant term).
9357 * In particular, if a constraint involves later variables than another
9358 * then it is sorted after this other constraint.
9359 * uset_gist depends on constraints without existentially quantified
9360 * variables sorting first.
9362 * For constraints that have the same latest variable, those
9363 * with the same coefficient for this latest variable (first in absolute value
9364 * and then in actual value) are grouped together.
9365 * This is useful for detecting pairs of constraints that can
9366 * be chained in their printed representation.
9368 * Finally, within a group, constraints are sorted according to
9369 * their coefficients (excluding the constant term).
9371 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9373 isl_int
**c1
= (isl_int
**) p1
;
9374 isl_int
**c2
= (isl_int
**) p2
;
9376 unsigned size
= *(unsigned *) arg
;
9379 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9380 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9385 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9388 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9392 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9395 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9396 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9397 * and 0 if the two constraints are the same (up to the constant term).
9399 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9400 isl_int
*c1
, isl_int
*c2
)
9406 total
= isl_basic_map_total_dim(bmap
);
9407 return sort_constraint_cmp(&c1
, &c2
, &total
);
9410 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9411 __isl_take isl_basic_map
*bmap
)
9417 if (bmap
->n_ineq
== 0)
9419 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9421 total
= isl_basic_map_total_dim(bmap
);
9422 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9423 &sort_constraint_cmp
, &total
) < 0)
9424 return isl_basic_map_free(bmap
);
9425 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9429 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9430 __isl_take isl_basic_set
*bset
)
9432 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9433 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9436 __isl_give isl_basic_map
*isl_basic_map_normalize(
9437 __isl_take isl_basic_map
*bmap
)
9439 bmap
= isl_basic_map_remove_redundancies(bmap
);
9440 bmap
= isl_basic_map_sort_constraints(bmap
);
9443 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9444 __isl_keep isl_basic_map
*bmap2
)
9448 isl_space
*space1
, *space2
;
9450 if (!bmap1
|| !bmap2
)
9455 space1
= isl_basic_map_peek_space(bmap1
);
9456 space2
= isl_basic_map_peek_space(bmap2
);
9457 cmp
= isl_space_cmp(space1
, space2
);
9460 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9461 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9462 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9463 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9464 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9466 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9468 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9470 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9471 return bmap1
->n_eq
- bmap2
->n_eq
;
9472 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9473 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9474 if (bmap1
->n_div
!= bmap2
->n_div
)
9475 return bmap1
->n_div
- bmap2
->n_div
;
9476 total
= isl_basic_map_total_dim(bmap1
);
9477 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9478 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9482 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9483 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9487 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9488 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9495 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9496 __isl_keep isl_basic_set
*bset2
)
9498 return isl_basic_map_plain_cmp(bset1
, bset2
);
9501 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9507 if (set1
->n
!= set2
->n
)
9508 return set1
->n
- set2
->n
;
9510 for (i
= 0; i
< set1
->n
; ++i
) {
9511 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9519 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9520 __isl_keep isl_basic_map
*bmap2
)
9522 if (!bmap1
|| !bmap2
)
9523 return isl_bool_error
;
9524 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9527 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9528 __isl_keep isl_basic_set
*bset2
)
9530 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9531 bset_to_bmap(bset2
));
9534 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9536 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9537 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9539 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9542 /* Sort the basic maps of "map" and remove duplicate basic maps.
9544 * While removing basic maps, we make sure that the basic maps remain
9545 * sorted because isl_map_normalize expects the basic maps of the result
9548 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9552 map
= isl_map_remove_empty_parts(map
);
9555 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9556 for (i
= map
->n
- 1; i
>= 1; --i
) {
9557 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9559 isl_basic_map_free(map
->p
[i
-1]);
9560 for (j
= i
; j
< map
->n
; ++j
)
9561 map
->p
[j
- 1] = map
->p
[j
];
9568 /* Remove obvious duplicates among the basic maps of "map".
9570 * Unlike isl_map_normalize, this function does not remove redundant
9571 * constraints and only removes duplicates that have exactly the same
9572 * constraints in the input. It does sort the constraints and
9573 * the basic maps to ease the detection of duplicates.
9575 * If "map" has already been normalized or if the basic maps are
9576 * disjoint, then there can be no duplicates.
9578 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9581 isl_basic_map
*bmap
;
9587 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9589 for (i
= 0; i
< map
->n
; ++i
) {
9590 bmap
= isl_basic_map_copy(map
->p
[i
]);
9591 bmap
= isl_basic_map_sort_constraints(bmap
);
9593 return isl_map_free(map
);
9594 isl_basic_map_free(map
->p
[i
]);
9598 map
= sort_and_remove_duplicates(map
);
9602 /* We normalize in place, but if anything goes wrong we need
9603 * to return NULL, so we need to make sure we don't change the
9604 * meaning of any possible other copies of map.
9606 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9609 struct isl_basic_map
*bmap
;
9613 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9615 for (i
= 0; i
< map
->n
; ++i
) {
9616 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9619 isl_basic_map_free(map
->p
[i
]);
9623 map
= sort_and_remove_duplicates(map
);
9625 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9632 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9634 return set_from_map(isl_map_normalize(set_to_map(set
)));
9637 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9638 __isl_keep isl_map
*map2
)
9644 return isl_bool_error
;
9647 return isl_bool_true
;
9648 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9649 return isl_bool_false
;
9651 map1
= isl_map_copy(map1
);
9652 map2
= isl_map_copy(map2
);
9653 map1
= isl_map_normalize(map1
);
9654 map2
= isl_map_normalize(map2
);
9657 equal
= map1
->n
== map2
->n
;
9658 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9659 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9669 return isl_bool_error
;
9672 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9673 __isl_keep isl_set
*set2
)
9675 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9678 /* Return the basic maps in "map" as a list.
9680 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9681 __isl_keep isl_map
*map
)
9685 isl_basic_map_list
*list
;
9689 ctx
= isl_map_get_ctx(map
);
9690 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9692 for (i
= 0; i
< map
->n
; ++i
) {
9693 isl_basic_map
*bmap
;
9695 bmap
= isl_basic_map_copy(map
->p
[i
]);
9696 list
= isl_basic_map_list_add(list
, bmap
);
9702 /* Return the intersection of the elements in the non-empty list "list".
9703 * All elements are assumed to live in the same space.
9705 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9706 __isl_take isl_basic_map_list
*list
)
9709 isl_basic_map
*bmap
;
9713 n
= isl_basic_map_list_n_basic_map(list
);
9715 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9716 "expecting non-empty list", goto error
);
9718 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9719 for (i
= 1; i
< n
; ++i
) {
9720 isl_basic_map
*bmap_i
;
9722 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9723 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9726 isl_basic_map_list_free(list
);
9729 isl_basic_map_list_free(list
);
9733 /* Return the intersection of the elements in the non-empty list "list".
9734 * All elements are assumed to live in the same space.
9736 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9737 __isl_take isl_basic_set_list
*list
)
9739 return isl_basic_map_list_intersect(list
);
9742 /* Return the union of the elements of "list".
9743 * The list is required to have at least one element.
9745 __isl_give isl_set
*isl_basic_set_list_union(
9746 __isl_take isl_basic_set_list
*list
)
9750 isl_basic_set
*bset
;
9755 n
= isl_basic_set_list_n_basic_set(list
);
9757 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9758 "expecting non-empty list", goto error
);
9760 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9761 space
= isl_basic_set_get_space(bset
);
9762 isl_basic_set_free(bset
);
9764 set
= isl_set_alloc_space(space
, n
, 0);
9765 for (i
= 0; i
< n
; ++i
) {
9766 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9767 set
= isl_set_add_basic_set(set
, bset
);
9770 isl_basic_set_list_free(list
);
9773 isl_basic_set_list_free(list
);
9777 /* Return the union of the elements in the non-empty list "list".
9778 * All elements are assumed to live in the same space.
9780 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9787 n
= isl_set_list_n_set(list
);
9789 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9790 "expecting non-empty list", goto error
);
9792 set
= isl_set_list_get_set(list
, 0);
9793 for (i
= 1; i
< n
; ++i
) {
9796 set_i
= isl_set_list_get_set(list
, i
);
9797 set
= isl_set_union(set
, set_i
);
9800 isl_set_list_free(list
);
9803 isl_set_list_free(list
);
9807 __isl_give isl_basic_map
*isl_basic_map_product(
9808 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9810 isl_space
*space_result
= NULL
;
9811 struct isl_basic_map
*bmap
;
9812 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9813 struct isl_dim_map
*dim_map1
, *dim_map2
;
9815 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9817 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9818 isl_space_copy(bmap2
->dim
));
9820 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9821 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9822 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9823 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9824 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9826 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9827 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9828 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9829 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9830 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9831 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9832 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9833 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9834 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9835 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9836 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9838 bmap
= isl_basic_map_alloc_space(space_result
,
9839 bmap1
->n_div
+ bmap2
->n_div
,
9840 bmap1
->n_eq
+ bmap2
->n_eq
,
9841 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9842 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9843 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9844 bmap
= isl_basic_map_simplify(bmap
);
9845 return isl_basic_map_finalize(bmap
);
9847 isl_basic_map_free(bmap1
);
9848 isl_basic_map_free(bmap2
);
9852 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9853 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9855 isl_basic_map
*prod
;
9857 prod
= isl_basic_map_product(bmap1
, bmap2
);
9858 prod
= isl_basic_map_flatten(prod
);
9862 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9863 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9865 return isl_basic_map_flat_range_product(bset1
, bset2
);
9868 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9869 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9871 isl_space
*space1
, *space2
;
9872 isl_space
*space_result
= NULL
;
9873 isl_basic_map
*bmap
;
9874 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9875 struct isl_dim_map
*dim_map1
, *dim_map2
;
9877 if (!bmap1
|| !bmap2
)
9880 space1
= isl_basic_map_get_space(bmap1
);
9881 space2
= isl_basic_map_get_space(bmap2
);
9882 space_result
= isl_space_domain_product(space1
, space2
);
9884 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9885 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9886 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9887 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9889 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9890 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9891 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9892 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9893 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9894 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9895 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9896 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9897 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9898 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9899 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9901 bmap
= isl_basic_map_alloc_space(space_result
,
9902 bmap1
->n_div
+ bmap2
->n_div
,
9903 bmap1
->n_eq
+ bmap2
->n_eq
,
9904 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9905 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9906 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9907 bmap
= isl_basic_map_simplify(bmap
);
9908 return isl_basic_map_finalize(bmap
);
9910 isl_basic_map_free(bmap1
);
9911 isl_basic_map_free(bmap2
);
9915 __isl_give isl_basic_map
*isl_basic_map_range_product(
9916 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9919 isl_space
*space_result
= NULL
;
9920 isl_basic_map
*bmap
;
9921 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9922 struct isl_dim_map
*dim_map1
, *dim_map2
;
9924 rational
= isl_basic_map_is_rational(bmap1
);
9925 if (rational
>= 0 && rational
)
9926 rational
= isl_basic_map_is_rational(bmap2
);
9927 if (!bmap1
|| !bmap2
|| rational
< 0)
9930 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9933 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9934 isl_space_copy(bmap2
->dim
));
9936 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9937 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9938 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9939 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9941 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9942 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9943 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9944 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9945 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9946 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9947 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9948 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9949 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9950 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9951 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9953 bmap
= isl_basic_map_alloc_space(space_result
,
9954 bmap1
->n_div
+ bmap2
->n_div
,
9955 bmap1
->n_eq
+ bmap2
->n_eq
,
9956 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9957 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9958 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9960 bmap
= isl_basic_map_set_rational(bmap
);
9961 bmap
= isl_basic_map_simplify(bmap
);
9962 return isl_basic_map_finalize(bmap
);
9964 isl_basic_map_free(bmap1
);
9965 isl_basic_map_free(bmap2
);
9969 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9970 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9972 isl_basic_map
*prod
;
9974 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9975 prod
= isl_basic_map_flatten_range(prod
);
9979 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9980 * and collect the results.
9981 * The result live in the space obtained by calling "space_product"
9982 * on the spaces of "map1" and "map2".
9983 * If "remove_duplicates" is set then the result may contain duplicates
9984 * (even if the inputs do not) and so we try and remove the obvious
9987 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9988 __isl_take isl_map
*map2
,
9989 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9990 __isl_take isl_space
*right
),
9991 __isl_give isl_basic_map
*(*basic_map_product
)(
9992 __isl_take isl_basic_map
*left
,
9993 __isl_take isl_basic_map
*right
),
9994 int remove_duplicates
)
9997 struct isl_map
*result
;
10001 m
= isl_map_has_equal_params(map1
, map2
);
10005 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10006 "parameters don't match", goto error
);
10008 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10009 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10010 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10012 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10013 isl_space_copy(map2
->dim
)),
10014 map1
->n
* map2
->n
, flags
);
10017 for (i
= 0; i
< map1
->n
; ++i
)
10018 for (j
= 0; j
< map2
->n
; ++j
) {
10019 struct isl_basic_map
*part
;
10020 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10021 isl_basic_map_copy(map2
->p
[j
]));
10022 if (isl_basic_map_is_empty(part
))
10023 isl_basic_map_free(part
);
10025 result
= isl_map_add_basic_map(result
, part
);
10029 if (remove_duplicates
)
10030 result
= isl_map_remove_obvious_duplicates(result
);
10031 isl_map_free(map1
);
10032 isl_map_free(map2
);
10035 isl_map_free(map1
);
10036 isl_map_free(map2
);
10040 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10042 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10043 __isl_take isl_map
*map2
)
10045 return map_product(map1
, map2
, &isl_space_product
,
10046 &isl_basic_map_product
, 0);
10049 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10050 __isl_take isl_map
*map2
)
10052 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10055 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10057 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10058 __isl_take isl_map
*map2
)
10062 prod
= isl_map_product(map1
, map2
);
10063 prod
= isl_map_flatten(prod
);
10067 /* Given two set A and B, construct its Cartesian product A x B.
10069 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10071 return isl_map_range_product(set1
, set2
);
10074 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10075 __isl_take isl_set
*set2
)
10077 return isl_map_flat_range_product(set1
, set2
);
10080 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10082 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10083 __isl_take isl_map
*map2
)
10085 return map_product(map1
, map2
, &isl_space_domain_product
,
10086 &isl_basic_map_domain_product
, 1);
10089 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10091 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10092 __isl_take isl_map
*map2
)
10094 return map_product(map1
, map2
, &isl_space_range_product
,
10095 &isl_basic_map_range_product
, 1);
10098 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10099 __isl_take isl_map
*map2
)
10101 return isl_map_align_params_map_map_and(map1
, map2
,
10102 &map_domain_product_aligned
);
10105 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10106 __isl_take isl_map
*map2
)
10108 return isl_map_align_params_map_map_and(map1
, map2
,
10109 &map_range_product_aligned
);
10112 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10114 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10117 int total1
, keep1
, total2
, keep2
;
10121 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10122 !isl_space_range_is_wrapping(map
->dim
))
10123 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10124 "not a product", return isl_map_free(map
));
10126 space
= isl_map_get_space(map
);
10127 total1
= isl_space_dim(space
, isl_dim_in
);
10128 total2
= isl_space_dim(space
, isl_dim_out
);
10129 space
= isl_space_factor_domain(space
);
10130 keep1
= isl_space_dim(space
, isl_dim_in
);
10131 keep2
= isl_space_dim(space
, isl_dim_out
);
10132 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10133 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10134 map
= isl_map_reset_space(map
, space
);
10139 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10141 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10144 int total1
, keep1
, total2
, keep2
;
10148 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10149 !isl_space_range_is_wrapping(map
->dim
))
10150 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10151 "not a product", return isl_map_free(map
));
10153 space
= isl_map_get_space(map
);
10154 total1
= isl_space_dim(space
, isl_dim_in
);
10155 total2
= isl_space_dim(space
, isl_dim_out
);
10156 space
= isl_space_factor_range(space
);
10157 keep1
= isl_space_dim(space
, isl_dim_in
);
10158 keep2
= isl_space_dim(space
, isl_dim_out
);
10159 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10160 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10161 map
= isl_map_reset_space(map
, space
);
10166 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10168 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10175 if (!isl_space_domain_is_wrapping(map
->dim
))
10176 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10177 "domain is not a product", return isl_map_free(map
));
10179 space
= isl_map_get_space(map
);
10180 total
= isl_space_dim(space
, isl_dim_in
);
10181 space
= isl_space_domain_factor_domain(space
);
10182 keep
= isl_space_dim(space
, isl_dim_in
);
10183 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10184 map
= isl_map_reset_space(map
, space
);
10189 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10191 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10198 if (!isl_space_domain_is_wrapping(map
->dim
))
10199 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10200 "domain is not a product", return isl_map_free(map
));
10202 space
= isl_map_get_space(map
);
10203 total
= isl_space_dim(space
, isl_dim_in
);
10204 space
= isl_space_domain_factor_range(space
);
10205 keep
= isl_space_dim(space
, isl_dim_in
);
10206 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10207 map
= isl_map_reset_space(map
, space
);
10212 /* Given a map A -> [B -> C], extract the map A -> B.
10214 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10221 if (!isl_space_range_is_wrapping(map
->dim
))
10222 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10223 "range is not a product", return isl_map_free(map
));
10225 space
= isl_map_get_space(map
);
10226 total
= isl_space_dim(space
, isl_dim_out
);
10227 space
= isl_space_range_factor_domain(space
);
10228 keep
= isl_space_dim(space
, isl_dim_out
);
10229 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10230 map
= isl_map_reset_space(map
, space
);
10235 /* Given a map A -> [B -> C], extract the map A -> C.
10237 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10244 if (!isl_space_range_is_wrapping(map
->dim
))
10245 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10246 "range is not a product", return isl_map_free(map
));
10248 space
= isl_map_get_space(map
);
10249 total
= isl_space_dim(space
, isl_dim_out
);
10250 space
= isl_space_range_factor_range(space
);
10251 keep
= isl_space_dim(space
, isl_dim_out
);
10252 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10253 map
= isl_map_reset_space(map
, space
);
10258 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10260 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10261 __isl_take isl_map
*map2
)
10265 prod
= isl_map_domain_product(map1
, map2
);
10266 prod
= isl_map_flatten_domain(prod
);
10270 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10272 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10273 __isl_take isl_map
*map2
)
10277 prod
= isl_map_range_product(map1
, map2
);
10278 prod
= isl_map_flatten_range(prod
);
10282 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10285 uint32_t hash
= isl_hash_init();
10290 bmap
= isl_basic_map_copy(bmap
);
10291 bmap
= isl_basic_map_normalize(bmap
);
10294 total
= isl_basic_map_total_dim(bmap
);
10295 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10296 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10298 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10299 isl_hash_hash(hash
, c_hash
);
10301 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10302 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10304 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10305 isl_hash_hash(hash
, c_hash
);
10307 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10308 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10310 if (isl_int_is_zero(bmap
->div
[i
][0]))
10312 isl_hash_byte(hash
, i
& 0xFF);
10313 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10314 isl_hash_hash(hash
, c_hash
);
10316 isl_basic_map_free(bmap
);
10320 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10322 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10325 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10332 map
= isl_map_copy(map
);
10333 map
= isl_map_normalize(map
);
10337 hash
= isl_hash_init();
10338 for (i
= 0; i
< map
->n
; ++i
) {
10339 uint32_t bmap_hash
;
10340 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10341 isl_hash_hash(hash
, bmap_hash
);
10349 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10351 return isl_map_get_hash(set_to_map(set
));
10354 /* Return the number of basic maps in the (current) representation of "map".
10356 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10358 return map
? map
->n
: 0;
10361 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10363 return set
? set
->n
: 0;
10366 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10367 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10372 return isl_stat_error
;
10374 for (i
= 0; i
< map
->n
; ++i
)
10375 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10376 return isl_stat_error
;
10378 return isl_stat_ok
;
10381 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10382 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10387 return isl_stat_error
;
10389 for (i
= 0; i
< set
->n
; ++i
)
10390 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10391 return isl_stat_error
;
10393 return isl_stat_ok
;
10396 /* Return a list of basic sets, the union of which is equal to "set".
10398 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10399 __isl_keep isl_set
*set
)
10402 isl_basic_set_list
*list
;
10407 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10408 for (i
= 0; i
< set
->n
; ++i
) {
10409 isl_basic_set
*bset
;
10411 bset
= isl_basic_set_copy(set
->p
[i
]);
10412 list
= isl_basic_set_list_add(list
, bset
);
10418 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10425 bset
= isl_basic_set_cow(bset
);
10429 space
= isl_basic_set_get_space(bset
);
10430 space
= isl_space_lift(space
, bset
->n_div
);
10433 isl_space_free(bset
->dim
);
10435 bset
->extra
-= bset
->n_div
;
10438 bset
= isl_basic_set_finalize(bset
);
10442 isl_basic_set_free(bset
);
10446 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10452 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10457 set
= isl_set_cow(set
);
10461 n_div
= set
->p
[0]->n_div
;
10462 space
= isl_set_get_space(set
);
10463 space
= isl_space_lift(space
, n_div
);
10466 isl_space_free(set
->dim
);
10469 for (i
= 0; i
< set
->n
; ++i
) {
10470 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10481 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10489 dim
= isl_basic_set_total_dim(bset
);
10490 size
+= bset
->n_eq
* (1 + dim
);
10491 size
+= bset
->n_ineq
* (1 + dim
);
10492 size
+= bset
->n_div
* (2 + dim
);
10497 int isl_set_size(__isl_keep isl_set
*set
)
10505 for (i
= 0; i
< set
->n
; ++i
)
10506 size
+= isl_basic_set_size(set
->p
[i
]);
10511 /* Check if there is any lower bound (if lower == 0) and/or upper
10512 * bound (if upper == 0) on the specified dim.
10514 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10515 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10519 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10520 return isl_bool_error
;
10522 pos
+= isl_basic_map_offset(bmap
, type
);
10524 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10525 if (isl_int_is_zero(bmap
->div
[i
][0]))
10527 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10528 return isl_bool_true
;
10531 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10532 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10533 return isl_bool_true
;
10535 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10536 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10543 return lower
&& upper
;
10546 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10547 enum isl_dim_type type
, unsigned pos
)
10549 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10552 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10553 enum isl_dim_type type
, unsigned pos
)
10555 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10558 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10559 enum isl_dim_type type
, unsigned pos
)
10561 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10564 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10565 enum isl_dim_type type
, unsigned pos
)
10570 return isl_bool_error
;
10572 for (i
= 0; i
< map
->n
; ++i
) {
10574 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10575 if (bounded
< 0 || !bounded
)
10579 return isl_bool_true
;
10582 /* Return true if the specified dim is involved in both an upper bound
10583 * and a lower bound.
10585 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10586 enum isl_dim_type type
, unsigned pos
)
10588 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10591 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10593 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10594 enum isl_dim_type type
, unsigned pos
,
10595 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10596 enum isl_dim_type type
, unsigned pos
))
10601 return isl_bool_error
;
10603 for (i
= 0; i
< map
->n
; ++i
) {
10605 bounded
= fn(map
->p
[i
], type
, pos
);
10606 if (bounded
< 0 || bounded
)
10610 return isl_bool_false
;
10613 /* Return 1 if the specified dim is involved in any lower bound.
10615 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10616 enum isl_dim_type type
, unsigned pos
)
10618 return has_any_bound(set
, type
, pos
,
10619 &isl_basic_map_dim_has_lower_bound
);
10622 /* Return 1 if the specified dim is involved in any upper bound.
10624 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10625 enum isl_dim_type type
, unsigned pos
)
10627 return has_any_bound(set
, type
, pos
,
10628 &isl_basic_map_dim_has_upper_bound
);
10631 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10633 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10634 enum isl_dim_type type
, unsigned pos
,
10635 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10636 enum isl_dim_type type
, unsigned pos
))
10641 return isl_bool_error
;
10643 for (i
= 0; i
< map
->n
; ++i
) {
10645 bounded
= fn(map
->p
[i
], type
, pos
);
10646 if (bounded
< 0 || !bounded
)
10650 return isl_bool_true
;
10653 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10655 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10656 enum isl_dim_type type
, unsigned pos
)
10658 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10661 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10663 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10664 enum isl_dim_type type
, unsigned pos
)
10666 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10669 /* For each of the "n" variables starting at "first", determine
10670 * the sign of the variable and put the results in the first "n"
10671 * elements of the array "signs".
10673 * 1 means that the variable is non-negative
10674 * -1 means that the variable is non-positive
10675 * 0 means the variable attains both positive and negative values.
10677 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10678 unsigned first
, unsigned n
, int *signs
)
10680 isl_vec
*bound
= NULL
;
10681 struct isl_tab
*tab
= NULL
;
10682 struct isl_tab_undo
*snap
;
10685 if (!bset
|| !signs
)
10686 return isl_stat_error
;
10688 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10689 tab
= isl_tab_from_basic_set(bset
, 0);
10690 if (!bound
|| !tab
)
10693 isl_seq_clr(bound
->el
, bound
->size
);
10694 isl_int_set_si(bound
->el
[0], -1);
10696 snap
= isl_tab_snap(tab
);
10697 for (i
= 0; i
< n
; ++i
) {
10700 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10701 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10703 empty
= tab
->empty
;
10704 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10705 if (isl_tab_rollback(tab
, snap
) < 0)
10713 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10714 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10716 empty
= tab
->empty
;
10717 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10718 if (isl_tab_rollback(tab
, snap
) < 0)
10721 signs
[i
] = empty
? -1 : 0;
10725 isl_vec_free(bound
);
10726 return isl_stat_ok
;
10729 isl_vec_free(bound
);
10730 return isl_stat_error
;
10733 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10734 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10736 if (!bset
|| !signs
)
10737 return isl_stat_error
;
10738 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10739 return isl_stat_error
;
10741 first
+= pos(bset
->dim
, type
) - 1;
10742 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10745 /* Is it possible for the integer division "div" to depend (possibly
10746 * indirectly) on any output dimensions?
10748 * If the div is undefined, then we conservatively assume that it
10749 * may depend on them.
10750 * Otherwise, we check if it actually depends on them or on any integer
10751 * divisions that may depend on them.
10753 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10756 unsigned n_out
, o_out
;
10757 unsigned n_div
, o_div
;
10759 if (isl_int_is_zero(bmap
->div
[div
][0]))
10760 return isl_bool_true
;
10762 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10763 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10765 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10766 return isl_bool_true
;
10768 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10769 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10771 for (i
= 0; i
< n_div
; ++i
) {
10772 isl_bool may_involve
;
10774 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10776 may_involve
= div_may_involve_output(bmap
, i
);
10777 if (may_involve
< 0 || may_involve
)
10778 return may_involve
;
10781 return isl_bool_false
;
10784 /* Return the first integer division of "bmap" in the range
10785 * [first, first + n[ that may depend on any output dimensions and
10786 * that has a non-zero coefficient in "c" (where the first coefficient
10787 * in "c" corresponds to integer division "first").
10789 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10790 isl_int
*c
, int first
, int n
)
10797 for (k
= first
; k
< first
+ n
; ++k
) {
10798 isl_bool may_involve
;
10800 if (isl_int_is_zero(c
[k
]))
10802 may_involve
= div_may_involve_output(bmap
, k
);
10803 if (may_involve
< 0)
10812 /* Look for a pair of inequality constraints in "bmap" of the form
10814 * -l + i >= 0 or i >= l
10816 * n + l - i >= 0 or i <= l + n
10818 * with n < "m" and i the output dimension at position "pos".
10819 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10820 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10821 * and earlier output dimensions, as well as integer divisions that do
10822 * not involve any of the output dimensions.
10824 * Return the index of the first inequality constraint or bmap->n_ineq
10825 * if no such pair can be found.
10827 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10828 int pos
, isl_int m
)
10833 unsigned n_div
, o_div
;
10834 unsigned n_out
, o_out
;
10840 ctx
= isl_basic_map_get_ctx(bmap
);
10841 total
= isl_basic_map_total_dim(bmap
);
10842 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10843 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10844 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10845 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10846 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10847 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10849 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10850 n_out
- (pos
+ 1)) != -1)
10852 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10855 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10856 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10859 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10860 bmap
->ineq
[j
] + 1, total
))
10864 if (j
>= bmap
->n_ineq
)
10866 isl_int_add(bmap
->ineq
[i
][0],
10867 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10868 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10869 isl_int_sub(bmap
->ineq
[i
][0],
10870 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10873 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10879 return bmap
->n_ineq
;
10882 /* Return the index of the equality of "bmap" that defines
10883 * the output dimension "pos" in terms of earlier dimensions.
10884 * The equality may also involve integer divisions, as long
10885 * as those integer divisions are defined in terms of
10886 * parameters or input dimensions.
10887 * In this case, *div is set to the number of integer divisions and
10888 * *ineq is set to the number of inequality constraints (provided
10889 * div and ineq are not NULL).
10891 * The equality may also involve a single integer division involving
10892 * the output dimensions (typically only output dimension "pos") as
10893 * long as the coefficient of output dimension "pos" is 1 or -1 and
10894 * there is a pair of constraints i >= l and i <= l + n, with i referring
10895 * to output dimension "pos", l an expression involving only earlier
10896 * dimensions and n smaller than the coefficient of the integer division
10897 * in the equality. In this case, the output dimension can be defined
10898 * in terms of a modulo expression that does not involve the integer division.
10899 * *div is then set to this single integer division and
10900 * *ineq is set to the index of constraint i >= l.
10902 * Return bmap->n_eq if there is no such equality.
10903 * Return -1 on error.
10905 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10906 int pos
, int *div
, int *ineq
)
10909 unsigned n_out
, o_out
;
10910 unsigned n_div
, o_div
;
10915 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10916 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10917 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10918 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10921 *ineq
= bmap
->n_ineq
;
10924 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10925 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10927 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10928 n_out
- (pos
+ 1)) != -1)
10930 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10934 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10935 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10937 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10938 k
+ 1, n_div
- (k
+1)) < n_div
)
10940 l
= find_modulo_constraint_pair(bmap
, pos
,
10941 bmap
->eq
[j
][o_div
+ k
]);
10944 if (l
>= bmap
->n_ineq
)
10956 /* Check if the given basic map is obviously single-valued.
10957 * In particular, for each output dimension, check that there is
10958 * an equality that defines the output dimension in terms of
10959 * earlier dimensions.
10961 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10967 return isl_bool_error
;
10969 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10971 for (i
= 0; i
< n_out
; ++i
) {
10974 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10977 return isl_bool_error
;
10978 if (eq
>= bmap
->n_eq
)
10979 return isl_bool_false
;
10982 return isl_bool_true
;
10985 /* Check if the given basic map is single-valued.
10986 * We simply compute
10990 * and check if the result is a subset of the identity mapping.
10992 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10995 isl_basic_map
*test
;
10999 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11003 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11004 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11006 space
= isl_basic_map_get_space(bmap
);
11007 space
= isl_space_map_from_set(isl_space_range(space
));
11008 id
= isl_basic_map_identity(space
);
11010 sv
= isl_basic_map_is_subset(test
, id
);
11012 isl_basic_map_free(test
);
11013 isl_basic_map_free(id
);
11018 /* Check if the given map is obviously single-valued.
11020 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11023 return isl_bool_error
;
11025 return isl_bool_true
;
11027 return isl_bool_false
;
11029 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11032 /* Check if the given map is single-valued.
11033 * We simply compute
11037 * and check if the result is a subset of the identity mapping.
11039 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11046 sv
= isl_map_plain_is_single_valued(map
);
11050 test
= isl_map_reverse(isl_map_copy(map
));
11051 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11053 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11054 id
= isl_map_identity(dim
);
11056 sv
= isl_map_is_subset(test
, id
);
11058 isl_map_free(test
);
11064 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11068 map
= isl_map_copy(map
);
11069 map
= isl_map_reverse(map
);
11070 in
= isl_map_is_single_valued(map
);
11076 /* Check if the given map is obviously injective.
11078 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11082 map
= isl_map_copy(map
);
11083 map
= isl_map_reverse(map
);
11084 in
= isl_map_plain_is_single_valued(map
);
11090 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11094 sv
= isl_map_is_single_valued(map
);
11098 return isl_map_is_injective(map
);
11101 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11103 return isl_map_is_single_valued(set_to_map(set
));
11106 /* Does "map" only map elements to themselves?
11108 * If the domain and range spaces are different, then "map"
11109 * is considered not to be an identity relation, even if it is empty.
11110 * Otherwise, construct the maximal identity relation and
11111 * check whether "map" is a subset of this relation.
11113 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11117 isl_bool equal
, is_identity
;
11119 space
= isl_map_get_space(map
);
11120 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11121 isl_space_free(space
);
11122 if (equal
< 0 || !equal
)
11125 id
= isl_map_identity(isl_map_get_space(map
));
11126 is_identity
= isl_map_is_subset(map
, id
);
11129 return is_identity
;
11132 int isl_map_is_translation(__isl_keep isl_map
*map
)
11137 delta
= isl_map_deltas(isl_map_copy(map
));
11138 ok
= isl_set_is_singleton(delta
);
11139 isl_set_free(delta
);
11144 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11146 if (isl_seq_first_non_zero(p
, pos
) != -1)
11148 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11153 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11160 return isl_bool_error
;
11162 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11163 return isl_bool_false
;
11165 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11166 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11167 for (j
= 0; j
< nvar
; ++j
) {
11168 int lower
= 0, upper
= 0;
11169 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11170 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11172 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11173 return isl_bool_false
;
11176 if (i
< bset
->n_eq
)
11178 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11179 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11181 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11182 return isl_bool_false
;
11183 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11188 if (!lower
|| !upper
)
11189 return isl_bool_false
;
11192 return isl_bool_true
;
11195 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11198 return isl_bool_error
;
11200 return isl_bool_false
;
11202 return isl_basic_set_is_box(set
->p
[0]);
11205 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11208 return isl_bool_error
;
11210 return isl_space_is_wrapping(bset
->dim
);
11213 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11216 return isl_bool_error
;
11218 return isl_space_is_wrapping(set
->dim
);
11221 /* Modify the space of "map" through a call to "change".
11222 * If "can_change" is set (not NULL), then first call it to check
11223 * if the modification is allowed, printing the error message "cannot_change"
11226 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11227 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11228 const char *cannot_change
,
11229 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11237 ok
= can_change
? can_change(map
) : isl_bool_true
;
11239 return isl_map_free(map
);
11241 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11242 return isl_map_free(map
));
11244 space
= change(isl_map_get_space(map
));
11245 map
= isl_map_reset_space(map
, space
);
11250 /* Is the domain of "map" a wrapped relation?
11252 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11255 return isl_bool_error
;
11257 return isl_space_domain_is_wrapping(map
->dim
);
11260 /* Does "map" have a wrapped relation in both domain and range?
11262 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11264 return isl_space_is_product(isl_map_peek_space(map
));
11267 /* Is the range of "map" a wrapped relation?
11269 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11272 return isl_bool_error
;
11274 return isl_space_range_is_wrapping(map
->dim
);
11277 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11279 bmap
= isl_basic_map_cow(bmap
);
11283 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11287 bmap
= isl_basic_map_finalize(bmap
);
11289 return bset_from_bmap(bmap
);
11291 isl_basic_map_free(bmap
);
11295 /* Given a map A -> B, return the set (A -> B).
11297 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11299 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11302 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11304 bset
= isl_basic_set_cow(bset
);
11308 bset
->dim
= isl_space_unwrap(bset
->dim
);
11312 bset
= isl_basic_set_finalize(bset
);
11314 return bset_to_bmap(bset
);
11316 isl_basic_set_free(bset
);
11320 /* Given a set (A -> B), return the map A -> B.
11321 * Error out if "set" is not of the form (A -> B).
11323 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11325 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11326 "not a wrapping set", &isl_space_unwrap
);
11329 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11330 enum isl_dim_type type
)
11335 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11338 bmap
= isl_basic_map_cow(bmap
);
11342 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11346 bmap
= isl_basic_map_finalize(bmap
);
11350 isl_basic_map_free(bmap
);
11354 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11355 enum isl_dim_type type
)
11362 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11365 map
= isl_map_cow(map
);
11369 for (i
= 0; i
< map
->n
; ++i
) {
11370 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11374 map
->dim
= isl_space_reset(map
->dim
, type
);
11384 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11389 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11392 bmap
= isl_basic_map_cow(bmap
);
11396 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11400 bmap
= isl_basic_map_finalize(bmap
);
11404 isl_basic_map_free(bmap
);
11408 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11410 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11413 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11414 __isl_take isl_basic_map
*bmap
)
11419 if (!bmap
->dim
->nested
[0])
11422 bmap
= isl_basic_map_cow(bmap
);
11426 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11430 bmap
= isl_basic_map_finalize(bmap
);
11434 isl_basic_map_free(bmap
);
11438 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11439 __isl_take isl_basic_map
*bmap
)
11444 if (!bmap
->dim
->nested
[1])
11447 bmap
= isl_basic_map_cow(bmap
);
11451 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11455 bmap
= isl_basic_map_finalize(bmap
);
11459 isl_basic_map_free(bmap
);
11463 /* Remove any internal structure from the spaces of domain and range of "map".
11465 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11470 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11473 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11476 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11478 return set_from_map(isl_map_flatten(set_to_map(set
)));
11481 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11483 isl_space
*space
, *flat_space
;
11486 space
= isl_set_get_space(set
);
11487 flat_space
= isl_space_flatten(isl_space_copy(space
));
11488 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11490 map
= isl_map_intersect_domain(map
, set
);
11495 /* Remove any internal structure from the space of the domain of "map".
11497 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11502 if (!map
->dim
->nested
[0])
11505 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11508 /* Remove any internal structure from the space of the range of "map".
11510 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11515 if (!map
->dim
->nested
[1])
11518 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11521 /* Reorder the dimensions of "bmap" according to the given dim_map
11522 * and set the dimension specification to "space" and
11523 * perform Gaussian elimination on the result.
11525 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11526 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11528 isl_basic_map
*res
;
11532 if (!bmap
|| !space
|| !dim_map
)
11535 flags
= bmap
->flags
;
11536 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11537 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11538 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11539 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11540 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11541 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11543 res
->flags
= flags
;
11544 res
= isl_basic_map_gauss(res
, NULL
);
11545 res
= isl_basic_map_finalize(res
);
11548 isl_dim_map_free(dim_map
);
11549 isl_basic_map_free(bmap
);
11550 isl_space_free(space
);
11554 /* Reorder the dimensions of "map" according to given reordering.
11556 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11557 __isl_take isl_reordering
*r
)
11560 struct isl_dim_map
*dim_map
;
11562 map
= isl_map_cow(map
);
11563 dim_map
= isl_dim_map_from_reordering(r
);
11564 if (!map
|| !r
|| !dim_map
)
11567 for (i
= 0; i
< map
->n
; ++i
) {
11568 struct isl_dim_map
*dim_map_i
;
11571 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11573 space
= isl_reordering_get_space(r
);
11574 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11580 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11581 map
= isl_map_unmark_normalized(map
);
11583 isl_reordering_free(r
);
11584 isl_dim_map_free(dim_map
);
11587 isl_dim_map_free(dim_map
);
11589 isl_reordering_free(r
);
11593 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11594 __isl_take isl_reordering
*r
)
11596 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11599 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11600 __isl_take isl_space
*model
)
11605 if (!map
|| !model
)
11608 ctx
= isl_space_get_ctx(model
);
11609 if (!isl_space_has_named_params(model
))
11610 isl_die(ctx
, isl_error_invalid
,
11611 "model has unnamed parameters", goto error
);
11612 if (isl_map_check_named_params(map
) < 0)
11614 aligned
= isl_map_space_has_equal_params(map
, model
);
11618 isl_reordering
*exp
;
11620 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11621 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11622 map
= isl_map_realign(map
, exp
);
11625 isl_space_free(model
);
11628 isl_space_free(model
);
11633 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11634 __isl_take isl_space
*model
)
11636 return isl_map_align_params(set
, model
);
11639 /* Align the parameters of "bmap" to those of "model", introducing
11640 * additional parameters if needed.
11642 __isl_give isl_basic_map
*isl_basic_map_align_params(
11643 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11646 isl_bool equal_params
;
11648 if (!bmap
|| !model
)
11651 ctx
= isl_space_get_ctx(model
);
11652 if (!isl_space_has_named_params(model
))
11653 isl_die(ctx
, isl_error_invalid
,
11654 "model has unnamed parameters", goto error
);
11655 if (isl_basic_map_check_named_params(bmap
) < 0)
11657 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11658 if (equal_params
< 0)
11660 if (!equal_params
) {
11661 isl_reordering
*exp
;
11662 struct isl_dim_map
*dim_map
;
11664 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11665 exp
= isl_reordering_extend_space(exp
,
11666 isl_basic_map_get_space(bmap
));
11667 dim_map
= isl_dim_map_from_reordering(exp
);
11668 bmap
= isl_basic_map_realign(bmap
,
11669 isl_reordering_get_space(exp
),
11670 isl_dim_map_extend(dim_map
, bmap
));
11671 isl_reordering_free(exp
);
11672 isl_dim_map_free(dim_map
);
11675 isl_space_free(model
);
11678 isl_space_free(model
);
11679 isl_basic_map_free(bmap
);
11683 /* Do "bset" and "space" have the same parameters?
11685 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11686 __isl_keep isl_space
*space
)
11688 isl_space
*bset_space
;
11690 bset_space
= isl_basic_set_peek_space(bset
);
11691 return isl_space_has_equal_params(bset_space
, space
);
11694 /* Do "map" and "space" have the same parameters?
11696 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11697 __isl_keep isl_space
*space
)
11699 isl_space
*map_space
;
11701 map_space
= isl_map_peek_space(map
);
11702 return isl_space_has_equal_params(map_space
, space
);
11705 /* Do "set" and "space" have the same parameters?
11707 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11708 __isl_keep isl_space
*space
)
11710 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11713 /* Align the parameters of "bset" to those of "model", introducing
11714 * additional parameters if needed.
11716 __isl_give isl_basic_set
*isl_basic_set_align_params(
11717 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11719 return isl_basic_map_align_params(bset
, model
);
11722 /* Drop all parameters not referenced by "map".
11724 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11728 if (isl_map_check_named_params(map
) < 0)
11729 return isl_map_free(map
);
11731 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11734 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11736 return isl_map_free(map
);
11738 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11744 /* Drop all parameters not referenced by "set".
11746 __isl_give isl_set
*isl_set_drop_unused_params(
11747 __isl_take isl_set
*set
)
11749 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11752 /* Drop all parameters not referenced by "bmap".
11754 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11755 __isl_take isl_basic_map
*bmap
)
11759 if (isl_basic_map_check_named_params(bmap
) < 0)
11760 return isl_basic_map_free(bmap
);
11762 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11765 involves
= isl_basic_map_involves_dims(bmap
,
11766 isl_dim_param
, i
, 1);
11768 return isl_basic_map_free(bmap
);
11770 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11776 /* Drop all parameters not referenced by "bset".
11778 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11779 __isl_take isl_basic_set
*bset
)
11781 return bset_from_bmap(isl_basic_map_drop_unused_params(
11782 bset_to_bmap(bset
)));
11785 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11786 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11787 enum isl_dim_type c2
, enum isl_dim_type c3
,
11788 enum isl_dim_type c4
, enum isl_dim_type c5
)
11790 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11791 struct isl_mat
*mat
;
11797 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11798 isl_basic_map_total_dim(bmap
) + 1);
11801 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11802 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11803 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11804 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11805 isl_int_set(mat
->row
[i
][pos
],
11806 bmap
->eq
[i
][off
+ k
]);
11814 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11815 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11816 enum isl_dim_type c2
, enum isl_dim_type c3
,
11817 enum isl_dim_type c4
, enum isl_dim_type c5
)
11819 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11820 struct isl_mat
*mat
;
11826 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11827 isl_basic_map_total_dim(bmap
) + 1);
11830 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11831 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11832 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11833 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11834 isl_int_set(mat
->row
[i
][pos
],
11835 bmap
->ineq
[i
][off
+ k
]);
11843 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11844 __isl_take isl_space
*space
,
11845 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11846 enum isl_dim_type c2
, enum isl_dim_type c3
,
11847 enum isl_dim_type c4
, enum isl_dim_type c5
)
11849 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11850 isl_basic_map
*bmap
= NULL
;
11856 if (!space
|| !eq
|| !ineq
)
11859 if (eq
->n_col
!= ineq
->n_col
)
11860 isl_die(space
->ctx
, isl_error_invalid
,
11861 "equalities and inequalities matrices should have "
11862 "same number of columns", goto error
);
11864 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11866 if (eq
->n_col
< total
)
11867 isl_die(space
->ctx
, isl_error_invalid
,
11868 "number of columns too small", goto error
);
11870 extra
= eq
->n_col
- total
;
11872 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11873 eq
->n_row
, ineq
->n_row
);
11876 for (i
= 0; i
< extra
; ++i
) {
11877 k
= isl_basic_map_alloc_div(bmap
);
11880 isl_int_set_si(bmap
->div
[k
][0], 0);
11882 for (i
= 0; i
< eq
->n_row
; ++i
) {
11883 l
= isl_basic_map_alloc_equality(bmap
);
11886 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11887 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11888 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11889 isl_int_set(bmap
->eq
[l
][off
+ k
],
11895 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11896 l
= isl_basic_map_alloc_inequality(bmap
);
11899 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11900 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11901 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11902 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11903 ineq
->row
[i
][pos
]);
11909 isl_space_free(space
);
11911 isl_mat_free(ineq
);
11913 bmap
= isl_basic_map_simplify(bmap
);
11914 return isl_basic_map_finalize(bmap
);
11916 isl_space_free(space
);
11918 isl_mat_free(ineq
);
11919 isl_basic_map_free(bmap
);
11923 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11924 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11925 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11927 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11928 c1
, c2
, c3
, c4
, isl_dim_in
);
11931 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11932 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11933 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11935 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11936 c1
, c2
, c3
, c4
, isl_dim_in
);
11939 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11940 __isl_take isl_space
*dim
,
11941 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11942 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11944 isl_basic_map
*bmap
;
11945 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11946 c1
, c2
, c3
, c4
, isl_dim_in
);
11947 return bset_from_bmap(bmap
);
11950 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11953 return isl_bool_error
;
11955 return isl_space_can_zip(bmap
->dim
);
11958 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11961 return isl_bool_error
;
11963 return isl_space_can_zip(map
->dim
);
11966 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11967 * (A -> C) -> (B -> D).
11969 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11978 if (!isl_basic_map_can_zip(bmap
))
11979 isl_die(bmap
->ctx
, isl_error_invalid
,
11980 "basic map cannot be zipped", goto error
);
11981 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11982 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11983 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11984 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11985 bmap
= isl_basic_map_cow(bmap
);
11986 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11989 bmap
->dim
= isl_space_zip(bmap
->dim
);
11992 bmap
= isl_basic_map_mark_final(bmap
);
11995 isl_basic_map_free(bmap
);
11999 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12000 * (A -> C) -> (B -> D).
12002 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12009 if (!isl_map_can_zip(map
))
12010 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12013 map
= isl_map_cow(map
);
12017 for (i
= 0; i
< map
->n
; ++i
) {
12018 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12023 map
->dim
= isl_space_zip(map
->dim
);
12033 /* Can we apply isl_basic_map_curry to "bmap"?
12034 * That is, does it have a nested relation in its domain?
12036 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12039 return isl_bool_error
;
12041 return isl_space_can_curry(bmap
->dim
);
12044 /* Can we apply isl_map_curry to "map"?
12045 * That is, does it have a nested relation in its domain?
12047 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12050 return isl_bool_error
;
12052 return isl_space_can_curry(map
->dim
);
12055 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12058 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12064 if (!isl_basic_map_can_curry(bmap
))
12065 isl_die(bmap
->ctx
, isl_error_invalid
,
12066 "basic map cannot be curried", goto error
);
12067 bmap
= isl_basic_map_cow(bmap
);
12070 bmap
->dim
= isl_space_curry(bmap
->dim
);
12073 bmap
= isl_basic_map_mark_final(bmap
);
12076 isl_basic_map_free(bmap
);
12080 /* Given a map (A -> B) -> C, return the corresponding map
12083 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12085 return isl_map_change_space(map
, &isl_map_can_curry
,
12086 "map cannot be curried", &isl_space_curry
);
12089 /* Can isl_map_range_curry be applied to "map"?
12090 * That is, does it have a nested relation in its range,
12091 * the domain of which is itself a nested relation?
12093 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12096 return isl_bool_error
;
12098 return isl_space_can_range_curry(map
->dim
);
12101 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12102 * A -> (B -> (C -> D)).
12104 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12106 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12107 "map range cannot be curried",
12108 &isl_space_range_curry
);
12111 /* Can we apply isl_basic_map_uncurry to "bmap"?
12112 * That is, does it have a nested relation in its domain?
12114 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12117 return isl_bool_error
;
12119 return isl_space_can_uncurry(bmap
->dim
);
12122 /* Can we apply isl_map_uncurry to "map"?
12123 * That is, does it have a nested relation in its domain?
12125 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12128 return isl_bool_error
;
12130 return isl_space_can_uncurry(map
->dim
);
12133 /* Given a basic map A -> (B -> C), return the corresponding basic map
12136 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12142 if (!isl_basic_map_can_uncurry(bmap
))
12143 isl_die(bmap
->ctx
, isl_error_invalid
,
12144 "basic map cannot be uncurried",
12145 return isl_basic_map_free(bmap
));
12146 bmap
= isl_basic_map_cow(bmap
);
12149 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12151 return isl_basic_map_free(bmap
);
12152 bmap
= isl_basic_map_mark_final(bmap
);
12156 /* Given a map A -> (B -> C), return the corresponding map
12159 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12161 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12162 "map cannot be uncurried", &isl_space_uncurry
);
12165 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12166 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12168 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12171 /* Construct a basic map where the given dimensions are equal to each other.
12173 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12174 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12176 isl_basic_map
*bmap
= NULL
;
12179 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12180 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12183 if (type1
== type2
&& pos1
== pos2
)
12184 return isl_basic_map_universe(space
);
12186 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12187 i
= isl_basic_map_alloc_equality(bmap
);
12190 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12191 pos1
+= isl_basic_map_offset(bmap
, type1
);
12192 pos2
+= isl_basic_map_offset(bmap
, type2
);
12193 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12194 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12195 bmap
= isl_basic_map_finalize(bmap
);
12196 isl_space_free(space
);
12199 isl_space_free(space
);
12200 isl_basic_map_free(bmap
);
12204 /* Add a constraint imposing that the given two dimensions are equal.
12206 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12207 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12211 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12213 bmap
= isl_basic_map_intersect(bmap
, eq
);
12218 /* Add a constraint imposing that the given two dimensions are equal.
12220 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12221 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12223 isl_basic_map
*bmap
;
12225 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12227 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12232 /* Add a constraint imposing that the given two dimensions have opposite values.
12234 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12235 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12237 isl_basic_map
*bmap
= NULL
;
12240 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12241 return isl_map_free(map
);
12242 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12243 return isl_map_free(map
);
12245 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12246 i
= isl_basic_map_alloc_equality(bmap
);
12249 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12250 pos1
+= isl_basic_map_offset(bmap
, type1
);
12251 pos2
+= isl_basic_map_offset(bmap
, type2
);
12252 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12253 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12254 bmap
= isl_basic_map_finalize(bmap
);
12256 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12260 isl_basic_map_free(bmap
);
12265 /* Construct a constraint imposing that the value of the first dimension is
12266 * greater than or equal to that of the second.
12268 static __isl_give isl_constraint
*constraint_order_ge(
12269 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12270 enum isl_dim_type type2
, int pos2
)
12274 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12275 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12276 space
= isl_space_free(space
);
12280 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12282 if (type1
== type2
&& pos1
== pos2
)
12285 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12286 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12291 /* Add a constraint imposing that the value of the first dimension is
12292 * greater than or equal to that of the second.
12294 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12295 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12300 if (type1
== type2
&& pos1
== pos2
)
12302 space
= isl_basic_map_get_space(bmap
);
12303 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12304 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12309 /* Add a constraint imposing that the value of the first dimension is
12310 * greater than or equal to that of the second.
12312 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12313 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12318 if (type1
== type2
&& pos1
== pos2
)
12320 space
= isl_map_get_space(map
);
12321 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12322 map
= isl_map_add_constraint(map
, c
);
12327 /* Add a constraint imposing that the value of the first dimension is
12328 * less than or equal to that of the second.
12330 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12331 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12333 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12336 /* Construct a basic map where the value of the first dimension is
12337 * greater than that of the second.
12339 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12340 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12342 isl_basic_map
*bmap
= NULL
;
12345 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12346 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12349 if (type1
== type2
&& pos1
== pos2
)
12350 return isl_basic_map_empty(space
);
12352 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12353 i
= isl_basic_map_alloc_inequality(bmap
);
12355 return isl_basic_map_free(bmap
);
12356 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12357 pos1
+= isl_basic_map_offset(bmap
, type1
);
12358 pos2
+= isl_basic_map_offset(bmap
, type2
);
12359 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12360 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12361 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12362 bmap
= isl_basic_map_finalize(bmap
);
12366 isl_space_free(space
);
12367 isl_basic_map_free(bmap
);
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * greater than that of the second.
12374 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12375 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12379 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12381 bmap
= isl_basic_map_intersect(bmap
, gt
);
12386 /* Add a constraint imposing that the value of the first dimension is
12387 * greater than that of the second.
12389 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12390 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12392 isl_basic_map
*bmap
;
12394 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12396 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12401 /* Add a constraint imposing that the value of the first dimension is
12402 * smaller than that of the second.
12404 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12405 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12407 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12410 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12414 isl_local_space
*ls
;
12419 if (!isl_basic_map_divs_known(bmap
))
12420 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12421 "some divs are unknown", return NULL
);
12423 ls
= isl_basic_map_get_local_space(bmap
);
12424 div
= isl_local_space_get_div(ls
, pos
);
12425 isl_local_space_free(ls
);
12430 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12433 return isl_basic_map_get_div(bset
, pos
);
12436 /* Plug in "subs" for dimension "type", "pos" of "bset".
12438 * Let i be the dimension to replace and let "subs" be of the form
12442 * Any integer division with a non-zero coefficient for i,
12444 * floor((a i + g)/m)
12448 * floor((a f + d g)/(m d))
12450 * Constraints of the form
12458 * We currently require that "subs" is an integral expression.
12459 * Handling rational expressions may require us to add stride constraints
12460 * as we do in isl_basic_set_preimage_multi_aff.
12462 __isl_give isl_basic_set
*isl_basic_set_substitute(
12463 __isl_take isl_basic_set
*bset
,
12464 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12470 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12473 bset
= isl_basic_set_cow(bset
);
12474 if (!bset
|| !subs
)
12477 ctx
= isl_basic_set_get_ctx(bset
);
12478 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12479 isl_die(ctx
, isl_error_invalid
,
12480 "spaces don't match", goto error
);
12481 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12482 isl_die(ctx
, isl_error_unsupported
,
12483 "cannot handle divs yet", goto error
);
12484 if (!isl_int_is_one(subs
->v
->el
[0]))
12485 isl_die(ctx
, isl_error_invalid
,
12486 "can only substitute integer expressions", goto error
);
12488 pos
+= isl_basic_set_offset(bset
, type
);
12492 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12493 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12495 isl_int_set(v
, bset
->eq
[i
][pos
]);
12496 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12497 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12498 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12501 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12502 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12504 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12505 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12506 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12507 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12510 for (i
= 0; i
< bset
->n_div
; ++i
) {
12511 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12513 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12514 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12515 isl_seq_combine(bset
->div
[i
] + 1,
12516 subs
->v
->el
[0], bset
->div
[i
] + 1,
12517 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12518 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12523 bset
= isl_basic_set_simplify(bset
);
12524 return isl_basic_set_finalize(bset
);
12526 isl_basic_set_free(bset
);
12530 /* Plug in "subs" for dimension "type", "pos" of "set".
12532 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12533 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12537 if (set
&& isl_set_plain_is_empty(set
))
12540 set
= isl_set_cow(set
);
12544 for (i
= set
->n
- 1; i
>= 0; --i
) {
12545 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12546 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12557 /* Check if the range of "ma" is compatible with the domain or range
12558 * (depending on "type") of "bmap".
12560 static isl_stat
check_basic_map_compatible_range_multi_aff(
12561 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12562 __isl_keep isl_multi_aff
*ma
)
12565 isl_space
*ma_space
;
12567 ma_space
= isl_multi_aff_get_space(ma
);
12569 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12573 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12574 "parameters don't match", goto error
);
12575 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12579 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12580 "spaces don't match", goto error
);
12582 isl_space_free(ma_space
);
12583 return isl_stat_ok
;
12585 isl_space_free(ma_space
);
12586 return isl_stat_error
;
12589 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12590 * coefficients before the transformed range of dimensions,
12591 * the "n_after" coefficients after the transformed range of dimensions
12592 * and the coefficients of the other divs in "bmap".
12594 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12595 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12600 isl_local_space
*ls
;
12605 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12607 return isl_basic_map_free(bmap
);
12609 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12610 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12611 for (i
= 0; i
< n_div
; ++i
) {
12612 int o_bmap
= 0, o_ls
= 0;
12614 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12615 o_bmap
+= 1 + 1 + n_param
;
12616 o_ls
+= 1 + 1 + n_param
;
12617 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12618 o_bmap
+= n_before
;
12619 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12620 ls
->div
->row
[i
] + o_ls
, n_set
);
12623 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12625 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12626 ls
->div
->row
[i
] + o_ls
, n_div
);
12629 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12630 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12635 isl_local_space_free(ls
);
12638 isl_local_space_free(ls
);
12639 return isl_basic_map_free(bmap
);
12642 /* How many stride constraints does "ma" enforce?
12643 * That is, how many of the affine expressions have a denominator
12644 * different from one?
12646 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12651 for (i
= 0; i
< ma
->n
; ++i
)
12652 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12658 /* For each affine expression in ma of the form
12660 * x_i = (f_i y + h_i)/m_i
12662 * with m_i different from one, add a constraint to "bmap"
12665 * f_i y + h_i = m_i alpha_i
12667 * with alpha_i an additional existentially quantified variable.
12669 * The input variables of "ma" correspond to a subset of the variables
12670 * of "bmap". There are "n_before" variables in "bmap" before this
12671 * subset and "n_after" variables after this subset.
12672 * The integer divisions of the affine expressions in "ma" are assumed
12673 * to have been aligned. There are "n_div_ma" of them and
12674 * they appear first in "bmap", straight after the "n_after" variables.
12676 static __isl_give isl_basic_map
*add_ma_strides(
12677 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12678 int n_before
, int n_after
, int n_div_ma
)
12686 total
= isl_basic_map_total_dim(bmap
);
12687 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12688 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12689 for (i
= 0; i
< ma
->n
; ++i
) {
12690 int o_bmap
= 0, o_ma
= 1;
12692 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12694 div
= isl_basic_map_alloc_div(bmap
);
12695 k
= isl_basic_map_alloc_equality(bmap
);
12696 if (div
< 0 || k
< 0)
12698 isl_int_set_si(bmap
->div
[div
][0], 0);
12699 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12700 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12701 o_bmap
+= 1 + n_param
;
12702 o_ma
+= 1 + n_param
;
12703 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12704 o_bmap
+= n_before
;
12705 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12706 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12709 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12711 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12712 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12713 o_bmap
+= n_div_ma
;
12715 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12716 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12722 isl_basic_map_free(bmap
);
12726 /* Replace the domain or range space (depending on "type) of "space" by "set".
12728 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12729 enum isl_dim_type type
, __isl_take isl_space
*set
)
12731 if (type
== isl_dim_in
) {
12732 space
= isl_space_range(space
);
12733 space
= isl_space_map_from_domain_and_range(set
, space
);
12735 space
= isl_space_domain(space
);
12736 space
= isl_space_map_from_domain_and_range(space
, set
);
12742 /* Compute the preimage of the domain or range (depending on "type")
12743 * of "bmap" under the function represented by "ma".
12744 * In other words, plug in "ma" in the domain or range of "bmap".
12745 * The result is a basic map that lives in the same space as "bmap"
12746 * except that the domain or range has been replaced by
12747 * the domain space of "ma".
12749 * If bmap is represented by
12751 * A(p) + S u + B x + T v + C(divs) >= 0,
12753 * where u and x are input and output dimensions if type == isl_dim_out
12754 * while x and v are input and output dimensions if type == isl_dim_in,
12755 * and ma is represented by
12757 * x = D(p) + F(y) + G(divs')
12759 * then the result is
12761 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12763 * The divs in the input set are similarly adjusted.
12766 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12770 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12771 * B_i G(divs') + c_i(divs))/n_i)
12773 * If bmap is not a rational map and if F(y) involves any denominators
12775 * x_i = (f_i y + h_i)/m_i
12777 * then additional constraints are added to ensure that we only
12778 * map back integer points. That is we enforce
12780 * f_i y + h_i = m_i alpha_i
12782 * with alpha_i an additional existentially quantified variable.
12784 * We first copy over the divs from "ma".
12785 * Then we add the modified constraints and divs from "bmap".
12786 * Finally, we add the stride constraints, if needed.
12788 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12789 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12790 __isl_take isl_multi_aff
*ma
)
12794 isl_basic_map
*res
= NULL
;
12795 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12796 isl_int f
, c1
, c2
, g
;
12805 ma
= isl_multi_aff_align_divs(ma
);
12808 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12811 if (type
== isl_dim_in
) {
12813 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12815 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12818 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12819 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12821 space
= isl_multi_aff_get_domain_space(ma
);
12822 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12823 rational
= isl_basic_map_is_rational(bmap
);
12824 strides
= rational
? 0 : multi_aff_strides(ma
);
12825 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12826 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12828 res
= isl_basic_map_set_rational(res
);
12830 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12831 if (isl_basic_map_alloc_div(res
) < 0)
12834 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
12838 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12839 k
= isl_basic_map_alloc_equality(res
);
12842 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12843 n_after
, n_div_ma
, n_div_bmap
,
12844 f
, c1
, c2
, g
, 0) < 0)
12848 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12849 k
= isl_basic_map_alloc_inequality(res
);
12852 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12853 n_after
, n_div_ma
, n_div_bmap
,
12854 f
, c1
, c2
, g
, 0) < 0)
12858 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12859 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12860 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12863 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12864 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12865 f
, c1
, c2
, g
, 1) < 0)
12870 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12876 isl_basic_map_free(bmap
);
12877 isl_multi_aff_free(ma
);
12878 res
= isl_basic_map_simplify(res
);
12879 return isl_basic_map_finalize(res
);
12885 isl_basic_map_free(bmap
);
12886 isl_multi_aff_free(ma
);
12887 isl_basic_map_free(res
);
12891 /* Compute the preimage of "bset" under the function represented by "ma".
12892 * In other words, plug in "ma" in "bset". The result is a basic set
12893 * that lives in the domain space of "ma".
12895 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12896 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12898 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12901 /* Compute the preimage of the domain of "bmap" under the function
12902 * represented by "ma".
12903 * In other words, plug in "ma" in the domain of "bmap".
12904 * The result is a basic map that lives in the same space as "bmap"
12905 * except that the domain has been replaced by the domain space of "ma".
12907 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12908 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12910 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12913 /* Compute the preimage of the range of "bmap" under the function
12914 * represented by "ma".
12915 * In other words, plug in "ma" in the range of "bmap".
12916 * The result is a basic map that lives in the same space as "bmap"
12917 * except that the range has been replaced by the domain space of "ma".
12919 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12920 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12922 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12925 /* Check if the range of "ma" is compatible with the domain or range
12926 * (depending on "type") of "map".
12927 * Return isl_stat_error if anything is wrong.
12929 static isl_stat
check_map_compatible_range_multi_aff(
12930 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12931 __isl_keep isl_multi_aff
*ma
)
12934 isl_space
*ma_space
;
12936 ma_space
= isl_multi_aff_get_space(ma
);
12937 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12938 isl_space_free(ma_space
);
12940 return isl_stat_error
;
12942 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12943 "spaces don't match", return isl_stat_error
);
12944 return isl_stat_ok
;
12947 /* Compute the preimage of the domain or range (depending on "type")
12948 * of "map" under the function represented by "ma".
12949 * In other words, plug in "ma" in the domain or range of "map".
12950 * The result is a map that lives in the same space as "map"
12951 * except that the domain or range has been replaced by
12952 * the domain space of "ma".
12954 * The parameters are assumed to have been aligned.
12956 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12957 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12962 map
= isl_map_cow(map
);
12963 ma
= isl_multi_aff_align_divs(ma
);
12966 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12969 for (i
= 0; i
< map
->n
; ++i
) {
12970 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12971 isl_multi_aff_copy(ma
));
12976 space
= isl_multi_aff_get_domain_space(ma
);
12977 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12979 isl_space_free(map
->dim
);
12984 isl_multi_aff_free(ma
);
12986 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12987 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12990 isl_multi_aff_free(ma
);
12995 /* Compute the preimage of the domain or range (depending on "type")
12996 * of "map" under the function represented by "ma".
12997 * In other words, plug in "ma" in the domain or range of "map".
12998 * The result is a map that lives in the same space as "map"
12999 * except that the domain or range has been replaced by
13000 * the domain space of "ma".
13002 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13003 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13010 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13014 return map_preimage_multi_aff(map
, type
, ma
);
13016 if (isl_map_check_named_params(map
) < 0)
13018 if (!isl_space_has_named_params(ma
->space
))
13019 isl_die(map
->ctx
, isl_error_invalid
,
13020 "unaligned unnamed parameters", goto error
);
13021 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13022 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13024 return map_preimage_multi_aff(map
, type
, ma
);
13026 isl_multi_aff_free(ma
);
13027 return isl_map_free(map
);
13030 /* Compute the preimage of "set" under the function represented by "ma".
13031 * In other words, plug in "ma" in "set". The result is a set
13032 * that lives in the domain space of "ma".
13034 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13035 __isl_take isl_multi_aff
*ma
)
13037 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13040 /* Compute the preimage of the domain of "map" under the function
13041 * represented by "ma".
13042 * In other words, plug in "ma" in the domain of "map".
13043 * The result is a map that lives in the same space as "map"
13044 * except that the domain has been replaced by the domain space of "ma".
13046 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13047 __isl_take isl_multi_aff
*ma
)
13049 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13052 /* Compute the preimage of the range of "map" under the function
13053 * represented by "ma".
13054 * In other words, plug in "ma" in the range of "map".
13055 * The result is a map that lives in the same space as "map"
13056 * except that the range has been replaced by the domain space of "ma".
13058 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13059 __isl_take isl_multi_aff
*ma
)
13061 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13064 /* Compute the preimage of "map" under the function represented by "pma".
13065 * In other words, plug in "pma" in the domain or range of "map".
13066 * The result is a map that lives in the same space as "map",
13067 * except that the space of type "type" has been replaced by
13068 * the domain space of "pma".
13070 * The parameters of "map" and "pma" are assumed to have been aligned.
13072 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13073 __isl_take isl_map
*map
, enum isl_dim_type type
,
13074 __isl_take isl_pw_multi_aff
*pma
)
13083 isl_pw_multi_aff_free(pma
);
13084 res
= isl_map_empty(isl_map_get_space(map
));
13089 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13090 isl_multi_aff_copy(pma
->p
[0].maff
));
13091 if (type
== isl_dim_in
)
13092 res
= isl_map_intersect_domain(res
,
13093 isl_map_copy(pma
->p
[0].set
));
13095 res
= isl_map_intersect_range(res
,
13096 isl_map_copy(pma
->p
[0].set
));
13098 for (i
= 1; i
< pma
->n
; ++i
) {
13101 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13102 isl_multi_aff_copy(pma
->p
[i
].maff
));
13103 if (type
== isl_dim_in
)
13104 res_i
= isl_map_intersect_domain(res_i
,
13105 isl_map_copy(pma
->p
[i
].set
));
13107 res_i
= isl_map_intersect_range(res_i
,
13108 isl_map_copy(pma
->p
[i
].set
));
13109 res
= isl_map_union(res
, res_i
);
13112 isl_pw_multi_aff_free(pma
);
13116 isl_pw_multi_aff_free(pma
);
13121 /* Compute the preimage of "map" under the function represented by "pma".
13122 * In other words, plug in "pma" in the domain or range of "map".
13123 * The result is a map that lives in the same space as "map",
13124 * except that the space of type "type" has been replaced by
13125 * the domain space of "pma".
13127 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13128 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13135 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13139 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13141 if (isl_map_check_named_params(map
) < 0)
13143 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13145 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13146 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13148 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13150 isl_pw_multi_aff_free(pma
);
13151 return isl_map_free(map
);
13154 /* Compute the preimage of "set" under the function represented by "pma".
13155 * In other words, plug in "pma" in "set". The result is a set
13156 * that lives in the domain space of "pma".
13158 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13159 __isl_take isl_pw_multi_aff
*pma
)
13161 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13164 /* Compute the preimage of the domain of "map" under the function
13165 * represented by "pma".
13166 * In other words, plug in "pma" in the domain of "map".
13167 * The result is a map that lives in the same space as "map",
13168 * except that domain space has been replaced by the domain space of "pma".
13170 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13171 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13173 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13176 /* Compute the preimage of the range of "map" under the function
13177 * represented by "pma".
13178 * In other words, plug in "pma" in the range of "map".
13179 * The result is a map that lives in the same space as "map",
13180 * except that range space has been replaced by the domain space of "pma".
13182 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13183 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13185 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13188 /* Compute the preimage of "map" under the function represented by "mpa".
13189 * In other words, plug in "mpa" in the domain or range of "map".
13190 * The result is a map that lives in the same space as "map",
13191 * except that the space of type "type" has been replaced by
13192 * the domain space of "mpa".
13194 * If the map does not involve any constraints that refer to the
13195 * dimensions of the substituted space, then the only possible
13196 * effect of "mpa" on the map is to map the space to a different space.
13197 * We create a separate isl_multi_aff to effectuate this change
13198 * in order to avoid spurious splitting of the map along the pieces
13200 * If "mpa" has a non-trivial explicit domain, however,
13201 * then the full substitution should be performed.
13203 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13204 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13208 isl_pw_multi_aff
*pma
;
13213 n
= isl_map_dim(map
, type
);
13214 full
= isl_map_involves_dims(map
, type
, 0, n
);
13215 if (full
>= 0 && !full
)
13216 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13223 space
= isl_multi_pw_aff_get_space(mpa
);
13224 isl_multi_pw_aff_free(mpa
);
13225 ma
= isl_multi_aff_zero(space
);
13226 return isl_map_preimage_multi_aff(map
, type
, ma
);
13229 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13230 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13233 isl_multi_pw_aff_free(mpa
);
13237 /* Compute the preimage of "map" under the function represented by "mpa".
13238 * In other words, plug in "mpa" in the domain "map".
13239 * The result is a map that lives in the same space as "map",
13240 * except that domain space has been replaced by the domain space of "mpa".
13242 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13243 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13245 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13248 /* Compute the preimage of "set" by the function represented by "mpa".
13249 * In other words, plug in "mpa" in "set".
13251 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13252 __isl_take isl_multi_pw_aff
*mpa
)
13254 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13257 /* Return a copy of the equality constraints of "bset" as a matrix.
13259 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13260 __isl_keep isl_basic_set
*bset
)
13268 ctx
= isl_basic_set_get_ctx(bset
);
13269 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13270 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13273 /* Are the "n" "coefficients" starting at "first" of the integer division
13274 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13276 * The "coefficient" at position 0 is the denominator.
13277 * The "coefficient" at position 1 is the constant term.
13279 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13280 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13281 unsigned first
, unsigned n
)
13283 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13284 return isl_bool_error
;
13285 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13286 return isl_bool_error
;
13287 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13288 bmap2
->div
[pos2
] + first
, n
);
13291 /* Are the integer division expressions at position "pos1" in "bmap1" and
13292 * "pos2" in "bmap2" equal to each other, except that the constant terms
13295 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13296 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13297 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13302 if (!bmap1
|| !bmap2
)
13303 return isl_bool_error
;
13304 total
= isl_basic_map_total_dim(bmap1
);
13305 if (total
!= isl_basic_map_total_dim(bmap2
))
13306 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13307 "incomparable div expressions", return isl_bool_error
);
13308 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13310 if (equal
< 0 || !equal
)
13312 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13314 if (equal
< 0 || equal
)
13315 return isl_bool_not(equal
);
13316 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13320 /* Replace the numerator of the constant term of the integer division
13321 * expression at position "div" in "bmap" by "value".
13322 * The caller guarantees that this does not change the meaning
13325 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13326 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13328 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13329 return isl_basic_map_free(bmap
);
13331 isl_int_set_si(bmap
->div
[div
][1], value
);
13336 /* Is the point "inner" internal to inequality constraint "ineq"
13338 * The point is considered to be internal to the inequality constraint,
13339 * if it strictly lies on the positive side of the inequality constraint,
13340 * or if it lies on the constraint and the constraint is lexico-positive.
13342 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13343 __isl_keep isl_basic_set
*bset
, int ineq
)
13349 if (!inner
|| !bset
)
13350 return isl_bool_error
;
13352 ctx
= isl_basic_set_get_ctx(bset
);
13353 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13354 &ctx
->normalize_gcd
);
13355 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13356 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13358 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13359 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13360 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13363 /* Tighten the inequality constraints of "bset" that are outward with respect
13364 * to the point "vec".
13365 * That is, tighten the constraints that are not satisfied by "vec".
13367 * "vec" is a point internal to some superset S of "bset" that is used
13368 * to make the subsets of S disjoint, by tightening one half of the constraints
13369 * that separate two subsets. In particular, the constraints of S
13370 * are all satisfied by "vec" and should not be tightened.
13371 * Of the internal constraints, those that have "vec" on the outside
13372 * are tightened. The shared facet is included in the adjacent subset
13373 * with the opposite constraint.
13374 * For constraints that saturate "vec", this criterion cannot be used
13375 * to determine which of the two sides should be tightened.
13376 * Instead, the sign of the first non-zero coefficient is used
13377 * to make this choice. Note that this second criterion is never used
13378 * on the constraints of S since "vec" is interior to "S".
13380 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13381 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13385 bset
= isl_basic_set_cow(bset
);
13388 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13391 internal
= is_internal(vec
, bset
, j
);
13393 return isl_basic_set_free(bset
);
13396 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13402 /* Replace the variables x of type "type" starting at "first" in "bmap"
13403 * by x' with x = M x' with M the matrix trans.
13404 * That is, replace the corresponding coefficients c by c M.
13406 * The transformation matrix should be a square matrix.
13408 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13409 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13410 __isl_take isl_mat
*trans
)
13414 bmap
= isl_basic_map_cow(bmap
);
13415 if (!bmap
|| !trans
)
13418 if (trans
->n_row
!= trans
->n_col
)
13419 isl_die(trans
->ctx
, isl_error_invalid
,
13420 "expecting square transformation matrix", goto error
);
13421 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13424 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13426 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13427 isl_mat_copy(trans
)) < 0)
13429 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13430 isl_mat_copy(trans
)) < 0)
13432 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13433 isl_mat_copy(trans
)) < 0)
13436 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13437 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13439 isl_mat_free(trans
);
13442 isl_mat_free(trans
);
13443 isl_basic_map_free(bmap
);
13447 /* Replace the variables x of type "type" starting at "first" in "bset"
13448 * by x' with x = M x' with M the matrix trans.
13449 * That is, replace the corresponding coefficients c by c M.
13451 * The transformation matrix should be a square matrix.
13453 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13454 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13455 __isl_take isl_mat
*trans
)
13457 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);