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 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 #include <bset_to_bmap.c>
47 #include <bset_from_bmap.c>
48 #include <set_to_map.c>
49 #include <set_from_map.c>
51 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
54 case isl_dim_param
: return dim
->nparam
;
55 case isl_dim_in
: return dim
->n_in
;
56 case isl_dim_out
: return dim
->n_out
;
57 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
62 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
65 case isl_dim_param
: return 1;
66 case isl_dim_in
: return 1 + dim
->nparam
;
67 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
72 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
73 enum isl_dim_type type
)
78 case isl_dim_cst
: return 1;
81 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
82 case isl_dim_div
: return bmap
->n_div
;
83 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
88 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
90 return map
? n(map
->dim
, type
) : 0;
93 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
95 return set
? n(set
->dim
, type
) : 0;
98 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
99 enum isl_dim_type type
)
108 case isl_dim_cst
: return 0;
109 case isl_dim_param
: return 1;
110 case isl_dim_in
: return 1 + space
->nparam
;
111 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
112 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
118 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
119 enum isl_dim_type type
)
121 return isl_basic_map_offset(bset
, type
);
124 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
126 return pos(map
->dim
, type
);
129 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
130 enum isl_dim_type type
)
132 return isl_basic_map_dim(bset
, type
);
135 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
137 return isl_basic_set_dim(bset
, isl_dim_set
);
140 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
142 return isl_basic_set_dim(bset
, isl_dim_param
);
145 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
149 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
152 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
154 return isl_set_dim(set
, isl_dim_set
);
157 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
159 return isl_set_dim(set
, isl_dim_param
);
162 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
164 return bmap
? bmap
->dim
->n_in
: 0;
167 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
169 return bmap
? bmap
->dim
->n_out
: 0;
172 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->nparam
: 0;
177 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
179 return bmap
? bmap
->n_div
: 0;
182 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
184 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
187 unsigned isl_map_n_in(const struct isl_map
*map
)
189 return map
? map
->dim
->n_in
: 0;
192 unsigned isl_map_n_out(const struct isl_map
*map
)
194 return map
? map
->dim
->n_out
: 0;
197 unsigned isl_map_n_param(const struct isl_map
*map
)
199 return map
? map
->dim
->nparam
: 0;
202 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
207 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
210 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
211 set
->dim
, isl_dim_set
);
214 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
215 __isl_keep isl_basic_set
*bset
)
219 return isl_bool_error
;
220 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
223 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
224 bset
->dim
, isl_dim_set
);
227 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
232 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
235 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
236 set
->dim
, isl_dim_set
);
239 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
240 struct isl_basic_set
*bset
)
245 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
248 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
249 bset
->dim
, isl_dim_set
);
252 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
254 return bmap
? bmap
->ctx
: NULL
;
257 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
259 return bset
? bset
->ctx
: NULL
;
262 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
264 return map
? map
->ctx
: NULL
;
267 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
269 return set
? set
->ctx
: NULL
;
272 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
276 return isl_space_copy(bmap
->dim
);
279 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
283 return isl_space_copy(bset
->dim
);
286 /* Extract the divs in "bmap" as a matrix.
288 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
299 ctx
= isl_basic_map_get_ctx(bmap
);
300 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
301 cols
= 1 + 1 + total
+ bmap
->n_div
;
302 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
306 for (i
= 0; i
< bmap
->n_div
; ++i
)
307 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
312 /* Extract the divs in "bset" as a matrix.
314 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
316 return isl_basic_map_get_divs(bset
);
319 __isl_give isl_local_space
*isl_basic_map_get_local_space(
320 __isl_keep isl_basic_map
*bmap
)
327 div
= isl_basic_map_get_divs(bmap
);
328 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
331 __isl_give isl_local_space
*isl_basic_set_get_local_space(
332 __isl_keep isl_basic_set
*bset
)
334 return isl_basic_map_get_local_space(bset
);
337 /* For each known div d = floor(f/m), add the constraints
340 * -(f-(m-1)) + m d >= 0
342 * Do not finalize the result.
344 static __isl_give isl_basic_map
*add_known_div_constraints(
345 __isl_take isl_basic_map
*bmap
)
352 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
355 bmap
= isl_basic_map_cow(bmap
);
356 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
359 for (i
= 0; i
< n_div
; ++i
) {
360 if (isl_int_is_zero(bmap
->div
[i
][0]))
362 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
363 return isl_basic_map_free(bmap
);
369 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
370 __isl_take isl_local_space
*ls
)
379 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
380 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
381 n_div
, 0, 2 * n_div
);
383 for (i
= 0; i
< n_div
; ++i
)
384 if (isl_basic_map_alloc_div(bmap
) < 0)
387 for (i
= 0; i
< n_div
; ++i
)
388 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
389 bmap
= add_known_div_constraints(bmap
);
391 isl_local_space_free(ls
);
394 isl_local_space_free(ls
);
395 isl_basic_map_free(bmap
);
399 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
400 __isl_take isl_local_space
*ls
)
402 return isl_basic_map_from_local_space(ls
);
405 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
409 return isl_space_copy(map
->dim
);
412 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
416 return isl_space_copy(set
->dim
);
419 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
420 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
422 bmap
= isl_basic_map_cow(bmap
);
425 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
428 bmap
= isl_basic_map_finalize(bmap
);
431 isl_basic_map_free(bmap
);
435 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
436 __isl_take isl_basic_set
*bset
, const char *s
)
438 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
441 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
442 enum isl_dim_type type
)
444 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
447 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
448 enum isl_dim_type type
, const char *s
)
452 map
= isl_map_cow(map
);
456 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
460 for (i
= 0; i
< map
->n
; ++i
) {
461 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
472 /* Replace the identifier of the tuple of type "type" by "id".
474 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
475 __isl_take isl_basic_map
*bmap
,
476 enum isl_dim_type type
, __isl_take isl_id
*id
)
478 bmap
= isl_basic_map_cow(bmap
);
481 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
483 return isl_basic_map_free(bmap
);
484 bmap
= isl_basic_map_finalize(bmap
);
491 /* Replace the identifier of the tuple by "id".
493 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
494 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
496 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
499 /* Does the input or output tuple have a name?
501 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
503 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
506 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
507 enum isl_dim_type type
)
509 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
512 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
515 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
519 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
520 enum isl_dim_type type
, __isl_take isl_id
*id
)
522 map
= isl_map_cow(map
);
526 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
528 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
534 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
535 __isl_take isl_id
*id
)
537 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
540 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
541 enum isl_dim_type type
)
543 map
= isl_map_cow(map
);
547 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
549 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
552 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
554 return isl_map_reset_tuple_id(set
, isl_dim_set
);
557 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
559 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
562 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
563 enum isl_dim_type type
)
565 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
568 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
570 return isl_map_has_tuple_id(set
, isl_dim_set
);
573 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
575 return isl_map_get_tuple_id(set
, isl_dim_set
);
578 /* Does the set tuple have a name?
580 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
583 return isl_bool_error
;
584 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
588 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
590 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
593 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
595 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
598 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
599 enum isl_dim_type type
, unsigned pos
)
601 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
604 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
605 enum isl_dim_type type
, unsigned pos
)
607 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
610 /* Does the given dimension have a name?
612 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
613 enum isl_dim_type type
, unsigned pos
)
616 return isl_bool_error
;
617 return isl_space_has_dim_name(map
->dim
, type
, pos
);
620 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
621 enum isl_dim_type type
, unsigned pos
)
623 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
626 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
627 enum isl_dim_type type
, unsigned pos
)
629 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
632 /* Does the given dimension have a name?
634 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
635 enum isl_dim_type type
, unsigned pos
)
638 return isl_bool_error
;
639 return isl_space_has_dim_name(set
->dim
, type
, pos
);
642 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
643 __isl_take isl_basic_map
*bmap
,
644 enum isl_dim_type type
, unsigned pos
, const char *s
)
646 bmap
= isl_basic_map_cow(bmap
);
649 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
652 return isl_basic_map_finalize(bmap
);
654 isl_basic_map_free(bmap
);
658 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
659 enum isl_dim_type type
, unsigned pos
, const char *s
)
663 map
= isl_map_cow(map
);
667 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
671 for (i
= 0; i
< map
->n
; ++i
) {
672 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
683 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
684 __isl_take isl_basic_set
*bset
,
685 enum isl_dim_type type
, unsigned pos
, const char *s
)
687 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
691 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
692 enum isl_dim_type type
, unsigned pos
, const char *s
)
694 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
698 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
699 enum isl_dim_type type
, unsigned pos
)
702 return isl_bool_error
;
703 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
706 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
707 enum isl_dim_type type
, unsigned pos
)
709 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
712 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
713 enum isl_dim_type type
, unsigned pos
)
715 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
718 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
719 enum isl_dim_type type
, unsigned pos
)
721 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
724 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
725 enum isl_dim_type type
, unsigned pos
)
727 return isl_map_has_dim_id(set
, type
, pos
);
730 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
731 enum isl_dim_type type
, unsigned pos
)
733 return isl_map_get_dim_id(set
, type
, pos
);
736 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
737 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
739 map
= isl_map_cow(map
);
743 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
745 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
751 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
752 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
754 return isl_map_set_dim_id(set
, type
, pos
, id
);
757 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
758 __isl_keep isl_id
*id
)
762 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
765 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
766 __isl_keep isl_id
*id
)
768 return isl_map_find_dim_by_id(set
, type
, id
);
771 /* Return the position of the dimension of the given type and name
773 * Return -1 if no such dimension can be found.
775 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
776 enum isl_dim_type type
, const char *name
)
780 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
783 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
788 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
791 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
794 return isl_map_find_dim_by_name(set
, type
, name
);
797 /* Reset the user pointer on all identifiers of parameters and tuples
798 * of the space of "map".
800 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
804 space
= isl_map_get_space(map
);
805 space
= isl_space_reset_user(space
);
806 map
= isl_map_reset_space(map
, space
);
811 /* Reset the user pointer on all identifiers of parameters and tuples
812 * of the space of "set".
814 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
816 return isl_map_reset_user(set
);
819 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
823 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
826 /* Has "map" been marked as a rational map?
827 * In particular, have all basic maps in "map" been marked this way?
828 * An empty map is not considered to be rational.
829 * Maps where only some of the basic maps are marked rational
832 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
838 return isl_bool_error
;
840 return isl_bool_false
;
841 rational
= isl_basic_map_is_rational(map
->p
[0]);
844 for (i
= 1; i
< map
->n
; ++i
) {
847 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
850 if (rational
!= rational_i
)
851 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
852 "mixed rational and integer basic maps "
853 "not supported", return isl_bool_error
);
859 /* Has "set" been marked as a rational set?
860 * In particular, have all basic set in "set" been marked this way?
861 * An empty set is not considered to be rational.
862 * Sets where only some of the basic sets are marked rational
865 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
867 return isl_map_is_rational(set
);
870 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
872 return isl_basic_map_is_rational(bset
);
875 /* Does "bmap" contain any rational points?
877 * If "bmap" has an equality for each dimension, equating the dimension
878 * to an integer constant, then it has no rational points, even if it
879 * is marked as rational.
881 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
883 int has_rational
= 1;
888 if (isl_basic_map_plain_is_empty(bmap
))
890 if (!isl_basic_map_is_rational(bmap
))
892 bmap
= isl_basic_map_copy(bmap
);
893 bmap
= isl_basic_map_implicit_equalities(bmap
);
896 total
= isl_basic_map_total_dim(bmap
);
897 if (bmap
->n_eq
== total
) {
899 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
900 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
903 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
904 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
906 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
914 isl_basic_map_free(bmap
);
919 /* Does "map" contain any rational points?
921 int isl_map_has_rational(__isl_keep isl_map
*map
)
928 for (i
= 0; i
< map
->n
; ++i
) {
929 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
930 if (has_rational
< 0)
938 /* Does "set" contain any rational points?
940 int isl_set_has_rational(__isl_keep isl_set
*set
)
942 return isl_map_has_rational(set
);
945 /* Is this basic set a parameter domain?
947 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
951 return isl_space_is_params(bset
->dim
);
954 /* Is this set a parameter domain?
956 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
959 return isl_bool_error
;
960 return isl_space_is_params(set
->dim
);
963 /* Is this map actually a parameter domain?
964 * Users should never call this function. Outside of isl,
965 * a map can never be a parameter domain.
967 int isl_map_is_params(__isl_keep isl_map
*map
)
971 return isl_space_is_params(map
->dim
);
974 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
975 struct isl_basic_map
*bmap
, unsigned extra
,
976 unsigned n_eq
, unsigned n_ineq
)
979 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
984 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
985 if (isl_blk_is_error(bmap
->block
))
988 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
989 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
993 bmap
->block2
= isl_blk_empty();
996 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
997 if (isl_blk_is_error(bmap
->block2
))
1000 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1005 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1006 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1008 for (i
= 0; i
< extra
; ++i
)
1009 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1013 bmap
->c_size
= n_eq
+ n_ineq
;
1014 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1015 bmap
->extra
= extra
;
1019 bmap
->sample
= NULL
;
1023 isl_basic_map_free(bmap
);
1027 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1028 unsigned nparam
, unsigned dim
, unsigned extra
,
1029 unsigned n_eq
, unsigned n_ineq
)
1031 struct isl_basic_map
*bmap
;
1034 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1038 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1039 return bset_from_bmap(bmap
);
1042 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1043 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1045 struct isl_basic_map
*bmap
;
1048 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1049 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1050 return bset_from_bmap(bmap
);
1052 isl_space_free(dim
);
1056 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1057 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1059 struct isl_basic_map
*bmap
;
1063 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1068 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1070 isl_space_free(dim
);
1074 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1075 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1076 unsigned n_eq
, unsigned n_ineq
)
1078 struct isl_basic_map
*bmap
;
1081 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1085 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1089 static void dup_constraints(
1090 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1093 unsigned total
= isl_basic_map_total_dim(src
);
1095 for (i
= 0; i
< src
->n_eq
; ++i
) {
1096 int j
= isl_basic_map_alloc_equality(dst
);
1097 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1100 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1101 int j
= isl_basic_map_alloc_inequality(dst
);
1102 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1105 for (i
= 0; i
< src
->n_div
; ++i
) {
1106 int j
= isl_basic_map_alloc_div(dst
);
1107 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1109 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1112 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1114 struct isl_basic_map
*dup
;
1118 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1119 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1122 dup_constraints(dup
, bmap
);
1123 dup
->flags
= bmap
->flags
;
1124 dup
->sample
= isl_vec_copy(bmap
->sample
);
1128 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1130 struct isl_basic_map
*dup
;
1132 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1133 return bset_from_bmap(dup
);
1136 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1141 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1145 return isl_basic_set_dup(bset
);
1148 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1157 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1162 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1166 bmap
= isl_basic_map_dup(bmap
);
1168 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1172 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1181 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1186 if (--bmap
->ref
> 0)
1189 isl_ctx_deref(bmap
->ctx
);
1191 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1193 isl_blk_free(bmap
->ctx
, bmap
->block
);
1194 isl_vec_free(bmap
->sample
);
1195 isl_space_free(bmap
->dim
);
1201 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1203 return isl_basic_map_free(bset_to_bmap(bset
));
1206 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1208 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1211 __isl_give isl_map
*isl_map_align_params_map_map_and(
1212 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1213 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1214 __isl_take isl_map
*map2
))
1218 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1219 return fn(map1
, map2
);
1220 if (!isl_space_has_named_params(map1
->dim
) ||
1221 !isl_space_has_named_params(map2
->dim
))
1222 isl_die(map1
->ctx
, isl_error_invalid
,
1223 "unaligned unnamed parameters", goto error
);
1224 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1225 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1226 return fn(map1
, map2
);
1233 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1234 __isl_keep isl_map
*map2
,
1235 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1240 return isl_bool_error
;
1241 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1242 return fn(map1
, map2
);
1243 if (!isl_space_has_named_params(map1
->dim
) ||
1244 !isl_space_has_named_params(map2
->dim
))
1245 isl_die(map1
->ctx
, isl_error_invalid
,
1246 "unaligned unnamed parameters", return isl_bool_error
);
1247 map1
= isl_map_copy(map1
);
1248 map2
= isl_map_copy(map2
);
1249 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1250 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1257 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1259 struct isl_ctx
*ctx
;
1263 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1264 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1266 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1267 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1268 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1269 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1271 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1273 int j
= isl_basic_map_alloc_inequality(bmap
);
1277 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1278 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1285 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1286 bmap
->extra
- bmap
->n_div
);
1287 return bmap
->n_eq
++;
1290 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1292 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1295 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1299 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1304 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1306 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1309 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1314 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1316 if (pos
!= bmap
->n_eq
- 1) {
1318 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1319 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1325 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1327 return isl_basic_map_drop_equality(bset_to_bmap(bset
), pos
);
1330 /* Turn inequality "pos" of "bmap" into an equality.
1332 * In particular, we move the inequality in front of the equalities
1333 * and move the last inequality in the position of the moved inequality.
1334 * Note that isl_tab_make_equalities_explicit depends on this particular
1335 * change in the ordering of the constraints.
1337 void isl_basic_map_inequality_to_equality(
1338 struct isl_basic_map
*bmap
, unsigned pos
)
1342 t
= bmap
->ineq
[pos
];
1343 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1344 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1349 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1350 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1351 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1355 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1357 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1360 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1362 struct isl_ctx
*ctx
;
1366 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1367 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1368 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1369 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1371 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1372 1 + isl_basic_map_total_dim(bmap
),
1373 bmap
->extra
- bmap
->n_div
);
1374 return bmap
->n_ineq
++;
1377 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1379 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1382 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1386 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1391 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1393 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1396 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1401 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1403 if (pos
!= bmap
->n_ineq
- 1) {
1404 t
= bmap
->ineq
[pos
];
1405 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1406 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1407 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1413 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1415 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1418 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1423 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1426 k
= isl_basic_map_alloc_equality(bmap
);
1429 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1432 isl_basic_map_free(bmap
);
1436 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1439 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1442 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1447 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1450 k
= isl_basic_map_alloc_inequality(bmap
);
1453 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1456 isl_basic_map_free(bmap
);
1460 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1463 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1466 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1470 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1471 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1472 1 + 1 + isl_basic_map_total_dim(bmap
),
1473 bmap
->extra
- bmap
->n_div
);
1474 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1475 return bmap
->n_div
++;
1478 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1480 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1483 /* Insert an extra integer division, prescribed by "div", to "bmap"
1484 * at (integer division) position "pos".
1486 * The integer division is first added at the end and then moved
1487 * into the right position.
1489 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1490 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1494 bmap
= isl_basic_map_cow(bmap
);
1496 return isl_basic_map_free(bmap
);
1498 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1499 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1500 "unexpected size", return isl_basic_map_free(bmap
));
1501 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1502 if (pos
< 0 || pos
> n_div
)
1503 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1504 "invalid position", return isl_basic_map_free(bmap
));
1506 bmap
= isl_basic_map_extend_space(bmap
,
1507 isl_basic_map_get_space(bmap
), 1, 0, 2);
1508 k
= isl_basic_map_alloc_div(bmap
);
1510 return isl_basic_map_free(bmap
);
1511 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1512 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1514 for (i
= k
; i
> pos
; --i
)
1515 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1520 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1524 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1529 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1531 return isl_basic_map_free_div(bset_to_bmap(bset
), n
);
1534 /* Copy constraint from src to dst, putting the vars of src at offset
1535 * dim_off in dst and the divs of src at offset div_off in dst.
1536 * If both sets are actually map, then dim_off applies to the input
1539 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1540 struct isl_basic_map
*src_map
, isl_int
*src
,
1541 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1543 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1544 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1545 unsigned src_in
= isl_basic_map_n_in(src_map
);
1546 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1547 unsigned src_out
= isl_basic_map_n_out(src_map
);
1548 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1549 isl_int_set(dst
[0], src
[0]);
1550 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1551 if (dst_nparam
> src_nparam
)
1552 isl_seq_clr(dst
+1+src_nparam
,
1553 dst_nparam
- src_nparam
);
1554 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1555 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1557 isl_min(dst_in
-in_off
, src_in
));
1558 if (dst_in
-in_off
> src_in
)
1559 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1560 dst_in
- in_off
- src_in
);
1561 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1562 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1563 src
+1+src_nparam
+src_in
,
1564 isl_min(dst_out
-out_off
, src_out
));
1565 if (dst_out
-out_off
> src_out
)
1566 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1567 dst_out
- out_off
- src_out
);
1568 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1569 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1570 src
+1+src_nparam
+src_in
+src_out
,
1571 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1572 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1573 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1574 div_off
+src_map
->n_div
,
1575 dst_map
->n_div
- div_off
- src_map
->n_div
);
1578 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1579 struct isl_basic_map
*src_map
, isl_int
*src
,
1580 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1582 isl_int_set(dst
[0], src
[0]);
1583 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1586 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1587 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1592 if (!bmap1
|| !bmap2
)
1595 div_off
= bmap1
->n_div
;
1597 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1598 int i1
= isl_basic_map_alloc_equality(bmap1
);
1601 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1602 i_pos
, o_pos
, div_off
);
1605 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1606 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1609 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1610 i_pos
, o_pos
, div_off
);
1613 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1614 int i1
= isl_basic_map_alloc_div(bmap1
);
1617 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1618 i_pos
, o_pos
, div_off
);
1621 isl_basic_map_free(bmap2
);
1626 isl_basic_map_free(bmap1
);
1627 isl_basic_map_free(bmap2
);
1631 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1632 struct isl_basic_set
*bset2
, unsigned pos
)
1634 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1635 bset_to_bmap(bset2
), 0, pos
));
1638 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1639 __isl_take isl_space
*dim
, unsigned extra
,
1640 unsigned n_eq
, unsigned n_ineq
)
1642 struct isl_basic_map
*ext
;
1652 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1653 base
->extra
>= base
->n_div
+ extra
;
1655 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1656 room_for_ineq(base
, n_ineq
)) {
1657 isl_space_free(dim
);
1661 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1662 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1663 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1664 extra
+= base
->extra
;
1666 n_ineq
+= base
->n_ineq
;
1668 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1674 ext
->sample
= isl_vec_copy(base
->sample
);
1675 flags
= base
->flags
;
1676 ext
= add_constraints(ext
, base
, 0, 0);
1679 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1685 isl_space_free(dim
);
1686 isl_basic_map_free(base
);
1690 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1691 __isl_take isl_space
*dim
, unsigned extra
,
1692 unsigned n_eq
, unsigned n_ineq
)
1694 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1695 dim
, extra
, n_eq
, n_ineq
));
1698 struct isl_basic_map
*isl_basic_map_extend_constraints(
1699 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1703 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1707 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1708 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1709 unsigned n_eq
, unsigned n_ineq
)
1711 struct isl_basic_map
*bmap
;
1716 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1720 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1723 isl_basic_map_free(base
);
1727 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1728 unsigned nparam
, unsigned dim
, unsigned extra
,
1729 unsigned n_eq
, unsigned n_ineq
)
1731 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1732 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1735 struct isl_basic_set
*isl_basic_set_extend_constraints(
1736 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1738 isl_basic_map
*bmap
= bset_to_bmap(base
);
1739 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1740 return bset_from_bmap(bmap
);
1743 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1745 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1748 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1753 if (bmap
->ref
> 1) {
1755 bmap
= isl_basic_map_dup(bmap
);
1758 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1759 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1764 /* Clear all cached information in "map", either because it is about
1765 * to be modified or because it is being freed.
1766 * Always return the same pointer that is passed in.
1767 * This is needed for the use in isl_map_free.
1769 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1771 isl_basic_map_free(map
->cached_simple_hull
[0]);
1772 isl_basic_map_free(map
->cached_simple_hull
[1]);
1773 map
->cached_simple_hull
[0] = NULL
;
1774 map
->cached_simple_hull
[1] = NULL
;
1778 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1780 return isl_map_cow(set
);
1783 /* Return an isl_map that is equal to "map" and that has only
1784 * a single reference.
1786 * If the original input already has only one reference, then
1787 * simply return it, but clear all cached information, since
1788 * it may be rendered invalid by the operations that will be
1789 * performed on the result.
1791 * Otherwise, create a duplicate (without any cached information).
1793 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1799 return clear_caches(map
);
1801 return isl_map_dup(map
);
1804 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1805 unsigned a_len
, unsigned b_len
)
1807 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1808 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1809 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1812 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1813 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1821 isl_assert(bmap
->ctx
,
1822 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1824 if (n1
== 0 || n2
== 0)
1827 bmap
= isl_basic_map_cow(bmap
);
1831 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1832 if (isl_blk_is_error(blk
))
1835 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1837 bmap
->eq
[i
] + pos
, n1
, n2
);
1839 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1841 bmap
->ineq
[i
] + pos
, n1
, n2
);
1843 for (i
= 0; i
< bmap
->n_div
; ++i
)
1845 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1847 isl_blk_free(bmap
->ctx
, blk
);
1849 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1850 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1851 return isl_basic_map_finalize(bmap
);
1853 isl_basic_map_free(bmap
);
1857 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1863 total
= isl_basic_map_total_dim(bmap
);
1864 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1865 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1867 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1869 i
= isl_basic_map_alloc_equality(bmap
);
1873 isl_int_set_si(bmap
->eq
[i
][0], 1);
1874 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1875 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1876 isl_vec_free(bmap
->sample
);
1877 bmap
->sample
= NULL
;
1878 return isl_basic_map_finalize(bmap
);
1880 isl_basic_map_free(bmap
);
1884 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1886 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1889 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1892 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1894 isl_int
*t
= bmap
->div
[a
];
1895 bmap
->div
[a
] = bmap
->div
[b
];
1899 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1900 * div definitions accordingly.
1902 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1905 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1907 swap_div(bmap
, a
, b
);
1909 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1910 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1912 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1913 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1915 for (i
= 0; i
< bmap
->n_div
; ++i
)
1916 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1917 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1920 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1921 * div definitions accordingly.
1923 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1925 isl_basic_map_swap_div(bset
, a
, b
);
1928 /* Eliminate the specified n dimensions starting at first from the
1929 * constraints, without removing the dimensions from the space.
1930 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1932 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1933 enum isl_dim_type type
, unsigned first
, unsigned n
)
1942 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1943 isl_die(map
->ctx
, isl_error_invalid
,
1944 "index out of bounds", goto error
);
1946 map
= isl_map_cow(map
);
1950 for (i
= 0; i
< map
->n
; ++i
) {
1951 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1961 /* Eliminate the specified n dimensions starting at first from the
1962 * constraints, without removing the dimensions from the space.
1963 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1965 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1966 enum isl_dim_type type
, unsigned first
, unsigned n
)
1968 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1971 /* Eliminate the specified n dimensions starting at first from the
1972 * constraints, without removing the dimensions from the space.
1973 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1975 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1976 unsigned first
, unsigned n
)
1978 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1981 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1982 __isl_take isl_basic_map
*bmap
)
1986 bmap
= isl_basic_map_eliminate_vars(bmap
,
1987 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1991 return isl_basic_map_finalize(bmap
);
1994 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1995 __isl_take isl_basic_set
*bset
)
1997 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2000 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2009 map
= isl_map_cow(map
);
2013 for (i
= 0; i
< map
->n
; ++i
) {
2014 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2024 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2026 return isl_map_remove_divs(set
);
2029 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2030 enum isl_dim_type type
, unsigned first
, unsigned n
)
2034 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2036 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2038 bmap
= isl_basic_map_eliminate_vars(bmap
,
2039 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2042 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2044 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2047 isl_basic_map_free(bmap
);
2051 /* Return true if the definition of the given div (recursively) involves
2052 * any of the given variables.
2054 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2055 unsigned first
, unsigned n
)
2058 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2060 if (isl_int_is_zero(bmap
->div
[div
][0]))
2062 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2065 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2066 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2068 if (div_involves_vars(bmap
, i
, first
, n
))
2075 /* Try and add a lower and/or upper bound on "div" to "bmap"
2076 * based on inequality "i".
2077 * "total" is the total number of variables (excluding the divs).
2078 * "v" is a temporary object that can be used during the calculations.
2079 * If "lb" is set, then a lower bound should be constructed.
2080 * If "ub" is set, then an upper bound should be constructed.
2082 * The calling function has already checked that the inequality does not
2083 * reference "div", but we still need to check that the inequality is
2084 * of the right form. We'll consider the case where we want to construct
2085 * a lower bound. The construction of upper bounds is similar.
2087 * Let "div" be of the form
2089 * q = floor((a + f(x))/d)
2091 * We essentially check if constraint "i" is of the form
2095 * so that we can use it to derive a lower bound on "div".
2096 * However, we allow a slightly more general form
2100 * with the condition that the coefficients of g(x) - f(x) are all
2102 * Rewriting this constraint as
2106 * adding a + f(x) to both sides and dividing by d, we obtain
2108 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2110 * Taking the floor on both sides, we obtain
2112 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2116 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2118 * In the case of an upper bound, we construct the constraint
2120 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2123 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2124 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2125 unsigned total
, isl_int v
, int lb
, int ub
)
2129 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2131 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2132 bmap
->div
[div
][1 + 1 + j
]);
2133 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2136 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2137 bmap
->div
[div
][1 + 1 + j
]);
2138 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2144 bmap
= isl_basic_map_cow(bmap
);
2145 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2147 int k
= isl_basic_map_alloc_inequality(bmap
);
2150 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2151 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2152 bmap
->div
[div
][1 + j
]);
2153 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2154 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2156 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2159 int k
= isl_basic_map_alloc_inequality(bmap
);
2162 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2163 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2164 bmap
->div
[div
][1 + j
]);
2165 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2166 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2168 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2171 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2174 isl_basic_map_free(bmap
);
2178 /* This function is called right before "div" is eliminated from "bmap"
2179 * using Fourier-Motzkin.
2180 * Look through the constraints of "bmap" for constraints on the argument
2181 * of the integer division and use them to construct constraints on the
2182 * integer division itself. These constraints can then be combined
2183 * during the Fourier-Motzkin elimination.
2184 * Note that it is only useful to introduce lower bounds on "div"
2185 * if "bmap" already contains upper bounds on "div" as the newly
2186 * introduce lower bounds can then be combined with the pre-existing
2187 * upper bounds. Similarly for upper bounds.
2188 * We therefore first check if "bmap" contains any lower and/or upper bounds
2191 * It is interesting to note that the introduction of these constraints
2192 * can indeed lead to more accurate results, even when compared to
2193 * deriving constraints on the argument of "div" from constraints on "div".
2194 * Consider, for example, the set
2196 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2198 * The second constraint can be rewritten as
2200 * 2 * [(-i-2j+3)/4] + k >= 0
2202 * from which we can derive
2204 * -i - 2j + 3 >= -2k
2210 * Combined with the first constraint, we obtain
2212 * -3 <= 3 + 2k or k >= -3
2214 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2215 * the first constraint, we obtain
2217 * [(i + 2j)/4] >= [-3/4] = -1
2219 * Combining this constraint with the second constraint, we obtain
2223 static __isl_give isl_basic_map
*insert_bounds_on_div(
2224 __isl_take isl_basic_map
*bmap
, int div
)
2227 int check_lb
, check_ub
;
2234 if (isl_int_is_zero(bmap
->div
[div
][0]))
2237 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2241 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2242 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2249 if (!check_lb
&& !check_ub
)
2254 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2255 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2258 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2259 check_lb
, check_ub
);
2267 /* Remove all divs (recursively) involving any of the given dimensions
2268 * in their definitions.
2270 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2271 __isl_take isl_basic_map
*bmap
,
2272 enum isl_dim_type type
, unsigned first
, unsigned n
)
2278 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2280 first
+= isl_basic_map_offset(bmap
, type
);
2282 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2283 if (!div_involves_vars(bmap
, i
, first
, n
))
2285 bmap
= insert_bounds_on_div(bmap
, i
);
2286 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2294 isl_basic_map_free(bmap
);
2298 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2299 __isl_take isl_basic_set
*bset
,
2300 enum isl_dim_type type
, unsigned first
, unsigned n
)
2302 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2305 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2306 enum isl_dim_type type
, unsigned first
, unsigned n
)
2315 map
= isl_map_cow(map
);
2319 for (i
= 0; i
< map
->n
; ++i
) {
2320 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2331 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2332 enum isl_dim_type type
, unsigned first
, unsigned n
)
2334 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2338 /* Does the description of "bmap" depend on the specified dimensions?
2339 * We also check whether the dimensions appear in any of the div definitions.
2340 * In principle there is no need for this check. If the dimensions appear
2341 * in a div definition, they also appear in the defining constraints of that
2344 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2345 enum isl_dim_type type
, unsigned first
, unsigned n
)
2350 return isl_bool_error
;
2352 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2353 isl_die(bmap
->ctx
, isl_error_invalid
,
2354 "index out of bounds", return isl_bool_error
);
2356 first
+= isl_basic_map_offset(bmap
, type
);
2357 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2358 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2359 return isl_bool_true
;
2360 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2361 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2362 return isl_bool_true
;
2363 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2364 if (isl_int_is_zero(bmap
->div
[i
][0]))
2366 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2367 return isl_bool_true
;
2370 return isl_bool_false
;
2373 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2374 enum isl_dim_type type
, unsigned first
, unsigned n
)
2379 return isl_bool_error
;
2381 if (first
+ n
> isl_map_dim(map
, type
))
2382 isl_die(map
->ctx
, isl_error_invalid
,
2383 "index out of bounds", return isl_bool_error
);
2385 for (i
= 0; i
< map
->n
; ++i
) {
2386 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2388 if (involves
< 0 || involves
)
2392 return isl_bool_false
;
2395 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2396 enum isl_dim_type type
, unsigned first
, unsigned n
)
2398 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2401 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2402 enum isl_dim_type type
, unsigned first
, unsigned n
)
2404 return isl_map_involves_dims(set
, type
, first
, n
);
2407 /* Does local variable "div" of "bmap" have a complete explicit representation?
2408 * Having a complete explicit representation requires not only
2409 * an explicit representation, but also that all local variables
2410 * that appear in this explicit representation in turn have
2411 * a complete explicit representation.
2413 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2416 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2419 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2420 if (marked
< 0 || marked
)
2421 return isl_bool_not(marked
);
2423 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2426 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2428 known
= isl_basic_map_div_is_known(bmap
, i
);
2429 if (known
< 0 || !known
)
2433 return isl_bool_true
;
2436 /* Does local variable "div" of "bset" have a complete explicit representation?
2438 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2440 return isl_basic_map_div_is_known(bset
, div
);
2443 /* Remove all divs that are unknown or defined in terms of unknown divs.
2445 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2446 __isl_take isl_basic_map
*bmap
)
2453 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2454 if (isl_basic_map_div_is_known(bmap
, i
))
2456 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2465 /* Remove all divs that are unknown or defined in terms of unknown divs.
2467 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2468 __isl_take isl_basic_set
*bset
)
2470 return isl_basic_map_remove_unknown_divs(bset
);
2473 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2482 map
= isl_map_cow(map
);
2486 for (i
= 0; i
< map
->n
; ++i
) {
2487 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2497 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2499 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2502 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2503 __isl_take isl_basic_set
*bset
,
2504 enum isl_dim_type type
, unsigned first
, unsigned n
)
2506 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2507 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2508 return bset_from_bmap(bmap
);
2511 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2512 enum isl_dim_type type
, unsigned first
, unsigned n
)
2519 map
= isl_map_cow(map
);
2522 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2524 for (i
= 0; i
< map
->n
; ++i
) {
2525 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2526 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2530 map
= isl_map_drop(map
, type
, first
, n
);
2537 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2538 enum isl_dim_type type
, unsigned first
, unsigned n
)
2540 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2544 /* Project out n inputs starting at first using Fourier-Motzkin */
2545 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2546 unsigned first
, unsigned n
)
2548 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2551 static void dump_term(struct isl_basic_map
*bmap
,
2552 isl_int c
, int pos
, FILE *out
)
2555 unsigned in
= isl_basic_map_n_in(bmap
);
2556 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2557 unsigned nparam
= isl_basic_map_n_param(bmap
);
2559 isl_int_print(out
, c
, 0);
2561 if (!isl_int_is_one(c
))
2562 isl_int_print(out
, c
, 0);
2563 if (pos
< 1 + nparam
) {
2564 name
= isl_space_get_dim_name(bmap
->dim
,
2565 isl_dim_param
, pos
- 1);
2567 fprintf(out
, "%s", name
);
2569 fprintf(out
, "p%d", pos
- 1);
2570 } else if (pos
< 1 + nparam
+ in
)
2571 fprintf(out
, "i%d", pos
- 1 - nparam
);
2572 else if (pos
< 1 + nparam
+ dim
)
2573 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2575 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2579 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2580 int sign
, FILE *out
)
2584 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2588 for (i
= 0, first
= 1; i
< len
; ++i
) {
2589 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2592 fprintf(out
, " + ");
2594 isl_int_abs(v
, c
[i
]);
2595 dump_term(bmap
, v
, i
, out
);
2602 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2603 const char *op
, FILE *out
, int indent
)
2607 fprintf(out
, "%*s", indent
, "");
2609 dump_constraint_sign(bmap
, c
, 1, out
);
2610 fprintf(out
, " %s ", op
);
2611 dump_constraint_sign(bmap
, c
, -1, out
);
2615 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2616 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2618 fprintf(out
, "%*s", indent
, "");
2619 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2624 static void dump_constraints(struct isl_basic_map
*bmap
,
2625 isl_int
**c
, unsigned n
,
2626 const char *op
, FILE *out
, int indent
)
2630 for (i
= 0; i
< n
; ++i
)
2631 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2634 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2638 unsigned total
= isl_basic_map_total_dim(bmap
);
2640 for (j
= 0; j
< 1 + total
; ++j
) {
2641 if (isl_int_is_zero(exp
[j
]))
2643 if (!first
&& isl_int_is_pos(exp
[j
]))
2645 dump_term(bmap
, exp
[j
], j
, out
);
2650 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2654 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2655 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2657 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2658 fprintf(out
, "%*s", indent
, "");
2659 fprintf(out
, "e%d = [(", i
);
2660 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2662 isl_int_print(out
, bmap
->div
[i
][0], 0);
2663 fprintf(out
, "]\n");
2667 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2668 FILE *out
, int indent
)
2671 fprintf(out
, "null basic set\n");
2675 fprintf(out
, "%*s", indent
, "");
2676 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2677 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2678 bset
->extra
, bset
->flags
);
2679 dump(bset_to_bmap(bset
), out
, indent
);
2682 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2683 FILE *out
, int indent
)
2686 fprintf(out
, "null basic map\n");
2690 fprintf(out
, "%*s", indent
, "");
2691 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2692 "flags: %x, n_name: %d\n",
2694 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2695 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2696 dump(bmap
, out
, indent
);
2699 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2704 total
= isl_basic_map_total_dim(bmap
);
2705 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2706 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2707 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2708 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2712 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2717 if (isl_space_dim(space
, isl_dim_in
) != 0)
2718 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2719 "set cannot have input dimensions", goto error
);
2720 return isl_map_alloc_space(space
, n
, flags
);
2722 isl_space_free(space
);
2726 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2727 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2729 struct isl_set
*set
;
2732 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2736 set
= isl_set_alloc_space(dims
, n
, flags
);
2740 /* Make sure "map" has room for at least "n" more basic maps.
2742 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2745 struct isl_map
*grown
= NULL
;
2749 isl_assert(map
->ctx
, n
>= 0, goto error
);
2750 if (map
->n
+ n
<= map
->size
)
2752 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2755 for (i
= 0; i
< map
->n
; ++i
) {
2756 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2764 isl_map_free(grown
);
2769 /* Make sure "set" has room for at least "n" more basic sets.
2771 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2773 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2776 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2779 struct isl_set
*dup
;
2784 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2787 for (i
= 0; i
< set
->n
; ++i
)
2788 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2792 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2794 return isl_map_from_basic_map(bset
);
2797 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2799 struct isl_map
*map
;
2804 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2805 return isl_map_add_basic_map(map
, bmap
);
2808 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2809 __isl_take isl_basic_set
*bset
)
2811 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
2812 bset_to_bmap(bset
)));
2815 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2817 return isl_map_free(set
);
2820 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2825 fprintf(out
, "null set\n");
2829 fprintf(out
, "%*s", indent
, "");
2830 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2831 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2833 for (i
= 0; i
< set
->n
; ++i
) {
2834 fprintf(out
, "%*s", indent
, "");
2835 fprintf(out
, "basic set %d:\n", i
);
2836 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2840 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2845 fprintf(out
, "null map\n");
2849 fprintf(out
, "%*s", indent
, "");
2850 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2851 "flags: %x, n_name: %d\n",
2852 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2853 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2854 for (i
= 0; i
< map
->n
; ++i
) {
2855 fprintf(out
, "%*s", indent
, "");
2856 fprintf(out
, "basic map %d:\n", i
);
2857 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2861 struct isl_basic_map
*isl_basic_map_intersect_domain(
2862 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2864 struct isl_basic_map
*bmap_domain
;
2869 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2870 bset
->dim
, isl_dim_param
), goto error
);
2872 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2873 isl_assert(bset
->ctx
,
2874 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2876 bmap
= isl_basic_map_cow(bmap
);
2879 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2880 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2881 bmap_domain
= isl_basic_map_from_domain(bset
);
2882 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2884 bmap
= isl_basic_map_simplify(bmap
);
2885 return isl_basic_map_finalize(bmap
);
2887 isl_basic_map_free(bmap
);
2888 isl_basic_set_free(bset
);
2892 struct isl_basic_map
*isl_basic_map_intersect_range(
2893 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2895 struct isl_basic_map
*bmap_range
;
2900 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2901 bset
->dim
, isl_dim_param
), goto error
);
2903 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2904 isl_assert(bset
->ctx
,
2905 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2907 if (isl_basic_set_plain_is_universe(bset
)) {
2908 isl_basic_set_free(bset
);
2912 bmap
= isl_basic_map_cow(bmap
);
2915 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2916 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2917 bmap_range
= bset_to_bmap(bset
);
2918 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2920 bmap
= isl_basic_map_simplify(bmap
);
2921 return isl_basic_map_finalize(bmap
);
2923 isl_basic_map_free(bmap
);
2924 isl_basic_set_free(bset
);
2928 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2929 __isl_keep isl_vec
*vec
)
2936 return isl_bool_error
;
2938 total
= 1 + isl_basic_map_total_dim(bmap
);
2939 if (total
!= vec
->size
)
2940 return isl_bool_error
;
2944 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2945 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2946 if (!isl_int_is_zero(s
)) {
2948 return isl_bool_false
;
2952 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2953 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2954 if (isl_int_is_neg(s
)) {
2956 return isl_bool_false
;
2962 return isl_bool_true
;
2965 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2966 __isl_keep isl_vec
*vec
)
2968 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
2971 struct isl_basic_map
*isl_basic_map_intersect(
2972 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2974 struct isl_vec
*sample
= NULL
;
2976 if (!bmap1
|| !bmap2
)
2979 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2980 bmap2
->dim
, isl_dim_param
), goto error
);
2981 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2982 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2983 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2984 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2985 return isl_basic_map_intersect(bmap2
, bmap1
);
2987 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2988 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2989 isl_assert(bmap1
->ctx
,
2990 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2992 if (isl_basic_map_plain_is_empty(bmap1
)) {
2993 isl_basic_map_free(bmap2
);
2996 if (isl_basic_map_plain_is_empty(bmap2
)) {
2997 isl_basic_map_free(bmap1
);
3001 if (bmap1
->sample
&&
3002 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3003 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3004 sample
= isl_vec_copy(bmap1
->sample
);
3005 else if (bmap2
->sample
&&
3006 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3007 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3008 sample
= isl_vec_copy(bmap2
->sample
);
3010 bmap1
= isl_basic_map_cow(bmap1
);
3013 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3014 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3015 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3018 isl_vec_free(sample
);
3020 isl_vec_free(bmap1
->sample
);
3021 bmap1
->sample
= sample
;
3024 bmap1
= isl_basic_map_simplify(bmap1
);
3025 return isl_basic_map_finalize(bmap1
);
3028 isl_vec_free(sample
);
3029 isl_basic_map_free(bmap1
);
3030 isl_basic_map_free(bmap2
);
3034 struct isl_basic_set
*isl_basic_set_intersect(
3035 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3037 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3038 bset_to_bmap(bset2
)));
3041 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3042 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3044 return isl_basic_set_intersect(bset1
, bset2
);
3047 /* Special case of isl_map_intersect, where both map1 and map2
3048 * are convex, without any divs and such that either map1 or map2
3049 * contains a single constraint. This constraint is then simply
3050 * added to the other map.
3052 static __isl_give isl_map
*map_intersect_add_constraint(
3053 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3055 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3056 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3057 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3058 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3060 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3061 return isl_map_intersect(map2
, map1
);
3063 isl_assert(map2
->ctx
,
3064 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3066 map1
= isl_map_cow(map1
);
3069 if (isl_map_plain_is_empty(map1
)) {
3073 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3074 if (map2
->p
[0]->n_eq
== 1)
3075 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3077 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3078 map2
->p
[0]->ineq
[0]);
3080 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3081 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3085 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3086 isl_basic_map_free(map1
->p
[0]);
3099 /* map2 may be either a parameter domain or a map living in the same
3102 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3103 __isl_take isl_map
*map2
)
3112 if ((isl_map_plain_is_empty(map1
) ||
3113 isl_map_plain_is_universe(map2
)) &&
3114 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3118 if ((isl_map_plain_is_empty(map2
) ||
3119 isl_map_plain_is_universe(map1
)) &&
3120 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3125 if (map1
->n
== 1 && map2
->n
== 1 &&
3126 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3127 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3128 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3129 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3130 return map_intersect_add_constraint(map1
, map2
);
3132 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3133 isl_space_dim(map2
->dim
, isl_dim_param
))
3134 isl_assert(map1
->ctx
,
3135 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3137 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3138 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3139 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3141 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3142 map1
->n
* map2
->n
, flags
);
3145 for (i
= 0; i
< map1
->n
; ++i
)
3146 for (j
= 0; j
< map2
->n
; ++j
) {
3147 struct isl_basic_map
*part
;
3148 part
= isl_basic_map_intersect(
3149 isl_basic_map_copy(map1
->p
[i
]),
3150 isl_basic_map_copy(map2
->p
[j
]));
3151 if (isl_basic_map_is_empty(part
) < 0)
3152 part
= isl_basic_map_free(part
);
3153 result
= isl_map_add_basic_map(result
, part
);
3166 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3167 __isl_take isl_map
*map2
)
3171 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3172 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3173 "spaces don't match", goto error
);
3174 return map_intersect_internal(map1
, map2
);
3181 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3182 __isl_take isl_map
*map2
)
3184 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3187 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3189 return set_from_map(isl_map_intersect(set_to_map(set1
),
3193 /* map_intersect_internal accepts intersections
3194 * with parameter domains, so we can just call that function.
3196 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3197 __isl_take isl_set
*params
)
3199 return map_intersect_internal(map
, params
);
3202 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3203 __isl_take isl_map
*map2
)
3205 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3208 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3209 __isl_take isl_set
*params
)
3211 return isl_map_intersect_params(set
, params
);
3214 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3217 unsigned pos
, n1
, n2
;
3221 bmap
= isl_basic_map_cow(bmap
);
3224 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3225 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3226 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3227 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3228 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3229 return isl_basic_map_reset_space(bmap
, space
);
3232 static __isl_give isl_basic_map
*basic_map_space_reset(
3233 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3239 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3242 space
= isl_basic_map_get_space(bmap
);
3243 space
= isl_space_reset(space
, type
);
3244 bmap
= isl_basic_map_reset_space(bmap
, space
);
3248 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3249 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3250 unsigned pos
, unsigned n
)
3253 struct isl_basic_map
*res
;
3254 struct isl_dim_map
*dim_map
;
3255 unsigned total
, off
;
3256 enum isl_dim_type t
;
3259 return basic_map_space_reset(bmap
, type
);
3264 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3266 total
= isl_basic_map_total_dim(bmap
) + n
;
3267 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3269 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3271 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3273 unsigned size
= isl_basic_map_dim(bmap
, t
);
3274 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3276 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3277 pos
, size
- pos
, off
+ pos
+ n
);
3279 off
+= isl_space_dim(res_dim
, t
);
3281 isl_dim_map_div(dim_map
, bmap
, off
);
3283 res
= isl_basic_map_alloc_space(res_dim
,
3284 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3285 if (isl_basic_map_is_rational(bmap
))
3286 res
= isl_basic_map_set_rational(res
);
3287 if (isl_basic_map_plain_is_empty(bmap
)) {
3288 isl_basic_map_free(bmap
);
3290 return isl_basic_map_set_to_empty(res
);
3292 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3293 return isl_basic_map_finalize(res
);
3296 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3297 __isl_take isl_basic_set
*bset
,
3298 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3300 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3303 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3304 enum isl_dim_type type
, unsigned n
)
3308 return isl_basic_map_insert_dims(bmap
, type
,
3309 isl_basic_map_dim(bmap
, type
), n
);
3312 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3313 enum isl_dim_type type
, unsigned n
)
3317 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3318 return isl_basic_map_add_dims(bset
, type
, n
);
3320 isl_basic_set_free(bset
);
3324 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3325 enum isl_dim_type type
)
3329 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3332 space
= isl_map_get_space(map
);
3333 space
= isl_space_reset(space
, type
);
3334 map
= isl_map_reset_space(map
, space
);
3338 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3339 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3344 return map_space_reset(map
, type
);
3346 map
= isl_map_cow(map
);
3350 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3354 for (i
= 0; i
< map
->n
; ++i
) {
3355 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3366 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3367 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3369 return isl_map_insert_dims(set
, type
, pos
, n
);
3372 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3373 enum isl_dim_type type
, unsigned n
)
3377 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3380 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3381 enum isl_dim_type type
, unsigned n
)
3385 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3386 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3392 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3393 __isl_take isl_basic_map
*bmap
,
3394 enum isl_dim_type dst_type
, unsigned dst_pos
,
3395 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3397 struct isl_dim_map
*dim_map
;
3398 struct isl_basic_map
*res
;
3399 enum isl_dim_type t
;
3400 unsigned total
, off
;
3407 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3410 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3413 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3415 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3416 pos(bmap
->dim
, src_type
) + src_pos
+
3417 ((src_type
< dst_type
) ? n
: 0)) {
3418 bmap
= isl_basic_map_cow(bmap
);
3422 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3423 src_type
, src_pos
, n
);
3427 bmap
= isl_basic_map_finalize(bmap
);
3432 total
= isl_basic_map_total_dim(bmap
);
3433 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3436 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3437 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3438 if (t
== dst_type
) {
3439 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3442 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3445 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3446 dst_pos
, size
- dst_pos
, off
);
3447 off
+= size
- dst_pos
;
3448 } else if (t
== src_type
) {
3449 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3452 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3453 src_pos
+ n
, size
- src_pos
- n
, off
);
3454 off
+= size
- src_pos
- n
;
3456 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3460 isl_dim_map_div(dim_map
, bmap
, off
);
3462 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3463 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3464 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3468 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3469 src_type
, src_pos
, n
);
3473 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3474 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3475 bmap
= isl_basic_map_finalize(bmap
);
3479 isl_basic_map_free(bmap
);
3483 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3484 enum isl_dim_type dst_type
, unsigned dst_pos
,
3485 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3487 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3488 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3489 src_type
, src_pos
, n
);
3490 return bset_from_bmap(bmap
);
3493 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3494 enum isl_dim_type dst_type
, unsigned dst_pos
,
3495 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3499 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3500 return set_from_map(isl_map_move_dims(set_to_map(set
),
3501 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3507 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3508 enum isl_dim_type dst_type
, unsigned dst_pos
,
3509 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3518 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3521 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3524 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3526 map
= isl_map_cow(map
);
3530 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3534 for (i
= 0; i
< map
->n
; ++i
) {
3535 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3537 src_type
, src_pos
, n
);
3548 /* Move the specified dimensions to the last columns right before
3549 * the divs. Don't change the dimension specification of bmap.
3550 * That's the responsibility of the caller.
3552 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3553 enum isl_dim_type type
, unsigned first
, unsigned n
)
3555 struct isl_dim_map
*dim_map
;
3556 struct isl_basic_map
*res
;
3557 enum isl_dim_type t
;
3558 unsigned total
, off
;
3562 if (pos(bmap
->dim
, type
) + first
+ n
==
3563 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3566 total
= isl_basic_map_total_dim(bmap
);
3567 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3570 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3571 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3573 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3576 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3577 first
, n
, total
- bmap
->n_div
- n
);
3578 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3579 first
+ n
, size
- (first
+ n
), off
);
3580 off
+= size
- (first
+ n
);
3582 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3586 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3588 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3589 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3590 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3594 /* Insert "n" rows in the divs of "bmap".
3596 * The number of columns is not changed, which means that the last
3597 * dimensions of "bmap" are being reintepreted as the new divs.
3598 * The space of "bmap" is not adjusted, however, which means
3599 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3600 * from the space of "bmap" is the responsibility of the caller.
3602 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3610 bmap
= isl_basic_map_cow(bmap
);
3614 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3615 old
= bmap
->block2
.data
;
3616 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3617 (bmap
->extra
+ n
) * (1 + row_size
));
3618 if (!bmap
->block2
.data
)
3619 return isl_basic_map_free(bmap
);
3620 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3622 return isl_basic_map_free(bmap
);
3623 for (i
= 0; i
< n
; ++i
) {
3624 new_div
[i
] = bmap
->block2
.data
+
3625 (bmap
->extra
+ i
) * (1 + row_size
);
3626 isl_seq_clr(new_div
[i
], 1 + row_size
);
3628 for (i
= 0; i
< bmap
->extra
; ++i
)
3629 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3631 bmap
->div
= new_div
;
3638 /* Drop constraints from "bmap" that only involve the variables
3639 * of "type" in the range [first, first + n] that are not related
3640 * to any of the variables outside that interval.
3641 * These constraints cannot influence the values for the variables
3642 * outside the interval, except in case they cause "bmap" to be empty.
3643 * Only drop the constraints if "bmap" is known to be non-empty.
3645 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3646 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3647 unsigned first
, unsigned n
)
3651 unsigned dim
, n_div
;
3654 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3656 return isl_basic_map_free(bmap
);
3660 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3661 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3662 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3664 return isl_basic_map_free(bmap
);
3665 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3666 for (i
= 0; i
< first
; ++i
)
3668 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3671 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3676 /* Turn the n dimensions of type type, starting at first
3677 * into existentially quantified variables.
3679 * If a subset of the projected out variables are unrelated
3680 * to any of the variables that remain, then the constraints
3681 * involving this subset are simply dropped first.
3683 __isl_give isl_basic_map
*isl_basic_map_project_out(
3684 __isl_take isl_basic_map
*bmap
,
3685 enum isl_dim_type type
, unsigned first
, unsigned n
)
3688 return basic_map_space_reset(bmap
, type
);
3689 if (type
== isl_dim_div
)
3690 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3691 "cannot project out existentially quantified variables",
3692 return isl_basic_map_free(bmap
));
3694 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3698 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3699 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3701 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3704 bmap
= move_last(bmap
, type
, first
, n
);
3705 bmap
= isl_basic_map_cow(bmap
);
3706 bmap
= insert_div_rows(bmap
, n
);
3710 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3713 bmap
= isl_basic_map_simplify(bmap
);
3714 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3715 return isl_basic_map_finalize(bmap
);
3717 isl_basic_map_free(bmap
);
3721 /* Turn the n dimensions of type type, starting at first
3722 * into existentially quantified variables.
3724 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3725 enum isl_dim_type type
, unsigned first
, unsigned n
)
3727 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3731 /* Turn the n dimensions of type type, starting at first
3732 * into existentially quantified variables.
3734 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3735 enum isl_dim_type type
, unsigned first
, unsigned n
)
3743 return map_space_reset(map
, type
);
3745 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3747 map
= isl_map_cow(map
);
3751 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3755 for (i
= 0; i
< map
->n
; ++i
) {
3756 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3767 /* Turn the n dimensions of type type, starting at first
3768 * into existentially quantified variables.
3770 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3771 enum isl_dim_type type
, unsigned first
, unsigned n
)
3773 return set_from_map(isl_map_project_out(set_to_map(set
),
3777 /* Return a map that projects the elements in "set" onto their
3778 * "n" set dimensions starting at "first".
3779 * "type" should be equal to isl_dim_set.
3781 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3782 enum isl_dim_type type
, unsigned first
, unsigned n
)
3790 if (type
!= isl_dim_set
)
3791 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3792 "only set dimensions can be projected out", goto error
);
3793 dim
= isl_set_dim(set
, isl_dim_set
);
3794 if (first
+ n
> dim
|| first
+ n
< first
)
3795 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3796 "index out of bounds", goto error
);
3798 map
= isl_map_from_domain(set
);
3799 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3800 for (i
= 0; i
< n
; ++i
)
3801 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3809 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3813 for (i
= 0; i
< n
; ++i
) {
3814 j
= isl_basic_map_alloc_div(bmap
);
3817 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3821 isl_basic_map_free(bmap
);
3825 struct isl_basic_map
*isl_basic_map_apply_range(
3826 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3828 isl_space
*dim_result
= NULL
;
3829 struct isl_basic_map
*bmap
;
3830 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3831 struct isl_dim_map
*dim_map1
, *dim_map2
;
3833 if (!bmap1
|| !bmap2
)
3835 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3836 bmap2
->dim
, isl_dim_param
))
3837 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3838 "parameters don't match", goto error
);
3839 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3840 bmap2
->dim
, isl_dim_in
))
3841 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3842 "spaces don't match", goto error
);
3844 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3845 isl_space_copy(bmap2
->dim
));
3847 n_in
= isl_basic_map_n_in(bmap1
);
3848 n_out
= isl_basic_map_n_out(bmap2
);
3849 n
= isl_basic_map_n_out(bmap1
);
3850 nparam
= isl_basic_map_n_param(bmap1
);
3852 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3853 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3854 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3855 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3856 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3857 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3858 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3859 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3860 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3861 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3862 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3864 bmap
= isl_basic_map_alloc_space(dim_result
,
3865 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3866 bmap1
->n_eq
+ bmap2
->n_eq
,
3867 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3868 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3869 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3870 bmap
= add_divs(bmap
, n
);
3871 bmap
= isl_basic_map_simplify(bmap
);
3872 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3873 return isl_basic_map_finalize(bmap
);
3875 isl_basic_map_free(bmap1
);
3876 isl_basic_map_free(bmap2
);
3880 struct isl_basic_set
*isl_basic_set_apply(
3881 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3886 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3889 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
3892 isl_basic_set_free(bset
);
3893 isl_basic_map_free(bmap
);
3897 struct isl_basic_map
*isl_basic_map_apply_domain(
3898 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3900 if (!bmap1
|| !bmap2
)
3903 isl_assert(bmap1
->ctx
,
3904 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3905 isl_assert(bmap1
->ctx
,
3906 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3909 bmap1
= isl_basic_map_reverse(bmap1
);
3910 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3911 return isl_basic_map_reverse(bmap1
);
3913 isl_basic_map_free(bmap1
);
3914 isl_basic_map_free(bmap2
);
3918 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3919 * A \cap B -> f(A) + f(B)
3921 struct isl_basic_map
*isl_basic_map_sum(
3922 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3924 unsigned n_in
, n_out
, nparam
, total
, pos
;
3925 struct isl_basic_map
*bmap
= NULL
;
3926 struct isl_dim_map
*dim_map1
, *dim_map2
;
3929 if (!bmap1
|| !bmap2
)
3932 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3935 nparam
= isl_basic_map_n_param(bmap1
);
3936 n_in
= isl_basic_map_n_in(bmap1
);
3937 n_out
= isl_basic_map_n_out(bmap1
);
3939 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3940 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3941 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3942 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3943 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3944 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3945 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3946 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3947 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3948 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3949 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3951 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3952 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3953 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3954 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3955 for (i
= 0; i
< n_out
; ++i
) {
3956 int j
= isl_basic_map_alloc_equality(bmap
);
3959 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3960 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3961 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3962 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3964 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3965 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3966 bmap
= add_divs(bmap
, 2 * n_out
);
3968 bmap
= isl_basic_map_simplify(bmap
);
3969 return isl_basic_map_finalize(bmap
);
3971 isl_basic_map_free(bmap
);
3972 isl_basic_map_free(bmap1
);
3973 isl_basic_map_free(bmap2
);
3977 /* Given two maps A -> f(A) and B -> g(B), construct a map
3978 * A \cap B -> f(A) + f(B)
3980 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3982 struct isl_map
*result
;
3988 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3990 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3991 map1
->n
* map2
->n
, 0);
3994 for (i
= 0; i
< map1
->n
; ++i
)
3995 for (j
= 0; j
< map2
->n
; ++j
) {
3996 struct isl_basic_map
*part
;
3997 part
= isl_basic_map_sum(
3998 isl_basic_map_copy(map1
->p
[i
]),
3999 isl_basic_map_copy(map2
->p
[j
]));
4000 if (isl_basic_map_is_empty(part
))
4001 isl_basic_map_free(part
);
4003 result
= isl_map_add_basic_map(result
, part
);
4016 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4017 __isl_take isl_set
*set2
)
4019 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4022 /* Given a basic map A -> f(A), construct A -> -f(A).
4024 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4029 bmap
= isl_basic_map_cow(bmap
);
4033 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4034 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4035 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4036 for (j
= 0; j
< n
; ++j
)
4037 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4038 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4039 for (j
= 0; j
< n
; ++j
)
4040 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4041 for (i
= 0; i
< bmap
->n_div
; ++i
)
4042 for (j
= 0; j
< n
; ++j
)
4043 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4044 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4045 return isl_basic_map_finalize(bmap
);
4048 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4050 return isl_basic_map_neg(bset
);
4053 /* Given a map A -> f(A), construct A -> -f(A).
4055 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4059 map
= isl_map_cow(map
);
4063 for (i
= 0; i
< map
->n
; ++i
) {
4064 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4075 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4077 return set_from_map(isl_map_neg(set_to_map(set
)));
4080 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4081 * A -> floor(f(A)/d).
4083 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4086 unsigned n_in
, n_out
, nparam
, total
, pos
;
4087 struct isl_basic_map
*result
= NULL
;
4088 struct isl_dim_map
*dim_map
;
4094 nparam
= isl_basic_map_n_param(bmap
);
4095 n_in
= isl_basic_map_n_in(bmap
);
4096 n_out
= isl_basic_map_n_out(bmap
);
4098 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4099 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4100 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4101 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4102 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4103 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4105 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4106 bmap
->n_div
+ n_out
,
4107 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4108 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4109 result
= add_divs(result
, n_out
);
4110 for (i
= 0; i
< n_out
; ++i
) {
4112 j
= isl_basic_map_alloc_inequality(result
);
4115 isl_seq_clr(result
->ineq
[j
], 1+total
);
4116 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4117 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4118 j
= isl_basic_map_alloc_inequality(result
);
4121 isl_seq_clr(result
->ineq
[j
], 1+total
);
4122 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4123 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4124 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4127 result
= isl_basic_map_simplify(result
);
4128 return isl_basic_map_finalize(result
);
4130 isl_basic_map_free(result
);
4134 /* Given a map A -> f(A) and an integer d, construct a map
4135 * A -> floor(f(A)/d).
4137 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4141 map
= isl_map_cow(map
);
4145 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4146 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4147 for (i
= 0; i
< map
->n
; ++i
) {
4148 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4159 /* Given a map A -> f(A) and an integer d, construct a map
4160 * A -> floor(f(A)/d).
4162 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4163 __isl_take isl_val
*d
)
4167 if (!isl_val_is_int(d
))
4168 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4169 "expecting integer denominator", goto error
);
4170 map
= isl_map_floordiv(map
, d
->n
);
4179 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4185 i
= isl_basic_map_alloc_equality(bmap
);
4188 nparam
= isl_basic_map_n_param(bmap
);
4189 n_in
= isl_basic_map_n_in(bmap
);
4190 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4191 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4192 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4193 return isl_basic_map_finalize(bmap
);
4195 isl_basic_map_free(bmap
);
4199 /* Add a constraint to "bmap" expressing i_pos < o_pos
4201 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4207 i
= isl_basic_map_alloc_inequality(bmap
);
4210 nparam
= isl_basic_map_n_param(bmap
);
4211 n_in
= isl_basic_map_n_in(bmap
);
4212 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4213 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4214 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4215 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4216 return isl_basic_map_finalize(bmap
);
4218 isl_basic_map_free(bmap
);
4222 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4224 static __isl_give isl_basic_map
*var_less_or_equal(
4225 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4231 i
= isl_basic_map_alloc_inequality(bmap
);
4234 nparam
= isl_basic_map_n_param(bmap
);
4235 n_in
= isl_basic_map_n_in(bmap
);
4236 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4237 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4238 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4239 return isl_basic_map_finalize(bmap
);
4241 isl_basic_map_free(bmap
);
4245 /* Add a constraint to "bmap" expressing i_pos > o_pos
4247 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4253 i
= isl_basic_map_alloc_inequality(bmap
);
4256 nparam
= isl_basic_map_n_param(bmap
);
4257 n_in
= isl_basic_map_n_in(bmap
);
4258 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4259 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4260 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4261 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4262 return isl_basic_map_finalize(bmap
);
4264 isl_basic_map_free(bmap
);
4268 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4270 static __isl_give isl_basic_map
*var_more_or_equal(
4271 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4277 i
= isl_basic_map_alloc_inequality(bmap
);
4280 nparam
= isl_basic_map_n_param(bmap
);
4281 n_in
= isl_basic_map_n_in(bmap
);
4282 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4283 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4284 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4285 return isl_basic_map_finalize(bmap
);
4287 isl_basic_map_free(bmap
);
4291 __isl_give isl_basic_map
*isl_basic_map_equal(
4292 __isl_take isl_space
*dim
, unsigned n_equal
)
4295 struct isl_basic_map
*bmap
;
4296 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4299 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4300 bmap
= var_equal(bmap
, i
);
4301 return isl_basic_map_finalize(bmap
);
4304 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4306 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4310 struct isl_basic_map
*bmap
;
4311 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4314 for (i
= 0; i
< pos
&& bmap
; ++i
)
4315 bmap
= var_equal(bmap
, i
);
4317 bmap
= var_less(bmap
, pos
);
4318 return isl_basic_map_finalize(bmap
);
4321 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4323 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4324 __isl_take isl_space
*dim
, unsigned pos
)
4327 isl_basic_map
*bmap
;
4329 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4330 for (i
= 0; i
< pos
; ++i
)
4331 bmap
= var_equal(bmap
, i
);
4332 bmap
= var_less_or_equal(bmap
, pos
);
4333 return isl_basic_map_finalize(bmap
);
4336 /* Return a relation on "dim" expressing i_pos > o_pos
4338 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4342 struct isl_basic_map
*bmap
;
4343 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4346 for (i
= 0; i
< pos
&& bmap
; ++i
)
4347 bmap
= var_equal(bmap
, i
);
4349 bmap
= var_more(bmap
, pos
);
4350 return isl_basic_map_finalize(bmap
);
4353 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4355 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4356 __isl_take isl_space
*dim
, unsigned pos
)
4359 isl_basic_map
*bmap
;
4361 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4362 for (i
= 0; i
< pos
; ++i
)
4363 bmap
= var_equal(bmap
, i
);
4364 bmap
= var_more_or_equal(bmap
, pos
);
4365 return isl_basic_map_finalize(bmap
);
4368 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4369 unsigned n
, int equal
)
4371 struct isl_map
*map
;
4374 if (n
== 0 && equal
)
4375 return isl_map_universe(dims
);
4377 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4379 for (i
= 0; i
+ 1 < n
; ++i
)
4380 map
= isl_map_add_basic_map(map
,
4381 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4384 map
= isl_map_add_basic_map(map
,
4385 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4387 map
= isl_map_add_basic_map(map
,
4388 isl_basic_map_less_at(dims
, n
- 1));
4390 isl_space_free(dims
);
4395 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4399 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4402 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4404 return map_lex_lte_first(dim
, n
, 0);
4407 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4409 return map_lex_lte_first(dim
, n
, 1);
4412 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4414 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4417 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4419 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4422 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4423 unsigned n
, int equal
)
4425 struct isl_map
*map
;
4428 if (n
== 0 && equal
)
4429 return isl_map_universe(dims
);
4431 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4433 for (i
= 0; i
+ 1 < n
; ++i
)
4434 map
= isl_map_add_basic_map(map
,
4435 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4438 map
= isl_map_add_basic_map(map
,
4439 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4441 map
= isl_map_add_basic_map(map
,
4442 isl_basic_map_more_at(dims
, n
- 1));
4444 isl_space_free(dims
);
4449 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4453 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4456 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4458 return map_lex_gte_first(dim
, n
, 0);
4461 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4463 return map_lex_gte_first(dim
, n
, 1);
4466 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4468 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4471 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4473 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4476 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4477 __isl_take isl_set
*set2
)
4480 map
= isl_map_lex_le(isl_set_get_space(set1
));
4481 map
= isl_map_intersect_domain(map
, set1
);
4482 map
= isl_map_intersect_range(map
, set2
);
4486 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4487 __isl_take isl_set
*set2
)
4490 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4491 map
= isl_map_intersect_domain(map
, set1
);
4492 map
= isl_map_intersect_range(map
, set2
);
4496 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4497 __isl_take isl_set
*set2
)
4500 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4501 map
= isl_map_intersect_domain(map
, set1
);
4502 map
= isl_map_intersect_range(map
, set2
);
4506 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4507 __isl_take isl_set
*set2
)
4510 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4511 map
= isl_map_intersect_domain(map
, set1
);
4512 map
= isl_map_intersect_range(map
, set2
);
4516 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4517 __isl_take isl_map
*map2
)
4520 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4521 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4522 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4526 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4527 __isl_take isl_map
*map2
)
4530 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4531 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4532 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4536 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4537 __isl_take isl_map
*map2
)
4540 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4541 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4542 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4546 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4547 __isl_take isl_map
*map2
)
4550 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4551 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4552 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4556 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4557 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4559 struct isl_basic_map
*bmap
;
4561 bset
= isl_basic_set_cow(bset
);
4565 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4567 isl_space_free(bset
->dim
);
4568 bmap
= bset_to_bmap(bset
);
4570 return isl_basic_map_finalize(bmap
);
4572 isl_basic_set_free(bset
);
4573 isl_space_free(dim
);
4577 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4578 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4580 return basic_map_from_basic_set(bset
, space
);
4583 /* For a div d = floor(f/m), add the constraint
4587 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4588 unsigned pos
, isl_int
*div
)
4591 unsigned total
= isl_basic_map_total_dim(bmap
);
4593 i
= isl_basic_map_alloc_inequality(bmap
);
4596 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4597 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4602 /* For a div d = floor(f/m), add the constraint
4604 * -(f-(m-1)) + m d >= 0
4606 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4607 unsigned pos
, isl_int
*div
)
4610 unsigned total
= isl_basic_map_total_dim(bmap
);
4612 i
= isl_basic_map_alloc_inequality(bmap
);
4615 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4616 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4617 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4618 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4623 /* For a div d = floor(f/m), add the constraints
4626 * -(f-(m-1)) + m d >= 0
4628 * Note that the second constraint is the negation of
4632 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4633 unsigned pos
, isl_int
*div
)
4635 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4637 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4642 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4643 unsigned pos
, isl_int
*div
)
4645 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4649 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4651 unsigned total
= isl_basic_map_total_dim(bmap
);
4652 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4654 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4658 /* For each known div d = floor(f/m), add the constraints
4661 * -(f-(m-1)) + m d >= 0
4663 * Remove duplicate constraints in case of some these div constraints
4664 * already appear in "bmap".
4666 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4667 __isl_take isl_basic_map
*bmap
)
4673 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4677 bmap
= add_known_div_constraints(bmap
);
4678 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4679 bmap
= isl_basic_map_finalize(bmap
);
4683 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4685 * In particular, if this div is of the form d = floor(f/m),
4686 * then add the constraint
4690 * if sign < 0 or the constraint
4692 * -(f-(m-1)) + m d >= 0
4696 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4697 unsigned div
, int sign
)
4705 total
= isl_basic_map_total_dim(bmap
);
4706 div_pos
= total
- bmap
->n_div
+ div
;
4709 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4711 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4714 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4716 return isl_basic_map_add_div_constraints(bset
, div
);
4719 struct isl_basic_set
*isl_basic_map_underlying_set(
4720 struct isl_basic_map
*bmap
)
4724 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4726 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4727 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4728 return bset_from_bmap(bmap
);
4729 bmap
= isl_basic_map_cow(bmap
);
4732 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4735 bmap
->extra
-= bmap
->n_div
;
4737 bmap
= isl_basic_map_finalize(bmap
);
4738 return bset_from_bmap(bmap
);
4740 isl_basic_map_free(bmap
);
4744 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4745 __isl_take isl_basic_set
*bset
)
4747 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4750 /* Replace each element in "list" by the result of applying
4751 * isl_basic_map_underlying_set to the element.
4753 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4754 __isl_take isl_basic_map_list
*list
)
4761 n
= isl_basic_map_list_n_basic_map(list
);
4762 for (i
= 0; i
< n
; ++i
) {
4763 isl_basic_map
*bmap
;
4764 isl_basic_set
*bset
;
4766 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4767 bset
= isl_basic_set_underlying_set(bmap
);
4768 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4774 struct isl_basic_map
*isl_basic_map_overlying_set(
4775 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4777 struct isl_basic_map
*bmap
;
4778 struct isl_ctx
*ctx
;
4785 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4786 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4787 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4789 if (like
->n_div
== 0) {
4790 isl_space
*space
= isl_basic_map_get_space(like
);
4791 isl_basic_map_free(like
);
4792 return isl_basic_map_reset_space(bset
, space
);
4794 bset
= isl_basic_set_cow(bset
);
4797 total
= bset
->dim
->n_out
+ bset
->extra
;
4798 bmap
= bset_to_bmap(bset
);
4799 isl_space_free(bmap
->dim
);
4800 bmap
->dim
= isl_space_copy(like
->dim
);
4803 bmap
->n_div
= like
->n_div
;
4804 bmap
->extra
+= like
->n_div
;
4808 ltotal
= total
- bmap
->extra
+ like
->extra
;
4811 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4812 bmap
->extra
* (1 + 1 + total
));
4813 if (isl_blk_is_error(bmap
->block2
))
4815 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4819 for (i
= 0; i
< bmap
->extra
; ++i
)
4820 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4821 for (i
= 0; i
< like
->n_div
; ++i
) {
4822 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4823 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4825 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4827 isl_basic_map_free(like
);
4828 bmap
= isl_basic_map_simplify(bmap
);
4829 bmap
= isl_basic_map_finalize(bmap
);
4832 isl_basic_map_free(like
);
4833 isl_basic_set_free(bset
);
4837 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4838 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4840 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
4841 bset_to_bmap(like
)));
4844 struct isl_set
*isl_set_from_underlying_set(
4845 struct isl_set
*set
, struct isl_basic_set
*like
)
4851 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4853 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4854 isl_basic_set_free(like
);
4857 set
= isl_set_cow(set
);
4860 for (i
= 0; i
< set
->n
; ++i
) {
4861 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4862 isl_basic_set_copy(like
));
4866 isl_space_free(set
->dim
);
4867 set
->dim
= isl_space_copy(like
->dim
);
4870 isl_basic_set_free(like
);
4873 isl_basic_set_free(like
);
4878 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4882 map
= isl_map_cow(map
);
4885 map
->dim
= isl_space_cow(map
->dim
);
4889 for (i
= 1; i
< map
->n
; ++i
)
4890 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4892 for (i
= 0; i
< map
->n
; ++i
) {
4893 map
->p
[i
] = bset_to_bmap(
4894 isl_basic_map_underlying_set(map
->p
[i
]));
4899 map
->dim
= isl_space_underlying(map
->dim
, 0);
4901 isl_space_free(map
->dim
);
4902 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4906 return set_from_map(map
);
4912 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4914 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
4917 /* Replace the space of "bmap" by "space".
4919 * If the space of "bmap" is identical to "space" (including the identifiers
4920 * of the input and output dimensions), then simply return the original input.
4922 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4923 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4929 equal
= isl_space_is_equal(bmap
->dim
, space
);
4930 if (equal
>= 0 && equal
)
4931 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4933 if (equal
>= 0 && equal
)
4934 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4935 space
, isl_dim_out
);
4939 isl_space_free(space
);
4942 bmap
= isl_basic_map_cow(bmap
);
4943 if (!bmap
|| !space
)
4946 isl_space_free(bmap
->dim
);
4949 bmap
= isl_basic_map_finalize(bmap
);
4953 isl_basic_map_free(bmap
);
4954 isl_space_free(space
);
4958 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4959 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4961 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
4965 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4966 __isl_take isl_space
*dim
)
4970 map
= isl_map_cow(map
);
4974 for (i
= 0; i
< map
->n
; ++i
) {
4975 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4976 isl_space_copy(dim
));
4980 isl_space_free(map
->dim
);
4986 isl_space_free(dim
);
4990 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4991 __isl_take isl_space
*dim
)
4993 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
4996 /* Compute the parameter domain of the given basic set.
4998 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5003 if (isl_basic_set_is_params(bset
))
5006 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5007 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5008 space
= isl_basic_set_get_space(bset
);
5009 space
= isl_space_params(space
);
5010 bset
= isl_basic_set_reset_space(bset
, space
);
5014 /* Construct a zero-dimensional basic set with the given parameter domain.
5016 __isl_give isl_basic_set
*isl_basic_set_from_params(
5017 __isl_take isl_basic_set
*bset
)
5020 space
= isl_basic_set_get_space(bset
);
5021 space
= isl_space_set_from_params(space
);
5022 bset
= isl_basic_set_reset_space(bset
, space
);
5026 /* Compute the parameter domain of the given set.
5028 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5033 if (isl_set_is_params(set
))
5036 n
= isl_set_dim(set
, isl_dim_set
);
5037 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5038 space
= isl_set_get_space(set
);
5039 space
= isl_space_params(space
);
5040 set
= isl_set_reset_space(set
, space
);
5044 /* Construct a zero-dimensional set with the given parameter domain.
5046 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5049 space
= isl_set_get_space(set
);
5050 space
= isl_space_set_from_params(space
);
5051 set
= isl_set_reset_space(set
, space
);
5055 /* Compute the parameter domain of the given map.
5057 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5062 n
= isl_map_dim(map
, isl_dim_in
);
5063 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5064 n
= isl_map_dim(map
, isl_dim_out
);
5065 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5066 space
= isl_map_get_space(map
);
5067 space
= isl_space_params(space
);
5068 map
= isl_map_reset_space(map
, space
);
5072 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5079 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5081 n_out
= isl_basic_map_n_out(bmap
);
5082 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5084 return isl_basic_map_reset_space(bmap
, space
);
5087 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5091 return isl_space_may_be_set(bmap
->dim
);
5094 /* Is this basic map actually a set?
5095 * Users should never call this function. Outside of isl,
5096 * the type should indicate whether something is a set or a map.
5098 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5102 return isl_space_is_set(bmap
->dim
);
5105 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5109 if (isl_basic_map_is_set(bmap
))
5111 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5114 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5115 __isl_take isl_basic_map
*bmap
)
5119 isl_basic_map
*domain
;
5120 int nparam
, n_in
, n_out
;
5123 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5124 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5125 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5127 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5128 domain
= isl_basic_map_universe(dim
);
5130 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5131 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5132 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5134 total
= isl_basic_map_total_dim(bmap
);
5136 for (i
= 0; i
< n_in
; ++i
) {
5137 k
= isl_basic_map_alloc_equality(bmap
);
5140 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5141 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5142 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5145 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5146 return isl_basic_map_finalize(bmap
);
5148 isl_basic_map_free(bmap
);
5152 __isl_give isl_basic_map
*isl_basic_map_range_map(
5153 __isl_take isl_basic_map
*bmap
)
5157 isl_basic_map
*range
;
5158 int nparam
, n_in
, n_out
;
5161 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5162 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5163 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5165 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5166 range
= isl_basic_map_universe(dim
);
5168 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5169 bmap
= isl_basic_map_apply_range(bmap
, range
);
5170 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5172 total
= isl_basic_map_total_dim(bmap
);
5174 for (i
= 0; i
< n_out
; ++i
) {
5175 k
= isl_basic_map_alloc_equality(bmap
);
5178 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5179 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5180 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5183 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5184 return isl_basic_map_finalize(bmap
);
5186 isl_basic_map_free(bmap
);
5190 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5194 return isl_space_may_be_set(map
->dim
);
5197 /* Is this map actually a set?
5198 * Users should never call this function. Outside of isl,
5199 * the type should indicate whether something is a set or a map.
5201 int isl_map_is_set(__isl_keep isl_map
*map
)
5205 return isl_space_is_set(map
->dim
);
5208 struct isl_set
*isl_map_range(struct isl_map
*map
)
5211 struct isl_set
*set
;
5215 if (isl_map_is_set(map
))
5216 return set_from_map(map
);
5218 map
= isl_map_cow(map
);
5222 set
= set_from_map(map
);
5223 set
->dim
= isl_space_range(set
->dim
);
5226 for (i
= 0; i
< map
->n
; ++i
) {
5227 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5231 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5232 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5239 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5243 map
= isl_map_cow(map
);
5247 map
->dim
= isl_space_domain_map(map
->dim
);
5250 for (i
= 0; i
< map
->n
; ++i
) {
5251 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5255 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5256 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5263 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5266 isl_space
*range_dim
;
5268 map
= isl_map_cow(map
);
5272 range_dim
= isl_space_range(isl_map_get_space(map
));
5273 range_dim
= isl_space_from_range(range_dim
);
5274 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5275 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5278 for (i
= 0; i
< map
->n
; ++i
) {
5279 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5283 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5284 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5291 /* Given a wrapped map of the form A[B -> C],
5292 * return the map A[B -> C] -> B.
5294 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5301 if (!isl_set_has_tuple_id(set
))
5302 return isl_map_domain_map(isl_set_unwrap(set
));
5304 id
= isl_set_get_tuple_id(set
);
5305 map
= isl_map_domain_map(isl_set_unwrap(set
));
5306 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5311 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5312 __isl_take isl_space
*dim
)
5315 struct isl_map
*map
= NULL
;
5317 set
= isl_set_cow(set
);
5320 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5322 map
= set_to_map(set
);
5323 for (i
= 0; i
< set
->n
; ++i
) {
5324 map
->p
[i
] = basic_map_from_basic_set(
5325 set
->p
[i
], isl_space_copy(dim
));
5329 isl_space_free(map
->dim
);
5333 isl_space_free(dim
);
5338 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5339 __isl_take isl_basic_set
*bset
)
5341 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5344 __isl_give isl_basic_map
*isl_basic_map_from_range(
5345 __isl_take isl_basic_set
*bset
)
5348 space
= isl_basic_set_get_space(bset
);
5349 space
= isl_space_from_range(space
);
5350 bset
= isl_basic_set_reset_space(bset
, space
);
5351 return bset_to_bmap(bset
);
5354 /* Create a relation with the given set as range.
5355 * The domain of the created relation is a zero-dimensional
5356 * flat anonymous space.
5358 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5361 space
= isl_set_get_space(set
);
5362 space
= isl_space_from_range(space
);
5363 set
= isl_set_reset_space(set
, space
);
5364 return set_to_map(set
);
5367 /* Create a relation with the given set as domain.
5368 * The range of the created relation is a zero-dimensional
5369 * flat anonymous space.
5371 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5373 return isl_map_reverse(isl_map_from_range(set
));
5376 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5377 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5379 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5382 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5383 __isl_take isl_set
*range
)
5385 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5388 /* Return a newly allocated isl_map with given space and flags and
5389 * room for "n" basic maps.
5390 * Make sure that all cached information is cleared.
5392 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5395 struct isl_map
*map
;
5400 isl_die(space
->ctx
, isl_error_internal
,
5401 "negative number of basic maps", goto error
);
5402 map
= isl_calloc(space
->ctx
, struct isl_map
,
5403 sizeof(struct isl_map
) +
5404 (n
- 1) * sizeof(struct isl_basic_map
*));
5408 map
->ctx
= space
->ctx
;
5409 isl_ctx_ref(map
->ctx
);
5417 isl_space_free(space
);
5421 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5422 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5425 struct isl_map
*map
;
5428 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5432 map
= isl_map_alloc_space(dims
, n
, flags
);
5436 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5438 struct isl_basic_map
*bmap
;
5439 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5440 bmap
= isl_basic_map_set_to_empty(bmap
);
5444 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5446 struct isl_basic_set
*bset
;
5447 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5448 bset
= isl_basic_set_set_to_empty(bset
);
5452 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5454 struct isl_basic_map
*bmap
;
5455 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5456 bmap
= isl_basic_map_finalize(bmap
);
5460 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5462 struct isl_basic_set
*bset
;
5463 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5464 bset
= isl_basic_set_finalize(bset
);
5468 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5471 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5472 isl_basic_map
*bmap
;
5474 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5475 for (i
= 0; i
< total
; ++i
) {
5476 int k
= isl_basic_map_alloc_inequality(bmap
);
5479 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5480 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5484 isl_basic_map_free(bmap
);
5488 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5490 return isl_basic_map_nat_universe(dim
);
5493 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5495 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5498 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5500 return isl_map_nat_universe(dim
);
5503 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5505 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5508 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5510 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5513 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5515 struct isl_map
*map
;
5518 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5519 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5523 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5525 struct isl_set
*set
;
5528 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5529 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5533 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5536 struct isl_map
*dup
;
5540 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5541 for (i
= 0; i
< map
->n
; ++i
)
5542 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5546 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5547 __isl_take isl_basic_map
*bmap
)
5551 if (isl_basic_map_plain_is_empty(bmap
)) {
5552 isl_basic_map_free(bmap
);
5555 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5556 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5557 map
->p
[map
->n
] = bmap
;
5559 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5565 isl_basic_map_free(bmap
);
5569 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5580 isl_ctx_deref(map
->ctx
);
5581 for (i
= 0; i
< map
->n
; ++i
)
5582 isl_basic_map_free(map
->p
[i
]);
5583 isl_space_free(map
->dim
);
5589 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5590 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5594 bmap
= isl_basic_map_cow(bmap
);
5595 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5596 j
= isl_basic_map_alloc_equality(bmap
);
5599 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5600 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5601 isl_int_set_si(bmap
->eq
[j
][0], value
);
5602 bmap
= isl_basic_map_simplify(bmap
);
5603 return isl_basic_map_finalize(bmap
);
5605 isl_basic_map_free(bmap
);
5609 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5610 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5614 bmap
= isl_basic_map_cow(bmap
);
5615 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5616 j
= isl_basic_map_alloc_equality(bmap
);
5619 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5620 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5621 isl_int_set(bmap
->eq
[j
][0], value
);
5622 bmap
= isl_basic_map_simplify(bmap
);
5623 return isl_basic_map_finalize(bmap
);
5625 isl_basic_map_free(bmap
);
5629 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5630 enum isl_dim_type type
, unsigned pos
, int value
)
5634 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5635 return isl_basic_map_fix_pos_si(bmap
,
5636 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5638 isl_basic_map_free(bmap
);
5642 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5643 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5647 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5648 return isl_basic_map_fix_pos(bmap
,
5649 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5651 isl_basic_map_free(bmap
);
5655 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5656 * to be equal to "v".
5658 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5659 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5663 if (!isl_val_is_int(v
))
5664 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5665 "expecting integer value", goto error
);
5666 if (pos
>= isl_basic_map_dim(bmap
, type
))
5667 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5668 "index out of bounds", goto error
);
5669 pos
+= isl_basic_map_offset(bmap
, type
);
5670 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5674 isl_basic_map_free(bmap
);
5679 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5680 * to be equal to "v".
5682 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5683 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5685 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5688 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5689 enum isl_dim_type type
, unsigned pos
, int value
)
5691 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5695 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5696 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5698 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5702 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5703 unsigned input
, int value
)
5705 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5708 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5709 unsigned dim
, int value
)
5711 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5712 isl_dim_set
, dim
, value
));
5715 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5717 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5724 isl_basic_map_free(map
->p
[i
]);
5725 if (i
!= map
->n
- 1) {
5726 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5727 map
->p
[i
] = map
->p
[map
->n
- 1];
5734 /* Perform "fn" on each basic map of "map", where we may not be holding
5735 * the only reference to "map".
5736 * In particular, "fn" should be a semantics preserving operation
5737 * that we want to apply to all copies of "map". We therefore need
5738 * to be careful not to modify "map" in a way that breaks "map"
5739 * in case anything goes wrong.
5741 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5742 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5744 struct isl_basic_map
*bmap
;
5750 for (i
= map
->n
- 1; i
>= 0; --i
) {
5751 bmap
= isl_basic_map_copy(map
->p
[i
]);
5755 isl_basic_map_free(map
->p
[i
]);
5757 if (remove_if_empty(map
, i
) < 0)
5767 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5768 enum isl_dim_type type
, unsigned pos
, int value
)
5772 map
= isl_map_cow(map
);
5776 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5777 for (i
= map
->n
- 1; i
>= 0; --i
) {
5778 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5779 if (remove_if_empty(map
, i
) < 0)
5782 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5789 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5790 enum isl_dim_type type
, unsigned pos
, int value
)
5792 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5795 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5796 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5800 map
= isl_map_cow(map
);
5804 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5805 for (i
= 0; i
< map
->n
; ++i
) {
5806 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5810 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5817 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5818 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5820 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5823 /* Fix the value of the variable at position "pos" of type "type" of "map"
5824 * to be equal to "v".
5826 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5827 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5831 map
= isl_map_cow(map
);
5835 if (!isl_val_is_int(v
))
5836 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5837 "expecting integer value", goto error
);
5838 if (pos
>= isl_map_dim(map
, type
))
5839 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5840 "index out of bounds", goto error
);
5841 for (i
= map
->n
- 1; i
>= 0; --i
) {
5842 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5844 if (remove_if_empty(map
, i
) < 0)
5847 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5856 /* Fix the value of the variable at position "pos" of type "type" of "set"
5857 * to be equal to "v".
5859 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5860 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5862 return isl_map_fix_val(set
, type
, pos
, v
);
5865 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5866 unsigned input
, int value
)
5868 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5871 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5873 return set_from_map(isl_map_fix_si(set_to_map(set
),
5874 isl_dim_set
, dim
, value
));
5877 static __isl_give isl_basic_map
*basic_map_bound_si(
5878 __isl_take isl_basic_map
*bmap
,
5879 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5885 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5886 pos
+= isl_basic_map_offset(bmap
, type
);
5887 bmap
= isl_basic_map_cow(bmap
);
5888 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5889 j
= isl_basic_map_alloc_inequality(bmap
);
5892 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5894 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5895 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5897 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5898 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5900 bmap
= isl_basic_map_simplify(bmap
);
5901 return isl_basic_map_finalize(bmap
);
5903 isl_basic_map_free(bmap
);
5907 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5908 __isl_take isl_basic_map
*bmap
,
5909 enum isl_dim_type type
, unsigned pos
, int value
)
5911 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5914 /* Constrain the values of the given dimension to be no greater than "value".
5916 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5917 __isl_take isl_basic_map
*bmap
,
5918 enum isl_dim_type type
, unsigned pos
, int value
)
5920 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5923 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5924 unsigned dim
, isl_int value
)
5928 bset
= isl_basic_set_cow(bset
);
5929 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5930 j
= isl_basic_set_alloc_inequality(bset
);
5933 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5934 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5935 isl_int_neg(bset
->ineq
[j
][0], value
);
5936 bset
= isl_basic_set_simplify(bset
);
5937 return isl_basic_set_finalize(bset
);
5939 isl_basic_set_free(bset
);
5943 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5944 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5948 map
= isl_map_cow(map
);
5952 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5953 for (i
= 0; i
< map
->n
; ++i
) {
5954 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5955 type
, pos
, value
, upper
);
5959 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5966 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5967 enum isl_dim_type type
, unsigned pos
, int value
)
5969 return map_bound_si(map
, type
, pos
, value
, 0);
5972 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5973 enum isl_dim_type type
, unsigned pos
, int value
)
5975 return map_bound_si(map
, type
, pos
, value
, 1);
5978 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5979 enum isl_dim_type type
, unsigned pos
, int value
)
5981 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
5985 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5986 enum isl_dim_type type
, unsigned pos
, int value
)
5988 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5991 /* Bound the given variable of "bmap" from below (or above is "upper"
5992 * is set) to "value".
5994 static __isl_give isl_basic_map
*basic_map_bound(
5995 __isl_take isl_basic_map
*bmap
,
5996 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6002 if (pos
>= isl_basic_map_dim(bmap
, type
))
6003 isl_die(bmap
->ctx
, isl_error_invalid
,
6004 "index out of bounds", goto error
);
6005 pos
+= isl_basic_map_offset(bmap
, type
);
6006 bmap
= isl_basic_map_cow(bmap
);
6007 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6008 j
= isl_basic_map_alloc_inequality(bmap
);
6011 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6013 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6014 isl_int_set(bmap
->ineq
[j
][0], value
);
6016 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6017 isl_int_neg(bmap
->ineq
[j
][0], value
);
6019 bmap
= isl_basic_map_simplify(bmap
);
6020 return isl_basic_map_finalize(bmap
);
6022 isl_basic_map_free(bmap
);
6026 /* Bound the given variable of "map" from below (or above is "upper"
6027 * is set) to "value".
6029 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6030 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6034 map
= isl_map_cow(map
);
6038 if (pos
>= isl_map_dim(map
, type
))
6039 isl_die(map
->ctx
, isl_error_invalid
,
6040 "index out of bounds", goto error
);
6041 for (i
= map
->n
- 1; i
>= 0; --i
) {
6042 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6043 if (remove_if_empty(map
, i
) < 0)
6046 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6053 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6054 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6056 return map_bound(map
, type
, pos
, value
, 0);
6059 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6060 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6062 return map_bound(map
, type
, pos
, value
, 1);
6065 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6066 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6068 return isl_map_lower_bound(set
, type
, pos
, value
);
6071 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6072 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6074 return isl_map_upper_bound(set
, type
, pos
, value
);
6077 /* Force the values of the variable at position "pos" of type "type" of "set"
6078 * to be no smaller than "value".
6080 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6081 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6085 if (!isl_val_is_int(value
))
6086 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6087 "expecting integer value", goto error
);
6088 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6089 isl_val_free(value
);
6092 isl_val_free(value
);
6097 /* Force the values of the variable at position "pos" of type "type" of "set"
6098 * to be no greater than "value".
6100 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6101 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6105 if (!isl_val_is_int(value
))
6106 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6107 "expecting integer value", goto error
);
6108 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6109 isl_val_free(value
);
6112 isl_val_free(value
);
6117 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6122 set
= isl_set_cow(set
);
6126 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6127 for (i
= 0; i
< set
->n
; ++i
) {
6128 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6138 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6142 map
= isl_map_cow(map
);
6146 map
->dim
= isl_space_reverse(map
->dim
);
6149 for (i
= 0; i
< map
->n
; ++i
) {
6150 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6154 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6162 #define TYPE isl_pw_multi_aff
6164 #define SUFFIX _pw_multi_aff
6166 #define EMPTY isl_pw_multi_aff_empty
6168 #define ADD isl_pw_multi_aff_union_add
6169 #include "isl_map_lexopt_templ.c"
6171 /* Given a map "map", compute the lexicographically minimal
6172 * (or maximal) image element for each domain element in dom,
6173 * in the form of an isl_pw_multi_aff.
6174 * If "empty" is not NULL, then set *empty to those elements in dom that
6175 * do not have an image element.
6176 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6177 * should be computed over the domain of "map". "empty" is also NULL
6180 * We first compute the lexicographically minimal or maximal element
6181 * in the first basic map. This results in a partial solution "res"
6182 * and a subset "todo" of dom that still need to be handled.
6183 * We then consider each of the remaining maps in "map" and successively
6184 * update both "res" and "todo".
6185 * If "empty" is NULL, then the todo sets are not needed and therefore
6186 * also not computed.
6188 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6189 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6190 __isl_give isl_set
**empty
, unsigned flags
)
6194 isl_pw_multi_aff
*res
;
6197 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6198 if (!map
|| (!full
&& !dom
))
6201 if (isl_map_plain_is_empty(map
)) {
6206 return isl_pw_multi_aff_from_map(map
);
6209 res
= basic_map_partial_lexopt_pw_multi_aff(
6210 isl_basic_map_copy(map
->p
[0]),
6211 isl_set_copy(dom
), empty
, flags
);
6215 for (i
= 1; i
< map
->n
; ++i
) {
6216 isl_pw_multi_aff
*res_i
;
6218 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6219 isl_basic_map_copy(map
->p
[i
]),
6220 isl_set_copy(dom
), empty
, flags
);
6222 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6223 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6225 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6228 todo
= isl_set_intersect(todo
, *empty
);
6247 #define TYPE isl_map
6251 #define EMPTY isl_map_empty
6253 #define ADD isl_map_union_disjoint
6254 #include "isl_map_lexopt_templ.c"
6256 /* Given a map "map", compute the lexicographically minimal
6257 * (or maximal) image element for each domain element in "dom",
6258 * in the form of an isl_map.
6259 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6260 * do not have an image element.
6261 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6262 * should be computed over the domain of "map". "empty" is also NULL
6265 * If the input consists of more than one disjunct, then first
6266 * compute the desired result in the form of an isl_pw_multi_aff and
6267 * then convert that into an isl_map.
6269 * This function used to have an explicit implementation in terms
6270 * of isl_maps, but it would continually intersect the domains of
6271 * partial results with the complement of the domain of the next
6272 * partial solution, potentially leading to an explosion in the number
6273 * of disjuncts if there are several disjuncts in the input.
6274 * An even earlier implementation of this function would look for
6275 * better results in the domain of the partial result and for extra
6276 * results in the complement of this domain, which would lead to
6277 * even more splintering.
6279 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6280 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6281 __isl_give isl_set
**empty
, unsigned flags
)
6284 struct isl_map
*res
;
6285 isl_pw_multi_aff
*pma
;
6287 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6288 if (!map
|| (!full
&& !dom
))
6291 if (isl_map_plain_is_empty(map
)) {
6300 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6306 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6308 return isl_map_from_pw_multi_aff(pma
);
6317 __isl_give isl_map
*isl_map_partial_lexmax(
6318 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6319 __isl_give isl_set
**empty
)
6321 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6324 __isl_give isl_map
*isl_map_partial_lexmin(
6325 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6326 __isl_give isl_set
**empty
)
6328 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6331 __isl_give isl_set
*isl_set_partial_lexmin(
6332 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6333 __isl_give isl_set
**empty
)
6335 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6339 __isl_give isl_set
*isl_set_partial_lexmax(
6340 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6341 __isl_give isl_set
**empty
)
6343 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6347 /* Compute the lexicographic minimum (or maximum if "flags" includes
6348 * ISL_OPT_MAX) of "bset" over its parametric domain.
6350 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6353 return isl_basic_map_lexopt(bset
, flags
);
6356 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6358 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6361 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6363 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6366 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6368 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6371 /* Compute the lexicographic minimum of "bset" over its parametric domain
6372 * for the purpose of quantifier elimination.
6373 * That is, find an explicit representation for all the existentially
6374 * quantified variables in "bset" by computing their lexicographic
6377 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6378 __isl_take isl_basic_set
*bset
)
6380 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6383 /* Extract the first and only affine expression from list
6384 * and then add it to *pwaff with the given dom.
6385 * This domain is known to be disjoint from other domains
6386 * because of the way isl_basic_map_foreach_lexmax works.
6388 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6389 __isl_take isl_aff_list
*list
, void *user
)
6391 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6393 isl_pw_aff
**pwaff
= user
;
6394 isl_pw_aff
*pwaff_i
;
6398 if (isl_aff_list_n_aff(list
) != 1)
6399 isl_die(ctx
, isl_error_internal
,
6400 "expecting single element list", goto error
);
6402 aff
= isl_aff_list_get_aff(list
, 0);
6403 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6405 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6407 isl_aff_list_free(list
);
6411 isl_basic_set_free(dom
);
6412 isl_aff_list_free(list
);
6416 /* Given a basic map with one output dimension, compute the minimum or
6417 * maximum of that dimension as an isl_pw_aff.
6419 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6420 * call update_dim_opt on each leaf of the result.
6422 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6425 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6429 dim
= isl_space_from_domain(isl_space_domain(dim
));
6430 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6431 pwaff
= isl_pw_aff_empty(dim
);
6433 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6435 return isl_pw_aff_free(pwaff
);
6440 /* Compute the minimum or maximum of the given output dimension
6441 * as a function of the parameters and the input dimensions,
6442 * but independently of the other output dimensions.
6444 * We first project out the other output dimension and then compute
6445 * the "lexicographic" maximum in each basic map, combining the results
6446 * using isl_pw_aff_union_max.
6448 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6455 n_out
= isl_map_dim(map
, isl_dim_out
);
6456 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6457 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6462 isl_space
*dim
= isl_map_get_space(map
);
6464 return isl_pw_aff_empty(dim
);
6467 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6468 for (i
= 1; i
< map
->n
; ++i
) {
6469 isl_pw_aff
*pwaff_i
;
6471 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6472 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6480 /* Compute the minimum of the given output dimension as a function of the
6481 * parameters and input dimensions, but independently of
6482 * the other output dimensions.
6484 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6486 return map_dim_opt(map
, pos
, 0);
6489 /* Compute the maximum of the given output dimension as a function of the
6490 * parameters and input dimensions, but independently of
6491 * the other output dimensions.
6493 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6495 return map_dim_opt(map
, pos
, 1);
6498 /* Compute the minimum or maximum of the given set dimension
6499 * as a function of the parameters,
6500 * but independently of the other set dimensions.
6502 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6505 return map_dim_opt(set
, pos
, max
);
6508 /* Compute the maximum of the given set dimension as a function of the
6509 * parameters, but independently of the other set dimensions.
6511 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6513 return set_dim_opt(set
, pos
, 1);
6516 /* Compute the minimum of the given set dimension as a function of the
6517 * parameters, but independently of the other set dimensions.
6519 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6521 return set_dim_opt(set
, pos
, 0);
6524 /* Apply a preimage specified by "mat" on the parameters of "bset".
6525 * bset is assumed to have only parameters and divs.
6527 static struct isl_basic_set
*basic_set_parameter_preimage(
6528 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6535 bset
->dim
= isl_space_cow(bset
->dim
);
6539 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6541 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6543 bset
->dim
->nparam
= 0;
6544 bset
->dim
->n_out
= nparam
;
6545 bset
= isl_basic_set_preimage(bset
, mat
);
6547 bset
->dim
->nparam
= bset
->dim
->n_out
;
6548 bset
->dim
->n_out
= 0;
6553 isl_basic_set_free(bset
);
6557 /* Apply a preimage specified by "mat" on the parameters of "set".
6558 * set is assumed to have only parameters and divs.
6560 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6561 __isl_take isl_mat
*mat
)
6569 nparam
= isl_set_dim(set
, isl_dim_param
);
6571 if (mat
->n_row
!= 1 + nparam
)
6572 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6573 "unexpected number of rows", goto error
);
6575 space
= isl_set_get_space(set
);
6576 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6577 isl_dim_param
, 0, nparam
);
6578 set
= isl_set_reset_space(set
, space
);
6579 set
= isl_set_preimage(set
, mat
);
6580 nparam
= isl_set_dim(set
, isl_dim_out
);
6581 space
= isl_set_get_space(set
);
6582 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6583 isl_dim_out
, 0, nparam
);
6584 set
= isl_set_reset_space(set
, space
);
6592 /* Intersect the basic set "bset" with the affine space specified by the
6593 * equalities in "eq".
6595 static struct isl_basic_set
*basic_set_append_equalities(
6596 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6604 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6609 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6610 for (i
= 0; i
< eq
->n_row
; ++i
) {
6611 k
= isl_basic_set_alloc_equality(bset
);
6614 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6615 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6619 bset
= isl_basic_set_gauss(bset
, NULL
);
6620 bset
= isl_basic_set_finalize(bset
);
6625 isl_basic_set_free(bset
);
6629 /* Intersect the set "set" with the affine space specified by the
6630 * equalities in "eq".
6632 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6640 for (i
= 0; i
< set
->n
; ++i
) {
6641 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6654 /* Given a basic set "bset" that only involves parameters and existentially
6655 * quantified variables, return the index of the first equality
6656 * that only involves parameters. If there is no such equality then
6657 * return bset->n_eq.
6659 * This function assumes that isl_basic_set_gauss has been called on "bset".
6661 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6664 unsigned nparam
, n_div
;
6669 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6670 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6672 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6673 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6680 /* Compute an explicit representation for the existentially quantified
6681 * variables in "bset" by computing the "minimal value" of the set
6682 * variables. Since there are no set variables, the computation of
6683 * the minimal value essentially computes an explicit representation
6684 * of the non-empty part(s) of "bset".
6686 * The input only involves parameters and existentially quantified variables.
6687 * All equalities among parameters have been removed.
6689 * Since the existentially quantified variables in the result are in general
6690 * going to be different from those in the input, we first replace
6691 * them by the minimal number of variables based on their equalities.
6692 * This should simplify the parametric integer programming.
6694 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6696 isl_morph
*morph1
, *morph2
;
6702 if (bset
->n_eq
== 0)
6703 return isl_basic_set_lexmin_compute_divs(bset
);
6705 morph1
= isl_basic_set_parameter_compression(bset
);
6706 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6707 bset
= isl_basic_set_lift(bset
);
6708 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6709 bset
= isl_morph_basic_set(morph2
, bset
);
6710 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6711 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6713 set
= isl_basic_set_lexmin_compute_divs(bset
);
6715 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6720 /* Project the given basic set onto its parameter domain, possibly introducing
6721 * new, explicit, existential variables in the constraints.
6722 * The input has parameters and (possibly implicit) existential variables.
6723 * The output has the same parameters, but only
6724 * explicit existentially quantified variables.
6726 * The actual projection is performed by pip, but pip doesn't seem
6727 * to like equalities very much, so we first remove the equalities
6728 * among the parameters by performing a variable compression on
6729 * the parameters. Afterward, an inverse transformation is performed
6730 * and the equalities among the parameters are inserted back in.
6732 * The variable compression on the parameters may uncover additional
6733 * equalities that were only implicit before. We therefore check
6734 * if there are any new parameter equalities in the result and
6735 * if so recurse. The removal of parameter equalities is required
6736 * for the parameter compression performed by base_compute_divs.
6738 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6742 struct isl_mat
*T
, *T2
;
6743 struct isl_set
*set
;
6746 bset
= isl_basic_set_cow(bset
);
6750 if (bset
->n_eq
== 0)
6751 return base_compute_divs(bset
);
6753 bset
= isl_basic_set_gauss(bset
, NULL
);
6756 if (isl_basic_set_plain_is_empty(bset
))
6757 return isl_set_from_basic_set(bset
);
6759 i
= first_parameter_equality(bset
);
6760 if (i
== bset
->n_eq
)
6761 return base_compute_divs(bset
);
6763 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6764 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6766 eq
= isl_mat_cow(eq
);
6767 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6768 if (T
&& T
->n_col
== 0) {
6772 bset
= isl_basic_set_set_to_empty(bset
);
6773 return isl_set_from_basic_set(bset
);
6775 bset
= basic_set_parameter_preimage(bset
, T
);
6777 i
= first_parameter_equality(bset
);
6780 else if (i
== bset
->n_eq
)
6781 set
= base_compute_divs(bset
);
6783 set
= parameter_compute_divs(bset
);
6784 set
= set_parameter_preimage(set
, T2
);
6785 set
= set_append_equalities(set
, eq
);
6789 /* Insert the divs from "ls" before those of "bmap".
6791 * The number of columns is not changed, which means that the last
6792 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6793 * The caller is responsible for removing the same number of dimensions
6794 * from the space of "bmap".
6796 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6797 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6803 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6807 old_n_div
= bmap
->n_div
;
6808 bmap
= insert_div_rows(bmap
, n_div
);
6812 for (i
= 0; i
< n_div
; ++i
) {
6813 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6814 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6820 /* Replace the space of "bmap" by the space and divs of "ls".
6822 * If "ls" has any divs, then we simplify the result since we may
6823 * have discovered some additional equalities that could simplify
6824 * the div expressions.
6826 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6827 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6831 bmap
= isl_basic_map_cow(bmap
);
6835 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6836 bmap
= insert_divs_from_local_space(bmap
, ls
);
6840 isl_space_free(bmap
->dim
);
6841 bmap
->dim
= isl_local_space_get_space(ls
);
6845 isl_local_space_free(ls
);
6847 bmap
= isl_basic_map_simplify(bmap
);
6848 bmap
= isl_basic_map_finalize(bmap
);
6851 isl_basic_map_free(bmap
);
6852 isl_local_space_free(ls
);
6856 /* Replace the space of "map" by the space and divs of "ls".
6858 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6859 __isl_take isl_local_space
*ls
)
6863 map
= isl_map_cow(map
);
6867 for (i
= 0; i
< map
->n
; ++i
) {
6868 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6869 isl_local_space_copy(ls
));
6873 isl_space_free(map
->dim
);
6874 map
->dim
= isl_local_space_get_space(ls
);
6878 isl_local_space_free(ls
);
6881 isl_local_space_free(ls
);
6886 /* Compute an explicit representation for the existentially
6887 * quantified variables for which do not know any explicit representation yet.
6889 * We first sort the existentially quantified variables so that the
6890 * existentially quantified variables for which we already have an explicit
6891 * representation are placed before those for which we do not.
6892 * The input dimensions, the output dimensions and the existentially
6893 * quantified variables for which we already have an explicit
6894 * representation are then turned into parameters.
6895 * compute_divs returns a map with the same parameters and
6896 * no input or output dimensions and the dimension specification
6897 * is reset to that of the input, including the existentially quantified
6898 * variables for which we already had an explicit representation.
6900 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6902 struct isl_basic_set
*bset
;
6903 struct isl_set
*set
;
6904 struct isl_map
*map
;
6906 isl_local_space
*ls
;
6913 bmap
= isl_basic_map_sort_divs(bmap
);
6914 bmap
= isl_basic_map_cow(bmap
);
6918 n_known
= isl_basic_map_first_unknown_div(bmap
);
6920 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6922 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6923 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6924 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6925 dim
= isl_space_set_alloc(bmap
->ctx
,
6926 nparam
+ n_in
+ n_out
+ n_known
, 0);
6930 ls
= isl_basic_map_get_local_space(bmap
);
6931 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6932 n_known
, bmap
->n_div
- n_known
);
6934 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6935 swap_div(bmap
, i
- n_known
, i
);
6936 bmap
->n_div
-= n_known
;
6937 bmap
->extra
-= n_known
;
6939 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6940 bset
= bset_from_bmap(bmap
);
6942 set
= parameter_compute_divs(bset
);
6943 map
= set_to_map(set
);
6944 map
= replace_space_by_local_space(map
, ls
);
6948 isl_basic_map_free(bmap
);
6952 /* Remove the explicit representation of local variable "div",
6955 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6956 __isl_take isl_basic_map
*bmap
, int div
)
6960 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6962 return isl_basic_map_free(bmap
);
6966 bmap
= isl_basic_map_cow(bmap
);
6969 isl_int_set_si(bmap
->div
[div
][0], 0);
6973 /* Is local variable "div" of "bmap" marked as not having an explicit
6975 * Note that even if "div" is not marked in this way and therefore
6976 * has an explicit representation, this representation may still
6977 * depend (indirectly) on other local variables that do not
6978 * have an explicit representation.
6980 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6984 return isl_bool_error
;
6985 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6986 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6987 "position out of bounds", return isl_bool_error
);
6988 return isl_int_is_zero(bmap
->div
[div
][0]);
6991 /* Return the position of the first local variable that does not
6992 * have an explicit representation.
6993 * Return the total number of local variables if they all have
6994 * an explicit representation.
6995 * Return -1 on error.
6997 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7004 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7005 if (!isl_basic_map_div_is_known(bmap
, i
))
7011 /* Return the position of the first local variable that does not
7012 * have an explicit representation.
7013 * Return the total number of local variables if they all have
7014 * an explicit representation.
7015 * Return -1 on error.
7017 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7019 return isl_basic_map_first_unknown_div(bset
);
7022 /* Does "bmap" have an explicit representation for all local variables?
7024 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7028 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7029 first
= isl_basic_map_first_unknown_div(bmap
);
7031 return isl_bool_error
;
7035 /* Do all basic maps in "map" have an explicit representation
7036 * for all local variables?
7038 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7043 return isl_bool_error
;
7045 for (i
= 0; i
< map
->n
; ++i
) {
7046 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7051 return isl_bool_true
;
7054 /* If bmap contains any unknown divs, then compute explicit
7055 * expressions for them. However, this computation may be
7056 * quite expensive, so first try to remove divs that aren't
7059 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7062 struct isl_map
*map
;
7064 known
= isl_basic_map_divs_known(bmap
);
7068 return isl_map_from_basic_map(bmap
);
7070 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7072 known
= isl_basic_map_divs_known(bmap
);
7076 return isl_map_from_basic_map(bmap
);
7078 map
= compute_divs(bmap
);
7081 isl_basic_map_free(bmap
);
7085 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7089 struct isl_map
*res
;
7096 known
= isl_map_divs_known(map
);
7104 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7105 for (i
= 1 ; i
< map
->n
; ++i
) {
7107 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7108 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7109 res
= isl_map_union_disjoint(res
, r2
);
7111 res
= isl_map_union(res
, r2
);
7118 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7120 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7123 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7125 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7128 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7131 struct isl_set
*set
;
7136 map
= isl_map_cow(map
);
7140 set
= set_from_map(map
);
7141 set
->dim
= isl_space_domain(set
->dim
);
7144 for (i
= 0; i
< map
->n
; ++i
) {
7145 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7149 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7150 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7157 /* Return the union of "map1" and "map2", where we assume for now that
7158 * "map1" and "map2" are disjoint. Note that the basic maps inside
7159 * "map1" or "map2" may not be disjoint from each other.
7160 * Also note that this function is also called from isl_map_union,
7161 * which takes care of handling the situation where "map1" and "map2"
7162 * may not be disjoint.
7164 * If one of the inputs is empty, we can simply return the other input.
7165 * Similarly, if one of the inputs is universal, then it is equal to the union.
7167 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7168 __isl_take isl_map
*map2
)
7172 struct isl_map
*map
= NULL
;
7178 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7179 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7180 "spaces don't match", goto error
);
7191 is_universe
= isl_map_plain_is_universe(map1
);
7192 if (is_universe
< 0)
7199 is_universe
= isl_map_plain_is_universe(map2
);
7200 if (is_universe
< 0)
7207 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7208 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7209 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7211 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7212 map1
->n
+ map2
->n
, flags
);
7215 for (i
= 0; i
< map1
->n
; ++i
) {
7216 map
= isl_map_add_basic_map(map
,
7217 isl_basic_map_copy(map1
->p
[i
]));
7221 for (i
= 0; i
< map2
->n
; ++i
) {
7222 map
= isl_map_add_basic_map(map
,
7223 isl_basic_map_copy(map2
->p
[i
]));
7237 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7238 * guaranteed to be disjoint by the caller.
7240 * Note that this functions is called from within isl_map_make_disjoint,
7241 * so we have to be careful not to touch the constraints of the inputs
7244 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7245 __isl_take isl_map
*map2
)
7247 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7250 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7251 * not be disjoint. The parameters are assumed to have been aligned.
7253 * We currently simply call map_union_disjoint, the internal operation
7254 * of which does not really depend on the inputs being disjoint.
7255 * If the result contains more than one basic map, then we clear
7256 * the disjoint flag since the result may contain basic maps from
7257 * both inputs and these are not guaranteed to be disjoint.
7259 * As a special case, if "map1" and "map2" are obviously equal,
7260 * then we simply return "map1".
7262 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7263 __isl_take isl_map
*map2
)
7270 equal
= isl_map_plain_is_equal(map1
, map2
);
7278 map1
= map_union_disjoint(map1
, map2
);
7282 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7290 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7293 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7294 __isl_take isl_map
*map2
)
7296 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7299 struct isl_set
*isl_set_union_disjoint(
7300 struct isl_set
*set1
, struct isl_set
*set2
)
7302 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7306 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7308 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7311 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7314 * "map" and "set" are assumed to be compatible and non-NULL.
7316 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7317 __isl_take isl_set
*set
,
7318 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7319 __isl_take isl_basic_set
*bset
))
7322 struct isl_map
*result
;
7325 if (isl_set_plain_is_universe(set
)) {
7330 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7331 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7332 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7334 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7335 map
->n
* set
->n
, flags
);
7336 for (i
= 0; result
&& i
< map
->n
; ++i
)
7337 for (j
= 0; j
< set
->n
; ++j
) {
7338 result
= isl_map_add_basic_map(result
,
7339 fn(isl_basic_map_copy(map
->p
[i
]),
7340 isl_basic_set_copy(set
->p
[j
])));
7350 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7351 __isl_take isl_set
*set
)
7356 if (!isl_map_compatible_range(map
, set
))
7357 isl_die(set
->ctx
, isl_error_invalid
,
7358 "incompatible spaces", goto error
);
7360 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7367 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7368 __isl_take isl_set
*set
)
7370 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7373 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7374 __isl_take isl_set
*set
)
7379 if (!isl_map_compatible_domain(map
, set
))
7380 isl_die(set
->ctx
, isl_error_invalid
,
7381 "incompatible spaces", goto error
);
7383 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7390 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7391 __isl_take isl_set
*set
)
7393 return isl_map_align_params_map_map_and(map
, set
,
7394 &map_intersect_domain
);
7397 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7398 __isl_take isl_map
*map2
)
7402 map1
= isl_map_reverse(map1
);
7403 map1
= isl_map_apply_range(map1
, map2
);
7404 return isl_map_reverse(map1
);
7411 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7412 __isl_take isl_map
*map2
)
7414 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7417 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7418 __isl_take isl_map
*map2
)
7420 isl_space
*dim_result
;
7421 struct isl_map
*result
;
7427 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7428 isl_space_copy(map2
->dim
));
7430 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7433 for (i
= 0; i
< map1
->n
; ++i
)
7434 for (j
= 0; j
< map2
->n
; ++j
) {
7435 result
= isl_map_add_basic_map(result
,
7436 isl_basic_map_apply_range(
7437 isl_basic_map_copy(map1
->p
[i
]),
7438 isl_basic_map_copy(map2
->p
[j
])));
7444 if (result
&& result
->n
<= 1)
7445 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7453 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7454 __isl_take isl_map
*map2
)
7456 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7460 * returns range - domain
7462 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7464 isl_space
*target_space
;
7465 struct isl_basic_set
*bset
;
7472 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7473 bmap
->dim
, isl_dim_out
),
7475 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7476 dim
= isl_basic_map_n_in(bmap
);
7477 nparam
= isl_basic_map_n_param(bmap
);
7478 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7479 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7480 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7481 for (i
= 0; i
< dim
; ++i
) {
7482 int j
= isl_basic_map_alloc_equality(bmap
);
7484 bmap
= isl_basic_map_free(bmap
);
7487 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7488 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7489 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7490 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7492 bset
= isl_basic_map_domain(bmap
);
7493 bset
= isl_basic_set_reset_space(bset
, target_space
);
7496 isl_basic_map_free(bmap
);
7501 * returns range - domain
7503 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7507 struct isl_set
*result
;
7512 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7513 map
->dim
, isl_dim_out
),
7515 dim
= isl_map_get_space(map
);
7516 dim
= isl_space_domain(dim
);
7517 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7520 for (i
= 0; i
< map
->n
; ++i
)
7521 result
= isl_set_add_basic_set(result
,
7522 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7531 * returns [domain -> range] -> range - domain
7533 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7534 __isl_take isl_basic_map
*bmap
)
7538 isl_basic_map
*domain
;
7542 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7543 bmap
->dim
, isl_dim_out
))
7544 isl_die(bmap
->ctx
, isl_error_invalid
,
7545 "domain and range don't match", goto error
);
7547 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7548 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7550 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7551 domain
= isl_basic_map_universe(dim
);
7553 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7554 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7555 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7557 total
= isl_basic_map_total_dim(bmap
);
7559 for (i
= 0; i
< n
; ++i
) {
7560 k
= isl_basic_map_alloc_equality(bmap
);
7563 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7564 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7565 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7566 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7569 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7570 return isl_basic_map_finalize(bmap
);
7572 isl_basic_map_free(bmap
);
7577 * returns [domain -> range] -> range - domain
7579 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7582 isl_space
*domain_dim
;
7587 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7588 map
->dim
, isl_dim_out
))
7589 isl_die(map
->ctx
, isl_error_invalid
,
7590 "domain and range don't match", goto error
);
7592 map
= isl_map_cow(map
);
7596 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7597 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7598 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7601 for (i
= 0; i
< map
->n
; ++i
) {
7602 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7606 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7613 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7615 struct isl_basic_map
*bmap
;
7623 nparam
= dims
->nparam
;
7625 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7629 for (i
= 0; i
< dim
; ++i
) {
7630 int j
= isl_basic_map_alloc_equality(bmap
);
7633 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7634 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7635 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7637 return isl_basic_map_finalize(bmap
);
7639 isl_basic_map_free(bmap
);
7643 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7647 if (dim
->n_in
!= dim
->n_out
)
7648 isl_die(dim
->ctx
, isl_error_invalid
,
7649 "number of input and output dimensions needs to be "
7650 "the same", goto error
);
7651 return basic_map_identity(dim
);
7653 isl_space_free(dim
);
7657 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7659 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7662 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7664 isl_space
*dim
= isl_set_get_space(set
);
7666 id
= isl_map_identity(isl_space_map_from_set(dim
));
7667 return isl_map_intersect_range(id
, set
);
7670 /* Construct a basic set with all set dimensions having only non-negative
7673 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7674 __isl_take isl_space
*space
)
7679 struct isl_basic_set
*bset
;
7683 nparam
= space
->nparam
;
7685 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7688 for (i
= 0; i
< dim
; ++i
) {
7689 int k
= isl_basic_set_alloc_inequality(bset
);
7692 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7693 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7697 isl_basic_set_free(bset
);
7701 /* Construct the half-space x_pos >= 0.
7703 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7707 isl_basic_set
*nonneg
;
7709 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7710 k
= isl_basic_set_alloc_inequality(nonneg
);
7713 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7714 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7716 return isl_basic_set_finalize(nonneg
);
7718 isl_basic_set_free(nonneg
);
7722 /* Construct the half-space x_pos <= -1.
7724 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7729 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7730 k
= isl_basic_set_alloc_inequality(neg
);
7733 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7734 isl_int_set_si(neg
->ineq
[k
][0], -1);
7735 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7737 return isl_basic_set_finalize(neg
);
7739 isl_basic_set_free(neg
);
7743 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7744 enum isl_dim_type type
, unsigned first
, unsigned n
)
7748 isl_basic_set
*nonneg
;
7756 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7758 offset
= pos(set
->dim
, type
);
7759 for (i
= 0; i
< n
; ++i
) {
7760 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7761 offset
+ first
+ i
);
7762 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7764 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7773 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7774 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7781 if (isl_set_plain_is_empty(set
)) {
7786 return fn(set
, signs
, user
);
7789 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7791 half
= isl_set_intersect(half
, isl_set_copy(set
));
7792 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7796 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7798 half
= isl_set_intersect(half
, set
);
7799 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7805 /* Call "fn" on the intersections of "set" with each of the orthants
7806 * (except for obviously empty intersections). The orthant is identified
7807 * by the signs array, with each entry having value 1 or -1 according
7808 * to the sign of the corresponding variable.
7810 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7811 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7821 if (isl_set_plain_is_empty(set
))
7824 nparam
= isl_set_dim(set
, isl_dim_param
);
7825 nvar
= isl_set_dim(set
, isl_dim_set
);
7827 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7829 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7837 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7839 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7842 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7843 __isl_keep isl_basic_map
*bmap2
)
7846 struct isl_map
*map1
;
7847 struct isl_map
*map2
;
7849 if (!bmap1
|| !bmap2
)
7850 return isl_bool_error
;
7852 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7853 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7855 is_subset
= isl_map_is_subset(map1
, map2
);
7863 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7864 __isl_keep isl_basic_set
*bset2
)
7866 return isl_basic_map_is_subset(bset1
, bset2
);
7869 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7870 __isl_keep isl_basic_map
*bmap2
)
7874 if (!bmap1
|| !bmap2
)
7875 return isl_bool_error
;
7876 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7877 if (is_subset
!= isl_bool_true
)
7879 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7883 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7884 __isl_keep isl_basic_set
*bset2
)
7886 return isl_basic_map_is_equal(
7887 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7890 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7896 return isl_bool_error
;
7897 for (i
= 0; i
< map
->n
; ++i
) {
7898 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7900 return isl_bool_error
;
7902 return isl_bool_false
;
7904 return isl_bool_true
;
7907 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7909 return map
? map
->n
== 0 : isl_bool_error
;
7912 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7914 return set
? set
->n
== 0 : isl_bool_error
;
7917 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7919 return isl_map_is_empty(set_to_map(set
));
7922 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7927 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7930 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7935 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7938 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7943 return isl_bool_error
;
7944 is_subset
= isl_map_is_subset(map1
, map2
);
7945 if (is_subset
!= isl_bool_true
)
7947 is_subset
= isl_map_is_subset(map2
, map1
);
7951 /* Is "map1" equal to "map2"?
7953 * First check if they are obviously equal.
7954 * If not, then perform a more detailed analysis.
7956 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7960 equal
= isl_map_plain_is_equal(map1
, map2
);
7961 if (equal
< 0 || equal
)
7963 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7966 isl_bool
isl_basic_map_is_strict_subset(
7967 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7971 if (!bmap1
|| !bmap2
)
7972 return isl_bool_error
;
7973 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7974 if (is_subset
!= isl_bool_true
)
7976 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7977 if (is_subset
== isl_bool_error
)
7982 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7983 __isl_keep isl_map
*map2
)
7988 return isl_bool_error
;
7989 is_subset
= isl_map_is_subset(map1
, map2
);
7990 if (is_subset
!= isl_bool_true
)
7992 is_subset
= isl_map_is_subset(map2
, map1
);
7993 if (is_subset
== isl_bool_error
)
7998 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7999 __isl_keep isl_set
*set2
)
8001 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8004 /* Is "bmap" obviously equal to the universe with the same space?
8006 * That is, does it not have any constraints?
8008 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8011 return isl_bool_error
;
8012 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8015 /* Is "bset" obviously equal to the universe with the same space?
8017 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8019 return isl_basic_map_plain_is_universe(bset
);
8022 /* If "c" does not involve any existentially quantified variables,
8023 * then set *univ to false and abort
8025 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8027 isl_bool
*univ
= user
;
8030 n
= isl_constraint_dim(c
, isl_dim_div
);
8031 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8032 isl_constraint_free(c
);
8033 if (*univ
< 0 || !*univ
)
8034 return isl_stat_error
;
8038 /* Is "bmap" equal to the universe with the same space?
8040 * First check if it is obviously equal to the universe.
8041 * If not and if there are any constraints not involving
8042 * existentially quantified variables, then it is certainly
8043 * not equal to the universe.
8044 * Otherwise, check if the universe is a subset of "bmap".
8046 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8049 isl_basic_map
*test
;
8051 univ
= isl_basic_map_plain_is_universe(bmap
);
8052 if (univ
< 0 || univ
)
8054 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8055 return isl_bool_false
;
8056 univ
= isl_bool_true
;
8057 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8059 return isl_bool_error
;
8060 if (univ
< 0 || !univ
)
8062 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8063 univ
= isl_basic_map_is_subset(test
, bmap
);
8064 isl_basic_map_free(test
);
8068 /* Is "bset" equal to the universe with the same space?
8070 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8072 return isl_basic_map_is_universe(bset
);
8075 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8080 return isl_bool_error
;
8082 for (i
= 0; i
< map
->n
; ++i
) {
8083 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8088 return isl_bool_false
;
8091 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8093 return isl_map_plain_is_universe(set_to_map(set
));
8096 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8098 struct isl_basic_set
*bset
= NULL
;
8099 struct isl_vec
*sample
= NULL
;
8100 isl_bool empty
, non_empty
;
8103 return isl_bool_error
;
8105 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8106 return isl_bool_true
;
8108 if (isl_basic_map_plain_is_universe(bmap
))
8109 return isl_bool_false
;
8111 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8112 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8113 copy
= isl_basic_map_remove_redundancies(copy
);
8114 empty
= isl_basic_map_plain_is_empty(copy
);
8115 isl_basic_map_free(copy
);
8119 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8121 return isl_bool_error
;
8123 return isl_bool_false
;
8124 isl_vec_free(bmap
->sample
);
8125 bmap
->sample
= NULL
;
8126 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8128 return isl_bool_error
;
8129 sample
= isl_basic_set_sample_vec(bset
);
8131 return isl_bool_error
;
8132 empty
= sample
->size
== 0;
8133 isl_vec_free(bmap
->sample
);
8134 bmap
->sample
= sample
;
8136 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8141 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8144 return isl_bool_error
;
8145 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8148 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8151 return isl_bool_error
;
8152 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8155 /* Is "bmap" known to be non-empty?
8157 * That is, is the cached sample still valid?
8159 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8164 return isl_bool_error
;
8166 return isl_bool_false
;
8167 total
= 1 + isl_basic_map_total_dim(bmap
);
8168 if (bmap
->sample
->size
!= total
)
8169 return isl_bool_false
;
8170 return isl_basic_map_contains(bmap
, bmap
->sample
);
8173 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8175 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8178 struct isl_map
*isl_basic_map_union(
8179 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8181 struct isl_map
*map
;
8182 if (!bmap1
|| !bmap2
)
8185 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8187 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8190 map
= isl_map_add_basic_map(map
, bmap1
);
8191 map
= isl_map_add_basic_map(map
, bmap2
);
8194 isl_basic_map_free(bmap1
);
8195 isl_basic_map_free(bmap2
);
8199 struct isl_set
*isl_basic_set_union(
8200 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8202 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8203 bset_to_bmap(bset2
)));
8206 /* Order divs such that any div only depends on previous divs */
8207 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8215 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8217 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8219 if (isl_int_is_zero(bmap
->div
[i
][0]))
8221 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8226 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8227 "integer division depends on itself",
8228 return isl_basic_map_free(bmap
));
8229 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8235 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8237 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8240 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8247 for (i
= 0; i
< map
->n
; ++i
) {
8248 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8259 /* Sort the local variables of "bset".
8261 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8262 __isl_take isl_basic_set
*bset
)
8264 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8267 /* Apply the expansion computed by isl_merge_divs.
8268 * The expansion itself is given by "exp" while the resulting
8269 * list of divs is given by "div".
8271 * Move the integer divisions of "bmap" into the right position
8272 * according to "exp" and then introduce the additional integer
8273 * divisions, adding div constraints.
8274 * The moving should be done first to avoid moving coefficients
8275 * in the definitions of the extra integer divisions.
8277 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8278 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8283 bmap
= isl_basic_map_cow(bmap
);
8287 if (div
->n_row
< bmap
->n_div
)
8288 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8289 "not an expansion", goto error
);
8291 n_div
= bmap
->n_div
;
8292 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8293 div
->n_row
- n_div
, 0,
8294 2 * (div
->n_row
- n_div
));
8296 for (i
= n_div
; i
< div
->n_row
; ++i
)
8297 if (isl_basic_map_alloc_div(bmap
) < 0)
8300 for (j
= n_div
- 1; j
>= 0; --j
) {
8303 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8306 for (i
= 0; i
< div
->n_row
; ++i
) {
8307 if (j
< n_div
&& exp
[j
] == i
) {
8310 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8311 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8313 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8321 isl_basic_map_free(bmap
);
8326 /* Apply the expansion computed by isl_merge_divs.
8327 * The expansion itself is given by "exp" while the resulting
8328 * list of divs is given by "div".
8330 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8331 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8333 return isl_basic_map_expand_divs(bset
, div
, exp
);
8336 /* Look for a div in dst that corresponds to the div "div" in src.
8337 * The divs before "div" in src and dst are assumed to be the same.
8339 * Returns -1 if no corresponding div was found and the position
8340 * of the corresponding div in dst otherwise.
8342 static int find_div(struct isl_basic_map
*dst
,
8343 struct isl_basic_map
*src
, unsigned div
)
8347 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8349 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8350 for (i
= div
; i
< dst
->n_div
; ++i
)
8351 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8352 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8353 dst
->n_div
- div
) == -1)
8358 /* Align the divs of "dst" to those of "src", adding divs from "src"
8359 * if needed. That is, make sure that the first src->n_div divs
8360 * of the result are equal to those of src.
8362 * The result is not finalized as by design it will have redundant
8363 * divs if any divs from "src" were copied.
8365 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8366 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8369 int known
, extended
;
8373 return isl_basic_map_free(dst
);
8375 if (src
->n_div
== 0)
8378 known
= isl_basic_map_divs_known(src
);
8380 return isl_basic_map_free(dst
);
8382 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8383 "some src divs are unknown",
8384 return isl_basic_map_free(dst
));
8386 src
= isl_basic_map_order_divs(src
);
8389 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8390 for (i
= 0; i
< src
->n_div
; ++i
) {
8391 int j
= find_div(dst
, src
, i
);
8394 int extra
= src
->n_div
- i
;
8395 dst
= isl_basic_map_cow(dst
);
8398 dst
= isl_basic_map_extend_space(dst
,
8399 isl_space_copy(dst
->dim
),
8400 extra
, 0, 2 * extra
);
8403 j
= isl_basic_map_alloc_div(dst
);
8405 return isl_basic_map_free(dst
);
8406 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8407 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8408 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8409 return isl_basic_map_free(dst
);
8412 isl_basic_map_swap_div(dst
, i
, j
);
8417 struct isl_basic_set
*isl_basic_set_align_divs(
8418 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8420 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8421 bset_to_bmap(src
)));
8424 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8432 map
= isl_map_compute_divs(map
);
8433 map
= isl_map_cow(map
);
8437 for (i
= 1; i
< map
->n
; ++i
)
8438 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8439 for (i
= 1; i
< map
->n
; ++i
) {
8440 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8442 return isl_map_free(map
);
8445 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8449 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8451 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8454 /* Align the divs of the basic maps in "map" to those
8455 * of the basic maps in "list", as well as to the other basic maps in "map".
8456 * The elements in "list" are assumed to have known divs.
8458 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8459 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8463 map
= isl_map_compute_divs(map
);
8464 map
= isl_map_cow(map
);
8466 return isl_map_free(map
);
8470 n
= isl_basic_map_list_n_basic_map(list
);
8471 for (i
= 0; i
< n
; ++i
) {
8472 isl_basic_map
*bmap
;
8474 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8475 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8476 isl_basic_map_free(bmap
);
8479 return isl_map_free(map
);
8481 return isl_map_align_divs(map
);
8484 /* Align the divs of each element of "list" to those of "bmap".
8485 * Both "bmap" and the elements of "list" are assumed to have known divs.
8487 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8488 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8493 return isl_basic_map_list_free(list
);
8495 n
= isl_basic_map_list_n_basic_map(list
);
8496 for (i
= 0; i
< n
; ++i
) {
8497 isl_basic_map
*bmap_i
;
8499 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8500 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8501 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8507 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8508 __isl_take isl_map
*map
)
8512 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8513 map
= isl_map_intersect_domain(map
, set
);
8514 set
= isl_map_range(map
);
8522 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8523 __isl_take isl_map
*map
)
8525 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8528 /* There is no need to cow as removing empty parts doesn't change
8529 * the meaning of the set.
8531 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8538 for (i
= map
->n
- 1; i
>= 0; --i
)
8539 remove_if_empty(map
, i
);
8544 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8546 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8549 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8551 struct isl_basic_map
*bmap
;
8552 if (!map
|| map
->n
== 0)
8554 bmap
= map
->p
[map
->n
-1];
8555 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8556 return isl_basic_map_copy(bmap
);
8559 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8561 return map_copy_basic_map(map
);
8564 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8566 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8569 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8570 __isl_keep isl_basic_map
*bmap
)
8576 for (i
= map
->n
-1; i
>= 0; --i
) {
8577 if (map
->p
[i
] != bmap
)
8579 map
= isl_map_cow(map
);
8582 isl_basic_map_free(map
->p
[i
]);
8583 if (i
!= map
->n
-1) {
8584 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8585 map
->p
[i
] = map
->p
[map
->n
-1];
8596 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8597 __isl_keep isl_basic_map
*bmap
)
8599 return map_drop_basic_map(map
, bmap
);
8602 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8603 struct isl_basic_set
*bset
)
8605 return set_from_map(map_drop_basic_map(set_to_map(set
),
8606 bset_to_bmap(bset
)));
8609 /* Given two basic sets bset1 and bset2, compute the maximal difference
8610 * between the values of dimension pos in bset1 and those in bset2
8611 * for any common value of the parameters and dimensions preceding pos.
8613 static enum isl_lp_result
basic_set_maximal_difference_at(
8614 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8615 int pos
, isl_int
*opt
)
8617 isl_basic_map
*bmap1
;
8618 isl_basic_map
*bmap2
;
8619 struct isl_ctx
*ctx
;
8620 struct isl_vec
*obj
;
8624 enum isl_lp_result res
;
8626 if (!bset1
|| !bset2
)
8627 return isl_lp_error
;
8629 nparam
= isl_basic_set_n_param(bset1
);
8630 dim1
= isl_basic_set_n_dim(bset1
);
8632 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8633 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8634 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8635 isl_dim_out
, 0, pos
);
8636 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8637 isl_dim_out
, 0, pos
);
8638 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8640 return isl_lp_error
;
8642 total
= isl_basic_map_total_dim(bmap1
);
8644 obj
= isl_vec_alloc(ctx
, 1 + total
);
8647 isl_seq_clr(obj
->block
.data
, 1 + total
);
8648 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8649 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8650 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8652 isl_basic_map_free(bmap1
);
8656 isl_basic_map_free(bmap1
);
8657 return isl_lp_error
;
8660 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8661 * for any common value of the parameters and dimensions preceding pos
8662 * in both basic sets, the values of dimension pos in bset1 are
8663 * smaller or larger than those in bset2.
8666 * 1 if bset1 follows bset2
8667 * -1 if bset1 precedes bset2
8668 * 0 if bset1 and bset2 are incomparable
8669 * -2 if some error occurred.
8671 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8672 struct isl_basic_set
*bset2
, int pos
)
8675 enum isl_lp_result res
;
8680 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8682 if (res
== isl_lp_empty
)
8684 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8685 res
== isl_lp_unbounded
)
8687 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8696 /* Given two basic sets bset1 and bset2, check whether
8697 * for any common value of the parameters and dimensions preceding pos
8698 * there is a value of dimension pos in bset1 that is larger
8699 * than a value of the same dimension in bset2.
8702 * 1 if there exists such a pair
8703 * 0 if there is no such pair, but there is a pair of equal values
8705 * -2 if some error occurred.
8707 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8708 __isl_keep isl_basic_set
*bset2
, int pos
)
8711 enum isl_lp_result res
;
8716 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8718 if (res
== isl_lp_empty
)
8720 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8721 res
== isl_lp_unbounded
)
8723 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8725 else if (res
== isl_lp_ok
)
8734 /* Given two sets set1 and set2, check whether
8735 * for any common value of the parameters and dimensions preceding pos
8736 * there is a value of dimension pos in set1 that is larger
8737 * than a value of the same dimension in set2.
8740 * 1 if there exists such a pair
8741 * 0 if there is no such pair, but there is a pair of equal values
8743 * -2 if some error occurred.
8745 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8746 __isl_keep isl_set
*set2
, int pos
)
8754 for (i
= 0; i
< set1
->n
; ++i
)
8755 for (j
= 0; j
< set2
->n
; ++j
) {
8757 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8758 if (f
== 1 || f
== -2)
8767 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8768 unsigned pos
, isl_int
*val
)
8776 total
= isl_basic_map_total_dim(bmap
);
8777 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8778 for (; d
+1 > pos
; --d
)
8779 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8783 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8785 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8787 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8790 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8796 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8797 unsigned pos
, isl_int
*val
)
8809 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8812 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8813 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8814 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8815 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8819 isl_int_set(*val
, v
);
8825 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8826 unsigned pos
, isl_int
*val
)
8828 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8832 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8835 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
8838 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8839 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8841 if (pos
>= isl_basic_map_dim(bmap
, type
))
8843 return isl_basic_map_plain_has_fixed_var(bmap
,
8844 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8847 /* If "bmap" obviously lies on a hyperplane where the given dimension
8848 * has a fixed value, then return that value.
8849 * Otherwise return NaN.
8851 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8852 __isl_keep isl_basic_map
*bmap
,
8853 enum isl_dim_type type
, unsigned pos
)
8861 ctx
= isl_basic_map_get_ctx(bmap
);
8862 v
= isl_val_alloc(ctx
);
8865 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8867 return isl_val_free(v
);
8869 isl_int_set_si(v
->d
, 1);
8873 return isl_val_nan(ctx
);
8876 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8877 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8879 if (pos
>= isl_map_dim(map
, type
))
8881 return isl_map_plain_has_fixed_var(map
,
8882 map_offset(map
, type
) - 1 + pos
, val
);
8885 /* If "map" obviously lies on a hyperplane where the given dimension
8886 * has a fixed value, then return that value.
8887 * Otherwise return NaN.
8889 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8890 enum isl_dim_type type
, unsigned pos
)
8898 ctx
= isl_map_get_ctx(map
);
8899 v
= isl_val_alloc(ctx
);
8902 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8904 return isl_val_free(v
);
8906 isl_int_set_si(v
->d
, 1);
8910 return isl_val_nan(ctx
);
8913 /* If "set" obviously lies on a hyperplane where the given dimension
8914 * has a fixed value, then return that value.
8915 * Otherwise return NaN.
8917 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8918 enum isl_dim_type type
, unsigned pos
)
8920 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8923 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8924 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8926 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8929 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8930 * then return this fixed value in *val.
8932 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8933 unsigned dim
, isl_int
*val
)
8935 return isl_basic_set_plain_has_fixed_var(bset
,
8936 isl_basic_set_n_param(bset
) + dim
, val
);
8939 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8940 * then return this fixed value in *val.
8942 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8943 unsigned dim
, isl_int
*val
)
8945 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8948 /* Check if input variable in has fixed value and if so and if val is not NULL,
8949 * then return this fixed value in *val.
8951 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8952 unsigned in
, isl_int
*val
)
8954 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8957 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8958 * and if val is not NULL, then return this lower bound in *val.
8960 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8961 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8963 int i
, i_eq
= -1, i_ineq
= -1;
8970 total
= isl_basic_set_total_dim(bset
);
8971 nparam
= isl_basic_set_n_param(bset
);
8972 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8973 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8979 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8980 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8982 if (i_eq
!= -1 || i_ineq
!= -1)
8986 if (i_eq
== -1 && i_ineq
== -1)
8988 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8989 /* The coefficient should always be one due to normalization. */
8990 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8992 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8994 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8995 total
- nparam
- dim
- 1) != -1)
8998 isl_int_neg(*val
, c
[0]);
9002 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9003 unsigned dim
, isl_int
*val
)
9015 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9019 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9021 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9022 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9024 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9028 isl_int_set(*val
, v
);
9034 /* Return -1 if the constraint "c1" should be sorted before "c2"
9035 * and 1 if it should be sorted after "c2".
9036 * Return 0 if the two constraints are the same (up to the constant term).
9038 * In particular, if a constraint involves later variables than another
9039 * then it is sorted after this other constraint.
9040 * uset_gist depends on constraints without existentially quantified
9041 * variables sorting first.
9043 * For constraints that have the same latest variable, those
9044 * with the same coefficient for this latest variable (first in absolute value
9045 * and then in actual value) are grouped together.
9046 * This is useful for detecting pairs of constraints that can
9047 * be chained in their printed representation.
9049 * Finally, within a group, constraints are sorted according to
9050 * their coefficients (excluding the constant term).
9052 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9054 isl_int
**c1
= (isl_int
**) p1
;
9055 isl_int
**c2
= (isl_int
**) p2
;
9057 unsigned size
= *(unsigned *) arg
;
9060 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9061 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9066 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9069 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9073 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9076 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9077 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9078 * and 0 if the two constraints are the same (up to the constant term).
9080 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9081 isl_int
*c1
, isl_int
*c2
)
9087 total
= isl_basic_map_total_dim(bmap
);
9088 return sort_constraint_cmp(&c1
, &c2
, &total
);
9091 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9092 __isl_take isl_basic_map
*bmap
)
9098 if (bmap
->n_ineq
== 0)
9100 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9102 total
= isl_basic_map_total_dim(bmap
);
9103 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9104 &sort_constraint_cmp
, &total
) < 0)
9105 return isl_basic_map_free(bmap
);
9109 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9110 __isl_take isl_basic_set
*bset
)
9112 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9113 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9116 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9120 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9122 bmap
= isl_basic_map_remove_redundancies(bmap
);
9123 bmap
= isl_basic_map_sort_constraints(bmap
);
9125 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9129 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9131 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9134 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9135 const __isl_keep isl_basic_map
*bmap2
)
9140 if (!bmap1
|| !bmap2
)
9145 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9146 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9147 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9148 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9149 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9150 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9151 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9152 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9153 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9154 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9155 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9157 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9159 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9161 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9162 return bmap1
->n_eq
- bmap2
->n_eq
;
9163 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9164 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9165 if (bmap1
->n_div
!= bmap2
->n_div
)
9166 return bmap1
->n_div
- bmap2
->n_div
;
9167 total
= isl_basic_map_total_dim(bmap1
);
9168 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9169 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9173 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9174 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9178 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9179 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9186 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9187 const __isl_keep isl_basic_set
*bset2
)
9189 return isl_basic_map_plain_cmp(bset1
, bset2
);
9192 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9198 if (set1
->n
!= set2
->n
)
9199 return set1
->n
- set2
->n
;
9201 for (i
= 0; i
< set1
->n
; ++i
) {
9202 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9210 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9211 __isl_keep isl_basic_map
*bmap2
)
9213 if (!bmap1
|| !bmap2
)
9214 return isl_bool_error
;
9215 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9218 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9219 __isl_keep isl_basic_set
*bset2
)
9221 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9222 bset_to_bmap(bset2
));
9225 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9227 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9228 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9230 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9233 /* Sort the basic maps of "map" and remove duplicate basic maps.
9235 * While removing basic maps, we make sure that the basic maps remain
9236 * sorted because isl_map_normalize expects the basic maps of the result
9239 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9243 map
= isl_map_remove_empty_parts(map
);
9246 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9247 for (i
= map
->n
- 1; i
>= 1; --i
) {
9248 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9250 isl_basic_map_free(map
->p
[i
-1]);
9251 for (j
= i
; j
< map
->n
; ++j
)
9252 map
->p
[j
- 1] = map
->p
[j
];
9259 /* Remove obvious duplicates among the basic maps of "map".
9261 * Unlike isl_map_normalize, this function does not remove redundant
9262 * constraints and only removes duplicates that have exactly the same
9263 * constraints in the input. It does sort the constraints and
9264 * the basic maps to ease the detection of duplicates.
9266 * If "map" has already been normalized or if the basic maps are
9267 * disjoint, then there can be no duplicates.
9269 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9272 isl_basic_map
*bmap
;
9278 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9280 for (i
= 0; i
< map
->n
; ++i
) {
9281 bmap
= isl_basic_map_copy(map
->p
[i
]);
9282 bmap
= isl_basic_map_sort_constraints(bmap
);
9284 return isl_map_free(map
);
9285 isl_basic_map_free(map
->p
[i
]);
9289 map
= sort_and_remove_duplicates(map
);
9293 /* We normalize in place, but if anything goes wrong we need
9294 * to return NULL, so we need to make sure we don't change the
9295 * meaning of any possible other copies of map.
9297 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9300 struct isl_basic_map
*bmap
;
9304 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9306 for (i
= 0; i
< map
->n
; ++i
) {
9307 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9310 isl_basic_map_free(map
->p
[i
]);
9314 map
= sort_and_remove_duplicates(map
);
9316 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9323 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9325 return set_from_map(isl_map_normalize(set_to_map(set
)));
9328 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9329 __isl_keep isl_map
*map2
)
9335 return isl_bool_error
;
9338 return isl_bool_true
;
9339 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9340 return isl_bool_false
;
9342 map1
= isl_map_copy(map1
);
9343 map2
= isl_map_copy(map2
);
9344 map1
= isl_map_normalize(map1
);
9345 map2
= isl_map_normalize(map2
);
9348 equal
= map1
->n
== map2
->n
;
9349 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9350 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9360 return isl_bool_error
;
9363 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9364 __isl_keep isl_set
*set2
)
9366 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9369 /* Return an interval that ranges from min to max (inclusive)
9371 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9372 isl_int min
, isl_int max
)
9375 struct isl_basic_set
*bset
= NULL
;
9377 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9381 k
= isl_basic_set_alloc_inequality(bset
);
9384 isl_int_set_si(bset
->ineq
[k
][1], 1);
9385 isl_int_neg(bset
->ineq
[k
][0], min
);
9387 k
= isl_basic_set_alloc_inequality(bset
);
9390 isl_int_set_si(bset
->ineq
[k
][1], -1);
9391 isl_int_set(bset
->ineq
[k
][0], max
);
9395 isl_basic_set_free(bset
);
9399 /* Return the basic maps in "map" as a list.
9401 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9402 __isl_keep isl_map
*map
)
9406 isl_basic_map_list
*list
;
9410 ctx
= isl_map_get_ctx(map
);
9411 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9413 for (i
= 0; i
< map
->n
; ++i
) {
9414 isl_basic_map
*bmap
;
9416 bmap
= isl_basic_map_copy(map
->p
[i
]);
9417 list
= isl_basic_map_list_add(list
, bmap
);
9423 /* Return the intersection of the elements in the non-empty list "list".
9424 * All elements are assumed to live in the same space.
9426 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9427 __isl_take isl_basic_map_list
*list
)
9430 isl_basic_map
*bmap
;
9434 n
= isl_basic_map_list_n_basic_map(list
);
9436 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9437 "expecting non-empty list", goto error
);
9439 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9440 for (i
= 1; i
< n
; ++i
) {
9441 isl_basic_map
*bmap_i
;
9443 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9444 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9447 isl_basic_map_list_free(list
);
9450 isl_basic_map_list_free(list
);
9454 /* Return the intersection of the elements in the non-empty list "list".
9455 * All elements are assumed to live in the same space.
9457 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9458 __isl_take isl_basic_set_list
*list
)
9460 return isl_basic_map_list_intersect(list
);
9463 /* Return the union of the elements of "list".
9464 * The list is required to have at least one element.
9466 __isl_give isl_set
*isl_basic_set_list_union(
9467 __isl_take isl_basic_set_list
*list
)
9471 isl_basic_set
*bset
;
9476 n
= isl_basic_set_list_n_basic_set(list
);
9478 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9479 "expecting non-empty list", goto error
);
9481 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9482 space
= isl_basic_set_get_space(bset
);
9483 isl_basic_set_free(bset
);
9485 set
= isl_set_alloc_space(space
, n
, 0);
9486 for (i
= 0; i
< n
; ++i
) {
9487 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9488 set
= isl_set_add_basic_set(set
, bset
);
9491 isl_basic_set_list_free(list
);
9494 isl_basic_set_list_free(list
);
9498 /* Return the union of the elements in the non-empty list "list".
9499 * All elements are assumed to live in the same space.
9501 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9508 n
= isl_set_list_n_set(list
);
9510 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9511 "expecting non-empty list", goto error
);
9513 set
= isl_set_list_get_set(list
, 0);
9514 for (i
= 1; i
< n
; ++i
) {
9517 set_i
= isl_set_list_get_set(list
, i
);
9518 set
= isl_set_union(set
, set_i
);
9521 isl_set_list_free(list
);
9524 isl_set_list_free(list
);
9528 /* Return the Cartesian product of the basic sets in list (in the given order).
9530 __isl_give isl_basic_set
*isl_basic_set_list_product(
9531 __isl_take
struct isl_basic_set_list
*list
)
9539 struct isl_basic_set
*product
= NULL
;
9543 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9544 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9545 nparam
= isl_basic_set_n_param(list
->p
[0]);
9546 dim
= isl_basic_set_n_dim(list
->p
[0]);
9547 extra
= list
->p
[0]->n_div
;
9548 n_eq
= list
->p
[0]->n_eq
;
9549 n_ineq
= list
->p
[0]->n_ineq
;
9550 for (i
= 1; i
< list
->n
; ++i
) {
9551 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9552 isl_assert(list
->ctx
,
9553 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9554 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9555 extra
+= list
->p
[i
]->n_div
;
9556 n_eq
+= list
->p
[i
]->n_eq
;
9557 n_ineq
+= list
->p
[i
]->n_ineq
;
9559 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9564 for (i
= 0; i
< list
->n
; ++i
) {
9565 isl_basic_set_add_constraints(product
,
9566 isl_basic_set_copy(list
->p
[i
]), dim
);
9567 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9569 isl_basic_set_list_free(list
);
9572 isl_basic_set_free(product
);
9573 isl_basic_set_list_free(list
);
9577 struct isl_basic_map
*isl_basic_map_product(
9578 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9580 isl_space
*dim_result
= NULL
;
9581 struct isl_basic_map
*bmap
;
9582 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9583 struct isl_dim_map
*dim_map1
, *dim_map2
;
9585 if (!bmap1
|| !bmap2
)
9588 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9589 bmap2
->dim
, isl_dim_param
), goto error
);
9590 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9591 isl_space_copy(bmap2
->dim
));
9593 in1
= isl_basic_map_n_in(bmap1
);
9594 in2
= isl_basic_map_n_in(bmap2
);
9595 out1
= isl_basic_map_n_out(bmap1
);
9596 out2
= isl_basic_map_n_out(bmap2
);
9597 nparam
= isl_basic_map_n_param(bmap1
);
9599 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9600 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9601 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9602 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9603 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9604 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9605 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9606 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9607 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9608 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9609 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9611 bmap
= isl_basic_map_alloc_space(dim_result
,
9612 bmap1
->n_div
+ bmap2
->n_div
,
9613 bmap1
->n_eq
+ bmap2
->n_eq
,
9614 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9615 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9616 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9617 bmap
= isl_basic_map_simplify(bmap
);
9618 return isl_basic_map_finalize(bmap
);
9620 isl_basic_map_free(bmap1
);
9621 isl_basic_map_free(bmap2
);
9625 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9626 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9628 isl_basic_map
*prod
;
9630 prod
= isl_basic_map_product(bmap1
, bmap2
);
9631 prod
= isl_basic_map_flatten(prod
);
9635 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9636 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9638 return isl_basic_map_flat_range_product(bset1
, bset2
);
9641 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9642 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9644 isl_space
*space_result
= NULL
;
9645 isl_basic_map
*bmap
;
9646 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9647 struct isl_dim_map
*dim_map1
, *dim_map2
;
9649 if (!bmap1
|| !bmap2
)
9652 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9653 isl_space_copy(bmap2
->dim
));
9655 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9656 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9657 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9658 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9660 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9661 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9662 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9663 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9664 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9665 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9666 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9667 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9668 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9669 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9670 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9672 bmap
= isl_basic_map_alloc_space(space_result
,
9673 bmap1
->n_div
+ bmap2
->n_div
,
9674 bmap1
->n_eq
+ bmap2
->n_eq
,
9675 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9676 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9677 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9678 bmap
= isl_basic_map_simplify(bmap
);
9679 return isl_basic_map_finalize(bmap
);
9681 isl_basic_map_free(bmap1
);
9682 isl_basic_map_free(bmap2
);
9686 __isl_give isl_basic_map
*isl_basic_map_range_product(
9687 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9690 isl_space
*dim_result
= NULL
;
9691 isl_basic_map
*bmap
;
9692 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9693 struct isl_dim_map
*dim_map1
, *dim_map2
;
9695 rational
= isl_basic_map_is_rational(bmap1
);
9696 if (rational
>= 0 && rational
)
9697 rational
= isl_basic_map_is_rational(bmap2
);
9698 if (!bmap1
|| !bmap2
|| rational
< 0)
9701 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9702 bmap2
->dim
, isl_dim_param
))
9703 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9704 "parameters don't match", goto error
);
9706 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9707 isl_space_copy(bmap2
->dim
));
9709 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9710 out1
= isl_basic_map_n_out(bmap1
);
9711 out2
= isl_basic_map_n_out(bmap2
);
9712 nparam
= isl_basic_map_n_param(bmap1
);
9714 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9715 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9716 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9717 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9718 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9719 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9720 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9721 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9722 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9723 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9724 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9726 bmap
= isl_basic_map_alloc_space(dim_result
,
9727 bmap1
->n_div
+ bmap2
->n_div
,
9728 bmap1
->n_eq
+ bmap2
->n_eq
,
9729 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9730 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9731 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9733 bmap
= isl_basic_map_set_rational(bmap
);
9734 bmap
= isl_basic_map_simplify(bmap
);
9735 return isl_basic_map_finalize(bmap
);
9737 isl_basic_map_free(bmap1
);
9738 isl_basic_map_free(bmap2
);
9742 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9743 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9745 isl_basic_map
*prod
;
9747 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9748 prod
= isl_basic_map_flatten_range(prod
);
9752 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9753 * and collect the results.
9754 * The result live in the space obtained by calling "space_product"
9755 * on the spaces of "map1" and "map2".
9756 * If "remove_duplicates" is set then the result may contain duplicates
9757 * (even if the inputs do not) and so we try and remove the obvious
9760 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9761 __isl_take isl_map
*map2
,
9762 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9763 __isl_take isl_space
*right
),
9764 __isl_give isl_basic_map
*(*basic_map_product
)(
9765 __isl_take isl_basic_map
*left
,
9766 __isl_take isl_basic_map
*right
),
9767 int remove_duplicates
)
9770 struct isl_map
*result
;
9776 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9777 map2
->dim
, isl_dim_param
), goto error
);
9779 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9780 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9781 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9783 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9784 isl_space_copy(map2
->dim
)),
9785 map1
->n
* map2
->n
, flags
);
9788 for (i
= 0; i
< map1
->n
; ++i
)
9789 for (j
= 0; j
< map2
->n
; ++j
) {
9790 struct isl_basic_map
*part
;
9791 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9792 isl_basic_map_copy(map2
->p
[j
]));
9793 if (isl_basic_map_is_empty(part
))
9794 isl_basic_map_free(part
);
9796 result
= isl_map_add_basic_map(result
, part
);
9800 if (remove_duplicates
)
9801 result
= isl_map_remove_obvious_duplicates(result
);
9811 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9813 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9814 __isl_take isl_map
*map2
)
9816 return map_product(map1
, map2
, &isl_space_product
,
9817 &isl_basic_map_product
, 0);
9820 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9821 __isl_take isl_map
*map2
)
9823 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9826 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9828 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9829 __isl_take isl_map
*map2
)
9833 prod
= isl_map_product(map1
, map2
);
9834 prod
= isl_map_flatten(prod
);
9838 /* Given two set A and B, construct its Cartesian product A x B.
9840 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9842 return isl_map_range_product(set1
, set2
);
9845 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9846 __isl_take isl_set
*set2
)
9848 return isl_map_flat_range_product(set1
, set2
);
9851 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9853 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9854 __isl_take isl_map
*map2
)
9856 return map_product(map1
, map2
, &isl_space_domain_product
,
9857 &isl_basic_map_domain_product
, 1);
9860 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9862 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9863 __isl_take isl_map
*map2
)
9865 return map_product(map1
, map2
, &isl_space_range_product
,
9866 &isl_basic_map_range_product
, 1);
9869 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9870 __isl_take isl_map
*map2
)
9872 return isl_map_align_params_map_map_and(map1
, map2
,
9873 &map_domain_product_aligned
);
9876 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9877 __isl_take isl_map
*map2
)
9879 return isl_map_align_params_map_map_and(map1
, map2
,
9880 &map_range_product_aligned
);
9883 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9885 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9888 int total1
, keep1
, total2
, keep2
;
9892 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9893 !isl_space_range_is_wrapping(map
->dim
))
9894 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9895 "not a product", return isl_map_free(map
));
9897 space
= isl_map_get_space(map
);
9898 total1
= isl_space_dim(space
, isl_dim_in
);
9899 total2
= isl_space_dim(space
, isl_dim_out
);
9900 space
= isl_space_factor_domain(space
);
9901 keep1
= isl_space_dim(space
, isl_dim_in
);
9902 keep2
= isl_space_dim(space
, isl_dim_out
);
9903 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9904 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9905 map
= isl_map_reset_space(map
, space
);
9910 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9912 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9915 int total1
, keep1
, total2
, keep2
;
9919 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9920 !isl_space_range_is_wrapping(map
->dim
))
9921 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9922 "not a product", return isl_map_free(map
));
9924 space
= isl_map_get_space(map
);
9925 total1
= isl_space_dim(space
, isl_dim_in
);
9926 total2
= isl_space_dim(space
, isl_dim_out
);
9927 space
= isl_space_factor_range(space
);
9928 keep1
= isl_space_dim(space
, isl_dim_in
);
9929 keep2
= isl_space_dim(space
, isl_dim_out
);
9930 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9931 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9932 map
= isl_map_reset_space(map
, space
);
9937 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9939 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9946 if (!isl_space_domain_is_wrapping(map
->dim
))
9947 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9948 "domain is not a product", return isl_map_free(map
));
9950 space
= isl_map_get_space(map
);
9951 total
= isl_space_dim(space
, isl_dim_in
);
9952 space
= isl_space_domain_factor_domain(space
);
9953 keep
= isl_space_dim(space
, isl_dim_in
);
9954 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9955 map
= isl_map_reset_space(map
, space
);
9960 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9962 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9969 if (!isl_space_domain_is_wrapping(map
->dim
))
9970 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9971 "domain is not a product", return isl_map_free(map
));
9973 space
= isl_map_get_space(map
);
9974 total
= isl_space_dim(space
, isl_dim_in
);
9975 space
= isl_space_domain_factor_range(space
);
9976 keep
= isl_space_dim(space
, isl_dim_in
);
9977 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9978 map
= isl_map_reset_space(map
, space
);
9983 /* Given a map A -> [B -> C], extract the map A -> B.
9985 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9992 if (!isl_space_range_is_wrapping(map
->dim
))
9993 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9994 "range is not a product", return isl_map_free(map
));
9996 space
= isl_map_get_space(map
);
9997 total
= isl_space_dim(space
, isl_dim_out
);
9998 space
= isl_space_range_factor_domain(space
);
9999 keep
= isl_space_dim(space
, isl_dim_out
);
10000 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10001 map
= isl_map_reset_space(map
, space
);
10006 /* Given a map A -> [B -> C], extract the map A -> C.
10008 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10015 if (!isl_space_range_is_wrapping(map
->dim
))
10016 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10017 "range is not a product", return isl_map_free(map
));
10019 space
= isl_map_get_space(map
);
10020 total
= isl_space_dim(space
, isl_dim_out
);
10021 space
= isl_space_range_factor_range(space
);
10022 keep
= isl_space_dim(space
, isl_dim_out
);
10023 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10024 map
= isl_map_reset_space(map
, space
);
10029 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10031 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10032 __isl_take isl_map
*map2
)
10036 prod
= isl_map_domain_product(map1
, map2
);
10037 prod
= isl_map_flatten_domain(prod
);
10041 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10043 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10044 __isl_take isl_map
*map2
)
10048 prod
= isl_map_range_product(map1
, map2
);
10049 prod
= isl_map_flatten_range(prod
);
10053 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10056 uint32_t hash
= isl_hash_init();
10061 bmap
= isl_basic_map_copy(bmap
);
10062 bmap
= isl_basic_map_normalize(bmap
);
10065 total
= isl_basic_map_total_dim(bmap
);
10066 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10067 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10069 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10070 isl_hash_hash(hash
, c_hash
);
10072 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10073 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10075 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10076 isl_hash_hash(hash
, c_hash
);
10078 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10079 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10081 if (isl_int_is_zero(bmap
->div
[i
][0]))
10083 isl_hash_byte(hash
, i
& 0xFF);
10084 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10085 isl_hash_hash(hash
, c_hash
);
10087 isl_basic_map_free(bmap
);
10091 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10093 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10096 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10103 map
= isl_map_copy(map
);
10104 map
= isl_map_normalize(map
);
10108 hash
= isl_hash_init();
10109 for (i
= 0; i
< map
->n
; ++i
) {
10110 uint32_t bmap_hash
;
10111 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10112 isl_hash_hash(hash
, bmap_hash
);
10120 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10122 return isl_map_get_hash(set_to_map(set
));
10125 /* Check if the value for dimension dim is completely determined
10126 * by the values of the other parameters and variables.
10127 * That is, check if dimension dim is involved in an equality.
10129 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10136 nparam
= isl_basic_set_n_param(bset
);
10137 for (i
= 0; i
< bset
->n_eq
; ++i
)
10138 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10143 /* Check if the value for dimension dim is completely determined
10144 * by the values of the other parameters and variables.
10145 * That is, check if dimension dim is involved in an equality
10146 * for each of the subsets.
10148 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10154 for (i
= 0; i
< set
->n
; ++i
) {
10156 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10163 /* Return the number of basic maps in the (current) representation of "map".
10165 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10167 return map
? map
->n
: 0;
10170 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10172 return set
? set
->n
: 0;
10175 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10176 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10181 return isl_stat_error
;
10183 for (i
= 0; i
< map
->n
; ++i
)
10184 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10185 return isl_stat_error
;
10187 return isl_stat_ok
;
10190 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10191 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10196 return isl_stat_error
;
10198 for (i
= 0; i
< set
->n
; ++i
)
10199 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10200 return isl_stat_error
;
10202 return isl_stat_ok
;
10205 /* Return a list of basic sets, the union of which is equal to "set".
10207 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10208 __isl_keep isl_set
*set
)
10211 isl_basic_set_list
*list
;
10216 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10217 for (i
= 0; i
< set
->n
; ++i
) {
10218 isl_basic_set
*bset
;
10220 bset
= isl_basic_set_copy(set
->p
[i
]);
10221 list
= isl_basic_set_list_add(list
, bset
);
10227 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10234 bset
= isl_basic_set_cow(bset
);
10238 dim
= isl_basic_set_get_space(bset
);
10239 dim
= isl_space_lift(dim
, bset
->n_div
);
10242 isl_space_free(bset
->dim
);
10244 bset
->extra
-= bset
->n_div
;
10247 bset
= isl_basic_set_finalize(bset
);
10251 isl_basic_set_free(bset
);
10255 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10261 set
= isl_set_align_divs(set
);
10266 set
= isl_set_cow(set
);
10270 n_div
= set
->p
[0]->n_div
;
10271 dim
= isl_set_get_space(set
);
10272 dim
= isl_space_lift(dim
, n_div
);
10275 isl_space_free(set
->dim
);
10278 for (i
= 0; i
< set
->n
; ++i
) {
10279 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10290 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10293 struct isl_basic_map
*bmap
;
10300 set
= isl_set_align_divs(set
);
10305 dim
= isl_set_get_space(set
);
10306 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10308 return isl_map_identity(isl_space_map_from_set(dim
));
10311 n_div
= set
->p
[0]->n_div
;
10312 dim
= isl_space_map_from_set(dim
);
10313 n_param
= isl_space_dim(dim
, isl_dim_param
);
10314 n_set
= isl_space_dim(dim
, isl_dim_in
);
10315 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10316 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10317 for (i
= 0; i
< n_set
; ++i
)
10318 bmap
= var_equal(bmap
, i
);
10320 total
= n_param
+ n_set
+ n_set
+ n_div
;
10321 for (i
= 0; i
< n_div
; ++i
) {
10322 k
= isl_basic_map_alloc_inequality(bmap
);
10325 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10326 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10327 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10328 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10329 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10330 set
->p
[0]->div
[i
][0]);
10332 l
= isl_basic_map_alloc_inequality(bmap
);
10335 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10336 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10337 set
->p
[0]->div
[i
][0]);
10338 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10342 bmap
= isl_basic_map_simplify(bmap
);
10343 bmap
= isl_basic_map_finalize(bmap
);
10344 return isl_map_from_basic_map(bmap
);
10347 isl_basic_map_free(bmap
);
10351 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10359 dim
= isl_basic_set_total_dim(bset
);
10360 size
+= bset
->n_eq
* (1 + dim
);
10361 size
+= bset
->n_ineq
* (1 + dim
);
10362 size
+= bset
->n_div
* (2 + dim
);
10367 int isl_set_size(__isl_keep isl_set
*set
)
10375 for (i
= 0; i
< set
->n
; ++i
)
10376 size
+= isl_basic_set_size(set
->p
[i
]);
10381 /* Check if there is any lower bound (if lower == 0) and/or upper
10382 * bound (if upper == 0) on the specified dim.
10384 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10385 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10390 return isl_bool_error
;
10392 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10393 return isl_bool_error
);
10395 pos
+= isl_basic_map_offset(bmap
, type
);
10397 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10398 if (isl_int_is_zero(bmap
->div
[i
][0]))
10400 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10401 return isl_bool_true
;
10404 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10405 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10406 return isl_bool_true
;
10408 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10409 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10416 return lower
&& upper
;
10419 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10420 enum isl_dim_type type
, unsigned pos
)
10422 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10425 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10426 enum isl_dim_type type
, unsigned pos
)
10428 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10431 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10432 enum isl_dim_type type
, unsigned pos
)
10434 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10437 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10438 enum isl_dim_type type
, unsigned pos
)
10445 for (i
= 0; i
< map
->n
; ++i
) {
10447 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10448 if (bounded
< 0 || !bounded
)
10455 /* Return 1 if the specified dim is involved in both an upper bound
10456 * and a lower bound.
10458 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10459 enum isl_dim_type type
, unsigned pos
)
10461 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10464 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10466 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10467 enum isl_dim_type type
, unsigned pos
,
10468 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10469 enum isl_dim_type type
, unsigned pos
))
10474 return isl_bool_error
;
10476 for (i
= 0; i
< map
->n
; ++i
) {
10478 bounded
= fn(map
->p
[i
], type
, pos
);
10479 if (bounded
< 0 || bounded
)
10483 return isl_bool_false
;
10486 /* Return 1 if the specified dim is involved in any lower bound.
10488 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10489 enum isl_dim_type type
, unsigned pos
)
10491 return has_any_bound(set
, type
, pos
,
10492 &isl_basic_map_dim_has_lower_bound
);
10495 /* Return 1 if the specified dim is involved in any upper bound.
10497 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10498 enum isl_dim_type type
, unsigned pos
)
10500 return has_any_bound(set
, type
, pos
,
10501 &isl_basic_map_dim_has_upper_bound
);
10504 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10506 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10507 enum isl_dim_type type
, unsigned pos
,
10508 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10509 enum isl_dim_type type
, unsigned pos
))
10514 return isl_bool_error
;
10516 for (i
= 0; i
< map
->n
; ++i
) {
10518 bounded
= fn(map
->p
[i
], type
, pos
);
10519 if (bounded
< 0 || !bounded
)
10523 return isl_bool_true
;
10526 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10528 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10529 enum isl_dim_type type
, unsigned pos
)
10531 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10534 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10536 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10537 enum isl_dim_type type
, unsigned pos
)
10539 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10542 /* For each of the "n" variables starting at "first", determine
10543 * the sign of the variable and put the results in the first "n"
10544 * elements of the array "signs".
10546 * 1 means that the variable is non-negative
10547 * -1 means that the variable is non-positive
10548 * 0 means the variable attains both positive and negative values.
10550 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10551 unsigned first
, unsigned n
, int *signs
)
10553 isl_vec
*bound
= NULL
;
10554 struct isl_tab
*tab
= NULL
;
10555 struct isl_tab_undo
*snap
;
10558 if (!bset
|| !signs
)
10561 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10562 tab
= isl_tab_from_basic_set(bset
, 0);
10563 if (!bound
|| !tab
)
10566 isl_seq_clr(bound
->el
, bound
->size
);
10567 isl_int_set_si(bound
->el
[0], -1);
10569 snap
= isl_tab_snap(tab
);
10570 for (i
= 0; i
< n
; ++i
) {
10573 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10574 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10576 empty
= tab
->empty
;
10577 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10578 if (isl_tab_rollback(tab
, snap
) < 0)
10586 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10587 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10589 empty
= tab
->empty
;
10590 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10591 if (isl_tab_rollback(tab
, snap
) < 0)
10594 signs
[i
] = empty
? -1 : 0;
10598 isl_vec_free(bound
);
10602 isl_vec_free(bound
);
10606 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10607 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10609 if (!bset
|| !signs
)
10611 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10614 first
+= pos(bset
->dim
, type
) - 1;
10615 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10618 /* Is it possible for the integer division "div" to depend (possibly
10619 * indirectly) on any output dimensions?
10621 * If the div is undefined, then we conservatively assume that it
10622 * may depend on them.
10623 * Otherwise, we check if it actually depends on them or on any integer
10624 * divisions that may depend on them.
10626 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10629 unsigned n_out
, o_out
;
10630 unsigned n_div
, o_div
;
10632 if (isl_int_is_zero(bmap
->div
[div
][0]))
10635 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10636 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10638 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10641 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10642 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10644 for (i
= 0; i
< n_div
; ++i
) {
10645 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10647 if (div_may_involve_output(bmap
, i
))
10654 /* Return the first integer division of "bmap" in the range
10655 * [first, first + n[ that may depend on any output dimensions and
10656 * that has a non-zero coefficient in "c" (where the first coefficient
10657 * in "c" corresponds to integer division "first").
10659 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10660 isl_int
*c
, int first
, int n
)
10667 for (k
= first
; k
< first
+ n
; ++k
) {
10668 if (isl_int_is_zero(c
[k
]))
10670 if (div_may_involve_output(bmap
, k
))
10677 /* Look for a pair of inequality constraints in "bmap" of the form
10679 * -l + i >= 0 or i >= l
10681 * n + l - i >= 0 or i <= l + n
10683 * with n < "m" and i the output dimension at position "pos".
10684 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10685 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10686 * and earlier output dimensions, as well as integer divisions that do
10687 * not involve any of the output dimensions.
10689 * Return the index of the first inequality constraint or bmap->n_ineq
10690 * if no such pair can be found.
10692 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10693 int pos
, isl_int m
)
10698 unsigned n_div
, o_div
;
10699 unsigned n_out
, o_out
;
10705 ctx
= isl_basic_map_get_ctx(bmap
);
10706 total
= isl_basic_map_total_dim(bmap
);
10707 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10708 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10709 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10710 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10711 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10712 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10714 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10715 n_out
- (pos
+ 1)) != -1)
10717 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10720 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10721 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10724 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10725 bmap
->ineq
[j
] + 1, total
))
10729 if (j
>= bmap
->n_ineq
)
10731 isl_int_add(bmap
->ineq
[i
][0],
10732 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10733 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10734 isl_int_sub(bmap
->ineq
[i
][0],
10735 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10738 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10744 return bmap
->n_ineq
;
10747 /* Return the index of the equality of "bmap" that defines
10748 * the output dimension "pos" in terms of earlier dimensions.
10749 * The equality may also involve integer divisions, as long
10750 * as those integer divisions are defined in terms of
10751 * parameters or input dimensions.
10752 * In this case, *div is set to the number of integer divisions and
10753 * *ineq is set to the number of inequality constraints (provided
10754 * div and ineq are not NULL).
10756 * The equality may also involve a single integer division involving
10757 * the output dimensions (typically only output dimension "pos") as
10758 * long as the coefficient of output dimension "pos" is 1 or -1 and
10759 * there is a pair of constraints i >= l and i <= l + n, with i referring
10760 * to output dimension "pos", l an expression involving only earlier
10761 * dimensions and n smaller than the coefficient of the integer division
10762 * in the equality. In this case, the output dimension can be defined
10763 * in terms of a modulo expression that does not involve the integer division.
10764 * *div is then set to this single integer division and
10765 * *ineq is set to the index of constraint i >= l.
10767 * Return bmap->n_eq if there is no such equality.
10768 * Return -1 on error.
10770 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10771 int pos
, int *div
, int *ineq
)
10774 unsigned n_out
, o_out
;
10775 unsigned n_div
, o_div
;
10780 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10781 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10782 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10783 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10786 *ineq
= bmap
->n_ineq
;
10789 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10790 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10792 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10793 n_out
- (pos
+ 1)) != -1)
10795 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10799 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10800 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10802 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10803 k
+ 1, n_div
- (k
+1)) < n_div
)
10805 l
= find_modulo_constraint_pair(bmap
, pos
,
10806 bmap
->eq
[j
][o_div
+ k
]);
10809 if (l
>= bmap
->n_ineq
)
10821 /* Check if the given basic map is obviously single-valued.
10822 * In particular, for each output dimension, check that there is
10823 * an equality that defines the output dimension in terms of
10824 * earlier dimensions.
10826 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10832 return isl_bool_error
;
10834 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10836 for (i
= 0; i
< n_out
; ++i
) {
10839 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10842 return isl_bool_error
;
10843 if (eq
>= bmap
->n_eq
)
10844 return isl_bool_false
;
10847 return isl_bool_true
;
10850 /* Check if the given basic map is single-valued.
10851 * We simply compute
10855 * and check if the result is a subset of the identity mapping.
10857 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10860 isl_basic_map
*test
;
10864 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10868 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10869 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10871 space
= isl_basic_map_get_space(bmap
);
10872 space
= isl_space_map_from_set(isl_space_range(space
));
10873 id
= isl_basic_map_identity(space
);
10875 sv
= isl_basic_map_is_subset(test
, id
);
10877 isl_basic_map_free(test
);
10878 isl_basic_map_free(id
);
10883 /* Check if the given map is obviously single-valued.
10885 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10888 return isl_bool_error
;
10890 return isl_bool_true
;
10892 return isl_bool_false
;
10894 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10897 /* Check if the given map is single-valued.
10898 * We simply compute
10902 * and check if the result is a subset of the identity mapping.
10904 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10911 sv
= isl_map_plain_is_single_valued(map
);
10915 test
= isl_map_reverse(isl_map_copy(map
));
10916 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10918 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10919 id
= isl_map_identity(dim
);
10921 sv
= isl_map_is_subset(test
, id
);
10923 isl_map_free(test
);
10929 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10933 map
= isl_map_copy(map
);
10934 map
= isl_map_reverse(map
);
10935 in
= isl_map_is_single_valued(map
);
10941 /* Check if the given map is obviously injective.
10943 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10947 map
= isl_map_copy(map
);
10948 map
= isl_map_reverse(map
);
10949 in
= isl_map_plain_is_single_valued(map
);
10955 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10959 sv
= isl_map_is_single_valued(map
);
10963 return isl_map_is_injective(map
);
10966 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10968 return isl_map_is_single_valued(set_to_map(set
));
10971 /* Does "map" only map elements to themselves?
10973 * If the domain and range spaces are different, then "map"
10974 * is considered not to be an identity relation, even if it is empty.
10975 * Otherwise, construct the maximal identity relation and
10976 * check whether "map" is a subset of this relation.
10978 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10982 isl_bool equal
, is_identity
;
10984 space
= isl_map_get_space(map
);
10985 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10986 isl_space_free(space
);
10987 if (equal
< 0 || !equal
)
10990 id
= isl_map_identity(isl_map_get_space(map
));
10991 is_identity
= isl_map_is_subset(map
, id
);
10994 return is_identity
;
10997 int isl_map_is_translation(__isl_keep isl_map
*map
)
11002 delta
= isl_map_deltas(isl_map_copy(map
));
11003 ok
= isl_set_is_singleton(delta
);
11004 isl_set_free(delta
);
11009 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11011 if (isl_seq_first_non_zero(p
, pos
) != -1)
11013 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11018 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11027 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11030 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11031 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11032 for (j
= 0; j
< nvar
; ++j
) {
11033 int lower
= 0, upper
= 0;
11034 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11035 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11037 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11041 if (i
< bset
->n_eq
)
11043 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11044 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11046 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11048 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11053 if (!lower
|| !upper
)
11060 int isl_set_is_box(__isl_keep isl_set
*set
)
11067 return isl_basic_set_is_box(set
->p
[0]);
11070 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11073 return isl_bool_error
;
11075 return isl_space_is_wrapping(bset
->dim
);
11078 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11081 return isl_bool_error
;
11083 return isl_space_is_wrapping(set
->dim
);
11086 /* Modify the space of "map" through a call to "change".
11087 * If "can_change" is set (not NULL), then first call it to check
11088 * if the modification is allowed, printing the error message "cannot_change"
11091 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11092 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11093 const char *cannot_change
,
11094 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11102 ok
= can_change
? can_change(map
) : isl_bool_true
;
11104 return isl_map_free(map
);
11106 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11107 return isl_map_free(map
));
11109 space
= change(isl_map_get_space(map
));
11110 map
= isl_map_reset_space(map
, space
);
11115 /* Is the domain of "map" a wrapped relation?
11117 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11120 return isl_bool_error
;
11122 return isl_space_domain_is_wrapping(map
->dim
);
11125 /* Is the range of "map" a wrapped relation?
11127 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11130 return isl_bool_error
;
11132 return isl_space_range_is_wrapping(map
->dim
);
11135 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11137 bmap
= isl_basic_map_cow(bmap
);
11141 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11145 bmap
= isl_basic_map_finalize(bmap
);
11147 return bset_from_bmap(bmap
);
11149 isl_basic_map_free(bmap
);
11153 /* Given a map A -> B, return the set (A -> B).
11155 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11157 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11160 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11162 bset
= isl_basic_set_cow(bset
);
11166 bset
->dim
= isl_space_unwrap(bset
->dim
);
11170 bset
= isl_basic_set_finalize(bset
);
11172 return bset_to_bmap(bset
);
11174 isl_basic_set_free(bset
);
11178 /* Given a set (A -> B), return the map A -> B.
11179 * Error out if "set" is not of the form (A -> B).
11181 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11183 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11184 "not a wrapping set", &isl_space_unwrap
);
11187 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11188 enum isl_dim_type type
)
11193 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11196 bmap
= isl_basic_map_cow(bmap
);
11200 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11204 bmap
= isl_basic_map_finalize(bmap
);
11208 isl_basic_map_free(bmap
);
11212 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11213 enum isl_dim_type type
)
11220 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11223 map
= isl_map_cow(map
);
11227 for (i
= 0; i
< map
->n
; ++i
) {
11228 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11232 map
->dim
= isl_space_reset(map
->dim
, type
);
11242 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11247 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11250 bmap
= isl_basic_map_cow(bmap
);
11254 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11258 bmap
= isl_basic_map_finalize(bmap
);
11262 isl_basic_map_free(bmap
);
11266 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11268 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11271 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11272 __isl_take isl_basic_map
*bmap
)
11277 if (!bmap
->dim
->nested
[0])
11280 bmap
= isl_basic_map_cow(bmap
);
11284 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11288 bmap
= isl_basic_map_finalize(bmap
);
11292 isl_basic_map_free(bmap
);
11296 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11297 __isl_take isl_basic_map
*bmap
)
11302 if (!bmap
->dim
->nested
[1])
11305 bmap
= isl_basic_map_cow(bmap
);
11309 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11313 bmap
= isl_basic_map_finalize(bmap
);
11317 isl_basic_map_free(bmap
);
11321 /* Remove any internal structure from the spaces of domain and range of "map".
11323 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11328 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11331 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11334 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11336 return set_from_map(isl_map_flatten(set_to_map(set
)));
11339 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11341 isl_space
*dim
, *flat_dim
;
11344 dim
= isl_set_get_space(set
);
11345 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11346 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11347 map
= isl_map_intersect_domain(map
, set
);
11352 /* Remove any internal structure from the space of the domain of "map".
11354 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11359 if (!map
->dim
->nested
[0])
11362 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11365 /* Remove any internal structure from the space of the range of "map".
11367 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11372 if (!map
->dim
->nested
[1])
11375 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11378 /* Reorder the dimensions of "bmap" according to the given dim_map
11379 * and set the dimension specification to "dim" and
11380 * perform Gaussian elimination on the result.
11382 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11383 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11385 isl_basic_map
*res
;
11388 bmap
= isl_basic_map_cow(bmap
);
11389 if (!bmap
|| !dim
|| !dim_map
)
11392 flags
= bmap
->flags
;
11393 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11394 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11395 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11396 res
= isl_basic_map_alloc_space(dim
,
11397 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11398 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11400 res
->flags
= flags
;
11401 res
= isl_basic_map_gauss(res
, NULL
);
11402 res
= isl_basic_map_finalize(res
);
11406 isl_basic_map_free(bmap
);
11407 isl_space_free(dim
);
11411 /* Reorder the dimensions of "map" according to given reordering.
11413 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11414 __isl_take isl_reordering
*r
)
11417 struct isl_dim_map
*dim_map
;
11419 map
= isl_map_cow(map
);
11420 dim_map
= isl_dim_map_from_reordering(r
);
11421 if (!map
|| !r
|| !dim_map
)
11424 for (i
= 0; i
< map
->n
; ++i
) {
11425 struct isl_dim_map
*dim_map_i
;
11427 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11429 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11430 isl_space_copy(r
->dim
), dim_map_i
);
11436 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11438 isl_reordering_free(r
);
11444 isl_reordering_free(r
);
11448 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11449 __isl_take isl_reordering
*r
)
11451 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11454 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11455 __isl_take isl_space
*model
)
11459 if (!map
|| !model
)
11462 ctx
= isl_space_get_ctx(model
);
11463 if (!isl_space_has_named_params(model
))
11464 isl_die(ctx
, isl_error_invalid
,
11465 "model has unnamed parameters", goto error
);
11466 if (!isl_space_has_named_params(map
->dim
))
11467 isl_die(ctx
, isl_error_invalid
,
11468 "relation has unnamed parameters", goto error
);
11469 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11470 isl_reordering
*exp
;
11472 model
= isl_space_drop_dims(model
, isl_dim_in
,
11473 0, isl_space_dim(model
, isl_dim_in
));
11474 model
= isl_space_drop_dims(model
, isl_dim_out
,
11475 0, isl_space_dim(model
, isl_dim_out
));
11476 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11477 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11478 map
= isl_map_realign(map
, exp
);
11481 isl_space_free(model
);
11484 isl_space_free(model
);
11489 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11490 __isl_take isl_space
*model
)
11492 return isl_map_align_params(set
, model
);
11495 /* Align the parameters of "bmap" to those of "model", introducing
11496 * additional parameters if needed.
11498 __isl_give isl_basic_map
*isl_basic_map_align_params(
11499 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11503 if (!bmap
|| !model
)
11506 ctx
= isl_space_get_ctx(model
);
11507 if (!isl_space_has_named_params(model
))
11508 isl_die(ctx
, isl_error_invalid
,
11509 "model has unnamed parameters", goto error
);
11510 if (!isl_space_has_named_params(bmap
->dim
))
11511 isl_die(ctx
, isl_error_invalid
,
11512 "relation has unnamed parameters", goto error
);
11513 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11514 isl_reordering
*exp
;
11515 struct isl_dim_map
*dim_map
;
11517 model
= isl_space_drop_dims(model
, isl_dim_in
,
11518 0, isl_space_dim(model
, isl_dim_in
));
11519 model
= isl_space_drop_dims(model
, isl_dim_out
,
11520 0, isl_space_dim(model
, isl_dim_out
));
11521 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11522 exp
= isl_reordering_extend_space(exp
,
11523 isl_basic_map_get_space(bmap
));
11524 dim_map
= isl_dim_map_from_reordering(exp
);
11525 bmap
= isl_basic_map_realign(bmap
,
11526 exp
? isl_space_copy(exp
->dim
) : NULL
,
11527 isl_dim_map_extend(dim_map
, bmap
));
11528 isl_reordering_free(exp
);
11532 isl_space_free(model
);
11535 isl_space_free(model
);
11536 isl_basic_map_free(bmap
);
11540 /* Align the parameters of "bset" to those of "model", introducing
11541 * additional parameters if needed.
11543 __isl_give isl_basic_set
*isl_basic_set_align_params(
11544 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11546 return isl_basic_map_align_params(bset
, model
);
11549 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11550 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11551 enum isl_dim_type c2
, enum isl_dim_type c3
,
11552 enum isl_dim_type c4
, enum isl_dim_type c5
)
11554 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11555 struct isl_mat
*mat
;
11561 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11562 isl_basic_map_total_dim(bmap
) + 1);
11565 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11566 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11567 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11568 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11569 isl_int_set(mat
->row
[i
][pos
],
11570 bmap
->eq
[i
][off
+ k
]);
11578 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11579 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11580 enum isl_dim_type c2
, enum isl_dim_type c3
,
11581 enum isl_dim_type c4
, enum isl_dim_type c5
)
11583 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11584 struct isl_mat
*mat
;
11590 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11591 isl_basic_map_total_dim(bmap
) + 1);
11594 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11595 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11596 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11597 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11598 isl_int_set(mat
->row
[i
][pos
],
11599 bmap
->ineq
[i
][off
+ k
]);
11607 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11608 __isl_take isl_space
*dim
,
11609 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11610 enum isl_dim_type c2
, enum isl_dim_type c3
,
11611 enum isl_dim_type c4
, enum isl_dim_type c5
)
11613 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11614 isl_basic_map
*bmap
;
11620 if (!dim
|| !eq
|| !ineq
)
11623 if (eq
->n_col
!= ineq
->n_col
)
11624 isl_die(dim
->ctx
, isl_error_invalid
,
11625 "equalities and inequalities matrices should have "
11626 "same number of columns", goto error
);
11628 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11630 if (eq
->n_col
< total
)
11631 isl_die(dim
->ctx
, isl_error_invalid
,
11632 "number of columns too small", goto error
);
11634 extra
= eq
->n_col
- total
;
11636 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11637 eq
->n_row
, ineq
->n_row
);
11640 for (i
= 0; i
< extra
; ++i
) {
11641 k
= isl_basic_map_alloc_div(bmap
);
11644 isl_int_set_si(bmap
->div
[k
][0], 0);
11646 for (i
= 0; i
< eq
->n_row
; ++i
) {
11647 l
= isl_basic_map_alloc_equality(bmap
);
11650 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11651 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11652 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11653 isl_int_set(bmap
->eq
[l
][off
+ k
],
11659 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11660 l
= isl_basic_map_alloc_inequality(bmap
);
11663 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11664 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11665 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11666 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11667 ineq
->row
[i
][pos
]);
11673 isl_space_free(dim
);
11675 isl_mat_free(ineq
);
11677 bmap
= isl_basic_map_simplify(bmap
);
11678 return isl_basic_map_finalize(bmap
);
11680 isl_space_free(dim
);
11682 isl_mat_free(ineq
);
11686 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11687 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11688 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11690 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11691 c1
, c2
, c3
, c4
, isl_dim_in
);
11694 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11695 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11696 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11698 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11699 c1
, c2
, c3
, c4
, isl_dim_in
);
11702 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11703 __isl_take isl_space
*dim
,
11704 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11705 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11707 isl_basic_map
*bmap
;
11708 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11709 c1
, c2
, c3
, c4
, isl_dim_in
);
11710 return bset_from_bmap(bmap
);
11713 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11716 return isl_bool_error
;
11718 return isl_space_can_zip(bmap
->dim
);
11721 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11724 return isl_bool_error
;
11726 return isl_space_can_zip(map
->dim
);
11729 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11730 * (A -> C) -> (B -> D).
11732 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11741 if (!isl_basic_map_can_zip(bmap
))
11742 isl_die(bmap
->ctx
, isl_error_invalid
,
11743 "basic map cannot be zipped", goto error
);
11744 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11745 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11746 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11747 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11748 bmap
= isl_basic_map_cow(bmap
);
11749 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11752 bmap
->dim
= isl_space_zip(bmap
->dim
);
11755 bmap
= isl_basic_map_mark_final(bmap
);
11758 isl_basic_map_free(bmap
);
11762 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11763 * (A -> C) -> (B -> D).
11765 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11772 if (!isl_map_can_zip(map
))
11773 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11776 map
= isl_map_cow(map
);
11780 for (i
= 0; i
< map
->n
; ++i
) {
11781 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11786 map
->dim
= isl_space_zip(map
->dim
);
11796 /* Can we apply isl_basic_map_curry to "bmap"?
11797 * That is, does it have a nested relation in its domain?
11799 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11802 return isl_bool_error
;
11804 return isl_space_can_curry(bmap
->dim
);
11807 /* Can we apply isl_map_curry to "map"?
11808 * That is, does it have a nested relation in its domain?
11810 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11813 return isl_bool_error
;
11815 return isl_space_can_curry(map
->dim
);
11818 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11821 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11827 if (!isl_basic_map_can_curry(bmap
))
11828 isl_die(bmap
->ctx
, isl_error_invalid
,
11829 "basic map cannot be curried", goto error
);
11830 bmap
= isl_basic_map_cow(bmap
);
11833 bmap
->dim
= isl_space_curry(bmap
->dim
);
11836 bmap
= isl_basic_map_mark_final(bmap
);
11839 isl_basic_map_free(bmap
);
11843 /* Given a map (A -> B) -> C, return the corresponding map
11846 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11848 return isl_map_change_space(map
, &isl_map_can_curry
,
11849 "map cannot be curried", &isl_space_curry
);
11852 /* Can isl_map_range_curry be applied to "map"?
11853 * That is, does it have a nested relation in its range,
11854 * the domain of which is itself a nested relation?
11856 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11859 return isl_bool_error
;
11861 return isl_space_can_range_curry(map
->dim
);
11864 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11865 * A -> (B -> (C -> D)).
11867 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11869 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11870 "map range cannot be curried",
11871 &isl_space_range_curry
);
11874 /* Can we apply isl_basic_map_uncurry to "bmap"?
11875 * That is, does it have a nested relation in its domain?
11877 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11880 return isl_bool_error
;
11882 return isl_space_can_uncurry(bmap
->dim
);
11885 /* Can we apply isl_map_uncurry to "map"?
11886 * That is, does it have a nested relation in its domain?
11888 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11891 return isl_bool_error
;
11893 return isl_space_can_uncurry(map
->dim
);
11896 /* Given a basic map A -> (B -> C), return the corresponding basic map
11899 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11905 if (!isl_basic_map_can_uncurry(bmap
))
11906 isl_die(bmap
->ctx
, isl_error_invalid
,
11907 "basic map cannot be uncurried",
11908 return isl_basic_map_free(bmap
));
11909 bmap
= isl_basic_map_cow(bmap
);
11912 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11914 return isl_basic_map_free(bmap
);
11915 bmap
= isl_basic_map_mark_final(bmap
);
11919 /* Given a map A -> (B -> C), return the corresponding map
11922 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11924 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11925 "map cannot be uncurried", &isl_space_uncurry
);
11928 /* Construct a basic map mapping the domain of the affine expression
11929 * to a one-dimensional range prescribed by the affine expression.
11930 * If "rational" is set, then construct a rational basic map.
11932 * A NaN affine expression cannot be converted to a basic map.
11934 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11935 __isl_take isl_aff
*aff
, int rational
)
11940 isl_local_space
*ls
;
11941 isl_basic_map
*bmap
= NULL
;
11945 is_nan
= isl_aff_is_nan(aff
);
11949 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11950 "cannot convert NaN", goto error
);
11952 ls
= isl_aff_get_local_space(aff
);
11953 bmap
= isl_basic_map_from_local_space(ls
);
11954 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11955 k
= isl_basic_map_alloc_equality(bmap
);
11959 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11960 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11961 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11962 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11963 aff
->v
->size
- (pos
+ 1));
11967 bmap
= isl_basic_map_set_rational(bmap
);
11968 bmap
= isl_basic_map_finalize(bmap
);
11972 isl_basic_map_free(bmap
);
11976 /* Construct a basic map mapping the domain of the affine expression
11977 * to a one-dimensional range prescribed by the affine expression.
11979 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11981 return isl_basic_map_from_aff2(aff
, 0);
11984 /* Construct a map mapping the domain of the affine expression
11985 * to a one-dimensional range prescribed by the affine expression.
11987 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11989 isl_basic_map
*bmap
;
11991 bmap
= isl_basic_map_from_aff(aff
);
11992 return isl_map_from_basic_map(bmap
);
11995 /* Construct a basic map mapping the domain the multi-affine expression
11996 * to its range, with each dimension in the range equated to the
11997 * corresponding affine expression.
11998 * If "rational" is set, then construct a rational basic map.
12000 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12001 __isl_take isl_multi_aff
*maff
, int rational
)
12005 isl_basic_map
*bmap
;
12010 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12011 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12012 "invalid space", goto error
);
12014 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12015 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12017 bmap
= isl_basic_map_set_rational(bmap
);
12019 for (i
= 0; i
< maff
->n
; ++i
) {
12021 isl_basic_map
*bmap_i
;
12023 aff
= isl_aff_copy(maff
->p
[i
]);
12024 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12026 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12029 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12031 isl_multi_aff_free(maff
);
12034 isl_multi_aff_free(maff
);
12038 /* Construct a basic map mapping the domain the multi-affine expression
12039 * to its range, with each dimension in the range equated to the
12040 * corresponding affine expression.
12042 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12043 __isl_take isl_multi_aff
*ma
)
12045 return isl_basic_map_from_multi_aff2(ma
, 0);
12048 /* Construct a map mapping the domain the multi-affine expression
12049 * to its range, with each dimension in the range equated to the
12050 * corresponding affine expression.
12052 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12054 isl_basic_map
*bmap
;
12056 bmap
= isl_basic_map_from_multi_aff(maff
);
12057 return isl_map_from_basic_map(bmap
);
12060 /* Construct a basic map mapping a domain in the given space to
12061 * to an n-dimensional range, with n the number of elements in the list,
12062 * where each coordinate in the range is prescribed by the
12063 * corresponding affine expression.
12064 * The domains of all affine expressions in the list are assumed to match
12067 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12068 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12072 isl_basic_map
*bmap
;
12077 dim
= isl_space_from_domain(domain_dim
);
12078 bmap
= isl_basic_map_universe(dim
);
12080 for (i
= 0; i
< list
->n
; ++i
) {
12082 isl_basic_map
*bmap_i
;
12084 aff
= isl_aff_copy(list
->p
[i
]);
12085 bmap_i
= isl_basic_map_from_aff(aff
);
12087 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12090 isl_aff_list_free(list
);
12094 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12095 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12097 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12100 /* Construct a basic map where the given dimensions are equal to each other.
12102 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12103 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12105 isl_basic_map
*bmap
= NULL
;
12111 if (pos1
>= isl_space_dim(space
, type1
))
12112 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12113 "index out of bounds", goto error
);
12114 if (pos2
>= isl_space_dim(space
, type2
))
12115 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12116 "index out of bounds", goto error
);
12118 if (type1
== type2
&& pos1
== pos2
)
12119 return isl_basic_map_universe(space
);
12121 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12122 i
= isl_basic_map_alloc_equality(bmap
);
12125 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12126 pos1
+= isl_basic_map_offset(bmap
, type1
);
12127 pos2
+= isl_basic_map_offset(bmap
, type2
);
12128 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12129 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12130 bmap
= isl_basic_map_finalize(bmap
);
12131 isl_space_free(space
);
12134 isl_space_free(space
);
12135 isl_basic_map_free(bmap
);
12139 /* Add a constraint imposing that the given two dimensions are equal.
12141 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12142 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12146 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12148 bmap
= isl_basic_map_intersect(bmap
, eq
);
12153 /* Add a constraint imposing that the given two dimensions are equal.
12155 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12156 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12158 isl_basic_map
*bmap
;
12160 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12162 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12167 /* Add a constraint imposing that the given two dimensions have opposite values.
12169 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12170 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12172 isl_basic_map
*bmap
= NULL
;
12178 if (pos1
>= isl_map_dim(map
, type1
))
12179 isl_die(map
->ctx
, isl_error_invalid
,
12180 "index out of bounds", goto error
);
12181 if (pos2
>= isl_map_dim(map
, type2
))
12182 isl_die(map
->ctx
, isl_error_invalid
,
12183 "index out of bounds", goto error
);
12185 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12186 i
= isl_basic_map_alloc_equality(bmap
);
12189 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12190 pos1
+= isl_basic_map_offset(bmap
, type1
);
12191 pos2
+= isl_basic_map_offset(bmap
, type2
);
12192 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12193 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12194 bmap
= isl_basic_map_finalize(bmap
);
12196 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12200 isl_basic_map_free(bmap
);
12205 /* Construct a constraint imposing that the value of the first dimension is
12206 * greater than or equal to that of the second.
12208 static __isl_give isl_constraint
*constraint_order_ge(
12209 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12210 enum isl_dim_type type2
, int pos2
)
12217 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12219 if (pos1
>= isl_constraint_dim(c
, type1
))
12220 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12221 "index out of bounds", return isl_constraint_free(c
));
12222 if (pos2
>= isl_constraint_dim(c
, type2
))
12223 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12224 "index out of bounds", return isl_constraint_free(c
));
12226 if (type1
== type2
&& pos1
== pos2
)
12229 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12230 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12235 /* Add a constraint imposing that the value of the first dimension is
12236 * greater than or equal to that of the second.
12238 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12239 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12244 if (type1
== type2
&& pos1
== pos2
)
12246 space
= isl_basic_map_get_space(bmap
);
12247 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12248 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12253 /* Add a constraint imposing that the value of the first dimension is
12254 * greater than or equal to that of the second.
12256 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12257 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12262 if (type1
== type2
&& pos1
== pos2
)
12264 space
= isl_map_get_space(map
);
12265 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12266 map
= isl_map_add_constraint(map
, c
);
12271 /* Add a constraint imposing that the value of the first dimension is
12272 * less than or equal to that of the second.
12274 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12275 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12277 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12280 /* Construct a basic map where the value of the first dimension is
12281 * greater than that of the second.
12283 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12284 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12286 isl_basic_map
*bmap
= NULL
;
12292 if (pos1
>= isl_space_dim(space
, type1
))
12293 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12294 "index out of bounds", goto error
);
12295 if (pos2
>= isl_space_dim(space
, type2
))
12296 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12297 "index out of bounds", goto error
);
12299 if (type1
== type2
&& pos1
== pos2
)
12300 return isl_basic_map_empty(space
);
12302 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12303 i
= isl_basic_map_alloc_inequality(bmap
);
12305 return isl_basic_map_free(bmap
);
12306 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12307 pos1
+= isl_basic_map_offset(bmap
, type1
);
12308 pos2
+= isl_basic_map_offset(bmap
, type2
);
12309 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12310 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12311 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12312 bmap
= isl_basic_map_finalize(bmap
);
12316 isl_space_free(space
);
12317 isl_basic_map_free(bmap
);
12321 /* Add a constraint imposing that the value of the first dimension is
12322 * greater than that of the second.
12324 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12325 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12329 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12331 bmap
= isl_basic_map_intersect(bmap
, gt
);
12336 /* Add a constraint imposing that the value of the first dimension is
12337 * greater than that of the second.
12339 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12340 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12342 isl_basic_map
*bmap
;
12344 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12346 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12351 /* Add a constraint imposing that the value of the first dimension is
12352 * smaller than that of the second.
12354 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12355 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12357 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12360 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12364 isl_local_space
*ls
;
12369 if (!isl_basic_map_divs_known(bmap
))
12370 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12371 "some divs are unknown", return NULL
);
12373 ls
= isl_basic_map_get_local_space(bmap
);
12374 div
= isl_local_space_get_div(ls
, pos
);
12375 isl_local_space_free(ls
);
12380 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12383 return isl_basic_map_get_div(bset
, pos
);
12386 /* Plug in "subs" for dimension "type", "pos" of "bset".
12388 * Let i be the dimension to replace and let "subs" be of the form
12392 * Any integer division with a non-zero coefficient for i,
12394 * floor((a i + g)/m)
12398 * floor((a f + d g)/(m d))
12400 * Constraints of the form
12408 * We currently require that "subs" is an integral expression.
12409 * Handling rational expressions may require us to add stride constraints
12410 * as we do in isl_basic_set_preimage_multi_aff.
12412 __isl_give isl_basic_set
*isl_basic_set_substitute(
12413 __isl_take isl_basic_set
*bset
,
12414 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12420 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12423 bset
= isl_basic_set_cow(bset
);
12424 if (!bset
|| !subs
)
12427 ctx
= isl_basic_set_get_ctx(bset
);
12428 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12429 isl_die(ctx
, isl_error_invalid
,
12430 "spaces don't match", goto error
);
12431 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12432 isl_die(ctx
, isl_error_unsupported
,
12433 "cannot handle divs yet", goto error
);
12434 if (!isl_int_is_one(subs
->v
->el
[0]))
12435 isl_die(ctx
, isl_error_invalid
,
12436 "can only substitute integer expressions", goto error
);
12438 pos
+= isl_basic_set_offset(bset
, type
);
12442 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12443 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12445 isl_int_set(v
, bset
->eq
[i
][pos
]);
12446 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12447 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12448 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12451 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12452 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12454 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12455 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12456 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12457 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12460 for (i
= 0; i
< bset
->n_div
; ++i
) {
12461 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12463 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12464 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12465 isl_seq_combine(bset
->div
[i
] + 1,
12466 subs
->v
->el
[0], bset
->div
[i
] + 1,
12467 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12468 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12473 bset
= isl_basic_set_simplify(bset
);
12474 return isl_basic_set_finalize(bset
);
12476 isl_basic_set_free(bset
);
12480 /* Plug in "subs" for dimension "type", "pos" of "set".
12482 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12483 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12487 if (set
&& isl_set_plain_is_empty(set
))
12490 set
= isl_set_cow(set
);
12494 for (i
= set
->n
- 1; i
>= 0; --i
) {
12495 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12496 if (remove_if_empty(set
, i
) < 0)
12506 /* Check if the range of "ma" is compatible with the domain or range
12507 * (depending on "type") of "bmap".
12508 * Return -1 if anything is wrong.
12510 static int check_basic_map_compatible_range_multi_aff(
12511 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12512 __isl_keep isl_multi_aff
*ma
)
12515 isl_space
*ma_space
;
12517 ma_space
= isl_multi_aff_get_space(ma
);
12519 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12523 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12524 "parameters don't match", goto error
);
12525 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12529 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12530 "spaces don't match", goto error
);
12532 isl_space_free(ma_space
);
12535 isl_space_free(ma_space
);
12539 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12540 * coefficients before the transformed range of dimensions,
12541 * the "n_after" coefficients after the transformed range of dimensions
12542 * and the coefficients of the other divs in "bmap".
12544 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12545 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12550 isl_local_space
*ls
;
12555 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12559 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12560 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12561 for (i
= 0; i
< n_div
; ++i
) {
12562 int o_bmap
= 0, o_ls
= 0;
12564 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12565 o_bmap
+= 1 + 1 + n_param
;
12566 o_ls
+= 1 + 1 + n_param
;
12567 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12568 o_bmap
+= n_before
;
12569 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12570 ls
->div
->row
[i
] + o_ls
, n_set
);
12573 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12575 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12576 ls
->div
->row
[i
] + o_ls
, n_div
);
12579 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12580 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12584 isl_local_space_free(ls
);
12587 isl_local_space_free(ls
);
12591 /* How many stride constraints does "ma" enforce?
12592 * That is, how many of the affine expressions have a denominator
12593 * different from one?
12595 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12600 for (i
= 0; i
< ma
->n
; ++i
)
12601 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12607 /* For each affine expression in ma of the form
12609 * x_i = (f_i y + h_i)/m_i
12611 * with m_i different from one, add a constraint to "bmap"
12614 * f_i y + h_i = m_i alpha_i
12616 * with alpha_i an additional existentially quantified variable.
12618 * The input variables of "ma" correspond to a subset of the variables
12619 * of "bmap". There are "n_before" variables in "bmap" before this
12620 * subset and "n_after" variables after this subset.
12621 * The integer divisions of the affine expressions in "ma" are assumed
12622 * to have been aligned. There are "n_div_ma" of them and
12623 * they appear first in "bmap", straight after the "n_after" variables.
12625 static __isl_give isl_basic_map
*add_ma_strides(
12626 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12627 int n_before
, int n_after
, int n_div_ma
)
12635 total
= isl_basic_map_total_dim(bmap
);
12636 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12637 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12638 for (i
= 0; i
< ma
->n
; ++i
) {
12639 int o_bmap
= 0, o_ma
= 1;
12641 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12643 div
= isl_basic_map_alloc_div(bmap
);
12644 k
= isl_basic_map_alloc_equality(bmap
);
12645 if (div
< 0 || k
< 0)
12647 isl_int_set_si(bmap
->div
[div
][0], 0);
12648 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12649 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12650 o_bmap
+= 1 + n_param
;
12651 o_ma
+= 1 + n_param
;
12652 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12653 o_bmap
+= n_before
;
12654 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12655 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12658 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12660 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12661 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12662 o_bmap
+= n_div_ma
;
12664 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12665 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12671 isl_basic_map_free(bmap
);
12675 /* Replace the domain or range space (depending on "type) of "space" by "set".
12677 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12678 enum isl_dim_type type
, __isl_take isl_space
*set
)
12680 if (type
== isl_dim_in
) {
12681 space
= isl_space_range(space
);
12682 space
= isl_space_map_from_domain_and_range(set
, space
);
12684 space
= isl_space_domain(space
);
12685 space
= isl_space_map_from_domain_and_range(space
, set
);
12691 /* Compute the preimage of the domain or range (depending on "type")
12692 * of "bmap" under the function represented by "ma".
12693 * In other words, plug in "ma" in the domain or range of "bmap".
12694 * The result is a basic map that lives in the same space as "bmap"
12695 * except that the domain or range has been replaced by
12696 * the domain space of "ma".
12698 * If bmap is represented by
12700 * A(p) + S u + B x + T v + C(divs) >= 0,
12702 * where u and x are input and output dimensions if type == isl_dim_out
12703 * while x and v are input and output dimensions if type == isl_dim_in,
12704 * and ma is represented by
12706 * x = D(p) + F(y) + G(divs')
12708 * then the result is
12710 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12712 * The divs in the input set are similarly adjusted.
12715 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12719 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12720 * B_i G(divs') + c_i(divs))/n_i)
12722 * If bmap is not a rational map and if F(y) involves any denominators
12724 * x_i = (f_i y + h_i)/m_i
12726 * then additional constraints are added to ensure that we only
12727 * map back integer points. That is we enforce
12729 * f_i y + h_i = m_i alpha_i
12731 * with alpha_i an additional existentially quantified variable.
12733 * We first copy over the divs from "ma".
12734 * Then we add the modified constraints and divs from "bmap".
12735 * Finally, we add the stride constraints, if needed.
12737 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12738 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12739 __isl_take isl_multi_aff
*ma
)
12743 isl_basic_map
*res
= NULL
;
12744 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12745 isl_int f
, c1
, c2
, g
;
12746 int rational
, strides
;
12753 ma
= isl_multi_aff_align_divs(ma
);
12756 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12759 if (type
== isl_dim_in
) {
12761 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12763 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12766 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12767 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12769 space
= isl_multi_aff_get_domain_space(ma
);
12770 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12771 rational
= isl_basic_map_is_rational(bmap
);
12772 strides
= rational
? 0 : multi_aff_strides(ma
);
12773 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12774 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12776 res
= isl_basic_map_set_rational(res
);
12778 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12779 if (isl_basic_map_alloc_div(res
) < 0)
12782 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12785 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12786 k
= isl_basic_map_alloc_equality(res
);
12789 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12790 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12793 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12794 k
= isl_basic_map_alloc_inequality(res
);
12797 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12798 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12801 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12802 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12803 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12806 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12807 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12812 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12818 isl_basic_map_free(bmap
);
12819 isl_multi_aff_free(ma
);
12820 res
= isl_basic_set_simplify(res
);
12821 return isl_basic_map_finalize(res
);
12827 isl_basic_map_free(bmap
);
12828 isl_multi_aff_free(ma
);
12829 isl_basic_map_free(res
);
12833 /* Compute the preimage of "bset" under the function represented by "ma".
12834 * In other words, plug in "ma" in "bset". The result is a basic set
12835 * that lives in the domain space of "ma".
12837 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12838 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12840 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12843 /* Compute the preimage of the domain of "bmap" under the function
12844 * represented by "ma".
12845 * In other words, plug in "ma" in the domain of "bmap".
12846 * The result is a basic map that lives in the same space as "bmap"
12847 * except that the domain has been replaced by the domain space of "ma".
12849 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12850 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12852 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12855 /* Compute the preimage of the range of "bmap" under the function
12856 * represented by "ma".
12857 * In other words, plug in "ma" in the range of "bmap".
12858 * The result is a basic map that lives in the same space as "bmap"
12859 * except that the range has been replaced by the domain space of "ma".
12861 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12862 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12864 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12867 /* Check if the range of "ma" is compatible with the domain or range
12868 * (depending on "type") of "map".
12869 * Return -1 if anything is wrong.
12871 static int check_map_compatible_range_multi_aff(
12872 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12873 __isl_keep isl_multi_aff
*ma
)
12876 isl_space
*ma_space
;
12878 ma_space
= isl_multi_aff_get_space(ma
);
12879 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12880 isl_space_free(ma_space
);
12882 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12883 "spaces don't match", return -1);
12887 /* Compute the preimage of the domain or range (depending on "type")
12888 * of "map" under the function represented by "ma".
12889 * In other words, plug in "ma" in the domain or range of "map".
12890 * The result is a map that lives in the same space as "map"
12891 * except that the domain or range has been replaced by
12892 * the domain space of "ma".
12894 * The parameters are assumed to have been aligned.
12896 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12897 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12902 map
= isl_map_cow(map
);
12903 ma
= isl_multi_aff_align_divs(ma
);
12906 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12909 for (i
= 0; i
< map
->n
; ++i
) {
12910 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12911 isl_multi_aff_copy(ma
));
12916 space
= isl_multi_aff_get_domain_space(ma
);
12917 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12919 isl_space_free(map
->dim
);
12924 isl_multi_aff_free(ma
);
12926 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12927 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12930 isl_multi_aff_free(ma
);
12935 /* Compute the preimage of the domain or range (depending on "type")
12936 * of "map" under the function represented by "ma".
12937 * In other words, plug in "ma" in the domain or range of "map".
12938 * The result is a map that lives in the same space as "map"
12939 * except that the domain or range has been replaced by
12940 * the domain space of "ma".
12942 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12943 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12948 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12949 return map_preimage_multi_aff(map
, type
, ma
);
12951 if (!isl_space_has_named_params(map
->dim
) ||
12952 !isl_space_has_named_params(ma
->space
))
12953 isl_die(map
->ctx
, isl_error_invalid
,
12954 "unaligned unnamed parameters", goto error
);
12955 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12956 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12958 return map_preimage_multi_aff(map
, type
, ma
);
12960 isl_multi_aff_free(ma
);
12961 return isl_map_free(map
);
12964 /* Compute the preimage of "set" under the function represented by "ma".
12965 * In other words, plug in "ma" in "set". The result is a set
12966 * that lives in the domain space of "ma".
12968 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12969 __isl_take isl_multi_aff
*ma
)
12971 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12974 /* Compute the preimage of the domain of "map" under the function
12975 * represented by "ma".
12976 * In other words, plug in "ma" in the domain of "map".
12977 * The result is a map that lives in the same space as "map"
12978 * except that the domain has been replaced by the domain space of "ma".
12980 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12981 __isl_take isl_multi_aff
*ma
)
12983 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12986 /* Compute the preimage of the range of "map" under the function
12987 * represented by "ma".
12988 * In other words, plug in "ma" in the range of "map".
12989 * The result is a map that lives in the same space as "map"
12990 * except that the range has been replaced by the domain space of "ma".
12992 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12993 __isl_take isl_multi_aff
*ma
)
12995 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12998 /* Compute the preimage of "map" under the function represented by "pma".
12999 * In other words, plug in "pma" in the domain or range of "map".
13000 * The result is a map that lives in the same space as "map",
13001 * except that the space of type "type" has been replaced by
13002 * the domain space of "pma".
13004 * The parameters of "map" and "pma" are assumed to have been aligned.
13006 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13007 __isl_take isl_map
*map
, enum isl_dim_type type
,
13008 __isl_take isl_pw_multi_aff
*pma
)
13017 isl_pw_multi_aff_free(pma
);
13018 res
= isl_map_empty(isl_map_get_space(map
));
13023 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13024 isl_multi_aff_copy(pma
->p
[0].maff
));
13025 if (type
== isl_dim_in
)
13026 res
= isl_map_intersect_domain(res
,
13027 isl_map_copy(pma
->p
[0].set
));
13029 res
= isl_map_intersect_range(res
,
13030 isl_map_copy(pma
->p
[0].set
));
13032 for (i
= 1; i
< pma
->n
; ++i
) {
13035 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13036 isl_multi_aff_copy(pma
->p
[i
].maff
));
13037 if (type
== isl_dim_in
)
13038 res_i
= isl_map_intersect_domain(res_i
,
13039 isl_map_copy(pma
->p
[i
].set
));
13041 res_i
= isl_map_intersect_range(res_i
,
13042 isl_map_copy(pma
->p
[i
].set
));
13043 res
= isl_map_union(res
, res_i
);
13046 isl_pw_multi_aff_free(pma
);
13050 isl_pw_multi_aff_free(pma
);
13055 /* Compute the preimage of "map" under the function represented by "pma".
13056 * In other words, plug in "pma" in the domain or range of "map".
13057 * The result is a map that lives in the same space as "map",
13058 * except that the space of type "type" has been replaced by
13059 * the domain space of "pma".
13061 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13062 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13067 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13068 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13070 if (!isl_space_has_named_params(map
->dim
) ||
13071 !isl_space_has_named_params(pma
->dim
))
13072 isl_die(map
->ctx
, isl_error_invalid
,
13073 "unaligned unnamed parameters", goto error
);
13074 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13075 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13077 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13079 isl_pw_multi_aff_free(pma
);
13080 return isl_map_free(map
);
13083 /* Compute the preimage of "set" under the function represented by "pma".
13084 * In other words, plug in "pma" in "set". The result is a set
13085 * that lives in the domain space of "pma".
13087 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13088 __isl_take isl_pw_multi_aff
*pma
)
13090 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13093 /* Compute the preimage of the domain of "map" under the function
13094 * represented by "pma".
13095 * In other words, plug in "pma" in the domain of "map".
13096 * The result is a map that lives in the same space as "map",
13097 * except that domain space has been replaced by the domain space of "pma".
13099 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13100 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13102 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13105 /* Compute the preimage of the range of "map" under the function
13106 * represented by "pma".
13107 * In other words, plug in "pma" in the range of "map".
13108 * The result is a map that lives in the same space as "map",
13109 * except that range space has been replaced by the domain space of "pma".
13111 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13112 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13114 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13117 /* Compute the preimage of "map" under the function represented by "mpa".
13118 * In other words, plug in "mpa" in the domain or range of "map".
13119 * The result is a map that lives in the same space as "map",
13120 * except that the space of type "type" has been replaced by
13121 * the domain space of "mpa".
13123 * If the map does not involve any constraints that refer to the
13124 * dimensions of the substituted space, then the only possible
13125 * effect of "mpa" on the map is to map the space to a different space.
13126 * We create a separate isl_multi_aff to effectuate this change
13127 * in order to avoid spurious splitting of the map along the pieces
13130 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13131 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13134 isl_pw_multi_aff
*pma
;
13139 n
= isl_map_dim(map
, type
);
13140 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13144 space
= isl_multi_pw_aff_get_space(mpa
);
13145 isl_multi_pw_aff_free(mpa
);
13146 ma
= isl_multi_aff_zero(space
);
13147 return isl_map_preimage_multi_aff(map
, type
, ma
);
13150 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13151 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13154 isl_multi_pw_aff_free(mpa
);
13158 /* Compute the preimage of "map" under the function represented by "mpa".
13159 * In other words, plug in "mpa" in the domain "map".
13160 * The result is a map that lives in the same space as "map",
13161 * except that domain space has been replaced by the domain space of "mpa".
13163 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13164 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13166 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13169 /* Compute the preimage of "set" by the function represented by "mpa".
13170 * In other words, plug in "mpa" in "set".
13172 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13173 __isl_take isl_multi_pw_aff
*mpa
)
13175 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13178 /* Is the point "inner" internal to inequality constraint "ineq"
13180 * The point is considered to be internal to the inequality constraint,
13181 * if it strictly lies on the positive side of the inequality constraint,
13182 * or if it lies on the constraint and the constraint is lexico-positive.
13184 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13185 __isl_keep isl_basic_set
*bset
, int ineq
)
13191 if (!inner
|| !bset
)
13192 return isl_bool_error
;
13194 ctx
= isl_basic_set_get_ctx(bset
);
13195 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13196 &ctx
->normalize_gcd
);
13197 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13198 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13200 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13201 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13202 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13205 /* Tighten the inequality constraints of "bset" that are outward with respect
13206 * to the point "vec".
13207 * That is, tighten the constraints that are not satisfied by "vec".
13209 * "vec" is a point internal to some superset S of "bset" that is used
13210 * to make the subsets of S disjoint, by tightening one half of the constraints
13211 * that separate two subsets. In particular, the constraints of S
13212 * are all satisfied by "vec" and should not be tightened.
13213 * Of the internal constraints, those that have "vec" on the outside
13214 * are tightened. The shared facet is included in the adjacent subset
13215 * with the opposite constraint.
13216 * For constraints that saturate "vec", this criterion cannot be used
13217 * to determine which of the two sides should be tightened.
13218 * Instead, the sign of the first non-zero coefficient is used
13219 * to make this choice. Note that this second criterion is never used
13220 * on the constraints of S since "vec" is interior to "S".
13222 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13223 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13227 bset
= isl_basic_set_cow(bset
);
13230 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13233 internal
= is_internal(vec
, bset
, j
);
13235 return isl_basic_set_free(bset
);
13238 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);