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
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
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 __isl_give isl_basic_map
*isl_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(bset
->dim
, dim
), goto error
);
4566 isl_space_free(bset
->dim
);
4567 bmap
= bset_to_bmap(bset
);
4569 return isl_basic_map_finalize(bmap
);
4571 isl_basic_set_free(bset
);
4572 isl_space_free(dim
);
4576 /* For a div d = floor(f/m), add the constraint
4580 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4581 unsigned pos
, isl_int
*div
)
4584 unsigned total
= isl_basic_map_total_dim(bmap
);
4586 i
= isl_basic_map_alloc_inequality(bmap
);
4589 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4590 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4595 /* For a div d = floor(f/m), add the constraint
4597 * -(f-(m-1)) + m d >= 0
4599 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4600 unsigned pos
, isl_int
*div
)
4603 unsigned total
= isl_basic_map_total_dim(bmap
);
4605 i
= isl_basic_map_alloc_inequality(bmap
);
4608 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4609 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4610 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4611 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4616 /* For a div d = floor(f/m), add the constraints
4619 * -(f-(m-1)) + m d >= 0
4621 * Note that the second constraint is the negation of
4625 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4626 unsigned pos
, isl_int
*div
)
4628 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4630 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4635 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4636 unsigned pos
, isl_int
*div
)
4638 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4642 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4644 unsigned total
= isl_basic_map_total_dim(bmap
);
4645 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4647 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4651 /* For each known div d = floor(f/m), add the constraints
4654 * -(f-(m-1)) + m d >= 0
4656 * Remove duplicate constraints in case of some these div constraints
4657 * already appear in "bmap".
4659 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4660 __isl_take isl_basic_map
*bmap
)
4666 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4670 bmap
= add_known_div_constraints(bmap
);
4671 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4672 bmap
= isl_basic_map_finalize(bmap
);
4676 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4678 * In particular, if this div is of the form d = floor(f/m),
4679 * then add the constraint
4683 * if sign < 0 or the constraint
4685 * -(f-(m-1)) + m d >= 0
4689 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4690 unsigned div
, int sign
)
4698 total
= isl_basic_map_total_dim(bmap
);
4699 div_pos
= total
- bmap
->n_div
+ div
;
4702 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4704 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4707 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4709 return isl_basic_map_add_div_constraints(bset
, div
);
4712 struct isl_basic_set
*isl_basic_map_underlying_set(
4713 struct isl_basic_map
*bmap
)
4717 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4719 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4720 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4721 return bset_from_bmap(bmap
);
4722 bmap
= isl_basic_map_cow(bmap
);
4725 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4728 bmap
->extra
-= bmap
->n_div
;
4730 bmap
= isl_basic_map_finalize(bmap
);
4731 return bset_from_bmap(bmap
);
4733 isl_basic_map_free(bmap
);
4737 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4738 __isl_take isl_basic_set
*bset
)
4740 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4743 /* Replace each element in "list" by the result of applying
4744 * isl_basic_map_underlying_set to the element.
4746 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4747 __isl_take isl_basic_map_list
*list
)
4754 n
= isl_basic_map_list_n_basic_map(list
);
4755 for (i
= 0; i
< n
; ++i
) {
4756 isl_basic_map
*bmap
;
4757 isl_basic_set
*bset
;
4759 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4760 bset
= isl_basic_set_underlying_set(bmap
);
4761 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4767 struct isl_basic_map
*isl_basic_map_overlying_set(
4768 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4770 struct isl_basic_map
*bmap
;
4771 struct isl_ctx
*ctx
;
4778 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4779 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4780 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4782 if (like
->n_div
== 0) {
4783 isl_space
*space
= isl_basic_map_get_space(like
);
4784 isl_basic_map_free(like
);
4785 return isl_basic_map_reset_space(bset
, space
);
4787 bset
= isl_basic_set_cow(bset
);
4790 total
= bset
->dim
->n_out
+ bset
->extra
;
4791 bmap
= bset_to_bmap(bset
);
4792 isl_space_free(bmap
->dim
);
4793 bmap
->dim
= isl_space_copy(like
->dim
);
4796 bmap
->n_div
= like
->n_div
;
4797 bmap
->extra
+= like
->n_div
;
4801 ltotal
= total
- bmap
->extra
+ like
->extra
;
4804 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4805 bmap
->extra
* (1 + 1 + total
));
4806 if (isl_blk_is_error(bmap
->block2
))
4808 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4812 for (i
= 0; i
< bmap
->extra
; ++i
)
4813 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4814 for (i
= 0; i
< like
->n_div
; ++i
) {
4815 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4816 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4818 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4820 isl_basic_map_free(like
);
4821 bmap
= isl_basic_map_simplify(bmap
);
4822 bmap
= isl_basic_map_finalize(bmap
);
4825 isl_basic_map_free(like
);
4826 isl_basic_set_free(bset
);
4830 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4831 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4833 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
4834 bset_to_bmap(like
)));
4837 struct isl_set
*isl_set_from_underlying_set(
4838 struct isl_set
*set
, struct isl_basic_set
*like
)
4844 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4846 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4847 isl_basic_set_free(like
);
4850 set
= isl_set_cow(set
);
4853 for (i
= 0; i
< set
->n
; ++i
) {
4854 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4855 isl_basic_set_copy(like
));
4859 isl_space_free(set
->dim
);
4860 set
->dim
= isl_space_copy(like
->dim
);
4863 isl_basic_set_free(like
);
4866 isl_basic_set_free(like
);
4871 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4875 map
= isl_map_cow(map
);
4878 map
->dim
= isl_space_cow(map
->dim
);
4882 for (i
= 1; i
< map
->n
; ++i
)
4883 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4885 for (i
= 0; i
< map
->n
; ++i
) {
4886 map
->p
[i
] = bset_to_bmap(
4887 isl_basic_map_underlying_set(map
->p
[i
]));
4892 map
->dim
= isl_space_underlying(map
->dim
, 0);
4894 isl_space_free(map
->dim
);
4895 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4899 return set_from_map(map
);
4905 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4907 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
4910 /* Replace the space of "bmap" by "space".
4912 * If the space of "bmap" is identical to "space" (including the identifiers
4913 * of the input and output dimensions), then simply return the original input.
4915 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4916 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4922 equal
= isl_space_is_equal(bmap
->dim
, space
);
4923 if (equal
>= 0 && equal
)
4924 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4926 if (equal
>= 0 && equal
)
4927 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4928 space
, isl_dim_out
);
4932 isl_space_free(space
);
4935 bmap
= isl_basic_map_cow(bmap
);
4936 if (!bmap
|| !space
)
4939 isl_space_free(bmap
->dim
);
4942 bmap
= isl_basic_map_finalize(bmap
);
4946 isl_basic_map_free(bmap
);
4947 isl_space_free(space
);
4951 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4952 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4954 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
4958 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4959 __isl_take isl_space
*dim
)
4963 map
= isl_map_cow(map
);
4967 for (i
= 0; i
< map
->n
; ++i
) {
4968 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4969 isl_space_copy(dim
));
4973 isl_space_free(map
->dim
);
4979 isl_space_free(dim
);
4983 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4984 __isl_take isl_space
*dim
)
4986 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
4989 /* Compute the parameter domain of the given basic set.
4991 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4996 if (isl_basic_set_is_params(bset
))
4999 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5000 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5001 space
= isl_basic_set_get_space(bset
);
5002 space
= isl_space_params(space
);
5003 bset
= isl_basic_set_reset_space(bset
, space
);
5007 /* Construct a zero-dimensional basic set with the given parameter domain.
5009 __isl_give isl_basic_set
*isl_basic_set_from_params(
5010 __isl_take isl_basic_set
*bset
)
5013 space
= isl_basic_set_get_space(bset
);
5014 space
= isl_space_set_from_params(space
);
5015 bset
= isl_basic_set_reset_space(bset
, space
);
5019 /* Compute the parameter domain of the given set.
5021 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5026 if (isl_set_is_params(set
))
5029 n
= isl_set_dim(set
, isl_dim_set
);
5030 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5031 space
= isl_set_get_space(set
);
5032 space
= isl_space_params(space
);
5033 set
= isl_set_reset_space(set
, space
);
5037 /* Construct a zero-dimensional set with the given parameter domain.
5039 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5042 space
= isl_set_get_space(set
);
5043 space
= isl_space_set_from_params(space
);
5044 set
= isl_set_reset_space(set
, space
);
5048 /* Compute the parameter domain of the given map.
5050 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5055 n
= isl_map_dim(map
, isl_dim_in
);
5056 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5057 n
= isl_map_dim(map
, isl_dim_out
);
5058 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5059 space
= isl_map_get_space(map
);
5060 space
= isl_space_params(space
);
5061 map
= isl_map_reset_space(map
, space
);
5065 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5072 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5074 n_out
= isl_basic_map_n_out(bmap
);
5075 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5077 return isl_basic_map_reset_space(bmap
, space
);
5080 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5084 return isl_space_may_be_set(bmap
->dim
);
5087 /* Is this basic map actually a set?
5088 * Users should never call this function. Outside of isl,
5089 * the type should indicate whether something is a set or a map.
5091 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5095 return isl_space_is_set(bmap
->dim
);
5098 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5102 if (isl_basic_map_is_set(bmap
))
5104 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5107 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5108 __isl_take isl_basic_map
*bmap
)
5112 isl_basic_map
*domain
;
5113 int nparam
, n_in
, n_out
;
5116 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5117 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5118 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5120 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5121 domain
= isl_basic_map_universe(dim
);
5123 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5124 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5125 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5127 total
= isl_basic_map_total_dim(bmap
);
5129 for (i
= 0; i
< n_in
; ++i
) {
5130 k
= isl_basic_map_alloc_equality(bmap
);
5133 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5134 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5135 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5138 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5139 return isl_basic_map_finalize(bmap
);
5141 isl_basic_map_free(bmap
);
5145 __isl_give isl_basic_map
*isl_basic_map_range_map(
5146 __isl_take isl_basic_map
*bmap
)
5150 isl_basic_map
*range
;
5151 int nparam
, n_in
, n_out
;
5154 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5155 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5156 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5158 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5159 range
= isl_basic_map_universe(dim
);
5161 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5162 bmap
= isl_basic_map_apply_range(bmap
, range
);
5163 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5165 total
= isl_basic_map_total_dim(bmap
);
5167 for (i
= 0; i
< n_out
; ++i
) {
5168 k
= isl_basic_map_alloc_equality(bmap
);
5171 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5172 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5173 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5176 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5177 return isl_basic_map_finalize(bmap
);
5179 isl_basic_map_free(bmap
);
5183 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5187 return isl_space_may_be_set(map
->dim
);
5190 /* Is this map actually a set?
5191 * Users should never call this function. Outside of isl,
5192 * the type should indicate whether something is a set or a map.
5194 int isl_map_is_set(__isl_keep isl_map
*map
)
5198 return isl_space_is_set(map
->dim
);
5201 struct isl_set
*isl_map_range(struct isl_map
*map
)
5204 struct isl_set
*set
;
5208 if (isl_map_is_set(map
))
5209 return set_from_map(map
);
5211 map
= isl_map_cow(map
);
5215 set
= set_from_map(map
);
5216 set
->dim
= isl_space_range(set
->dim
);
5219 for (i
= 0; i
< map
->n
; ++i
) {
5220 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5224 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5225 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5232 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5236 map
= isl_map_cow(map
);
5240 map
->dim
= isl_space_domain_map(map
->dim
);
5243 for (i
= 0; i
< map
->n
; ++i
) {
5244 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5248 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5249 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5256 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5259 isl_space
*range_dim
;
5261 map
= isl_map_cow(map
);
5265 range_dim
= isl_space_range(isl_map_get_space(map
));
5266 range_dim
= isl_space_from_range(range_dim
);
5267 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5268 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5271 for (i
= 0; i
< map
->n
; ++i
) {
5272 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5276 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5277 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5284 /* Given a wrapped map of the form A[B -> C],
5285 * return the map A[B -> C] -> B.
5287 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5294 if (!isl_set_has_tuple_id(set
))
5295 return isl_map_domain_map(isl_set_unwrap(set
));
5297 id
= isl_set_get_tuple_id(set
);
5298 map
= isl_map_domain_map(isl_set_unwrap(set
));
5299 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5304 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5305 __isl_take isl_space
*dim
)
5308 struct isl_map
*map
= NULL
;
5310 set
= isl_set_cow(set
);
5313 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5314 map
= set_to_map(set
);
5315 for (i
= 0; i
< set
->n
; ++i
) {
5316 map
->p
[i
] = isl_basic_map_from_basic_set(
5317 set
->p
[i
], isl_space_copy(dim
));
5321 isl_space_free(map
->dim
);
5325 isl_space_free(dim
);
5330 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5331 __isl_take isl_basic_set
*bset
)
5333 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5336 __isl_give isl_basic_map
*isl_basic_map_from_range(
5337 __isl_take isl_basic_set
*bset
)
5340 space
= isl_basic_set_get_space(bset
);
5341 space
= isl_space_from_range(space
);
5342 bset
= isl_basic_set_reset_space(bset
, space
);
5343 return bset_to_bmap(bset
);
5346 /* Create a relation with the given set as range.
5347 * The domain of the created relation is a zero-dimensional
5348 * flat anonymous space.
5350 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5353 space
= isl_set_get_space(set
);
5354 space
= isl_space_from_range(space
);
5355 set
= isl_set_reset_space(set
, space
);
5356 return set_to_map(set
);
5359 /* Create a relation with the given set as domain.
5360 * The range of the created relation is a zero-dimensional
5361 * flat anonymous space.
5363 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5365 return isl_map_reverse(isl_map_from_range(set
));
5368 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5369 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5371 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5374 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5375 __isl_take isl_set
*range
)
5377 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5380 /* Return a newly allocated isl_map with given space and flags and
5381 * room for "n" basic maps.
5382 * Make sure that all cached information is cleared.
5384 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5387 struct isl_map
*map
;
5392 isl_die(space
->ctx
, isl_error_internal
,
5393 "negative number of basic maps", goto error
);
5394 map
= isl_calloc(space
->ctx
, struct isl_map
,
5395 sizeof(struct isl_map
) +
5396 (n
- 1) * sizeof(struct isl_basic_map
*));
5400 map
->ctx
= space
->ctx
;
5401 isl_ctx_ref(map
->ctx
);
5409 isl_space_free(space
);
5413 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5414 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5417 struct isl_map
*map
;
5420 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5424 map
= isl_map_alloc_space(dims
, n
, flags
);
5428 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5430 struct isl_basic_map
*bmap
;
5431 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5432 bmap
= isl_basic_map_set_to_empty(bmap
);
5436 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5438 struct isl_basic_set
*bset
;
5439 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5440 bset
= isl_basic_set_set_to_empty(bset
);
5444 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5446 struct isl_basic_map
*bmap
;
5447 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5448 bmap
= isl_basic_map_finalize(bmap
);
5452 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5454 struct isl_basic_set
*bset
;
5455 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5456 bset
= isl_basic_set_finalize(bset
);
5460 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5463 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5464 isl_basic_map
*bmap
;
5466 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5467 for (i
= 0; i
< total
; ++i
) {
5468 int k
= isl_basic_map_alloc_inequality(bmap
);
5471 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5472 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5476 isl_basic_map_free(bmap
);
5480 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5482 return isl_basic_map_nat_universe(dim
);
5485 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5487 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5490 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5492 return isl_map_nat_universe(dim
);
5495 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5497 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5500 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5502 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5505 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5507 struct isl_map
*map
;
5510 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5511 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5515 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5517 struct isl_set
*set
;
5520 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5521 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5525 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5528 struct isl_map
*dup
;
5532 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5533 for (i
= 0; i
< map
->n
; ++i
)
5534 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5538 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5539 __isl_take isl_basic_map
*bmap
)
5543 if (isl_basic_map_plain_is_empty(bmap
)) {
5544 isl_basic_map_free(bmap
);
5547 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5548 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5549 map
->p
[map
->n
] = bmap
;
5551 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5557 isl_basic_map_free(bmap
);
5561 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5572 isl_ctx_deref(map
->ctx
);
5573 for (i
= 0; i
< map
->n
; ++i
)
5574 isl_basic_map_free(map
->p
[i
]);
5575 isl_space_free(map
->dim
);
5581 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5582 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5586 bmap
= isl_basic_map_cow(bmap
);
5587 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5588 j
= isl_basic_map_alloc_equality(bmap
);
5591 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5592 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5593 isl_int_set_si(bmap
->eq
[j
][0], value
);
5594 bmap
= isl_basic_map_simplify(bmap
);
5595 return isl_basic_map_finalize(bmap
);
5597 isl_basic_map_free(bmap
);
5601 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5602 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5606 bmap
= isl_basic_map_cow(bmap
);
5607 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5608 j
= isl_basic_map_alloc_equality(bmap
);
5611 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5612 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5613 isl_int_set(bmap
->eq
[j
][0], value
);
5614 bmap
= isl_basic_map_simplify(bmap
);
5615 return isl_basic_map_finalize(bmap
);
5617 isl_basic_map_free(bmap
);
5621 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5622 enum isl_dim_type type
, unsigned pos
, int value
)
5626 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5627 return isl_basic_map_fix_pos_si(bmap
,
5628 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5630 isl_basic_map_free(bmap
);
5634 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5635 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5639 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5640 return isl_basic_map_fix_pos(bmap
,
5641 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5643 isl_basic_map_free(bmap
);
5647 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5648 * to be equal to "v".
5650 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5651 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5655 if (!isl_val_is_int(v
))
5656 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5657 "expecting integer value", goto error
);
5658 if (pos
>= isl_basic_map_dim(bmap
, type
))
5659 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5660 "index out of bounds", goto error
);
5661 pos
+= isl_basic_map_offset(bmap
, type
);
5662 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5666 isl_basic_map_free(bmap
);
5671 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5672 * to be equal to "v".
5674 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5675 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5677 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5680 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5681 enum isl_dim_type type
, unsigned pos
, int value
)
5683 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5687 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5688 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5690 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5694 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5695 unsigned input
, int value
)
5697 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5700 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5701 unsigned dim
, int value
)
5703 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5704 isl_dim_set
, dim
, value
));
5707 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5709 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5716 isl_basic_map_free(map
->p
[i
]);
5717 if (i
!= map
->n
- 1) {
5718 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5719 map
->p
[i
] = map
->p
[map
->n
- 1];
5726 /* Perform "fn" on each basic map of "map", where we may not be holding
5727 * the only reference to "map".
5728 * In particular, "fn" should be a semantics preserving operation
5729 * that we want to apply to all copies of "map". We therefore need
5730 * to be careful not to modify "map" in a way that breaks "map"
5731 * in case anything goes wrong.
5733 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5734 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5736 struct isl_basic_map
*bmap
;
5742 for (i
= map
->n
- 1; i
>= 0; --i
) {
5743 bmap
= isl_basic_map_copy(map
->p
[i
]);
5747 isl_basic_map_free(map
->p
[i
]);
5749 if (remove_if_empty(map
, i
) < 0)
5759 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5760 enum isl_dim_type type
, unsigned pos
, int value
)
5764 map
= isl_map_cow(map
);
5768 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5769 for (i
= map
->n
- 1; i
>= 0; --i
) {
5770 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5771 if (remove_if_empty(map
, i
) < 0)
5774 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5781 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5782 enum isl_dim_type type
, unsigned pos
, int value
)
5784 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5787 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5788 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5792 map
= isl_map_cow(map
);
5796 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5797 for (i
= 0; i
< map
->n
; ++i
) {
5798 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5802 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5809 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5810 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5812 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5815 /* Fix the value of the variable at position "pos" of type "type" of "map"
5816 * to be equal to "v".
5818 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5819 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5823 map
= isl_map_cow(map
);
5827 if (!isl_val_is_int(v
))
5828 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5829 "expecting integer value", goto error
);
5830 if (pos
>= isl_map_dim(map
, type
))
5831 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5832 "index out of bounds", goto error
);
5833 for (i
= map
->n
- 1; i
>= 0; --i
) {
5834 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5836 if (remove_if_empty(map
, i
) < 0)
5839 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5848 /* Fix the value of the variable at position "pos" of type "type" of "set"
5849 * to be equal to "v".
5851 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5852 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5854 return isl_map_fix_val(set
, type
, pos
, v
);
5857 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5858 unsigned input
, int value
)
5860 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5863 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5865 return set_from_map(isl_map_fix_si(set_to_map(set
),
5866 isl_dim_set
, dim
, value
));
5869 static __isl_give isl_basic_map
*basic_map_bound_si(
5870 __isl_take isl_basic_map
*bmap
,
5871 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5877 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5878 pos
+= isl_basic_map_offset(bmap
, type
);
5879 bmap
= isl_basic_map_cow(bmap
);
5880 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5881 j
= isl_basic_map_alloc_inequality(bmap
);
5884 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5886 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5887 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5889 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5890 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5892 bmap
= isl_basic_map_simplify(bmap
);
5893 return isl_basic_map_finalize(bmap
);
5895 isl_basic_map_free(bmap
);
5899 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5900 __isl_take isl_basic_map
*bmap
,
5901 enum isl_dim_type type
, unsigned pos
, int value
)
5903 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5906 /* Constrain the values of the given dimension to be no greater than "value".
5908 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5909 __isl_take isl_basic_map
*bmap
,
5910 enum isl_dim_type type
, unsigned pos
, int value
)
5912 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5915 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5916 unsigned dim
, isl_int value
)
5920 bset
= isl_basic_set_cow(bset
);
5921 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5922 j
= isl_basic_set_alloc_inequality(bset
);
5925 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5926 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5927 isl_int_neg(bset
->ineq
[j
][0], value
);
5928 bset
= isl_basic_set_simplify(bset
);
5929 return isl_basic_set_finalize(bset
);
5931 isl_basic_set_free(bset
);
5935 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5936 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5940 map
= isl_map_cow(map
);
5944 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5945 for (i
= 0; i
< map
->n
; ++i
) {
5946 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5947 type
, pos
, value
, upper
);
5951 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5958 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5959 enum isl_dim_type type
, unsigned pos
, int value
)
5961 return map_bound_si(map
, type
, pos
, value
, 0);
5964 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5965 enum isl_dim_type type
, unsigned pos
, int value
)
5967 return map_bound_si(map
, type
, pos
, value
, 1);
5970 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5971 enum isl_dim_type type
, unsigned pos
, int value
)
5973 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
5977 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5978 enum isl_dim_type type
, unsigned pos
, int value
)
5980 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5983 /* Bound the given variable of "bmap" from below (or above is "upper"
5984 * is set) to "value".
5986 static __isl_give isl_basic_map
*basic_map_bound(
5987 __isl_take isl_basic_map
*bmap
,
5988 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5994 if (pos
>= isl_basic_map_dim(bmap
, type
))
5995 isl_die(bmap
->ctx
, isl_error_invalid
,
5996 "index out of bounds", goto error
);
5997 pos
+= isl_basic_map_offset(bmap
, type
);
5998 bmap
= isl_basic_map_cow(bmap
);
5999 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6000 j
= isl_basic_map_alloc_inequality(bmap
);
6003 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6005 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6006 isl_int_set(bmap
->ineq
[j
][0], value
);
6008 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6009 isl_int_neg(bmap
->ineq
[j
][0], value
);
6011 bmap
= isl_basic_map_simplify(bmap
);
6012 return isl_basic_map_finalize(bmap
);
6014 isl_basic_map_free(bmap
);
6018 /* Bound the given variable of "map" from below (or above is "upper"
6019 * is set) to "value".
6021 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6022 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6026 map
= isl_map_cow(map
);
6030 if (pos
>= isl_map_dim(map
, type
))
6031 isl_die(map
->ctx
, isl_error_invalid
,
6032 "index out of bounds", goto error
);
6033 for (i
= map
->n
- 1; i
>= 0; --i
) {
6034 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6035 if (remove_if_empty(map
, i
) < 0)
6038 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6045 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6046 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6048 return map_bound(map
, type
, pos
, value
, 0);
6051 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6052 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6054 return map_bound(map
, type
, pos
, value
, 1);
6057 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6058 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6060 return isl_map_lower_bound(set
, type
, pos
, value
);
6063 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6064 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6066 return isl_map_upper_bound(set
, type
, pos
, value
);
6069 /* Force the values of the variable at position "pos" of type "type" of "set"
6070 * to be no smaller than "value".
6072 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6073 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6077 if (!isl_val_is_int(value
))
6078 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6079 "expecting integer value", goto error
);
6080 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6081 isl_val_free(value
);
6084 isl_val_free(value
);
6089 /* Force the values of the variable at position "pos" of type "type" of "set"
6090 * to be no greater than "value".
6092 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6093 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6097 if (!isl_val_is_int(value
))
6098 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6099 "expecting integer value", goto error
);
6100 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6101 isl_val_free(value
);
6104 isl_val_free(value
);
6109 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6114 set
= isl_set_cow(set
);
6118 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6119 for (i
= 0; i
< set
->n
; ++i
) {
6120 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6130 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6134 map
= isl_map_cow(map
);
6138 map
->dim
= isl_space_reverse(map
->dim
);
6141 for (i
= 0; i
< map
->n
; ++i
) {
6142 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6146 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6154 #define TYPE isl_pw_multi_aff
6156 #define SUFFIX _pw_multi_aff
6158 #define EMPTY isl_pw_multi_aff_empty
6160 #define ADD isl_pw_multi_aff_union_add
6161 #include "isl_map_lexopt_templ.c"
6163 /* Given a map "map", compute the lexicographically minimal
6164 * (or maximal) image element for each domain element in dom,
6165 * in the form of an isl_pw_multi_aff.
6166 * If "empty" is not NULL, then set *empty to those elements in dom that
6167 * do not have an image element.
6168 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6169 * should be computed over the domain of "map". "empty" is also NULL
6172 * We first compute the lexicographically minimal or maximal element
6173 * in the first basic map. This results in a partial solution "res"
6174 * and a subset "todo" of dom that still need to be handled.
6175 * We then consider each of the remaining maps in "map" and successively
6176 * update both "res" and "todo".
6177 * If "empty" is NULL, then the todo sets are not needed and therefore
6178 * also not computed.
6180 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6181 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6182 __isl_give isl_set
**empty
, unsigned flags
)
6186 isl_pw_multi_aff
*res
;
6189 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6190 if (!map
|| (!full
&& !dom
))
6193 if (isl_map_plain_is_empty(map
)) {
6198 return isl_pw_multi_aff_from_map(map
);
6201 res
= basic_map_partial_lexopt_pw_multi_aff(
6202 isl_basic_map_copy(map
->p
[0]),
6203 isl_set_copy(dom
), empty
, flags
);
6207 for (i
= 1; i
< map
->n
; ++i
) {
6208 isl_pw_multi_aff
*res_i
;
6210 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6211 isl_basic_map_copy(map
->p
[i
]),
6212 isl_set_copy(dom
), empty
, flags
);
6214 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6215 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6217 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6220 todo
= isl_set_intersect(todo
, *empty
);
6239 #define TYPE isl_map
6243 #define EMPTY isl_map_empty
6245 #define ADD isl_map_union_disjoint
6246 #include "isl_map_lexopt_templ.c"
6248 /* Given a map "map", compute the lexicographically minimal
6249 * (or maximal) image element for each domain element in "dom",
6250 * in the form of an isl_map.
6251 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6252 * do not have an image element.
6253 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6254 * should be computed over the domain of "map". "empty" is also NULL
6257 * If the input consists of more than one disjunct, then first
6258 * compute the desired result in the form of an isl_pw_multi_aff and
6259 * then convert that into an isl_map.
6261 * This function used to have an explicit implementation in terms
6262 * of isl_maps, but it would continually intersect the domains of
6263 * partial results with the complement of the domain of the next
6264 * partial solution, potentially leading to an explosion in the number
6265 * of disjuncts if there are several disjuncts in the input.
6266 * An even earlier implementation of this function would look for
6267 * better results in the domain of the partial result and for extra
6268 * results in the complement of this domain, which would lead to
6269 * even more splintering.
6271 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6272 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6273 __isl_give isl_set
**empty
, unsigned flags
)
6276 struct isl_map
*res
;
6277 isl_pw_multi_aff
*pma
;
6279 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6280 if (!map
|| (!full
&& !dom
))
6283 if (isl_map_plain_is_empty(map
)) {
6292 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6298 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6300 return isl_map_from_pw_multi_aff(pma
);
6309 __isl_give isl_map
*isl_map_partial_lexmax(
6310 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6311 __isl_give isl_set
**empty
)
6313 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6316 __isl_give isl_map
*isl_map_partial_lexmin(
6317 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6318 __isl_give isl_set
**empty
)
6320 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6323 __isl_give isl_set
*isl_set_partial_lexmin(
6324 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6325 __isl_give isl_set
**empty
)
6327 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6331 __isl_give isl_set
*isl_set_partial_lexmax(
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_lexmax(set_to_map(set
),
6339 /* Compute the lexicographic minimum (or maximum if "flags" includes
6340 * ISL_OPT_MAX) of "bset" over its parametric domain.
6342 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6345 return isl_basic_map_lexopt(bset
, flags
);
6348 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6350 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6353 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6355 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6358 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6360 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6363 /* Compute the lexicographic minimum of "bset" over its parametric domain
6364 * for the purpose of quantifier elimination.
6365 * That is, find an explicit representation for all the existentially
6366 * quantified variables in "bset" by computing their lexicographic
6369 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6370 __isl_take isl_basic_set
*bset
)
6372 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6375 /* Extract the first and only affine expression from list
6376 * and then add it to *pwaff with the given dom.
6377 * This domain is known to be disjoint from other domains
6378 * because of the way isl_basic_map_foreach_lexmax works.
6380 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6381 __isl_take isl_aff_list
*list
, void *user
)
6383 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6385 isl_pw_aff
**pwaff
= user
;
6386 isl_pw_aff
*pwaff_i
;
6390 if (isl_aff_list_n_aff(list
) != 1)
6391 isl_die(ctx
, isl_error_internal
,
6392 "expecting single element list", goto error
);
6394 aff
= isl_aff_list_get_aff(list
, 0);
6395 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6397 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6399 isl_aff_list_free(list
);
6403 isl_basic_set_free(dom
);
6404 isl_aff_list_free(list
);
6408 /* Given a basic map with one output dimension, compute the minimum or
6409 * maximum of that dimension as an isl_pw_aff.
6411 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6412 * call update_dim_opt on each leaf of the result.
6414 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6417 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6421 dim
= isl_space_from_domain(isl_space_domain(dim
));
6422 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6423 pwaff
= isl_pw_aff_empty(dim
);
6425 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6427 return isl_pw_aff_free(pwaff
);
6432 /* Compute the minimum or maximum of the given output dimension
6433 * as a function of the parameters and the input dimensions,
6434 * but independently of the other output dimensions.
6436 * We first project out the other output dimension and then compute
6437 * the "lexicographic" maximum in each basic map, combining the results
6438 * using isl_pw_aff_union_max.
6440 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6447 n_out
= isl_map_dim(map
, isl_dim_out
);
6448 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6449 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6454 isl_space
*dim
= isl_map_get_space(map
);
6456 return isl_pw_aff_empty(dim
);
6459 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6460 for (i
= 1; i
< map
->n
; ++i
) {
6461 isl_pw_aff
*pwaff_i
;
6463 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6464 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6472 /* Compute the minimum of the given output dimension as a function of the
6473 * parameters and input dimensions, but independently of
6474 * the other output dimensions.
6476 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6478 return map_dim_opt(map
, pos
, 0);
6481 /* Compute the maximum of the given output dimension as a function of the
6482 * parameters and input dimensions, but independently of
6483 * the other output dimensions.
6485 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6487 return map_dim_opt(map
, pos
, 1);
6490 /* Compute the minimum or maximum of the given set dimension
6491 * as a function of the parameters,
6492 * but independently of the other set dimensions.
6494 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6497 return map_dim_opt(set
, pos
, max
);
6500 /* Compute the maximum of the given set dimension as a function of the
6501 * parameters, but independently of the other set dimensions.
6503 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6505 return set_dim_opt(set
, pos
, 1);
6508 /* Compute the minimum 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_min(__isl_take isl_set
*set
, int pos
)
6513 return set_dim_opt(set
, pos
, 0);
6516 /* Apply a preimage specified by "mat" on the parameters of "bset".
6517 * bset is assumed to have only parameters and divs.
6519 static struct isl_basic_set
*basic_set_parameter_preimage(
6520 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6527 bset
->dim
= isl_space_cow(bset
->dim
);
6531 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6533 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6535 bset
->dim
->nparam
= 0;
6536 bset
->dim
->n_out
= nparam
;
6537 bset
= isl_basic_set_preimage(bset
, mat
);
6539 bset
->dim
->nparam
= bset
->dim
->n_out
;
6540 bset
->dim
->n_out
= 0;
6545 isl_basic_set_free(bset
);
6549 /* Apply a preimage specified by "mat" on the parameters of "set".
6550 * set is assumed to have only parameters and divs.
6552 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6553 __isl_take isl_mat
*mat
)
6561 nparam
= isl_set_dim(set
, isl_dim_param
);
6563 if (mat
->n_row
!= 1 + nparam
)
6564 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6565 "unexpected number of rows", goto error
);
6567 space
= isl_set_get_space(set
);
6568 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6569 isl_dim_param
, 0, nparam
);
6570 set
= isl_set_reset_space(set
, space
);
6571 set
= isl_set_preimage(set
, mat
);
6572 nparam
= isl_set_dim(set
, isl_dim_out
);
6573 space
= isl_set_get_space(set
);
6574 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6575 isl_dim_out
, 0, nparam
);
6576 set
= isl_set_reset_space(set
, space
);
6584 /* Intersect the basic set "bset" with the affine space specified by the
6585 * equalities in "eq".
6587 static struct isl_basic_set
*basic_set_append_equalities(
6588 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6596 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6601 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6602 for (i
= 0; i
< eq
->n_row
; ++i
) {
6603 k
= isl_basic_set_alloc_equality(bset
);
6606 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6607 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6611 bset
= isl_basic_set_gauss(bset
, NULL
);
6612 bset
= isl_basic_set_finalize(bset
);
6617 isl_basic_set_free(bset
);
6621 /* Intersect the set "set" with the affine space specified by the
6622 * equalities in "eq".
6624 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6632 for (i
= 0; i
< set
->n
; ++i
) {
6633 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6646 /* Given a basic set "bset" that only involves parameters and existentially
6647 * quantified variables, return the index of the first equality
6648 * that only involves parameters. If there is no such equality then
6649 * return bset->n_eq.
6651 * This function assumes that isl_basic_set_gauss has been called on "bset".
6653 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6656 unsigned nparam
, n_div
;
6661 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6662 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6664 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6665 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6672 /* Compute an explicit representation for the existentially quantified
6673 * variables in "bset" by computing the "minimal value" of the set
6674 * variables. Since there are no set variables, the computation of
6675 * the minimal value essentially computes an explicit representation
6676 * of the non-empty part(s) of "bset".
6678 * The input only involves parameters and existentially quantified variables.
6679 * All equalities among parameters have been removed.
6681 * Since the existentially quantified variables in the result are in general
6682 * going to be different from those in the input, we first replace
6683 * them by the minimal number of variables based on their equalities.
6684 * This should simplify the parametric integer programming.
6686 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6688 isl_morph
*morph1
, *morph2
;
6694 if (bset
->n_eq
== 0)
6695 return isl_basic_set_lexmin_compute_divs(bset
);
6697 morph1
= isl_basic_set_parameter_compression(bset
);
6698 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6699 bset
= isl_basic_set_lift(bset
);
6700 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6701 bset
= isl_morph_basic_set(morph2
, bset
);
6702 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6703 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6705 set
= isl_basic_set_lexmin_compute_divs(bset
);
6707 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6712 /* Project the given basic set onto its parameter domain, possibly introducing
6713 * new, explicit, existential variables in the constraints.
6714 * The input has parameters and (possibly implicit) existential variables.
6715 * The output has the same parameters, but only
6716 * explicit existentially quantified variables.
6718 * The actual projection is performed by pip, but pip doesn't seem
6719 * to like equalities very much, so we first remove the equalities
6720 * among the parameters by performing a variable compression on
6721 * the parameters. Afterward, an inverse transformation is performed
6722 * and the equalities among the parameters are inserted back in.
6724 * The variable compression on the parameters may uncover additional
6725 * equalities that were only implicit before. We therefore check
6726 * if there are any new parameter equalities in the result and
6727 * if so recurse. The removal of parameter equalities is required
6728 * for the parameter compression performed by base_compute_divs.
6730 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6734 struct isl_mat
*T
, *T2
;
6735 struct isl_set
*set
;
6738 bset
= isl_basic_set_cow(bset
);
6742 if (bset
->n_eq
== 0)
6743 return base_compute_divs(bset
);
6745 bset
= isl_basic_set_gauss(bset
, NULL
);
6748 if (isl_basic_set_plain_is_empty(bset
))
6749 return isl_set_from_basic_set(bset
);
6751 i
= first_parameter_equality(bset
);
6752 if (i
== bset
->n_eq
)
6753 return base_compute_divs(bset
);
6755 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6756 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6758 eq
= isl_mat_cow(eq
);
6759 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6760 if (T
&& T
->n_col
== 0) {
6764 bset
= isl_basic_set_set_to_empty(bset
);
6765 return isl_set_from_basic_set(bset
);
6767 bset
= basic_set_parameter_preimage(bset
, T
);
6769 i
= first_parameter_equality(bset
);
6772 else if (i
== bset
->n_eq
)
6773 set
= base_compute_divs(bset
);
6775 set
= parameter_compute_divs(bset
);
6776 set
= set_parameter_preimage(set
, T2
);
6777 set
= set_append_equalities(set
, eq
);
6781 /* Insert the divs from "ls" before those of "bmap".
6783 * The number of columns is not changed, which means that the last
6784 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6785 * The caller is responsible for removing the same number of dimensions
6786 * from the space of "bmap".
6788 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6789 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6795 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6799 old_n_div
= bmap
->n_div
;
6800 bmap
= insert_div_rows(bmap
, n_div
);
6804 for (i
= 0; i
< n_div
; ++i
) {
6805 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6806 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6812 /* Replace the space of "bmap" by the space and divs of "ls".
6814 * If "ls" has any divs, then we simplify the result since we may
6815 * have discovered some additional equalities that could simplify
6816 * the div expressions.
6818 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6819 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6823 bmap
= isl_basic_map_cow(bmap
);
6827 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6828 bmap
= insert_divs_from_local_space(bmap
, ls
);
6832 isl_space_free(bmap
->dim
);
6833 bmap
->dim
= isl_local_space_get_space(ls
);
6837 isl_local_space_free(ls
);
6839 bmap
= isl_basic_map_simplify(bmap
);
6840 bmap
= isl_basic_map_finalize(bmap
);
6843 isl_basic_map_free(bmap
);
6844 isl_local_space_free(ls
);
6848 /* Replace the space of "map" by the space and divs of "ls".
6850 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6851 __isl_take isl_local_space
*ls
)
6855 map
= isl_map_cow(map
);
6859 for (i
= 0; i
< map
->n
; ++i
) {
6860 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6861 isl_local_space_copy(ls
));
6865 isl_space_free(map
->dim
);
6866 map
->dim
= isl_local_space_get_space(ls
);
6870 isl_local_space_free(ls
);
6873 isl_local_space_free(ls
);
6878 /* Compute an explicit representation for the existentially
6879 * quantified variables for which do not know any explicit representation yet.
6881 * We first sort the existentially quantified variables so that the
6882 * existentially quantified variables for which we already have an explicit
6883 * representation are placed before those for which we do not.
6884 * The input dimensions, the output dimensions and the existentially
6885 * quantified variables for which we already have an explicit
6886 * representation are then turned into parameters.
6887 * compute_divs returns a map with the same parameters and
6888 * no input or output dimensions and the dimension specification
6889 * is reset to that of the input, including the existentially quantified
6890 * variables for which we already had an explicit representation.
6892 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6894 struct isl_basic_set
*bset
;
6895 struct isl_set
*set
;
6896 struct isl_map
*map
;
6898 isl_local_space
*ls
;
6905 bmap
= isl_basic_map_sort_divs(bmap
);
6906 bmap
= isl_basic_map_cow(bmap
);
6910 n_known
= isl_basic_map_first_unknown_div(bmap
);
6912 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6914 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6915 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6916 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6917 dim
= isl_space_set_alloc(bmap
->ctx
,
6918 nparam
+ n_in
+ n_out
+ n_known
, 0);
6922 ls
= isl_basic_map_get_local_space(bmap
);
6923 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6924 n_known
, bmap
->n_div
- n_known
);
6926 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6927 swap_div(bmap
, i
- n_known
, i
);
6928 bmap
->n_div
-= n_known
;
6929 bmap
->extra
-= n_known
;
6931 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6932 bset
= bset_from_bmap(bmap
);
6934 set
= parameter_compute_divs(bset
);
6935 map
= set_to_map(set
);
6936 map
= replace_space_by_local_space(map
, ls
);
6940 isl_basic_map_free(bmap
);
6944 /* Remove the explicit representation of local variable "div",
6947 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6948 __isl_take isl_basic_map
*bmap
, int div
)
6952 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6954 return isl_basic_map_free(bmap
);
6958 bmap
= isl_basic_map_cow(bmap
);
6961 isl_int_set_si(bmap
->div
[div
][0], 0);
6965 /* Is local variable "div" of "bmap" marked as not having an explicit
6967 * Note that even if "div" is not marked in this way and therefore
6968 * has an explicit representation, this representation may still
6969 * depend (indirectly) on other local variables that do not
6970 * have an explicit representation.
6972 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6976 return isl_bool_error
;
6977 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6978 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6979 "position out of bounds", return isl_bool_error
);
6980 return isl_int_is_zero(bmap
->div
[div
][0]);
6983 /* Return the position of the first local variable that does not
6984 * have an explicit representation.
6985 * Return the total number of local variables if they all have
6986 * an explicit representation.
6987 * Return -1 on error.
6989 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6996 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6997 if (!isl_basic_map_div_is_known(bmap
, i
))
7003 /* Return the position of the first local variable that does not
7004 * have an explicit representation.
7005 * Return the total number of local variables if they all have
7006 * an explicit representation.
7007 * Return -1 on error.
7009 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7011 return isl_basic_map_first_unknown_div(bset
);
7014 /* Does "bmap" have an explicit representation for all local variables?
7016 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7020 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7021 first
= isl_basic_map_first_unknown_div(bmap
);
7023 return isl_bool_error
;
7027 /* Do all basic maps in "map" have an explicit representation
7028 * for all local variables?
7030 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7035 return isl_bool_error
;
7037 for (i
= 0; i
< map
->n
; ++i
) {
7038 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7043 return isl_bool_true
;
7046 /* If bmap contains any unknown divs, then compute explicit
7047 * expressions for them. However, this computation may be
7048 * quite expensive, so first try to remove divs that aren't
7051 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7054 struct isl_map
*map
;
7056 known
= isl_basic_map_divs_known(bmap
);
7060 return isl_map_from_basic_map(bmap
);
7062 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7064 known
= isl_basic_map_divs_known(bmap
);
7068 return isl_map_from_basic_map(bmap
);
7070 map
= compute_divs(bmap
);
7073 isl_basic_map_free(bmap
);
7077 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7081 struct isl_map
*res
;
7088 known
= isl_map_divs_known(map
);
7096 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7097 for (i
= 1 ; i
< map
->n
; ++i
) {
7099 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7100 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7101 res
= isl_map_union_disjoint(res
, r2
);
7103 res
= isl_map_union(res
, r2
);
7110 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7112 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7115 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7117 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7120 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7123 struct isl_set
*set
;
7128 map
= isl_map_cow(map
);
7132 set
= set_from_map(map
);
7133 set
->dim
= isl_space_domain(set
->dim
);
7136 for (i
= 0; i
< map
->n
; ++i
) {
7137 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7141 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7142 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7149 /* Return the union of "map1" and "map2", where we assume for now that
7150 * "map1" and "map2" are disjoint. Note that the basic maps inside
7151 * "map1" or "map2" may not be disjoint from each other.
7152 * Also note that this function is also called from isl_map_union,
7153 * which takes care of handling the situation where "map1" and "map2"
7154 * may not be disjoint.
7156 * If one of the inputs is empty, we can simply return the other input.
7157 * Similarly, if one of the inputs is universal, then it is equal to the union.
7159 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7160 __isl_take isl_map
*map2
)
7164 struct isl_map
*map
= NULL
;
7170 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7171 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7172 "spaces don't match", goto error
);
7183 is_universe
= isl_map_plain_is_universe(map1
);
7184 if (is_universe
< 0)
7191 is_universe
= isl_map_plain_is_universe(map2
);
7192 if (is_universe
< 0)
7199 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7200 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7201 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7203 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7204 map1
->n
+ map2
->n
, flags
);
7207 for (i
= 0; i
< map1
->n
; ++i
) {
7208 map
= isl_map_add_basic_map(map
,
7209 isl_basic_map_copy(map1
->p
[i
]));
7213 for (i
= 0; i
< map2
->n
; ++i
) {
7214 map
= isl_map_add_basic_map(map
,
7215 isl_basic_map_copy(map2
->p
[i
]));
7229 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7230 * guaranteed to be disjoint by the caller.
7232 * Note that this functions is called from within isl_map_make_disjoint,
7233 * so we have to be careful not to touch the constraints of the inputs
7236 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7237 __isl_take isl_map
*map2
)
7239 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7242 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7243 * not be disjoint. The parameters are assumed to have been aligned.
7245 * We currently simply call map_union_disjoint, the internal operation
7246 * of which does not really depend on the inputs being disjoint.
7247 * If the result contains more than one basic map, then we clear
7248 * the disjoint flag since the result may contain basic maps from
7249 * both inputs and these are not guaranteed to be disjoint.
7251 * As a special case, if "map1" and "map2" are obviously equal,
7252 * then we simply return "map1".
7254 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7255 __isl_take isl_map
*map2
)
7262 equal
= isl_map_plain_is_equal(map1
, map2
);
7270 map1
= map_union_disjoint(map1
, map2
);
7274 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7282 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7285 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7286 __isl_take isl_map
*map2
)
7288 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7291 struct isl_set
*isl_set_union_disjoint(
7292 struct isl_set
*set1
, struct isl_set
*set2
)
7294 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7298 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7300 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7303 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7306 * "map" and "set" are assumed to be compatible and non-NULL.
7308 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7309 __isl_take isl_set
*set
,
7310 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7311 __isl_take isl_basic_set
*bset
))
7314 struct isl_map
*result
;
7317 if (isl_set_plain_is_universe(set
)) {
7322 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7323 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7324 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7326 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7327 map
->n
* set
->n
, flags
);
7328 for (i
= 0; result
&& i
< map
->n
; ++i
)
7329 for (j
= 0; j
< set
->n
; ++j
) {
7330 result
= isl_map_add_basic_map(result
,
7331 fn(isl_basic_map_copy(map
->p
[i
]),
7332 isl_basic_set_copy(set
->p
[j
])));
7342 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7343 __isl_take isl_set
*set
)
7348 if (!isl_map_compatible_range(map
, set
))
7349 isl_die(set
->ctx
, isl_error_invalid
,
7350 "incompatible spaces", goto error
);
7352 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7359 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7360 __isl_take isl_set
*set
)
7362 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7365 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7366 __isl_take isl_set
*set
)
7371 if (!isl_map_compatible_domain(map
, set
))
7372 isl_die(set
->ctx
, isl_error_invalid
,
7373 "incompatible spaces", goto error
);
7375 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7382 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7383 __isl_take isl_set
*set
)
7385 return isl_map_align_params_map_map_and(map
, set
,
7386 &map_intersect_domain
);
7389 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7390 __isl_take isl_map
*map2
)
7394 map1
= isl_map_reverse(map1
);
7395 map1
= isl_map_apply_range(map1
, map2
);
7396 return isl_map_reverse(map1
);
7403 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7404 __isl_take isl_map
*map2
)
7406 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7409 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7410 __isl_take isl_map
*map2
)
7412 isl_space
*dim_result
;
7413 struct isl_map
*result
;
7419 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7420 isl_space_copy(map2
->dim
));
7422 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7425 for (i
= 0; i
< map1
->n
; ++i
)
7426 for (j
= 0; j
< map2
->n
; ++j
) {
7427 result
= isl_map_add_basic_map(result
,
7428 isl_basic_map_apply_range(
7429 isl_basic_map_copy(map1
->p
[i
]),
7430 isl_basic_map_copy(map2
->p
[j
])));
7436 if (result
&& result
->n
<= 1)
7437 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7445 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7446 __isl_take isl_map
*map2
)
7448 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7452 * returns range - domain
7454 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7456 isl_space
*target_space
;
7457 struct isl_basic_set
*bset
;
7464 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7465 bmap
->dim
, isl_dim_out
),
7467 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7468 dim
= isl_basic_map_n_in(bmap
);
7469 nparam
= isl_basic_map_n_param(bmap
);
7470 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7471 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7472 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7473 for (i
= 0; i
< dim
; ++i
) {
7474 int j
= isl_basic_map_alloc_equality(bmap
);
7476 bmap
= isl_basic_map_free(bmap
);
7479 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7480 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7481 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7482 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7484 bset
= isl_basic_map_domain(bmap
);
7485 bset
= isl_basic_set_reset_space(bset
, target_space
);
7488 isl_basic_map_free(bmap
);
7493 * returns range - domain
7495 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7499 struct isl_set
*result
;
7504 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7505 map
->dim
, isl_dim_out
),
7507 dim
= isl_map_get_space(map
);
7508 dim
= isl_space_domain(dim
);
7509 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7512 for (i
= 0; i
< map
->n
; ++i
)
7513 result
= isl_set_add_basic_set(result
,
7514 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7523 * returns [domain -> range] -> range - domain
7525 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7526 __isl_take isl_basic_map
*bmap
)
7530 isl_basic_map
*domain
;
7534 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7535 bmap
->dim
, isl_dim_out
))
7536 isl_die(bmap
->ctx
, isl_error_invalid
,
7537 "domain and range don't match", goto error
);
7539 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7540 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7542 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7543 domain
= isl_basic_map_universe(dim
);
7545 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7546 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7547 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7549 total
= isl_basic_map_total_dim(bmap
);
7551 for (i
= 0; i
< n
; ++i
) {
7552 k
= isl_basic_map_alloc_equality(bmap
);
7555 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7556 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7557 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7558 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7561 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7562 return isl_basic_map_finalize(bmap
);
7564 isl_basic_map_free(bmap
);
7569 * returns [domain -> range] -> range - domain
7571 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7574 isl_space
*domain_dim
;
7579 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7580 map
->dim
, isl_dim_out
))
7581 isl_die(map
->ctx
, isl_error_invalid
,
7582 "domain and range don't match", goto error
);
7584 map
= isl_map_cow(map
);
7588 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7589 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7590 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7593 for (i
= 0; i
< map
->n
; ++i
) {
7594 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7598 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7605 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7607 struct isl_basic_map
*bmap
;
7615 nparam
= dims
->nparam
;
7617 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7621 for (i
= 0; i
< dim
; ++i
) {
7622 int j
= isl_basic_map_alloc_equality(bmap
);
7625 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7626 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7627 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7629 return isl_basic_map_finalize(bmap
);
7631 isl_basic_map_free(bmap
);
7635 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7639 if (dim
->n_in
!= dim
->n_out
)
7640 isl_die(dim
->ctx
, isl_error_invalid
,
7641 "number of input and output dimensions needs to be "
7642 "the same", goto error
);
7643 return basic_map_identity(dim
);
7645 isl_space_free(dim
);
7649 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7651 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7654 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7656 isl_space
*dim
= isl_set_get_space(set
);
7658 id
= isl_map_identity(isl_space_map_from_set(dim
));
7659 return isl_map_intersect_range(id
, set
);
7662 /* Construct a basic set with all set dimensions having only non-negative
7665 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7666 __isl_take isl_space
*space
)
7671 struct isl_basic_set
*bset
;
7675 nparam
= space
->nparam
;
7677 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7680 for (i
= 0; i
< dim
; ++i
) {
7681 int k
= isl_basic_set_alloc_inequality(bset
);
7684 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7685 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7689 isl_basic_set_free(bset
);
7693 /* Construct the half-space x_pos >= 0.
7695 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7699 isl_basic_set
*nonneg
;
7701 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7702 k
= isl_basic_set_alloc_inequality(nonneg
);
7705 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7706 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7708 return isl_basic_set_finalize(nonneg
);
7710 isl_basic_set_free(nonneg
);
7714 /* Construct the half-space x_pos <= -1.
7716 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7721 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7722 k
= isl_basic_set_alloc_inequality(neg
);
7725 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7726 isl_int_set_si(neg
->ineq
[k
][0], -1);
7727 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7729 return isl_basic_set_finalize(neg
);
7731 isl_basic_set_free(neg
);
7735 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7736 enum isl_dim_type type
, unsigned first
, unsigned n
)
7740 isl_basic_set
*nonneg
;
7748 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7750 offset
= pos(set
->dim
, type
);
7751 for (i
= 0; i
< n
; ++i
) {
7752 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7753 offset
+ first
+ i
);
7754 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7756 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7765 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7766 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7773 if (isl_set_plain_is_empty(set
)) {
7778 return fn(set
, signs
, user
);
7781 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7783 half
= isl_set_intersect(half
, isl_set_copy(set
));
7784 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7788 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7790 half
= isl_set_intersect(half
, set
);
7791 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7797 /* Call "fn" on the intersections of "set" with each of the orthants
7798 * (except for obviously empty intersections). The orthant is identified
7799 * by the signs array, with each entry having value 1 or -1 according
7800 * to the sign of the corresponding variable.
7802 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7803 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7813 if (isl_set_plain_is_empty(set
))
7816 nparam
= isl_set_dim(set
, isl_dim_param
);
7817 nvar
= isl_set_dim(set
, isl_dim_set
);
7819 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7821 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7829 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7831 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7834 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7835 __isl_keep isl_basic_map
*bmap2
)
7838 struct isl_map
*map1
;
7839 struct isl_map
*map2
;
7841 if (!bmap1
|| !bmap2
)
7842 return isl_bool_error
;
7844 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7845 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7847 is_subset
= isl_map_is_subset(map1
, map2
);
7855 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7856 __isl_keep isl_basic_set
*bset2
)
7858 return isl_basic_map_is_subset(bset1
, bset2
);
7861 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7862 __isl_keep isl_basic_map
*bmap2
)
7866 if (!bmap1
|| !bmap2
)
7867 return isl_bool_error
;
7868 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7869 if (is_subset
!= isl_bool_true
)
7871 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7875 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7876 __isl_keep isl_basic_set
*bset2
)
7878 return isl_basic_map_is_equal(
7879 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7882 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7888 return isl_bool_error
;
7889 for (i
= 0; i
< map
->n
; ++i
) {
7890 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7892 return isl_bool_error
;
7894 return isl_bool_false
;
7896 return isl_bool_true
;
7899 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7901 return map
? map
->n
== 0 : isl_bool_error
;
7904 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7906 return set
? set
->n
== 0 : isl_bool_error
;
7909 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7911 return isl_map_is_empty(set_to_map(set
));
7914 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7919 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7922 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7927 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7930 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7935 return isl_bool_error
;
7936 is_subset
= isl_map_is_subset(map1
, map2
);
7937 if (is_subset
!= isl_bool_true
)
7939 is_subset
= isl_map_is_subset(map2
, map1
);
7943 /* Is "map1" equal to "map2"?
7945 * First check if they are obviously equal.
7946 * If not, then perform a more detailed analysis.
7948 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7952 equal
= isl_map_plain_is_equal(map1
, map2
);
7953 if (equal
< 0 || equal
)
7955 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7958 isl_bool
isl_basic_map_is_strict_subset(
7959 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7963 if (!bmap1
|| !bmap2
)
7964 return isl_bool_error
;
7965 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7966 if (is_subset
!= isl_bool_true
)
7968 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7969 if (is_subset
== isl_bool_error
)
7974 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7975 __isl_keep isl_map
*map2
)
7980 return isl_bool_error
;
7981 is_subset
= isl_map_is_subset(map1
, map2
);
7982 if (is_subset
!= isl_bool_true
)
7984 is_subset
= isl_map_is_subset(map2
, map1
);
7985 if (is_subset
== isl_bool_error
)
7990 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7991 __isl_keep isl_set
*set2
)
7993 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
7996 /* Is "bmap" obviously equal to the universe with the same space?
7998 * That is, does it not have any constraints?
8000 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8003 return isl_bool_error
;
8004 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8007 /* Is "bset" obviously equal to the universe with the same space?
8009 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8011 return isl_basic_map_plain_is_universe(bset
);
8014 /* If "c" does not involve any existentially quantified variables,
8015 * then set *univ to false and abort
8017 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8019 isl_bool
*univ
= user
;
8022 n
= isl_constraint_dim(c
, isl_dim_div
);
8023 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8024 isl_constraint_free(c
);
8025 if (*univ
< 0 || !*univ
)
8026 return isl_stat_error
;
8030 /* Is "bmap" equal to the universe with the same space?
8032 * First check if it is obviously equal to the universe.
8033 * If not and if there are any constraints not involving
8034 * existentially quantified variables, then it is certainly
8035 * not equal to the universe.
8036 * Otherwise, check if the universe is a subset of "bmap".
8038 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8041 isl_basic_map
*test
;
8043 univ
= isl_basic_map_plain_is_universe(bmap
);
8044 if (univ
< 0 || univ
)
8046 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8047 return isl_bool_false
;
8048 univ
= isl_bool_true
;
8049 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8051 return isl_bool_error
;
8052 if (univ
< 0 || !univ
)
8054 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8055 univ
= isl_basic_map_is_subset(test
, bmap
);
8056 isl_basic_map_free(test
);
8060 /* Is "bset" equal to the universe with the same space?
8062 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8064 return isl_basic_map_is_universe(bset
);
8067 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8072 return isl_bool_error
;
8074 for (i
= 0; i
< map
->n
; ++i
) {
8075 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8080 return isl_bool_false
;
8083 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8085 return isl_map_plain_is_universe(set_to_map(set
));
8088 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8090 struct isl_basic_set
*bset
= NULL
;
8091 struct isl_vec
*sample
= NULL
;
8092 isl_bool empty
, non_empty
;
8095 return isl_bool_error
;
8097 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8098 return isl_bool_true
;
8100 if (isl_basic_map_plain_is_universe(bmap
))
8101 return isl_bool_false
;
8103 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8104 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8105 copy
= isl_basic_map_remove_redundancies(copy
);
8106 empty
= isl_basic_map_plain_is_empty(copy
);
8107 isl_basic_map_free(copy
);
8111 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8113 return isl_bool_error
;
8115 return isl_bool_false
;
8116 isl_vec_free(bmap
->sample
);
8117 bmap
->sample
= NULL
;
8118 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8120 return isl_bool_error
;
8121 sample
= isl_basic_set_sample_vec(bset
);
8123 return isl_bool_error
;
8124 empty
= sample
->size
== 0;
8125 isl_vec_free(bmap
->sample
);
8126 bmap
->sample
= sample
;
8128 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8133 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8136 return isl_bool_error
;
8137 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8140 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8143 return isl_bool_error
;
8144 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8147 /* Is "bmap" known to be non-empty?
8149 * That is, is the cached sample still valid?
8151 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8156 return isl_bool_error
;
8158 return isl_bool_false
;
8159 total
= 1 + isl_basic_map_total_dim(bmap
);
8160 if (bmap
->sample
->size
!= total
)
8161 return isl_bool_false
;
8162 return isl_basic_map_contains(bmap
, bmap
->sample
);
8165 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8167 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8170 struct isl_map
*isl_basic_map_union(
8171 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8173 struct isl_map
*map
;
8174 if (!bmap1
|| !bmap2
)
8177 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8179 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8182 map
= isl_map_add_basic_map(map
, bmap1
);
8183 map
= isl_map_add_basic_map(map
, bmap2
);
8186 isl_basic_map_free(bmap1
);
8187 isl_basic_map_free(bmap2
);
8191 struct isl_set
*isl_basic_set_union(
8192 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8194 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8195 bset_to_bmap(bset2
)));
8198 /* Order divs such that any div only depends on previous divs */
8199 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8207 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8209 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8211 if (isl_int_is_zero(bmap
->div
[i
][0]))
8213 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8218 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8219 "integer division depends on itself",
8220 return isl_basic_map_free(bmap
));
8221 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8227 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8229 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8232 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8239 for (i
= 0; i
< map
->n
; ++i
) {
8240 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8251 /* Sort the local variables of "bset".
8253 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8254 __isl_take isl_basic_set
*bset
)
8256 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8259 /* Apply the expansion computed by isl_merge_divs.
8260 * The expansion itself is given by "exp" while the resulting
8261 * list of divs is given by "div".
8263 * Move the integer divisions of "bmap" into the right position
8264 * according to "exp" and then introduce the additional integer
8265 * divisions, adding div constraints.
8266 * The moving should be done first to avoid moving coefficients
8267 * in the definitions of the extra integer divisions.
8269 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8270 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8275 bmap
= isl_basic_map_cow(bmap
);
8279 if (div
->n_row
< bmap
->n_div
)
8280 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8281 "not an expansion", goto error
);
8283 n_div
= bmap
->n_div
;
8284 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8285 div
->n_row
- n_div
, 0,
8286 2 * (div
->n_row
- n_div
));
8288 for (i
= n_div
; i
< div
->n_row
; ++i
)
8289 if (isl_basic_map_alloc_div(bmap
) < 0)
8292 for (j
= n_div
- 1; j
>= 0; --j
) {
8295 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8298 for (i
= 0; i
< div
->n_row
; ++i
) {
8299 if (j
< n_div
&& exp
[j
] == i
) {
8302 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8303 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8305 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8313 isl_basic_map_free(bmap
);
8318 /* Apply the expansion computed by isl_merge_divs.
8319 * The expansion itself is given by "exp" while the resulting
8320 * list of divs is given by "div".
8322 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8323 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8325 return isl_basic_map_expand_divs(bset
, div
, exp
);
8328 /* Look for a div in dst that corresponds to the div "div" in src.
8329 * The divs before "div" in src and dst are assumed to be the same.
8331 * Returns -1 if no corresponding div was found and the position
8332 * of the corresponding div in dst otherwise.
8334 static int find_div(struct isl_basic_map
*dst
,
8335 struct isl_basic_map
*src
, unsigned div
)
8339 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8341 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8342 for (i
= div
; i
< dst
->n_div
; ++i
)
8343 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8344 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8345 dst
->n_div
- div
) == -1)
8350 /* Align the divs of "dst" to those of "src", adding divs from "src"
8351 * if needed. That is, make sure that the first src->n_div divs
8352 * of the result are equal to those of src.
8354 * The result is not finalized as by design it will have redundant
8355 * divs if any divs from "src" were copied.
8357 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8358 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8361 int known
, extended
;
8365 return isl_basic_map_free(dst
);
8367 if (src
->n_div
== 0)
8370 known
= isl_basic_map_divs_known(src
);
8372 return isl_basic_map_free(dst
);
8374 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8375 "some src divs are unknown",
8376 return isl_basic_map_free(dst
));
8378 src
= isl_basic_map_order_divs(src
);
8381 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8382 for (i
= 0; i
< src
->n_div
; ++i
) {
8383 int j
= find_div(dst
, src
, i
);
8386 int extra
= src
->n_div
- i
;
8387 dst
= isl_basic_map_cow(dst
);
8390 dst
= isl_basic_map_extend_space(dst
,
8391 isl_space_copy(dst
->dim
),
8392 extra
, 0, 2 * extra
);
8395 j
= isl_basic_map_alloc_div(dst
);
8397 return isl_basic_map_free(dst
);
8398 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8399 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8400 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8401 return isl_basic_map_free(dst
);
8404 isl_basic_map_swap_div(dst
, i
, j
);
8409 struct isl_basic_set
*isl_basic_set_align_divs(
8410 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8412 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8413 bset_to_bmap(src
)));
8416 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8424 map
= isl_map_compute_divs(map
);
8425 map
= isl_map_cow(map
);
8429 for (i
= 1; i
< map
->n
; ++i
)
8430 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8431 for (i
= 1; i
< map
->n
; ++i
) {
8432 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8434 return isl_map_free(map
);
8437 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8441 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8443 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8446 /* Align the divs of the basic maps in "map" to those
8447 * of the basic maps in "list", as well as to the other basic maps in "map".
8448 * The elements in "list" are assumed to have known divs.
8450 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8451 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8455 map
= isl_map_compute_divs(map
);
8456 map
= isl_map_cow(map
);
8458 return isl_map_free(map
);
8462 n
= isl_basic_map_list_n_basic_map(list
);
8463 for (i
= 0; i
< n
; ++i
) {
8464 isl_basic_map
*bmap
;
8466 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8467 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8468 isl_basic_map_free(bmap
);
8471 return isl_map_free(map
);
8473 return isl_map_align_divs(map
);
8476 /* Align the divs of each element of "list" to those of "bmap".
8477 * Both "bmap" and the elements of "list" are assumed to have known divs.
8479 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8480 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8485 return isl_basic_map_list_free(list
);
8487 n
= isl_basic_map_list_n_basic_map(list
);
8488 for (i
= 0; i
< n
; ++i
) {
8489 isl_basic_map
*bmap_i
;
8491 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8492 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8493 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8499 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8500 __isl_take isl_map
*map
)
8504 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8505 map
= isl_map_intersect_domain(map
, set
);
8506 set
= isl_map_range(map
);
8514 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8515 __isl_take isl_map
*map
)
8517 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8520 /* There is no need to cow as removing empty parts doesn't change
8521 * the meaning of the set.
8523 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8530 for (i
= map
->n
- 1; i
>= 0; --i
)
8531 remove_if_empty(map
, i
);
8536 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8538 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8541 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8543 struct isl_basic_map
*bmap
;
8544 if (!map
|| map
->n
== 0)
8546 bmap
= map
->p
[map
->n
-1];
8547 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8548 return isl_basic_map_copy(bmap
);
8551 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8553 return bset_from_bmap(isl_map_copy_basic_map(set_to_map(set
)));
8556 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8557 __isl_keep isl_basic_map
*bmap
)
8563 for (i
= map
->n
-1; i
>= 0; --i
) {
8564 if (map
->p
[i
] != bmap
)
8566 map
= isl_map_cow(map
);
8569 isl_basic_map_free(map
->p
[i
]);
8570 if (i
!= map
->n
-1) {
8571 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8572 map
->p
[i
] = map
->p
[map
->n
-1];
8583 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8584 struct isl_basic_set
*bset
)
8586 return set_from_map(isl_map_drop_basic_map(set_to_map(set
),
8587 bset_to_bmap(bset
)));
8590 /* Given two basic sets bset1 and bset2, compute the maximal difference
8591 * between the values of dimension pos in bset1 and those in bset2
8592 * for any common value of the parameters and dimensions preceding pos.
8594 static enum isl_lp_result
basic_set_maximal_difference_at(
8595 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8596 int pos
, isl_int
*opt
)
8599 struct isl_basic_map
*bmap1
= NULL
;
8600 struct isl_basic_map
*bmap2
= NULL
;
8601 struct isl_ctx
*ctx
;
8602 struct isl_vec
*obj
;
8605 unsigned dim1
, dim2
;
8606 enum isl_lp_result res
;
8608 if (!bset1
|| !bset2
)
8609 return isl_lp_error
;
8611 nparam
= isl_basic_set_n_param(bset1
);
8612 dim1
= isl_basic_set_n_dim(bset1
);
8613 dim2
= isl_basic_set_n_dim(bset2
);
8614 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8615 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8616 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8617 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8618 if (!bmap1
|| !bmap2
)
8620 bmap1
= isl_basic_map_cow(bmap1
);
8621 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8622 pos
, (dim1
- pos
) + (dim2
- pos
),
8623 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8624 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8627 total
= isl_basic_map_total_dim(bmap1
);
8629 obj
= isl_vec_alloc(ctx
, 1 + total
);
8632 isl_seq_clr(obj
->block
.data
, 1 + total
);
8633 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8634 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8635 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8637 isl_basic_map_free(bmap1
);
8641 isl_basic_map_free(bmap2
);
8643 isl_basic_map_free(bmap1
);
8644 return isl_lp_error
;
8647 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * in both basic sets, the values of dimension pos in bset1 are
8650 * smaller or larger than those in bset2.
8653 * 1 if bset1 follows bset2
8654 * -1 if bset1 precedes bset2
8655 * 0 if bset1 and bset2 are incomparable
8656 * -2 if some error occurred.
8658 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8659 struct isl_basic_set
*bset2
, int pos
)
8662 enum isl_lp_result res
;
8667 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8669 if (res
== isl_lp_empty
)
8671 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8672 res
== isl_lp_unbounded
)
8674 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8683 /* Given two basic sets bset1 and bset2, check whether
8684 * for any common value of the parameters and dimensions preceding pos
8685 * there is a value of dimension pos in bset1 that is larger
8686 * than a value of the same dimension in bset2.
8689 * 1 if there exists such a pair
8690 * 0 if there is no such pair, but there is a pair of equal values
8692 * -2 if some error occurred.
8694 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8695 __isl_keep isl_basic_set
*bset2
, int pos
)
8698 enum isl_lp_result res
;
8703 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8705 if (res
== isl_lp_empty
)
8707 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8708 res
== isl_lp_unbounded
)
8710 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8712 else if (res
== isl_lp_ok
)
8721 /* Given two sets set1 and set2, check whether
8722 * for any common value of the parameters and dimensions preceding pos
8723 * there is a value of dimension pos in set1 that is larger
8724 * than a value of the same dimension in set2.
8727 * 1 if there exists such a pair
8728 * 0 if there is no such pair, but there is a pair of equal values
8730 * -2 if some error occurred.
8732 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8733 __isl_keep isl_set
*set2
, int pos
)
8741 for (i
= 0; i
< set1
->n
; ++i
)
8742 for (j
= 0; j
< set2
->n
; ++j
) {
8744 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8745 if (f
== 1 || f
== -2)
8754 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8755 unsigned pos
, isl_int
*val
)
8763 total
= isl_basic_map_total_dim(bmap
);
8764 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8765 for (; d
+1 > pos
; --d
)
8766 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8770 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8772 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8774 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8777 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8783 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8784 unsigned pos
, isl_int
*val
)
8796 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8799 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8800 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8801 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8802 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8806 isl_int_set(*val
, v
);
8812 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8813 unsigned pos
, isl_int
*val
)
8815 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8819 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8822 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
8825 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8826 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8828 if (pos
>= isl_basic_map_dim(bmap
, type
))
8830 return isl_basic_map_plain_has_fixed_var(bmap
,
8831 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8834 /* If "bmap" obviously lies on a hyperplane where the given dimension
8835 * has a fixed value, then return that value.
8836 * Otherwise return NaN.
8838 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8839 __isl_keep isl_basic_map
*bmap
,
8840 enum isl_dim_type type
, unsigned pos
)
8848 ctx
= isl_basic_map_get_ctx(bmap
);
8849 v
= isl_val_alloc(ctx
);
8852 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8854 return isl_val_free(v
);
8856 isl_int_set_si(v
->d
, 1);
8860 return isl_val_nan(ctx
);
8863 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8864 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8866 if (pos
>= isl_map_dim(map
, type
))
8868 return isl_map_plain_has_fixed_var(map
,
8869 map_offset(map
, type
) - 1 + pos
, val
);
8872 /* If "map" obviously lies on a hyperplane where the given dimension
8873 * has a fixed value, then return that value.
8874 * Otherwise return NaN.
8876 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8877 enum isl_dim_type type
, unsigned pos
)
8885 ctx
= isl_map_get_ctx(map
);
8886 v
= isl_val_alloc(ctx
);
8889 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8891 return isl_val_free(v
);
8893 isl_int_set_si(v
->d
, 1);
8897 return isl_val_nan(ctx
);
8900 /* If "set" obviously lies on a hyperplane where the given dimension
8901 * has a fixed value, then return that value.
8902 * Otherwise return NaN.
8904 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8905 enum isl_dim_type type
, unsigned pos
)
8907 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8910 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8911 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8913 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8916 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8917 * then return this fixed value in *val.
8919 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8920 unsigned dim
, isl_int
*val
)
8922 return isl_basic_set_plain_has_fixed_var(bset
,
8923 isl_basic_set_n_param(bset
) + dim
, val
);
8926 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8927 * then return this fixed value in *val.
8929 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8930 unsigned dim
, isl_int
*val
)
8932 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8935 /* Check if input variable in has fixed value and if so and if val is not NULL,
8936 * then return this fixed value in *val.
8938 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8939 unsigned in
, isl_int
*val
)
8941 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8944 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8945 * and if val is not NULL, then return this lower bound in *val.
8947 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8948 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8950 int i
, i_eq
= -1, i_ineq
= -1;
8957 total
= isl_basic_set_total_dim(bset
);
8958 nparam
= isl_basic_set_n_param(bset
);
8959 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8960 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8966 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8967 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8969 if (i_eq
!= -1 || i_ineq
!= -1)
8973 if (i_eq
== -1 && i_ineq
== -1)
8975 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8976 /* The coefficient should always be one due to normalization. */
8977 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8979 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8981 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8982 total
- nparam
- dim
- 1) != -1)
8985 isl_int_neg(*val
, c
[0]);
8989 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8990 unsigned dim
, isl_int
*val
)
9002 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9006 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9008 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9009 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9011 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9015 isl_int_set(*val
, v
);
9021 /* Return -1 if the constraint "c1" should be sorted before "c2"
9022 * and 1 if it should be sorted after "c2".
9023 * Return 0 if the two constraints are the same (up to the constant term).
9025 * In particular, if a constraint involves later variables than another
9026 * then it is sorted after this other constraint.
9027 * uset_gist depends on constraints without existentially quantified
9028 * variables sorting first.
9030 * For constraints that have the same latest variable, those
9031 * with the same coefficient for this latest variable (first in absolute value
9032 * and then in actual value) are grouped together.
9033 * This is useful for detecting pairs of constraints that can
9034 * be chained in their printed representation.
9036 * Finally, within a group, constraints are sorted according to
9037 * their coefficients (excluding the constant term).
9039 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9041 isl_int
**c1
= (isl_int
**) p1
;
9042 isl_int
**c2
= (isl_int
**) p2
;
9044 unsigned size
= *(unsigned *) arg
;
9047 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9048 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9053 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9056 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9060 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9063 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9064 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9065 * and 0 if the two constraints are the same (up to the constant term).
9067 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9068 isl_int
*c1
, isl_int
*c2
)
9074 total
= isl_basic_map_total_dim(bmap
);
9075 return sort_constraint_cmp(&c1
, &c2
, &total
);
9078 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9079 __isl_take isl_basic_map
*bmap
)
9085 if (bmap
->n_ineq
== 0)
9087 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9089 total
= isl_basic_map_total_dim(bmap
);
9090 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9091 &sort_constraint_cmp
, &total
) < 0)
9092 return isl_basic_map_free(bmap
);
9096 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9097 __isl_take isl_basic_set
*bset
)
9099 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9100 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9103 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9107 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9109 bmap
= isl_basic_map_remove_redundancies(bmap
);
9110 bmap
= isl_basic_map_sort_constraints(bmap
);
9112 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9116 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9118 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9121 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9122 const __isl_keep isl_basic_map
*bmap2
)
9127 if (!bmap1
|| !bmap2
)
9132 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9133 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9134 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9135 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9136 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9137 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9138 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9139 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9140 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9141 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9142 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9144 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9146 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9148 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9149 return bmap1
->n_eq
- bmap2
->n_eq
;
9150 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9151 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9152 if (bmap1
->n_div
!= bmap2
->n_div
)
9153 return bmap1
->n_div
- bmap2
->n_div
;
9154 total
= isl_basic_map_total_dim(bmap1
);
9155 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9156 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9160 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9161 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9165 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9166 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9173 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9174 const __isl_keep isl_basic_set
*bset2
)
9176 return isl_basic_map_plain_cmp(bset1
, bset2
);
9179 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9185 if (set1
->n
!= set2
->n
)
9186 return set1
->n
- set2
->n
;
9188 for (i
= 0; i
< set1
->n
; ++i
) {
9189 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9197 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9198 __isl_keep isl_basic_map
*bmap2
)
9200 if (!bmap1
|| !bmap2
)
9201 return isl_bool_error
;
9202 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9205 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9206 __isl_keep isl_basic_set
*bset2
)
9208 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9209 bset_to_bmap(bset2
));
9212 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9214 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9215 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9217 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9220 /* Sort the basic maps of "map" and remove duplicate basic maps.
9222 * While removing basic maps, we make sure that the basic maps remain
9223 * sorted because isl_map_normalize expects the basic maps of the result
9226 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9230 map
= isl_map_remove_empty_parts(map
);
9233 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9234 for (i
= map
->n
- 1; i
>= 1; --i
) {
9235 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9237 isl_basic_map_free(map
->p
[i
-1]);
9238 for (j
= i
; j
< map
->n
; ++j
)
9239 map
->p
[j
- 1] = map
->p
[j
];
9246 /* Remove obvious duplicates among the basic maps of "map".
9248 * Unlike isl_map_normalize, this function does not remove redundant
9249 * constraints and only removes duplicates that have exactly the same
9250 * constraints in the input. It does sort the constraints and
9251 * the basic maps to ease the detection of duplicates.
9253 * If "map" has already been normalized or if the basic maps are
9254 * disjoint, then there can be no duplicates.
9256 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9259 isl_basic_map
*bmap
;
9265 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9267 for (i
= 0; i
< map
->n
; ++i
) {
9268 bmap
= isl_basic_map_copy(map
->p
[i
]);
9269 bmap
= isl_basic_map_sort_constraints(bmap
);
9271 return isl_map_free(map
);
9272 isl_basic_map_free(map
->p
[i
]);
9276 map
= sort_and_remove_duplicates(map
);
9280 /* We normalize in place, but if anything goes wrong we need
9281 * to return NULL, so we need to make sure we don't change the
9282 * meaning of any possible other copies of map.
9284 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9287 struct isl_basic_map
*bmap
;
9291 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9293 for (i
= 0; i
< map
->n
; ++i
) {
9294 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9297 isl_basic_map_free(map
->p
[i
]);
9301 map
= sort_and_remove_duplicates(map
);
9303 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9310 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9312 return set_from_map(isl_map_normalize(set_to_map(set
)));
9315 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9316 __isl_keep isl_map
*map2
)
9322 return isl_bool_error
;
9325 return isl_bool_true
;
9326 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9327 return isl_bool_false
;
9329 map1
= isl_map_copy(map1
);
9330 map2
= isl_map_copy(map2
);
9331 map1
= isl_map_normalize(map1
);
9332 map2
= isl_map_normalize(map2
);
9335 equal
= map1
->n
== map2
->n
;
9336 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9337 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9347 return isl_bool_error
;
9350 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9351 __isl_keep isl_set
*set2
)
9353 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9356 /* Return an interval that ranges from min to max (inclusive)
9358 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9359 isl_int min
, isl_int max
)
9362 struct isl_basic_set
*bset
= NULL
;
9364 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9368 k
= isl_basic_set_alloc_inequality(bset
);
9371 isl_int_set_si(bset
->ineq
[k
][1], 1);
9372 isl_int_neg(bset
->ineq
[k
][0], min
);
9374 k
= isl_basic_set_alloc_inequality(bset
);
9377 isl_int_set_si(bset
->ineq
[k
][1], -1);
9378 isl_int_set(bset
->ineq
[k
][0], max
);
9382 isl_basic_set_free(bset
);
9386 /* Return the basic maps in "map" as a list.
9388 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9389 __isl_keep isl_map
*map
)
9393 isl_basic_map_list
*list
;
9397 ctx
= isl_map_get_ctx(map
);
9398 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9400 for (i
= 0; i
< map
->n
; ++i
) {
9401 isl_basic_map
*bmap
;
9403 bmap
= isl_basic_map_copy(map
->p
[i
]);
9404 list
= isl_basic_map_list_add(list
, bmap
);
9410 /* Return the intersection of the elements in the non-empty list "list".
9411 * All elements are assumed to live in the same space.
9413 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9414 __isl_take isl_basic_map_list
*list
)
9417 isl_basic_map
*bmap
;
9421 n
= isl_basic_map_list_n_basic_map(list
);
9423 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9424 "expecting non-empty list", goto error
);
9426 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9427 for (i
= 1; i
< n
; ++i
) {
9428 isl_basic_map
*bmap_i
;
9430 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9431 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9434 isl_basic_map_list_free(list
);
9437 isl_basic_map_list_free(list
);
9441 /* Return the intersection of the elements in the non-empty list "list".
9442 * All elements are assumed to live in the same space.
9444 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9445 __isl_take isl_basic_set_list
*list
)
9447 return isl_basic_map_list_intersect(list
);
9450 /* Return the union of the elements in the non-empty list "list".
9451 * All elements are assumed to live in the same space.
9453 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9460 n
= isl_set_list_n_set(list
);
9462 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9463 "expecting non-empty list", goto error
);
9465 set
= isl_set_list_get_set(list
, 0);
9466 for (i
= 1; i
< n
; ++i
) {
9469 set_i
= isl_set_list_get_set(list
, i
);
9470 set
= isl_set_union(set
, set_i
);
9473 isl_set_list_free(list
);
9476 isl_set_list_free(list
);
9480 /* Return the Cartesian product of the basic sets in list (in the given order).
9482 __isl_give isl_basic_set
*isl_basic_set_list_product(
9483 __isl_take
struct isl_basic_set_list
*list
)
9491 struct isl_basic_set
*product
= NULL
;
9495 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9496 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9497 nparam
= isl_basic_set_n_param(list
->p
[0]);
9498 dim
= isl_basic_set_n_dim(list
->p
[0]);
9499 extra
= list
->p
[0]->n_div
;
9500 n_eq
= list
->p
[0]->n_eq
;
9501 n_ineq
= list
->p
[0]->n_ineq
;
9502 for (i
= 1; i
< list
->n
; ++i
) {
9503 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9504 isl_assert(list
->ctx
,
9505 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9506 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9507 extra
+= list
->p
[i
]->n_div
;
9508 n_eq
+= list
->p
[i
]->n_eq
;
9509 n_ineq
+= list
->p
[i
]->n_ineq
;
9511 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9516 for (i
= 0; i
< list
->n
; ++i
) {
9517 isl_basic_set_add_constraints(product
,
9518 isl_basic_set_copy(list
->p
[i
]), dim
);
9519 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9521 isl_basic_set_list_free(list
);
9524 isl_basic_set_free(product
);
9525 isl_basic_set_list_free(list
);
9529 struct isl_basic_map
*isl_basic_map_product(
9530 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9532 isl_space
*dim_result
= NULL
;
9533 struct isl_basic_map
*bmap
;
9534 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9535 struct isl_dim_map
*dim_map1
, *dim_map2
;
9537 if (!bmap1
|| !bmap2
)
9540 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9541 bmap2
->dim
, isl_dim_param
), goto error
);
9542 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9543 isl_space_copy(bmap2
->dim
));
9545 in1
= isl_basic_map_n_in(bmap1
);
9546 in2
= isl_basic_map_n_in(bmap2
);
9547 out1
= isl_basic_map_n_out(bmap1
);
9548 out2
= isl_basic_map_n_out(bmap2
);
9549 nparam
= isl_basic_map_n_param(bmap1
);
9551 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9552 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9553 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9554 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9555 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9556 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9557 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9558 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9559 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9560 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9561 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9563 bmap
= isl_basic_map_alloc_space(dim_result
,
9564 bmap1
->n_div
+ bmap2
->n_div
,
9565 bmap1
->n_eq
+ bmap2
->n_eq
,
9566 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9567 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9568 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9569 bmap
= isl_basic_map_simplify(bmap
);
9570 return isl_basic_map_finalize(bmap
);
9572 isl_basic_map_free(bmap1
);
9573 isl_basic_map_free(bmap2
);
9577 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9578 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9580 isl_basic_map
*prod
;
9582 prod
= isl_basic_map_product(bmap1
, bmap2
);
9583 prod
= isl_basic_map_flatten(prod
);
9587 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9588 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9590 return isl_basic_map_flat_range_product(bset1
, bset2
);
9593 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9594 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9596 isl_space
*space_result
= NULL
;
9597 isl_basic_map
*bmap
;
9598 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9599 struct isl_dim_map
*dim_map1
, *dim_map2
;
9601 if (!bmap1
|| !bmap2
)
9604 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9605 isl_space_copy(bmap2
->dim
));
9607 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9608 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9609 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9610 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9612 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9613 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9614 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9615 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9616 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9617 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9618 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9619 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9620 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9621 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9622 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9624 bmap
= isl_basic_map_alloc_space(space_result
,
9625 bmap1
->n_div
+ bmap2
->n_div
,
9626 bmap1
->n_eq
+ bmap2
->n_eq
,
9627 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9628 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9629 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9630 bmap
= isl_basic_map_simplify(bmap
);
9631 return isl_basic_map_finalize(bmap
);
9633 isl_basic_map_free(bmap1
);
9634 isl_basic_map_free(bmap2
);
9638 __isl_give isl_basic_map
*isl_basic_map_range_product(
9639 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9642 isl_space
*dim_result
= NULL
;
9643 isl_basic_map
*bmap
;
9644 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9645 struct isl_dim_map
*dim_map1
, *dim_map2
;
9647 rational
= isl_basic_map_is_rational(bmap1
);
9648 if (rational
>= 0 && rational
)
9649 rational
= isl_basic_map_is_rational(bmap2
);
9650 if (!bmap1
|| !bmap2
|| rational
< 0)
9653 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9654 bmap2
->dim
, isl_dim_param
))
9655 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9656 "parameters don't match", goto error
);
9658 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9659 isl_space_copy(bmap2
->dim
));
9661 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9662 out1
= isl_basic_map_n_out(bmap1
);
9663 out2
= isl_basic_map_n_out(bmap2
);
9664 nparam
= isl_basic_map_n_param(bmap1
);
9666 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9667 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9668 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9669 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9670 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9671 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9672 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9673 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9674 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9675 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9676 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9678 bmap
= isl_basic_map_alloc_space(dim_result
,
9679 bmap1
->n_div
+ bmap2
->n_div
,
9680 bmap1
->n_eq
+ bmap2
->n_eq
,
9681 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9682 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9683 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9685 bmap
= isl_basic_map_set_rational(bmap
);
9686 bmap
= isl_basic_map_simplify(bmap
);
9687 return isl_basic_map_finalize(bmap
);
9689 isl_basic_map_free(bmap1
);
9690 isl_basic_map_free(bmap2
);
9694 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9695 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9697 isl_basic_map
*prod
;
9699 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9700 prod
= isl_basic_map_flatten_range(prod
);
9704 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9705 * and collect the results.
9706 * The result live in the space obtained by calling "space_product"
9707 * on the spaces of "map1" and "map2".
9708 * If "remove_duplicates" is set then the result may contain duplicates
9709 * (even if the inputs do not) and so we try and remove the obvious
9712 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9713 __isl_take isl_map
*map2
,
9714 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9715 __isl_take isl_space
*right
),
9716 __isl_give isl_basic_map
*(*basic_map_product
)(
9717 __isl_take isl_basic_map
*left
,
9718 __isl_take isl_basic_map
*right
),
9719 int remove_duplicates
)
9722 struct isl_map
*result
;
9728 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9729 map2
->dim
, isl_dim_param
), goto error
);
9731 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9732 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9733 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9735 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9736 isl_space_copy(map2
->dim
)),
9737 map1
->n
* map2
->n
, flags
);
9740 for (i
= 0; i
< map1
->n
; ++i
)
9741 for (j
= 0; j
< map2
->n
; ++j
) {
9742 struct isl_basic_map
*part
;
9743 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9744 isl_basic_map_copy(map2
->p
[j
]));
9745 if (isl_basic_map_is_empty(part
))
9746 isl_basic_map_free(part
);
9748 result
= isl_map_add_basic_map(result
, part
);
9752 if (remove_duplicates
)
9753 result
= isl_map_remove_obvious_duplicates(result
);
9763 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9765 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9766 __isl_take isl_map
*map2
)
9768 return map_product(map1
, map2
, &isl_space_product
,
9769 &isl_basic_map_product
, 0);
9772 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9773 __isl_take isl_map
*map2
)
9775 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9778 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9780 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9781 __isl_take isl_map
*map2
)
9785 prod
= isl_map_product(map1
, map2
);
9786 prod
= isl_map_flatten(prod
);
9790 /* Given two set A and B, construct its Cartesian product A x B.
9792 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9794 return isl_map_range_product(set1
, set2
);
9797 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9798 __isl_take isl_set
*set2
)
9800 return isl_map_flat_range_product(set1
, set2
);
9803 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9805 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9806 __isl_take isl_map
*map2
)
9808 return map_product(map1
, map2
, &isl_space_domain_product
,
9809 &isl_basic_map_domain_product
, 1);
9812 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9814 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9815 __isl_take isl_map
*map2
)
9817 return map_product(map1
, map2
, &isl_space_range_product
,
9818 &isl_basic_map_range_product
, 1);
9821 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9822 __isl_take isl_map
*map2
)
9824 return isl_map_align_params_map_map_and(map1
, map2
,
9825 &map_domain_product_aligned
);
9828 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9829 __isl_take isl_map
*map2
)
9831 return isl_map_align_params_map_map_and(map1
, map2
,
9832 &map_range_product_aligned
);
9835 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9837 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9840 int total1
, keep1
, total2
, keep2
;
9844 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9845 !isl_space_range_is_wrapping(map
->dim
))
9846 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9847 "not a product", return isl_map_free(map
));
9849 space
= isl_map_get_space(map
);
9850 total1
= isl_space_dim(space
, isl_dim_in
);
9851 total2
= isl_space_dim(space
, isl_dim_out
);
9852 space
= isl_space_factor_domain(space
);
9853 keep1
= isl_space_dim(space
, isl_dim_in
);
9854 keep2
= isl_space_dim(space
, isl_dim_out
);
9855 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9856 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9857 map
= isl_map_reset_space(map
, space
);
9862 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9864 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9867 int total1
, keep1
, total2
, keep2
;
9871 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9872 !isl_space_range_is_wrapping(map
->dim
))
9873 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9874 "not a product", return isl_map_free(map
));
9876 space
= isl_map_get_space(map
);
9877 total1
= isl_space_dim(space
, isl_dim_in
);
9878 total2
= isl_space_dim(space
, isl_dim_out
);
9879 space
= isl_space_factor_range(space
);
9880 keep1
= isl_space_dim(space
, isl_dim_in
);
9881 keep2
= isl_space_dim(space
, isl_dim_out
);
9882 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9883 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9884 map
= isl_map_reset_space(map
, space
);
9889 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9891 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9898 if (!isl_space_domain_is_wrapping(map
->dim
))
9899 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9900 "domain is not a product", return isl_map_free(map
));
9902 space
= isl_map_get_space(map
);
9903 total
= isl_space_dim(space
, isl_dim_in
);
9904 space
= isl_space_domain_factor_domain(space
);
9905 keep
= isl_space_dim(space
, isl_dim_in
);
9906 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9907 map
= isl_map_reset_space(map
, space
);
9912 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9914 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9921 if (!isl_space_domain_is_wrapping(map
->dim
))
9922 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9923 "domain is not a product", return isl_map_free(map
));
9925 space
= isl_map_get_space(map
);
9926 total
= isl_space_dim(space
, isl_dim_in
);
9927 space
= isl_space_domain_factor_range(space
);
9928 keep
= isl_space_dim(space
, isl_dim_in
);
9929 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9930 map
= isl_map_reset_space(map
, space
);
9935 /* Given a map A -> [B -> C], extract the map A -> B.
9937 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9944 if (!isl_space_range_is_wrapping(map
->dim
))
9945 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9946 "range is not a product", return isl_map_free(map
));
9948 space
= isl_map_get_space(map
);
9949 total
= isl_space_dim(space
, isl_dim_out
);
9950 space
= isl_space_range_factor_domain(space
);
9951 keep
= isl_space_dim(space
, isl_dim_out
);
9952 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9953 map
= isl_map_reset_space(map
, space
);
9958 /* Given a map A -> [B -> C], extract the map A -> C.
9960 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9967 if (!isl_space_range_is_wrapping(map
->dim
))
9968 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9969 "range is not a product", return isl_map_free(map
));
9971 space
= isl_map_get_space(map
);
9972 total
= isl_space_dim(space
, isl_dim_out
);
9973 space
= isl_space_range_factor_range(space
);
9974 keep
= isl_space_dim(space
, isl_dim_out
);
9975 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9976 map
= isl_map_reset_space(map
, space
);
9981 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9983 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9984 __isl_take isl_map
*map2
)
9988 prod
= isl_map_domain_product(map1
, map2
);
9989 prod
= isl_map_flatten_domain(prod
);
9993 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9995 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9996 __isl_take isl_map
*map2
)
10000 prod
= isl_map_range_product(map1
, map2
);
10001 prod
= isl_map_flatten_range(prod
);
10005 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10008 uint32_t hash
= isl_hash_init();
10013 bmap
= isl_basic_map_copy(bmap
);
10014 bmap
= isl_basic_map_normalize(bmap
);
10017 total
= isl_basic_map_total_dim(bmap
);
10018 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10019 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10021 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10022 isl_hash_hash(hash
, c_hash
);
10024 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10025 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10027 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10028 isl_hash_hash(hash
, c_hash
);
10030 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10031 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10033 if (isl_int_is_zero(bmap
->div
[i
][0]))
10035 isl_hash_byte(hash
, i
& 0xFF);
10036 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10037 isl_hash_hash(hash
, c_hash
);
10039 isl_basic_map_free(bmap
);
10043 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10045 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10048 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10055 map
= isl_map_copy(map
);
10056 map
= isl_map_normalize(map
);
10060 hash
= isl_hash_init();
10061 for (i
= 0; i
< map
->n
; ++i
) {
10062 uint32_t bmap_hash
;
10063 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10064 isl_hash_hash(hash
, bmap_hash
);
10072 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10074 return isl_map_get_hash(set_to_map(set
));
10077 /* Check if the value for dimension dim is completely determined
10078 * by the values of the other parameters and variables.
10079 * That is, check if dimension dim is involved in an equality.
10081 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10088 nparam
= isl_basic_set_n_param(bset
);
10089 for (i
= 0; i
< bset
->n_eq
; ++i
)
10090 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10095 /* Check if the value for dimension dim is completely determined
10096 * by the values of the other parameters and variables.
10097 * That is, check if dimension dim is involved in an equality
10098 * for each of the subsets.
10100 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10106 for (i
= 0; i
< set
->n
; ++i
) {
10108 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10115 /* Return the number of basic maps in the (current) representation of "map".
10117 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10119 return map
? map
->n
: 0;
10122 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10124 return set
? set
->n
: 0;
10127 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10128 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10133 return isl_stat_error
;
10135 for (i
= 0; i
< map
->n
; ++i
)
10136 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10137 return isl_stat_error
;
10139 return isl_stat_ok
;
10142 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10143 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10148 return isl_stat_error
;
10150 for (i
= 0; i
< set
->n
; ++i
)
10151 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10152 return isl_stat_error
;
10154 return isl_stat_ok
;
10157 /* Return a list of basic sets, the union of which is equal to "set".
10159 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10160 __isl_keep isl_set
*set
)
10163 isl_basic_set_list
*list
;
10168 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10169 for (i
= 0; i
< set
->n
; ++i
) {
10170 isl_basic_set
*bset
;
10172 bset
= isl_basic_set_copy(set
->p
[i
]);
10173 list
= isl_basic_set_list_add(list
, bset
);
10179 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10186 bset
= isl_basic_set_cow(bset
);
10190 dim
= isl_basic_set_get_space(bset
);
10191 dim
= isl_space_lift(dim
, bset
->n_div
);
10194 isl_space_free(bset
->dim
);
10196 bset
->extra
-= bset
->n_div
;
10199 bset
= isl_basic_set_finalize(bset
);
10203 isl_basic_set_free(bset
);
10207 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10213 set
= isl_set_align_divs(set
);
10218 set
= isl_set_cow(set
);
10222 n_div
= set
->p
[0]->n_div
;
10223 dim
= isl_set_get_space(set
);
10224 dim
= isl_space_lift(dim
, n_div
);
10227 isl_space_free(set
->dim
);
10230 for (i
= 0; i
< set
->n
; ++i
) {
10231 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10242 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10245 struct isl_basic_map
*bmap
;
10252 set
= isl_set_align_divs(set
);
10257 dim
= isl_set_get_space(set
);
10258 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10260 return isl_map_identity(isl_space_map_from_set(dim
));
10263 n_div
= set
->p
[0]->n_div
;
10264 dim
= isl_space_map_from_set(dim
);
10265 n_param
= isl_space_dim(dim
, isl_dim_param
);
10266 n_set
= isl_space_dim(dim
, isl_dim_in
);
10267 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10268 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10269 for (i
= 0; i
< n_set
; ++i
)
10270 bmap
= var_equal(bmap
, i
);
10272 total
= n_param
+ n_set
+ n_set
+ n_div
;
10273 for (i
= 0; i
< n_div
; ++i
) {
10274 k
= isl_basic_map_alloc_inequality(bmap
);
10277 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10278 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10279 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10280 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10281 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10282 set
->p
[0]->div
[i
][0]);
10284 l
= isl_basic_map_alloc_inequality(bmap
);
10287 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10288 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10289 set
->p
[0]->div
[i
][0]);
10290 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10294 bmap
= isl_basic_map_simplify(bmap
);
10295 bmap
= isl_basic_map_finalize(bmap
);
10296 return isl_map_from_basic_map(bmap
);
10299 isl_basic_map_free(bmap
);
10303 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10311 dim
= isl_basic_set_total_dim(bset
);
10312 size
+= bset
->n_eq
* (1 + dim
);
10313 size
+= bset
->n_ineq
* (1 + dim
);
10314 size
+= bset
->n_div
* (2 + dim
);
10319 int isl_set_size(__isl_keep isl_set
*set
)
10327 for (i
= 0; i
< set
->n
; ++i
)
10328 size
+= isl_basic_set_size(set
->p
[i
]);
10333 /* Check if there is any lower bound (if lower == 0) and/or upper
10334 * bound (if upper == 0) on the specified dim.
10336 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10337 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10342 return isl_bool_error
;
10344 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10345 return isl_bool_error
);
10347 pos
+= isl_basic_map_offset(bmap
, type
);
10349 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10350 if (isl_int_is_zero(bmap
->div
[i
][0]))
10352 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10353 return isl_bool_true
;
10356 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10357 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10358 return isl_bool_true
;
10360 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10361 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10368 return lower
&& upper
;
10371 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10372 enum isl_dim_type type
, unsigned pos
)
10374 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10377 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10378 enum isl_dim_type type
, unsigned pos
)
10380 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10383 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10384 enum isl_dim_type type
, unsigned pos
)
10386 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10389 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10390 enum isl_dim_type type
, unsigned pos
)
10397 for (i
= 0; i
< map
->n
; ++i
) {
10399 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10400 if (bounded
< 0 || !bounded
)
10407 /* Return 1 if the specified dim is involved in both an upper bound
10408 * and a lower bound.
10410 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10411 enum isl_dim_type type
, unsigned pos
)
10413 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10416 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10418 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10419 enum isl_dim_type type
, unsigned pos
,
10420 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10421 enum isl_dim_type type
, unsigned pos
))
10426 return isl_bool_error
;
10428 for (i
= 0; i
< map
->n
; ++i
) {
10430 bounded
= fn(map
->p
[i
], type
, pos
);
10431 if (bounded
< 0 || bounded
)
10435 return isl_bool_false
;
10438 /* Return 1 if the specified dim is involved in any lower bound.
10440 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10441 enum isl_dim_type type
, unsigned pos
)
10443 return has_any_bound(set
, type
, pos
,
10444 &isl_basic_map_dim_has_lower_bound
);
10447 /* Return 1 if the specified dim is involved in any upper bound.
10449 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10450 enum isl_dim_type type
, unsigned pos
)
10452 return has_any_bound(set
, type
, pos
,
10453 &isl_basic_map_dim_has_upper_bound
);
10456 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10458 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10459 enum isl_dim_type type
, unsigned pos
,
10460 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10461 enum isl_dim_type type
, unsigned pos
))
10466 return isl_bool_error
;
10468 for (i
= 0; i
< map
->n
; ++i
) {
10470 bounded
= fn(map
->p
[i
], type
, pos
);
10471 if (bounded
< 0 || !bounded
)
10475 return isl_bool_true
;
10478 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10480 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10481 enum isl_dim_type type
, unsigned pos
)
10483 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10486 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10488 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10489 enum isl_dim_type type
, unsigned pos
)
10491 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10494 /* For each of the "n" variables starting at "first", determine
10495 * the sign of the variable and put the results in the first "n"
10496 * elements of the array "signs".
10498 * 1 means that the variable is non-negative
10499 * -1 means that the variable is non-positive
10500 * 0 means the variable attains both positive and negative values.
10502 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10503 unsigned first
, unsigned n
, int *signs
)
10505 isl_vec
*bound
= NULL
;
10506 struct isl_tab
*tab
= NULL
;
10507 struct isl_tab_undo
*snap
;
10510 if (!bset
|| !signs
)
10513 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10514 tab
= isl_tab_from_basic_set(bset
, 0);
10515 if (!bound
|| !tab
)
10518 isl_seq_clr(bound
->el
, bound
->size
);
10519 isl_int_set_si(bound
->el
[0], -1);
10521 snap
= isl_tab_snap(tab
);
10522 for (i
= 0; i
< n
; ++i
) {
10525 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10526 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10528 empty
= tab
->empty
;
10529 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10530 if (isl_tab_rollback(tab
, snap
) < 0)
10538 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10539 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10541 empty
= tab
->empty
;
10542 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10543 if (isl_tab_rollback(tab
, snap
) < 0)
10546 signs
[i
] = empty
? -1 : 0;
10550 isl_vec_free(bound
);
10554 isl_vec_free(bound
);
10558 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10559 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10561 if (!bset
|| !signs
)
10563 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10566 first
+= pos(bset
->dim
, type
) - 1;
10567 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10570 /* Is it possible for the integer division "div" to depend (possibly
10571 * indirectly) on any output dimensions?
10573 * If the div is undefined, then we conservatively assume that it
10574 * may depend on them.
10575 * Otherwise, we check if it actually depends on them or on any integer
10576 * divisions that may depend on them.
10578 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10581 unsigned n_out
, o_out
;
10582 unsigned n_div
, o_div
;
10584 if (isl_int_is_zero(bmap
->div
[div
][0]))
10587 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10588 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10590 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10593 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10594 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10596 for (i
= 0; i
< n_div
; ++i
) {
10597 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10599 if (div_may_involve_output(bmap
, i
))
10606 /* Return the first integer division of "bmap" in the range
10607 * [first, first + n[ that may depend on any output dimensions and
10608 * that has a non-zero coefficient in "c" (where the first coefficient
10609 * in "c" corresponds to integer division "first").
10611 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10612 isl_int
*c
, int first
, int n
)
10619 for (k
= first
; k
< first
+ n
; ++k
) {
10620 if (isl_int_is_zero(c
[k
]))
10622 if (div_may_involve_output(bmap
, k
))
10629 /* Look for a pair of inequality constraints in "bmap" of the form
10631 * -l + i >= 0 or i >= l
10633 * n + l - i >= 0 or i <= l + n
10635 * with n < "m" and i the output dimension at position "pos".
10636 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10637 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10638 * and earlier output dimensions, as well as integer divisions that do
10639 * not involve any of the output dimensions.
10641 * Return the index of the first inequality constraint or bmap->n_ineq
10642 * if no such pair can be found.
10644 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10645 int pos
, isl_int m
)
10650 unsigned n_div
, o_div
;
10651 unsigned n_out
, o_out
;
10657 ctx
= isl_basic_map_get_ctx(bmap
);
10658 total
= isl_basic_map_total_dim(bmap
);
10659 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10660 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10661 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10662 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10663 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10664 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10666 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10667 n_out
- (pos
+ 1)) != -1)
10669 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10672 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10673 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10676 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10677 bmap
->ineq
[j
] + 1, total
))
10681 if (j
>= bmap
->n_ineq
)
10683 isl_int_add(bmap
->ineq
[i
][0],
10684 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10685 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10686 isl_int_sub(bmap
->ineq
[i
][0],
10687 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10690 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10696 return bmap
->n_ineq
;
10699 /* Return the index of the equality of "bmap" that defines
10700 * the output dimension "pos" in terms of earlier dimensions.
10701 * The equality may also involve integer divisions, as long
10702 * as those integer divisions are defined in terms of
10703 * parameters or input dimensions.
10704 * In this case, *div is set to the number of integer divisions and
10705 * *ineq is set to the number of inequality constraints (provided
10706 * div and ineq are not NULL).
10708 * The equality may also involve a single integer division involving
10709 * the output dimensions (typically only output dimension "pos") as
10710 * long as the coefficient of output dimension "pos" is 1 or -1 and
10711 * there is a pair of constraints i >= l and i <= l + n, with i referring
10712 * to output dimension "pos", l an expression involving only earlier
10713 * dimensions and n smaller than the coefficient of the integer division
10714 * in the equality. In this case, the output dimension can be defined
10715 * in terms of a modulo expression that does not involve the integer division.
10716 * *div is then set to this single integer division and
10717 * *ineq is set to the index of constraint i >= l.
10719 * Return bmap->n_eq if there is no such equality.
10720 * Return -1 on error.
10722 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10723 int pos
, int *div
, int *ineq
)
10726 unsigned n_out
, o_out
;
10727 unsigned n_div
, o_div
;
10732 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10733 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10734 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10735 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10738 *ineq
= bmap
->n_ineq
;
10741 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10742 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10744 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10745 n_out
- (pos
+ 1)) != -1)
10747 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10751 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10752 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10754 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10755 k
+ 1, n_div
- (k
+1)) < n_div
)
10757 l
= find_modulo_constraint_pair(bmap
, pos
,
10758 bmap
->eq
[j
][o_div
+ k
]);
10761 if (l
>= bmap
->n_ineq
)
10773 /* Check if the given basic map is obviously single-valued.
10774 * In particular, for each output dimension, check that there is
10775 * an equality that defines the output dimension in terms of
10776 * earlier dimensions.
10778 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10784 return isl_bool_error
;
10786 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10788 for (i
= 0; i
< n_out
; ++i
) {
10791 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10794 return isl_bool_error
;
10795 if (eq
>= bmap
->n_eq
)
10796 return isl_bool_false
;
10799 return isl_bool_true
;
10802 /* Check if the given basic map is single-valued.
10803 * We simply compute
10807 * and check if the result is a subset of the identity mapping.
10809 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10812 isl_basic_map
*test
;
10816 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10820 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10821 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10823 space
= isl_basic_map_get_space(bmap
);
10824 space
= isl_space_map_from_set(isl_space_range(space
));
10825 id
= isl_basic_map_identity(space
);
10827 sv
= isl_basic_map_is_subset(test
, id
);
10829 isl_basic_map_free(test
);
10830 isl_basic_map_free(id
);
10835 /* Check if the given map is obviously single-valued.
10837 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10840 return isl_bool_error
;
10842 return isl_bool_true
;
10844 return isl_bool_false
;
10846 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10849 /* Check if the given map is single-valued.
10850 * We simply compute
10854 * and check if the result is a subset of the identity mapping.
10856 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10863 sv
= isl_map_plain_is_single_valued(map
);
10867 test
= isl_map_reverse(isl_map_copy(map
));
10868 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10870 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10871 id
= isl_map_identity(dim
);
10873 sv
= isl_map_is_subset(test
, id
);
10875 isl_map_free(test
);
10881 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10885 map
= isl_map_copy(map
);
10886 map
= isl_map_reverse(map
);
10887 in
= isl_map_is_single_valued(map
);
10893 /* Check if the given map is obviously injective.
10895 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10899 map
= isl_map_copy(map
);
10900 map
= isl_map_reverse(map
);
10901 in
= isl_map_plain_is_single_valued(map
);
10907 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10911 sv
= isl_map_is_single_valued(map
);
10915 return isl_map_is_injective(map
);
10918 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10920 return isl_map_is_single_valued(set_to_map(set
));
10923 /* Does "map" only map elements to themselves?
10925 * If the domain and range spaces are different, then "map"
10926 * is considered not to be an identity relation, even if it is empty.
10927 * Otherwise, construct the maximal identity relation and
10928 * check whether "map" is a subset of this relation.
10930 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10934 isl_bool equal
, is_identity
;
10936 space
= isl_map_get_space(map
);
10937 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10938 isl_space_free(space
);
10939 if (equal
< 0 || !equal
)
10942 id
= isl_map_identity(isl_map_get_space(map
));
10943 is_identity
= isl_map_is_subset(map
, id
);
10946 return is_identity
;
10949 int isl_map_is_translation(__isl_keep isl_map
*map
)
10954 delta
= isl_map_deltas(isl_map_copy(map
));
10955 ok
= isl_set_is_singleton(delta
);
10956 isl_set_free(delta
);
10961 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10963 if (isl_seq_first_non_zero(p
, pos
) != -1)
10965 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10970 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10979 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10982 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10983 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10984 for (j
= 0; j
< nvar
; ++j
) {
10985 int lower
= 0, upper
= 0;
10986 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10987 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10989 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10993 if (i
< bset
->n_eq
)
10995 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10996 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10998 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11000 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11005 if (!lower
|| !upper
)
11012 int isl_set_is_box(__isl_keep isl_set
*set
)
11019 return isl_basic_set_is_box(set
->p
[0]);
11022 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11025 return isl_bool_error
;
11027 return isl_space_is_wrapping(bset
->dim
);
11030 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11033 return isl_bool_error
;
11035 return isl_space_is_wrapping(set
->dim
);
11038 /* Modify the space of "map" through a call to "change".
11039 * If "can_change" is set (not NULL), then first call it to check
11040 * if the modification is allowed, printing the error message "cannot_change"
11043 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11044 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11045 const char *cannot_change
,
11046 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11054 ok
= can_change
? can_change(map
) : isl_bool_true
;
11056 return isl_map_free(map
);
11058 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11059 return isl_map_free(map
));
11061 space
= change(isl_map_get_space(map
));
11062 map
= isl_map_reset_space(map
, space
);
11067 /* Is the domain of "map" a wrapped relation?
11069 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11072 return isl_bool_error
;
11074 return isl_space_domain_is_wrapping(map
->dim
);
11077 /* Is the range of "map" a wrapped relation?
11079 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11082 return isl_bool_error
;
11084 return isl_space_range_is_wrapping(map
->dim
);
11087 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11089 bmap
= isl_basic_map_cow(bmap
);
11093 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11097 bmap
= isl_basic_map_finalize(bmap
);
11099 return bset_from_bmap(bmap
);
11101 isl_basic_map_free(bmap
);
11105 /* Given a map A -> B, return the set (A -> B).
11107 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11109 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11112 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11114 bset
= isl_basic_set_cow(bset
);
11118 bset
->dim
= isl_space_unwrap(bset
->dim
);
11122 bset
= isl_basic_set_finalize(bset
);
11124 return bset_to_bmap(bset
);
11126 isl_basic_set_free(bset
);
11130 /* Given a set (A -> B), return the map A -> B.
11131 * Error out if "set" is not of the form (A -> B).
11133 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11135 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11136 "not a wrapping set", &isl_space_unwrap
);
11139 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11140 enum isl_dim_type type
)
11145 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11148 bmap
= isl_basic_map_cow(bmap
);
11152 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11156 bmap
= isl_basic_map_finalize(bmap
);
11160 isl_basic_map_free(bmap
);
11164 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11165 enum isl_dim_type type
)
11172 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11175 map
= isl_map_cow(map
);
11179 for (i
= 0; i
< map
->n
; ++i
) {
11180 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11184 map
->dim
= isl_space_reset(map
->dim
, type
);
11194 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11199 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11202 bmap
= isl_basic_map_cow(bmap
);
11206 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11210 bmap
= isl_basic_map_finalize(bmap
);
11214 isl_basic_map_free(bmap
);
11218 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11220 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11223 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11224 __isl_take isl_basic_map
*bmap
)
11229 if (!bmap
->dim
->nested
[0])
11232 bmap
= isl_basic_map_cow(bmap
);
11236 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11240 bmap
= isl_basic_map_finalize(bmap
);
11244 isl_basic_map_free(bmap
);
11248 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11249 __isl_take isl_basic_map
*bmap
)
11254 if (!bmap
->dim
->nested
[1])
11257 bmap
= isl_basic_map_cow(bmap
);
11261 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11265 bmap
= isl_basic_map_finalize(bmap
);
11269 isl_basic_map_free(bmap
);
11273 /* Remove any internal structure from the spaces of domain and range of "map".
11275 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11280 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11283 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11286 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11288 return set_from_map(isl_map_flatten(set_to_map(set
)));
11291 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11293 isl_space
*dim
, *flat_dim
;
11296 dim
= isl_set_get_space(set
);
11297 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11298 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11299 map
= isl_map_intersect_domain(map
, set
);
11304 /* Remove any internal structure from the space of the domain of "map".
11306 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11311 if (!map
->dim
->nested
[0])
11314 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11317 /* Remove any internal structure from the space of the range of "map".
11319 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11324 if (!map
->dim
->nested
[1])
11327 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11330 /* Reorder the dimensions of "bmap" according to the given dim_map
11331 * and set the dimension specification to "dim" and
11332 * perform Gaussian elimination on the result.
11334 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11335 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11337 isl_basic_map
*res
;
11340 bmap
= isl_basic_map_cow(bmap
);
11341 if (!bmap
|| !dim
|| !dim_map
)
11344 flags
= bmap
->flags
;
11345 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11346 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11347 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11348 res
= isl_basic_map_alloc_space(dim
,
11349 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11350 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11352 res
->flags
= flags
;
11353 res
= isl_basic_map_gauss(res
, NULL
);
11354 res
= isl_basic_map_finalize(res
);
11358 isl_basic_map_free(bmap
);
11359 isl_space_free(dim
);
11363 /* Reorder the dimensions of "map" according to given reordering.
11365 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11366 __isl_take isl_reordering
*r
)
11369 struct isl_dim_map
*dim_map
;
11371 map
= isl_map_cow(map
);
11372 dim_map
= isl_dim_map_from_reordering(r
);
11373 if (!map
|| !r
|| !dim_map
)
11376 for (i
= 0; i
< map
->n
; ++i
) {
11377 struct isl_dim_map
*dim_map_i
;
11379 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11381 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11382 isl_space_copy(r
->dim
), dim_map_i
);
11388 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11390 isl_reordering_free(r
);
11396 isl_reordering_free(r
);
11400 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11401 __isl_take isl_reordering
*r
)
11403 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11406 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11407 __isl_take isl_space
*model
)
11411 if (!map
|| !model
)
11414 ctx
= isl_space_get_ctx(model
);
11415 if (!isl_space_has_named_params(model
))
11416 isl_die(ctx
, isl_error_invalid
,
11417 "model has unnamed parameters", goto error
);
11418 if (!isl_space_has_named_params(map
->dim
))
11419 isl_die(ctx
, isl_error_invalid
,
11420 "relation has unnamed parameters", goto error
);
11421 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11422 isl_reordering
*exp
;
11424 model
= isl_space_drop_dims(model
, isl_dim_in
,
11425 0, isl_space_dim(model
, isl_dim_in
));
11426 model
= isl_space_drop_dims(model
, isl_dim_out
,
11427 0, isl_space_dim(model
, isl_dim_out
));
11428 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11429 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11430 map
= isl_map_realign(map
, exp
);
11433 isl_space_free(model
);
11436 isl_space_free(model
);
11441 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11442 __isl_take isl_space
*model
)
11444 return isl_map_align_params(set
, model
);
11447 /* Align the parameters of "bmap" to those of "model", introducing
11448 * additional parameters if needed.
11450 __isl_give isl_basic_map
*isl_basic_map_align_params(
11451 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11455 if (!bmap
|| !model
)
11458 ctx
= isl_space_get_ctx(model
);
11459 if (!isl_space_has_named_params(model
))
11460 isl_die(ctx
, isl_error_invalid
,
11461 "model has unnamed parameters", goto error
);
11462 if (!isl_space_has_named_params(bmap
->dim
))
11463 isl_die(ctx
, isl_error_invalid
,
11464 "relation has unnamed parameters", goto error
);
11465 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11466 isl_reordering
*exp
;
11467 struct isl_dim_map
*dim_map
;
11469 model
= isl_space_drop_dims(model
, isl_dim_in
,
11470 0, isl_space_dim(model
, isl_dim_in
));
11471 model
= isl_space_drop_dims(model
, isl_dim_out
,
11472 0, isl_space_dim(model
, isl_dim_out
));
11473 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11474 exp
= isl_reordering_extend_space(exp
,
11475 isl_basic_map_get_space(bmap
));
11476 dim_map
= isl_dim_map_from_reordering(exp
);
11477 bmap
= isl_basic_map_realign(bmap
,
11478 exp
? isl_space_copy(exp
->dim
) : NULL
,
11479 isl_dim_map_extend(dim_map
, bmap
));
11480 isl_reordering_free(exp
);
11484 isl_space_free(model
);
11487 isl_space_free(model
);
11488 isl_basic_map_free(bmap
);
11492 /* Align the parameters of "bset" to those of "model", introducing
11493 * additional parameters if needed.
11495 __isl_give isl_basic_set
*isl_basic_set_align_params(
11496 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11498 return isl_basic_map_align_params(bset
, model
);
11501 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11502 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11503 enum isl_dim_type c2
, enum isl_dim_type c3
,
11504 enum isl_dim_type c4
, enum isl_dim_type c5
)
11506 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11507 struct isl_mat
*mat
;
11513 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11514 isl_basic_map_total_dim(bmap
) + 1);
11517 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11518 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11519 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11520 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11521 isl_int_set(mat
->row
[i
][pos
],
11522 bmap
->eq
[i
][off
+ k
]);
11530 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11531 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11532 enum isl_dim_type c2
, enum isl_dim_type c3
,
11533 enum isl_dim_type c4
, enum isl_dim_type c5
)
11535 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11536 struct isl_mat
*mat
;
11542 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11543 isl_basic_map_total_dim(bmap
) + 1);
11546 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11547 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11548 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11549 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11550 isl_int_set(mat
->row
[i
][pos
],
11551 bmap
->ineq
[i
][off
+ k
]);
11559 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11560 __isl_take isl_space
*dim
,
11561 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11562 enum isl_dim_type c2
, enum isl_dim_type c3
,
11563 enum isl_dim_type c4
, enum isl_dim_type c5
)
11565 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11566 isl_basic_map
*bmap
;
11572 if (!dim
|| !eq
|| !ineq
)
11575 if (eq
->n_col
!= ineq
->n_col
)
11576 isl_die(dim
->ctx
, isl_error_invalid
,
11577 "equalities and inequalities matrices should have "
11578 "same number of columns", goto error
);
11580 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11582 if (eq
->n_col
< total
)
11583 isl_die(dim
->ctx
, isl_error_invalid
,
11584 "number of columns too small", goto error
);
11586 extra
= eq
->n_col
- total
;
11588 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11589 eq
->n_row
, ineq
->n_row
);
11592 for (i
= 0; i
< extra
; ++i
) {
11593 k
= isl_basic_map_alloc_div(bmap
);
11596 isl_int_set_si(bmap
->div
[k
][0], 0);
11598 for (i
= 0; i
< eq
->n_row
; ++i
) {
11599 l
= isl_basic_map_alloc_equality(bmap
);
11602 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11603 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11604 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11605 isl_int_set(bmap
->eq
[l
][off
+ k
],
11611 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11612 l
= isl_basic_map_alloc_inequality(bmap
);
11615 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11616 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11617 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11618 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11619 ineq
->row
[i
][pos
]);
11625 isl_space_free(dim
);
11627 isl_mat_free(ineq
);
11629 bmap
= isl_basic_map_simplify(bmap
);
11630 return isl_basic_map_finalize(bmap
);
11632 isl_space_free(dim
);
11634 isl_mat_free(ineq
);
11638 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11639 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11640 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11642 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11643 c1
, c2
, c3
, c4
, isl_dim_in
);
11646 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11647 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11648 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11650 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11651 c1
, c2
, c3
, c4
, isl_dim_in
);
11654 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11655 __isl_take isl_space
*dim
,
11656 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11657 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11659 isl_basic_map
*bmap
;
11660 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11661 c1
, c2
, c3
, c4
, isl_dim_in
);
11662 return bset_from_bmap(bmap
);
11665 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11668 return isl_bool_error
;
11670 return isl_space_can_zip(bmap
->dim
);
11673 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11676 return isl_bool_error
;
11678 return isl_space_can_zip(map
->dim
);
11681 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11682 * (A -> C) -> (B -> D).
11684 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11693 if (!isl_basic_map_can_zip(bmap
))
11694 isl_die(bmap
->ctx
, isl_error_invalid
,
11695 "basic map cannot be zipped", goto error
);
11696 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11697 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11698 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11699 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11700 bmap
= isl_basic_map_cow(bmap
);
11701 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11704 bmap
->dim
= isl_space_zip(bmap
->dim
);
11707 bmap
= isl_basic_map_mark_final(bmap
);
11710 isl_basic_map_free(bmap
);
11714 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11715 * (A -> C) -> (B -> D).
11717 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11724 if (!isl_map_can_zip(map
))
11725 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11728 map
= isl_map_cow(map
);
11732 for (i
= 0; i
< map
->n
; ++i
) {
11733 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11738 map
->dim
= isl_space_zip(map
->dim
);
11748 /* Can we apply isl_basic_map_curry to "bmap"?
11749 * That is, does it have a nested relation in its domain?
11751 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11754 return isl_bool_error
;
11756 return isl_space_can_curry(bmap
->dim
);
11759 /* Can we apply isl_map_curry to "map"?
11760 * That is, does it have a nested relation in its domain?
11762 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11765 return isl_bool_error
;
11767 return isl_space_can_curry(map
->dim
);
11770 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11773 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11779 if (!isl_basic_map_can_curry(bmap
))
11780 isl_die(bmap
->ctx
, isl_error_invalid
,
11781 "basic map cannot be curried", goto error
);
11782 bmap
= isl_basic_map_cow(bmap
);
11785 bmap
->dim
= isl_space_curry(bmap
->dim
);
11788 bmap
= isl_basic_map_mark_final(bmap
);
11791 isl_basic_map_free(bmap
);
11795 /* Given a map (A -> B) -> C, return the corresponding map
11798 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11800 return isl_map_change_space(map
, &isl_map_can_curry
,
11801 "map cannot be curried", &isl_space_curry
);
11804 /* Can isl_map_range_curry be applied to "map"?
11805 * That is, does it have a nested relation in its range,
11806 * the domain of which is itself a nested relation?
11808 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11811 return isl_bool_error
;
11813 return isl_space_can_range_curry(map
->dim
);
11816 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11817 * A -> (B -> (C -> D)).
11819 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11821 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11822 "map range cannot be curried",
11823 &isl_space_range_curry
);
11826 /* Can we apply isl_basic_map_uncurry to "bmap"?
11827 * That is, does it have a nested relation in its domain?
11829 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11832 return isl_bool_error
;
11834 return isl_space_can_uncurry(bmap
->dim
);
11837 /* Can we apply isl_map_uncurry to "map"?
11838 * That is, does it have a nested relation in its domain?
11840 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11843 return isl_bool_error
;
11845 return isl_space_can_uncurry(map
->dim
);
11848 /* Given a basic map A -> (B -> C), return the corresponding basic map
11851 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11857 if (!isl_basic_map_can_uncurry(bmap
))
11858 isl_die(bmap
->ctx
, isl_error_invalid
,
11859 "basic map cannot be uncurried",
11860 return isl_basic_map_free(bmap
));
11861 bmap
= isl_basic_map_cow(bmap
);
11864 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11866 return isl_basic_map_free(bmap
);
11867 bmap
= isl_basic_map_mark_final(bmap
);
11871 /* Given a map A -> (B -> C), return the corresponding map
11874 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11876 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11877 "map cannot be uncurried", &isl_space_uncurry
);
11880 /* Construct a basic map mapping the domain of the affine expression
11881 * to a one-dimensional range prescribed by the affine expression.
11882 * If "rational" is set, then construct a rational basic map.
11884 * A NaN affine expression cannot be converted to a basic map.
11886 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11887 __isl_take isl_aff
*aff
, int rational
)
11892 isl_local_space
*ls
;
11893 isl_basic_map
*bmap
= NULL
;
11897 is_nan
= isl_aff_is_nan(aff
);
11901 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11902 "cannot convert NaN", goto error
);
11904 ls
= isl_aff_get_local_space(aff
);
11905 bmap
= isl_basic_map_from_local_space(ls
);
11906 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11907 k
= isl_basic_map_alloc_equality(bmap
);
11911 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11912 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11913 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11914 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11915 aff
->v
->size
- (pos
+ 1));
11919 bmap
= isl_basic_map_set_rational(bmap
);
11920 bmap
= isl_basic_map_finalize(bmap
);
11924 isl_basic_map_free(bmap
);
11928 /* Construct a basic map mapping the domain of the affine expression
11929 * to a one-dimensional range prescribed by the affine expression.
11931 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11933 return isl_basic_map_from_aff2(aff
, 0);
11936 /* Construct a map mapping the domain of the affine expression
11937 * to a one-dimensional range prescribed by the affine expression.
11939 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11941 isl_basic_map
*bmap
;
11943 bmap
= isl_basic_map_from_aff(aff
);
11944 return isl_map_from_basic_map(bmap
);
11947 /* Construct a basic map mapping the domain the multi-affine expression
11948 * to its range, with each dimension in the range equated to the
11949 * corresponding affine expression.
11950 * If "rational" is set, then construct a rational basic map.
11952 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11953 __isl_take isl_multi_aff
*maff
, int rational
)
11957 isl_basic_map
*bmap
;
11962 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11963 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11964 "invalid space", goto error
);
11966 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11967 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11969 bmap
= isl_basic_map_set_rational(bmap
);
11971 for (i
= 0; i
< maff
->n
; ++i
) {
11973 isl_basic_map
*bmap_i
;
11975 aff
= isl_aff_copy(maff
->p
[i
]);
11976 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11978 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11981 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11983 isl_multi_aff_free(maff
);
11986 isl_multi_aff_free(maff
);
11990 /* Construct a basic map mapping the domain the multi-affine expression
11991 * to its range, with each dimension in the range equated to the
11992 * corresponding affine expression.
11994 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11995 __isl_take isl_multi_aff
*ma
)
11997 return isl_basic_map_from_multi_aff2(ma
, 0);
12000 /* Construct a map mapping the domain the multi-affine expression
12001 * to its range, with each dimension in the range equated to the
12002 * corresponding affine expression.
12004 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12006 isl_basic_map
*bmap
;
12008 bmap
= isl_basic_map_from_multi_aff(maff
);
12009 return isl_map_from_basic_map(bmap
);
12012 /* Construct a basic map mapping a domain in the given space to
12013 * to an n-dimensional range, with n the number of elements in the list,
12014 * where each coordinate in the range is prescribed by the
12015 * corresponding affine expression.
12016 * The domains of all affine expressions in the list are assumed to match
12019 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12020 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12024 isl_basic_map
*bmap
;
12029 dim
= isl_space_from_domain(domain_dim
);
12030 bmap
= isl_basic_map_universe(dim
);
12032 for (i
= 0; i
< list
->n
; ++i
) {
12034 isl_basic_map
*bmap_i
;
12036 aff
= isl_aff_copy(list
->p
[i
]);
12037 bmap_i
= isl_basic_map_from_aff(aff
);
12039 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12042 isl_aff_list_free(list
);
12046 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12047 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12049 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12052 /* Construct a basic map where the given dimensions are equal to each other.
12054 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12055 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12057 isl_basic_map
*bmap
= NULL
;
12063 if (pos1
>= isl_space_dim(space
, type1
))
12064 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12065 "index out of bounds", goto error
);
12066 if (pos2
>= isl_space_dim(space
, type2
))
12067 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12068 "index out of bounds", goto error
);
12070 if (type1
== type2
&& pos1
== pos2
)
12071 return isl_basic_map_universe(space
);
12073 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12074 i
= isl_basic_map_alloc_equality(bmap
);
12077 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12078 pos1
+= isl_basic_map_offset(bmap
, type1
);
12079 pos2
+= isl_basic_map_offset(bmap
, type2
);
12080 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12081 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12082 bmap
= isl_basic_map_finalize(bmap
);
12083 isl_space_free(space
);
12086 isl_space_free(space
);
12087 isl_basic_map_free(bmap
);
12091 /* Add a constraint imposing that the given two dimensions are equal.
12093 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12094 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12098 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12100 bmap
= isl_basic_map_intersect(bmap
, eq
);
12105 /* Add a constraint imposing that the given two dimensions are equal.
12107 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12108 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12110 isl_basic_map
*bmap
;
12112 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12114 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12119 /* Add a constraint imposing that the given two dimensions have opposite values.
12121 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12122 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12124 isl_basic_map
*bmap
= NULL
;
12130 if (pos1
>= isl_map_dim(map
, type1
))
12131 isl_die(map
->ctx
, isl_error_invalid
,
12132 "index out of bounds", goto error
);
12133 if (pos2
>= isl_map_dim(map
, type2
))
12134 isl_die(map
->ctx
, isl_error_invalid
,
12135 "index out of bounds", goto error
);
12137 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12138 i
= isl_basic_map_alloc_equality(bmap
);
12141 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12142 pos1
+= isl_basic_map_offset(bmap
, type1
);
12143 pos2
+= isl_basic_map_offset(bmap
, type2
);
12144 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12145 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12146 bmap
= isl_basic_map_finalize(bmap
);
12148 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12152 isl_basic_map_free(bmap
);
12157 /* Construct a constraint imposing that the value of the first dimension is
12158 * greater than or equal to that of the second.
12160 static __isl_give isl_constraint
*constraint_order_ge(
12161 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12162 enum isl_dim_type type2
, int pos2
)
12169 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12171 if (pos1
>= isl_constraint_dim(c
, type1
))
12172 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12173 "index out of bounds", return isl_constraint_free(c
));
12174 if (pos2
>= isl_constraint_dim(c
, type2
))
12175 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12176 "index out of bounds", return isl_constraint_free(c
));
12178 if (type1
== type2
&& pos1
== pos2
)
12181 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12182 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12187 /* Add a constraint imposing that the value of the first dimension is
12188 * greater than or equal to that of the second.
12190 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12191 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12196 if (type1
== type2
&& pos1
== pos2
)
12198 space
= isl_basic_map_get_space(bmap
);
12199 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12200 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12205 /* Add a constraint imposing that the value of the first dimension is
12206 * greater than or equal to that of the second.
12208 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12209 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12214 if (type1
== type2
&& pos1
== pos2
)
12216 space
= isl_map_get_space(map
);
12217 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12218 map
= isl_map_add_constraint(map
, c
);
12223 /* Add a constraint imposing that the value of the first dimension is
12224 * less than or equal to that of the second.
12226 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12227 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12229 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12232 /* Construct a basic map where the value of the first dimension is
12233 * greater than that of the second.
12235 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12236 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12238 isl_basic_map
*bmap
= NULL
;
12244 if (pos1
>= isl_space_dim(space
, type1
))
12245 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12246 "index out of bounds", goto error
);
12247 if (pos2
>= isl_space_dim(space
, type2
))
12248 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12249 "index out of bounds", goto error
);
12251 if (type1
== type2
&& pos1
== pos2
)
12252 return isl_basic_map_empty(space
);
12254 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12255 i
= isl_basic_map_alloc_inequality(bmap
);
12257 return isl_basic_map_free(bmap
);
12258 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12259 pos1
+= isl_basic_map_offset(bmap
, type1
);
12260 pos2
+= isl_basic_map_offset(bmap
, type2
);
12261 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12262 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12263 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12264 bmap
= isl_basic_map_finalize(bmap
);
12268 isl_space_free(space
);
12269 isl_basic_map_free(bmap
);
12273 /* Add a constraint imposing that the value of the first dimension is
12274 * greater than that of the second.
12276 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12277 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12281 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12283 bmap
= isl_basic_map_intersect(bmap
, gt
);
12288 /* Add a constraint imposing that the value of the first dimension is
12289 * greater than that of the second.
12291 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12292 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12294 isl_basic_map
*bmap
;
12296 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12298 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12303 /* Add a constraint imposing that the value of the first dimension is
12304 * smaller than that of the second.
12306 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12307 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12309 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12312 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12316 isl_local_space
*ls
;
12321 if (!isl_basic_map_divs_known(bmap
))
12322 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12323 "some divs are unknown", return NULL
);
12325 ls
= isl_basic_map_get_local_space(bmap
);
12326 div
= isl_local_space_get_div(ls
, pos
);
12327 isl_local_space_free(ls
);
12332 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12335 return isl_basic_map_get_div(bset
, pos
);
12338 /* Plug in "subs" for dimension "type", "pos" of "bset".
12340 * Let i be the dimension to replace and let "subs" be of the form
12344 * Any integer division with a non-zero coefficient for i,
12346 * floor((a i + g)/m)
12350 * floor((a f + d g)/(m d))
12352 * Constraints of the form
12360 * We currently require that "subs" is an integral expression.
12361 * Handling rational expressions may require us to add stride constraints
12362 * as we do in isl_basic_set_preimage_multi_aff.
12364 __isl_give isl_basic_set
*isl_basic_set_substitute(
12365 __isl_take isl_basic_set
*bset
,
12366 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12372 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12375 bset
= isl_basic_set_cow(bset
);
12376 if (!bset
|| !subs
)
12379 ctx
= isl_basic_set_get_ctx(bset
);
12380 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12381 isl_die(ctx
, isl_error_invalid
,
12382 "spaces don't match", goto error
);
12383 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12384 isl_die(ctx
, isl_error_unsupported
,
12385 "cannot handle divs yet", goto error
);
12386 if (!isl_int_is_one(subs
->v
->el
[0]))
12387 isl_die(ctx
, isl_error_invalid
,
12388 "can only substitute integer expressions", goto error
);
12390 pos
+= isl_basic_set_offset(bset
, type
);
12394 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12395 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12397 isl_int_set(v
, bset
->eq
[i
][pos
]);
12398 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12399 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12400 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12403 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12404 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12406 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12407 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12408 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12409 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12412 for (i
= 0; i
< bset
->n_div
; ++i
) {
12413 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12415 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12416 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12417 isl_seq_combine(bset
->div
[i
] + 1,
12418 subs
->v
->el
[0], bset
->div
[i
] + 1,
12419 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12420 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12425 bset
= isl_basic_set_simplify(bset
);
12426 return isl_basic_set_finalize(bset
);
12428 isl_basic_set_free(bset
);
12432 /* Plug in "subs" for dimension "type", "pos" of "set".
12434 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12435 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12439 if (set
&& isl_set_plain_is_empty(set
))
12442 set
= isl_set_cow(set
);
12446 for (i
= set
->n
- 1; i
>= 0; --i
) {
12447 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12448 if (remove_if_empty(set
, i
) < 0)
12458 /* Check if the range of "ma" is compatible with the domain or range
12459 * (depending on "type") of "bmap".
12460 * Return -1 if anything is wrong.
12462 static int check_basic_map_compatible_range_multi_aff(
12463 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12464 __isl_keep isl_multi_aff
*ma
)
12467 isl_space
*ma_space
;
12469 ma_space
= isl_multi_aff_get_space(ma
);
12471 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12475 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12476 "parameters don't match", goto error
);
12477 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12481 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12482 "spaces don't match", goto error
);
12484 isl_space_free(ma_space
);
12487 isl_space_free(ma_space
);
12491 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12492 * coefficients before the transformed range of dimensions,
12493 * the "n_after" coefficients after the transformed range of dimensions
12494 * and the coefficients of the other divs in "bmap".
12496 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12497 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12502 isl_local_space
*ls
;
12507 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12511 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12512 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12513 for (i
= 0; i
< n_div
; ++i
) {
12514 int o_bmap
= 0, o_ls
= 0;
12516 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12517 o_bmap
+= 1 + 1 + n_param
;
12518 o_ls
+= 1 + 1 + n_param
;
12519 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12520 o_bmap
+= n_before
;
12521 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12522 ls
->div
->row
[i
] + o_ls
, n_set
);
12525 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12527 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12528 ls
->div
->row
[i
] + o_ls
, n_div
);
12531 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12532 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12536 isl_local_space_free(ls
);
12539 isl_local_space_free(ls
);
12543 /* How many stride constraints does "ma" enforce?
12544 * That is, how many of the affine expressions have a denominator
12545 * different from one?
12547 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12552 for (i
= 0; i
< ma
->n
; ++i
)
12553 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12559 /* For each affine expression in ma of the form
12561 * x_i = (f_i y + h_i)/m_i
12563 * with m_i different from one, add a constraint to "bmap"
12566 * f_i y + h_i = m_i alpha_i
12568 * with alpha_i an additional existentially quantified variable.
12570 * The input variables of "ma" correspond to a subset of the variables
12571 * of "bmap". There are "n_before" variables in "bmap" before this
12572 * subset and "n_after" variables after this subset.
12573 * The integer divisions of the affine expressions in "ma" are assumed
12574 * to have been aligned. There are "n_div_ma" of them and
12575 * they appear first in "bmap", straight after the "n_after" variables.
12577 static __isl_give isl_basic_map
*add_ma_strides(
12578 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12579 int n_before
, int n_after
, int n_div_ma
)
12587 total
= isl_basic_map_total_dim(bmap
);
12588 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12589 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12590 for (i
= 0; i
< ma
->n
; ++i
) {
12591 int o_bmap
= 0, o_ma
= 1;
12593 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12595 div
= isl_basic_map_alloc_div(bmap
);
12596 k
= isl_basic_map_alloc_equality(bmap
);
12597 if (div
< 0 || k
< 0)
12599 isl_int_set_si(bmap
->div
[div
][0], 0);
12600 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12601 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12602 o_bmap
+= 1 + n_param
;
12603 o_ma
+= 1 + n_param
;
12604 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12605 o_bmap
+= n_before
;
12606 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12607 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12610 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12612 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12613 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12614 o_bmap
+= n_div_ma
;
12616 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12617 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12623 isl_basic_map_free(bmap
);
12627 /* Replace the domain or range space (depending on "type) of "space" by "set".
12629 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12630 enum isl_dim_type type
, __isl_take isl_space
*set
)
12632 if (type
== isl_dim_in
) {
12633 space
= isl_space_range(space
);
12634 space
= isl_space_map_from_domain_and_range(set
, space
);
12636 space
= isl_space_domain(space
);
12637 space
= isl_space_map_from_domain_and_range(space
, set
);
12643 /* Compute the preimage of the domain or range (depending on "type")
12644 * of "bmap" under the function represented by "ma".
12645 * In other words, plug in "ma" in the domain or range of "bmap".
12646 * The result is a basic map that lives in the same space as "bmap"
12647 * except that the domain or range has been replaced by
12648 * the domain space of "ma".
12650 * If bmap is represented by
12652 * A(p) + S u + B x + T v + C(divs) >= 0,
12654 * where u and x are input and output dimensions if type == isl_dim_out
12655 * while x and v are input and output dimensions if type == isl_dim_in,
12656 * and ma is represented by
12658 * x = D(p) + F(y) + G(divs')
12660 * then the result is
12662 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12664 * The divs in the input set are similarly adjusted.
12667 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12671 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12672 * B_i G(divs') + c_i(divs))/n_i)
12674 * If bmap is not a rational map and if F(y) involves any denominators
12676 * x_i = (f_i y + h_i)/m_i
12678 * then additional constraints are added to ensure that we only
12679 * map back integer points. That is we enforce
12681 * f_i y + h_i = m_i alpha_i
12683 * with alpha_i an additional existentially quantified variable.
12685 * We first copy over the divs from "ma".
12686 * Then we add the modified constraints and divs from "bmap".
12687 * Finally, we add the stride constraints, if needed.
12689 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12690 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12691 __isl_take isl_multi_aff
*ma
)
12695 isl_basic_map
*res
= NULL
;
12696 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12697 isl_int f
, c1
, c2
, g
;
12698 int rational
, strides
;
12705 ma
= isl_multi_aff_align_divs(ma
);
12708 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12711 if (type
== isl_dim_in
) {
12713 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12715 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12718 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12719 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12721 space
= isl_multi_aff_get_domain_space(ma
);
12722 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12723 rational
= isl_basic_map_is_rational(bmap
);
12724 strides
= rational
? 0 : multi_aff_strides(ma
);
12725 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12726 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12728 res
= isl_basic_map_set_rational(res
);
12730 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12731 if (isl_basic_map_alloc_div(res
) < 0)
12734 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12737 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12738 k
= isl_basic_map_alloc_equality(res
);
12741 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12742 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12745 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12746 k
= isl_basic_map_alloc_inequality(res
);
12749 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12750 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12753 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12754 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12755 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12758 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12759 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12764 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12770 isl_basic_map_free(bmap
);
12771 isl_multi_aff_free(ma
);
12772 res
= isl_basic_set_simplify(res
);
12773 return isl_basic_map_finalize(res
);
12779 isl_basic_map_free(bmap
);
12780 isl_multi_aff_free(ma
);
12781 isl_basic_map_free(res
);
12785 /* Compute the preimage of "bset" under the function represented by "ma".
12786 * In other words, plug in "ma" in "bset". The result is a basic set
12787 * that lives in the domain space of "ma".
12789 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12790 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12792 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12795 /* Compute the preimage of the domain of "bmap" under the function
12796 * represented by "ma".
12797 * In other words, plug in "ma" in the domain of "bmap".
12798 * The result is a basic map that lives in the same space as "bmap"
12799 * except that the domain has been replaced by the domain space of "ma".
12801 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12802 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12804 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12807 /* Compute the preimage of the range of "bmap" under the function
12808 * represented by "ma".
12809 * In other words, plug in "ma" in the range of "bmap".
12810 * The result is a basic map that lives in the same space as "bmap"
12811 * except that the range has been replaced by the domain space of "ma".
12813 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12814 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12816 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12819 /* Check if the range of "ma" is compatible with the domain or range
12820 * (depending on "type") of "map".
12821 * Return -1 if anything is wrong.
12823 static int check_map_compatible_range_multi_aff(
12824 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12825 __isl_keep isl_multi_aff
*ma
)
12828 isl_space
*ma_space
;
12830 ma_space
= isl_multi_aff_get_space(ma
);
12831 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12832 isl_space_free(ma_space
);
12834 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12835 "spaces don't match", return -1);
12839 /* Compute the preimage of the domain or range (depending on "type")
12840 * of "map" under the function represented by "ma".
12841 * In other words, plug in "ma" in the domain or range of "map".
12842 * The result is a map that lives in the same space as "map"
12843 * except that the domain or range has been replaced by
12844 * the domain space of "ma".
12846 * The parameters are assumed to have been aligned.
12848 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12849 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12854 map
= isl_map_cow(map
);
12855 ma
= isl_multi_aff_align_divs(ma
);
12858 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12861 for (i
= 0; i
< map
->n
; ++i
) {
12862 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12863 isl_multi_aff_copy(ma
));
12868 space
= isl_multi_aff_get_domain_space(ma
);
12869 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12871 isl_space_free(map
->dim
);
12876 isl_multi_aff_free(ma
);
12878 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12879 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12882 isl_multi_aff_free(ma
);
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 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12895 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12900 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12901 return map_preimage_multi_aff(map
, type
, ma
);
12903 if (!isl_space_has_named_params(map
->dim
) ||
12904 !isl_space_has_named_params(ma
->space
))
12905 isl_die(map
->ctx
, isl_error_invalid
,
12906 "unaligned unnamed parameters", goto error
);
12907 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12908 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12910 return map_preimage_multi_aff(map
, type
, ma
);
12912 isl_multi_aff_free(ma
);
12913 return isl_map_free(map
);
12916 /* Compute the preimage of "set" under the function represented by "ma".
12917 * In other words, plug in "ma" in "set". The result is a set
12918 * that lives in the domain space of "ma".
12920 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12921 __isl_take isl_multi_aff
*ma
)
12923 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12926 /* Compute the preimage of the domain of "map" under the function
12927 * represented by "ma".
12928 * In other words, plug in "ma" in the domain of "map".
12929 * The result is a map that lives in the same space as "map"
12930 * except that the domain has been replaced by the domain space of "ma".
12932 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12933 __isl_take isl_multi_aff
*ma
)
12935 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12938 /* Compute the preimage of the range of "map" under the function
12939 * represented by "ma".
12940 * In other words, plug in "ma" in the range of "map".
12941 * The result is a map that lives in the same space as "map"
12942 * except that the range has been replaced by the domain space of "ma".
12944 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12945 __isl_take isl_multi_aff
*ma
)
12947 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12950 /* Compute the preimage of "map" under the function represented by "pma".
12951 * In other words, plug in "pma" in the domain or range of "map".
12952 * The result is a map that lives in the same space as "map",
12953 * except that the space of type "type" has been replaced by
12954 * the domain space of "pma".
12956 * The parameters of "map" and "pma" are assumed to have been aligned.
12958 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12959 __isl_take isl_map
*map
, enum isl_dim_type type
,
12960 __isl_take isl_pw_multi_aff
*pma
)
12969 isl_pw_multi_aff_free(pma
);
12970 res
= isl_map_empty(isl_map_get_space(map
));
12975 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12976 isl_multi_aff_copy(pma
->p
[0].maff
));
12977 if (type
== isl_dim_in
)
12978 res
= isl_map_intersect_domain(res
,
12979 isl_map_copy(pma
->p
[0].set
));
12981 res
= isl_map_intersect_range(res
,
12982 isl_map_copy(pma
->p
[0].set
));
12984 for (i
= 1; i
< pma
->n
; ++i
) {
12987 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12988 isl_multi_aff_copy(pma
->p
[i
].maff
));
12989 if (type
== isl_dim_in
)
12990 res_i
= isl_map_intersect_domain(res_i
,
12991 isl_map_copy(pma
->p
[i
].set
));
12993 res_i
= isl_map_intersect_range(res_i
,
12994 isl_map_copy(pma
->p
[i
].set
));
12995 res
= isl_map_union(res
, res_i
);
12998 isl_pw_multi_aff_free(pma
);
13002 isl_pw_multi_aff_free(pma
);
13007 /* Compute the preimage of "map" under the function represented by "pma".
13008 * In other words, plug in "pma" in the domain or range of "map".
13009 * The result is a map that lives in the same space as "map",
13010 * except that the space of type "type" has been replaced by
13011 * the domain space of "pma".
13013 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13014 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13019 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13020 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13022 if (!isl_space_has_named_params(map
->dim
) ||
13023 !isl_space_has_named_params(pma
->dim
))
13024 isl_die(map
->ctx
, isl_error_invalid
,
13025 "unaligned unnamed parameters", goto error
);
13026 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13027 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13029 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13031 isl_pw_multi_aff_free(pma
);
13032 return isl_map_free(map
);
13035 /* Compute the preimage of "set" under the function represented by "pma".
13036 * In other words, plug in "pma" in "set". The result is a set
13037 * that lives in the domain space of "pma".
13039 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13040 __isl_take isl_pw_multi_aff
*pma
)
13042 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13045 /* Compute the preimage of the domain of "map" under the function
13046 * represented by "pma".
13047 * In other words, plug in "pma" in the domain of "map".
13048 * The result is a map that lives in the same space as "map",
13049 * except that domain space has been replaced by the domain space of "pma".
13051 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13052 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13054 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13057 /* Compute the preimage of the range of "map" under the function
13058 * represented by "pma".
13059 * In other words, plug in "pma" in the range of "map".
13060 * The result is a map that lives in the same space as "map",
13061 * except that range space has been replaced by the domain space of "pma".
13063 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13064 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13066 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13069 /* Compute the preimage of "map" under the function represented by "mpa".
13070 * In other words, plug in "mpa" in the domain or range of "map".
13071 * The result is a map that lives in the same space as "map",
13072 * except that the space of type "type" has been replaced by
13073 * the domain space of "mpa".
13075 * If the map does not involve any constraints that refer to the
13076 * dimensions of the substituted space, then the only possible
13077 * effect of "mpa" on the map is to map the space to a different space.
13078 * We create a separate isl_multi_aff to effectuate this change
13079 * in order to avoid spurious splitting of the map along the pieces
13082 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13083 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13086 isl_pw_multi_aff
*pma
;
13091 n
= isl_map_dim(map
, type
);
13092 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13096 space
= isl_multi_pw_aff_get_space(mpa
);
13097 isl_multi_pw_aff_free(mpa
);
13098 ma
= isl_multi_aff_zero(space
);
13099 return isl_map_preimage_multi_aff(map
, type
, ma
);
13102 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13103 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13106 isl_multi_pw_aff_free(mpa
);
13110 /* Compute the preimage of "map" under the function represented by "mpa".
13111 * In other words, plug in "mpa" in the domain "map".
13112 * The result is a map that lives in the same space as "map",
13113 * except that domain space has been replaced by the domain space of "mpa".
13115 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13116 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13118 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13121 /* Compute the preimage of "set" by the function represented by "mpa".
13122 * In other words, plug in "mpa" in "set".
13124 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13125 __isl_take isl_multi_pw_aff
*mpa
)
13127 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);