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>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.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 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 /* Return the space of "map".
93 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
95 return map
? map
->dim
: NULL
;
98 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
100 return map
? n(map
->dim
, type
) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
105 return set
? n(set
->dim
, type
) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
109 enum isl_dim_type type
)
118 case isl_dim_cst
: return 0;
119 case isl_dim_param
: return 1;
120 case isl_dim_in
: return 1 + space
->nparam
;
121 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
122 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
128 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
136 return pos(map
->dim
, type
);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
140 enum isl_dim_type type
)
142 return isl_basic_map_dim(bset
, type
);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
147 return isl_basic_set_dim(bset
, isl_dim_set
);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
152 return isl_basic_set_dim(bset
, isl_dim_param
);
155 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
159 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
162 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
164 return isl_set_dim(set
, isl_dim_set
);
167 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
169 return isl_set_dim(set
, isl_dim_param
);
172 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->n_in
: 0;
177 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
179 return bmap
? bmap
->dim
->n_out
: 0;
182 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
184 return bmap
? bmap
->dim
->nparam
: 0;
187 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
189 return bmap
? bmap
->n_div
: 0;
192 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
194 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
197 unsigned isl_map_n_in(const struct isl_map
*map
)
199 return map
? map
->dim
->n_in
: 0;
202 unsigned isl_map_n_out(const struct isl_map
*map
)
204 return map
? map
->dim
->n_out
: 0;
207 unsigned isl_map_n_param(const struct isl_map
*map
)
209 return map
? map
->dim
->nparam
: 0;
212 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
213 __isl_keep isl_set
*set
)
217 return isl_bool_error
;
218 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
221 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
222 set
->dim
, isl_dim_set
);
225 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
226 __isl_keep isl_basic_set
*bset
)
230 return isl_bool_error
;
231 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
234 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
235 bset
->dim
, isl_dim_set
);
238 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
239 __isl_keep isl_set
*set
)
243 return isl_bool_error
;
244 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
247 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
248 set
->dim
, isl_dim_set
);
251 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
252 __isl_keep isl_basic_set
*bset
)
256 return isl_bool_error
;
257 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
260 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
261 bset
->dim
, isl_dim_set
);
264 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
266 return bmap
? bmap
->ctx
: NULL
;
269 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
271 return bset
? bset
->ctx
: NULL
;
274 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
276 return map
? map
->ctx
: NULL
;
279 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
281 return set
? set
->ctx
: NULL
;
284 /* Return the space of "bmap".
286 __isl_keep isl_space
*isl_basic_map_peek_space(
287 __isl_keep
const isl_basic_map
*bmap
)
289 return bmap
? bmap
->dim
: NULL
;
292 /* Return the space of "bset".
294 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
296 return isl_basic_map_peek_space(bset_to_bmap(bset
));
299 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
301 return isl_space_copy(isl_basic_map_peek_space(bmap
));
304 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
306 return isl_basic_map_get_space(bset_to_bmap(bset
));
309 /* Extract the divs in "bmap" as a matrix.
311 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
322 ctx
= isl_basic_map_get_ctx(bmap
);
323 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
324 cols
= 1 + 1 + total
+ bmap
->n_div
;
325 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
329 for (i
= 0; i
< bmap
->n_div
; ++i
)
330 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
335 /* Extract the divs in "bset" as a matrix.
337 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
339 return isl_basic_map_get_divs(bset
);
342 __isl_give isl_local_space
*isl_basic_map_get_local_space(
343 __isl_keep isl_basic_map
*bmap
)
350 div
= isl_basic_map_get_divs(bmap
);
351 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
354 __isl_give isl_local_space
*isl_basic_set_get_local_space(
355 __isl_keep isl_basic_set
*bset
)
357 return isl_basic_map_get_local_space(bset
);
360 /* For each known div d = floor(f/m), add the constraints
363 * -(f-(m-1)) + m d >= 0
365 * Do not finalize the result.
367 static __isl_give isl_basic_map
*add_known_div_constraints(
368 __isl_take isl_basic_map
*bmap
)
375 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
378 bmap
= isl_basic_map_cow(bmap
);
379 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
382 for (i
= 0; i
< n_div
; ++i
) {
383 if (isl_int_is_zero(bmap
->div
[i
][0]))
385 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
386 return isl_basic_map_free(bmap
);
392 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
393 __isl_take isl_local_space
*ls
)
402 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
403 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
404 n_div
, 0, 2 * n_div
);
406 for (i
= 0; i
< n_div
; ++i
)
407 if (isl_basic_map_alloc_div(bmap
) < 0)
410 for (i
= 0; i
< n_div
; ++i
)
411 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
412 bmap
= add_known_div_constraints(bmap
);
414 isl_local_space_free(ls
);
417 isl_local_space_free(ls
);
418 isl_basic_map_free(bmap
);
422 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
423 __isl_take isl_local_space
*ls
)
425 return isl_basic_map_from_local_space(ls
);
428 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
430 return isl_space_copy(isl_map_peek_space(map
));
433 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
437 return isl_space_copy(set
->dim
);
440 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
441 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
443 bmap
= isl_basic_map_cow(bmap
);
446 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
449 bmap
= isl_basic_map_finalize(bmap
);
452 isl_basic_map_free(bmap
);
456 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
457 __isl_take isl_basic_set
*bset
, const char *s
)
459 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
462 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
463 enum isl_dim_type type
)
465 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
468 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
469 enum isl_dim_type type
, const char *s
)
473 map
= isl_map_cow(map
);
477 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
481 for (i
= 0; i
< map
->n
; ++i
) {
482 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
493 /* Replace the identifier of the tuple of type "type" by "id".
495 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
496 __isl_take isl_basic_map
*bmap
,
497 enum isl_dim_type type
, __isl_take isl_id
*id
)
499 bmap
= isl_basic_map_cow(bmap
);
502 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
504 return isl_basic_map_free(bmap
);
505 bmap
= isl_basic_map_finalize(bmap
);
512 /* Replace the identifier of the tuple by "id".
514 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
515 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
517 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
520 /* Does the input or output tuple have a name?
522 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
524 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
527 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
528 enum isl_dim_type type
)
530 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
533 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
536 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
540 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
541 enum isl_dim_type type
, __isl_take isl_id
*id
)
543 map
= isl_map_cow(map
);
547 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
549 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
555 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
556 __isl_take isl_id
*id
)
558 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
561 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
562 enum isl_dim_type type
)
564 map
= isl_map_cow(map
);
568 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
570 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
573 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
575 return isl_map_reset_tuple_id(set
, isl_dim_set
);
578 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
580 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
583 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
584 enum isl_dim_type type
)
586 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
589 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
591 return isl_map_has_tuple_id(set
, isl_dim_set
);
594 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
596 return isl_map_get_tuple_id(set
, isl_dim_set
);
599 /* Does the set tuple have a name?
601 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
604 return isl_bool_error
;
605 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
609 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
611 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
614 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
616 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
619 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
620 enum isl_dim_type type
, unsigned pos
)
622 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
625 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
626 enum isl_dim_type type
, unsigned pos
)
628 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
631 /* Does the given dimension have a name?
633 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
634 enum isl_dim_type type
, unsigned pos
)
637 return isl_bool_error
;
638 return isl_space_has_dim_name(map
->dim
, type
, pos
);
641 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
642 enum isl_dim_type type
, unsigned pos
)
644 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
647 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
648 enum isl_dim_type type
, unsigned pos
)
650 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
653 /* Does the given dimension have a name?
655 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
656 enum isl_dim_type type
, unsigned pos
)
659 return isl_bool_error
;
660 return isl_space_has_dim_name(set
->dim
, type
, pos
);
663 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
664 __isl_take isl_basic_map
*bmap
,
665 enum isl_dim_type type
, unsigned pos
, const char *s
)
667 bmap
= isl_basic_map_cow(bmap
);
670 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
673 return isl_basic_map_finalize(bmap
);
675 isl_basic_map_free(bmap
);
679 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
680 enum isl_dim_type type
, unsigned pos
, const char *s
)
684 map
= isl_map_cow(map
);
688 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
692 for (i
= 0; i
< map
->n
; ++i
) {
693 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
704 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
705 __isl_take isl_basic_set
*bset
,
706 enum isl_dim_type type
, unsigned pos
, const char *s
)
708 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
712 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
713 enum isl_dim_type type
, unsigned pos
, const char *s
)
715 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
719 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
720 enum isl_dim_type type
, unsigned pos
)
723 return isl_bool_error
;
724 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
727 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
728 enum isl_dim_type type
, unsigned pos
)
730 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
733 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
734 enum isl_dim_type type
, unsigned pos
)
736 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
739 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
740 enum isl_dim_type type
, unsigned pos
)
742 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
745 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
746 enum isl_dim_type type
, unsigned pos
)
748 return isl_map_has_dim_id(set
, type
, pos
);
751 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
752 enum isl_dim_type type
, unsigned pos
)
754 return isl_map_get_dim_id(set
, type
, pos
);
757 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
758 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
760 map
= isl_map_cow(map
);
764 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
766 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
772 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
773 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
775 return isl_map_set_dim_id(set
, type
, pos
, id
);
778 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
779 __isl_keep isl_id
*id
)
783 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
786 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
787 __isl_keep isl_id
*id
)
789 return isl_map_find_dim_by_id(set
, type
, id
);
792 /* Return the position of the dimension of the given type and name
794 * Return -1 if no such dimension can be found.
796 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
797 enum isl_dim_type type
, const char *name
)
801 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
804 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
809 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
812 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
815 return isl_map_find_dim_by_name(set
, type
, name
);
818 /* Reset the user pointer on all identifiers of parameters and tuples
819 * of the space of "map".
821 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
825 space
= isl_map_get_space(map
);
826 space
= isl_space_reset_user(space
);
827 map
= isl_map_reset_space(map
, space
);
832 /* Reset the user pointer on all identifiers of parameters and tuples
833 * of the space of "set".
835 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
837 return isl_map_reset_user(set
);
840 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
843 return isl_bool_error
;
844 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
847 /* Has "map" been marked as a rational map?
848 * In particular, have all basic maps in "map" been marked this way?
849 * An empty map is not considered to be rational.
850 * Maps where only some of the basic maps are marked rational
853 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
859 return isl_bool_error
;
861 return isl_bool_false
;
862 rational
= isl_basic_map_is_rational(map
->p
[0]);
865 for (i
= 1; i
< map
->n
; ++i
) {
868 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
871 if (rational
!= rational_i
)
872 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
873 "mixed rational and integer basic maps "
874 "not supported", return isl_bool_error
);
880 /* Has "set" been marked as a rational set?
881 * In particular, have all basic set in "set" been marked this way?
882 * An empty set is not considered to be rational.
883 * Sets where only some of the basic sets are marked rational
886 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
888 return isl_map_is_rational(set
);
891 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
893 return isl_basic_map_is_rational(bset
);
896 /* Does "bmap" contain any rational points?
898 * If "bmap" has an equality for each dimension, equating the dimension
899 * to an integer constant, then it has no rational points, even if it
900 * is marked as rational.
902 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
904 isl_bool has_rational
= isl_bool_true
;
908 return isl_bool_error
;
909 if (isl_basic_map_plain_is_empty(bmap
))
910 return isl_bool_false
;
911 if (!isl_basic_map_is_rational(bmap
))
912 return isl_bool_false
;
913 bmap
= isl_basic_map_copy(bmap
);
914 bmap
= isl_basic_map_implicit_equalities(bmap
);
916 return isl_bool_error
;
917 total
= isl_basic_map_total_dim(bmap
);
918 if (bmap
->n_eq
== total
) {
920 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
921 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
924 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
925 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
927 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
933 has_rational
= isl_bool_false
;
935 isl_basic_map_free(bmap
);
940 /* Does "map" contain any rational points?
942 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
945 isl_bool has_rational
;
948 return isl_bool_error
;
949 for (i
= 0; i
< map
->n
; ++i
) {
950 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
951 if (has_rational
< 0 || has_rational
)
954 return isl_bool_false
;
957 /* Does "set" contain any rational points?
959 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
961 return isl_map_has_rational(set
);
964 /* Is this basic set a parameter domain?
966 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
969 return isl_bool_error
;
970 return isl_space_is_params(bset
->dim
);
973 /* Is this set a parameter domain?
975 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
978 return isl_bool_error
;
979 return isl_space_is_params(set
->dim
);
982 /* Is this map actually a parameter domain?
983 * Users should never call this function. Outside of isl,
984 * a map can never be a parameter domain.
986 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
989 return isl_bool_error
;
990 return isl_space_is_params(map
->dim
);
993 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
994 struct isl_basic_map
*bmap
, unsigned extra
,
995 unsigned n_eq
, unsigned n_ineq
)
998 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1003 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1004 if (isl_blk_is_error(bmap
->block
))
1007 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1008 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1012 bmap
->block2
= isl_blk_empty();
1015 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1016 if (isl_blk_is_error(bmap
->block2
))
1019 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1024 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1025 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1027 for (i
= 0; i
< extra
; ++i
)
1028 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1032 bmap
->c_size
= n_eq
+ n_ineq
;
1033 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1034 bmap
->extra
= extra
;
1038 bmap
->sample
= NULL
;
1042 isl_basic_map_free(bmap
);
1046 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1047 unsigned nparam
, unsigned dim
, unsigned extra
,
1048 unsigned n_eq
, unsigned n_ineq
)
1050 struct isl_basic_map
*bmap
;
1053 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1057 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1058 return bset_from_bmap(bmap
);
1061 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1062 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1064 struct isl_basic_map
*bmap
;
1067 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1068 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1069 return bset_from_bmap(bmap
);
1071 isl_space_free(dim
);
1075 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1076 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1078 struct isl_basic_map
*bmap
;
1082 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1087 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1089 isl_space_free(dim
);
1093 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1094 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1095 unsigned n_eq
, unsigned n_ineq
)
1097 struct isl_basic_map
*bmap
;
1100 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1104 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1108 static void dup_constraints(
1109 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1112 unsigned total
= isl_basic_map_total_dim(src
);
1114 for (i
= 0; i
< src
->n_eq
; ++i
) {
1115 int j
= isl_basic_map_alloc_equality(dst
);
1116 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1119 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1120 int j
= isl_basic_map_alloc_inequality(dst
);
1121 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1124 for (i
= 0; i
< src
->n_div
; ++i
) {
1125 int j
= isl_basic_map_alloc_div(dst
);
1126 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1128 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1131 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1133 struct isl_basic_map
*dup
;
1137 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1138 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1141 dup_constraints(dup
, bmap
);
1142 dup
->flags
= bmap
->flags
;
1143 dup
->sample
= isl_vec_copy(bmap
->sample
);
1147 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1149 struct isl_basic_map
*dup
;
1151 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1152 return bset_from_bmap(dup
);
1155 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1160 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1164 return isl_basic_set_dup(bset
);
1167 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1176 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1181 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1185 bmap
= isl_basic_map_dup(bmap
);
1187 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1191 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1200 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1205 if (--bmap
->ref
> 0)
1208 isl_ctx_deref(bmap
->ctx
);
1210 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1212 isl_blk_free(bmap
->ctx
, bmap
->block
);
1213 isl_vec_free(bmap
->sample
);
1214 isl_space_free(bmap
->dim
);
1220 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1222 return isl_basic_map_free(bset_to_bmap(bset
));
1225 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1227 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1230 /* Check that "map" has only named parameters, reporting an error
1233 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1235 return isl_space_check_named_params(isl_map_peek_space(map
));
1238 __isl_give isl_map
*isl_map_align_params_map_map_and(
1239 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1240 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1241 __isl_take isl_map
*map2
))
1245 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1246 return fn(map1
, map2
);
1247 if (isl_map_check_named_params(map1
) < 0)
1249 if (isl_map_check_named_params(map2
) < 0)
1251 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1252 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1253 return fn(map1
, map2
);
1260 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1261 __isl_keep isl_map
*map2
,
1262 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1267 return isl_bool_error
;
1268 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1269 return fn(map1
, map2
);
1270 if (isl_map_check_named_params(map1
) < 0)
1271 return isl_bool_error
;
1272 if (isl_map_check_named_params(map2
) < 0)
1273 return isl_bool_error
;
1274 map1
= isl_map_copy(map1
);
1275 map2
= isl_map_copy(map2
);
1276 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1277 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1284 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1286 struct isl_ctx
*ctx
;
1290 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1291 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1293 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1294 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1295 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1296 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1297 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1298 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1300 int j
= isl_basic_map_alloc_inequality(bmap
);
1304 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1305 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1312 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1313 bmap
->extra
- bmap
->n_div
);
1314 return bmap
->n_eq
++;
1317 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1319 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1322 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1326 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1331 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1333 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1336 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1341 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1343 if (pos
!= bmap
->n_eq
- 1) {
1345 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1346 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1352 /* Turn inequality "pos" of "bmap" into an equality.
1354 * In particular, we move the inequality in front of the equalities
1355 * and move the last inequality in the position of the moved inequality.
1356 * Note that isl_tab_make_equalities_explicit depends on this particular
1357 * change in the ordering of the constraints.
1359 void isl_basic_map_inequality_to_equality(
1360 struct isl_basic_map
*bmap
, unsigned pos
)
1364 t
= bmap
->ineq
[pos
];
1365 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1366 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1371 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1374 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1377 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1379 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1382 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1384 struct isl_ctx
*ctx
;
1388 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1389 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1390 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1391 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1392 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1393 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1394 1 + isl_basic_map_total_dim(bmap
),
1395 bmap
->extra
- bmap
->n_div
);
1396 return bmap
->n_ineq
++;
1399 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1401 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1404 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1408 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1413 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1415 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1418 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1423 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1425 if (pos
!= bmap
->n_ineq
- 1) {
1426 t
= bmap
->ineq
[pos
];
1427 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1428 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1429 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1435 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1437 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1440 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1445 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1448 k
= isl_basic_map_alloc_equality(bmap
);
1451 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1454 isl_basic_map_free(bmap
);
1458 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1461 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1464 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1469 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1472 k
= isl_basic_map_alloc_inequality(bmap
);
1475 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1478 isl_basic_map_free(bmap
);
1482 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1485 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1488 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1492 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1493 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1494 1 + 1 + isl_basic_map_total_dim(bmap
),
1495 bmap
->extra
- bmap
->n_div
);
1496 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1497 return bmap
->n_div
++;
1500 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1502 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1505 /* Check that there are "n" dimensions of type "type" starting at "first"
1508 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1509 enum isl_dim_type type
, unsigned first
, unsigned n
)
1514 return isl_stat_error
;
1515 dim
= isl_basic_map_dim(bmap
, type
);
1516 if (first
+ n
> dim
|| first
+ n
< first
)
1517 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1518 "position or range out of bounds",
1519 return isl_stat_error
);
1523 /* Insert an extra integer division, prescribed by "div", to "bmap"
1524 * at (integer division) position "pos".
1526 * The integer division is first added at the end and then moved
1527 * into the right position.
1529 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1530 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1534 bmap
= isl_basic_map_cow(bmap
);
1536 return isl_basic_map_free(bmap
);
1538 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1539 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1540 "unexpected size", return isl_basic_map_free(bmap
));
1541 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1542 return isl_basic_map_free(bmap
);
1544 bmap
= isl_basic_map_extend_space(bmap
,
1545 isl_basic_map_get_space(bmap
), 1, 0, 2);
1546 k
= isl_basic_map_alloc_div(bmap
);
1548 return isl_basic_map_free(bmap
);
1549 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1550 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1552 for (i
= k
; i
> pos
; --i
)
1553 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1558 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1561 return isl_stat_error
;
1562 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1567 /* Copy constraint from src to dst, putting the vars of src at offset
1568 * dim_off in dst and the divs of src at offset div_off in dst.
1569 * If both sets are actually map, then dim_off applies to the input
1572 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1573 struct isl_basic_map
*src_map
, isl_int
*src
,
1574 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1576 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1577 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1578 unsigned src_in
= isl_basic_map_n_in(src_map
);
1579 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1580 unsigned src_out
= isl_basic_map_n_out(src_map
);
1581 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1582 isl_int_set(dst
[0], src
[0]);
1583 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1584 if (dst_nparam
> src_nparam
)
1585 isl_seq_clr(dst
+1+src_nparam
,
1586 dst_nparam
- src_nparam
);
1587 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1588 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1590 isl_min(dst_in
-in_off
, src_in
));
1591 if (dst_in
-in_off
> src_in
)
1592 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1593 dst_in
- in_off
- src_in
);
1594 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1595 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1596 src
+1+src_nparam
+src_in
,
1597 isl_min(dst_out
-out_off
, src_out
));
1598 if (dst_out
-out_off
> src_out
)
1599 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1600 dst_out
- out_off
- src_out
);
1601 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1602 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1603 src
+1+src_nparam
+src_in
+src_out
,
1604 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1605 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1606 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1607 div_off
+src_map
->n_div
,
1608 dst_map
->n_div
- div_off
- src_map
->n_div
);
1611 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1612 struct isl_basic_map
*src_map
, isl_int
*src
,
1613 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1615 isl_int_set(dst
[0], src
[0]);
1616 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1619 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1620 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1625 if (!bmap1
|| !bmap2
)
1628 div_off
= bmap1
->n_div
;
1630 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1631 int i1
= isl_basic_map_alloc_equality(bmap1
);
1634 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1635 i_pos
, o_pos
, div_off
);
1638 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1639 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1642 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1643 i_pos
, o_pos
, div_off
);
1646 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1647 int i1
= isl_basic_map_alloc_div(bmap1
);
1650 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1651 i_pos
, o_pos
, div_off
);
1654 isl_basic_map_free(bmap2
);
1659 isl_basic_map_free(bmap1
);
1660 isl_basic_map_free(bmap2
);
1664 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1665 struct isl_basic_set
*bset2
, unsigned pos
)
1667 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1668 bset_to_bmap(bset2
), 0, pos
));
1671 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1672 __isl_take isl_space
*dim
, unsigned extra
,
1673 unsigned n_eq
, unsigned n_ineq
)
1675 struct isl_basic_map
*ext
;
1685 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1686 base
->extra
>= base
->n_div
+ extra
;
1688 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1689 room_for_ineq(base
, n_ineq
)) {
1690 isl_space_free(dim
);
1694 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1695 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1696 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1697 extra
+= base
->extra
;
1699 n_ineq
+= base
->n_ineq
;
1701 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1707 ext
->sample
= isl_vec_copy(base
->sample
);
1708 flags
= base
->flags
;
1709 ext
= add_constraints(ext
, base
, 0, 0);
1712 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1718 isl_space_free(dim
);
1719 isl_basic_map_free(base
);
1723 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1724 __isl_take isl_space
*dim
, unsigned extra
,
1725 unsigned n_eq
, unsigned n_ineq
)
1727 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1728 dim
, extra
, n_eq
, n_ineq
));
1731 struct isl_basic_map
*isl_basic_map_extend_constraints(
1732 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1736 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1740 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1741 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1742 unsigned n_eq
, unsigned n_ineq
)
1744 struct isl_basic_map
*bmap
;
1749 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1753 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1756 isl_basic_map_free(base
);
1760 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1761 unsigned nparam
, unsigned dim
, unsigned extra
,
1762 unsigned n_eq
, unsigned n_ineq
)
1764 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1765 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1768 struct isl_basic_set
*isl_basic_set_extend_constraints(
1769 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1771 isl_basic_map
*bmap
= bset_to_bmap(base
);
1772 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1773 return bset_from_bmap(bmap
);
1776 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1778 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1781 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1786 if (bmap
->ref
> 1) {
1788 bmap
= isl_basic_map_dup(bmap
);
1791 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1792 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1797 /* Clear all cached information in "map", either because it is about
1798 * to be modified or because it is being freed.
1799 * Always return the same pointer that is passed in.
1800 * This is needed for the use in isl_map_free.
1802 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1804 isl_basic_map_free(map
->cached_simple_hull
[0]);
1805 isl_basic_map_free(map
->cached_simple_hull
[1]);
1806 map
->cached_simple_hull
[0] = NULL
;
1807 map
->cached_simple_hull
[1] = NULL
;
1811 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1813 return isl_map_cow(set
);
1816 /* Return an isl_map that is equal to "map" and that has only
1817 * a single reference.
1819 * If the original input already has only one reference, then
1820 * simply return it, but clear all cached information, since
1821 * it may be rendered invalid by the operations that will be
1822 * performed on the result.
1824 * Otherwise, create a duplicate (without any cached information).
1826 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1832 return clear_caches(map
);
1834 return isl_map_dup(map
);
1837 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1838 unsigned a_len
, unsigned b_len
)
1840 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1841 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1842 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1845 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1846 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1854 isl_assert(bmap
->ctx
,
1855 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1857 if (n1
== 0 || n2
== 0)
1860 bmap
= isl_basic_map_cow(bmap
);
1864 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1865 if (isl_blk_is_error(blk
))
1868 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1870 bmap
->eq
[i
] + pos
, n1
, n2
);
1872 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1874 bmap
->ineq
[i
] + pos
, n1
, n2
);
1876 for (i
= 0; i
< bmap
->n_div
; ++i
)
1878 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1880 isl_blk_free(bmap
->ctx
, blk
);
1882 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1883 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1884 return isl_basic_map_finalize(bmap
);
1886 isl_basic_map_free(bmap
);
1890 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1896 total
= isl_basic_map_total_dim(bmap
);
1897 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1898 return isl_basic_map_free(bmap
);
1899 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1901 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1903 i
= isl_basic_map_alloc_equality(bmap
);
1907 isl_int_set_si(bmap
->eq
[i
][0], 1);
1908 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1909 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1910 isl_vec_free(bmap
->sample
);
1911 bmap
->sample
= NULL
;
1912 return isl_basic_map_finalize(bmap
);
1914 isl_basic_map_free(bmap
);
1918 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1920 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1923 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1926 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1928 isl_int
*t
= bmap
->div
[a
];
1929 bmap
->div
[a
] = bmap
->div
[b
];
1933 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1934 * div definitions accordingly.
1936 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1939 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1941 swap_div(bmap
, a
, b
);
1943 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1944 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1946 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1947 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1949 for (i
= 0; i
< bmap
->n_div
; ++i
)
1950 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1951 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1954 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1955 * div definitions accordingly.
1957 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1959 isl_basic_map_swap_div(bset
, a
, b
);
1962 /* Eliminate the specified n dimensions starting at first from the
1963 * constraints, without removing the dimensions from the space.
1964 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1966 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1967 enum isl_dim_type type
, unsigned first
, unsigned n
)
1976 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1977 isl_die(map
->ctx
, isl_error_invalid
,
1978 "index out of bounds", goto error
);
1980 map
= isl_map_cow(map
);
1984 for (i
= 0; i
< map
->n
; ++i
) {
1985 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1995 /* Eliminate the specified n dimensions starting at first from the
1996 * constraints, without removing the dimensions from the space.
1997 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1999 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2000 enum isl_dim_type type
, unsigned first
, unsigned n
)
2002 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2005 /* Eliminate the specified n dimensions starting at first from the
2006 * constraints, without removing the dimensions from the space.
2007 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2009 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2010 unsigned first
, unsigned n
)
2012 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2015 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2016 __isl_take isl_basic_map
*bmap
)
2020 bmap
= isl_basic_map_eliminate_vars(bmap
,
2021 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2025 return isl_basic_map_finalize(bmap
);
2028 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2029 __isl_take isl_basic_set
*bset
)
2031 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2034 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2043 map
= isl_map_cow(map
);
2047 for (i
= 0; i
< map
->n
; ++i
) {
2048 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2058 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2060 return isl_map_remove_divs(set
);
2063 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2064 enum isl_dim_type type
, unsigned first
, unsigned n
)
2066 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2067 return isl_basic_map_free(bmap
);
2068 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2070 bmap
= isl_basic_map_eliminate_vars(bmap
,
2071 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2074 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2076 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2080 /* Return true if the definition of the given div (recursively) involves
2081 * any of the given variables.
2083 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2084 unsigned first
, unsigned n
)
2087 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2089 if (isl_int_is_zero(bmap
->div
[div
][0]))
2090 return isl_bool_false
;
2091 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2092 return isl_bool_true
;
2094 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2097 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2099 involves
= div_involves_vars(bmap
, i
, first
, n
);
2100 if (involves
< 0 || involves
)
2104 return isl_bool_false
;
2107 /* Try and add a lower and/or upper bound on "div" to "bmap"
2108 * based on inequality "i".
2109 * "total" is the total number of variables (excluding the divs).
2110 * "v" is a temporary object that can be used during the calculations.
2111 * If "lb" is set, then a lower bound should be constructed.
2112 * If "ub" is set, then an upper bound should be constructed.
2114 * The calling function has already checked that the inequality does not
2115 * reference "div", but we still need to check that the inequality is
2116 * of the right form. We'll consider the case where we want to construct
2117 * a lower bound. The construction of upper bounds is similar.
2119 * Let "div" be of the form
2121 * q = floor((a + f(x))/d)
2123 * We essentially check if constraint "i" is of the form
2127 * so that we can use it to derive a lower bound on "div".
2128 * However, we allow a slightly more general form
2132 * with the condition that the coefficients of g(x) - f(x) are all
2134 * Rewriting this constraint as
2138 * adding a + f(x) to both sides and dividing by d, we obtain
2140 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2142 * Taking the floor on both sides, we obtain
2144 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2148 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2150 * In the case of an upper bound, we construct the constraint
2152 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2155 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2156 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2157 unsigned total
, isl_int v
, int lb
, int ub
)
2161 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2163 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2164 bmap
->div
[div
][1 + 1 + j
]);
2165 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2168 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2169 bmap
->div
[div
][1 + 1 + j
]);
2170 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2176 bmap
= isl_basic_map_cow(bmap
);
2177 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2179 int k
= isl_basic_map_alloc_inequality(bmap
);
2182 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2183 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2184 bmap
->div
[div
][1 + j
]);
2185 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2186 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2188 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2191 int k
= isl_basic_map_alloc_inequality(bmap
);
2194 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2195 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2196 bmap
->div
[div
][1 + j
]);
2197 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2198 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2200 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2203 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2206 isl_basic_map_free(bmap
);
2210 /* This function is called right before "div" is eliminated from "bmap"
2211 * using Fourier-Motzkin.
2212 * Look through the constraints of "bmap" for constraints on the argument
2213 * of the integer division and use them to construct constraints on the
2214 * integer division itself. These constraints can then be combined
2215 * during the Fourier-Motzkin elimination.
2216 * Note that it is only useful to introduce lower bounds on "div"
2217 * if "bmap" already contains upper bounds on "div" as the newly
2218 * introduce lower bounds can then be combined with the pre-existing
2219 * upper bounds. Similarly for upper bounds.
2220 * We therefore first check if "bmap" contains any lower and/or upper bounds
2223 * It is interesting to note that the introduction of these constraints
2224 * can indeed lead to more accurate results, even when compared to
2225 * deriving constraints on the argument of "div" from constraints on "div".
2226 * Consider, for example, the set
2228 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2230 * The second constraint can be rewritten as
2232 * 2 * [(-i-2j+3)/4] + k >= 0
2234 * from which we can derive
2236 * -i - 2j + 3 >= -2k
2242 * Combined with the first constraint, we obtain
2244 * -3 <= 3 + 2k or k >= -3
2246 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2247 * the first constraint, we obtain
2249 * [(i + 2j)/4] >= [-3/4] = -1
2251 * Combining this constraint with the second constraint, we obtain
2255 static __isl_give isl_basic_map
*insert_bounds_on_div(
2256 __isl_take isl_basic_map
*bmap
, int div
)
2259 int check_lb
, check_ub
;
2266 if (isl_int_is_zero(bmap
->div
[div
][0]))
2269 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2273 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2274 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2281 if (!check_lb
&& !check_ub
)
2286 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2287 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2290 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2291 check_lb
, check_ub
);
2299 /* Remove all divs (recursively) involving any of the given dimensions
2300 * in their definitions.
2302 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2303 __isl_take isl_basic_map
*bmap
,
2304 enum isl_dim_type type
, unsigned first
, unsigned n
)
2308 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2309 return isl_basic_map_free(bmap
);
2310 first
+= isl_basic_map_offset(bmap
, type
);
2312 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2315 involves
= div_involves_vars(bmap
, i
, first
, n
);
2317 return isl_basic_map_free(bmap
);
2320 bmap
= insert_bounds_on_div(bmap
, i
);
2321 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2330 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2331 __isl_take isl_basic_set
*bset
,
2332 enum isl_dim_type type
, unsigned first
, unsigned n
)
2334 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2337 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2338 enum isl_dim_type type
, unsigned first
, unsigned n
)
2347 map
= isl_map_cow(map
);
2351 for (i
= 0; i
< map
->n
; ++i
) {
2352 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2363 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2364 enum isl_dim_type type
, unsigned first
, unsigned n
)
2366 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2370 /* Does the description of "bmap" depend on the specified dimensions?
2371 * We also check whether the dimensions appear in any of the div definitions.
2372 * In principle there is no need for this check. If the dimensions appear
2373 * in a div definition, they also appear in the defining constraints of that
2376 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2377 enum isl_dim_type type
, unsigned first
, unsigned n
)
2381 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2382 return isl_bool_error
;
2384 first
+= isl_basic_map_offset(bmap
, type
);
2385 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2386 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2387 return isl_bool_true
;
2388 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2389 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2390 return isl_bool_true
;
2391 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2392 if (isl_int_is_zero(bmap
->div
[i
][0]))
2394 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2395 return isl_bool_true
;
2398 return isl_bool_false
;
2401 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2402 enum isl_dim_type type
, unsigned first
, unsigned n
)
2407 return isl_bool_error
;
2409 if (first
+ n
> isl_map_dim(map
, type
))
2410 isl_die(map
->ctx
, isl_error_invalid
,
2411 "index out of bounds", return isl_bool_error
);
2413 for (i
= 0; i
< map
->n
; ++i
) {
2414 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2416 if (involves
< 0 || involves
)
2420 return isl_bool_false
;
2423 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2424 enum isl_dim_type type
, unsigned first
, unsigned n
)
2426 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2429 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2430 enum isl_dim_type type
, unsigned first
, unsigned n
)
2432 return isl_map_involves_dims(set
, type
, first
, n
);
2435 /* Drop all constraints in bmap that involve any of the dimensions
2436 * first to first+n-1.
2438 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2439 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2446 bmap
= isl_basic_map_cow(bmap
);
2451 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2452 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2454 isl_basic_map_drop_equality(bmap
, i
);
2457 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2458 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2460 isl_basic_map_drop_inequality(bmap
, i
);
2463 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2467 /* Drop all constraints in bset that involve any of the dimensions
2468 * first to first+n-1.
2470 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2471 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2473 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2476 /* Drop all constraints in bmap that do not involve any of the dimensions
2477 * first to first + n - 1 of the given type.
2479 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2480 __isl_take isl_basic_map
*bmap
,
2481 enum isl_dim_type type
, unsigned first
, unsigned n
)
2486 isl_space
*space
= isl_basic_map_get_space(bmap
);
2487 isl_basic_map_free(bmap
);
2488 return isl_basic_map_universe(space
);
2490 bmap
= isl_basic_map_cow(bmap
);
2494 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2495 return isl_basic_map_free(bmap
);
2497 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2499 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2500 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2502 isl_basic_map_drop_equality(bmap
, i
);
2505 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2506 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2508 isl_basic_map_drop_inequality(bmap
, i
);
2511 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2515 /* Drop all constraints in bset that do not involve any of the dimensions
2516 * first to first + n - 1 of the given type.
2518 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2519 __isl_take isl_basic_set
*bset
,
2520 enum isl_dim_type type
, unsigned first
, unsigned n
)
2522 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2526 /* Drop all constraints in bmap that involve any of the dimensions
2527 * first to first + n - 1 of the given type.
2529 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2530 __isl_take isl_basic_map
*bmap
,
2531 enum isl_dim_type type
, unsigned first
, unsigned n
)
2538 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2539 return isl_basic_map_free(bmap
);
2541 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2542 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2543 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2546 /* Drop all constraints in bset that involve any of the dimensions
2547 * first to first + n - 1 of the given type.
2549 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2550 __isl_take isl_basic_set
*bset
,
2551 enum isl_dim_type type
, unsigned first
, unsigned n
)
2553 return isl_basic_map_drop_constraints_involving_dims(bset
,
2557 /* Drop constraints from "map" by applying "drop" to each basic map.
2559 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2560 enum isl_dim_type type
, unsigned first
, unsigned n
,
2561 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2562 enum isl_dim_type type
, unsigned first
, unsigned n
))
2570 dim
= isl_map_dim(map
, type
);
2571 if (first
+ n
> dim
|| first
+ n
< first
)
2572 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2573 "index out of bounds", return isl_map_free(map
));
2575 map
= isl_map_cow(map
);
2579 for (i
= 0; i
< map
->n
; ++i
) {
2580 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2582 return isl_map_free(map
);
2586 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2591 /* Drop all constraints in map that involve any of the dimensions
2592 * first to first + n - 1 of the given type.
2594 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2595 __isl_take isl_map
*map
,
2596 enum isl_dim_type type
, unsigned first
, unsigned n
)
2600 return drop_constraints(map
, type
, first
, n
,
2601 &isl_basic_map_drop_constraints_involving_dims
);
2604 /* Drop all constraints in "map" that do not involve any of the dimensions
2605 * first to first + n - 1 of the given type.
2607 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2608 __isl_take isl_map
*map
,
2609 enum isl_dim_type type
, unsigned first
, unsigned n
)
2612 isl_space
*space
= isl_map_get_space(map
);
2614 return isl_map_universe(space
);
2616 return drop_constraints(map
, type
, first
, n
,
2617 &isl_basic_map_drop_constraints_not_involving_dims
);
2620 /* Drop all constraints in set that involve any of the dimensions
2621 * first to first + n - 1 of the given type.
2623 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2624 __isl_take isl_set
*set
,
2625 enum isl_dim_type type
, unsigned first
, unsigned n
)
2627 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2630 /* Drop all constraints in "set" that do not involve any of the dimensions
2631 * first to first + n - 1 of the given type.
2633 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2634 __isl_take isl_set
*set
,
2635 enum isl_dim_type type
, unsigned first
, unsigned n
)
2637 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2640 /* Does local variable "div" of "bmap" have a complete explicit representation?
2641 * Having a complete explicit representation requires not only
2642 * an explicit representation, but also that all local variables
2643 * that appear in this explicit representation in turn have
2644 * a complete explicit representation.
2646 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2649 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2652 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2653 if (marked
< 0 || marked
)
2654 return isl_bool_not(marked
);
2656 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2659 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2661 known
= isl_basic_map_div_is_known(bmap
, i
);
2662 if (known
< 0 || !known
)
2666 return isl_bool_true
;
2669 /* Remove all divs that are unknown or defined in terms of unknown divs.
2671 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2672 __isl_take isl_basic_map
*bmap
)
2679 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2680 if (isl_basic_map_div_is_known(bmap
, i
))
2682 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2691 /* Remove all divs that are unknown or defined in terms of unknown divs.
2693 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2694 __isl_take isl_basic_set
*bset
)
2696 return isl_basic_map_remove_unknown_divs(bset
);
2699 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2708 map
= isl_map_cow(map
);
2712 for (i
= 0; i
< map
->n
; ++i
) {
2713 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2723 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2725 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2728 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2729 __isl_take isl_basic_set
*bset
,
2730 enum isl_dim_type type
, unsigned first
, unsigned n
)
2732 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2733 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2734 return bset_from_bmap(bmap
);
2737 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2738 enum isl_dim_type type
, unsigned first
, unsigned n
)
2745 map
= isl_map_cow(map
);
2748 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2750 for (i
= 0; i
< map
->n
; ++i
) {
2751 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2752 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2756 map
= isl_map_drop(map
, type
, first
, n
);
2763 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2764 enum isl_dim_type type
, unsigned first
, unsigned n
)
2766 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2770 /* Project out n inputs starting at first using Fourier-Motzkin */
2771 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2772 unsigned first
, unsigned n
)
2774 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2777 static void dump_term(struct isl_basic_map
*bmap
,
2778 isl_int c
, int pos
, FILE *out
)
2781 unsigned in
= isl_basic_map_n_in(bmap
);
2782 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2783 unsigned nparam
= isl_basic_map_n_param(bmap
);
2785 isl_int_print(out
, c
, 0);
2787 if (!isl_int_is_one(c
))
2788 isl_int_print(out
, c
, 0);
2789 if (pos
< 1 + nparam
) {
2790 name
= isl_space_get_dim_name(bmap
->dim
,
2791 isl_dim_param
, pos
- 1);
2793 fprintf(out
, "%s", name
);
2795 fprintf(out
, "p%d", pos
- 1);
2796 } else if (pos
< 1 + nparam
+ in
)
2797 fprintf(out
, "i%d", pos
- 1 - nparam
);
2798 else if (pos
< 1 + nparam
+ dim
)
2799 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2801 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2805 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2806 int sign
, FILE *out
)
2810 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2814 for (i
= 0, first
= 1; i
< len
; ++i
) {
2815 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2818 fprintf(out
, " + ");
2820 isl_int_abs(v
, c
[i
]);
2821 dump_term(bmap
, v
, i
, out
);
2828 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2829 const char *op
, FILE *out
, int indent
)
2833 fprintf(out
, "%*s", indent
, "");
2835 dump_constraint_sign(bmap
, c
, 1, out
);
2836 fprintf(out
, " %s ", op
);
2837 dump_constraint_sign(bmap
, c
, -1, out
);
2841 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2842 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2844 fprintf(out
, "%*s", indent
, "");
2845 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2850 static void dump_constraints(struct isl_basic_map
*bmap
,
2851 isl_int
**c
, unsigned n
,
2852 const char *op
, FILE *out
, int indent
)
2856 for (i
= 0; i
< n
; ++i
)
2857 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2860 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2864 unsigned total
= isl_basic_map_total_dim(bmap
);
2866 for (j
= 0; j
< 1 + total
; ++j
) {
2867 if (isl_int_is_zero(exp
[j
]))
2869 if (!first
&& isl_int_is_pos(exp
[j
]))
2871 dump_term(bmap
, exp
[j
], j
, out
);
2876 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2880 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2881 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2883 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2884 fprintf(out
, "%*s", indent
, "");
2885 fprintf(out
, "e%d = [(", i
);
2886 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2888 isl_int_print(out
, bmap
->div
[i
][0], 0);
2889 fprintf(out
, "]\n");
2893 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2894 FILE *out
, int indent
)
2897 fprintf(out
, "null basic set\n");
2901 fprintf(out
, "%*s", indent
, "");
2902 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2903 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2904 bset
->extra
, bset
->flags
);
2905 dump(bset_to_bmap(bset
), out
, indent
);
2908 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2909 FILE *out
, int indent
)
2912 fprintf(out
, "null basic map\n");
2916 fprintf(out
, "%*s", indent
, "");
2917 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2918 "flags: %x, n_name: %d\n",
2920 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2921 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2922 dump(bmap
, out
, indent
);
2925 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2930 total
= isl_basic_map_total_dim(bmap
);
2931 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2932 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2933 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2934 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2938 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2943 if (isl_space_dim(space
, isl_dim_in
) != 0)
2944 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2945 "set cannot have input dimensions", goto error
);
2946 return isl_map_alloc_space(space
, n
, flags
);
2948 isl_space_free(space
);
2952 /* Make sure "map" has room for at least "n" more basic maps.
2954 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2957 struct isl_map
*grown
= NULL
;
2961 isl_assert(map
->ctx
, n
>= 0, goto error
);
2962 if (map
->n
+ n
<= map
->size
)
2964 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2967 for (i
= 0; i
< map
->n
; ++i
) {
2968 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2976 isl_map_free(grown
);
2981 /* Make sure "set" has room for at least "n" more basic sets.
2983 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2985 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2988 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2990 return isl_map_from_basic_map(bset
);
2993 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2995 struct isl_map
*map
;
3000 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3001 return isl_map_add_basic_map(map
, bmap
);
3004 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3005 __isl_take isl_basic_set
*bset
)
3007 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3008 bset_to_bmap(bset
)));
3011 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3013 return isl_map_free(set
);
3016 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3021 fprintf(out
, "null set\n");
3025 fprintf(out
, "%*s", indent
, "");
3026 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3027 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3029 for (i
= 0; i
< set
->n
; ++i
) {
3030 fprintf(out
, "%*s", indent
, "");
3031 fprintf(out
, "basic set %d:\n", i
);
3032 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3036 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3041 fprintf(out
, "null map\n");
3045 fprintf(out
, "%*s", indent
, "");
3046 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3047 "flags: %x, n_name: %d\n",
3048 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3049 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3050 for (i
= 0; i
< map
->n
; ++i
) {
3051 fprintf(out
, "%*s", indent
, "");
3052 fprintf(out
, "basic map %d:\n", i
);
3053 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3057 struct isl_basic_map
*isl_basic_map_intersect_domain(
3058 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3060 struct isl_basic_map
*bmap_domain
;
3065 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3066 bset
->dim
, isl_dim_param
), goto error
);
3068 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3069 isl_assert(bset
->ctx
,
3070 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3072 bmap
= isl_basic_map_cow(bmap
);
3075 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3076 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3077 bmap_domain
= isl_basic_map_from_domain(bset
);
3078 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3080 bmap
= isl_basic_map_simplify(bmap
);
3081 return isl_basic_map_finalize(bmap
);
3083 isl_basic_map_free(bmap
);
3084 isl_basic_set_free(bset
);
3088 /* Check that the space of "bset" is the same as that of the range of "bmap".
3090 static isl_stat
isl_basic_map_check_compatible_range(
3091 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3095 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3097 return isl_stat_error
;
3099 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3100 "incompatible spaces", return isl_stat_error
);
3105 struct isl_basic_map
*isl_basic_map_intersect_range(
3106 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3108 struct isl_basic_map
*bmap_range
;
3113 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3114 bset
->dim
, isl_dim_param
), goto error
);
3116 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3117 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3120 if (isl_basic_set_plain_is_universe(bset
)) {
3121 isl_basic_set_free(bset
);
3125 bmap
= isl_basic_map_cow(bmap
);
3128 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3129 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3130 bmap_range
= bset_to_bmap(bset
);
3131 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3133 bmap
= isl_basic_map_simplify(bmap
);
3134 return isl_basic_map_finalize(bmap
);
3136 isl_basic_map_free(bmap
);
3137 isl_basic_set_free(bset
);
3141 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3142 __isl_keep isl_vec
*vec
)
3149 return isl_bool_error
;
3151 total
= 1 + isl_basic_map_total_dim(bmap
);
3152 if (total
!= vec
->size
)
3153 return isl_bool_false
;
3157 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3158 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3159 if (!isl_int_is_zero(s
)) {
3161 return isl_bool_false
;
3165 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3166 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3167 if (isl_int_is_neg(s
)) {
3169 return isl_bool_false
;
3175 return isl_bool_true
;
3178 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3179 __isl_keep isl_vec
*vec
)
3181 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3184 struct isl_basic_map
*isl_basic_map_intersect(
3185 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3187 struct isl_vec
*sample
= NULL
;
3189 if (!bmap1
|| !bmap2
)
3192 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3193 bmap2
->dim
, isl_dim_param
), goto error
);
3194 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3195 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3196 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3197 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3198 return isl_basic_map_intersect(bmap2
, bmap1
);
3200 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3201 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3202 isl_assert(bmap1
->ctx
,
3203 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3205 if (isl_basic_map_plain_is_empty(bmap1
)) {
3206 isl_basic_map_free(bmap2
);
3209 if (isl_basic_map_plain_is_empty(bmap2
)) {
3210 isl_basic_map_free(bmap1
);
3214 if (bmap1
->sample
&&
3215 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3216 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3217 sample
= isl_vec_copy(bmap1
->sample
);
3218 else if (bmap2
->sample
&&
3219 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3220 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3221 sample
= isl_vec_copy(bmap2
->sample
);
3223 bmap1
= isl_basic_map_cow(bmap1
);
3226 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3227 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3228 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3231 isl_vec_free(sample
);
3233 isl_vec_free(bmap1
->sample
);
3234 bmap1
->sample
= sample
;
3237 bmap1
= isl_basic_map_simplify(bmap1
);
3238 return isl_basic_map_finalize(bmap1
);
3241 isl_vec_free(sample
);
3242 isl_basic_map_free(bmap1
);
3243 isl_basic_map_free(bmap2
);
3247 struct isl_basic_set
*isl_basic_set_intersect(
3248 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3250 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3251 bset_to_bmap(bset2
)));
3254 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3255 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3257 return isl_basic_set_intersect(bset1
, bset2
);
3260 /* Special case of isl_map_intersect, where both map1 and map2
3261 * are convex, without any divs and such that either map1 or map2
3262 * contains a single constraint. This constraint is then simply
3263 * added to the other map.
3265 static __isl_give isl_map
*map_intersect_add_constraint(
3266 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3268 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3269 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3270 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3271 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3273 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3274 return isl_map_intersect(map2
, map1
);
3276 map1
= isl_map_cow(map1
);
3279 if (isl_map_plain_is_empty(map1
)) {
3283 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3284 if (map2
->p
[0]->n_eq
== 1)
3285 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3287 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3288 map2
->p
[0]->ineq
[0]);
3290 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3291 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3295 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3296 isl_basic_map_free(map1
->p
[0]);
3309 /* map2 may be either a parameter domain or a map living in the same
3312 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3313 __isl_take isl_map
*map2
)
3322 if ((isl_map_plain_is_empty(map1
) ||
3323 isl_map_plain_is_universe(map2
)) &&
3324 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3328 if ((isl_map_plain_is_empty(map2
) ||
3329 isl_map_plain_is_universe(map1
)) &&
3330 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3335 if (map1
->n
== 1 && map2
->n
== 1 &&
3336 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3337 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3338 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3339 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3340 return map_intersect_add_constraint(map1
, map2
);
3342 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3343 isl_space_dim(map2
->dim
, isl_dim_param
))
3344 isl_assert(map1
->ctx
,
3345 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3347 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3348 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3349 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3351 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3352 map1
->n
* map2
->n
, flags
);
3355 for (i
= 0; i
< map1
->n
; ++i
)
3356 for (j
= 0; j
< map2
->n
; ++j
) {
3357 struct isl_basic_map
*part
;
3358 part
= isl_basic_map_intersect(
3359 isl_basic_map_copy(map1
->p
[i
]),
3360 isl_basic_map_copy(map2
->p
[j
]));
3361 if (isl_basic_map_is_empty(part
) < 0)
3362 part
= isl_basic_map_free(part
);
3363 result
= isl_map_add_basic_map(result
, part
);
3376 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3377 __isl_take isl_map
*map2
)
3381 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3382 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3383 "spaces don't match", goto error
);
3384 return map_intersect_internal(map1
, map2
);
3391 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3392 __isl_take isl_map
*map2
)
3394 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3397 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3399 return set_from_map(isl_map_intersect(set_to_map(set1
),
3403 /* map_intersect_internal accepts intersections
3404 * with parameter domains, so we can just call that function.
3406 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3407 __isl_take isl_set
*params
)
3409 return map_intersect_internal(map
, params
);
3412 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3413 __isl_take isl_map
*map2
)
3415 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3418 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3419 __isl_take isl_set
*params
)
3421 return isl_map_intersect_params(set
, params
);
3424 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3427 unsigned pos
, n1
, n2
;
3431 bmap
= isl_basic_map_cow(bmap
);
3434 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3435 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3436 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3437 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3438 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3439 return isl_basic_map_reset_space(bmap
, space
);
3442 static __isl_give isl_basic_map
*basic_map_space_reset(
3443 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3449 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3452 space
= isl_basic_map_get_space(bmap
);
3453 space
= isl_space_reset(space
, type
);
3454 bmap
= isl_basic_map_reset_space(bmap
, space
);
3458 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3459 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3460 unsigned pos
, unsigned n
)
3464 struct isl_basic_map
*res
;
3465 struct isl_dim_map
*dim_map
;
3466 unsigned total
, off
;
3467 enum isl_dim_type t
;
3470 return basic_map_space_reset(bmap
, type
);
3475 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3477 total
= isl_basic_map_total_dim(bmap
) + n
;
3478 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3480 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3482 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3484 unsigned size
= isl_basic_map_dim(bmap
, t
);
3485 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3487 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3488 pos
, size
- pos
, off
+ pos
+ n
);
3490 off
+= isl_space_dim(res_dim
, t
);
3492 isl_dim_map_div(dim_map
, bmap
, off
);
3494 res
= isl_basic_map_alloc_space(res_dim
,
3495 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3496 rational
= isl_basic_map_is_rational(bmap
);
3498 res
= isl_basic_map_free(res
);
3500 res
= isl_basic_map_set_rational(res
);
3501 if (isl_basic_map_plain_is_empty(bmap
)) {
3502 isl_basic_map_free(bmap
);
3504 return isl_basic_map_set_to_empty(res
);
3506 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3507 return isl_basic_map_finalize(res
);
3510 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3511 __isl_take isl_basic_set
*bset
,
3512 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3514 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3517 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3518 enum isl_dim_type type
, unsigned n
)
3522 return isl_basic_map_insert_dims(bmap
, type
,
3523 isl_basic_map_dim(bmap
, type
), n
);
3526 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3527 enum isl_dim_type type
, unsigned n
)
3531 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3532 return isl_basic_map_add_dims(bset
, type
, n
);
3534 isl_basic_set_free(bset
);
3538 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3539 enum isl_dim_type type
)
3543 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3546 space
= isl_map_get_space(map
);
3547 space
= isl_space_reset(space
, type
);
3548 map
= isl_map_reset_space(map
, space
);
3552 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3553 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3558 return map_space_reset(map
, type
);
3560 map
= isl_map_cow(map
);
3564 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3568 for (i
= 0; i
< map
->n
; ++i
) {
3569 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3580 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3581 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3583 return isl_map_insert_dims(set
, type
, pos
, n
);
3586 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3587 enum isl_dim_type type
, unsigned n
)
3591 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3594 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3595 enum isl_dim_type type
, unsigned n
)
3599 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3600 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3606 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3607 __isl_take isl_basic_map
*bmap
,
3608 enum isl_dim_type dst_type
, unsigned dst_pos
,
3609 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3611 struct isl_dim_map
*dim_map
;
3612 struct isl_basic_map
*res
;
3613 enum isl_dim_type t
;
3614 unsigned total
, off
;
3621 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3622 return isl_basic_map_free(bmap
);
3624 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3627 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3629 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3630 pos(bmap
->dim
, src_type
) + src_pos
+
3631 ((src_type
< dst_type
) ? n
: 0)) {
3632 bmap
= isl_basic_map_cow(bmap
);
3636 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3637 src_type
, src_pos
, n
);
3641 bmap
= isl_basic_map_finalize(bmap
);
3646 total
= isl_basic_map_total_dim(bmap
);
3647 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3650 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3651 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3652 if (t
== dst_type
) {
3653 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3656 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3659 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3660 dst_pos
, size
- dst_pos
, off
);
3661 off
+= size
- dst_pos
;
3662 } else if (t
== src_type
) {
3663 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3666 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3667 src_pos
+ n
, size
- src_pos
- n
, off
);
3668 off
+= size
- src_pos
- n
;
3670 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3674 isl_dim_map_div(dim_map
, bmap
, off
);
3676 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3677 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3678 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3682 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3683 src_type
, src_pos
, n
);
3687 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3688 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3689 bmap
= isl_basic_map_finalize(bmap
);
3693 isl_basic_map_free(bmap
);
3697 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3698 enum isl_dim_type dst_type
, unsigned dst_pos
,
3699 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3701 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3702 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3703 src_type
, src_pos
, n
);
3704 return bset_from_bmap(bmap
);
3707 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3708 enum isl_dim_type dst_type
, unsigned dst_pos
,
3709 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3713 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3714 return set_from_map(isl_map_move_dims(set_to_map(set
),
3715 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3721 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3722 enum isl_dim_type dst_type
, unsigned dst_pos
,
3723 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3732 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3735 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3738 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3740 map
= isl_map_cow(map
);
3744 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3748 for (i
= 0; i
< map
->n
; ++i
) {
3749 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3751 src_type
, src_pos
, n
);
3762 /* Move the specified dimensions to the last columns right before
3763 * the divs. Don't change the dimension specification of bmap.
3764 * That's the responsibility of the caller.
3766 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3767 enum isl_dim_type type
, unsigned first
, unsigned n
)
3769 struct isl_dim_map
*dim_map
;
3770 struct isl_basic_map
*res
;
3771 enum isl_dim_type t
;
3772 unsigned total
, off
;
3776 if (pos(bmap
->dim
, type
) + first
+ n
==
3777 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3780 total
= isl_basic_map_total_dim(bmap
);
3781 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3784 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3785 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3787 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3790 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3791 first
, n
, total
- bmap
->n_div
- n
);
3792 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3793 first
+ n
, size
- (first
+ n
), off
);
3794 off
+= size
- (first
+ n
);
3796 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3800 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3802 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3803 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3804 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3808 /* Insert "n" rows in the divs of "bmap".
3810 * The number of columns is not changed, which means that the last
3811 * dimensions of "bmap" are being reintepreted as the new divs.
3812 * The space of "bmap" is not adjusted, however, which means
3813 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3814 * from the space of "bmap" is the responsibility of the caller.
3816 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3824 bmap
= isl_basic_map_cow(bmap
);
3828 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3829 old
= bmap
->block2
.data
;
3830 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3831 (bmap
->extra
+ n
) * (1 + row_size
));
3832 if (!bmap
->block2
.data
)
3833 return isl_basic_map_free(bmap
);
3834 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3836 return isl_basic_map_free(bmap
);
3837 for (i
= 0; i
< n
; ++i
) {
3838 new_div
[i
] = bmap
->block2
.data
+
3839 (bmap
->extra
+ i
) * (1 + row_size
);
3840 isl_seq_clr(new_div
[i
], 1 + row_size
);
3842 for (i
= 0; i
< bmap
->extra
; ++i
)
3843 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3845 bmap
->div
= new_div
;
3852 /* Drop constraints from "bmap" that only involve the variables
3853 * of "type" in the range [first, first + n] that are not related
3854 * to any of the variables outside that interval.
3855 * These constraints cannot influence the values for the variables
3856 * outside the interval, except in case they cause "bmap" to be empty.
3857 * Only drop the constraints if "bmap" is known to be non-empty.
3859 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3860 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3861 unsigned first
, unsigned n
)
3865 unsigned dim
, n_div
;
3868 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3870 return isl_basic_map_free(bmap
);
3874 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3875 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3876 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3878 return isl_basic_map_free(bmap
);
3879 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3880 for (i
= 0; i
< first
; ++i
)
3882 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3885 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3890 /* Turn the n dimensions of type type, starting at first
3891 * into existentially quantified variables.
3893 * If a subset of the projected out variables are unrelated
3894 * to any of the variables that remain, then the constraints
3895 * involving this subset are simply dropped first.
3897 __isl_give isl_basic_map
*isl_basic_map_project_out(
3898 __isl_take isl_basic_map
*bmap
,
3899 enum isl_dim_type type
, unsigned first
, unsigned n
)
3902 return basic_map_space_reset(bmap
, type
);
3903 if (type
== isl_dim_div
)
3904 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3905 "cannot project out existentially quantified variables",
3906 return isl_basic_map_free(bmap
));
3908 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3912 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3913 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3915 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3916 return isl_basic_map_free(bmap
);
3918 bmap
= move_last(bmap
, type
, first
, n
);
3919 bmap
= isl_basic_map_cow(bmap
);
3920 bmap
= insert_div_rows(bmap
, n
);
3924 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3927 bmap
= isl_basic_map_simplify(bmap
);
3928 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3929 return isl_basic_map_finalize(bmap
);
3931 isl_basic_map_free(bmap
);
3935 /* Turn the n dimensions of type type, starting at first
3936 * into existentially quantified variables.
3938 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3939 enum isl_dim_type type
, unsigned first
, unsigned n
)
3941 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3945 /* Turn the n dimensions of type type, starting at first
3946 * into existentially quantified variables.
3948 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3949 enum isl_dim_type type
, unsigned first
, unsigned n
)
3957 return map_space_reset(map
, type
);
3959 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3961 map
= isl_map_cow(map
);
3965 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3969 for (i
= 0; i
< map
->n
; ++i
) {
3970 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3981 /* Turn the n dimensions of type type, starting at first
3982 * into existentially quantified variables.
3984 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3985 enum isl_dim_type type
, unsigned first
, unsigned n
)
3987 return set_from_map(isl_map_project_out(set_to_map(set
),
3991 /* Return a map that projects the elements in "set" onto their
3992 * "n" set dimensions starting at "first".
3993 * "type" should be equal to isl_dim_set.
3995 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3996 enum isl_dim_type type
, unsigned first
, unsigned n
)
4004 if (type
!= isl_dim_set
)
4005 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4006 "only set dimensions can be projected out", goto error
);
4007 dim
= isl_set_dim(set
, isl_dim_set
);
4008 if (first
+ n
> dim
|| first
+ n
< first
)
4009 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4010 "index out of bounds", goto error
);
4012 map
= isl_map_from_domain(set
);
4013 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4014 for (i
= 0; i
< n
; ++i
)
4015 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4023 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4027 for (i
= 0; i
< n
; ++i
) {
4028 j
= isl_basic_map_alloc_div(bmap
);
4031 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4035 isl_basic_map_free(bmap
);
4039 struct isl_basic_map
*isl_basic_map_apply_range(
4040 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4042 isl_space
*dim_result
= NULL
;
4043 struct isl_basic_map
*bmap
;
4044 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4045 struct isl_dim_map
*dim_map1
, *dim_map2
;
4047 if (!bmap1
|| !bmap2
)
4049 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4050 bmap2
->dim
, isl_dim_param
))
4051 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4052 "parameters don't match", goto error
);
4053 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4054 bmap2
->dim
, isl_dim_in
))
4055 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4056 "spaces don't match", goto error
);
4058 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4059 isl_space_copy(bmap2
->dim
));
4061 n_in
= isl_basic_map_n_in(bmap1
);
4062 n_out
= isl_basic_map_n_out(bmap2
);
4063 n
= isl_basic_map_n_out(bmap1
);
4064 nparam
= isl_basic_map_n_param(bmap1
);
4066 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4067 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4068 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4069 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4070 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4071 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4072 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4073 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4074 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4075 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4076 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4078 bmap
= isl_basic_map_alloc_space(dim_result
,
4079 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4080 bmap1
->n_eq
+ bmap2
->n_eq
,
4081 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4082 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4083 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4084 bmap
= add_divs(bmap
, n
);
4085 bmap
= isl_basic_map_simplify(bmap
);
4086 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4087 return isl_basic_map_finalize(bmap
);
4089 isl_basic_map_free(bmap1
);
4090 isl_basic_map_free(bmap2
);
4094 struct isl_basic_set
*isl_basic_set_apply(
4095 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4100 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4103 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4106 isl_basic_set_free(bset
);
4107 isl_basic_map_free(bmap
);
4111 struct isl_basic_map
*isl_basic_map_apply_domain(
4112 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4114 if (!bmap1
|| !bmap2
)
4117 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4118 bmap2
->dim
, isl_dim_param
))
4119 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4120 "parameters don't match", goto error
);
4121 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4122 bmap2
->dim
, isl_dim_in
))
4123 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4124 "spaces don't match", goto error
);
4126 bmap1
= isl_basic_map_reverse(bmap1
);
4127 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4128 return isl_basic_map_reverse(bmap1
);
4130 isl_basic_map_free(bmap1
);
4131 isl_basic_map_free(bmap2
);
4135 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4136 * A \cap B -> f(A) + f(B)
4138 struct isl_basic_map
*isl_basic_map_sum(
4139 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4141 unsigned n_in
, n_out
, nparam
, total
, pos
;
4142 struct isl_basic_map
*bmap
= NULL
;
4143 struct isl_dim_map
*dim_map1
, *dim_map2
;
4146 if (!bmap1
|| !bmap2
)
4149 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4152 nparam
= isl_basic_map_n_param(bmap1
);
4153 n_in
= isl_basic_map_n_in(bmap1
);
4154 n_out
= isl_basic_map_n_out(bmap1
);
4156 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4157 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4158 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4159 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4160 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4161 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4162 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4163 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4164 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4165 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4166 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4168 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4169 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4170 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4171 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4172 for (i
= 0; i
< n_out
; ++i
) {
4173 int j
= isl_basic_map_alloc_equality(bmap
);
4176 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4177 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4178 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4179 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4181 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4182 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4183 bmap
= add_divs(bmap
, 2 * n_out
);
4185 bmap
= isl_basic_map_simplify(bmap
);
4186 return isl_basic_map_finalize(bmap
);
4188 isl_basic_map_free(bmap
);
4189 isl_basic_map_free(bmap1
);
4190 isl_basic_map_free(bmap2
);
4194 /* Given two maps A -> f(A) and B -> g(B), construct a map
4195 * A \cap B -> f(A) + f(B)
4197 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4199 struct isl_map
*result
;
4205 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4207 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4208 map1
->n
* map2
->n
, 0);
4211 for (i
= 0; i
< map1
->n
; ++i
)
4212 for (j
= 0; j
< map2
->n
; ++j
) {
4213 struct isl_basic_map
*part
;
4214 part
= isl_basic_map_sum(
4215 isl_basic_map_copy(map1
->p
[i
]),
4216 isl_basic_map_copy(map2
->p
[j
]));
4217 if (isl_basic_map_is_empty(part
))
4218 isl_basic_map_free(part
);
4220 result
= isl_map_add_basic_map(result
, part
);
4233 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4234 __isl_take isl_set
*set2
)
4236 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4239 /* Given a basic map A -> f(A), construct A -> -f(A).
4241 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4246 bmap
= isl_basic_map_cow(bmap
);
4250 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4251 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4252 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4253 for (j
= 0; j
< n
; ++j
)
4254 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4255 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4256 for (j
= 0; j
< n
; ++j
)
4257 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4258 for (i
= 0; i
< bmap
->n_div
; ++i
)
4259 for (j
= 0; j
< n
; ++j
)
4260 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4261 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4262 return isl_basic_map_finalize(bmap
);
4265 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4267 return isl_basic_map_neg(bset
);
4270 /* Given a map A -> f(A), construct A -> -f(A).
4272 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4276 map
= isl_map_cow(map
);
4280 for (i
= 0; i
< map
->n
; ++i
) {
4281 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4292 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4294 return set_from_map(isl_map_neg(set_to_map(set
)));
4297 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4298 * A -> floor(f(A)/d).
4300 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4303 unsigned n_in
, n_out
, nparam
, total
, pos
;
4304 struct isl_basic_map
*result
= NULL
;
4305 struct isl_dim_map
*dim_map
;
4311 nparam
= isl_basic_map_n_param(bmap
);
4312 n_in
= isl_basic_map_n_in(bmap
);
4313 n_out
= isl_basic_map_n_out(bmap
);
4315 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4316 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4317 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4318 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4319 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4320 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4322 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4323 bmap
->n_div
+ n_out
,
4324 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4325 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4326 result
= add_divs(result
, n_out
);
4327 for (i
= 0; i
< n_out
; ++i
) {
4329 j
= isl_basic_map_alloc_inequality(result
);
4332 isl_seq_clr(result
->ineq
[j
], 1+total
);
4333 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4334 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4335 j
= isl_basic_map_alloc_inequality(result
);
4338 isl_seq_clr(result
->ineq
[j
], 1+total
);
4339 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4340 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4341 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4344 result
= isl_basic_map_simplify(result
);
4345 return isl_basic_map_finalize(result
);
4347 isl_basic_map_free(result
);
4351 /* Given a map A -> f(A) and an integer d, construct a map
4352 * A -> floor(f(A)/d).
4354 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4358 map
= isl_map_cow(map
);
4362 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4363 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4364 for (i
= 0; i
< map
->n
; ++i
) {
4365 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4376 /* Given a map A -> f(A) and an integer d, construct a map
4377 * A -> floor(f(A)/d).
4379 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4380 __isl_take isl_val
*d
)
4384 if (!isl_val_is_int(d
))
4385 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4386 "expecting integer denominator", goto error
);
4387 map
= isl_map_floordiv(map
, d
->n
);
4396 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4402 i
= isl_basic_map_alloc_equality(bmap
);
4405 nparam
= isl_basic_map_n_param(bmap
);
4406 n_in
= isl_basic_map_n_in(bmap
);
4407 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4408 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4409 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4410 return isl_basic_map_finalize(bmap
);
4412 isl_basic_map_free(bmap
);
4416 /* Add a constraint to "bmap" expressing i_pos < o_pos
4418 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4424 i
= isl_basic_map_alloc_inequality(bmap
);
4427 nparam
= isl_basic_map_n_param(bmap
);
4428 n_in
= isl_basic_map_n_in(bmap
);
4429 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4430 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4431 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4432 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4433 return isl_basic_map_finalize(bmap
);
4435 isl_basic_map_free(bmap
);
4439 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4441 static __isl_give isl_basic_map
*var_less_or_equal(
4442 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4448 i
= isl_basic_map_alloc_inequality(bmap
);
4451 nparam
= isl_basic_map_n_param(bmap
);
4452 n_in
= isl_basic_map_n_in(bmap
);
4453 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4454 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4455 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4456 return isl_basic_map_finalize(bmap
);
4458 isl_basic_map_free(bmap
);
4462 /* Add a constraint to "bmap" expressing i_pos > o_pos
4464 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4470 i
= isl_basic_map_alloc_inequality(bmap
);
4473 nparam
= isl_basic_map_n_param(bmap
);
4474 n_in
= isl_basic_map_n_in(bmap
);
4475 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4476 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4477 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4478 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4479 return isl_basic_map_finalize(bmap
);
4481 isl_basic_map_free(bmap
);
4485 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4487 static __isl_give isl_basic_map
*var_more_or_equal(
4488 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4494 i
= isl_basic_map_alloc_inequality(bmap
);
4497 nparam
= isl_basic_map_n_param(bmap
);
4498 n_in
= isl_basic_map_n_in(bmap
);
4499 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4500 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4501 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4502 return isl_basic_map_finalize(bmap
);
4504 isl_basic_map_free(bmap
);
4508 __isl_give isl_basic_map
*isl_basic_map_equal(
4509 __isl_take isl_space
*dim
, unsigned n_equal
)
4512 struct isl_basic_map
*bmap
;
4513 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4516 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4517 bmap
= var_equal(bmap
, i
);
4518 return isl_basic_map_finalize(bmap
);
4521 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4523 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4527 struct isl_basic_map
*bmap
;
4528 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4531 for (i
= 0; i
< pos
&& bmap
; ++i
)
4532 bmap
= var_equal(bmap
, i
);
4534 bmap
= var_less(bmap
, pos
);
4535 return isl_basic_map_finalize(bmap
);
4538 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4540 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4541 __isl_take isl_space
*dim
, unsigned pos
)
4544 isl_basic_map
*bmap
;
4546 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4547 for (i
= 0; i
< pos
; ++i
)
4548 bmap
= var_equal(bmap
, i
);
4549 bmap
= var_less_or_equal(bmap
, pos
);
4550 return isl_basic_map_finalize(bmap
);
4553 /* Return a relation on "dim" expressing i_pos > o_pos
4555 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4559 struct isl_basic_map
*bmap
;
4560 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4563 for (i
= 0; i
< pos
&& bmap
; ++i
)
4564 bmap
= var_equal(bmap
, i
);
4566 bmap
= var_more(bmap
, pos
);
4567 return isl_basic_map_finalize(bmap
);
4570 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4572 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4573 __isl_take isl_space
*dim
, unsigned pos
)
4576 isl_basic_map
*bmap
;
4578 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4579 for (i
= 0; i
< pos
; ++i
)
4580 bmap
= var_equal(bmap
, i
);
4581 bmap
= var_more_or_equal(bmap
, pos
);
4582 return isl_basic_map_finalize(bmap
);
4585 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4586 unsigned n
, int equal
)
4588 struct isl_map
*map
;
4591 if (n
== 0 && equal
)
4592 return isl_map_universe(dims
);
4594 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4596 for (i
= 0; i
+ 1 < n
; ++i
)
4597 map
= isl_map_add_basic_map(map
,
4598 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4601 map
= isl_map_add_basic_map(map
,
4602 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4604 map
= isl_map_add_basic_map(map
,
4605 isl_basic_map_less_at(dims
, n
- 1));
4607 isl_space_free(dims
);
4612 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4616 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4619 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4621 return map_lex_lte_first(dim
, n
, 0);
4624 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4626 return map_lex_lte_first(dim
, n
, 1);
4629 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4631 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4634 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4636 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4639 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4640 unsigned n
, int equal
)
4642 struct isl_map
*map
;
4645 if (n
== 0 && equal
)
4646 return isl_map_universe(dims
);
4648 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4650 for (i
= 0; i
+ 1 < n
; ++i
)
4651 map
= isl_map_add_basic_map(map
,
4652 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4655 map
= isl_map_add_basic_map(map
,
4656 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4658 map
= isl_map_add_basic_map(map
,
4659 isl_basic_map_more_at(dims
, n
- 1));
4661 isl_space_free(dims
);
4666 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4670 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4673 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4675 return map_lex_gte_first(dim
, n
, 0);
4678 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4680 return map_lex_gte_first(dim
, n
, 1);
4683 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4685 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4688 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4690 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4693 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4694 __isl_take isl_set
*set2
)
4697 map
= isl_map_lex_le(isl_set_get_space(set1
));
4698 map
= isl_map_intersect_domain(map
, set1
);
4699 map
= isl_map_intersect_range(map
, set2
);
4703 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4704 __isl_take isl_set
*set2
)
4707 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4708 map
= isl_map_intersect_domain(map
, set1
);
4709 map
= isl_map_intersect_range(map
, set2
);
4713 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4714 __isl_take isl_set
*set2
)
4717 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4718 map
= isl_map_intersect_domain(map
, set1
);
4719 map
= isl_map_intersect_range(map
, set2
);
4723 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4724 __isl_take isl_set
*set2
)
4727 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4728 map
= isl_map_intersect_domain(map
, set1
);
4729 map
= isl_map_intersect_range(map
, set2
);
4733 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4734 __isl_take isl_map
*map2
)
4737 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4738 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4739 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4743 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4744 __isl_take isl_map
*map2
)
4747 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4748 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4749 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4753 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4754 __isl_take isl_map
*map2
)
4757 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4758 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4759 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4763 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4764 __isl_take isl_map
*map2
)
4767 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4768 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4769 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4773 /* For a div d = floor(f/m), add the constraint
4777 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4778 unsigned pos
, isl_int
*div
)
4781 unsigned total
= isl_basic_map_total_dim(bmap
);
4783 i
= isl_basic_map_alloc_inequality(bmap
);
4785 return isl_stat_error
;
4786 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4787 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4792 /* For a div d = floor(f/m), add the constraint
4794 * -(f-(m-1)) + m d >= 0
4796 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4797 unsigned pos
, isl_int
*div
)
4800 unsigned total
= isl_basic_map_total_dim(bmap
);
4802 i
= isl_basic_map_alloc_inequality(bmap
);
4804 return isl_stat_error
;
4805 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4806 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4807 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4808 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4813 /* For a div d = floor(f/m), add the constraints
4816 * -(f-(m-1)) + m d >= 0
4818 * Note that the second constraint is the negation of
4822 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4823 unsigned pos
, isl_int
*div
)
4825 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4827 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4832 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4833 unsigned pos
, isl_int
*div
)
4835 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4839 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4841 unsigned total
= isl_basic_map_total_dim(bmap
);
4842 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4844 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4848 /* For each known div d = floor(f/m), add the constraints
4851 * -(f-(m-1)) + m d >= 0
4853 * Remove duplicate constraints in case of some these div constraints
4854 * already appear in "bmap".
4856 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4857 __isl_take isl_basic_map
*bmap
)
4863 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4867 bmap
= add_known_div_constraints(bmap
);
4868 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4869 bmap
= isl_basic_map_finalize(bmap
);
4873 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4875 * In particular, if this div is of the form d = floor(f/m),
4876 * then add the constraint
4880 * if sign < 0 or the constraint
4882 * -(f-(m-1)) + m d >= 0
4886 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4887 unsigned div
, int sign
)
4895 total
= isl_basic_map_total_dim(bmap
);
4896 div_pos
= total
- bmap
->n_div
+ div
;
4899 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4901 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4904 struct isl_basic_set
*isl_basic_map_underlying_set(
4905 struct isl_basic_map
*bmap
)
4909 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4911 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4912 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4913 return bset_from_bmap(bmap
);
4914 bmap
= isl_basic_map_cow(bmap
);
4917 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4920 bmap
->extra
-= bmap
->n_div
;
4922 bmap
= isl_basic_map_finalize(bmap
);
4923 return bset_from_bmap(bmap
);
4925 isl_basic_map_free(bmap
);
4929 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4930 __isl_take isl_basic_set
*bset
)
4932 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4935 /* Replace each element in "list" by the result of applying
4936 * isl_basic_map_underlying_set to the element.
4938 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4939 __isl_take isl_basic_map_list
*list
)
4946 n
= isl_basic_map_list_n_basic_map(list
);
4947 for (i
= 0; i
< n
; ++i
) {
4948 isl_basic_map
*bmap
;
4949 isl_basic_set
*bset
;
4951 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4952 bset
= isl_basic_set_underlying_set(bmap
);
4953 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4959 struct isl_basic_map
*isl_basic_map_overlying_set(
4960 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4962 struct isl_basic_map
*bmap
;
4963 struct isl_ctx
*ctx
;
4970 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4971 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4972 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4974 if (like
->n_div
== 0) {
4975 isl_space
*space
= isl_basic_map_get_space(like
);
4976 isl_basic_map_free(like
);
4977 return isl_basic_map_reset_space(bset
, space
);
4979 bset
= isl_basic_set_cow(bset
);
4982 total
= bset
->dim
->n_out
+ bset
->extra
;
4983 bmap
= bset_to_bmap(bset
);
4984 isl_space_free(bmap
->dim
);
4985 bmap
->dim
= isl_space_copy(like
->dim
);
4988 bmap
->n_div
= like
->n_div
;
4989 bmap
->extra
+= like
->n_div
;
4993 ltotal
= total
- bmap
->extra
+ like
->extra
;
4996 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4997 bmap
->extra
* (1 + 1 + total
));
4998 if (isl_blk_is_error(bmap
->block2
))
5000 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5004 for (i
= 0; i
< bmap
->extra
; ++i
)
5005 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5006 for (i
= 0; i
< like
->n_div
; ++i
) {
5007 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5008 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5010 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5012 isl_basic_map_free(like
);
5013 bmap
= isl_basic_map_simplify(bmap
);
5014 bmap
= isl_basic_map_finalize(bmap
);
5017 isl_basic_map_free(like
);
5018 isl_basic_set_free(bset
);
5022 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5023 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5025 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5026 bset_to_bmap(like
)));
5029 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5033 map
= isl_map_cow(map
);
5036 map
->dim
= isl_space_cow(map
->dim
);
5040 for (i
= 1; i
< map
->n
; ++i
)
5041 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5043 for (i
= 0; i
< map
->n
; ++i
) {
5044 map
->p
[i
] = bset_to_bmap(
5045 isl_basic_map_underlying_set(map
->p
[i
]));
5050 map
->dim
= isl_space_underlying(map
->dim
, 0);
5052 isl_space_free(map
->dim
);
5053 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5057 return set_from_map(map
);
5063 /* Replace the space of "bmap" by "space".
5065 * If the space of "bmap" is identical to "space" (including the identifiers
5066 * of the input and output dimensions), then simply return the original input.
5068 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5069 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5072 isl_space
*bmap_space
;
5074 bmap_space
= isl_basic_map_peek_space(bmap
);
5075 equal
= isl_space_is_equal(bmap_space
, space
);
5076 if (equal
>= 0 && equal
)
5077 equal
= isl_space_match(bmap_space
, isl_dim_in
,
5079 if (equal
>= 0 && equal
)
5080 equal
= isl_space_match(bmap_space
, isl_dim_out
,
5081 space
, isl_dim_out
);
5085 isl_space_free(space
);
5088 bmap
= isl_basic_map_cow(bmap
);
5089 if (!bmap
|| !space
)
5092 isl_space_free(bmap
->dim
);
5095 bmap
= isl_basic_map_finalize(bmap
);
5099 isl_basic_map_free(bmap
);
5100 isl_space_free(space
);
5104 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5105 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5107 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5111 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5112 __isl_take isl_space
*dim
)
5116 map
= isl_map_cow(map
);
5120 for (i
= 0; i
< map
->n
; ++i
) {
5121 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5122 isl_space_copy(dim
));
5126 isl_space_free(map
->dim
);
5132 isl_space_free(dim
);
5136 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5137 __isl_take isl_space
*dim
)
5139 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5142 /* Compute the parameter domain of the given basic set.
5144 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5150 is_params
= isl_basic_set_is_params(bset
);
5152 return isl_basic_set_free(bset
);
5156 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5157 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5158 space
= isl_basic_set_get_space(bset
);
5159 space
= isl_space_params(space
);
5160 bset
= isl_basic_set_reset_space(bset
, space
);
5164 /* Construct a zero-dimensional basic set with the given parameter domain.
5166 __isl_give isl_basic_set
*isl_basic_set_from_params(
5167 __isl_take isl_basic_set
*bset
)
5170 space
= isl_basic_set_get_space(bset
);
5171 space
= isl_space_set_from_params(space
);
5172 bset
= isl_basic_set_reset_space(bset
, space
);
5176 /* Compute the parameter domain of the given set.
5178 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5183 if (isl_set_is_params(set
))
5186 n
= isl_set_dim(set
, isl_dim_set
);
5187 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5188 space
= isl_set_get_space(set
);
5189 space
= isl_space_params(space
);
5190 set
= isl_set_reset_space(set
, space
);
5194 /* Construct a zero-dimensional set with the given parameter domain.
5196 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5199 space
= isl_set_get_space(set
);
5200 space
= isl_space_set_from_params(space
);
5201 set
= isl_set_reset_space(set
, space
);
5205 /* Compute the parameter domain of the given map.
5207 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5212 n
= isl_map_dim(map
, isl_dim_in
);
5213 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5214 n
= isl_map_dim(map
, isl_dim_out
);
5215 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5216 space
= isl_map_get_space(map
);
5217 space
= isl_space_params(space
);
5218 map
= isl_map_reset_space(map
, space
);
5222 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5229 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5231 n_out
= isl_basic_map_n_out(bmap
);
5232 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5234 return isl_basic_map_reset_space(bmap
, space
);
5237 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5240 return isl_bool_error
;
5241 return isl_space_may_be_set(bmap
->dim
);
5244 /* Is this basic map actually a set?
5245 * Users should never call this function. Outside of isl,
5246 * the type should indicate whether something is a set or a map.
5248 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5251 return isl_bool_error
;
5252 return isl_space_is_set(bmap
->dim
);
5255 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5259 is_set
= isl_basic_map_is_set(bmap
);
5264 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5266 isl_basic_map_free(bmap
);
5270 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5271 __isl_take isl_basic_map
*bmap
)
5275 isl_basic_map
*domain
;
5276 int nparam
, n_in
, n_out
;
5278 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5279 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5280 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5282 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5283 domain
= isl_basic_map_universe(dim
);
5285 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5286 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5287 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5289 for (i
= 0; i
< n_in
; ++i
)
5290 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5293 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5294 return isl_basic_map_finalize(bmap
);
5297 __isl_give isl_basic_map
*isl_basic_map_range_map(
5298 __isl_take isl_basic_map
*bmap
)
5302 isl_basic_map
*range
;
5303 int nparam
, n_in
, n_out
;
5305 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5306 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5307 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5309 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5310 range
= isl_basic_map_universe(dim
);
5312 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5313 bmap
= isl_basic_map_apply_range(bmap
, range
);
5314 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5316 for (i
= 0; i
< n_out
; ++i
)
5317 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5320 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5321 return isl_basic_map_finalize(bmap
);
5324 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5328 return isl_space_may_be_set(map
->dim
);
5331 /* Is this map actually a set?
5332 * Users should never call this function. Outside of isl,
5333 * the type should indicate whether something is a set or a map.
5335 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5338 return isl_bool_error
;
5339 return isl_space_is_set(map
->dim
);
5342 struct isl_set
*isl_map_range(struct isl_map
*map
)
5346 struct isl_set
*set
;
5348 is_set
= isl_map_is_set(map
);
5352 return set_from_map(map
);
5354 map
= isl_map_cow(map
);
5358 set
= set_from_map(map
);
5359 set
->dim
= isl_space_range(set
->dim
);
5362 for (i
= 0; i
< map
->n
; ++i
) {
5363 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5367 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5368 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5375 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5379 map
= isl_map_cow(map
);
5383 map
->dim
= isl_space_domain_map(map
->dim
);
5386 for (i
= 0; i
< map
->n
; ++i
) {
5387 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5391 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5392 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5399 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5402 isl_space
*range_dim
;
5404 map
= isl_map_cow(map
);
5408 range_dim
= isl_space_range(isl_map_get_space(map
));
5409 range_dim
= isl_space_from_range(range_dim
);
5410 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5411 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5414 for (i
= 0; i
< map
->n
; ++i
) {
5415 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5419 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5420 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5427 /* Given a wrapped map of the form A[B -> C],
5428 * return the map A[B -> C] -> B.
5430 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5437 if (!isl_set_has_tuple_id(set
))
5438 return isl_map_domain_map(isl_set_unwrap(set
));
5440 id
= isl_set_get_tuple_id(set
);
5441 map
= isl_map_domain_map(isl_set_unwrap(set
));
5442 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5447 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5448 __isl_take isl_basic_set
*bset
)
5450 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5453 __isl_give isl_basic_map
*isl_basic_map_from_range(
5454 __isl_take isl_basic_set
*bset
)
5457 space
= isl_basic_set_get_space(bset
);
5458 space
= isl_space_from_range(space
);
5459 bset
= isl_basic_set_reset_space(bset
, space
);
5460 return bset_to_bmap(bset
);
5463 /* Create a relation with the given set as range.
5464 * The domain of the created relation is a zero-dimensional
5465 * flat anonymous space.
5467 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5470 space
= isl_set_get_space(set
);
5471 space
= isl_space_from_range(space
);
5472 set
= isl_set_reset_space(set
, space
);
5473 return set_to_map(set
);
5476 /* Create a relation with the given set as domain.
5477 * The range of the created relation is a zero-dimensional
5478 * flat anonymous space.
5480 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5482 return isl_map_reverse(isl_map_from_range(set
));
5485 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5486 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5488 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5491 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5492 __isl_take isl_set
*range
)
5494 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5497 /* Return a newly allocated isl_map with given space and flags and
5498 * room for "n" basic maps.
5499 * Make sure that all cached information is cleared.
5501 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5504 struct isl_map
*map
;
5509 isl_die(space
->ctx
, isl_error_internal
,
5510 "negative number of basic maps", goto error
);
5511 map
= isl_calloc(space
->ctx
, struct isl_map
,
5512 sizeof(struct isl_map
) +
5513 (n
- 1) * sizeof(struct isl_basic_map
*));
5517 map
->ctx
= space
->ctx
;
5518 isl_ctx_ref(map
->ctx
);
5526 isl_space_free(space
);
5530 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5532 struct isl_basic_map
*bmap
;
5533 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5534 bmap
= isl_basic_map_set_to_empty(bmap
);
5538 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5540 struct isl_basic_set
*bset
;
5541 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5542 bset
= isl_basic_set_set_to_empty(bset
);
5546 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5548 struct isl_basic_map
*bmap
;
5549 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5550 bmap
= isl_basic_map_finalize(bmap
);
5554 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5556 struct isl_basic_set
*bset
;
5557 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5558 bset
= isl_basic_set_finalize(bset
);
5562 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5565 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5566 isl_basic_map
*bmap
;
5568 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5569 for (i
= 0; i
< total
; ++i
) {
5570 int k
= isl_basic_map_alloc_inequality(bmap
);
5573 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5574 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5578 isl_basic_map_free(bmap
);
5582 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5584 return isl_basic_map_nat_universe(dim
);
5587 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5589 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5592 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5594 return isl_map_nat_universe(dim
);
5597 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5599 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5602 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5604 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5607 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5609 struct isl_map
*map
;
5612 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5613 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5617 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5619 struct isl_set
*set
;
5622 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5623 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5627 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5630 struct isl_map
*dup
;
5634 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5635 for (i
= 0; i
< map
->n
; ++i
)
5636 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5640 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5641 __isl_take isl_basic_map
*bmap
)
5645 if (isl_basic_map_plain_is_empty(bmap
)) {
5646 isl_basic_map_free(bmap
);
5649 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5650 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5651 map
->p
[map
->n
] = bmap
;
5653 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5659 isl_basic_map_free(bmap
);
5663 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5674 isl_ctx_deref(map
->ctx
);
5675 for (i
= 0; i
< map
->n
; ++i
)
5676 isl_basic_map_free(map
->p
[i
]);
5677 isl_space_free(map
->dim
);
5683 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5684 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5688 bmap
= isl_basic_map_cow(bmap
);
5689 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5690 j
= isl_basic_map_alloc_equality(bmap
);
5693 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5694 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5695 isl_int_set_si(bmap
->eq
[j
][0], value
);
5696 bmap
= isl_basic_map_simplify(bmap
);
5697 return isl_basic_map_finalize(bmap
);
5699 isl_basic_map_free(bmap
);
5703 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5704 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5708 bmap
= isl_basic_map_cow(bmap
);
5709 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5710 j
= isl_basic_map_alloc_equality(bmap
);
5713 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5714 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5715 isl_int_set(bmap
->eq
[j
][0], value
);
5716 bmap
= isl_basic_map_simplify(bmap
);
5717 return isl_basic_map_finalize(bmap
);
5719 isl_basic_map_free(bmap
);
5723 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5724 enum isl_dim_type type
, unsigned pos
, int value
)
5726 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5727 return isl_basic_map_free(bmap
);
5728 return isl_basic_map_fix_pos_si(bmap
,
5729 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5732 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5733 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5735 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5736 return isl_basic_map_free(bmap
);
5737 return isl_basic_map_fix_pos(bmap
,
5738 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5741 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5742 * to be equal to "v".
5744 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5745 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5749 if (!isl_val_is_int(v
))
5750 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5751 "expecting integer value", goto error
);
5752 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5754 pos
+= isl_basic_map_offset(bmap
, type
);
5755 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5759 isl_basic_map_free(bmap
);
5764 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5765 * to be equal to "v".
5767 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5768 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5770 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5773 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5774 enum isl_dim_type type
, unsigned pos
, int value
)
5776 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5780 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5781 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5783 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5787 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5788 unsigned input
, int value
)
5790 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5793 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5794 unsigned dim
, int value
)
5796 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5797 isl_dim_set
, dim
, value
));
5800 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5802 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5809 isl_basic_map_free(map
->p
[i
]);
5810 if (i
!= map
->n
- 1) {
5811 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5812 map
->p
[i
] = map
->p
[map
->n
- 1];
5819 /* Perform "fn" on each basic map of "map", where we may not be holding
5820 * the only reference to "map".
5821 * In particular, "fn" should be a semantics preserving operation
5822 * that we want to apply to all copies of "map". We therefore need
5823 * to be careful not to modify "map" in a way that breaks "map"
5824 * in case anything goes wrong.
5826 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5827 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5829 struct isl_basic_map
*bmap
;
5835 for (i
= map
->n
- 1; i
>= 0; --i
) {
5836 bmap
= isl_basic_map_copy(map
->p
[i
]);
5840 isl_basic_map_free(map
->p
[i
]);
5842 if (remove_if_empty(map
, i
) < 0)
5852 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5853 enum isl_dim_type type
, unsigned pos
, int value
)
5857 map
= isl_map_cow(map
);
5861 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5862 for (i
= map
->n
- 1; i
>= 0; --i
) {
5863 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5864 if (remove_if_empty(map
, i
) < 0)
5867 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5874 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5875 enum isl_dim_type type
, unsigned pos
, int value
)
5877 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5880 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5881 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5885 map
= isl_map_cow(map
);
5889 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5890 for (i
= 0; i
< map
->n
; ++i
) {
5891 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5895 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5902 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5903 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5905 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5908 /* Fix the value of the variable at position "pos" of type "type" of "map"
5909 * to be equal to "v".
5911 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5912 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5916 map
= isl_map_cow(map
);
5920 if (!isl_val_is_int(v
))
5921 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5922 "expecting integer value", goto error
);
5923 if (pos
>= isl_map_dim(map
, type
))
5924 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5925 "index out of bounds", goto error
);
5926 for (i
= map
->n
- 1; i
>= 0; --i
) {
5927 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5929 if (remove_if_empty(map
, i
) < 0)
5932 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5941 /* Fix the value of the variable at position "pos" of type "type" of "set"
5942 * to be equal to "v".
5944 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5945 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5947 return isl_map_fix_val(set
, type
, pos
, v
);
5950 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5951 unsigned input
, int value
)
5953 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5956 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5958 return set_from_map(isl_map_fix_si(set_to_map(set
),
5959 isl_dim_set
, dim
, value
));
5962 static __isl_give isl_basic_map
*basic_map_bound_si(
5963 __isl_take isl_basic_map
*bmap
,
5964 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5968 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5969 return isl_basic_map_free(bmap
);
5970 pos
+= isl_basic_map_offset(bmap
, type
);
5971 bmap
= isl_basic_map_cow(bmap
);
5972 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5973 j
= isl_basic_map_alloc_inequality(bmap
);
5976 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5978 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5979 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5981 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5982 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5984 bmap
= isl_basic_map_simplify(bmap
);
5985 return isl_basic_map_finalize(bmap
);
5987 isl_basic_map_free(bmap
);
5991 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5992 __isl_take isl_basic_map
*bmap
,
5993 enum isl_dim_type type
, unsigned pos
, int value
)
5995 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5998 /* Constrain the values of the given dimension to be no greater than "value".
6000 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6001 __isl_take isl_basic_map
*bmap
,
6002 enum isl_dim_type type
, unsigned pos
, int value
)
6004 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6007 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6008 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6012 map
= isl_map_cow(map
);
6016 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6017 for (i
= 0; i
< map
->n
; ++i
) {
6018 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6019 type
, pos
, value
, upper
);
6023 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6030 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6031 enum isl_dim_type type
, unsigned pos
, int value
)
6033 return map_bound_si(map
, type
, pos
, value
, 0);
6036 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6037 enum isl_dim_type type
, unsigned pos
, int value
)
6039 return map_bound_si(map
, type
, pos
, value
, 1);
6042 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6043 enum isl_dim_type type
, unsigned pos
, int value
)
6045 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6049 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6050 enum isl_dim_type type
, unsigned pos
, int value
)
6052 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6055 /* Bound the given variable of "bmap" from below (or above is "upper"
6056 * is set) to "value".
6058 static __isl_give isl_basic_map
*basic_map_bound(
6059 __isl_take isl_basic_map
*bmap
,
6060 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6064 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6065 return isl_basic_map_free(bmap
);
6066 pos
+= isl_basic_map_offset(bmap
, type
);
6067 bmap
= isl_basic_map_cow(bmap
);
6068 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6069 j
= isl_basic_map_alloc_inequality(bmap
);
6072 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6074 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6075 isl_int_set(bmap
->ineq
[j
][0], value
);
6077 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6078 isl_int_neg(bmap
->ineq
[j
][0], value
);
6080 bmap
= isl_basic_map_simplify(bmap
);
6081 return isl_basic_map_finalize(bmap
);
6083 isl_basic_map_free(bmap
);
6087 /* Bound the given variable of "map" from below (or above is "upper"
6088 * is set) to "value".
6090 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6091 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6095 map
= isl_map_cow(map
);
6099 if (pos
>= isl_map_dim(map
, type
))
6100 isl_die(map
->ctx
, isl_error_invalid
,
6101 "index out of bounds", goto error
);
6102 for (i
= map
->n
- 1; i
>= 0; --i
) {
6103 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6104 if (remove_if_empty(map
, i
) < 0)
6107 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6114 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6115 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6117 return map_bound(map
, type
, pos
, value
, 0);
6120 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6121 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6123 return map_bound(map
, type
, pos
, value
, 1);
6126 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6127 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6129 return isl_map_lower_bound(set
, type
, pos
, value
);
6132 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6133 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6135 return isl_map_upper_bound(set
, type
, pos
, value
);
6138 /* Force the values of the variable at position "pos" of type "type" of "set"
6139 * to be no smaller than "value".
6141 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6142 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6146 if (!isl_val_is_int(value
))
6147 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6148 "expecting integer value", goto error
);
6149 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6150 isl_val_free(value
);
6153 isl_val_free(value
);
6158 /* Force the values of the variable at position "pos" of type "type" of "set"
6159 * to be no greater than "value".
6161 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6162 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6166 if (!isl_val_is_int(value
))
6167 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6168 "expecting integer value", goto error
);
6169 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6170 isl_val_free(value
);
6173 isl_val_free(value
);
6178 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6182 map
= isl_map_cow(map
);
6186 map
->dim
= isl_space_reverse(map
->dim
);
6189 for (i
= 0; i
< map
->n
; ++i
) {
6190 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6194 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6202 #define TYPE isl_pw_multi_aff
6204 #define SUFFIX _pw_multi_aff
6206 #define EMPTY isl_pw_multi_aff_empty
6208 #define ADD isl_pw_multi_aff_union_add
6209 #include "isl_map_lexopt_templ.c"
6211 /* Given a map "map", compute the lexicographically minimal
6212 * (or maximal) image element for each domain element in dom,
6213 * in the form of an isl_pw_multi_aff.
6214 * If "empty" is not NULL, then set *empty to those elements in dom that
6215 * do not have an image element.
6216 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6217 * should be computed over the domain of "map". "empty" is also NULL
6220 * We first compute the lexicographically minimal or maximal element
6221 * in the first basic map. This results in a partial solution "res"
6222 * and a subset "todo" of dom that still need to be handled.
6223 * We then consider each of the remaining maps in "map" and successively
6224 * update both "res" and "todo".
6225 * If "empty" is NULL, then the todo sets are not needed and therefore
6226 * also not computed.
6228 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6229 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6230 __isl_give isl_set
**empty
, unsigned flags
)
6234 isl_pw_multi_aff
*res
;
6237 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6238 if (!map
|| (!full
&& !dom
))
6241 if (isl_map_plain_is_empty(map
)) {
6246 return isl_pw_multi_aff_from_map(map
);
6249 res
= basic_map_partial_lexopt_pw_multi_aff(
6250 isl_basic_map_copy(map
->p
[0]),
6251 isl_set_copy(dom
), empty
, flags
);
6255 for (i
= 1; i
< map
->n
; ++i
) {
6256 isl_pw_multi_aff
*res_i
;
6258 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6259 isl_basic_map_copy(map
->p
[i
]),
6260 isl_set_copy(dom
), empty
, flags
);
6262 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6263 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6265 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6268 todo
= isl_set_intersect(todo
, *empty
);
6287 #define TYPE isl_map
6291 #define EMPTY isl_map_empty
6293 #define ADD isl_map_union_disjoint
6294 #include "isl_map_lexopt_templ.c"
6296 /* Given a map "map", compute the lexicographically minimal
6297 * (or maximal) image element for each domain element in "dom",
6298 * in the form of an isl_map.
6299 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6300 * do not have an image element.
6301 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6302 * should be computed over the domain of "map". "empty" is also NULL
6305 * If the input consists of more than one disjunct, then first
6306 * compute the desired result in the form of an isl_pw_multi_aff and
6307 * then convert that into an isl_map.
6309 * This function used to have an explicit implementation in terms
6310 * of isl_maps, but it would continually intersect the domains of
6311 * partial results with the complement of the domain of the next
6312 * partial solution, potentially leading to an explosion in the number
6313 * of disjuncts if there are several disjuncts in the input.
6314 * An even earlier implementation of this function would look for
6315 * better results in the domain of the partial result and for extra
6316 * results in the complement of this domain, which would lead to
6317 * even more splintering.
6319 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6320 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6321 __isl_give isl_set
**empty
, unsigned flags
)
6324 struct isl_map
*res
;
6325 isl_pw_multi_aff
*pma
;
6327 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6328 if (!map
|| (!full
&& !dom
))
6331 if (isl_map_plain_is_empty(map
)) {
6340 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6346 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6348 return isl_map_from_pw_multi_aff(pma
);
6357 __isl_give isl_map
*isl_map_partial_lexmax(
6358 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6359 __isl_give isl_set
**empty
)
6361 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6364 __isl_give isl_map
*isl_map_partial_lexmin(
6365 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6366 __isl_give isl_set
**empty
)
6368 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6371 __isl_give isl_set
*isl_set_partial_lexmin(
6372 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6373 __isl_give isl_set
**empty
)
6375 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6379 __isl_give isl_set
*isl_set_partial_lexmax(
6380 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6381 __isl_give isl_set
**empty
)
6383 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6387 /* Compute the lexicographic minimum (or maximum if "flags" includes
6388 * ISL_OPT_MAX) of "bset" over its parametric domain.
6390 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6393 return isl_basic_map_lexopt(bset
, flags
);
6396 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6398 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6401 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6403 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6406 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6408 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6411 /* Compute the lexicographic minimum of "bset" over its parametric domain
6412 * for the purpose of quantifier elimination.
6413 * That is, find an explicit representation for all the existentially
6414 * quantified variables in "bset" by computing their lexicographic
6417 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6418 __isl_take isl_basic_set
*bset
)
6420 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6423 /* Extract the first and only affine expression from list
6424 * and then add it to *pwaff with the given dom.
6425 * This domain is known to be disjoint from other domains
6426 * because of the way isl_basic_map_foreach_lexmax works.
6428 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6429 __isl_take isl_aff_list
*list
, void *user
)
6431 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6433 isl_pw_aff
**pwaff
= user
;
6434 isl_pw_aff
*pwaff_i
;
6438 if (isl_aff_list_n_aff(list
) != 1)
6439 isl_die(ctx
, isl_error_internal
,
6440 "expecting single element list", goto error
);
6442 aff
= isl_aff_list_get_aff(list
, 0);
6443 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6445 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6447 isl_aff_list_free(list
);
6451 isl_basic_set_free(dom
);
6452 isl_aff_list_free(list
);
6453 return isl_stat_error
;
6456 /* Given a basic map with one output dimension, compute the minimum or
6457 * maximum of that dimension as an isl_pw_aff.
6459 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6460 * call update_dim_opt on each leaf of the result.
6462 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6465 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6469 dim
= isl_space_from_domain(isl_space_domain(dim
));
6470 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6471 pwaff
= isl_pw_aff_empty(dim
);
6473 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6475 return isl_pw_aff_free(pwaff
);
6480 /* Compute the minimum or maximum of the given output dimension
6481 * as a function of the parameters and the input dimensions,
6482 * but independently of the other output dimensions.
6484 * We first project out the other output dimension and then compute
6485 * the "lexicographic" maximum in each basic map, combining the results
6486 * using isl_pw_aff_union_max.
6488 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6495 n_out
= isl_map_dim(map
, isl_dim_out
);
6496 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6497 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6502 isl_space
*dim
= isl_map_get_space(map
);
6504 return isl_pw_aff_empty(dim
);
6507 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6508 for (i
= 1; i
< map
->n
; ++i
) {
6509 isl_pw_aff
*pwaff_i
;
6511 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6512 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6520 /* Compute the minimum of the given output dimension as a function of the
6521 * parameters and input dimensions, but independently of
6522 * the other output dimensions.
6524 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6526 return map_dim_opt(map
, pos
, 0);
6529 /* Compute the maximum of the given output dimension as a function of the
6530 * parameters and input dimensions, but independently of
6531 * the other output dimensions.
6533 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6535 return map_dim_opt(map
, pos
, 1);
6538 /* Compute the minimum or maximum of the given set dimension
6539 * as a function of the parameters,
6540 * but independently of the other set dimensions.
6542 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6545 return map_dim_opt(set
, pos
, max
);
6548 /* Compute the maximum of the given set dimension as a function of the
6549 * parameters, but independently of the other set dimensions.
6551 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6553 return set_dim_opt(set
, pos
, 1);
6556 /* Compute the minimum of the given set dimension as a function of the
6557 * parameters, but independently of the other set dimensions.
6559 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6561 return set_dim_opt(set
, pos
, 0);
6564 /* Apply a preimage specified by "mat" on the parameters of "bset".
6565 * bset is assumed to have only parameters and divs.
6567 static struct isl_basic_set
*basic_set_parameter_preimage(
6568 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6575 bset
->dim
= isl_space_cow(bset
->dim
);
6579 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6581 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6583 bset
->dim
->nparam
= 0;
6584 bset
->dim
->n_out
= nparam
;
6585 bset
= isl_basic_set_preimage(bset
, mat
);
6587 bset
->dim
->nparam
= bset
->dim
->n_out
;
6588 bset
->dim
->n_out
= 0;
6593 isl_basic_set_free(bset
);
6597 /* Apply a preimage specified by "mat" on the parameters of "set".
6598 * set is assumed to have only parameters and divs.
6600 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6601 __isl_take isl_mat
*mat
)
6609 nparam
= isl_set_dim(set
, isl_dim_param
);
6611 if (mat
->n_row
!= 1 + nparam
)
6612 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6613 "unexpected number of rows", goto error
);
6615 space
= isl_set_get_space(set
);
6616 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6617 isl_dim_param
, 0, nparam
);
6618 set
= isl_set_reset_space(set
, space
);
6619 set
= isl_set_preimage(set
, mat
);
6620 nparam
= isl_set_dim(set
, isl_dim_out
);
6621 space
= isl_set_get_space(set
);
6622 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6623 isl_dim_out
, 0, nparam
);
6624 set
= isl_set_reset_space(set
, space
);
6632 /* Intersect the basic set "bset" with the affine space specified by the
6633 * equalities in "eq".
6635 static struct isl_basic_set
*basic_set_append_equalities(
6636 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6644 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6649 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6650 for (i
= 0; i
< eq
->n_row
; ++i
) {
6651 k
= isl_basic_set_alloc_equality(bset
);
6654 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6655 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6659 bset
= isl_basic_set_gauss(bset
, NULL
);
6660 bset
= isl_basic_set_finalize(bset
);
6665 isl_basic_set_free(bset
);
6669 /* Intersect the set "set" with the affine space specified by the
6670 * equalities in "eq".
6672 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6680 for (i
= 0; i
< set
->n
; ++i
) {
6681 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6694 /* Given a basic set "bset" that only involves parameters and existentially
6695 * quantified variables, return the index of the first equality
6696 * that only involves parameters. If there is no such equality then
6697 * return bset->n_eq.
6699 * This function assumes that isl_basic_set_gauss has been called on "bset".
6701 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6704 unsigned nparam
, n_div
;
6709 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6710 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6712 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6713 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6720 /* Compute an explicit representation for the existentially quantified
6721 * variables in "bset" by computing the "minimal value" of the set
6722 * variables. Since there are no set variables, the computation of
6723 * the minimal value essentially computes an explicit representation
6724 * of the non-empty part(s) of "bset".
6726 * The input only involves parameters and existentially quantified variables.
6727 * All equalities among parameters have been removed.
6729 * Since the existentially quantified variables in the result are in general
6730 * going to be different from those in the input, we first replace
6731 * them by the minimal number of variables based on their equalities.
6732 * This should simplify the parametric integer programming.
6734 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6736 isl_morph
*morph1
, *morph2
;
6742 if (bset
->n_eq
== 0)
6743 return isl_basic_set_lexmin_compute_divs(bset
);
6745 morph1
= isl_basic_set_parameter_compression(bset
);
6746 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6747 bset
= isl_basic_set_lift(bset
);
6748 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6749 bset
= isl_morph_basic_set(morph2
, bset
);
6750 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6751 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6753 set
= isl_basic_set_lexmin_compute_divs(bset
);
6755 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6760 /* Project the given basic set onto its parameter domain, possibly introducing
6761 * new, explicit, existential variables in the constraints.
6762 * The input has parameters and (possibly implicit) existential variables.
6763 * The output has the same parameters, but only
6764 * explicit existentially quantified variables.
6766 * The actual projection is performed by pip, but pip doesn't seem
6767 * to like equalities very much, so we first remove the equalities
6768 * among the parameters by performing a variable compression on
6769 * the parameters. Afterward, an inverse transformation is performed
6770 * and the equalities among the parameters are inserted back in.
6772 * The variable compression on the parameters may uncover additional
6773 * equalities that were only implicit before. We therefore check
6774 * if there are any new parameter equalities in the result and
6775 * if so recurse. The removal of parameter equalities is required
6776 * for the parameter compression performed by base_compute_divs.
6778 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6782 struct isl_mat
*T
, *T2
;
6783 struct isl_set
*set
;
6786 bset
= isl_basic_set_cow(bset
);
6790 if (bset
->n_eq
== 0)
6791 return base_compute_divs(bset
);
6793 bset
= isl_basic_set_gauss(bset
, NULL
);
6796 if (isl_basic_set_plain_is_empty(bset
))
6797 return isl_set_from_basic_set(bset
);
6799 i
= first_parameter_equality(bset
);
6800 if (i
== bset
->n_eq
)
6801 return base_compute_divs(bset
);
6803 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6804 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6806 eq
= isl_mat_cow(eq
);
6807 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6808 if (T
&& T
->n_col
== 0) {
6812 bset
= isl_basic_set_set_to_empty(bset
);
6813 return isl_set_from_basic_set(bset
);
6815 bset
= basic_set_parameter_preimage(bset
, T
);
6817 i
= first_parameter_equality(bset
);
6820 else if (i
== bset
->n_eq
)
6821 set
= base_compute_divs(bset
);
6823 set
= parameter_compute_divs(bset
);
6824 set
= set_parameter_preimage(set
, T2
);
6825 set
= set_append_equalities(set
, eq
);
6829 /* Insert the divs from "ls" before those of "bmap".
6831 * The number of columns is not changed, which means that the last
6832 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6833 * The caller is responsible for removing the same number of dimensions
6834 * from the space of "bmap".
6836 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6837 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6843 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6847 old_n_div
= bmap
->n_div
;
6848 bmap
= insert_div_rows(bmap
, n_div
);
6852 for (i
= 0; i
< n_div
; ++i
) {
6853 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6854 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6860 /* Replace the space of "bmap" by the space and divs of "ls".
6862 * If "ls" has any divs, then we simplify the result since we may
6863 * have discovered some additional equalities that could simplify
6864 * the div expressions.
6866 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6867 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6871 bmap
= isl_basic_map_cow(bmap
);
6875 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6876 bmap
= insert_divs_from_local_space(bmap
, ls
);
6880 isl_space_free(bmap
->dim
);
6881 bmap
->dim
= isl_local_space_get_space(ls
);
6885 isl_local_space_free(ls
);
6887 bmap
= isl_basic_map_simplify(bmap
);
6888 bmap
= isl_basic_map_finalize(bmap
);
6891 isl_basic_map_free(bmap
);
6892 isl_local_space_free(ls
);
6896 /* Replace the space of "map" by the space and divs of "ls".
6898 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6899 __isl_take isl_local_space
*ls
)
6903 map
= isl_map_cow(map
);
6907 for (i
= 0; i
< map
->n
; ++i
) {
6908 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6909 isl_local_space_copy(ls
));
6913 isl_space_free(map
->dim
);
6914 map
->dim
= isl_local_space_get_space(ls
);
6918 isl_local_space_free(ls
);
6921 isl_local_space_free(ls
);
6926 /* Compute an explicit representation for the existentially
6927 * quantified variables for which do not know any explicit representation yet.
6929 * We first sort the existentially quantified variables so that the
6930 * existentially quantified variables for which we already have an explicit
6931 * representation are placed before those for which we do not.
6932 * The input dimensions, the output dimensions and the existentially
6933 * quantified variables for which we already have an explicit
6934 * representation are then turned into parameters.
6935 * compute_divs returns a map with the same parameters and
6936 * no input or output dimensions and the dimension specification
6937 * is reset to that of the input, including the existentially quantified
6938 * variables for which we already had an explicit representation.
6940 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6942 struct isl_basic_set
*bset
;
6943 struct isl_set
*set
;
6944 struct isl_map
*map
;
6946 isl_local_space
*ls
;
6953 bmap
= isl_basic_map_sort_divs(bmap
);
6954 bmap
= isl_basic_map_cow(bmap
);
6958 n_known
= isl_basic_map_first_unknown_div(bmap
);
6960 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6962 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6963 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6964 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6965 dim
= isl_space_set_alloc(bmap
->ctx
,
6966 nparam
+ n_in
+ n_out
+ n_known
, 0);
6970 ls
= isl_basic_map_get_local_space(bmap
);
6971 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6972 n_known
, bmap
->n_div
- n_known
);
6974 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6975 swap_div(bmap
, i
- n_known
, i
);
6976 bmap
->n_div
-= n_known
;
6977 bmap
->extra
-= n_known
;
6979 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6980 bset
= bset_from_bmap(bmap
);
6982 set
= parameter_compute_divs(bset
);
6983 map
= set_to_map(set
);
6984 map
= replace_space_by_local_space(map
, ls
);
6988 isl_basic_map_free(bmap
);
6992 /* Remove the explicit representation of local variable "div",
6995 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6996 __isl_take isl_basic_map
*bmap
, int div
)
7000 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7002 return isl_basic_map_free(bmap
);
7006 bmap
= isl_basic_map_cow(bmap
);
7009 isl_int_set_si(bmap
->div
[div
][0], 0);
7013 /* Is local variable "div" of "bmap" marked as not having an explicit
7015 * Note that even if "div" is not marked in this way and therefore
7016 * has an explicit representation, this representation may still
7017 * depend (indirectly) on other local variables that do not
7018 * have an explicit representation.
7020 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7023 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7024 return isl_bool_error
;
7025 return isl_int_is_zero(bmap
->div
[div
][0]);
7028 /* Return the position of the first local variable that does not
7029 * have an explicit representation.
7030 * Return the total number of local variables if they all have
7031 * an explicit representation.
7032 * Return -1 on error.
7034 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7041 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7042 if (!isl_basic_map_div_is_known(bmap
, i
))
7048 /* Return the position of the first local variable that does not
7049 * have an explicit representation.
7050 * Return the total number of local variables if they all have
7051 * an explicit representation.
7052 * Return -1 on error.
7054 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7056 return isl_basic_map_first_unknown_div(bset
);
7059 /* Does "bmap" have an explicit representation for all local variables?
7061 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7065 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7066 first
= isl_basic_map_first_unknown_div(bmap
);
7068 return isl_bool_error
;
7072 /* Do all basic maps in "map" have an explicit representation
7073 * for all local variables?
7075 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7080 return isl_bool_error
;
7082 for (i
= 0; i
< map
->n
; ++i
) {
7083 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7088 return isl_bool_true
;
7091 /* If bmap contains any unknown divs, then compute explicit
7092 * expressions for them. However, this computation may be
7093 * quite expensive, so first try to remove divs that aren't
7096 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7099 struct isl_map
*map
;
7101 known
= isl_basic_map_divs_known(bmap
);
7105 return isl_map_from_basic_map(bmap
);
7107 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7109 known
= isl_basic_map_divs_known(bmap
);
7113 return isl_map_from_basic_map(bmap
);
7115 map
= compute_divs(bmap
);
7118 isl_basic_map_free(bmap
);
7122 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7126 struct isl_map
*res
;
7133 known
= isl_map_divs_known(map
);
7141 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7142 for (i
= 1 ; i
< map
->n
; ++i
) {
7144 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7145 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7146 res
= isl_map_union_disjoint(res
, r2
);
7148 res
= isl_map_union(res
, r2
);
7155 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7157 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7160 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7162 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7165 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7168 struct isl_set
*set
;
7173 map
= isl_map_cow(map
);
7177 set
= set_from_map(map
);
7178 set
->dim
= isl_space_domain(set
->dim
);
7181 for (i
= 0; i
< map
->n
; ++i
) {
7182 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7186 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7187 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7194 /* Return the union of "map1" and "map2", where we assume for now that
7195 * "map1" and "map2" are disjoint. Note that the basic maps inside
7196 * "map1" or "map2" may not be disjoint from each other.
7197 * Also note that this function is also called from isl_map_union,
7198 * which takes care of handling the situation where "map1" and "map2"
7199 * may not be disjoint.
7201 * If one of the inputs is empty, we can simply return the other input.
7202 * Similarly, if one of the inputs is universal, then it is equal to the union.
7204 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7205 __isl_take isl_map
*map2
)
7209 struct isl_map
*map
= NULL
;
7215 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7216 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7217 "spaces don't match", goto error
);
7228 is_universe
= isl_map_plain_is_universe(map1
);
7229 if (is_universe
< 0)
7236 is_universe
= isl_map_plain_is_universe(map2
);
7237 if (is_universe
< 0)
7244 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7245 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7246 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7248 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7249 map1
->n
+ map2
->n
, flags
);
7252 for (i
= 0; i
< map1
->n
; ++i
) {
7253 map
= isl_map_add_basic_map(map
,
7254 isl_basic_map_copy(map1
->p
[i
]));
7258 for (i
= 0; i
< map2
->n
; ++i
) {
7259 map
= isl_map_add_basic_map(map
,
7260 isl_basic_map_copy(map2
->p
[i
]));
7274 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7275 * guaranteed to be disjoint by the caller.
7277 * Note that this functions is called from within isl_map_make_disjoint,
7278 * so we have to be careful not to touch the constraints of the inputs
7281 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7282 __isl_take isl_map
*map2
)
7284 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7287 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7288 * not be disjoint. The parameters are assumed to have been aligned.
7290 * We currently simply call map_union_disjoint, the internal operation
7291 * of which does not really depend on the inputs being disjoint.
7292 * If the result contains more than one basic map, then we clear
7293 * the disjoint flag since the result may contain basic maps from
7294 * both inputs and these are not guaranteed to be disjoint.
7296 * As a special case, if "map1" and "map2" are obviously equal,
7297 * then we simply return "map1".
7299 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7300 __isl_take isl_map
*map2
)
7307 equal
= isl_map_plain_is_equal(map1
, map2
);
7315 map1
= map_union_disjoint(map1
, map2
);
7319 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7327 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7330 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7331 __isl_take isl_map
*map2
)
7333 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7336 struct isl_set
*isl_set_union_disjoint(
7337 struct isl_set
*set1
, struct isl_set
*set2
)
7339 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7343 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7345 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7348 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7351 * "map" and "set" are assumed to be compatible and non-NULL.
7353 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7354 __isl_take isl_set
*set
,
7355 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7356 __isl_take isl_basic_set
*bset
))
7359 struct isl_map
*result
;
7362 if (isl_set_plain_is_universe(set
)) {
7367 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7368 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7369 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7371 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7372 map
->n
* set
->n
, flags
);
7373 for (i
= 0; result
&& i
< map
->n
; ++i
)
7374 for (j
= 0; j
< set
->n
; ++j
) {
7375 result
= isl_map_add_basic_map(result
,
7376 fn(isl_basic_map_copy(map
->p
[i
]),
7377 isl_basic_set_copy(set
->p
[j
])));
7387 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7388 __isl_take isl_set
*set
)
7392 ok
= isl_map_compatible_range(map
, set
);
7396 isl_die(set
->ctx
, isl_error_invalid
,
7397 "incompatible spaces", goto error
);
7399 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7406 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7407 __isl_take isl_set
*set
)
7409 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7412 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7413 __isl_take isl_set
*set
)
7417 ok
= isl_map_compatible_domain(map
, set
);
7421 isl_die(set
->ctx
, isl_error_invalid
,
7422 "incompatible spaces", goto error
);
7424 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7431 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7432 __isl_take isl_set
*set
)
7434 return isl_map_align_params_map_map_and(map
, set
,
7435 &map_intersect_domain
);
7438 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7439 __isl_take isl_map
*map2
)
7443 map1
= isl_map_reverse(map1
);
7444 map1
= isl_map_apply_range(map1
, map2
);
7445 return isl_map_reverse(map1
);
7452 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7453 __isl_take isl_map
*map2
)
7455 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7458 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7459 __isl_take isl_map
*map2
)
7461 isl_space
*dim_result
;
7462 struct isl_map
*result
;
7468 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7469 isl_space_copy(map2
->dim
));
7471 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7474 for (i
= 0; i
< map1
->n
; ++i
)
7475 for (j
= 0; j
< map2
->n
; ++j
) {
7476 result
= isl_map_add_basic_map(result
,
7477 isl_basic_map_apply_range(
7478 isl_basic_map_copy(map1
->p
[i
]),
7479 isl_basic_map_copy(map2
->p
[j
])));
7485 if (result
&& result
->n
<= 1)
7486 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7494 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7495 __isl_take isl_map
*map2
)
7497 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7501 * returns range - domain
7503 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7505 isl_space
*target_space
;
7506 struct isl_basic_set
*bset
;
7513 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7514 bmap
->dim
, isl_dim_out
),
7516 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7517 dim
= isl_basic_map_n_in(bmap
);
7518 nparam
= isl_basic_map_n_param(bmap
);
7519 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7520 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7521 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7522 for (i
= 0; i
< dim
; ++i
) {
7523 int j
= isl_basic_map_alloc_equality(bmap
);
7525 bmap
= isl_basic_map_free(bmap
);
7528 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7529 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7530 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7531 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7533 bset
= isl_basic_map_domain(bmap
);
7534 bset
= isl_basic_set_reset_space(bset
, target_space
);
7537 isl_basic_map_free(bmap
);
7542 * returns range - domain
7544 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7548 struct isl_set
*result
;
7553 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7554 map
->dim
, isl_dim_out
),
7556 dim
= isl_map_get_space(map
);
7557 dim
= isl_space_domain(dim
);
7558 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7561 for (i
= 0; i
< map
->n
; ++i
)
7562 result
= isl_set_add_basic_set(result
,
7563 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7572 * returns [domain -> range] -> range - domain
7574 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7575 __isl_take isl_basic_map
*bmap
)
7579 isl_basic_map
*domain
;
7583 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7584 bmap
->dim
, isl_dim_out
))
7585 isl_die(bmap
->ctx
, isl_error_invalid
,
7586 "domain and range don't match", goto error
);
7588 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7589 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7591 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7592 domain
= isl_basic_map_universe(dim
);
7594 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7595 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7596 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7598 total
= isl_basic_map_total_dim(bmap
);
7600 for (i
= 0; i
< n
; ++i
) {
7601 k
= isl_basic_map_alloc_equality(bmap
);
7604 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7605 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7606 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7607 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7610 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7611 return isl_basic_map_finalize(bmap
);
7613 isl_basic_map_free(bmap
);
7618 * returns [domain -> range] -> range - domain
7620 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7623 isl_space
*domain_dim
;
7628 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7629 map
->dim
, isl_dim_out
))
7630 isl_die(map
->ctx
, isl_error_invalid
,
7631 "domain and range don't match", goto error
);
7633 map
= isl_map_cow(map
);
7637 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7638 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7639 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7642 for (i
= 0; i
< map
->n
; ++i
) {
7643 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7647 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7654 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7656 struct isl_basic_map
*bmap
;
7664 nparam
= dims
->nparam
;
7666 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7670 for (i
= 0; i
< dim
; ++i
) {
7671 int j
= isl_basic_map_alloc_equality(bmap
);
7674 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7675 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7676 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7678 return isl_basic_map_finalize(bmap
);
7680 isl_basic_map_free(bmap
);
7684 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7688 if (dim
->n_in
!= dim
->n_out
)
7689 isl_die(dim
->ctx
, isl_error_invalid
,
7690 "number of input and output dimensions needs to be "
7691 "the same", goto error
);
7692 return basic_map_identity(dim
);
7694 isl_space_free(dim
);
7698 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7700 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7703 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7705 isl_space
*dim
= isl_set_get_space(set
);
7707 id
= isl_map_identity(isl_space_map_from_set(dim
));
7708 return isl_map_intersect_range(id
, set
);
7711 /* Construct a basic set with all set dimensions having only non-negative
7714 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7715 __isl_take isl_space
*space
)
7720 struct isl_basic_set
*bset
;
7724 nparam
= space
->nparam
;
7726 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7729 for (i
= 0; i
< dim
; ++i
) {
7730 int k
= isl_basic_set_alloc_inequality(bset
);
7733 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7734 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7738 isl_basic_set_free(bset
);
7742 /* Construct the half-space x_pos >= 0.
7744 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7748 isl_basic_set
*nonneg
;
7750 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7751 k
= isl_basic_set_alloc_inequality(nonneg
);
7754 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7755 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7757 return isl_basic_set_finalize(nonneg
);
7759 isl_basic_set_free(nonneg
);
7763 /* Construct the half-space x_pos <= -1.
7765 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7770 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7771 k
= isl_basic_set_alloc_inequality(neg
);
7774 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7775 isl_int_set_si(neg
->ineq
[k
][0], -1);
7776 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7778 return isl_basic_set_finalize(neg
);
7780 isl_basic_set_free(neg
);
7784 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7785 enum isl_dim_type type
, unsigned first
, unsigned n
)
7789 isl_basic_set
*nonneg
;
7797 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7799 offset
= pos(set
->dim
, type
);
7800 for (i
= 0; i
< n
; ++i
) {
7801 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7802 offset
+ first
+ i
);
7803 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7805 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7814 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7816 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7822 return isl_stat_error
;
7823 if (isl_set_plain_is_empty(set
)) {
7828 return fn(set
, signs
, user
);
7831 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7833 half
= isl_set_intersect(half
, isl_set_copy(set
));
7834 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7838 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7840 half
= isl_set_intersect(half
, set
);
7841 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7844 return isl_stat_error
;
7847 /* Call "fn" on the intersections of "set" with each of the orthants
7848 * (except for obviously empty intersections). The orthant is identified
7849 * by the signs array, with each entry having value 1 or -1 according
7850 * to the sign of the corresponding variable.
7852 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7853 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7862 return isl_stat_error
;
7863 if (isl_set_plain_is_empty(set
))
7866 nparam
= isl_set_dim(set
, isl_dim_param
);
7867 nvar
= isl_set_dim(set
, isl_dim_set
);
7869 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7871 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7879 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7881 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7884 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7885 __isl_keep isl_basic_map
*bmap2
)
7888 struct isl_map
*map1
;
7889 struct isl_map
*map2
;
7891 if (!bmap1
|| !bmap2
)
7892 return isl_bool_error
;
7894 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7895 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7897 is_subset
= isl_map_is_subset(map1
, map2
);
7905 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7906 __isl_keep isl_basic_set
*bset2
)
7908 return isl_basic_map_is_subset(bset1
, bset2
);
7911 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7912 __isl_keep isl_basic_map
*bmap2
)
7916 if (!bmap1
|| !bmap2
)
7917 return isl_bool_error
;
7918 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7919 if (is_subset
!= isl_bool_true
)
7921 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7925 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7926 __isl_keep isl_basic_set
*bset2
)
7928 return isl_basic_map_is_equal(
7929 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7932 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7938 return isl_bool_error
;
7939 for (i
= 0; i
< map
->n
; ++i
) {
7940 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7942 return isl_bool_error
;
7944 return isl_bool_false
;
7946 return isl_bool_true
;
7949 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7951 return map
? map
->n
== 0 : isl_bool_error
;
7954 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7956 return set
? set
->n
== 0 : isl_bool_error
;
7959 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7961 return isl_map_is_empty(set_to_map(set
));
7964 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
7965 __isl_keep isl_map
*map2
)
7968 return isl_bool_error
;
7970 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7973 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
7974 __isl_keep isl_set
*set2
)
7977 return isl_bool_error
;
7979 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7982 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7987 return isl_bool_error
;
7988 is_subset
= isl_map_is_subset(map1
, map2
);
7989 if (is_subset
!= isl_bool_true
)
7991 is_subset
= isl_map_is_subset(map2
, map1
);
7995 /* Is "map1" equal to "map2"?
7997 * First check if they are obviously equal.
7998 * If not, then perform a more detailed analysis.
8000 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8004 equal
= isl_map_plain_is_equal(map1
, map2
);
8005 if (equal
< 0 || equal
)
8007 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8010 isl_bool
isl_basic_map_is_strict_subset(
8011 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8015 if (!bmap1
|| !bmap2
)
8016 return isl_bool_error
;
8017 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8018 if (is_subset
!= isl_bool_true
)
8020 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8021 if (is_subset
== isl_bool_error
)
8026 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8027 __isl_keep isl_map
*map2
)
8032 return isl_bool_error
;
8033 is_subset
= isl_map_is_subset(map1
, map2
);
8034 if (is_subset
!= isl_bool_true
)
8036 is_subset
= isl_map_is_subset(map2
, map1
);
8037 if (is_subset
== isl_bool_error
)
8042 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8043 __isl_keep isl_set
*set2
)
8045 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8048 /* Is "bmap" obviously equal to the universe with the same space?
8050 * That is, does it not have any constraints?
8052 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8055 return isl_bool_error
;
8056 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8059 /* Is "bset" obviously equal to the universe with the same space?
8061 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8063 return isl_basic_map_plain_is_universe(bset
);
8066 /* If "c" does not involve any existentially quantified variables,
8067 * then set *univ to false and abort
8069 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8071 isl_bool
*univ
= user
;
8074 n
= isl_constraint_dim(c
, isl_dim_div
);
8075 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8076 isl_constraint_free(c
);
8077 if (*univ
< 0 || !*univ
)
8078 return isl_stat_error
;
8082 /* Is "bmap" equal to the universe with the same space?
8084 * First check if it is obviously equal to the universe.
8085 * If not and if there are any constraints not involving
8086 * existentially quantified variables, then it is certainly
8087 * not equal to the universe.
8088 * Otherwise, check if the universe is a subset of "bmap".
8090 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8093 isl_basic_map
*test
;
8095 univ
= isl_basic_map_plain_is_universe(bmap
);
8096 if (univ
< 0 || univ
)
8098 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8099 return isl_bool_false
;
8100 univ
= isl_bool_true
;
8101 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8103 return isl_bool_error
;
8104 if (univ
< 0 || !univ
)
8106 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8107 univ
= isl_basic_map_is_subset(test
, bmap
);
8108 isl_basic_map_free(test
);
8112 /* Is "bset" equal to the universe with the same space?
8114 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8116 return isl_basic_map_is_universe(bset
);
8119 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8124 return isl_bool_error
;
8126 for (i
= 0; i
< map
->n
; ++i
) {
8127 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8132 return isl_bool_false
;
8135 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8137 return isl_map_plain_is_universe(set_to_map(set
));
8140 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8142 struct isl_basic_set
*bset
= NULL
;
8143 struct isl_vec
*sample
= NULL
;
8144 isl_bool empty
, non_empty
;
8147 return isl_bool_error
;
8149 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8150 return isl_bool_true
;
8152 if (isl_basic_map_plain_is_universe(bmap
))
8153 return isl_bool_false
;
8155 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8156 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8157 copy
= isl_basic_map_remove_redundancies(copy
);
8158 empty
= isl_basic_map_plain_is_empty(copy
);
8159 isl_basic_map_free(copy
);
8163 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8165 return isl_bool_error
;
8167 return isl_bool_false
;
8168 isl_vec_free(bmap
->sample
);
8169 bmap
->sample
= NULL
;
8170 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8172 return isl_bool_error
;
8173 sample
= isl_basic_set_sample_vec(bset
);
8175 return isl_bool_error
;
8176 empty
= sample
->size
== 0;
8177 isl_vec_free(bmap
->sample
);
8178 bmap
->sample
= sample
;
8180 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8185 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8188 return isl_bool_error
;
8189 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8192 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8195 return isl_bool_error
;
8196 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8199 /* Is "bmap" known to be non-empty?
8201 * That is, is the cached sample still valid?
8203 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8208 return isl_bool_error
;
8210 return isl_bool_false
;
8211 total
= 1 + isl_basic_map_total_dim(bmap
);
8212 if (bmap
->sample
->size
!= total
)
8213 return isl_bool_false
;
8214 return isl_basic_map_contains(bmap
, bmap
->sample
);
8217 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8219 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8222 struct isl_map
*isl_basic_map_union(
8223 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8225 struct isl_map
*map
;
8226 if (!bmap1
|| !bmap2
)
8229 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8231 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8234 map
= isl_map_add_basic_map(map
, bmap1
);
8235 map
= isl_map_add_basic_map(map
, bmap2
);
8238 isl_basic_map_free(bmap1
);
8239 isl_basic_map_free(bmap2
);
8243 struct isl_set
*isl_basic_set_union(
8244 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8246 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8247 bset_to_bmap(bset2
)));
8250 /* Order divs such that any div only depends on previous divs */
8251 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8259 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8261 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8263 if (isl_int_is_zero(bmap
->div
[i
][0]))
8265 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8270 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8271 "integer division depends on itself",
8272 return isl_basic_map_free(bmap
));
8273 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8279 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8281 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8284 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8291 for (i
= 0; i
< map
->n
; ++i
) {
8292 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8303 /* Sort the local variables of "bset".
8305 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8306 __isl_take isl_basic_set
*bset
)
8308 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8311 /* Apply the expansion computed by isl_merge_divs.
8312 * The expansion itself is given by "exp" while the resulting
8313 * list of divs is given by "div".
8315 * Move the integer divisions of "bmap" into the right position
8316 * according to "exp" and then introduce the additional integer
8317 * divisions, adding div constraints.
8318 * The moving should be done first to avoid moving coefficients
8319 * in the definitions of the extra integer divisions.
8321 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8322 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8327 bmap
= isl_basic_map_cow(bmap
);
8331 if (div
->n_row
< bmap
->n_div
)
8332 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8333 "not an expansion", goto error
);
8335 n_div
= bmap
->n_div
;
8336 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8337 div
->n_row
- n_div
, 0,
8338 2 * (div
->n_row
- n_div
));
8340 for (i
= n_div
; i
< div
->n_row
; ++i
)
8341 if (isl_basic_map_alloc_div(bmap
) < 0)
8344 for (j
= n_div
- 1; j
>= 0; --j
) {
8347 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8350 for (i
= 0; i
< div
->n_row
; ++i
) {
8351 if (j
< n_div
&& exp
[j
] == i
) {
8354 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8355 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8357 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8365 isl_basic_map_free(bmap
);
8370 /* Apply the expansion computed by isl_merge_divs.
8371 * The expansion itself is given by "exp" while the resulting
8372 * list of divs is given by "div".
8374 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8375 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8377 return isl_basic_map_expand_divs(bset
, div
, exp
);
8380 /* Look for a div in dst that corresponds to the div "div" in src.
8381 * The divs before "div" in src and dst are assumed to be the same.
8383 * Returns -1 if no corresponding div was found and the position
8384 * of the corresponding div in dst otherwise.
8386 static int find_div(struct isl_basic_map
*dst
,
8387 struct isl_basic_map
*src
, unsigned div
)
8391 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8393 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8394 for (i
= div
; i
< dst
->n_div
; ++i
)
8395 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8396 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8397 dst
->n_div
- div
) == -1)
8402 /* Align the divs of "dst" to those of "src", adding divs from "src"
8403 * if needed. That is, make sure that the first src->n_div divs
8404 * of the result are equal to those of src.
8406 * The result is not finalized as by design it will have redundant
8407 * divs if any divs from "src" were copied.
8409 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8410 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8413 int known
, extended
;
8417 return isl_basic_map_free(dst
);
8419 if (src
->n_div
== 0)
8422 known
= isl_basic_map_divs_known(src
);
8424 return isl_basic_map_free(dst
);
8426 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8427 "some src divs are unknown",
8428 return isl_basic_map_free(dst
));
8430 src
= isl_basic_map_order_divs(src
);
8433 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8434 for (i
= 0; i
< src
->n_div
; ++i
) {
8435 int j
= find_div(dst
, src
, i
);
8438 int extra
= src
->n_div
- i
;
8439 dst
= isl_basic_map_cow(dst
);
8442 dst
= isl_basic_map_extend_space(dst
,
8443 isl_space_copy(dst
->dim
),
8444 extra
, 0, 2 * extra
);
8447 j
= isl_basic_map_alloc_div(dst
);
8449 return isl_basic_map_free(dst
);
8450 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8451 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8452 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8453 return isl_basic_map_free(dst
);
8456 isl_basic_map_swap_div(dst
, i
, j
);
8461 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8469 map
= isl_map_compute_divs(map
);
8470 map
= isl_map_cow(map
);
8474 for (i
= 1; i
< map
->n
; ++i
)
8475 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8476 for (i
= 1; i
< map
->n
; ++i
) {
8477 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8479 return isl_map_free(map
);
8482 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8486 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8488 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8491 /* Align the divs of the basic maps in "map" to those
8492 * of the basic maps in "list", as well as to the other basic maps in "map".
8493 * The elements in "list" are assumed to have known divs.
8495 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8496 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8500 map
= isl_map_compute_divs(map
);
8501 map
= isl_map_cow(map
);
8503 return isl_map_free(map
);
8507 n
= isl_basic_map_list_n_basic_map(list
);
8508 for (i
= 0; i
< n
; ++i
) {
8509 isl_basic_map
*bmap
;
8511 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8512 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8513 isl_basic_map_free(bmap
);
8516 return isl_map_free(map
);
8518 return isl_map_align_divs(map
);
8521 /* Align the divs of each element of "list" to those of "bmap".
8522 * Both "bmap" and the elements of "list" are assumed to have known divs.
8524 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8525 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8530 return isl_basic_map_list_free(list
);
8532 n
= isl_basic_map_list_n_basic_map(list
);
8533 for (i
= 0; i
< n
; ++i
) {
8534 isl_basic_map
*bmap_i
;
8536 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8537 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8538 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8544 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8545 __isl_take isl_map
*map
)
8549 ok
= isl_map_compatible_domain(map
, set
);
8553 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8554 "incompatible spaces", goto error
);
8555 map
= isl_map_intersect_domain(map
, set
);
8556 set
= isl_map_range(map
);
8564 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8565 __isl_take isl_map
*map
)
8567 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8570 /* There is no need to cow as removing empty parts doesn't change
8571 * the meaning of the set.
8573 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8580 for (i
= map
->n
- 1; i
>= 0; --i
)
8581 remove_if_empty(map
, i
);
8586 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8588 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8591 /* Given two basic sets bset1 and bset2, compute the maximal difference
8592 * between the values of dimension pos in bset1 and those in bset2
8593 * for any common value of the parameters and dimensions preceding pos.
8595 static enum isl_lp_result
basic_set_maximal_difference_at(
8596 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8597 int pos
, isl_int
*opt
)
8599 isl_basic_map
*bmap1
;
8600 isl_basic_map
*bmap2
;
8601 struct isl_ctx
*ctx
;
8602 struct isl_vec
*obj
;
8606 enum isl_lp_result res
;
8608 if (!bset1
|| !bset2
)
8609 return isl_lp_error
;
8611 nparam
= isl_basic_set_n_param(bset1
);
8612 dim1
= isl_basic_set_n_dim(bset1
);
8614 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8615 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8616 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8617 isl_dim_out
, 0, pos
);
8618 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8619 isl_dim_out
, 0, pos
);
8620 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8622 return isl_lp_error
;
8624 total
= isl_basic_map_total_dim(bmap1
);
8626 obj
= isl_vec_alloc(ctx
, 1 + total
);
8629 isl_seq_clr(obj
->block
.data
, 1 + total
);
8630 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8631 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8632 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8634 isl_basic_map_free(bmap1
);
8638 isl_basic_map_free(bmap1
);
8639 return isl_lp_error
;
8642 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8643 * for any common value of the parameters and dimensions preceding pos
8644 * in both basic sets, the values of dimension pos in bset1 are
8645 * smaller or larger than those in bset2.
8648 * 1 if bset1 follows bset2
8649 * -1 if bset1 precedes bset2
8650 * 0 if bset1 and bset2 are incomparable
8651 * -2 if some error occurred.
8653 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8654 struct isl_basic_set
*bset2
, int pos
)
8657 enum isl_lp_result res
;
8662 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8664 if (res
== isl_lp_empty
)
8666 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8667 res
== isl_lp_unbounded
)
8669 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8678 /* Given two basic sets bset1 and bset2, check whether
8679 * for any common value of the parameters and dimensions preceding pos
8680 * there is a value of dimension pos in bset1 that is larger
8681 * than a value of the same dimension in bset2.
8684 * 1 if there exists such a pair
8685 * 0 if there is no such pair, but there is a pair of equal values
8687 * -2 if some error occurred.
8689 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8690 __isl_keep isl_basic_set
*bset2
, int pos
)
8693 enum isl_lp_result res
;
8698 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8700 if (res
== isl_lp_empty
)
8702 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8703 res
== isl_lp_unbounded
)
8705 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8707 else if (res
== isl_lp_ok
)
8716 /* Given two sets set1 and set2, check whether
8717 * for any common value of the parameters and dimensions preceding pos
8718 * there is a value of dimension pos in set1 that is larger
8719 * than a value of the same dimension in set2.
8722 * 1 if there exists such a pair
8723 * 0 if there is no such pair, but there is a pair of equal values
8725 * -2 if some error occurred.
8727 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8728 __isl_keep isl_set
*set2
, int pos
)
8736 for (i
= 0; i
< set1
->n
; ++i
)
8737 for (j
= 0; j
< set2
->n
; ++j
) {
8739 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8740 if (f
== 1 || f
== -2)
8749 static isl_bool
isl_basic_map_plain_has_fixed_var(
8750 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8757 return isl_bool_error
;
8758 total
= isl_basic_map_total_dim(bmap
);
8759 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8760 for (; d
+1 > pos
; --d
)
8761 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8765 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8766 return isl_bool_false
;
8767 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8768 return isl_bool_false
;
8769 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8770 return isl_bool_false
;
8772 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8773 return isl_bool_true
;
8775 return isl_bool_false
;
8778 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8779 unsigned pos
, isl_int
*val
)
8787 return isl_bool_error
;
8789 return isl_bool_false
;
8791 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8794 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8795 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
8796 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8797 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
8798 fixed
= isl_bool_false
;
8801 isl_int_set(*val
, v
);
8807 static isl_bool
isl_basic_set_plain_has_fixed_var(
8808 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
8810 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8814 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8815 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8817 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
8818 return isl_bool_error
;
8819 return isl_basic_map_plain_has_fixed_var(bmap
,
8820 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8823 /* If "bmap" obviously lies on a hyperplane where the given dimension
8824 * has a fixed value, then return that value.
8825 * Otherwise return NaN.
8827 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8828 __isl_keep isl_basic_map
*bmap
,
8829 enum isl_dim_type type
, unsigned pos
)
8837 ctx
= isl_basic_map_get_ctx(bmap
);
8838 v
= isl_val_alloc(ctx
);
8841 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8843 return isl_val_free(v
);
8845 isl_int_set_si(v
->d
, 1);
8849 return isl_val_nan(ctx
);
8852 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8853 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8855 if (pos
>= isl_map_dim(map
, type
))
8856 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8857 "position out of bounds", return isl_bool_error
);
8858 return isl_map_plain_has_fixed_var(map
,
8859 map_offset(map
, type
) - 1 + pos
, val
);
8862 /* If "map" obviously lies on a hyperplane where the given dimension
8863 * has a fixed value, then return that value.
8864 * Otherwise return NaN.
8866 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8867 enum isl_dim_type type
, unsigned pos
)
8875 ctx
= isl_map_get_ctx(map
);
8876 v
= isl_val_alloc(ctx
);
8879 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8881 return isl_val_free(v
);
8883 isl_int_set_si(v
->d
, 1);
8887 return isl_val_nan(ctx
);
8890 /* If "set" obviously lies on a hyperplane where the given dimension
8891 * has a fixed value, then return that value.
8892 * Otherwise return NaN.
8894 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8895 enum isl_dim_type type
, unsigned pos
)
8897 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8900 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8901 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8903 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8906 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8907 * then return this fixed value in *val.
8909 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8910 unsigned dim
, isl_int
*val
)
8912 return isl_basic_set_plain_has_fixed_var(bset
,
8913 isl_basic_set_n_param(bset
) + dim
, val
);
8916 /* Return -1 if the constraint "c1" should be sorted before "c2"
8917 * and 1 if it should be sorted after "c2".
8918 * Return 0 if the two constraints are the same (up to the constant term).
8920 * In particular, if a constraint involves later variables than another
8921 * then it is sorted after this other constraint.
8922 * uset_gist depends on constraints without existentially quantified
8923 * variables sorting first.
8925 * For constraints that have the same latest variable, those
8926 * with the same coefficient for this latest variable (first in absolute value
8927 * and then in actual value) are grouped together.
8928 * This is useful for detecting pairs of constraints that can
8929 * be chained in their printed representation.
8931 * Finally, within a group, constraints are sorted according to
8932 * their coefficients (excluding the constant term).
8934 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8936 isl_int
**c1
= (isl_int
**) p1
;
8937 isl_int
**c2
= (isl_int
**) p2
;
8939 unsigned size
= *(unsigned *) arg
;
8942 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8943 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8948 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8951 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8955 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8958 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8959 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8960 * and 0 if the two constraints are the same (up to the constant term).
8962 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
8963 isl_int
*c1
, isl_int
*c2
)
8969 total
= isl_basic_map_total_dim(bmap
);
8970 return sort_constraint_cmp(&c1
, &c2
, &total
);
8973 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
8974 __isl_take isl_basic_map
*bmap
)
8980 if (bmap
->n_ineq
== 0)
8982 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8984 total
= isl_basic_map_total_dim(bmap
);
8985 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8986 &sort_constraint_cmp
, &total
) < 0)
8987 return isl_basic_map_free(bmap
);
8991 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8992 __isl_take isl_basic_set
*bset
)
8994 isl_basic_map
*bmap
= bset_to_bmap(bset
);
8995 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
8998 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9002 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9004 bmap
= isl_basic_map_remove_redundancies(bmap
);
9005 bmap
= isl_basic_map_sort_constraints(bmap
);
9007 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9010 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9011 __isl_keep isl_basic_map
*bmap2
)
9016 if (!bmap1
|| !bmap2
)
9021 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9022 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9023 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9024 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9025 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9026 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9027 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9028 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9029 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9030 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9031 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9033 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9035 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9037 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9038 return bmap1
->n_eq
- bmap2
->n_eq
;
9039 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9040 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9041 if (bmap1
->n_div
!= bmap2
->n_div
)
9042 return bmap1
->n_div
- bmap2
->n_div
;
9043 total
= isl_basic_map_total_dim(bmap1
);
9044 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9045 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9049 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9050 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9054 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9055 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9062 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9063 __isl_keep isl_basic_set
*bset2
)
9065 return isl_basic_map_plain_cmp(bset1
, bset2
);
9068 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9074 if (set1
->n
!= set2
->n
)
9075 return set1
->n
- set2
->n
;
9077 for (i
= 0; i
< set1
->n
; ++i
) {
9078 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9086 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9087 __isl_keep isl_basic_map
*bmap2
)
9089 if (!bmap1
|| !bmap2
)
9090 return isl_bool_error
;
9091 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9094 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9095 __isl_keep isl_basic_set
*bset2
)
9097 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9098 bset_to_bmap(bset2
));
9101 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9103 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9104 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9106 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9109 /* Sort the basic maps of "map" and remove duplicate basic maps.
9111 * While removing basic maps, we make sure that the basic maps remain
9112 * sorted because isl_map_normalize expects the basic maps of the result
9115 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9119 map
= isl_map_remove_empty_parts(map
);
9122 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9123 for (i
= map
->n
- 1; i
>= 1; --i
) {
9124 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9126 isl_basic_map_free(map
->p
[i
-1]);
9127 for (j
= i
; j
< map
->n
; ++j
)
9128 map
->p
[j
- 1] = map
->p
[j
];
9135 /* Remove obvious duplicates among the basic maps of "map".
9137 * Unlike isl_map_normalize, this function does not remove redundant
9138 * constraints and only removes duplicates that have exactly the same
9139 * constraints in the input. It does sort the constraints and
9140 * the basic maps to ease the detection of duplicates.
9142 * If "map" has already been normalized or if the basic maps are
9143 * disjoint, then there can be no duplicates.
9145 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9148 isl_basic_map
*bmap
;
9154 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9156 for (i
= 0; i
< map
->n
; ++i
) {
9157 bmap
= isl_basic_map_copy(map
->p
[i
]);
9158 bmap
= isl_basic_map_sort_constraints(bmap
);
9160 return isl_map_free(map
);
9161 isl_basic_map_free(map
->p
[i
]);
9165 map
= sort_and_remove_duplicates(map
);
9169 /* We normalize in place, but if anything goes wrong we need
9170 * to return NULL, so we need to make sure we don't change the
9171 * meaning of any possible other copies of map.
9173 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9176 struct isl_basic_map
*bmap
;
9180 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9182 for (i
= 0; i
< map
->n
; ++i
) {
9183 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9186 isl_basic_map_free(map
->p
[i
]);
9190 map
= sort_and_remove_duplicates(map
);
9192 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9199 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9201 return set_from_map(isl_map_normalize(set_to_map(set
)));
9204 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9205 __isl_keep isl_map
*map2
)
9211 return isl_bool_error
;
9214 return isl_bool_true
;
9215 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9216 return isl_bool_false
;
9218 map1
= isl_map_copy(map1
);
9219 map2
= isl_map_copy(map2
);
9220 map1
= isl_map_normalize(map1
);
9221 map2
= isl_map_normalize(map2
);
9224 equal
= map1
->n
== map2
->n
;
9225 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9226 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9236 return isl_bool_error
;
9239 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9240 __isl_keep isl_set
*set2
)
9242 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9245 /* Return the basic maps in "map" as a list.
9247 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9248 __isl_keep isl_map
*map
)
9252 isl_basic_map_list
*list
;
9256 ctx
= isl_map_get_ctx(map
);
9257 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9259 for (i
= 0; i
< map
->n
; ++i
) {
9260 isl_basic_map
*bmap
;
9262 bmap
= isl_basic_map_copy(map
->p
[i
]);
9263 list
= isl_basic_map_list_add(list
, bmap
);
9269 /* Return the intersection of the elements in the non-empty list "list".
9270 * All elements are assumed to live in the same space.
9272 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9273 __isl_take isl_basic_map_list
*list
)
9276 isl_basic_map
*bmap
;
9280 n
= isl_basic_map_list_n_basic_map(list
);
9282 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9283 "expecting non-empty list", goto error
);
9285 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9286 for (i
= 1; i
< n
; ++i
) {
9287 isl_basic_map
*bmap_i
;
9289 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9290 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9293 isl_basic_map_list_free(list
);
9296 isl_basic_map_list_free(list
);
9300 /* Return the intersection of the elements in the non-empty list "list".
9301 * All elements are assumed to live in the same space.
9303 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9304 __isl_take isl_basic_set_list
*list
)
9306 return isl_basic_map_list_intersect(list
);
9309 /* Return the union of the elements of "list".
9310 * The list is required to have at least one element.
9312 __isl_give isl_set
*isl_basic_set_list_union(
9313 __isl_take isl_basic_set_list
*list
)
9317 isl_basic_set
*bset
;
9322 n
= isl_basic_set_list_n_basic_set(list
);
9324 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9325 "expecting non-empty list", goto error
);
9327 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9328 space
= isl_basic_set_get_space(bset
);
9329 isl_basic_set_free(bset
);
9331 set
= isl_set_alloc_space(space
, n
, 0);
9332 for (i
= 0; i
< n
; ++i
) {
9333 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9334 set
= isl_set_add_basic_set(set
, bset
);
9337 isl_basic_set_list_free(list
);
9340 isl_basic_set_list_free(list
);
9344 /* Return the union of the elements in the non-empty list "list".
9345 * All elements are assumed to live in the same space.
9347 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9354 n
= isl_set_list_n_set(list
);
9356 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9357 "expecting non-empty list", goto error
);
9359 set
= isl_set_list_get_set(list
, 0);
9360 for (i
= 1; i
< n
; ++i
) {
9363 set_i
= isl_set_list_get_set(list
, i
);
9364 set
= isl_set_union(set
, set_i
);
9367 isl_set_list_free(list
);
9370 isl_set_list_free(list
);
9374 struct isl_basic_map
*isl_basic_map_product(
9375 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9377 isl_space
*dim_result
= NULL
;
9378 struct isl_basic_map
*bmap
;
9379 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9380 struct isl_dim_map
*dim_map1
, *dim_map2
;
9382 if (!bmap1
|| !bmap2
)
9385 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9386 bmap2
->dim
, isl_dim_param
), goto error
);
9387 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9388 isl_space_copy(bmap2
->dim
));
9390 in1
= isl_basic_map_n_in(bmap1
);
9391 in2
= isl_basic_map_n_in(bmap2
);
9392 out1
= isl_basic_map_n_out(bmap1
);
9393 out2
= isl_basic_map_n_out(bmap2
);
9394 nparam
= isl_basic_map_n_param(bmap1
);
9396 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9397 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9398 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9399 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9400 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9401 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9402 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9403 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9404 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9405 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9406 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9408 bmap
= isl_basic_map_alloc_space(dim_result
,
9409 bmap1
->n_div
+ bmap2
->n_div
,
9410 bmap1
->n_eq
+ bmap2
->n_eq
,
9411 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9412 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9413 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9414 bmap
= isl_basic_map_simplify(bmap
);
9415 return isl_basic_map_finalize(bmap
);
9417 isl_basic_map_free(bmap1
);
9418 isl_basic_map_free(bmap2
);
9422 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9423 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9425 isl_basic_map
*prod
;
9427 prod
= isl_basic_map_product(bmap1
, bmap2
);
9428 prod
= isl_basic_map_flatten(prod
);
9432 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9433 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9435 return isl_basic_map_flat_range_product(bset1
, bset2
);
9438 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9439 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9441 isl_space
*space_result
= NULL
;
9442 isl_basic_map
*bmap
;
9443 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9444 struct isl_dim_map
*dim_map1
, *dim_map2
;
9446 if (!bmap1
|| !bmap2
)
9449 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9450 isl_space_copy(bmap2
->dim
));
9452 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9453 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9454 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9455 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9457 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9458 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9459 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9460 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9461 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9462 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9463 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9464 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9465 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9466 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9467 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9469 bmap
= isl_basic_map_alloc_space(space_result
,
9470 bmap1
->n_div
+ bmap2
->n_div
,
9471 bmap1
->n_eq
+ bmap2
->n_eq
,
9472 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9473 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9474 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9475 bmap
= isl_basic_map_simplify(bmap
);
9476 return isl_basic_map_finalize(bmap
);
9478 isl_basic_map_free(bmap1
);
9479 isl_basic_map_free(bmap2
);
9483 __isl_give isl_basic_map
*isl_basic_map_range_product(
9484 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9487 isl_space
*dim_result
= NULL
;
9488 isl_basic_map
*bmap
;
9489 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9490 struct isl_dim_map
*dim_map1
, *dim_map2
;
9492 rational
= isl_basic_map_is_rational(bmap1
);
9493 if (rational
>= 0 && rational
)
9494 rational
= isl_basic_map_is_rational(bmap2
);
9495 if (!bmap1
|| !bmap2
|| rational
< 0)
9498 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9499 bmap2
->dim
, isl_dim_param
))
9500 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9501 "parameters don't match", goto error
);
9503 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9504 isl_space_copy(bmap2
->dim
));
9506 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9507 out1
= isl_basic_map_n_out(bmap1
);
9508 out2
= isl_basic_map_n_out(bmap2
);
9509 nparam
= isl_basic_map_n_param(bmap1
);
9511 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9512 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9513 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9514 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9515 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9516 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9517 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9518 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9519 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9520 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9521 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9523 bmap
= isl_basic_map_alloc_space(dim_result
,
9524 bmap1
->n_div
+ bmap2
->n_div
,
9525 bmap1
->n_eq
+ bmap2
->n_eq
,
9526 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9527 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9528 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9530 bmap
= isl_basic_map_set_rational(bmap
);
9531 bmap
= isl_basic_map_simplify(bmap
);
9532 return isl_basic_map_finalize(bmap
);
9534 isl_basic_map_free(bmap1
);
9535 isl_basic_map_free(bmap2
);
9539 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9540 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9542 isl_basic_map
*prod
;
9544 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9545 prod
= isl_basic_map_flatten_range(prod
);
9549 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9550 * and collect the results.
9551 * The result live in the space obtained by calling "space_product"
9552 * on the spaces of "map1" and "map2".
9553 * If "remove_duplicates" is set then the result may contain duplicates
9554 * (even if the inputs do not) and so we try and remove the obvious
9557 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9558 __isl_take isl_map
*map2
,
9559 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9560 __isl_take isl_space
*right
),
9561 __isl_give isl_basic_map
*(*basic_map_product
)(
9562 __isl_take isl_basic_map
*left
,
9563 __isl_take isl_basic_map
*right
),
9564 int remove_duplicates
)
9567 struct isl_map
*result
;
9573 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9574 map2
->dim
, isl_dim_param
), goto error
);
9576 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9577 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9578 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9580 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9581 isl_space_copy(map2
->dim
)),
9582 map1
->n
* map2
->n
, flags
);
9585 for (i
= 0; i
< map1
->n
; ++i
)
9586 for (j
= 0; j
< map2
->n
; ++j
) {
9587 struct isl_basic_map
*part
;
9588 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9589 isl_basic_map_copy(map2
->p
[j
]));
9590 if (isl_basic_map_is_empty(part
))
9591 isl_basic_map_free(part
);
9593 result
= isl_map_add_basic_map(result
, part
);
9597 if (remove_duplicates
)
9598 result
= isl_map_remove_obvious_duplicates(result
);
9608 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9610 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9611 __isl_take isl_map
*map2
)
9613 return map_product(map1
, map2
, &isl_space_product
,
9614 &isl_basic_map_product
, 0);
9617 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9618 __isl_take isl_map
*map2
)
9620 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9623 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9625 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9626 __isl_take isl_map
*map2
)
9630 prod
= isl_map_product(map1
, map2
);
9631 prod
= isl_map_flatten(prod
);
9635 /* Given two set A and B, construct its Cartesian product A x B.
9637 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9639 return isl_map_range_product(set1
, set2
);
9642 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9643 __isl_take isl_set
*set2
)
9645 return isl_map_flat_range_product(set1
, set2
);
9648 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9650 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9651 __isl_take isl_map
*map2
)
9653 return map_product(map1
, map2
, &isl_space_domain_product
,
9654 &isl_basic_map_domain_product
, 1);
9657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9659 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9660 __isl_take isl_map
*map2
)
9662 return map_product(map1
, map2
, &isl_space_range_product
,
9663 &isl_basic_map_range_product
, 1);
9666 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9667 __isl_take isl_map
*map2
)
9669 return isl_map_align_params_map_map_and(map1
, map2
,
9670 &map_domain_product_aligned
);
9673 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9674 __isl_take isl_map
*map2
)
9676 return isl_map_align_params_map_map_and(map1
, map2
,
9677 &map_range_product_aligned
);
9680 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9682 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9685 int total1
, keep1
, total2
, keep2
;
9689 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9690 !isl_space_range_is_wrapping(map
->dim
))
9691 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9692 "not a product", return isl_map_free(map
));
9694 space
= isl_map_get_space(map
);
9695 total1
= isl_space_dim(space
, isl_dim_in
);
9696 total2
= isl_space_dim(space
, isl_dim_out
);
9697 space
= isl_space_factor_domain(space
);
9698 keep1
= isl_space_dim(space
, isl_dim_in
);
9699 keep2
= isl_space_dim(space
, isl_dim_out
);
9700 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9701 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9702 map
= isl_map_reset_space(map
, space
);
9707 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9709 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9712 int total1
, keep1
, total2
, keep2
;
9716 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9717 !isl_space_range_is_wrapping(map
->dim
))
9718 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9719 "not a product", return isl_map_free(map
));
9721 space
= isl_map_get_space(map
);
9722 total1
= isl_space_dim(space
, isl_dim_in
);
9723 total2
= isl_space_dim(space
, isl_dim_out
);
9724 space
= isl_space_factor_range(space
);
9725 keep1
= isl_space_dim(space
, isl_dim_in
);
9726 keep2
= isl_space_dim(space
, isl_dim_out
);
9727 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9728 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9729 map
= isl_map_reset_space(map
, space
);
9734 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9736 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9743 if (!isl_space_domain_is_wrapping(map
->dim
))
9744 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9745 "domain is not a product", return isl_map_free(map
));
9747 space
= isl_map_get_space(map
);
9748 total
= isl_space_dim(space
, isl_dim_in
);
9749 space
= isl_space_domain_factor_domain(space
);
9750 keep
= isl_space_dim(space
, isl_dim_in
);
9751 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9752 map
= isl_map_reset_space(map
, space
);
9757 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9759 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9766 if (!isl_space_domain_is_wrapping(map
->dim
))
9767 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9768 "domain is not a product", return isl_map_free(map
));
9770 space
= isl_map_get_space(map
);
9771 total
= isl_space_dim(space
, isl_dim_in
);
9772 space
= isl_space_domain_factor_range(space
);
9773 keep
= isl_space_dim(space
, isl_dim_in
);
9774 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9775 map
= isl_map_reset_space(map
, space
);
9780 /* Given a map A -> [B -> C], extract the map A -> B.
9782 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9789 if (!isl_space_range_is_wrapping(map
->dim
))
9790 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9791 "range is not a product", return isl_map_free(map
));
9793 space
= isl_map_get_space(map
);
9794 total
= isl_space_dim(space
, isl_dim_out
);
9795 space
= isl_space_range_factor_domain(space
);
9796 keep
= isl_space_dim(space
, isl_dim_out
);
9797 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9798 map
= isl_map_reset_space(map
, space
);
9803 /* Given a map A -> [B -> C], extract the map A -> C.
9805 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9812 if (!isl_space_range_is_wrapping(map
->dim
))
9813 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9814 "range is not a product", return isl_map_free(map
));
9816 space
= isl_map_get_space(map
);
9817 total
= isl_space_dim(space
, isl_dim_out
);
9818 space
= isl_space_range_factor_range(space
);
9819 keep
= isl_space_dim(space
, isl_dim_out
);
9820 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9821 map
= isl_map_reset_space(map
, space
);
9826 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9828 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9829 __isl_take isl_map
*map2
)
9833 prod
= isl_map_domain_product(map1
, map2
);
9834 prod
= isl_map_flatten_domain(prod
);
9838 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9840 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9841 __isl_take isl_map
*map2
)
9845 prod
= isl_map_range_product(map1
, map2
);
9846 prod
= isl_map_flatten_range(prod
);
9850 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9853 uint32_t hash
= isl_hash_init();
9858 bmap
= isl_basic_map_copy(bmap
);
9859 bmap
= isl_basic_map_normalize(bmap
);
9862 total
= isl_basic_map_total_dim(bmap
);
9863 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9864 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9866 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9867 isl_hash_hash(hash
, c_hash
);
9869 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9870 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9872 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9873 isl_hash_hash(hash
, c_hash
);
9875 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9876 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9878 if (isl_int_is_zero(bmap
->div
[i
][0]))
9880 isl_hash_byte(hash
, i
& 0xFF);
9881 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9882 isl_hash_hash(hash
, c_hash
);
9884 isl_basic_map_free(bmap
);
9888 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9890 return isl_basic_map_get_hash(bset_to_bmap(bset
));
9893 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9900 map
= isl_map_copy(map
);
9901 map
= isl_map_normalize(map
);
9905 hash
= isl_hash_init();
9906 for (i
= 0; i
< map
->n
; ++i
) {
9908 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9909 isl_hash_hash(hash
, bmap_hash
);
9917 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9919 return isl_map_get_hash(set_to_map(set
));
9922 /* Return the number of basic maps in the (current) representation of "map".
9924 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9926 return map
? map
->n
: 0;
9929 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9931 return set
? set
->n
: 0;
9934 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9935 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9940 return isl_stat_error
;
9942 for (i
= 0; i
< map
->n
; ++i
)
9943 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9944 return isl_stat_error
;
9949 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9950 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9955 return isl_stat_error
;
9957 for (i
= 0; i
< set
->n
; ++i
)
9958 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9959 return isl_stat_error
;
9964 /* Return a list of basic sets, the union of which is equal to "set".
9966 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9967 __isl_keep isl_set
*set
)
9970 isl_basic_set_list
*list
;
9975 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
9976 for (i
= 0; i
< set
->n
; ++i
) {
9977 isl_basic_set
*bset
;
9979 bset
= isl_basic_set_copy(set
->p
[i
]);
9980 list
= isl_basic_set_list_add(list
, bset
);
9986 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9993 bset
= isl_basic_set_cow(bset
);
9997 dim
= isl_basic_set_get_space(bset
);
9998 dim
= isl_space_lift(dim
, bset
->n_div
);
10001 isl_space_free(bset
->dim
);
10003 bset
->extra
-= bset
->n_div
;
10006 bset
= isl_basic_set_finalize(bset
);
10010 isl_basic_set_free(bset
);
10014 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10020 set
= isl_set_align_divs(set
);
10025 set
= isl_set_cow(set
);
10029 n_div
= set
->p
[0]->n_div
;
10030 dim
= isl_set_get_space(set
);
10031 dim
= isl_space_lift(dim
, n_div
);
10034 isl_space_free(set
->dim
);
10037 for (i
= 0; i
< set
->n
; ++i
) {
10038 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10049 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10057 dim
= isl_basic_set_total_dim(bset
);
10058 size
+= bset
->n_eq
* (1 + dim
);
10059 size
+= bset
->n_ineq
* (1 + dim
);
10060 size
+= bset
->n_div
* (2 + dim
);
10065 int isl_set_size(__isl_keep isl_set
*set
)
10073 for (i
= 0; i
< set
->n
; ++i
)
10074 size
+= isl_basic_set_size(set
->p
[i
]);
10079 /* Check if there is any lower bound (if lower == 0) and/or upper
10080 * bound (if upper == 0) on the specified dim.
10082 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10083 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10087 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10088 return isl_bool_error
;
10090 pos
+= isl_basic_map_offset(bmap
, type
);
10092 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10093 if (isl_int_is_zero(bmap
->div
[i
][0]))
10095 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10096 return isl_bool_true
;
10099 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10100 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10101 return isl_bool_true
;
10103 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10104 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10111 return lower
&& upper
;
10114 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10115 enum isl_dim_type type
, unsigned pos
)
10117 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10120 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10121 enum isl_dim_type type
, unsigned pos
)
10123 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10126 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10127 enum isl_dim_type type
, unsigned pos
)
10129 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10132 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10133 enum isl_dim_type type
, unsigned pos
)
10138 return isl_bool_error
;
10140 for (i
= 0; i
< map
->n
; ++i
) {
10142 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10143 if (bounded
< 0 || !bounded
)
10147 return isl_bool_true
;
10150 /* Return true if the specified dim is involved in both an upper bound
10151 * and a lower bound.
10153 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10154 enum isl_dim_type type
, unsigned pos
)
10156 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10159 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10161 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10162 enum isl_dim_type type
, unsigned pos
,
10163 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10164 enum isl_dim_type type
, unsigned pos
))
10169 return isl_bool_error
;
10171 for (i
= 0; i
< map
->n
; ++i
) {
10173 bounded
= fn(map
->p
[i
], type
, pos
);
10174 if (bounded
< 0 || bounded
)
10178 return isl_bool_false
;
10181 /* Return 1 if the specified dim is involved in any lower bound.
10183 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10184 enum isl_dim_type type
, unsigned pos
)
10186 return has_any_bound(set
, type
, pos
,
10187 &isl_basic_map_dim_has_lower_bound
);
10190 /* Return 1 if the specified dim is involved in any upper bound.
10192 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10193 enum isl_dim_type type
, unsigned pos
)
10195 return has_any_bound(set
, type
, pos
,
10196 &isl_basic_map_dim_has_upper_bound
);
10199 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10201 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10202 enum isl_dim_type type
, unsigned pos
,
10203 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10204 enum isl_dim_type type
, unsigned pos
))
10209 return isl_bool_error
;
10211 for (i
= 0; i
< map
->n
; ++i
) {
10213 bounded
= fn(map
->p
[i
], type
, pos
);
10214 if (bounded
< 0 || !bounded
)
10218 return isl_bool_true
;
10221 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10223 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10224 enum isl_dim_type type
, unsigned pos
)
10226 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10229 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10231 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10232 enum isl_dim_type type
, unsigned pos
)
10234 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10237 /* For each of the "n" variables starting at "first", determine
10238 * the sign of the variable and put the results in the first "n"
10239 * elements of the array "signs".
10241 * 1 means that the variable is non-negative
10242 * -1 means that the variable is non-positive
10243 * 0 means the variable attains both positive and negative values.
10245 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10246 unsigned first
, unsigned n
, int *signs
)
10248 isl_vec
*bound
= NULL
;
10249 struct isl_tab
*tab
= NULL
;
10250 struct isl_tab_undo
*snap
;
10253 if (!bset
|| !signs
)
10254 return isl_stat_error
;
10256 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10257 tab
= isl_tab_from_basic_set(bset
, 0);
10258 if (!bound
|| !tab
)
10261 isl_seq_clr(bound
->el
, bound
->size
);
10262 isl_int_set_si(bound
->el
[0], -1);
10264 snap
= isl_tab_snap(tab
);
10265 for (i
= 0; i
< n
; ++i
) {
10268 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10269 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10271 empty
= tab
->empty
;
10272 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10273 if (isl_tab_rollback(tab
, snap
) < 0)
10281 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10282 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10284 empty
= tab
->empty
;
10285 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10286 if (isl_tab_rollback(tab
, snap
) < 0)
10289 signs
[i
] = empty
? -1 : 0;
10293 isl_vec_free(bound
);
10294 return isl_stat_ok
;
10297 isl_vec_free(bound
);
10298 return isl_stat_error
;
10301 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10302 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10304 if (!bset
|| !signs
)
10305 return isl_stat_error
;
10306 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10307 return isl_stat_error
);
10309 first
+= pos(bset
->dim
, type
) - 1;
10310 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10313 /* Is it possible for the integer division "div" to depend (possibly
10314 * indirectly) on any output dimensions?
10316 * If the div is undefined, then we conservatively assume that it
10317 * may depend on them.
10318 * Otherwise, we check if it actually depends on them or on any integer
10319 * divisions that may depend on them.
10321 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10324 unsigned n_out
, o_out
;
10325 unsigned n_div
, o_div
;
10327 if (isl_int_is_zero(bmap
->div
[div
][0]))
10328 return isl_bool_true
;
10330 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10331 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10333 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10334 return isl_bool_true
;
10336 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10337 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10339 for (i
= 0; i
< n_div
; ++i
) {
10340 isl_bool may_involve
;
10342 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10344 may_involve
= div_may_involve_output(bmap
, i
);
10345 if (may_involve
< 0 || may_involve
)
10346 return may_involve
;
10349 return isl_bool_false
;
10352 /* Return the first integer division of "bmap" in the range
10353 * [first, first + n[ that may depend on any output dimensions and
10354 * that has a non-zero coefficient in "c" (where the first coefficient
10355 * in "c" corresponds to integer division "first").
10357 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10358 isl_int
*c
, int first
, int n
)
10365 for (k
= first
; k
< first
+ n
; ++k
) {
10366 isl_bool may_involve
;
10368 if (isl_int_is_zero(c
[k
]))
10370 may_involve
= div_may_involve_output(bmap
, k
);
10371 if (may_involve
< 0)
10380 /* Look for a pair of inequality constraints in "bmap" of the form
10382 * -l + i >= 0 or i >= l
10384 * n + l - i >= 0 or i <= l + n
10386 * with n < "m" and i the output dimension at position "pos".
10387 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10388 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10389 * and earlier output dimensions, as well as integer divisions that do
10390 * not involve any of the output dimensions.
10392 * Return the index of the first inequality constraint or bmap->n_ineq
10393 * if no such pair can be found.
10395 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10396 int pos
, isl_int m
)
10401 unsigned n_div
, o_div
;
10402 unsigned n_out
, o_out
;
10408 ctx
= isl_basic_map_get_ctx(bmap
);
10409 total
= isl_basic_map_total_dim(bmap
);
10410 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10411 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10412 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10413 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10414 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10415 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10417 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10418 n_out
- (pos
+ 1)) != -1)
10420 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10423 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10424 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10427 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10428 bmap
->ineq
[j
] + 1, total
))
10432 if (j
>= bmap
->n_ineq
)
10434 isl_int_add(bmap
->ineq
[i
][0],
10435 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10436 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10437 isl_int_sub(bmap
->ineq
[i
][0],
10438 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10441 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10447 return bmap
->n_ineq
;
10450 /* Return the index of the equality of "bmap" that defines
10451 * the output dimension "pos" in terms of earlier dimensions.
10452 * The equality may also involve integer divisions, as long
10453 * as those integer divisions are defined in terms of
10454 * parameters or input dimensions.
10455 * In this case, *div is set to the number of integer divisions and
10456 * *ineq is set to the number of inequality constraints (provided
10457 * div and ineq are not NULL).
10459 * The equality may also involve a single integer division involving
10460 * the output dimensions (typically only output dimension "pos") as
10461 * long as the coefficient of output dimension "pos" is 1 or -1 and
10462 * there is a pair of constraints i >= l and i <= l + n, with i referring
10463 * to output dimension "pos", l an expression involving only earlier
10464 * dimensions and n smaller than the coefficient of the integer division
10465 * in the equality. In this case, the output dimension can be defined
10466 * in terms of a modulo expression that does not involve the integer division.
10467 * *div is then set to this single integer division and
10468 * *ineq is set to the index of constraint i >= l.
10470 * Return bmap->n_eq if there is no such equality.
10471 * Return -1 on error.
10473 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10474 int pos
, int *div
, int *ineq
)
10477 unsigned n_out
, o_out
;
10478 unsigned n_div
, o_div
;
10483 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10484 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10485 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10486 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10489 *ineq
= bmap
->n_ineq
;
10492 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10493 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10495 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10496 n_out
- (pos
+ 1)) != -1)
10498 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10502 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10503 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10505 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10506 k
+ 1, n_div
- (k
+1)) < n_div
)
10508 l
= find_modulo_constraint_pair(bmap
, pos
,
10509 bmap
->eq
[j
][o_div
+ k
]);
10512 if (l
>= bmap
->n_ineq
)
10524 /* Check if the given basic map is obviously single-valued.
10525 * In particular, for each output dimension, check that there is
10526 * an equality that defines the output dimension in terms of
10527 * earlier dimensions.
10529 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10535 return isl_bool_error
;
10537 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10539 for (i
= 0; i
< n_out
; ++i
) {
10542 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10545 return isl_bool_error
;
10546 if (eq
>= bmap
->n_eq
)
10547 return isl_bool_false
;
10550 return isl_bool_true
;
10553 /* Check if the given basic map is single-valued.
10554 * We simply compute
10558 * and check if the result is a subset of the identity mapping.
10560 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10563 isl_basic_map
*test
;
10567 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10571 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10572 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10574 space
= isl_basic_map_get_space(bmap
);
10575 space
= isl_space_map_from_set(isl_space_range(space
));
10576 id
= isl_basic_map_identity(space
);
10578 sv
= isl_basic_map_is_subset(test
, id
);
10580 isl_basic_map_free(test
);
10581 isl_basic_map_free(id
);
10586 /* Check if the given map is obviously single-valued.
10588 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10591 return isl_bool_error
;
10593 return isl_bool_true
;
10595 return isl_bool_false
;
10597 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10600 /* Check if the given map is single-valued.
10601 * We simply compute
10605 * and check if the result is a subset of the identity mapping.
10607 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10614 sv
= isl_map_plain_is_single_valued(map
);
10618 test
= isl_map_reverse(isl_map_copy(map
));
10619 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10621 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10622 id
= isl_map_identity(dim
);
10624 sv
= isl_map_is_subset(test
, id
);
10626 isl_map_free(test
);
10632 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10636 map
= isl_map_copy(map
);
10637 map
= isl_map_reverse(map
);
10638 in
= isl_map_is_single_valued(map
);
10644 /* Check if the given map is obviously injective.
10646 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10650 map
= isl_map_copy(map
);
10651 map
= isl_map_reverse(map
);
10652 in
= isl_map_plain_is_single_valued(map
);
10658 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10662 sv
= isl_map_is_single_valued(map
);
10666 return isl_map_is_injective(map
);
10669 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10671 return isl_map_is_single_valued(set_to_map(set
));
10674 /* Does "map" only map elements to themselves?
10676 * If the domain and range spaces are different, then "map"
10677 * is considered not to be an identity relation, even if it is empty.
10678 * Otherwise, construct the maximal identity relation and
10679 * check whether "map" is a subset of this relation.
10681 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10685 isl_bool equal
, is_identity
;
10687 space
= isl_map_get_space(map
);
10688 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10689 isl_space_free(space
);
10690 if (equal
< 0 || !equal
)
10693 id
= isl_map_identity(isl_map_get_space(map
));
10694 is_identity
= isl_map_is_subset(map
, id
);
10697 return is_identity
;
10700 int isl_map_is_translation(__isl_keep isl_map
*map
)
10705 delta
= isl_map_deltas(isl_map_copy(map
));
10706 ok
= isl_set_is_singleton(delta
);
10707 isl_set_free(delta
);
10712 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10714 if (isl_seq_first_non_zero(p
, pos
) != -1)
10716 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10721 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10728 return isl_bool_error
;
10730 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10731 return isl_bool_false
;
10733 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10734 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10735 for (j
= 0; j
< nvar
; ++j
) {
10736 int lower
= 0, upper
= 0;
10737 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10738 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10740 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10741 return isl_bool_false
;
10744 if (i
< bset
->n_eq
)
10746 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10747 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10749 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10750 return isl_bool_false
;
10751 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10756 if (!lower
|| !upper
)
10757 return isl_bool_false
;
10760 return isl_bool_true
;
10763 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
10766 return isl_bool_error
;
10768 return isl_bool_false
;
10770 return isl_basic_set_is_box(set
->p
[0]);
10773 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10776 return isl_bool_error
;
10778 return isl_space_is_wrapping(bset
->dim
);
10781 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10784 return isl_bool_error
;
10786 return isl_space_is_wrapping(set
->dim
);
10789 /* Modify the space of "map" through a call to "change".
10790 * If "can_change" is set (not NULL), then first call it to check
10791 * if the modification is allowed, printing the error message "cannot_change"
10794 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10795 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10796 const char *cannot_change
,
10797 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10805 ok
= can_change
? can_change(map
) : isl_bool_true
;
10807 return isl_map_free(map
);
10809 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10810 return isl_map_free(map
));
10812 space
= change(isl_map_get_space(map
));
10813 map
= isl_map_reset_space(map
, space
);
10818 /* Is the domain of "map" a wrapped relation?
10820 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10823 return isl_bool_error
;
10825 return isl_space_domain_is_wrapping(map
->dim
);
10828 /* Is the range of "map" a wrapped relation?
10830 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10833 return isl_bool_error
;
10835 return isl_space_range_is_wrapping(map
->dim
);
10838 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10840 bmap
= isl_basic_map_cow(bmap
);
10844 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10848 bmap
= isl_basic_map_finalize(bmap
);
10850 return bset_from_bmap(bmap
);
10852 isl_basic_map_free(bmap
);
10856 /* Given a map A -> B, return the set (A -> B).
10858 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10860 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
10863 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10865 bset
= isl_basic_set_cow(bset
);
10869 bset
->dim
= isl_space_unwrap(bset
->dim
);
10873 bset
= isl_basic_set_finalize(bset
);
10875 return bset_to_bmap(bset
);
10877 isl_basic_set_free(bset
);
10881 /* Given a set (A -> B), return the map A -> B.
10882 * Error out if "set" is not of the form (A -> B).
10884 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10886 return isl_map_change_space(set
, &isl_set_is_wrapping
,
10887 "not a wrapping set", &isl_space_unwrap
);
10890 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10891 enum isl_dim_type type
)
10896 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10899 bmap
= isl_basic_map_cow(bmap
);
10903 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10907 bmap
= isl_basic_map_finalize(bmap
);
10911 isl_basic_map_free(bmap
);
10915 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10916 enum isl_dim_type type
)
10923 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10926 map
= isl_map_cow(map
);
10930 for (i
= 0; i
< map
->n
; ++i
) {
10931 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10935 map
->dim
= isl_space_reset(map
->dim
, type
);
10945 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10950 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10953 bmap
= isl_basic_map_cow(bmap
);
10957 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10961 bmap
= isl_basic_map_finalize(bmap
);
10965 isl_basic_map_free(bmap
);
10969 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10971 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
10974 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10975 __isl_take isl_basic_map
*bmap
)
10980 if (!bmap
->dim
->nested
[0])
10983 bmap
= isl_basic_map_cow(bmap
);
10987 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10991 bmap
= isl_basic_map_finalize(bmap
);
10995 isl_basic_map_free(bmap
);
10999 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11000 __isl_take isl_basic_map
*bmap
)
11005 if (!bmap
->dim
->nested
[1])
11008 bmap
= isl_basic_map_cow(bmap
);
11012 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11016 bmap
= isl_basic_map_finalize(bmap
);
11020 isl_basic_map_free(bmap
);
11024 /* Remove any internal structure from the spaces of domain and range of "map".
11026 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11031 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11034 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11037 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11039 return set_from_map(isl_map_flatten(set_to_map(set
)));
11042 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11044 isl_space
*dim
, *flat_dim
;
11047 dim
= isl_set_get_space(set
);
11048 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11049 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11050 map
= isl_map_intersect_domain(map
, set
);
11055 /* Remove any internal structure from the space of the domain of "map".
11057 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11062 if (!map
->dim
->nested
[0])
11065 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11068 /* Remove any internal structure from the space of the range of "map".
11070 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11075 if (!map
->dim
->nested
[1])
11078 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11081 /* Reorder the dimensions of "bmap" according to the given dim_map
11082 * and set the dimension specification to "dim" and
11083 * perform Gaussian elimination on the result.
11085 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11086 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11088 isl_basic_map
*res
;
11091 bmap
= isl_basic_map_cow(bmap
);
11092 if (!bmap
|| !dim
|| !dim_map
)
11095 flags
= bmap
->flags
;
11096 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11097 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11098 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11099 res
= isl_basic_map_alloc_space(dim
,
11100 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11101 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11103 res
->flags
= flags
;
11104 res
= isl_basic_map_gauss(res
, NULL
);
11105 res
= isl_basic_map_finalize(res
);
11109 isl_basic_map_free(bmap
);
11110 isl_space_free(dim
);
11114 /* Reorder the dimensions of "map" according to given reordering.
11116 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11117 __isl_take isl_reordering
*r
)
11120 struct isl_dim_map
*dim_map
;
11122 map
= isl_map_cow(map
);
11123 dim_map
= isl_dim_map_from_reordering(r
);
11124 if (!map
|| !r
|| !dim_map
)
11127 for (i
= 0; i
< map
->n
; ++i
) {
11128 struct isl_dim_map
*dim_map_i
;
11130 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11132 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11133 isl_space_copy(r
->dim
), dim_map_i
);
11139 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11141 isl_reordering_free(r
);
11147 isl_reordering_free(r
);
11151 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11152 __isl_take isl_reordering
*r
)
11154 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11157 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11158 __isl_take isl_space
*model
)
11162 if (!map
|| !model
)
11165 ctx
= isl_space_get_ctx(model
);
11166 if (!isl_space_has_named_params(model
))
11167 isl_die(ctx
, isl_error_invalid
,
11168 "model has unnamed parameters", goto error
);
11169 if (isl_map_check_named_params(map
) < 0)
11171 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11172 isl_reordering
*exp
;
11174 model
= isl_space_drop_dims(model
, isl_dim_in
,
11175 0, isl_space_dim(model
, isl_dim_in
));
11176 model
= isl_space_drop_dims(model
, isl_dim_out
,
11177 0, isl_space_dim(model
, isl_dim_out
));
11178 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11179 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11180 map
= isl_map_realign(map
, exp
);
11183 isl_space_free(model
);
11186 isl_space_free(model
);
11191 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11192 __isl_take isl_space
*model
)
11194 return isl_map_align_params(set
, model
);
11197 /* Align the parameters of "bmap" to those of "model", introducing
11198 * additional parameters if needed.
11200 __isl_give isl_basic_map
*isl_basic_map_align_params(
11201 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11205 if (!bmap
|| !model
)
11208 ctx
= isl_space_get_ctx(model
);
11209 if (!isl_space_has_named_params(model
))
11210 isl_die(ctx
, isl_error_invalid
,
11211 "model has unnamed parameters", goto error
);
11212 if (!isl_space_has_named_params(bmap
->dim
))
11213 isl_die(ctx
, isl_error_invalid
,
11214 "relation has unnamed parameters", goto error
);
11215 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11216 isl_reordering
*exp
;
11217 struct isl_dim_map
*dim_map
;
11219 model
= isl_space_drop_dims(model
, isl_dim_in
,
11220 0, isl_space_dim(model
, isl_dim_in
));
11221 model
= isl_space_drop_dims(model
, isl_dim_out
,
11222 0, isl_space_dim(model
, isl_dim_out
));
11223 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11224 exp
= isl_reordering_extend_space(exp
,
11225 isl_basic_map_get_space(bmap
));
11226 dim_map
= isl_dim_map_from_reordering(exp
);
11227 bmap
= isl_basic_map_realign(bmap
,
11228 exp
? isl_space_copy(exp
->dim
) : NULL
,
11229 isl_dim_map_extend(dim_map
, bmap
));
11230 isl_reordering_free(exp
);
11234 isl_space_free(model
);
11237 isl_space_free(model
);
11238 isl_basic_map_free(bmap
);
11242 /* Align the parameters of "bset" to those of "model", introducing
11243 * additional parameters if needed.
11245 __isl_give isl_basic_set
*isl_basic_set_align_params(
11246 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11248 return isl_basic_map_align_params(bset
, model
);
11251 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11252 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11253 enum isl_dim_type c2
, enum isl_dim_type c3
,
11254 enum isl_dim_type c4
, enum isl_dim_type c5
)
11256 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11257 struct isl_mat
*mat
;
11263 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11264 isl_basic_map_total_dim(bmap
) + 1);
11267 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11268 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11269 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11270 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11271 isl_int_set(mat
->row
[i
][pos
],
11272 bmap
->eq
[i
][off
+ k
]);
11280 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11281 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11282 enum isl_dim_type c2
, enum isl_dim_type c3
,
11283 enum isl_dim_type c4
, enum isl_dim_type c5
)
11285 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11286 struct isl_mat
*mat
;
11292 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11293 isl_basic_map_total_dim(bmap
) + 1);
11296 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11297 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11298 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11299 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11300 isl_int_set(mat
->row
[i
][pos
],
11301 bmap
->ineq
[i
][off
+ k
]);
11309 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11310 __isl_take isl_space
*dim
,
11311 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11312 enum isl_dim_type c2
, enum isl_dim_type c3
,
11313 enum isl_dim_type c4
, enum isl_dim_type c5
)
11315 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11316 isl_basic_map
*bmap
;
11322 if (!dim
|| !eq
|| !ineq
)
11325 if (eq
->n_col
!= ineq
->n_col
)
11326 isl_die(dim
->ctx
, isl_error_invalid
,
11327 "equalities and inequalities matrices should have "
11328 "same number of columns", goto error
);
11330 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11332 if (eq
->n_col
< total
)
11333 isl_die(dim
->ctx
, isl_error_invalid
,
11334 "number of columns too small", goto error
);
11336 extra
= eq
->n_col
- total
;
11338 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11339 eq
->n_row
, ineq
->n_row
);
11342 for (i
= 0; i
< extra
; ++i
) {
11343 k
= isl_basic_map_alloc_div(bmap
);
11346 isl_int_set_si(bmap
->div
[k
][0], 0);
11348 for (i
= 0; i
< eq
->n_row
; ++i
) {
11349 l
= isl_basic_map_alloc_equality(bmap
);
11352 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11353 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11354 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11355 isl_int_set(bmap
->eq
[l
][off
+ k
],
11361 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11362 l
= isl_basic_map_alloc_inequality(bmap
);
11365 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11366 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11367 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11368 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11369 ineq
->row
[i
][pos
]);
11375 isl_space_free(dim
);
11377 isl_mat_free(ineq
);
11379 bmap
= isl_basic_map_simplify(bmap
);
11380 return isl_basic_map_finalize(bmap
);
11382 isl_space_free(dim
);
11384 isl_mat_free(ineq
);
11388 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11389 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11390 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11392 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11393 c1
, c2
, c3
, c4
, isl_dim_in
);
11396 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11397 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11398 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11400 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11401 c1
, c2
, c3
, c4
, isl_dim_in
);
11404 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11405 __isl_take isl_space
*dim
,
11406 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11407 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11409 isl_basic_map
*bmap
;
11410 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11411 c1
, c2
, c3
, c4
, isl_dim_in
);
11412 return bset_from_bmap(bmap
);
11415 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11418 return isl_bool_error
;
11420 return isl_space_can_zip(bmap
->dim
);
11423 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11426 return isl_bool_error
;
11428 return isl_space_can_zip(map
->dim
);
11431 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11432 * (A -> C) -> (B -> D).
11434 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11443 if (!isl_basic_map_can_zip(bmap
))
11444 isl_die(bmap
->ctx
, isl_error_invalid
,
11445 "basic map cannot be zipped", goto error
);
11446 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11447 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11448 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11449 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11450 bmap
= isl_basic_map_cow(bmap
);
11451 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11454 bmap
->dim
= isl_space_zip(bmap
->dim
);
11457 bmap
= isl_basic_map_mark_final(bmap
);
11460 isl_basic_map_free(bmap
);
11464 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11465 * (A -> C) -> (B -> D).
11467 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11474 if (!isl_map_can_zip(map
))
11475 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11478 map
= isl_map_cow(map
);
11482 for (i
= 0; i
< map
->n
; ++i
) {
11483 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11488 map
->dim
= isl_space_zip(map
->dim
);
11498 /* Can we apply isl_basic_map_curry to "bmap"?
11499 * That is, does it have a nested relation in its domain?
11501 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11504 return isl_bool_error
;
11506 return isl_space_can_curry(bmap
->dim
);
11509 /* Can we apply isl_map_curry to "map"?
11510 * That is, does it have a nested relation in its domain?
11512 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11515 return isl_bool_error
;
11517 return isl_space_can_curry(map
->dim
);
11520 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11523 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11529 if (!isl_basic_map_can_curry(bmap
))
11530 isl_die(bmap
->ctx
, isl_error_invalid
,
11531 "basic map cannot be curried", goto error
);
11532 bmap
= isl_basic_map_cow(bmap
);
11535 bmap
->dim
= isl_space_curry(bmap
->dim
);
11538 bmap
= isl_basic_map_mark_final(bmap
);
11541 isl_basic_map_free(bmap
);
11545 /* Given a map (A -> B) -> C, return the corresponding map
11548 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11550 return isl_map_change_space(map
, &isl_map_can_curry
,
11551 "map cannot be curried", &isl_space_curry
);
11554 /* Can isl_map_range_curry be applied to "map"?
11555 * That is, does it have a nested relation in its range,
11556 * the domain of which is itself a nested relation?
11558 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11561 return isl_bool_error
;
11563 return isl_space_can_range_curry(map
->dim
);
11566 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11567 * A -> (B -> (C -> D)).
11569 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11571 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11572 "map range cannot be curried",
11573 &isl_space_range_curry
);
11576 /* Can we apply isl_basic_map_uncurry to "bmap"?
11577 * That is, does it have a nested relation in its domain?
11579 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11582 return isl_bool_error
;
11584 return isl_space_can_uncurry(bmap
->dim
);
11587 /* Can we apply isl_map_uncurry to "map"?
11588 * That is, does it have a nested relation in its domain?
11590 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11593 return isl_bool_error
;
11595 return isl_space_can_uncurry(map
->dim
);
11598 /* Given a basic map A -> (B -> C), return the corresponding basic map
11601 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11607 if (!isl_basic_map_can_uncurry(bmap
))
11608 isl_die(bmap
->ctx
, isl_error_invalid
,
11609 "basic map cannot be uncurried",
11610 return isl_basic_map_free(bmap
));
11611 bmap
= isl_basic_map_cow(bmap
);
11614 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11616 return isl_basic_map_free(bmap
);
11617 bmap
= isl_basic_map_mark_final(bmap
);
11621 /* Given a map A -> (B -> C), return the corresponding map
11624 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11626 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11627 "map cannot be uncurried", &isl_space_uncurry
);
11630 /* Construct a basic map mapping the domain of the affine expression
11631 * to a one-dimensional range prescribed by the affine expression.
11632 * If "rational" is set, then construct a rational basic map.
11634 * A NaN affine expression cannot be converted to a basic map.
11636 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11637 __isl_take isl_aff
*aff
, int rational
)
11642 isl_local_space
*ls
;
11643 isl_basic_map
*bmap
= NULL
;
11647 is_nan
= isl_aff_is_nan(aff
);
11651 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11652 "cannot convert NaN", goto error
);
11654 ls
= isl_aff_get_local_space(aff
);
11655 bmap
= isl_basic_map_from_local_space(ls
);
11656 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11657 k
= isl_basic_map_alloc_equality(bmap
);
11661 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11662 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11663 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11664 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11665 aff
->v
->size
- (pos
+ 1));
11669 bmap
= isl_basic_map_set_rational(bmap
);
11670 bmap
= isl_basic_map_finalize(bmap
);
11674 isl_basic_map_free(bmap
);
11678 /* Construct a basic map mapping the domain of the affine expression
11679 * to a one-dimensional range prescribed by the affine expression.
11681 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11683 return isl_basic_map_from_aff2(aff
, 0);
11686 /* Construct a map mapping the domain of the affine expression
11687 * to a one-dimensional range prescribed by the affine expression.
11689 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11691 isl_basic_map
*bmap
;
11693 bmap
= isl_basic_map_from_aff(aff
);
11694 return isl_map_from_basic_map(bmap
);
11697 /* Construct a basic map mapping the domain the multi-affine expression
11698 * to its range, with each dimension in the range equated to the
11699 * corresponding affine expression.
11700 * If "rational" is set, then construct a rational basic map.
11702 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11703 __isl_take isl_multi_aff
*maff
, int rational
)
11707 isl_basic_map
*bmap
;
11712 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11713 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11714 "invalid space", goto error
);
11716 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11717 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11719 bmap
= isl_basic_map_set_rational(bmap
);
11721 for (i
= 0; i
< maff
->n
; ++i
) {
11723 isl_basic_map
*bmap_i
;
11725 aff
= isl_aff_copy(maff
->p
[i
]);
11726 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11728 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11731 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11733 isl_multi_aff_free(maff
);
11736 isl_multi_aff_free(maff
);
11740 /* Construct a basic map mapping the domain the multi-affine expression
11741 * to its range, with each dimension in the range equated to the
11742 * corresponding affine expression.
11744 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11745 __isl_take isl_multi_aff
*ma
)
11747 return isl_basic_map_from_multi_aff2(ma
, 0);
11750 /* Construct a map mapping the domain the multi-affine expression
11751 * to its range, with each dimension in the range equated to the
11752 * corresponding affine expression.
11754 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11756 isl_basic_map
*bmap
;
11758 bmap
= isl_basic_map_from_multi_aff(maff
);
11759 return isl_map_from_basic_map(bmap
);
11762 /* Construct a basic map mapping a domain in the given space to
11763 * to an n-dimensional range, with n the number of elements in the list,
11764 * where each coordinate in the range is prescribed by the
11765 * corresponding affine expression.
11766 * The domains of all affine expressions in the list are assumed to match
11769 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11770 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11774 isl_basic_map
*bmap
;
11779 dim
= isl_space_from_domain(domain_dim
);
11780 bmap
= isl_basic_map_universe(dim
);
11782 for (i
= 0; i
< list
->n
; ++i
) {
11784 isl_basic_map
*bmap_i
;
11786 aff
= isl_aff_copy(list
->p
[i
]);
11787 bmap_i
= isl_basic_map_from_aff(aff
);
11789 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11792 isl_aff_list_free(list
);
11796 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11797 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11799 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11802 /* Construct a basic map where the given dimensions are equal to each other.
11804 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11805 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11807 isl_basic_map
*bmap
= NULL
;
11813 if (pos1
>= isl_space_dim(space
, type1
))
11814 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11815 "index out of bounds", goto error
);
11816 if (pos2
>= isl_space_dim(space
, type2
))
11817 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11818 "index out of bounds", goto error
);
11820 if (type1
== type2
&& pos1
== pos2
)
11821 return isl_basic_map_universe(space
);
11823 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11824 i
= isl_basic_map_alloc_equality(bmap
);
11827 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11828 pos1
+= isl_basic_map_offset(bmap
, type1
);
11829 pos2
+= isl_basic_map_offset(bmap
, type2
);
11830 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11831 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11832 bmap
= isl_basic_map_finalize(bmap
);
11833 isl_space_free(space
);
11836 isl_space_free(space
);
11837 isl_basic_map_free(bmap
);
11841 /* Add a constraint imposing that the given two dimensions are equal.
11843 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11844 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11848 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11850 bmap
= isl_basic_map_intersect(bmap
, eq
);
11855 /* Add a constraint imposing that the given two dimensions are equal.
11857 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11858 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11860 isl_basic_map
*bmap
;
11862 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11864 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11869 /* Add a constraint imposing that the given two dimensions have opposite values.
11871 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11872 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11874 isl_basic_map
*bmap
= NULL
;
11880 if (pos1
>= isl_map_dim(map
, type1
))
11881 isl_die(map
->ctx
, isl_error_invalid
,
11882 "index out of bounds", goto error
);
11883 if (pos2
>= isl_map_dim(map
, type2
))
11884 isl_die(map
->ctx
, isl_error_invalid
,
11885 "index out of bounds", goto error
);
11887 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11888 i
= isl_basic_map_alloc_equality(bmap
);
11891 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11892 pos1
+= isl_basic_map_offset(bmap
, type1
);
11893 pos2
+= isl_basic_map_offset(bmap
, type2
);
11894 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11895 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11896 bmap
= isl_basic_map_finalize(bmap
);
11898 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11902 isl_basic_map_free(bmap
);
11907 /* Construct a constraint imposing that the value of the first dimension is
11908 * greater than or equal to that of the second.
11910 static __isl_give isl_constraint
*constraint_order_ge(
11911 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11912 enum isl_dim_type type2
, int pos2
)
11919 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
11921 if (pos1
>= isl_constraint_dim(c
, type1
))
11922 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11923 "index out of bounds", return isl_constraint_free(c
));
11924 if (pos2
>= isl_constraint_dim(c
, type2
))
11925 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11926 "index out of bounds", return isl_constraint_free(c
));
11928 if (type1
== type2
&& pos1
== pos2
)
11931 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11932 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11937 /* Add a constraint imposing that the value of the first dimension is
11938 * greater than or equal to that of the second.
11940 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11941 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11946 if (type1
== type2
&& pos1
== pos2
)
11948 space
= isl_basic_map_get_space(bmap
);
11949 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11950 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11955 /* Add a constraint imposing that the value of the first dimension is
11956 * greater than or equal to that of the second.
11958 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11959 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11964 if (type1
== type2
&& pos1
== pos2
)
11966 space
= isl_map_get_space(map
);
11967 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11968 map
= isl_map_add_constraint(map
, c
);
11973 /* Add a constraint imposing that the value of the first dimension is
11974 * less than or equal to that of the second.
11976 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11977 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11979 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11982 /* Construct a basic map where the value of the first dimension is
11983 * greater than that of the second.
11985 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11986 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11988 isl_basic_map
*bmap
= NULL
;
11994 if (pos1
>= isl_space_dim(space
, type1
))
11995 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11996 "index out of bounds", goto error
);
11997 if (pos2
>= isl_space_dim(space
, type2
))
11998 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11999 "index out of bounds", goto error
);
12001 if (type1
== type2
&& pos1
== pos2
)
12002 return isl_basic_map_empty(space
);
12004 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12005 i
= isl_basic_map_alloc_inequality(bmap
);
12007 return isl_basic_map_free(bmap
);
12008 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12009 pos1
+= isl_basic_map_offset(bmap
, type1
);
12010 pos2
+= isl_basic_map_offset(bmap
, type2
);
12011 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12012 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12013 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12014 bmap
= isl_basic_map_finalize(bmap
);
12018 isl_space_free(space
);
12019 isl_basic_map_free(bmap
);
12023 /* Add a constraint imposing that the value of the first dimension is
12024 * greater than that of the second.
12026 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12027 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12031 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12033 bmap
= isl_basic_map_intersect(bmap
, gt
);
12038 /* Add a constraint imposing that the value of the first dimension is
12039 * greater than that of the second.
12041 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12042 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12044 isl_basic_map
*bmap
;
12046 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12048 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12053 /* Add a constraint imposing that the value of the first dimension is
12054 * smaller than that of the second.
12056 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12057 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12059 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12062 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12066 isl_local_space
*ls
;
12071 if (!isl_basic_map_divs_known(bmap
))
12072 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12073 "some divs are unknown", return NULL
);
12075 ls
= isl_basic_map_get_local_space(bmap
);
12076 div
= isl_local_space_get_div(ls
, pos
);
12077 isl_local_space_free(ls
);
12082 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12085 return isl_basic_map_get_div(bset
, pos
);
12088 /* Plug in "subs" for dimension "type", "pos" of "bset".
12090 * Let i be the dimension to replace and let "subs" be of the form
12094 * Any integer division with a non-zero coefficient for i,
12096 * floor((a i + g)/m)
12100 * floor((a f + d g)/(m d))
12102 * Constraints of the form
12110 * We currently require that "subs" is an integral expression.
12111 * Handling rational expressions may require us to add stride constraints
12112 * as we do in isl_basic_set_preimage_multi_aff.
12114 __isl_give isl_basic_set
*isl_basic_set_substitute(
12115 __isl_take isl_basic_set
*bset
,
12116 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12122 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12125 bset
= isl_basic_set_cow(bset
);
12126 if (!bset
|| !subs
)
12129 ctx
= isl_basic_set_get_ctx(bset
);
12130 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12131 isl_die(ctx
, isl_error_invalid
,
12132 "spaces don't match", goto error
);
12133 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12134 isl_die(ctx
, isl_error_unsupported
,
12135 "cannot handle divs yet", goto error
);
12136 if (!isl_int_is_one(subs
->v
->el
[0]))
12137 isl_die(ctx
, isl_error_invalid
,
12138 "can only substitute integer expressions", goto error
);
12140 pos
+= isl_basic_set_offset(bset
, type
);
12144 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12145 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12147 isl_int_set(v
, bset
->eq
[i
][pos
]);
12148 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12149 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12150 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12153 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12154 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12156 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12157 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12158 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12159 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12162 for (i
= 0; i
< bset
->n_div
; ++i
) {
12163 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12165 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12166 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12167 isl_seq_combine(bset
->div
[i
] + 1,
12168 subs
->v
->el
[0], bset
->div
[i
] + 1,
12169 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12170 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12175 bset
= isl_basic_set_simplify(bset
);
12176 return isl_basic_set_finalize(bset
);
12178 isl_basic_set_free(bset
);
12182 /* Plug in "subs" for dimension "type", "pos" of "set".
12184 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12185 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12189 if (set
&& isl_set_plain_is_empty(set
))
12192 set
= isl_set_cow(set
);
12196 for (i
= set
->n
- 1; i
>= 0; --i
) {
12197 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12198 if (remove_if_empty(set
, i
) < 0)
12208 /* Check if the range of "ma" is compatible with the domain or range
12209 * (depending on "type") of "bmap".
12211 static isl_stat
check_basic_map_compatible_range_multi_aff(
12212 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12213 __isl_keep isl_multi_aff
*ma
)
12216 isl_space
*ma_space
;
12218 ma_space
= isl_multi_aff_get_space(ma
);
12220 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12224 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12225 "parameters don't match", goto error
);
12226 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12230 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12231 "spaces don't match", goto error
);
12233 isl_space_free(ma_space
);
12234 return isl_stat_ok
;
12236 isl_space_free(ma_space
);
12237 return isl_stat_error
;
12240 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12241 * coefficients before the transformed range of dimensions,
12242 * the "n_after" coefficients after the transformed range of dimensions
12243 * and the coefficients of the other divs in "bmap".
12245 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12246 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12251 isl_local_space
*ls
;
12256 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12260 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12261 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12262 for (i
= 0; i
< n_div
; ++i
) {
12263 int o_bmap
= 0, o_ls
= 0;
12265 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12266 o_bmap
+= 1 + 1 + n_param
;
12267 o_ls
+= 1 + 1 + n_param
;
12268 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12269 o_bmap
+= n_before
;
12270 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12271 ls
->div
->row
[i
] + o_ls
, n_set
);
12274 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12276 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12277 ls
->div
->row
[i
] + o_ls
, n_div
);
12280 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12281 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12285 isl_local_space_free(ls
);
12288 isl_local_space_free(ls
);
12292 /* How many stride constraints does "ma" enforce?
12293 * That is, how many of the affine expressions have a denominator
12294 * different from one?
12296 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12301 for (i
= 0; i
< ma
->n
; ++i
)
12302 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12308 /* For each affine expression in ma of the form
12310 * x_i = (f_i y + h_i)/m_i
12312 * with m_i different from one, add a constraint to "bmap"
12315 * f_i y + h_i = m_i alpha_i
12317 * with alpha_i an additional existentially quantified variable.
12319 * The input variables of "ma" correspond to a subset of the variables
12320 * of "bmap". There are "n_before" variables in "bmap" before this
12321 * subset and "n_after" variables after this subset.
12322 * The integer divisions of the affine expressions in "ma" are assumed
12323 * to have been aligned. There are "n_div_ma" of them and
12324 * they appear first in "bmap", straight after the "n_after" variables.
12326 static __isl_give isl_basic_map
*add_ma_strides(
12327 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12328 int n_before
, int n_after
, int n_div_ma
)
12336 total
= isl_basic_map_total_dim(bmap
);
12337 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12338 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12339 for (i
= 0; i
< ma
->n
; ++i
) {
12340 int o_bmap
= 0, o_ma
= 1;
12342 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12344 div
= isl_basic_map_alloc_div(bmap
);
12345 k
= isl_basic_map_alloc_equality(bmap
);
12346 if (div
< 0 || k
< 0)
12348 isl_int_set_si(bmap
->div
[div
][0], 0);
12349 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12350 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12351 o_bmap
+= 1 + n_param
;
12352 o_ma
+= 1 + n_param
;
12353 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12354 o_bmap
+= n_before
;
12355 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12356 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12359 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12361 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12362 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12363 o_bmap
+= n_div_ma
;
12365 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12366 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12372 isl_basic_map_free(bmap
);
12376 /* Replace the domain or range space (depending on "type) of "space" by "set".
12378 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12379 enum isl_dim_type type
, __isl_take isl_space
*set
)
12381 if (type
== isl_dim_in
) {
12382 space
= isl_space_range(space
);
12383 space
= isl_space_map_from_domain_and_range(set
, space
);
12385 space
= isl_space_domain(space
);
12386 space
= isl_space_map_from_domain_and_range(space
, set
);
12392 /* Compute the preimage of the domain or range (depending on "type")
12393 * of "bmap" under the function represented by "ma".
12394 * In other words, plug in "ma" in the domain or range of "bmap".
12395 * The result is a basic map that lives in the same space as "bmap"
12396 * except that the domain or range has been replaced by
12397 * the domain space of "ma".
12399 * If bmap is represented by
12401 * A(p) + S u + B x + T v + C(divs) >= 0,
12403 * where u and x are input and output dimensions if type == isl_dim_out
12404 * while x and v are input and output dimensions if type == isl_dim_in,
12405 * and ma is represented by
12407 * x = D(p) + F(y) + G(divs')
12409 * then the result is
12411 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12413 * The divs in the input set are similarly adjusted.
12416 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12420 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12421 * B_i G(divs') + c_i(divs))/n_i)
12423 * If bmap is not a rational map and if F(y) involves any denominators
12425 * x_i = (f_i y + h_i)/m_i
12427 * then additional constraints are added to ensure that we only
12428 * map back integer points. That is we enforce
12430 * f_i y + h_i = m_i alpha_i
12432 * with alpha_i an additional existentially quantified variable.
12434 * We first copy over the divs from "ma".
12435 * Then we add the modified constraints and divs from "bmap".
12436 * Finally, we add the stride constraints, if needed.
12438 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12439 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12440 __isl_take isl_multi_aff
*ma
)
12444 isl_basic_map
*res
= NULL
;
12445 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12446 isl_int f
, c1
, c2
, g
;
12455 ma
= isl_multi_aff_align_divs(ma
);
12458 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12461 if (type
== isl_dim_in
) {
12463 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12465 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12468 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12469 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12471 space
= isl_multi_aff_get_domain_space(ma
);
12472 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12473 rational
= isl_basic_map_is_rational(bmap
);
12474 strides
= rational
? 0 : multi_aff_strides(ma
);
12475 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12476 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12478 res
= isl_basic_map_set_rational(res
);
12480 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12481 if (isl_basic_map_alloc_div(res
) < 0)
12484 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12487 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12488 k
= isl_basic_map_alloc_equality(res
);
12491 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12492 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12495 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12496 k
= isl_basic_map_alloc_inequality(res
);
12499 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12500 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12503 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12504 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12505 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12508 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12509 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12514 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12520 isl_basic_map_free(bmap
);
12521 isl_multi_aff_free(ma
);
12522 res
= isl_basic_map_simplify(res
);
12523 return isl_basic_map_finalize(res
);
12529 isl_basic_map_free(bmap
);
12530 isl_multi_aff_free(ma
);
12531 isl_basic_map_free(res
);
12535 /* Compute the preimage of "bset" under the function represented by "ma".
12536 * In other words, plug in "ma" in "bset". The result is a basic set
12537 * that lives in the domain space of "ma".
12539 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12540 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12542 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12545 /* Compute the preimage of the domain of "bmap" under the function
12546 * represented by "ma".
12547 * In other words, plug in "ma" in the domain of "bmap".
12548 * The result is a basic map that lives in the same space as "bmap"
12549 * except that the domain has been replaced by the domain space of "ma".
12551 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12552 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12554 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12557 /* Compute the preimage of the range of "bmap" under the function
12558 * represented by "ma".
12559 * In other words, plug in "ma" in the range of "bmap".
12560 * The result is a basic map that lives in the same space as "bmap"
12561 * except that the range has been replaced by the domain space of "ma".
12563 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12564 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12566 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12569 /* Check if the range of "ma" is compatible with the domain or range
12570 * (depending on "type") of "map".
12571 * Return isl_stat_error if anything is wrong.
12573 static isl_stat
check_map_compatible_range_multi_aff(
12574 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12575 __isl_keep isl_multi_aff
*ma
)
12578 isl_space
*ma_space
;
12580 ma_space
= isl_multi_aff_get_space(ma
);
12581 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12582 isl_space_free(ma_space
);
12584 return isl_stat_error
;
12586 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12587 "spaces don't match", return isl_stat_error
);
12588 return isl_stat_ok
;
12591 /* Compute the preimage of the domain or range (depending on "type")
12592 * of "map" under the function represented by "ma".
12593 * In other words, plug in "ma" in the domain or range of "map".
12594 * The result is a map that lives in the same space as "map"
12595 * except that the domain or range has been replaced by
12596 * the domain space of "ma".
12598 * The parameters are assumed to have been aligned.
12600 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12601 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12606 map
= isl_map_cow(map
);
12607 ma
= isl_multi_aff_align_divs(ma
);
12610 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12613 for (i
= 0; i
< map
->n
; ++i
) {
12614 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12615 isl_multi_aff_copy(ma
));
12620 space
= isl_multi_aff_get_domain_space(ma
);
12621 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12623 isl_space_free(map
->dim
);
12628 isl_multi_aff_free(ma
);
12630 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12631 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12634 isl_multi_aff_free(ma
);
12639 /* Compute the preimage of the domain or range (depending on "type")
12640 * of "map" under the function represented by "ma".
12641 * In other words, plug in "ma" in the domain or range of "map".
12642 * The result is a map that lives in the same space as "map"
12643 * except that the domain or range has been replaced by
12644 * the domain space of "ma".
12646 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12647 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12652 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12653 return map_preimage_multi_aff(map
, type
, ma
);
12655 if (isl_map_check_named_params(map
) < 0)
12657 if (!isl_space_has_named_params(ma
->space
))
12658 isl_die(map
->ctx
, isl_error_invalid
,
12659 "unaligned unnamed parameters", goto error
);
12660 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12661 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12663 return map_preimage_multi_aff(map
, type
, ma
);
12665 isl_multi_aff_free(ma
);
12666 return isl_map_free(map
);
12669 /* Compute the preimage of "set" under the function represented by "ma".
12670 * In other words, plug in "ma" in "set". The result is a set
12671 * that lives in the domain space of "ma".
12673 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12674 __isl_take isl_multi_aff
*ma
)
12676 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12679 /* Compute the preimage of the domain of "map" under the function
12680 * represented by "ma".
12681 * In other words, plug in "ma" in the domain of "map".
12682 * The result is a map that lives in the same space as "map"
12683 * except that the domain has been replaced by the domain space of "ma".
12685 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12686 __isl_take isl_multi_aff
*ma
)
12688 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12691 /* Compute the preimage of the range of "map" under the function
12692 * represented by "ma".
12693 * In other words, plug in "ma" in the range of "map".
12694 * The result is a map that lives in the same space as "map"
12695 * except that the range has been replaced by the domain space of "ma".
12697 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12698 __isl_take isl_multi_aff
*ma
)
12700 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12703 /* Compute the preimage of "map" under the function represented by "pma".
12704 * In other words, plug in "pma" in the domain or range of "map".
12705 * The result is a map that lives in the same space as "map",
12706 * except that the space of type "type" has been replaced by
12707 * the domain space of "pma".
12709 * The parameters of "map" and "pma" are assumed to have been aligned.
12711 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12712 __isl_take isl_map
*map
, enum isl_dim_type type
,
12713 __isl_take isl_pw_multi_aff
*pma
)
12722 isl_pw_multi_aff_free(pma
);
12723 res
= isl_map_empty(isl_map_get_space(map
));
12728 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12729 isl_multi_aff_copy(pma
->p
[0].maff
));
12730 if (type
== isl_dim_in
)
12731 res
= isl_map_intersect_domain(res
,
12732 isl_map_copy(pma
->p
[0].set
));
12734 res
= isl_map_intersect_range(res
,
12735 isl_map_copy(pma
->p
[0].set
));
12737 for (i
= 1; i
< pma
->n
; ++i
) {
12740 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12741 isl_multi_aff_copy(pma
->p
[i
].maff
));
12742 if (type
== isl_dim_in
)
12743 res_i
= isl_map_intersect_domain(res_i
,
12744 isl_map_copy(pma
->p
[i
].set
));
12746 res_i
= isl_map_intersect_range(res_i
,
12747 isl_map_copy(pma
->p
[i
].set
));
12748 res
= isl_map_union(res
, res_i
);
12751 isl_pw_multi_aff_free(pma
);
12755 isl_pw_multi_aff_free(pma
);
12760 /* Compute the preimage of "map" under the function represented by "pma".
12761 * In other words, plug in "pma" in the domain or range of "map".
12762 * The result is a map that lives in the same space as "map",
12763 * except that the space of type "type" has been replaced by
12764 * the domain space of "pma".
12766 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12767 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12772 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12773 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12775 if (isl_map_check_named_params(map
) < 0)
12777 if (!isl_space_has_named_params(pma
->dim
))
12778 isl_die(map
->ctx
, isl_error_invalid
,
12779 "unaligned unnamed parameters", goto error
);
12780 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12781 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12783 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12785 isl_pw_multi_aff_free(pma
);
12786 return isl_map_free(map
);
12789 /* Compute the preimage of "set" under the function represented by "pma".
12790 * In other words, plug in "pma" in "set". The result is a set
12791 * that lives in the domain space of "pma".
12793 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12794 __isl_take isl_pw_multi_aff
*pma
)
12796 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12799 /* Compute the preimage of the domain of "map" under the function
12800 * represented by "pma".
12801 * In other words, plug in "pma" in the domain of "map".
12802 * The result is a map that lives in the same space as "map",
12803 * except that domain space has been replaced by the domain space of "pma".
12805 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12806 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12808 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12811 /* Compute the preimage of the range of "map" under the function
12812 * represented by "pma".
12813 * In other words, plug in "pma" in the range of "map".
12814 * The result is a map that lives in the same space as "map",
12815 * except that range space has been replaced by the domain space of "pma".
12817 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12818 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12820 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12823 /* Compute the preimage of "map" under the function represented by "mpa".
12824 * In other words, plug in "mpa" in the domain or range of "map".
12825 * The result is a map that lives in the same space as "map",
12826 * except that the space of type "type" has been replaced by
12827 * the domain space of "mpa".
12829 * If the map does not involve any constraints that refer to the
12830 * dimensions of the substituted space, then the only possible
12831 * effect of "mpa" on the map is to map the space to a different space.
12832 * We create a separate isl_multi_aff to effectuate this change
12833 * in order to avoid spurious splitting of the map along the pieces
12836 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12837 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12840 isl_pw_multi_aff
*pma
;
12845 n
= isl_map_dim(map
, type
);
12846 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12850 space
= isl_multi_pw_aff_get_space(mpa
);
12851 isl_multi_pw_aff_free(mpa
);
12852 ma
= isl_multi_aff_zero(space
);
12853 return isl_map_preimage_multi_aff(map
, type
, ma
);
12856 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12857 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12860 isl_multi_pw_aff_free(mpa
);
12864 /* Compute the preimage of "map" under the function represented by "mpa".
12865 * In other words, plug in "mpa" in the domain "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that domain space has been replaced by the domain space of "mpa".
12869 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12870 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12872 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12875 /* Compute the preimage of "set" by the function represented by "mpa".
12876 * In other words, plug in "mpa" in "set".
12878 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12879 __isl_take isl_multi_pw_aff
*mpa
)
12881 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
12884 /* Are the "n" "coefficients" starting at "first" of the integer division
12885 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12887 * The "coefficient" at position 0 is the denominator.
12888 * The "coefficient" at position 1 is the constant term.
12890 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
12891 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
12892 unsigned first
, unsigned n
)
12894 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
12895 return isl_bool_error
;
12896 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
12897 return isl_bool_error
;
12898 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
12899 bmap2
->div
[pos2
] + first
, n
);
12902 /* Are the integer division expressions at position "pos1" in "bmap1" and
12903 * "pos2" in "bmap2" equal to each other, except that the constant terms
12906 isl_bool
isl_basic_map_equal_div_expr_except_constant(
12907 __isl_keep isl_basic_map
*bmap1
, int pos1
,
12908 __isl_keep isl_basic_map
*bmap2
, int pos2
)
12913 if (!bmap1
|| !bmap2
)
12914 return isl_bool_error
;
12915 total
= isl_basic_map_total_dim(bmap1
);
12916 if (total
!= isl_basic_map_total_dim(bmap2
))
12917 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
12918 "incomparable div expressions", return isl_bool_error
);
12919 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12921 if (equal
< 0 || !equal
)
12923 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12925 if (equal
< 0 || equal
)
12926 return isl_bool_not(equal
);
12927 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12931 /* Replace the numerator of the constant term of the integer division
12932 * expression at position "div" in "bmap" by "value".
12933 * The caller guarantees that this does not change the meaning
12936 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
12937 __isl_take isl_basic_map
*bmap
, int div
, int value
)
12939 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
12940 return isl_basic_map_free(bmap
);
12942 isl_int_set_si(bmap
->div
[div
][1], value
);
12947 /* Is the point "inner" internal to inequality constraint "ineq"
12949 * The point is considered to be internal to the inequality constraint,
12950 * if it strictly lies on the positive side of the inequality constraint,
12951 * or if it lies on the constraint and the constraint is lexico-positive.
12953 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
12954 __isl_keep isl_basic_set
*bset
, int ineq
)
12960 if (!inner
|| !bset
)
12961 return isl_bool_error
;
12963 ctx
= isl_basic_set_get_ctx(bset
);
12964 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
12965 &ctx
->normalize_gcd
);
12966 if (!isl_int_is_zero(ctx
->normalize_gcd
))
12967 return isl_int_is_nonneg(ctx
->normalize_gcd
);
12969 total
= isl_basic_set_dim(bset
, isl_dim_all
);
12970 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
12971 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
12974 /* Tighten the inequality constraints of "bset" that are outward with respect
12975 * to the point "vec".
12976 * That is, tighten the constraints that are not satisfied by "vec".
12978 * "vec" is a point internal to some superset S of "bset" that is used
12979 * to make the subsets of S disjoint, by tightening one half of the constraints
12980 * that separate two subsets. In particular, the constraints of S
12981 * are all satisfied by "vec" and should not be tightened.
12982 * Of the internal constraints, those that have "vec" on the outside
12983 * are tightened. The shared facet is included in the adjacent subset
12984 * with the opposite constraint.
12985 * For constraints that saturate "vec", this criterion cannot be used
12986 * to determine which of the two sides should be tightened.
12987 * Instead, the sign of the first non-zero coefficient is used
12988 * to make this choice. Note that this second criterion is never used
12989 * on the constraints of S since "vec" is interior to "S".
12991 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
12992 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
12996 bset
= isl_basic_set_cow(bset
);
12999 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13002 internal
= is_internal(vec
, bset
, j
);
13004 return isl_basic_set_free(bset
);
13007 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);