2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_vec_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
37 #include <isl/deprecated/map_int.h>
38 #include <isl/deprecated/set_int.h>
40 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
43 case isl_dim_param
: return dim
->nparam
;
44 case isl_dim_in
: return dim
->n_in
;
45 case isl_dim_out
: return dim
->n_out
;
46 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
51 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
54 case isl_dim_param
: return 1;
55 case isl_dim_in
: return 1 + dim
->nparam
;
56 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
61 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
62 enum isl_dim_type type
)
67 case isl_dim_cst
: return 1;
70 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
71 case isl_dim_div
: return bmap
->n_div
;
72 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
77 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
79 return map
? n(map
->dim
, type
) : 0;
82 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
84 return set
? n(set
->dim
, type
) : 0;
87 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
88 enum isl_dim_type type
)
90 isl_space
*dim
= bmap
->dim
;
92 case isl_dim_cst
: return 0;
93 case isl_dim_param
: return 1;
94 case isl_dim_in
: return 1 + dim
->nparam
;
95 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
96 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
101 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
102 enum isl_dim_type type
)
104 return isl_basic_map_offset(bset
, type
);
107 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
109 return pos(map
->dim
, type
);
112 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
113 enum isl_dim_type type
)
115 return isl_basic_map_dim(bset
, type
);
118 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
120 return isl_basic_set_dim(bset
, isl_dim_set
);
123 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
125 return isl_basic_set_dim(bset
, isl_dim_param
);
128 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
132 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
135 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
137 return isl_set_dim(set
, isl_dim_set
);
140 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
142 return isl_set_dim(set
, isl_dim_param
);
145 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
147 return bmap
? bmap
->dim
->n_in
: 0;
150 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
152 return bmap
? bmap
->dim
->n_out
: 0;
155 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
157 return bmap
? bmap
->dim
->nparam
: 0;
160 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
162 return bmap
? bmap
->n_div
: 0;
165 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
167 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
170 unsigned isl_map_n_in(const struct isl_map
*map
)
172 return map
? map
->dim
->n_in
: 0;
175 unsigned isl_map_n_out(const struct isl_map
*map
)
177 return map
? map
->dim
->n_out
: 0;
180 unsigned isl_map_n_param(const struct isl_map
*map
)
182 return map
? map
->dim
->nparam
: 0;
185 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
190 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
193 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
196 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
197 struct isl_basic_set
*bset
)
202 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
205 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
208 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
213 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
216 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
219 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
220 struct isl_basic_set
*bset
)
225 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
228 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
231 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
233 return bmap
? bmap
->ctx
: NULL
;
236 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
238 return bset
? bset
->ctx
: NULL
;
241 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
243 return map
? map
->ctx
: NULL
;
246 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
248 return set
? set
->ctx
: NULL
;
251 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
255 return isl_space_copy(bmap
->dim
);
258 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
262 return isl_space_copy(bset
->dim
);
265 /* Extract the divs in "bmap" as a matrix.
267 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
278 ctx
= isl_basic_map_get_ctx(bmap
);
279 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
280 cols
= 1 + 1 + total
+ bmap
->n_div
;
281 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
285 for (i
= 0; i
< bmap
->n_div
; ++i
)
286 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
291 /* Extract the divs in "bset" as a matrix.
293 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
295 return isl_basic_map_get_divs(bset
);
298 __isl_give isl_local_space
*isl_basic_map_get_local_space(
299 __isl_keep isl_basic_map
*bmap
)
306 div
= isl_basic_map_get_divs(bmap
);
307 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
310 __isl_give isl_local_space
*isl_basic_set_get_local_space(
311 __isl_keep isl_basic_set
*bset
)
313 return isl_basic_map_get_local_space(bset
);
316 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
317 __isl_take isl_local_space
*ls
)
326 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
327 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
328 n_div
, 0, 2 * n_div
);
330 for (i
= 0; i
< n_div
; ++i
)
331 if (isl_basic_map_alloc_div(bmap
) < 0)
334 for (i
= 0; i
< n_div
; ++i
) {
335 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
336 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
340 isl_local_space_free(ls
);
343 isl_local_space_free(ls
);
344 isl_basic_map_free(bmap
);
348 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
349 __isl_take isl_local_space
*ls
)
351 return isl_basic_map_from_local_space(ls
);
354 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
358 return isl_space_copy(map
->dim
);
361 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
365 return isl_space_copy(set
->dim
);
368 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
369 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
371 bmap
= isl_basic_map_cow(bmap
);
374 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
377 bmap
= isl_basic_map_finalize(bmap
);
380 isl_basic_map_free(bmap
);
384 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
385 __isl_take isl_basic_set
*bset
, const char *s
)
387 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
390 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
391 enum isl_dim_type type
)
393 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
396 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
397 enum isl_dim_type type
, const char *s
)
401 map
= isl_map_cow(map
);
405 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
409 for (i
= 0; i
< map
->n
; ++i
) {
410 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
421 /* Does the input or output tuple have a name?
423 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
425 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
428 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
429 enum isl_dim_type type
)
431 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
434 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
437 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
440 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
441 enum isl_dim_type type
, __isl_take isl_id
*id
)
443 map
= isl_map_cow(map
);
445 return isl_id_free(id
);
447 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
449 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
452 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
453 __isl_take isl_id
*id
)
455 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
458 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
459 enum isl_dim_type type
)
461 map
= isl_map_cow(map
);
465 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
467 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
470 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
472 return isl_map_reset_tuple_id(set
, isl_dim_set
);
475 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
477 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
480 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
481 enum isl_dim_type type
)
483 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
486 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
488 return isl_map_has_tuple_id(set
, isl_dim_set
);
491 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
493 return isl_map_get_tuple_id(set
, isl_dim_set
);
496 /* Does the set tuple have a name?
498 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
500 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
504 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
506 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
509 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
511 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
514 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
515 enum isl_dim_type type
, unsigned pos
)
517 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
520 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
521 enum isl_dim_type type
, unsigned pos
)
523 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
526 /* Does the given dimension have a name?
528 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
529 enum isl_dim_type type
, unsigned pos
)
531 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
534 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
535 enum isl_dim_type type
, unsigned pos
)
537 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
540 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
541 enum isl_dim_type type
, unsigned pos
)
543 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
546 /* Does the given dimension have a name?
548 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
549 enum isl_dim_type type
, unsigned pos
)
551 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
554 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
555 __isl_take isl_basic_map
*bmap
,
556 enum isl_dim_type type
, unsigned pos
, const char *s
)
558 bmap
= isl_basic_map_cow(bmap
);
561 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
564 return isl_basic_map_finalize(bmap
);
566 isl_basic_map_free(bmap
);
570 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
571 enum isl_dim_type type
, unsigned pos
, const char *s
)
575 map
= isl_map_cow(map
);
579 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
583 for (i
= 0; i
< map
->n
; ++i
) {
584 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
595 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
596 __isl_take isl_basic_set
*bset
,
597 enum isl_dim_type type
, unsigned pos
, const char *s
)
599 return (isl_basic_set
*)isl_basic_map_set_dim_name(
600 (isl_basic_map
*)bset
, type
, pos
, s
);
603 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
604 enum isl_dim_type type
, unsigned pos
, const char *s
)
606 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
609 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
610 enum isl_dim_type type
, unsigned pos
)
612 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
615 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
616 enum isl_dim_type type
, unsigned pos
)
618 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
621 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
622 enum isl_dim_type type
, unsigned pos
)
624 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
627 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
628 enum isl_dim_type type
, unsigned pos
)
630 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
633 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
634 enum isl_dim_type type
, unsigned pos
)
636 return isl_map_has_dim_id(set
, type
, pos
);
639 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
640 enum isl_dim_type type
, unsigned pos
)
642 return isl_map_get_dim_id(set
, type
, pos
);
645 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
646 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
648 map
= isl_map_cow(map
);
650 return isl_id_free(id
);
652 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
654 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
657 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
658 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
660 return isl_map_set_dim_id(set
, type
, pos
, id
);
663 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
664 __isl_keep isl_id
*id
)
668 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
671 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
672 __isl_keep isl_id
*id
)
674 return isl_map_find_dim_by_id(set
, type
, id
);
677 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
682 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
685 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
688 return isl_map_find_dim_by_name(set
, type
, name
);
691 /* Reset the user pointer on all identifiers of parameters and tuples
692 * of the space of "map".
694 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
698 space
= isl_map_get_space(map
);
699 space
= isl_space_reset_user(space
);
700 map
= isl_map_reset_space(map
, space
);
705 /* Reset the user pointer on all identifiers of parameters and tuples
706 * of the space of "set".
708 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
710 return isl_map_reset_user(set
);
713 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
717 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
720 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
722 return isl_basic_map_is_rational(bset
);
725 /* Does "bmap" contain any rational points?
727 * If "bmap" has an equality for each dimension, equating the dimension
728 * to an integer constant, then it has no rational points, even if it
729 * is marked as rational.
731 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
733 int has_rational
= 1;
738 if (isl_basic_map_plain_is_empty(bmap
))
740 if (!isl_basic_map_is_rational(bmap
))
742 bmap
= isl_basic_map_copy(bmap
);
743 bmap
= isl_basic_map_implicit_equalities(bmap
);
746 total
= isl_basic_map_total_dim(bmap
);
747 if (bmap
->n_eq
== total
) {
749 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
750 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
753 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
754 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
756 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
764 isl_basic_map_free(bmap
);
769 /* Does "map" contain any rational points?
771 int isl_map_has_rational(__isl_keep isl_map
*map
)
778 for (i
= 0; i
< map
->n
; ++i
) {
779 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
780 if (has_rational
< 0)
788 /* Does "set" contain any rational points?
790 int isl_set_has_rational(__isl_keep isl_set
*set
)
792 return isl_map_has_rational(set
);
795 /* Is this basic set a parameter domain?
797 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
801 return isl_space_is_params(bset
->dim
);
804 /* Is this set a parameter domain?
806 int isl_set_is_params(__isl_keep isl_set
*set
)
810 return isl_space_is_params(set
->dim
);
813 /* Is this map actually a parameter domain?
814 * Users should never call this function. Outside of isl,
815 * a map can never be a parameter domain.
817 int isl_map_is_params(__isl_keep isl_map
*map
)
821 return isl_space_is_params(map
->dim
);
824 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
825 struct isl_basic_map
*bmap
, unsigned extra
,
826 unsigned n_eq
, unsigned n_ineq
)
829 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
834 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
835 if (isl_blk_is_error(bmap
->block
))
838 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
839 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
843 bmap
->block2
= isl_blk_empty();
846 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
847 if (isl_blk_is_error(bmap
->block2
))
850 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
855 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
856 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
858 for (i
= 0; i
< extra
; ++i
)
859 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
863 bmap
->c_size
= n_eq
+ n_ineq
;
864 bmap
->eq
= bmap
->ineq
+ n_ineq
;
873 isl_basic_map_free(bmap
);
877 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
878 unsigned nparam
, unsigned dim
, unsigned extra
,
879 unsigned n_eq
, unsigned n_ineq
)
881 struct isl_basic_map
*bmap
;
884 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
888 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
889 return (struct isl_basic_set
*)bmap
;
892 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
893 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
895 struct isl_basic_map
*bmap
;
898 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
899 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
900 return (struct isl_basic_set
*)bmap
;
906 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
907 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
909 struct isl_basic_map
*bmap
;
913 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
918 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
924 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
925 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
926 unsigned n_eq
, unsigned n_ineq
)
928 struct isl_basic_map
*bmap
;
931 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
935 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
939 static void dup_constraints(
940 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
943 unsigned total
= isl_basic_map_total_dim(src
);
945 for (i
= 0; i
< src
->n_eq
; ++i
) {
946 int j
= isl_basic_map_alloc_equality(dst
);
947 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
950 for (i
= 0; i
< src
->n_ineq
; ++i
) {
951 int j
= isl_basic_map_alloc_inequality(dst
);
952 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
955 for (i
= 0; i
< src
->n_div
; ++i
) {
956 int j
= isl_basic_map_alloc_div(dst
);
957 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
959 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
962 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
964 struct isl_basic_map
*dup
;
968 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
969 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
972 dup_constraints(dup
, bmap
);
973 dup
->flags
= bmap
->flags
;
974 dup
->sample
= isl_vec_copy(bmap
->sample
);
978 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
980 struct isl_basic_map
*dup
;
982 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
983 return (struct isl_basic_set
*)dup
;
986 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
991 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
995 return isl_basic_set_dup(bset
);
998 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1007 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1012 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1016 bmap
= isl_basic_map_dup(bmap
);
1018 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1022 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1031 void *isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1036 if (--bmap
->ref
> 0)
1039 isl_ctx_deref(bmap
->ctx
);
1041 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1043 isl_blk_free(bmap
->ctx
, bmap
->block
);
1044 isl_vec_free(bmap
->sample
);
1045 isl_space_free(bmap
->dim
);
1051 void *isl_basic_set_free(struct isl_basic_set
*bset
)
1053 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1056 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1058 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1061 __isl_give isl_map
*isl_map_align_params_map_map_and(
1062 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1063 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1064 __isl_take isl_map
*map2
))
1068 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1069 return fn(map1
, map2
);
1070 if (!isl_space_has_named_params(map1
->dim
) ||
1071 !isl_space_has_named_params(map2
->dim
))
1072 isl_die(map1
->ctx
, isl_error_invalid
,
1073 "unaligned unnamed parameters", goto error
);
1074 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1075 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1076 return fn(map1
, map2
);
1083 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1084 __isl_keep isl_map
*map2
,
1085 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1091 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1092 return fn(map1
, map2
);
1093 if (!isl_space_has_named_params(map1
->dim
) ||
1094 !isl_space_has_named_params(map2
->dim
))
1095 isl_die(map1
->ctx
, isl_error_invalid
,
1096 "unaligned unnamed parameters", return -1);
1097 map1
= isl_map_copy(map1
);
1098 map2
= isl_map_copy(map2
);
1099 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1100 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1107 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1109 struct isl_ctx
*ctx
;
1113 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1114 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1116 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1117 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1118 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1119 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1120 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1121 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1123 int j
= isl_basic_map_alloc_inequality(bmap
);
1127 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1128 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1135 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1136 bmap
->extra
- bmap
->n_div
);
1137 return bmap
->n_eq
++;
1140 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1142 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1145 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1149 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1154 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1156 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1159 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1164 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1166 if (pos
!= bmap
->n_eq
- 1) {
1168 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1169 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1175 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1177 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1180 /* Turn inequality "pos" of "bmap" into an equality.
1182 * In particular, we move the inequality in front of the equalities
1183 * and move the last inequality in the position of the moved inequality.
1184 * Note that isl_tab_make_equalities_explicit depends on this particular
1185 * change in the ordering of the constraints.
1187 void isl_basic_map_inequality_to_equality(
1188 struct isl_basic_map
*bmap
, unsigned pos
)
1192 t
= bmap
->ineq
[pos
];
1193 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1194 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1199 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1200 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1201 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1202 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1205 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1207 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1210 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1212 struct isl_ctx
*ctx
;
1216 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1217 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1218 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1219 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1220 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1221 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1222 1 + isl_basic_map_total_dim(bmap
),
1223 bmap
->extra
- bmap
->n_div
);
1224 return bmap
->n_ineq
++;
1227 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1229 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1232 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1236 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1241 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1243 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1246 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1251 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1253 if (pos
!= bmap
->n_ineq
- 1) {
1254 t
= bmap
->ineq
[pos
];
1255 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1256 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1257 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1263 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1265 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1268 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1273 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1276 k
= isl_basic_map_alloc_equality(bmap
);
1279 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1282 isl_basic_map_free(bmap
);
1286 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1289 return (isl_basic_set
*)
1290 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1293 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1298 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1301 k
= isl_basic_map_alloc_inequality(bmap
);
1304 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1307 isl_basic_map_free(bmap
);
1311 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1314 return (isl_basic_set
*)
1315 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1318 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1322 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1323 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1324 1 + 1 + isl_basic_map_total_dim(bmap
),
1325 bmap
->extra
- bmap
->n_div
);
1326 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1327 return bmap
->n_div
++;
1330 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1332 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1335 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1339 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1344 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1346 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1349 /* Copy constraint from src to dst, putting the vars of src at offset
1350 * dim_off in dst and the divs of src at offset div_off in dst.
1351 * If both sets are actually map, then dim_off applies to the input
1354 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1355 struct isl_basic_map
*src_map
, isl_int
*src
,
1356 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1358 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1359 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1360 unsigned src_in
= isl_basic_map_n_in(src_map
);
1361 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1362 unsigned src_out
= isl_basic_map_n_out(src_map
);
1363 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1364 isl_int_set(dst
[0], src
[0]);
1365 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1366 if (dst_nparam
> src_nparam
)
1367 isl_seq_clr(dst
+1+src_nparam
,
1368 dst_nparam
- src_nparam
);
1369 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1370 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1372 isl_min(dst_in
-in_off
, src_in
));
1373 if (dst_in
-in_off
> src_in
)
1374 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1375 dst_in
- in_off
- src_in
);
1376 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1377 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1378 src
+1+src_nparam
+src_in
,
1379 isl_min(dst_out
-out_off
, src_out
));
1380 if (dst_out
-out_off
> src_out
)
1381 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1382 dst_out
- out_off
- src_out
);
1383 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1384 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1385 src
+1+src_nparam
+src_in
+src_out
,
1386 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1387 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1388 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1389 div_off
+src_map
->n_div
,
1390 dst_map
->n_div
- div_off
- src_map
->n_div
);
1393 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1394 struct isl_basic_map
*src_map
, isl_int
*src
,
1395 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1397 isl_int_set(dst
[0], src
[0]);
1398 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1401 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1402 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1407 if (!bmap1
|| !bmap2
)
1410 div_off
= bmap1
->n_div
;
1412 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1413 int i1
= isl_basic_map_alloc_equality(bmap1
);
1416 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1417 i_pos
, o_pos
, div_off
);
1420 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1421 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1424 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1425 i_pos
, o_pos
, div_off
);
1428 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1429 int i1
= isl_basic_map_alloc_div(bmap1
);
1432 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1433 i_pos
, o_pos
, div_off
);
1436 isl_basic_map_free(bmap2
);
1441 isl_basic_map_free(bmap1
);
1442 isl_basic_map_free(bmap2
);
1446 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1447 struct isl_basic_set
*bset2
, unsigned pos
)
1449 return (struct isl_basic_set
*)
1450 add_constraints((struct isl_basic_map
*)bset1
,
1451 (struct isl_basic_map
*)bset2
, 0, pos
);
1454 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1455 __isl_take isl_space
*dim
, unsigned extra
,
1456 unsigned n_eq
, unsigned n_ineq
)
1458 struct isl_basic_map
*ext
;
1468 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1469 base
->extra
>= base
->n_div
+ extra
;
1471 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1472 room_for_ineq(base
, n_ineq
)) {
1473 isl_space_free(dim
);
1477 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1478 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1479 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1480 extra
+= base
->extra
;
1482 n_ineq
+= base
->n_ineq
;
1484 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1490 ext
->sample
= isl_vec_copy(base
->sample
);
1491 flags
= base
->flags
;
1492 ext
= add_constraints(ext
, base
, 0, 0);
1495 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1501 isl_space_free(dim
);
1502 isl_basic_map_free(base
);
1506 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1507 __isl_take isl_space
*dim
, unsigned extra
,
1508 unsigned n_eq
, unsigned n_ineq
)
1510 return (struct isl_basic_set
*)
1511 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1512 extra
, n_eq
, n_ineq
);
1515 struct isl_basic_map
*isl_basic_map_extend_constraints(
1516 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1520 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1524 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1525 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1526 unsigned n_eq
, unsigned n_ineq
)
1528 struct isl_basic_map
*bmap
;
1533 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1537 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1540 isl_basic_map_free(base
);
1544 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1545 unsigned nparam
, unsigned dim
, unsigned extra
,
1546 unsigned n_eq
, unsigned n_ineq
)
1548 return (struct isl_basic_set
*)
1549 isl_basic_map_extend((struct isl_basic_map
*)base
,
1550 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1553 struct isl_basic_set
*isl_basic_set_extend_constraints(
1554 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1556 return (struct isl_basic_set
*)
1557 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1561 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1563 return (struct isl_basic_set
*)
1564 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1567 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1572 if (bmap
->ref
> 1) {
1574 bmap
= isl_basic_map_dup(bmap
);
1577 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1581 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1589 return isl_set_dup(set
);
1592 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1600 return isl_map_dup(map
);
1603 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1604 unsigned a_len
, unsigned b_len
)
1606 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1607 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1608 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1611 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1612 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1620 isl_assert(bmap
->ctx
,
1621 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1623 if (n1
== 0 || n2
== 0)
1626 bmap
= isl_basic_map_cow(bmap
);
1630 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1631 if (isl_blk_is_error(blk
))
1634 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1636 bmap
->eq
[i
] + pos
, n1
, n2
);
1638 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1640 bmap
->ineq
[i
] + pos
, n1
, n2
);
1642 for (i
= 0; i
< bmap
->n_div
; ++i
)
1644 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1646 isl_blk_free(bmap
->ctx
, blk
);
1648 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1649 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1650 return isl_basic_map_finalize(bmap
);
1652 isl_basic_map_free(bmap
);
1656 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1657 __isl_take isl_basic_set
*bset
, unsigned n
)
1665 nparam
= isl_basic_set_n_param(bset
);
1666 dim
= isl_basic_set_n_dim(bset
);
1667 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1669 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1671 isl_basic_set_free(bset
);
1675 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1681 total
= isl_basic_map_total_dim(bmap
);
1682 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1683 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1685 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1687 i
= isl_basic_map_alloc_equality(bmap
);
1691 isl_int_set_si(bmap
->eq
[i
][0], 1);
1692 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1693 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1694 isl_vec_free(bmap
->sample
);
1695 bmap
->sample
= NULL
;
1696 return isl_basic_map_finalize(bmap
);
1698 isl_basic_map_free(bmap
);
1702 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1704 return (struct isl_basic_set
*)
1705 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1708 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1711 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1713 isl_int
*t
= bmap
->div
[a
];
1714 bmap
->div
[a
] = bmap
->div
[b
];
1718 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1719 * div definitions accordingly.
1721 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1724 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1726 swap_div(bmap
, a
, b
);
1728 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1729 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1731 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1732 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1734 for (i
= 0; i
< bmap
->n_div
; ++i
)
1735 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1736 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1739 /* Eliminate the specified n dimensions starting at first from the
1740 * constraints, without removing the dimensions from the space.
1741 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1743 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1744 enum isl_dim_type type
, unsigned first
, unsigned n
)
1753 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1754 isl_die(map
->ctx
, isl_error_invalid
,
1755 "index out of bounds", goto error
);
1757 map
= isl_map_cow(map
);
1761 for (i
= 0; i
< map
->n
; ++i
) {
1762 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1772 /* Eliminate the specified n dimensions starting at first from the
1773 * constraints, without removing the dimensions from the space.
1774 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1776 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1777 enum isl_dim_type type
, unsigned first
, unsigned n
)
1779 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1782 /* Eliminate the specified n dimensions starting at first from the
1783 * constraints, without removing the dimensions from the space.
1784 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1786 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1787 unsigned first
, unsigned n
)
1789 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1792 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1793 __isl_take isl_basic_map
*bmap
)
1797 bmap
= isl_basic_map_eliminate_vars(bmap
,
1798 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1802 return isl_basic_map_finalize(bmap
);
1805 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1806 __isl_take isl_basic_set
*bset
)
1808 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1809 (struct isl_basic_map
*)bset
);
1812 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1821 map
= isl_map_cow(map
);
1825 for (i
= 0; i
< map
->n
; ++i
) {
1826 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1836 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1838 return isl_map_remove_divs(set
);
1841 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1842 enum isl_dim_type type
, unsigned first
, unsigned n
)
1846 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1848 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1850 bmap
= isl_basic_map_eliminate_vars(bmap
,
1851 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1854 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1856 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1859 isl_basic_map_free(bmap
);
1863 /* Return true if the definition of the given div (recursively) involves
1864 * any of the given variables.
1866 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1867 unsigned first
, unsigned n
)
1870 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1872 if (isl_int_is_zero(bmap
->div
[div
][0]))
1874 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1877 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1878 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1880 if (div_involves_vars(bmap
, i
, first
, n
))
1887 /* Try and add a lower and/or upper bound on "div" to "bmap"
1888 * based on inequality "i".
1889 * "total" is the total number of variables (excluding the divs).
1890 * "v" is a temporary object that can be used during the calculations.
1891 * If "lb" is set, then a lower bound should be constructed.
1892 * If "ub" is set, then an upper bound should be constructed.
1894 * The calling function has already checked that the inequality does not
1895 * reference "div", but we still need to check that the inequality is
1896 * of the right form. We'll consider the case where we want to construct
1897 * a lower bound. The construction of upper bounds is similar.
1899 * Let "div" be of the form
1901 * q = floor((a + f(x))/d)
1903 * We essentially check if constraint "i" is of the form
1907 * so that we can use it to derive a lower bound on "div".
1908 * However, we allow a slightly more general form
1912 * with the condition that the coefficients of g(x) - f(x) are all
1914 * Rewriting this constraint as
1918 * adding a + f(x) to both sides and dividing by d, we obtain
1920 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1922 * Taking the floor on both sides, we obtain
1924 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1928 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1930 * In the case of an upper bound, we construct the constraint
1932 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1935 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1936 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1937 unsigned total
, isl_int v
, int lb
, int ub
)
1941 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1943 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1944 bmap
->div
[div
][1 + 1 + j
]);
1945 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1948 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1949 bmap
->div
[div
][1 + 1 + j
]);
1950 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1956 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1958 int k
= isl_basic_map_alloc_inequality(bmap
);
1961 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1962 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1963 bmap
->div
[div
][1 + j
]);
1964 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1965 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1967 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1970 int k
= isl_basic_map_alloc_inequality(bmap
);
1973 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1974 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1975 bmap
->div
[div
][1 + j
]);
1976 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
1977 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1979 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
1984 isl_basic_map_free(bmap
);
1988 /* This function is called right before "div" is eliminated from "bmap"
1989 * using Fourier-Motzkin.
1990 * Look through the constraints of "bmap" for constraints on the argument
1991 * of the integer division and use them to construct constraints on the
1992 * integer division itself. These constraints can then be combined
1993 * during the Fourier-Motzkin elimination.
1994 * Note that it is only useful to introduce lower bounds on "div"
1995 * if "bmap" already contains upper bounds on "div" as the newly
1996 * introduce lower bounds can then be combined with the pre-existing
1997 * upper bounds. Similarly for upper bounds.
1998 * We therefore first check if "bmap" contains any lower and/or upper bounds
2001 * It is interesting to note that the introduction of these constraints
2002 * can indeed lead to more accurate results, even when compared to
2003 * deriving constraints on the argument of "div" from constraints on "div".
2004 * Consider, for example, the set
2006 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2008 * The second constraint can be rewritten as
2010 * 2 * [(-i-2j+3)/4] + k >= 0
2012 * from which we can derive
2014 * -i - 2j + 3 >= -2k
2020 * Combined with the first constraint, we obtain
2022 * -3 <= 3 + 2k or k >= -3
2024 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2025 * the first constraint, we obtain
2027 * [(i + 2j)/4] >= [-3/4] = -1
2029 * Combining this constraint with the second constraint, we obtain
2033 static __isl_give isl_basic_map
*insert_bounds_on_div(
2034 __isl_take isl_basic_map
*bmap
, int div
)
2037 int check_lb
, check_ub
;
2044 if (isl_int_is_zero(bmap
->div
[div
][0]))
2047 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2051 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2052 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2059 if (!check_lb
&& !check_ub
)
2064 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2065 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2068 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2069 check_lb
, check_ub
);
2077 /* Remove all divs (recursively) involving any of the given dimensions
2078 * in their definitions.
2080 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2081 __isl_take isl_basic_map
*bmap
,
2082 enum isl_dim_type type
, unsigned first
, unsigned n
)
2088 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2090 first
+= isl_basic_map_offset(bmap
, type
);
2092 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2093 if (!div_involves_vars(bmap
, i
, first
, n
))
2095 bmap
= insert_bounds_on_div(bmap
, i
);
2096 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2104 isl_basic_map_free(bmap
);
2108 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2109 __isl_take isl_basic_set
*bset
,
2110 enum isl_dim_type type
, unsigned first
, unsigned n
)
2112 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2115 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2116 enum isl_dim_type type
, unsigned first
, unsigned n
)
2125 map
= isl_map_cow(map
);
2129 for (i
= 0; i
< map
->n
; ++i
) {
2130 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2141 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2142 enum isl_dim_type type
, unsigned first
, unsigned n
)
2144 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2148 /* Does the desciption of "bmap" depend on the specified dimensions?
2149 * We also check whether the dimensions appear in any of the div definitions.
2150 * In principle there is no need for this check. If the dimensions appear
2151 * in a div definition, they also appear in the defining constraints of that
2154 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2155 enum isl_dim_type type
, unsigned first
, unsigned n
)
2162 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2163 isl_die(bmap
->ctx
, isl_error_invalid
,
2164 "index out of bounds", return -1);
2166 first
+= isl_basic_map_offset(bmap
, type
);
2167 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2168 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2170 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2171 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2173 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2174 if (isl_int_is_zero(bmap
->div
[i
][0]))
2176 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2183 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2184 enum isl_dim_type type
, unsigned first
, unsigned n
)
2191 if (first
+ n
> isl_map_dim(map
, type
))
2192 isl_die(map
->ctx
, isl_error_invalid
,
2193 "index out of bounds", return -1);
2195 for (i
= 0; i
< map
->n
; ++i
) {
2196 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2198 if (involves
< 0 || involves
)
2205 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2206 enum isl_dim_type type
, unsigned first
, unsigned n
)
2208 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2211 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2212 enum isl_dim_type type
, unsigned first
, unsigned n
)
2214 return isl_map_involves_dims(set
, type
, first
, n
);
2217 /* Return true if the definition of the given div is unknown or depends
2220 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2223 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2225 if (isl_int_is_zero(bmap
->div
[div
][0]))
2228 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2229 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2231 if (div_is_unknown(bmap
, i
))
2238 /* Remove all divs that are unknown or defined in terms of unknown divs.
2240 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2241 __isl_take isl_basic_map
*bmap
)
2248 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2249 if (!div_is_unknown(bmap
, i
))
2251 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2260 /* Remove all divs that are unknown or defined in terms of unknown divs.
2262 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2263 __isl_take isl_basic_set
*bset
)
2265 return isl_basic_map_remove_unknown_divs(bset
);
2268 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2277 map
= isl_map_cow(map
);
2281 for (i
= 0; i
< map
->n
; ++i
) {
2282 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2292 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2294 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2297 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2298 __isl_take isl_basic_set
*bset
,
2299 enum isl_dim_type type
, unsigned first
, unsigned n
)
2301 return (isl_basic_set
*)
2302 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2305 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2306 enum isl_dim_type type
, unsigned first
, unsigned n
)
2313 map
= isl_map_cow(map
);
2316 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2318 for (i
= 0; i
< map
->n
; ++i
) {
2319 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2320 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2324 map
= isl_map_drop(map
, type
, first
, n
);
2331 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2332 enum isl_dim_type type
, unsigned first
, unsigned n
)
2334 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2337 /* Project out n inputs starting at first using Fourier-Motzkin */
2338 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2339 unsigned first
, unsigned n
)
2341 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2344 static void dump_term(struct isl_basic_map
*bmap
,
2345 isl_int c
, int pos
, FILE *out
)
2348 unsigned in
= isl_basic_map_n_in(bmap
);
2349 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2350 unsigned nparam
= isl_basic_map_n_param(bmap
);
2352 isl_int_print(out
, c
, 0);
2354 if (!isl_int_is_one(c
))
2355 isl_int_print(out
, c
, 0);
2356 if (pos
< 1 + nparam
) {
2357 name
= isl_space_get_dim_name(bmap
->dim
,
2358 isl_dim_param
, pos
- 1);
2360 fprintf(out
, "%s", name
);
2362 fprintf(out
, "p%d", pos
- 1);
2363 } else if (pos
< 1 + nparam
+ in
)
2364 fprintf(out
, "i%d", pos
- 1 - nparam
);
2365 else if (pos
< 1 + nparam
+ dim
)
2366 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2368 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2372 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2373 int sign
, FILE *out
)
2377 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2381 for (i
= 0, first
= 1; i
< len
; ++i
) {
2382 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2385 fprintf(out
, " + ");
2387 isl_int_abs(v
, c
[i
]);
2388 dump_term(bmap
, v
, i
, out
);
2395 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2396 const char *op
, FILE *out
, int indent
)
2400 fprintf(out
, "%*s", indent
, "");
2402 dump_constraint_sign(bmap
, c
, 1, out
);
2403 fprintf(out
, " %s ", op
);
2404 dump_constraint_sign(bmap
, c
, -1, out
);
2408 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2409 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2411 fprintf(out
, "%*s", indent
, "");
2412 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2417 static void dump_constraints(struct isl_basic_map
*bmap
,
2418 isl_int
**c
, unsigned n
,
2419 const char *op
, FILE *out
, int indent
)
2423 for (i
= 0; i
< n
; ++i
)
2424 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2427 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2431 unsigned total
= isl_basic_map_total_dim(bmap
);
2433 for (j
= 0; j
< 1 + total
; ++j
) {
2434 if (isl_int_is_zero(exp
[j
]))
2436 if (!first
&& isl_int_is_pos(exp
[j
]))
2438 dump_term(bmap
, exp
[j
], j
, out
);
2443 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2447 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2448 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2450 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2451 fprintf(out
, "%*s", indent
, "");
2452 fprintf(out
, "e%d = [(", i
);
2453 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2455 isl_int_print(out
, bmap
->div
[i
][0], 0);
2456 fprintf(out
, "]\n");
2460 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2461 FILE *out
, int indent
)
2464 fprintf(out
, "null basic set\n");
2468 fprintf(out
, "%*s", indent
, "");
2469 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2470 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2471 bset
->extra
, bset
->flags
);
2472 dump((struct isl_basic_map
*)bset
, out
, indent
);
2475 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2476 FILE *out
, int indent
)
2479 fprintf(out
, "null basic map\n");
2483 fprintf(out
, "%*s", indent
, "");
2484 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2485 "flags: %x, n_name: %d\n",
2487 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2488 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2489 dump(bmap
, out
, indent
);
2492 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2497 total
= isl_basic_map_total_dim(bmap
);
2498 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2499 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2500 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2505 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2508 struct isl_set
*set
;
2512 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2513 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2514 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2515 sizeof(struct isl_set
) +
2516 (n
- 1) * sizeof(struct isl_basic_set
*));
2520 set
->ctx
= dim
->ctx
;
2521 isl_ctx_ref(set
->ctx
);
2529 isl_space_free(dim
);
2533 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2534 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2536 struct isl_set
*set
;
2539 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2543 set
= isl_set_alloc_space(dims
, n
, flags
);
2547 /* Make sure "map" has room for at least "n" more basic maps.
2549 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2552 struct isl_map
*grown
= NULL
;
2556 isl_assert(map
->ctx
, n
>= 0, goto error
);
2557 if (map
->n
+ n
<= map
->size
)
2559 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2562 for (i
= 0; i
< map
->n
; ++i
) {
2563 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2571 isl_map_free(grown
);
2576 /* Make sure "set" has room for at least "n" more basic sets.
2578 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2580 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2583 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2586 struct isl_set
*dup
;
2591 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2594 for (i
= 0; i
< set
->n
; ++i
)
2595 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2599 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2601 return isl_map_from_basic_map(bset
);
2604 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2606 struct isl_map
*map
;
2611 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2612 return isl_map_add_basic_map(map
, bmap
);
2615 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2616 __isl_take isl_basic_set
*bset
)
2618 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2619 (struct isl_basic_map
*)bset
);
2622 void *isl_set_free(__isl_take isl_set
*set
)
2632 isl_ctx_deref(set
->ctx
);
2633 for (i
= 0; i
< set
->n
; ++i
)
2634 isl_basic_set_free(set
->p
[i
]);
2635 isl_space_free(set
->dim
);
2641 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2646 fprintf(out
, "null set\n");
2650 fprintf(out
, "%*s", indent
, "");
2651 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2652 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2654 for (i
= 0; i
< set
->n
; ++i
) {
2655 fprintf(out
, "%*s", indent
, "");
2656 fprintf(out
, "basic set %d:\n", i
);
2657 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2661 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2666 fprintf(out
, "null map\n");
2670 fprintf(out
, "%*s", indent
, "");
2671 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2672 "flags: %x, n_name: %d\n",
2673 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2674 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2675 for (i
= 0; i
< map
->n
; ++i
) {
2676 fprintf(out
, "%*s", indent
, "");
2677 fprintf(out
, "basic map %d:\n", i
);
2678 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2682 struct isl_basic_map
*isl_basic_map_intersect_domain(
2683 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2685 struct isl_basic_map
*bmap_domain
;
2690 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2691 bset
->dim
, isl_dim_param
), goto error
);
2693 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2694 isl_assert(bset
->ctx
,
2695 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2697 bmap
= isl_basic_map_cow(bmap
);
2700 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2701 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2702 bmap_domain
= isl_basic_map_from_domain(bset
);
2703 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2705 bmap
= isl_basic_map_simplify(bmap
);
2706 return isl_basic_map_finalize(bmap
);
2708 isl_basic_map_free(bmap
);
2709 isl_basic_set_free(bset
);
2713 struct isl_basic_map
*isl_basic_map_intersect_range(
2714 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2716 struct isl_basic_map
*bmap_range
;
2721 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2722 bset
->dim
, isl_dim_param
), goto error
);
2724 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2725 isl_assert(bset
->ctx
,
2726 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2728 if (isl_basic_set_is_universe(bset
)) {
2729 isl_basic_set_free(bset
);
2733 bmap
= isl_basic_map_cow(bmap
);
2736 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2737 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2738 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2739 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2741 bmap
= isl_basic_map_simplify(bmap
);
2742 return isl_basic_map_finalize(bmap
);
2744 isl_basic_map_free(bmap
);
2745 isl_basic_set_free(bset
);
2749 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2758 total
= 1 + isl_basic_map_total_dim(bmap
);
2759 if (total
!= vec
->size
)
2764 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2765 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2766 if (!isl_int_is_zero(s
)) {
2772 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2773 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2774 if (isl_int_is_neg(s
)) {
2785 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2787 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2790 struct isl_basic_map
*isl_basic_map_intersect(
2791 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2793 struct isl_vec
*sample
= NULL
;
2795 if (!bmap1
|| !bmap2
)
2798 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2799 bmap2
->dim
, isl_dim_param
), goto error
);
2800 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2801 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2802 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2803 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2804 return isl_basic_map_intersect(bmap2
, bmap1
);
2806 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2807 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2808 isl_assert(bmap1
->ctx
,
2809 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2811 if (bmap1
->sample
&&
2812 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2813 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2814 sample
= isl_vec_copy(bmap1
->sample
);
2815 else if (bmap2
->sample
&&
2816 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2817 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2818 sample
= isl_vec_copy(bmap2
->sample
);
2820 bmap1
= isl_basic_map_cow(bmap1
);
2823 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2824 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2825 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2828 isl_vec_free(sample
);
2830 isl_vec_free(bmap1
->sample
);
2831 bmap1
->sample
= sample
;
2834 bmap1
= isl_basic_map_simplify(bmap1
);
2835 return isl_basic_map_finalize(bmap1
);
2838 isl_vec_free(sample
);
2839 isl_basic_map_free(bmap1
);
2840 isl_basic_map_free(bmap2
);
2844 struct isl_basic_set
*isl_basic_set_intersect(
2845 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2847 return (struct isl_basic_set
*)
2848 isl_basic_map_intersect(
2849 (struct isl_basic_map
*)bset1
,
2850 (struct isl_basic_map
*)bset2
);
2853 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2854 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2856 return isl_basic_set_intersect(bset1
, bset2
);
2859 /* Special case of isl_map_intersect, where both map1 and map2
2860 * are convex, without any divs and such that either map1 or map2
2861 * contains a single constraint. This constraint is then simply
2862 * added to the other map.
2864 static __isl_give isl_map
*map_intersect_add_constraint(
2865 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2867 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2868 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2869 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2870 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2872 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2873 return isl_map_intersect(map2
, map1
);
2875 isl_assert(map2
->ctx
,
2876 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2878 map1
= isl_map_cow(map1
);
2881 if (isl_map_plain_is_empty(map1
)) {
2885 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2886 if (map2
->p
[0]->n_eq
== 1)
2887 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2889 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2890 map2
->p
[0]->ineq
[0]);
2892 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2893 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2897 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2898 isl_basic_map_free(map1
->p
[0]);
2911 /* map2 may be either a parameter domain or a map living in the same
2914 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2915 __isl_take isl_map
*map2
)
2924 if ((isl_map_plain_is_empty(map1
) ||
2925 isl_map_plain_is_universe(map2
)) &&
2926 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2930 if ((isl_map_plain_is_empty(map2
) ||
2931 isl_map_plain_is_universe(map1
)) &&
2932 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2937 if (map1
->n
== 1 && map2
->n
== 1 &&
2938 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2939 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2940 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2941 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2942 return map_intersect_add_constraint(map1
, map2
);
2944 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2945 isl_space_dim(map2
->dim
, isl_dim_param
))
2946 isl_assert(map1
->ctx
,
2947 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2949 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2950 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2951 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2953 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2954 map1
->n
* map2
->n
, flags
);
2957 for (i
= 0; i
< map1
->n
; ++i
)
2958 for (j
= 0; j
< map2
->n
; ++j
) {
2959 struct isl_basic_map
*part
;
2960 part
= isl_basic_map_intersect(
2961 isl_basic_map_copy(map1
->p
[i
]),
2962 isl_basic_map_copy(map2
->p
[j
]));
2963 if (isl_basic_map_is_empty(part
) < 0)
2964 part
= isl_basic_map_free(part
);
2965 result
= isl_map_add_basic_map(result
, part
);
2978 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2979 __isl_take isl_map
*map2
)
2983 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2984 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2985 "spaces don't match", goto error
);
2986 return map_intersect_internal(map1
, map2
);
2993 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2994 __isl_take isl_map
*map2
)
2996 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2999 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3001 return (struct isl_set
*)
3002 isl_map_intersect((struct isl_map
*)set1
,
3003 (struct isl_map
*)set2
);
3006 /* map_intersect_internal accepts intersections
3007 * with parameter domains, so we can just call that function.
3009 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3010 __isl_take isl_set
*params
)
3012 return map_intersect_internal(map
, params
);
3015 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3016 __isl_take isl_map
*map2
)
3018 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3021 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3022 __isl_take isl_set
*params
)
3024 return isl_map_intersect_params(set
, params
);
3027 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3030 struct isl_basic_set
*bset
;
3035 bmap
= isl_basic_map_cow(bmap
);
3038 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3039 in
= isl_basic_map_n_in(bmap
);
3040 bset
= isl_basic_set_from_basic_map(bmap
);
3041 bset
= isl_basic_set_swap_vars(bset
, in
);
3042 return isl_basic_map_from_basic_set(bset
, dim
);
3045 static __isl_give isl_basic_map
*basic_map_space_reset(
3046 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3052 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3055 space
= isl_basic_map_get_space(bmap
);
3056 space
= isl_space_reset(space
, type
);
3057 bmap
= isl_basic_map_reset_space(bmap
, space
);
3061 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3062 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3063 unsigned pos
, unsigned n
)
3066 struct isl_basic_map
*res
;
3067 struct isl_dim_map
*dim_map
;
3068 unsigned total
, off
;
3069 enum isl_dim_type t
;
3072 return basic_map_space_reset(bmap
, type
);
3077 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3079 total
= isl_basic_map_total_dim(bmap
) + n
;
3080 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3082 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3084 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3086 unsigned size
= isl_basic_map_dim(bmap
, t
);
3087 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3089 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3090 pos
, size
- pos
, off
+ pos
+ n
);
3092 off
+= isl_space_dim(res_dim
, t
);
3094 isl_dim_map_div(dim_map
, bmap
, off
);
3096 res
= isl_basic_map_alloc_space(res_dim
,
3097 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3098 if (isl_basic_map_is_rational(bmap
))
3099 res
= isl_basic_map_set_rational(res
);
3100 if (isl_basic_map_plain_is_empty(bmap
)) {
3101 isl_basic_map_free(bmap
);
3103 return isl_basic_map_set_to_empty(res
);
3105 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3106 return isl_basic_map_finalize(res
);
3109 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3110 __isl_take isl_basic_set
*bset
,
3111 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3113 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3116 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3117 enum isl_dim_type type
, unsigned n
)
3121 return isl_basic_map_insert_dims(bmap
, type
,
3122 isl_basic_map_dim(bmap
, type
), n
);
3125 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3126 enum isl_dim_type type
, unsigned n
)
3130 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3131 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3133 isl_basic_set_free(bset
);
3137 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3138 enum isl_dim_type type
)
3142 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3145 space
= isl_map_get_space(map
);
3146 space
= isl_space_reset(space
, type
);
3147 map
= isl_map_reset_space(map
, space
);
3151 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3152 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3157 return map_space_reset(map
, type
);
3159 map
= isl_map_cow(map
);
3163 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3167 for (i
= 0; i
< map
->n
; ++i
) {
3168 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3179 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3180 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3182 return isl_map_insert_dims(set
, type
, pos
, n
);
3185 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3186 enum isl_dim_type type
, unsigned n
)
3190 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3193 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3194 enum isl_dim_type type
, unsigned n
)
3198 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3199 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3205 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3206 __isl_take isl_basic_map
*bmap
,
3207 enum isl_dim_type dst_type
, unsigned dst_pos
,
3208 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3210 struct isl_dim_map
*dim_map
;
3211 struct isl_basic_map
*res
;
3212 enum isl_dim_type t
;
3213 unsigned total
, off
;
3220 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3223 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3226 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3228 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3229 pos(bmap
->dim
, src_type
) + src_pos
+
3230 ((src_type
< dst_type
) ? n
: 0)) {
3231 bmap
= isl_basic_map_cow(bmap
);
3235 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3236 src_type
, src_pos
, n
);
3240 bmap
= isl_basic_map_finalize(bmap
);
3245 total
= isl_basic_map_total_dim(bmap
);
3246 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3249 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3250 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3251 if (t
== dst_type
) {
3252 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3255 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3258 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3259 dst_pos
, size
- dst_pos
, off
);
3260 off
+= size
- dst_pos
;
3261 } else if (t
== src_type
) {
3262 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3265 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3266 src_pos
+ n
, size
- src_pos
- n
, off
);
3267 off
+= size
- src_pos
- n
;
3269 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3273 isl_dim_map_div(dim_map
, bmap
, off
);
3275 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3276 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3277 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3281 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3282 src_type
, src_pos
, n
);
3286 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3287 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3288 bmap
= isl_basic_map_finalize(bmap
);
3292 isl_basic_map_free(bmap
);
3296 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3297 enum isl_dim_type dst_type
, unsigned dst_pos
,
3298 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3300 return (isl_basic_set
*)isl_basic_map_move_dims(
3301 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3304 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3305 enum isl_dim_type dst_type
, unsigned dst_pos
,
3306 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3310 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3311 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3312 src_type
, src_pos
, n
);
3318 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3319 enum isl_dim_type dst_type
, unsigned dst_pos
,
3320 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3329 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3332 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3335 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3337 map
= isl_map_cow(map
);
3341 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3345 for (i
= 0; i
< map
->n
; ++i
) {
3346 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3348 src_type
, src_pos
, n
);
3359 /* Move the specified dimensions to the last columns right before
3360 * the divs. Don't change the dimension specification of bmap.
3361 * That's the responsibility of the caller.
3363 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3364 enum isl_dim_type type
, unsigned first
, unsigned n
)
3366 struct isl_dim_map
*dim_map
;
3367 struct isl_basic_map
*res
;
3368 enum isl_dim_type t
;
3369 unsigned total
, off
;
3373 if (pos(bmap
->dim
, type
) + first
+ n
==
3374 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3377 total
= isl_basic_map_total_dim(bmap
);
3378 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3381 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3382 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3384 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3387 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3388 first
, n
, total
- bmap
->n_div
- n
);
3389 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3390 first
+ n
, size
- (first
+ n
), off
);
3391 off
+= size
- (first
+ n
);
3393 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3397 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3399 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3400 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3401 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3405 /* Insert "n" rows in the divs of "bmap".
3407 * The number of columns is not changed, which means that the last
3408 * dimensions of "bmap" are being reintepreted as the new divs.
3409 * The space of "bmap" is not adjusted, however, which means
3410 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3411 * from the space of "bmap" is the responsibility of the caller.
3413 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3421 bmap
= isl_basic_map_cow(bmap
);
3425 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3426 old
= bmap
->block2
.data
;
3427 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3428 (bmap
->extra
+ n
) * (1 + row_size
));
3429 if (!bmap
->block2
.data
)
3430 return isl_basic_map_free(bmap
);
3431 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3433 return isl_basic_map_free(bmap
);
3434 for (i
= 0; i
< n
; ++i
) {
3435 new_div
[i
] = bmap
->block2
.data
+
3436 (bmap
->extra
+ i
) * (1 + row_size
);
3437 isl_seq_clr(new_div
[i
], 1 + row_size
);
3439 for (i
= 0; i
< bmap
->extra
; ++i
)
3440 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3442 bmap
->div
= new_div
;
3449 /* Turn the n dimensions of type type, starting at first
3450 * into existentially quantified variables.
3452 __isl_give isl_basic_map
*isl_basic_map_project_out(
3453 __isl_take isl_basic_map
*bmap
,
3454 enum isl_dim_type type
, unsigned first
, unsigned n
)
3457 return basic_map_space_reset(bmap
, type
);
3462 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3463 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3465 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3468 bmap
= move_last(bmap
, type
, first
, n
);
3469 bmap
= isl_basic_map_cow(bmap
);
3470 bmap
= insert_div_rows(bmap
, n
);
3474 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3477 bmap
= isl_basic_map_simplify(bmap
);
3478 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3479 return isl_basic_map_finalize(bmap
);
3481 isl_basic_map_free(bmap
);
3485 /* Turn the n dimensions of type type, starting at first
3486 * into existentially quantified variables.
3488 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3489 enum isl_dim_type type
, unsigned first
, unsigned n
)
3491 return (isl_basic_set
*)isl_basic_map_project_out(
3492 (isl_basic_map
*)bset
, type
, first
, n
);
3495 /* Turn the n dimensions of type type, starting at first
3496 * into existentially quantified variables.
3498 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3499 enum isl_dim_type type
, unsigned first
, unsigned n
)
3507 return map_space_reset(map
, type
);
3509 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3511 map
= isl_map_cow(map
);
3515 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3519 for (i
= 0; i
< map
->n
; ++i
) {
3520 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3531 /* Turn the n dimensions of type type, starting at first
3532 * into existentially quantified variables.
3534 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3535 enum isl_dim_type type
, unsigned first
, unsigned n
)
3537 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3540 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3544 for (i
= 0; i
< n
; ++i
) {
3545 j
= isl_basic_map_alloc_div(bmap
);
3548 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3552 isl_basic_map_free(bmap
);
3556 struct isl_basic_map
*isl_basic_map_apply_range(
3557 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3559 isl_space
*dim_result
= NULL
;
3560 struct isl_basic_map
*bmap
;
3561 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3562 struct isl_dim_map
*dim_map1
, *dim_map2
;
3564 if (!bmap1
|| !bmap2
)
3566 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3567 bmap2
->dim
, isl_dim_param
))
3568 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3569 "parameters don't match", goto error
);
3570 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3571 bmap2
->dim
, isl_dim_in
))
3572 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3573 "spaces don't match", goto error
);
3575 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3576 isl_space_copy(bmap2
->dim
));
3578 n_in
= isl_basic_map_n_in(bmap1
);
3579 n_out
= isl_basic_map_n_out(bmap2
);
3580 n
= isl_basic_map_n_out(bmap1
);
3581 nparam
= isl_basic_map_n_param(bmap1
);
3583 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3584 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3585 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3586 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3587 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3588 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3589 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3590 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3591 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3592 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3593 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3595 bmap
= isl_basic_map_alloc_space(dim_result
,
3596 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3597 bmap1
->n_eq
+ bmap2
->n_eq
,
3598 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3599 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3600 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3601 bmap
= add_divs(bmap
, n
);
3602 bmap
= isl_basic_map_simplify(bmap
);
3603 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3604 return isl_basic_map_finalize(bmap
);
3606 isl_basic_map_free(bmap1
);
3607 isl_basic_map_free(bmap2
);
3611 struct isl_basic_set
*isl_basic_set_apply(
3612 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3617 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3620 return (struct isl_basic_set
*)
3621 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3623 isl_basic_set_free(bset
);
3624 isl_basic_map_free(bmap
);
3628 struct isl_basic_map
*isl_basic_map_apply_domain(
3629 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3631 if (!bmap1
|| !bmap2
)
3634 isl_assert(bmap1
->ctx
,
3635 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3636 isl_assert(bmap1
->ctx
,
3637 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3640 bmap1
= isl_basic_map_reverse(bmap1
);
3641 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3642 return isl_basic_map_reverse(bmap1
);
3644 isl_basic_map_free(bmap1
);
3645 isl_basic_map_free(bmap2
);
3649 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3650 * A \cap B -> f(A) + f(B)
3652 struct isl_basic_map
*isl_basic_map_sum(
3653 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3655 unsigned n_in
, n_out
, nparam
, total
, pos
;
3656 struct isl_basic_map
*bmap
= NULL
;
3657 struct isl_dim_map
*dim_map1
, *dim_map2
;
3660 if (!bmap1
|| !bmap2
)
3663 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3666 nparam
= isl_basic_map_n_param(bmap1
);
3667 n_in
= isl_basic_map_n_in(bmap1
);
3668 n_out
= isl_basic_map_n_out(bmap1
);
3670 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3671 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3672 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3673 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3674 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3675 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3676 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3677 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3678 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3679 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3680 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3682 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3683 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3684 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3685 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3686 for (i
= 0; i
< n_out
; ++i
) {
3687 int j
= isl_basic_map_alloc_equality(bmap
);
3690 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3691 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3692 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3693 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3695 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3696 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3697 bmap
= add_divs(bmap
, 2 * n_out
);
3699 bmap
= isl_basic_map_simplify(bmap
);
3700 return isl_basic_map_finalize(bmap
);
3702 isl_basic_map_free(bmap
);
3703 isl_basic_map_free(bmap1
);
3704 isl_basic_map_free(bmap2
);
3708 /* Given two maps A -> f(A) and B -> g(B), construct a map
3709 * A \cap B -> f(A) + f(B)
3711 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3713 struct isl_map
*result
;
3719 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3721 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3722 map1
->n
* map2
->n
, 0);
3725 for (i
= 0; i
< map1
->n
; ++i
)
3726 for (j
= 0; j
< map2
->n
; ++j
) {
3727 struct isl_basic_map
*part
;
3728 part
= isl_basic_map_sum(
3729 isl_basic_map_copy(map1
->p
[i
]),
3730 isl_basic_map_copy(map2
->p
[j
]));
3731 if (isl_basic_map_is_empty(part
))
3732 isl_basic_map_free(part
);
3734 result
= isl_map_add_basic_map(result
, part
);
3747 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3748 __isl_take isl_set
*set2
)
3750 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3753 /* Given a basic map A -> f(A), construct A -> -f(A).
3755 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3760 bmap
= isl_basic_map_cow(bmap
);
3764 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3765 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3766 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3767 for (j
= 0; j
< n
; ++j
)
3768 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3769 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3770 for (j
= 0; j
< n
; ++j
)
3771 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3772 for (i
= 0; i
< bmap
->n_div
; ++i
)
3773 for (j
= 0; j
< n
; ++j
)
3774 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3775 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3776 return isl_basic_map_finalize(bmap
);
3779 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3781 return isl_basic_map_neg(bset
);
3784 /* Given a map A -> f(A), construct A -> -f(A).
3786 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3790 map
= isl_map_cow(map
);
3794 for (i
= 0; i
< map
->n
; ++i
) {
3795 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3806 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3808 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3811 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3812 * A -> floor(f(A)/d).
3814 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3817 unsigned n_in
, n_out
, nparam
, total
, pos
;
3818 struct isl_basic_map
*result
= NULL
;
3819 struct isl_dim_map
*dim_map
;
3825 nparam
= isl_basic_map_n_param(bmap
);
3826 n_in
= isl_basic_map_n_in(bmap
);
3827 n_out
= isl_basic_map_n_out(bmap
);
3829 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3830 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3831 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3832 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3833 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3834 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3836 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3837 bmap
->n_div
+ n_out
,
3838 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3839 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3840 result
= add_divs(result
, n_out
);
3841 for (i
= 0; i
< n_out
; ++i
) {
3843 j
= isl_basic_map_alloc_inequality(result
);
3846 isl_seq_clr(result
->ineq
[j
], 1+total
);
3847 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3848 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3849 j
= isl_basic_map_alloc_inequality(result
);
3852 isl_seq_clr(result
->ineq
[j
], 1+total
);
3853 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3854 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3855 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3858 result
= isl_basic_map_simplify(result
);
3859 return isl_basic_map_finalize(result
);
3861 isl_basic_map_free(result
);
3865 /* Given a map A -> f(A) and an integer d, construct a map
3866 * A -> floor(f(A)/d).
3868 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3872 map
= isl_map_cow(map
);
3876 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3877 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3878 for (i
= 0; i
< map
->n
; ++i
) {
3879 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3890 /* Given a map A -> f(A) and an integer d, construct a map
3891 * A -> floor(f(A)/d).
3893 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3894 __isl_take isl_val
*d
)
3898 if (!isl_val_is_int(d
))
3899 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3900 "expecting integer denominator", goto error
);
3901 map
= isl_map_floordiv(map
, d
->n
);
3910 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3916 i
= isl_basic_map_alloc_equality(bmap
);
3919 nparam
= isl_basic_map_n_param(bmap
);
3920 n_in
= isl_basic_map_n_in(bmap
);
3921 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3922 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3923 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3924 return isl_basic_map_finalize(bmap
);
3926 isl_basic_map_free(bmap
);
3930 /* Add a constraints to "bmap" expressing i_pos < o_pos
3932 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3938 i
= isl_basic_map_alloc_inequality(bmap
);
3941 nparam
= isl_basic_map_n_param(bmap
);
3942 n_in
= isl_basic_map_n_in(bmap
);
3943 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3944 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3945 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3946 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3947 return isl_basic_map_finalize(bmap
);
3949 isl_basic_map_free(bmap
);
3953 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3955 static __isl_give isl_basic_map
*var_less_or_equal(
3956 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3962 i
= isl_basic_map_alloc_inequality(bmap
);
3965 nparam
= isl_basic_map_n_param(bmap
);
3966 n_in
= isl_basic_map_n_in(bmap
);
3967 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3968 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3969 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3970 return isl_basic_map_finalize(bmap
);
3972 isl_basic_map_free(bmap
);
3976 /* Add a constraints to "bmap" expressing i_pos > o_pos
3978 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3984 i
= isl_basic_map_alloc_inequality(bmap
);
3987 nparam
= isl_basic_map_n_param(bmap
);
3988 n_in
= isl_basic_map_n_in(bmap
);
3989 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3990 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3991 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3992 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3993 return isl_basic_map_finalize(bmap
);
3995 isl_basic_map_free(bmap
);
3999 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4001 static __isl_give isl_basic_map
*var_more_or_equal(
4002 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4008 i
= isl_basic_map_alloc_inequality(bmap
);
4011 nparam
= isl_basic_map_n_param(bmap
);
4012 n_in
= isl_basic_map_n_in(bmap
);
4013 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4014 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4015 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4016 return isl_basic_map_finalize(bmap
);
4018 isl_basic_map_free(bmap
);
4022 __isl_give isl_basic_map
*isl_basic_map_equal(
4023 __isl_take isl_space
*dim
, unsigned n_equal
)
4026 struct isl_basic_map
*bmap
;
4027 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4030 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4031 bmap
= var_equal(bmap
, i
);
4032 return isl_basic_map_finalize(bmap
);
4035 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4037 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4041 struct isl_basic_map
*bmap
;
4042 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4045 for (i
= 0; i
< pos
&& bmap
; ++i
)
4046 bmap
= var_equal(bmap
, i
);
4048 bmap
= var_less(bmap
, pos
);
4049 return isl_basic_map_finalize(bmap
);
4052 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4054 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4055 __isl_take isl_space
*dim
, unsigned pos
)
4058 isl_basic_map
*bmap
;
4060 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4061 for (i
= 0; i
< pos
; ++i
)
4062 bmap
= var_equal(bmap
, i
);
4063 bmap
= var_less_or_equal(bmap
, pos
);
4064 return isl_basic_map_finalize(bmap
);
4067 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4069 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4073 struct isl_basic_map
*bmap
;
4074 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4077 for (i
= 0; i
< pos
&& bmap
; ++i
)
4078 bmap
= var_equal(bmap
, i
);
4080 bmap
= var_more(bmap
, pos
);
4081 return isl_basic_map_finalize(bmap
);
4084 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4086 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4087 __isl_take isl_space
*dim
, unsigned pos
)
4090 isl_basic_map
*bmap
;
4092 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4093 for (i
= 0; i
< pos
; ++i
)
4094 bmap
= var_equal(bmap
, i
);
4095 bmap
= var_more_or_equal(bmap
, pos
);
4096 return isl_basic_map_finalize(bmap
);
4099 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4100 unsigned n
, int equal
)
4102 struct isl_map
*map
;
4105 if (n
== 0 && equal
)
4106 return isl_map_universe(dims
);
4108 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4110 for (i
= 0; i
+ 1 < n
; ++i
)
4111 map
= isl_map_add_basic_map(map
,
4112 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4115 map
= isl_map_add_basic_map(map
,
4116 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4118 map
= isl_map_add_basic_map(map
,
4119 isl_basic_map_less_at(dims
, n
- 1));
4121 isl_space_free(dims
);
4126 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4130 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4133 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4135 return map_lex_lte_first(dim
, n
, 0);
4138 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4140 return map_lex_lte_first(dim
, n
, 1);
4143 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4145 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4148 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4150 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4153 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4154 unsigned n
, int equal
)
4156 struct isl_map
*map
;
4159 if (n
== 0 && equal
)
4160 return isl_map_universe(dims
);
4162 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4164 for (i
= 0; i
+ 1 < n
; ++i
)
4165 map
= isl_map_add_basic_map(map
,
4166 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4169 map
= isl_map_add_basic_map(map
,
4170 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4172 map
= isl_map_add_basic_map(map
,
4173 isl_basic_map_more_at(dims
, n
- 1));
4175 isl_space_free(dims
);
4180 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4184 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4187 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4189 return map_lex_gte_first(dim
, n
, 0);
4192 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4194 return map_lex_gte_first(dim
, n
, 1);
4197 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4199 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4202 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4204 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4207 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4208 __isl_take isl_set
*set2
)
4211 map
= isl_map_lex_le(isl_set_get_space(set1
));
4212 map
= isl_map_intersect_domain(map
, set1
);
4213 map
= isl_map_intersect_range(map
, set2
);
4217 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4218 __isl_take isl_set
*set2
)
4221 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4222 map
= isl_map_intersect_domain(map
, set1
);
4223 map
= isl_map_intersect_range(map
, set2
);
4227 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4228 __isl_take isl_set
*set2
)
4231 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4232 map
= isl_map_intersect_domain(map
, set1
);
4233 map
= isl_map_intersect_range(map
, set2
);
4237 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4238 __isl_take isl_set
*set2
)
4241 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4242 map
= isl_map_intersect_domain(map
, set1
);
4243 map
= isl_map_intersect_range(map
, set2
);
4247 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4248 __isl_take isl_map
*map2
)
4251 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4252 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4253 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4257 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4258 __isl_take isl_map
*map2
)
4261 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4262 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4263 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4267 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4268 __isl_take isl_map
*map2
)
4271 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4272 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4273 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4277 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4278 __isl_take isl_map
*map2
)
4281 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4282 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4283 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4287 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4288 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4290 struct isl_basic_map
*bmap
;
4292 bset
= isl_basic_set_cow(bset
);
4296 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4297 isl_space_free(bset
->dim
);
4298 bmap
= (struct isl_basic_map
*) bset
;
4300 return isl_basic_map_finalize(bmap
);
4302 isl_basic_set_free(bset
);
4303 isl_space_free(dim
);
4307 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4311 if (bmap
->dim
->n_in
== 0)
4312 return (struct isl_basic_set
*)bmap
;
4313 bmap
= isl_basic_map_cow(bmap
);
4316 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4319 bmap
= isl_basic_map_finalize(bmap
);
4320 return (struct isl_basic_set
*)bmap
;
4322 isl_basic_map_free(bmap
);
4326 /* For a div d = floor(f/m), add the constraints
4329 * -(f-(n-1)) + m d >= 0
4331 * Note that the second constraint is the negation of
4335 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4336 unsigned pos
, isl_int
*div
)
4339 unsigned total
= isl_basic_map_total_dim(bmap
);
4341 i
= isl_basic_map_alloc_inequality(bmap
);
4344 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4345 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4347 j
= isl_basic_map_alloc_inequality(bmap
);
4350 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
4351 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
4352 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
4356 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4357 unsigned pos
, isl_int
*div
)
4359 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4363 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4365 unsigned total
= isl_basic_map_total_dim(bmap
);
4366 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4368 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4372 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4374 return isl_basic_map_add_div_constraints(bset
, div
);
4377 struct isl_basic_set
*isl_basic_map_underlying_set(
4378 struct isl_basic_map
*bmap
)
4382 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4384 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4385 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4386 return (struct isl_basic_set
*)bmap
;
4387 bmap
= isl_basic_map_cow(bmap
);
4390 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4393 bmap
->extra
-= bmap
->n_div
;
4395 bmap
= isl_basic_map_finalize(bmap
);
4396 return (struct isl_basic_set
*)bmap
;
4398 isl_basic_map_free(bmap
);
4402 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4403 __isl_take isl_basic_set
*bset
)
4405 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4408 struct isl_basic_map
*isl_basic_map_overlying_set(
4409 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4411 struct isl_basic_map
*bmap
;
4412 struct isl_ctx
*ctx
;
4419 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4420 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4421 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4423 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4424 isl_basic_map_free(like
);
4425 return (struct isl_basic_map
*)bset
;
4427 bset
= isl_basic_set_cow(bset
);
4430 total
= bset
->dim
->n_out
+ bset
->extra
;
4431 bmap
= (struct isl_basic_map
*)bset
;
4432 isl_space_free(bmap
->dim
);
4433 bmap
->dim
= isl_space_copy(like
->dim
);
4436 bmap
->n_div
= like
->n_div
;
4437 bmap
->extra
+= like
->n_div
;
4441 ltotal
= total
- bmap
->extra
+ like
->extra
;
4444 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4445 bmap
->extra
* (1 + 1 + total
));
4446 if (isl_blk_is_error(bmap
->block2
))
4448 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4452 for (i
= 0; i
< bmap
->extra
; ++i
)
4453 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4454 for (i
= 0; i
< like
->n_div
; ++i
) {
4455 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4456 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4458 bmap
= isl_basic_map_extend_constraints(bmap
,
4459 0, 2 * like
->n_div
);
4460 for (i
= 0; i
< like
->n_div
; ++i
) {
4463 if (isl_int_is_zero(bmap
->div
[i
][0]))
4465 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4466 bmap
= isl_basic_map_free(bmap
);
4469 isl_basic_map_free(like
);
4470 bmap
= isl_basic_map_simplify(bmap
);
4471 bmap
= isl_basic_map_finalize(bmap
);
4474 isl_basic_map_free(like
);
4475 isl_basic_set_free(bset
);
4479 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4480 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4482 return (struct isl_basic_set
*)
4483 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4486 struct isl_set
*isl_set_from_underlying_set(
4487 struct isl_set
*set
, struct isl_basic_set
*like
)
4493 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4495 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4496 isl_basic_set_free(like
);
4499 set
= isl_set_cow(set
);
4502 for (i
= 0; i
< set
->n
; ++i
) {
4503 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4504 isl_basic_set_copy(like
));
4508 isl_space_free(set
->dim
);
4509 set
->dim
= isl_space_copy(like
->dim
);
4512 isl_basic_set_free(like
);
4515 isl_basic_set_free(like
);
4520 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4524 map
= isl_map_cow(map
);
4527 map
->dim
= isl_space_cow(map
->dim
);
4531 for (i
= 1; i
< map
->n
; ++i
)
4532 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4534 for (i
= 0; i
< map
->n
; ++i
) {
4535 map
->p
[i
] = (struct isl_basic_map
*)
4536 isl_basic_map_underlying_set(map
->p
[i
]);
4541 map
->dim
= isl_space_underlying(map
->dim
, 0);
4543 isl_space_free(map
->dim
);
4544 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4548 return (struct isl_set
*)map
;
4554 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4556 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4559 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4560 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4562 bmap
= isl_basic_map_cow(bmap
);
4566 isl_space_free(bmap
->dim
);
4569 bmap
= isl_basic_map_finalize(bmap
);
4573 isl_basic_map_free(bmap
);
4574 isl_space_free(dim
);
4578 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4579 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4581 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4585 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4586 __isl_take isl_space
*dim
)
4590 map
= isl_map_cow(map
);
4594 for (i
= 0; i
< map
->n
; ++i
) {
4595 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4596 isl_space_copy(dim
));
4600 isl_space_free(map
->dim
);
4606 isl_space_free(dim
);
4610 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4611 __isl_take isl_space
*dim
)
4613 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4616 /* Compute the parameter domain of the given basic set.
4618 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4623 if (isl_basic_set_is_params(bset
))
4626 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4627 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4628 space
= isl_basic_set_get_space(bset
);
4629 space
= isl_space_params(space
);
4630 bset
= isl_basic_set_reset_space(bset
, space
);
4634 /* Construct a zero-dimensional basic set with the given parameter domain.
4636 __isl_give isl_basic_set
*isl_basic_set_from_params(
4637 __isl_take isl_basic_set
*bset
)
4640 space
= isl_basic_set_get_space(bset
);
4641 space
= isl_space_set_from_params(space
);
4642 bset
= isl_basic_set_reset_space(bset
, space
);
4646 /* Compute the parameter domain of the given set.
4648 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4653 if (isl_set_is_params(set
))
4656 n
= isl_set_dim(set
, isl_dim_set
);
4657 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4658 space
= isl_set_get_space(set
);
4659 space
= isl_space_params(space
);
4660 set
= isl_set_reset_space(set
, space
);
4664 /* Construct a zero-dimensional set with the given parameter domain.
4666 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4669 space
= isl_set_get_space(set
);
4670 space
= isl_space_set_from_params(space
);
4671 set
= isl_set_reset_space(set
, space
);
4675 /* Compute the parameter domain of the given map.
4677 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4682 n
= isl_map_dim(map
, isl_dim_in
);
4683 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4684 n
= isl_map_dim(map
, isl_dim_out
);
4685 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4686 space
= isl_map_get_space(map
);
4687 space
= isl_space_params(space
);
4688 map
= isl_map_reset_space(map
, space
);
4692 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4695 struct isl_basic_set
*domain
;
4701 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4703 n_in
= isl_basic_map_n_in(bmap
);
4704 n_out
= isl_basic_map_n_out(bmap
);
4705 domain
= isl_basic_set_from_basic_map(bmap
);
4706 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4708 domain
= isl_basic_set_reset_space(domain
, dim
);
4713 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4717 return isl_space_may_be_set(bmap
->dim
);
4720 /* Is this basic map actually a set?
4721 * Users should never call this function. Outside of isl,
4722 * the type should indicate whether something is a set or a map.
4724 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4728 return isl_space_is_set(bmap
->dim
);
4731 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4735 if (isl_basic_map_is_set(bmap
))
4737 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4740 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4741 __isl_take isl_basic_map
*bmap
)
4745 isl_basic_map
*domain
;
4746 int nparam
, n_in
, n_out
;
4749 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4750 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4751 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4753 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4754 domain
= isl_basic_map_universe(dim
);
4756 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4757 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4758 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4760 total
= isl_basic_map_total_dim(bmap
);
4762 for (i
= 0; i
< n_in
; ++i
) {
4763 k
= isl_basic_map_alloc_equality(bmap
);
4766 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4767 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4768 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4771 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4772 return isl_basic_map_finalize(bmap
);
4774 isl_basic_map_free(bmap
);
4778 __isl_give isl_basic_map
*isl_basic_map_range_map(
4779 __isl_take isl_basic_map
*bmap
)
4783 isl_basic_map
*range
;
4784 int nparam
, n_in
, n_out
;
4787 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4788 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4789 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4791 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4792 range
= isl_basic_map_universe(dim
);
4794 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4795 bmap
= isl_basic_map_apply_range(bmap
, range
);
4796 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4798 total
= isl_basic_map_total_dim(bmap
);
4800 for (i
= 0; i
< n_out
; ++i
) {
4801 k
= isl_basic_map_alloc_equality(bmap
);
4804 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4805 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4806 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4809 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4810 return isl_basic_map_finalize(bmap
);
4812 isl_basic_map_free(bmap
);
4816 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4820 return isl_space_may_be_set(map
->dim
);
4823 /* Is this map actually a set?
4824 * Users should never call this function. Outside of isl,
4825 * the type should indicate whether something is a set or a map.
4827 int isl_map_is_set(__isl_keep isl_map
*map
)
4831 return isl_space_is_set(map
->dim
);
4834 struct isl_set
*isl_map_range(struct isl_map
*map
)
4837 struct isl_set
*set
;
4841 if (isl_map_is_set(map
))
4842 return (isl_set
*)map
;
4844 map
= isl_map_cow(map
);
4848 set
= (struct isl_set
*) map
;
4849 set
->dim
= isl_space_range(set
->dim
);
4852 for (i
= 0; i
< map
->n
; ++i
) {
4853 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4857 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4858 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4865 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4869 map
= isl_map_cow(map
);
4873 map
->dim
= isl_space_domain_map(map
->dim
);
4876 for (i
= 0; i
< map
->n
; ++i
) {
4877 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4881 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4882 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4889 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4892 isl_space
*range_dim
;
4894 map
= isl_map_cow(map
);
4898 range_dim
= isl_space_range(isl_map_get_space(map
));
4899 range_dim
= isl_space_from_range(range_dim
);
4900 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4901 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4904 for (i
= 0; i
< map
->n
; ++i
) {
4905 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4909 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4910 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4917 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4918 __isl_take isl_space
*dim
)
4921 struct isl_map
*map
= NULL
;
4923 set
= isl_set_cow(set
);
4926 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4927 map
= (struct isl_map
*)set
;
4928 for (i
= 0; i
< set
->n
; ++i
) {
4929 map
->p
[i
] = isl_basic_map_from_basic_set(
4930 set
->p
[i
], isl_space_copy(dim
));
4934 isl_space_free(map
->dim
);
4938 isl_space_free(dim
);
4943 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4944 __isl_take isl_basic_set
*bset
)
4946 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4949 __isl_give isl_basic_map
*isl_basic_map_from_range(
4950 __isl_take isl_basic_set
*bset
)
4953 space
= isl_basic_set_get_space(bset
);
4954 space
= isl_space_from_range(space
);
4955 bset
= isl_basic_set_reset_space(bset
, space
);
4956 return (isl_basic_map
*)bset
;
4959 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4962 space
= isl_set_get_space(set
);
4963 space
= isl_space_from_range(space
);
4964 set
= isl_set_reset_space(set
, space
);
4965 return (struct isl_map
*)set
;
4968 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4970 return isl_map_reverse(isl_map_from_range(set
));
4973 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4974 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4976 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4979 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4980 __isl_take isl_set
*range
)
4982 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4985 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4988 struct isl_set
*set
= NULL
;
4992 map
= isl_map_cow(map
);
4995 map
->dim
= isl_space_as_set_space(map
->dim
);
4998 set
= (struct isl_set
*)map
;
4999 for (i
= 0; i
< map
->n
; ++i
) {
5000 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5010 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5013 struct isl_map
*map
;
5018 isl_die(dim
->ctx
, isl_error_internal
,
5019 "negative number of basic maps", goto error
);
5020 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5021 sizeof(struct isl_map
) +
5022 (n
- 1) * sizeof(struct isl_basic_map
*));
5026 map
->ctx
= dim
->ctx
;
5027 isl_ctx_ref(map
->ctx
);
5035 isl_space_free(dim
);
5039 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5040 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5043 struct isl_map
*map
;
5046 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5050 map
= isl_map_alloc_space(dims
, n
, flags
);
5054 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5056 struct isl_basic_map
*bmap
;
5057 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5058 bmap
= isl_basic_map_set_to_empty(bmap
);
5062 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5064 struct isl_basic_set
*bset
;
5065 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5066 bset
= isl_basic_set_set_to_empty(bset
);
5070 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5072 struct isl_basic_map
*bmap
;
5075 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5076 bmap
= isl_basic_map_set_to_empty(bmap
);
5080 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5082 struct isl_basic_map
*bmap
;
5085 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5086 bmap
= isl_basic_map_set_to_empty(bmap
);
5090 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5092 struct isl_basic_set
*bset
;
5095 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5096 bset
= isl_basic_set_set_to_empty(bset
);
5100 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5102 struct isl_basic_map
*bmap
;
5103 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5104 bmap
= isl_basic_map_finalize(bmap
);
5108 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5110 struct isl_basic_set
*bset
;
5111 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5112 bset
= isl_basic_set_finalize(bset
);
5116 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5119 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5120 isl_basic_map
*bmap
;
5122 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5123 for (i
= 0; i
< total
; ++i
) {
5124 int k
= isl_basic_map_alloc_inequality(bmap
);
5127 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5128 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5132 isl_basic_map_free(bmap
);
5136 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5138 return isl_basic_map_nat_universe(dim
);
5141 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5143 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5146 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5148 return isl_map_nat_universe(dim
);
5151 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5152 __isl_keep isl_basic_map
*model
)
5156 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5159 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5163 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5166 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5167 __isl_keep isl_set
*model
)
5171 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5174 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5176 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5179 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5183 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5186 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5190 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5193 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5195 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5198 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5202 return isl_set_empty(isl_space_copy(model
->dim
));
5205 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5207 struct isl_map
*map
;
5210 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5211 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5215 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5217 struct isl_set
*set
;
5220 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5221 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5225 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5229 return isl_set_universe(isl_space_copy(model
->dim
));
5232 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5235 struct isl_map
*dup
;
5239 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5240 for (i
= 0; i
< map
->n
; ++i
)
5241 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5245 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5246 __isl_take isl_basic_map
*bmap
)
5250 if (isl_basic_map_plain_is_empty(bmap
)) {
5251 isl_basic_map_free(bmap
);
5254 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5255 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5256 map
->p
[map
->n
] = bmap
;
5258 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5264 isl_basic_map_free(bmap
);
5268 void *isl_map_free(struct isl_map
*map
)
5278 isl_ctx_deref(map
->ctx
);
5279 for (i
= 0; i
< map
->n
; ++i
)
5280 isl_basic_map_free(map
->p
[i
]);
5281 isl_space_free(map
->dim
);
5287 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5288 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5292 base
= isl_map_cow(base
);
5296 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5299 for (i
= 0; i
< base
->n
; ++i
) {
5300 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5301 isl_space_copy(base
->dim
), 0, 0, 0);
5311 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5312 unsigned nparam
, unsigned dim
)
5314 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5318 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5319 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5323 bmap
= isl_basic_map_cow(bmap
);
5324 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5325 j
= isl_basic_map_alloc_equality(bmap
);
5328 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5329 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5330 isl_int_set_si(bmap
->eq
[j
][0], value
);
5331 bmap
= isl_basic_map_simplify(bmap
);
5332 return isl_basic_map_finalize(bmap
);
5334 isl_basic_map_free(bmap
);
5338 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5339 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5343 bmap
= isl_basic_map_cow(bmap
);
5344 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5345 j
= isl_basic_map_alloc_equality(bmap
);
5348 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5349 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5350 isl_int_set(bmap
->eq
[j
][0], value
);
5351 bmap
= isl_basic_map_simplify(bmap
);
5352 return isl_basic_map_finalize(bmap
);
5354 isl_basic_map_free(bmap
);
5358 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5359 enum isl_dim_type type
, unsigned pos
, int value
)
5363 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5364 return isl_basic_map_fix_pos_si(bmap
,
5365 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5367 isl_basic_map_free(bmap
);
5371 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5372 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5376 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5377 return isl_basic_map_fix_pos(bmap
,
5378 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5380 isl_basic_map_free(bmap
);
5384 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5385 * to be equal to "v".
5387 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5388 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5392 if (!isl_val_is_int(v
))
5393 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5394 "expecting integer value", goto error
);
5395 if (pos
>= isl_basic_map_dim(bmap
, type
))
5396 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5397 "index out of bounds", goto error
);
5398 pos
+= isl_basic_map_offset(bmap
, type
);
5399 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5403 isl_basic_map_free(bmap
);
5408 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5409 * to be equal to "v".
5411 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5412 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5414 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5417 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5418 enum isl_dim_type type
, unsigned pos
, int value
)
5420 return (struct isl_basic_set
*)
5421 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5425 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5426 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5428 return (struct isl_basic_set
*)
5429 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5433 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5434 unsigned input
, int value
)
5436 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5439 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5440 unsigned dim
, int value
)
5442 return (struct isl_basic_set
*)
5443 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5444 isl_dim_set
, dim
, value
);
5447 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5449 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5456 isl_basic_map_free(map
->p
[i
]);
5457 if (i
!= map
->n
- 1) {
5458 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5459 map
->p
[i
] = map
->p
[map
->n
- 1];
5466 /* Perform "fn" on each basic map of "map", where we may not be holding
5467 * the only reference to "map".
5468 * In particular, "fn" should be a semantics preserving operation
5469 * that we want to apply to all copies of "map". We therefore need
5470 * to be careful not to modify "map" in a way that breaks "map"
5471 * in case anything goes wrong.
5473 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5474 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5476 struct isl_basic_map
*bmap
;
5482 for (i
= map
->n
- 1; i
>= 0; --i
) {
5483 bmap
= isl_basic_map_copy(map
->p
[i
]);
5487 isl_basic_map_free(map
->p
[i
]);
5489 if (remove_if_empty(map
, i
) < 0)
5499 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5500 enum isl_dim_type type
, unsigned pos
, int value
)
5504 map
= isl_map_cow(map
);
5508 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5509 for (i
= map
->n
- 1; i
>= 0; --i
) {
5510 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5511 if (remove_if_empty(map
, i
) < 0)
5514 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5521 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5522 enum isl_dim_type type
, unsigned pos
, int value
)
5524 return (struct isl_set
*)
5525 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5528 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5529 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5533 map
= isl_map_cow(map
);
5537 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5538 for (i
= 0; i
< map
->n
; ++i
) {
5539 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5543 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5550 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5551 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5553 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5556 /* Fix the value of the variable at position "pos" of type "type" of "map"
5557 * to be equal to "v".
5559 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5560 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5564 map
= isl_map_cow(map
);
5568 if (!isl_val_is_int(v
))
5569 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5570 "expecting integer value", goto error
);
5571 if (pos
>= isl_map_dim(map
, type
))
5572 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5573 "index out of bounds", goto error
);
5574 for (i
= map
->n
- 1; i
>= 0; --i
) {
5575 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5577 if (remove_if_empty(map
, i
) < 0)
5580 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5589 /* Fix the value of the variable at position "pos" of type "type" of "set"
5590 * to be equal to "v".
5592 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5593 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5595 return isl_map_fix_val(set
, type
, pos
, v
);
5598 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5599 unsigned input
, int value
)
5601 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5604 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5606 return (struct isl_set
*)
5607 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5610 static __isl_give isl_basic_map
*basic_map_bound_si(
5611 __isl_take isl_basic_map
*bmap
,
5612 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5618 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5619 pos
+= isl_basic_map_offset(bmap
, type
);
5620 bmap
= isl_basic_map_cow(bmap
);
5621 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5622 j
= isl_basic_map_alloc_inequality(bmap
);
5625 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5627 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5628 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5630 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5631 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5633 bmap
= isl_basic_map_simplify(bmap
);
5634 return isl_basic_map_finalize(bmap
);
5636 isl_basic_map_free(bmap
);
5640 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5641 __isl_take isl_basic_map
*bmap
,
5642 enum isl_dim_type type
, unsigned pos
, int value
)
5644 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5647 /* Constrain the values of the given dimension to be no greater than "value".
5649 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5650 __isl_take isl_basic_map
*bmap
,
5651 enum isl_dim_type type
, unsigned pos
, int value
)
5653 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5656 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5657 unsigned dim
, isl_int value
)
5661 bset
= isl_basic_set_cow(bset
);
5662 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5663 j
= isl_basic_set_alloc_inequality(bset
);
5666 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5667 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5668 isl_int_neg(bset
->ineq
[j
][0], value
);
5669 bset
= isl_basic_set_simplify(bset
);
5670 return isl_basic_set_finalize(bset
);
5672 isl_basic_set_free(bset
);
5676 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5677 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5681 map
= isl_map_cow(map
);
5685 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5686 for (i
= 0; i
< map
->n
; ++i
) {
5687 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5688 type
, pos
, value
, upper
);
5692 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5699 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5700 enum isl_dim_type type
, unsigned pos
, int value
)
5702 return map_bound_si(map
, type
, pos
, value
, 0);
5705 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5706 enum isl_dim_type type
, unsigned pos
, int value
)
5708 return map_bound_si(map
, type
, pos
, value
, 1);
5711 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5712 enum isl_dim_type type
, unsigned pos
, int value
)
5714 return (struct isl_set
*)
5715 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5718 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5719 enum isl_dim_type type
, unsigned pos
, int value
)
5721 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5724 /* Bound the given variable of "bmap" from below (or above is "upper"
5725 * is set) to "value".
5727 static __isl_give isl_basic_map
*basic_map_bound(
5728 __isl_take isl_basic_map
*bmap
,
5729 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5735 if (pos
>= isl_basic_map_dim(bmap
, type
))
5736 isl_die(bmap
->ctx
, isl_error_invalid
,
5737 "index out of bounds", goto error
);
5738 pos
+= isl_basic_map_offset(bmap
, type
);
5739 bmap
= isl_basic_map_cow(bmap
);
5740 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5741 j
= isl_basic_map_alloc_inequality(bmap
);
5744 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5746 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5747 isl_int_set(bmap
->ineq
[j
][0], value
);
5749 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5750 isl_int_neg(bmap
->ineq
[j
][0], value
);
5752 bmap
= isl_basic_map_simplify(bmap
);
5753 return isl_basic_map_finalize(bmap
);
5755 isl_basic_map_free(bmap
);
5759 /* Bound the given variable of "map" from below (or above is "upper"
5760 * is set) to "value".
5762 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5763 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5767 map
= isl_map_cow(map
);
5771 if (pos
>= isl_map_dim(map
, type
))
5772 isl_die(map
->ctx
, isl_error_invalid
,
5773 "index out of bounds", goto error
);
5774 for (i
= map
->n
- 1; i
>= 0; --i
) {
5775 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5776 if (remove_if_empty(map
, i
) < 0)
5779 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5786 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5787 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5789 return map_bound(map
, type
, pos
, value
, 0);
5792 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5793 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5795 return map_bound(map
, type
, pos
, value
, 1);
5798 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5799 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5801 return isl_map_lower_bound(set
, type
, pos
, value
);
5804 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5805 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5807 return isl_map_upper_bound(set
, type
, pos
, value
);
5810 /* Force the values of the variable at position "pos" of type "type" of "set"
5811 * to be no smaller than "value".
5813 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5814 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5818 if (!isl_val_is_int(value
))
5819 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5820 "expecting integer value", goto error
);
5821 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5822 isl_val_free(value
);
5825 isl_val_free(value
);
5830 /* Force the values of the variable at position "pos" of type "type" of "set"
5831 * to be no greater than "value".
5833 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5834 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5838 if (!isl_val_is_int(value
))
5839 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5840 "expecting integer value", goto error
);
5841 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5842 isl_val_free(value
);
5845 isl_val_free(value
);
5850 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5855 set
= isl_set_cow(set
);
5859 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5860 for (i
= 0; i
< set
->n
; ++i
) {
5861 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5871 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5875 map
= isl_map_cow(map
);
5879 map
->dim
= isl_space_reverse(map
->dim
);
5882 for (i
= 0; i
< map
->n
; ++i
) {
5883 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5887 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5894 static struct isl_map
*isl_basic_map_partial_lexopt(
5895 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5896 struct isl_set
**empty
, int max
)
5898 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5901 struct isl_map
*isl_basic_map_partial_lexmax(
5902 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5903 struct isl_set
**empty
)
5905 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5908 struct isl_map
*isl_basic_map_partial_lexmin(
5909 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5910 struct isl_set
**empty
)
5912 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5915 struct isl_set
*isl_basic_set_partial_lexmin(
5916 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5917 struct isl_set
**empty
)
5919 return (struct isl_set
*)
5920 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5924 struct isl_set
*isl_basic_set_partial_lexmax(
5925 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5926 struct isl_set
**empty
)
5928 return (struct isl_set
*)
5929 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5933 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5934 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5935 __isl_give isl_set
**empty
)
5937 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5940 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5941 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5942 __isl_give isl_set
**empty
)
5944 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5947 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5948 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5949 __isl_give isl_set
**empty
)
5951 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5954 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5955 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5956 __isl_give isl_set
**empty
)
5958 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5961 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5962 __isl_take isl_basic_map
*bmap
, int max
)
5964 isl_basic_set
*dom
= NULL
;
5965 isl_space
*dom_space
;
5969 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5970 dom
= isl_basic_set_universe(dom_space
);
5971 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5973 isl_basic_map_free(bmap
);
5977 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5978 __isl_take isl_basic_map
*bmap
)
5980 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5984 #define TYPE isl_pw_multi_aff
5986 #define SUFFIX _pw_multi_aff
5988 #define EMPTY isl_pw_multi_aff_empty
5990 #define ADD isl_pw_multi_aff_union_add
5991 #include "isl_map_lexopt_templ.c"
5993 /* Given a map "map", compute the lexicographically minimal
5994 * (or maximal) image element for each domain element in dom,
5995 * in the form of an isl_pw_multi_aff.
5996 * Set *empty to those elements in dom that do not have an image element.
5998 * We first compute the lexicographically minimal or maximal element
5999 * in the first basic map. This results in a partial solution "res"
6000 * and a subset "todo" of dom that still need to be handled.
6001 * We then consider each of the remaining maps in "map" and successively
6002 * update both "res" and "todo".
6004 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6005 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6006 __isl_give isl_set
**empty
, int max
)
6009 isl_pw_multi_aff
*res
;
6015 if (isl_map_plain_is_empty(map
)) {
6020 return isl_pw_multi_aff_from_map(map
);
6023 res
= basic_map_partial_lexopt_pw_multi_aff(
6024 isl_basic_map_copy(map
->p
[0]),
6025 isl_set_copy(dom
), &todo
, max
);
6027 for (i
= 1; i
< map
->n
; ++i
) {
6028 isl_pw_multi_aff
*res_i
;
6031 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6032 isl_basic_map_copy(map
->p
[i
]),
6033 isl_set_copy(dom
), &todo_i
, max
);
6036 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6038 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6040 todo
= isl_set_intersect(todo
, todo_i
);
6061 #define TYPE isl_map
6065 #define EMPTY isl_map_empty
6067 #define ADD isl_map_union_disjoint
6068 #include "isl_map_lexopt_templ.c"
6070 /* Given a map "map", compute the lexicographically minimal
6071 * (or maximal) image element for each domain element in dom.
6072 * Set *empty to those elements in dom that do not have an image element.
6074 * We first compute the lexicographically minimal or maximal element
6075 * in the first basic map. This results in a partial solution "res"
6076 * and a subset "todo" of dom that still need to be handled.
6077 * We then consider each of the remaining maps in "map" and successively
6078 * update both "res" and "todo".
6080 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6081 * Assume we are computing the lexicographical maximum.
6082 * We first compute the lexicographically maximal element in basic map i.
6083 * This results in a partial solution res_i and a subset todo_i.
6084 * Then we combine these results with those obtain for the first k basic maps
6085 * to obtain a result that is valid for the first k+1 basic maps.
6086 * In particular, the set where there is no solution is the set where
6087 * there is no solution for the first k basic maps and also no solution
6088 * for the ith basic map, i.e.,
6090 * todo^i = todo^k * todo_i
6092 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6093 * solutions, arbitrarily breaking ties in favor of res^k.
6094 * That is, when res^k(a) >= res_i(a), we pick res^k and
6095 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6096 * the lexicographic order.)
6097 * In practice, we compute
6099 * res^k * (res_i . "<=")
6103 * res_i * (res^k . "<")
6105 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6106 * where only one of res^k and res_i provides a solution and we simply pick
6113 * Note that we only compute these intersections when dom(res^k) intersects
6114 * dom(res_i). Otherwise, the only effect of these intersections is to
6115 * potentially break up res^k and res_i into smaller pieces.
6116 * We want to avoid such splintering as much as possible.
6117 * In fact, an earlier implementation of this function would look for
6118 * better results in the domain of res^k and for extra results in todo^k,
6119 * but this would always result in a splintering according to todo^k,
6120 * even when the domain of basic map i is disjoint from the domains of
6121 * the previous basic maps.
6123 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6124 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6125 __isl_give isl_set
**empty
, int max
)
6128 struct isl_map
*res
;
6129 struct isl_set
*todo
;
6134 if (isl_map_plain_is_empty(map
)) {
6142 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6143 isl_set_copy(dom
), &todo
, max
);
6145 for (i
= 1; i
< map
->n
; ++i
) {
6149 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6151 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6152 isl_set_copy(dom
), &todo_i
, max
);
6155 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6156 le
= isl_map_lex_le(dim
);
6158 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6159 le
= isl_map_lex_ge(dim
);
6161 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6162 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6163 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6164 le
= isl_map_intersect(le
, isl_map_copy(res
));
6166 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6167 res
= isl_map_intersect_domain(res
,
6168 isl_set_copy(todo_i
));
6169 res_i
= isl_map_intersect_domain(res_i
,
6170 isl_set_copy(todo
));
6173 res
= isl_map_union_disjoint(res
, res_i
);
6174 res
= isl_map_union_disjoint(res
, lt
);
6175 res
= isl_map_union_disjoint(res
, le
);
6177 todo
= isl_set_intersect(todo
, todo_i
);
6197 __isl_give isl_map
*isl_map_partial_lexmax(
6198 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6199 __isl_give isl_set
**empty
)
6201 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6204 __isl_give isl_map
*isl_map_partial_lexmin(
6205 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6206 __isl_give isl_set
**empty
)
6208 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6211 __isl_give isl_set
*isl_set_partial_lexmin(
6212 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6213 __isl_give isl_set
**empty
)
6215 return (struct isl_set
*)
6216 isl_map_partial_lexmin((struct isl_map
*)set
,
6220 __isl_give isl_set
*isl_set_partial_lexmax(
6221 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6222 __isl_give isl_set
**empty
)
6224 return (struct isl_set
*)
6225 isl_map_partial_lexmax((struct isl_map
*)set
,
6229 /* Compute the lexicographic minimum (or maximum if "max" is set)
6230 * of "bmap" over its domain.
6232 * Since we are not interested in the part of the domain space where
6233 * there is no solution, we initialize the domain to those constraints
6234 * of "bmap" that only involve the parameters and the input dimensions.
6235 * This relieves the parametric programming engine from detecting those
6236 * inequalities and transferring them to the context. More importantly,
6237 * it ensures that those inequalities are transferred first and not
6238 * intermixed with inequalities that actually split the domain.
6240 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6244 isl_basic_map
*copy
;
6247 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6248 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6249 copy
= isl_basic_map_copy(bmap
);
6250 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6251 isl_dim_div
, 0, n_div
);
6252 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6253 isl_dim_out
, 0, n_out
);
6254 dom
= isl_basic_map_domain(copy
);
6255 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6258 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6260 return isl_basic_map_lexopt(bmap
, 0);
6263 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6265 return isl_basic_map_lexopt(bmap
, 1);
6268 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6270 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6273 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6275 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6278 /* Extract the first and only affine expression from list
6279 * and then add it to *pwaff with the given dom.
6280 * This domain is known to be disjoint from other domains
6281 * because of the way isl_basic_map_foreach_lexmax works.
6283 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6284 __isl_take isl_aff_list
*list
, void *user
)
6286 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6288 isl_pw_aff
**pwaff
= user
;
6289 isl_pw_aff
*pwaff_i
;
6293 if (isl_aff_list_n_aff(list
) != 1)
6294 isl_die(ctx
, isl_error_internal
,
6295 "expecting single element list", goto error
);
6297 aff
= isl_aff_list_get_aff(list
, 0);
6298 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6300 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6302 isl_aff_list_free(list
);
6306 isl_basic_set_free(dom
);
6307 isl_aff_list_free(list
);
6311 /* Given a basic map with one output dimension, compute the minimum or
6312 * maximum of that dimension as an isl_pw_aff.
6314 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6315 * call update_dim_opt on each leaf of the result.
6317 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6320 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6324 dim
= isl_space_from_domain(isl_space_domain(dim
));
6325 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6326 pwaff
= isl_pw_aff_empty(dim
);
6328 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6330 return isl_pw_aff_free(pwaff
);
6335 /* Compute the minimum or maximum of the given output dimension
6336 * as a function of the parameters and the input dimensions,
6337 * but independently of the other output dimensions.
6339 * We first project out the other output dimension and then compute
6340 * the "lexicographic" maximum in each basic map, combining the results
6341 * using isl_pw_aff_union_max.
6343 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6350 n_out
= isl_map_dim(map
, isl_dim_out
);
6351 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6352 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6357 isl_space
*dim
= isl_map_get_space(map
);
6358 dim
= isl_space_domain(isl_space_from_range(dim
));
6360 return isl_pw_aff_empty(dim
);
6363 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6364 for (i
= 1; i
< map
->n
; ++i
) {
6365 isl_pw_aff
*pwaff_i
;
6367 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6368 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6376 /* Compute the maximum of the given output dimension as a function of the
6377 * parameters and input dimensions, but independently of
6378 * the other output dimensions.
6380 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6382 return map_dim_opt(map
, pos
, 1);
6385 /* Compute the minimum or maximum of the given set dimension
6386 * as a function of the parameters,
6387 * but independently of the other set dimensions.
6389 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6392 return map_dim_opt(set
, pos
, max
);
6395 /* Compute the maximum of the given set dimension as a function of the
6396 * parameters, but independently of the other set dimensions.
6398 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6400 return set_dim_opt(set
, pos
, 1);
6403 /* Compute the minimum of the given set dimension as a function of the
6404 * parameters, but independently of the other set dimensions.
6406 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6408 return set_dim_opt(set
, pos
, 0);
6411 /* Apply a preimage specified by "mat" on the parameters of "bset".
6412 * bset is assumed to have only parameters and divs.
6414 static struct isl_basic_set
*basic_set_parameter_preimage(
6415 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6422 bset
->dim
= isl_space_cow(bset
->dim
);
6426 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6428 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6430 bset
->dim
->nparam
= 0;
6431 bset
->dim
->n_out
= nparam
;
6432 bset
= isl_basic_set_preimage(bset
, mat
);
6434 bset
->dim
->nparam
= bset
->dim
->n_out
;
6435 bset
->dim
->n_out
= 0;
6440 isl_basic_set_free(bset
);
6444 /* Apply a preimage specified by "mat" on the parameters of "set".
6445 * set is assumed to have only parameters and divs.
6447 static struct isl_set
*set_parameter_preimage(
6448 struct isl_set
*set
, struct isl_mat
*mat
)
6450 isl_space
*dim
= NULL
;
6456 dim
= isl_space_copy(set
->dim
);
6457 dim
= isl_space_cow(dim
);
6461 nparam
= isl_set_dim(set
, isl_dim_param
);
6463 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6466 dim
->n_out
= nparam
;
6467 isl_set_reset_space(set
, dim
);
6468 set
= isl_set_preimage(set
, mat
);
6471 dim
= isl_space_copy(set
->dim
);
6472 dim
= isl_space_cow(dim
);
6475 dim
->nparam
= dim
->n_out
;
6477 isl_set_reset_space(set
, dim
);
6480 isl_space_free(dim
);
6487 /* Intersect the basic set "bset" with the affine space specified by the
6488 * equalities in "eq".
6490 static struct isl_basic_set
*basic_set_append_equalities(
6491 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6499 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6504 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6505 for (i
= 0; i
< eq
->n_row
; ++i
) {
6506 k
= isl_basic_set_alloc_equality(bset
);
6509 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6510 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6514 bset
= isl_basic_set_gauss(bset
, NULL
);
6515 bset
= isl_basic_set_finalize(bset
);
6520 isl_basic_set_free(bset
);
6524 /* Intersect the set "set" with the affine space specified by the
6525 * equalities in "eq".
6527 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6535 for (i
= 0; i
< set
->n
; ++i
) {
6536 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6549 /* Given a basic set "bset" that only involves parameters and existentially
6550 * quantified variables, return the index of the first equality
6551 * that only involves parameters. If there is no such equality then
6552 * return bset->n_eq.
6554 * This function assumes that isl_basic_set_gauss has been called on "bset".
6556 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6559 unsigned nparam
, n_div
;
6564 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6565 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6567 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6568 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6575 /* Compute an explicit representation for the existentially quantified
6576 * variables in "bset" by computing the "minimal value" of the set
6577 * variables. Since there are no set variables, the computation of
6578 * the minimal value essentially computes an explicit representation
6579 * of the non-empty part(s) of "bset".
6581 * The input only involves parameters and existentially quantified variables.
6582 * All equalities among parameters have been removed.
6584 * Since the existentially quantified variables in the result are in general
6585 * going to be different from those in the input, we first replace
6586 * them by the minimal number of variables based on their equalities.
6587 * This should simplify the parametric integer programming.
6589 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6591 isl_morph
*morph1
, *morph2
;
6597 if (bset
->n_eq
== 0)
6598 return isl_basic_set_lexmin(bset
);
6600 morph1
= isl_basic_set_parameter_compression(bset
);
6601 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6602 bset
= isl_basic_set_lift(bset
);
6603 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6604 bset
= isl_morph_basic_set(morph2
, bset
);
6605 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6606 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6608 set
= isl_basic_set_lexmin(bset
);
6610 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6615 /* Project the given basic set onto its parameter domain, possibly introducing
6616 * new, explicit, existential variables in the constraints.
6617 * The input has parameters and (possibly implicit) existential variables.
6618 * The output has the same parameters, but only
6619 * explicit existentially quantified variables.
6621 * The actual projection is performed by pip, but pip doesn't seem
6622 * to like equalities very much, so we first remove the equalities
6623 * among the parameters by performing a variable compression on
6624 * the parameters. Afterward, an inverse transformation is performed
6625 * and the equalities among the parameters are inserted back in.
6627 * The variable compression on the parameters may uncover additional
6628 * equalities that were only implicit before. We therefore check
6629 * if there are any new parameter equalities in the result and
6630 * if so recurse. The removal of parameter equalities is required
6631 * for the parameter compression performed by base_compute_divs.
6633 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6637 struct isl_mat
*T
, *T2
;
6638 struct isl_set
*set
;
6641 bset
= isl_basic_set_cow(bset
);
6645 if (bset
->n_eq
== 0)
6646 return base_compute_divs(bset
);
6648 bset
= isl_basic_set_gauss(bset
, NULL
);
6651 if (isl_basic_set_plain_is_empty(bset
))
6652 return isl_set_from_basic_set(bset
);
6654 i
= first_parameter_equality(bset
);
6655 if (i
== bset
->n_eq
)
6656 return base_compute_divs(bset
);
6658 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6659 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6661 eq
= isl_mat_cow(eq
);
6662 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6663 if (T
&& T
->n_col
== 0) {
6667 bset
= isl_basic_set_set_to_empty(bset
);
6668 return isl_set_from_basic_set(bset
);
6670 bset
= basic_set_parameter_preimage(bset
, T
);
6672 i
= first_parameter_equality(bset
);
6675 else if (i
== bset
->n_eq
)
6676 set
= base_compute_divs(bset
);
6678 set
= parameter_compute_divs(bset
);
6679 set
= set_parameter_preimage(set
, T2
);
6680 set
= set_append_equalities(set
, eq
);
6684 /* Insert the divs from "ls" before those of "bmap".
6686 * The number of columns is not changed, which means that the last
6687 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6688 * The caller is responsible for removing the same number of dimensions
6689 * from the space of "bmap".
6691 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6692 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6698 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6702 old_n_div
= bmap
->n_div
;
6703 bmap
= insert_div_rows(bmap
, n_div
);
6707 for (i
= 0; i
< n_div
; ++i
) {
6708 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6709 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6715 /* Replace the space of "bmap" by the space and divs of "ls".
6717 * If "ls" has any divs, then we simplify the result since we may
6718 * have discovered some additional equalities that could simplify
6719 * the div expressions.
6721 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6722 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6726 bmap
= isl_basic_map_cow(bmap
);
6730 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6731 bmap
= insert_divs_from_local_space(bmap
, ls
);
6735 isl_space_free(bmap
->dim
);
6736 bmap
->dim
= isl_local_space_get_space(ls
);
6740 isl_local_space_free(ls
);
6742 bmap
= isl_basic_map_simplify(bmap
);
6743 bmap
= isl_basic_map_finalize(bmap
);
6746 isl_basic_map_free(bmap
);
6747 isl_local_space_free(ls
);
6751 /* Replace the space of "map" by the space and divs of "ls".
6753 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6754 __isl_take isl_local_space
*ls
)
6758 map
= isl_map_cow(map
);
6762 for (i
= 0; i
< map
->n
; ++i
) {
6763 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6764 isl_local_space_copy(ls
));
6768 isl_space_free(map
->dim
);
6769 map
->dim
= isl_local_space_get_space(ls
);
6773 isl_local_space_free(ls
);
6776 isl_local_space_free(ls
);
6781 /* Compute an explicit representation for the existentially
6782 * quantified variables for which do not know any explicit representation yet.
6784 * We first sort the existentially quantified variables so that the
6785 * existentially quantified variables for which we already have an explicit
6786 * representation are placed before those for which we do not.
6787 * The input dimensions, the output dimensions and the existentially
6788 * quantified variables for which we already have an explicit
6789 * representation are then turned into parameters.
6790 * compute_divs returns a map with the same parameters and
6791 * no input or output dimensions and the dimension specification
6792 * is reset to that of the input, including the existentially quantified
6793 * variables for which we already had an explicit representation.
6795 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6797 struct isl_basic_set
*bset
;
6798 struct isl_set
*set
;
6799 struct isl_map
*map
;
6801 isl_local_space
*ls
;
6808 bmap
= isl_basic_map_sort_divs(bmap
);
6809 bmap
= isl_basic_map_cow(bmap
);
6813 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6814 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6817 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6818 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6819 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6820 dim
= isl_space_set_alloc(bmap
->ctx
,
6821 nparam
+ n_in
+ n_out
+ n_known
, 0);
6825 ls
= isl_basic_map_get_local_space(bmap
);
6826 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6827 n_known
, bmap
->n_div
- n_known
);
6829 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6830 swap_div(bmap
, i
- n_known
, i
);
6831 bmap
->n_div
-= n_known
;
6832 bmap
->extra
-= n_known
;
6834 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6835 bset
= (struct isl_basic_set
*)bmap
;
6837 set
= parameter_compute_divs(bset
);
6838 map
= (struct isl_map
*)set
;
6839 map
= replace_space_by_local_space(map
, ls
);
6843 isl_basic_map_free(bmap
);
6847 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6855 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6856 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6857 if (isl_int_is_zero(bmap
->div
[i
][0]))
6859 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6865 static int map_divs_known(__isl_keep isl_map
*map
)
6872 for (i
= 0; i
< map
->n
; ++i
) {
6873 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6881 /* If bmap contains any unknown divs, then compute explicit
6882 * expressions for them. However, this computation may be
6883 * quite expensive, so first try to remove divs that aren't
6886 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6889 struct isl_map
*map
;
6891 known
= isl_basic_map_divs_known(bmap
);
6895 return isl_map_from_basic_map(bmap
);
6897 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6899 known
= isl_basic_map_divs_known(bmap
);
6903 return isl_map_from_basic_map(bmap
);
6905 map
= compute_divs(bmap
);
6908 isl_basic_map_free(bmap
);
6912 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6916 struct isl_map
*res
;
6923 known
= map_divs_known(map
);
6931 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6932 for (i
= 1 ; i
< map
->n
; ++i
) {
6934 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6935 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6936 res
= isl_map_union_disjoint(res
, r2
);
6938 res
= isl_map_union(res
, r2
);
6945 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6947 return (struct isl_set
*)
6948 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6951 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6953 return (struct isl_set
*)
6954 isl_map_compute_divs((struct isl_map
*)set
);
6957 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6960 struct isl_set
*set
;
6965 map
= isl_map_cow(map
);
6969 set
= (struct isl_set
*)map
;
6970 set
->dim
= isl_space_domain(set
->dim
);
6973 for (i
= 0; i
< map
->n
; ++i
) {
6974 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6978 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6979 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6986 /* Return the union of "map1" and "map2", where we assume for now that
6987 * "map1" and "map2" are disjoint. Note that the basic maps inside
6988 * "map1" or "map2" may not be disjoint from each other.
6989 * Also note that this function is also called from isl_map_union,
6990 * which takes care of handling the situation where "map1" and "map2"
6991 * may not be disjoint.
6993 * If one of the inputs is empty, we can simply return the other input.
6994 * Similarly, if one of the inputs is universal, then it is equal to the union.
6996 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6997 __isl_take isl_map
*map2
)
7001 struct isl_map
*map
= NULL
;
7016 is_universe
= isl_map_plain_is_universe(map1
);
7017 if (is_universe
< 0)
7024 is_universe
= isl_map_plain_is_universe(map2
);
7025 if (is_universe
< 0)
7032 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
7034 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7035 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7036 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7038 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7039 map1
->n
+ map2
->n
, flags
);
7042 for (i
= 0; i
< map1
->n
; ++i
) {
7043 map
= isl_map_add_basic_map(map
,
7044 isl_basic_map_copy(map1
->p
[i
]));
7048 for (i
= 0; i
< map2
->n
; ++i
) {
7049 map
= isl_map_add_basic_map(map
,
7050 isl_basic_map_copy(map2
->p
[i
]));
7064 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7065 __isl_take isl_map
*map2
)
7067 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7070 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
7072 map1
= isl_map_union_disjoint(map1
, map2
);
7076 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7080 struct isl_set
*isl_set_union_disjoint(
7081 struct isl_set
*set1
, struct isl_set
*set2
)
7083 return (struct isl_set
*)
7084 isl_map_union_disjoint(
7085 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7088 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7090 return (struct isl_set
*)
7091 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7094 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7097 * "map" and "set" are assumed to be compatible and non-NULL.
7099 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7100 __isl_take isl_set
*set
,
7101 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7102 __isl_take isl_basic_set
*bset
))
7105 struct isl_map
*result
;
7108 if (isl_set_plain_is_universe(set
)) {
7113 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7114 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7115 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7117 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7118 map
->n
* set
->n
, flags
);
7119 for (i
= 0; result
&& i
< map
->n
; ++i
)
7120 for (j
= 0; j
< set
->n
; ++j
) {
7121 result
= isl_map_add_basic_map(result
,
7122 fn(isl_basic_map_copy(map
->p
[i
]),
7123 isl_basic_set_copy(set
->p
[j
])));
7133 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7134 __isl_take isl_set
*set
)
7139 if (!isl_map_compatible_range(map
, set
))
7140 isl_die(set
->ctx
, isl_error_invalid
,
7141 "incompatible spaces", goto error
);
7143 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7150 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7151 __isl_take isl_set
*set
)
7153 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7156 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7157 __isl_take isl_set
*set
)
7162 if (!isl_map_compatible_domain(map
, set
))
7163 isl_die(set
->ctx
, isl_error_invalid
,
7164 "incompatible spaces", goto error
);
7166 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7173 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7174 __isl_take isl_set
*set
)
7176 return isl_map_align_params_map_map_and(map
, set
,
7177 &map_intersect_domain
);
7180 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7181 __isl_take isl_map
*map2
)
7185 map1
= isl_map_reverse(map1
);
7186 map1
= isl_map_apply_range(map1
, map2
);
7187 return isl_map_reverse(map1
);
7194 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7195 __isl_take isl_map
*map2
)
7197 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7200 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7201 __isl_take isl_map
*map2
)
7203 isl_space
*dim_result
;
7204 struct isl_map
*result
;
7210 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7211 isl_space_copy(map2
->dim
));
7213 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7216 for (i
= 0; i
< map1
->n
; ++i
)
7217 for (j
= 0; j
< map2
->n
; ++j
) {
7218 result
= isl_map_add_basic_map(result
,
7219 isl_basic_map_apply_range(
7220 isl_basic_map_copy(map1
->p
[i
]),
7221 isl_basic_map_copy(map2
->p
[j
])));
7227 if (result
&& result
->n
<= 1)
7228 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7236 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7237 __isl_take isl_map
*map2
)
7239 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7243 * returns range - domain
7245 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7247 isl_space
*dims
, *target_dim
;
7248 struct isl_basic_set
*bset
;
7255 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7256 bmap
->dim
, isl_dim_out
),
7258 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7259 dim
= isl_basic_map_n_in(bmap
);
7260 nparam
= isl_basic_map_n_param(bmap
);
7261 bset
= isl_basic_set_from_basic_map(bmap
);
7262 bset
= isl_basic_set_cow(bset
);
7263 dims
= isl_basic_set_get_space(bset
);
7264 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7265 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7266 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7267 for (i
= 0; i
< dim
; ++i
) {
7268 int j
= isl_basic_map_alloc_equality(
7269 (struct isl_basic_map
*)bset
);
7271 bset
= isl_basic_set_free(bset
);
7274 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7275 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7276 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7277 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7279 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7280 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7283 isl_basic_map_free(bmap
);
7288 * returns range - domain
7290 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7294 struct isl_set
*result
;
7299 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7300 map
->dim
, isl_dim_out
),
7302 dim
= isl_map_get_space(map
);
7303 dim
= isl_space_domain(dim
);
7304 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7307 for (i
= 0; i
< map
->n
; ++i
)
7308 result
= isl_set_add_basic_set(result
,
7309 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7318 * returns [domain -> range] -> range - domain
7320 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7321 __isl_take isl_basic_map
*bmap
)
7325 isl_basic_map
*domain
;
7329 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7330 isl_die(bmap
->ctx
, isl_error_invalid
,
7331 "domain and range don't match", goto error
);
7333 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7334 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7336 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7337 domain
= isl_basic_map_universe(dim
);
7339 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7340 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7341 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7343 total
= isl_basic_map_total_dim(bmap
);
7345 for (i
= 0; i
< n
; ++i
) {
7346 k
= isl_basic_map_alloc_equality(bmap
);
7349 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7350 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7351 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7352 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7355 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7356 return isl_basic_map_finalize(bmap
);
7358 isl_basic_map_free(bmap
);
7363 * returns [domain -> range] -> range - domain
7365 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7368 isl_space
*domain_dim
;
7373 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7374 isl_die(map
->ctx
, isl_error_invalid
,
7375 "domain and range don't match", goto error
);
7377 map
= isl_map_cow(map
);
7381 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7382 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7383 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7386 for (i
= 0; i
< map
->n
; ++i
) {
7387 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7391 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7398 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7400 struct isl_basic_map
*bmap
;
7408 nparam
= dims
->nparam
;
7410 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7414 for (i
= 0; i
< dim
; ++i
) {
7415 int j
= isl_basic_map_alloc_equality(bmap
);
7418 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7419 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7420 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7422 return isl_basic_map_finalize(bmap
);
7424 isl_basic_map_free(bmap
);
7428 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7432 if (dim
->n_in
!= dim
->n_out
)
7433 isl_die(dim
->ctx
, isl_error_invalid
,
7434 "number of input and output dimensions needs to be "
7435 "the same", goto error
);
7436 return basic_map_identity(dim
);
7438 isl_space_free(dim
);
7442 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7444 if (!model
|| !model
->dim
)
7446 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7449 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7451 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7454 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7456 if (!model
|| !model
->dim
)
7458 return isl_map_identity(isl_space_copy(model
->dim
));
7461 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7463 if (!model
|| !model
->dim
)
7465 return isl_map_identity(isl_space_copy(model
->dim
));
7468 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7470 isl_space
*dim
= isl_set_get_space(set
);
7472 id
= isl_map_identity(isl_space_map_from_set(dim
));
7473 return isl_map_intersect_range(id
, set
);
7476 /* Construct a basic set with all set dimensions having only non-negative
7479 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7480 __isl_take isl_space
*space
)
7485 struct isl_basic_set
*bset
;
7489 nparam
= space
->nparam
;
7491 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7494 for (i
= 0; i
< dim
; ++i
) {
7495 int k
= isl_basic_set_alloc_inequality(bset
);
7498 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7499 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7503 isl_basic_set_free(bset
);
7507 /* Construct the half-space x_pos >= 0.
7509 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7513 isl_basic_set
*nonneg
;
7515 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7516 k
= isl_basic_set_alloc_inequality(nonneg
);
7519 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7520 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7522 return isl_basic_set_finalize(nonneg
);
7524 isl_basic_set_free(nonneg
);
7528 /* Construct the half-space x_pos <= -1.
7530 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7535 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7536 k
= isl_basic_set_alloc_inequality(neg
);
7539 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7540 isl_int_set_si(neg
->ineq
[k
][0], -1);
7541 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7543 return isl_basic_set_finalize(neg
);
7545 isl_basic_set_free(neg
);
7549 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7550 enum isl_dim_type type
, unsigned first
, unsigned n
)
7553 isl_basic_set
*nonneg
;
7561 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7563 for (i
= 0; i
< n
; ++i
) {
7564 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7565 pos(set
->dim
, type
) + first
+ i
);
7566 neg
= neg_halfspace(isl_set_get_space(set
),
7567 pos(set
->dim
, type
) + first
+ i
);
7569 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7578 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7579 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7586 if (isl_set_plain_is_empty(set
)) {
7591 return fn(set
, signs
, user
);
7594 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7596 half
= isl_set_intersect(half
, isl_set_copy(set
));
7597 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7601 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7603 half
= isl_set_intersect(half
, set
);
7604 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7610 /* Call "fn" on the intersections of "set" with each of the orthants
7611 * (except for obviously empty intersections). The orthant is identified
7612 * by the signs array, with each entry having value 1 or -1 according
7613 * to the sign of the corresponding variable.
7615 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7616 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7626 if (isl_set_plain_is_empty(set
))
7629 nparam
= isl_set_dim(set
, isl_dim_param
);
7630 nvar
= isl_set_dim(set
, isl_dim_set
);
7632 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7634 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7642 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7644 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7647 int isl_basic_map_is_subset(
7648 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7651 struct isl_map
*map1
;
7652 struct isl_map
*map2
;
7654 if (!bmap1
|| !bmap2
)
7657 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7658 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7660 is_subset
= isl_map_is_subset(map1
, map2
);
7668 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7669 __isl_keep isl_basic_set
*bset2
)
7671 return isl_basic_map_is_subset(bset1
, bset2
);
7674 int isl_basic_map_is_equal(
7675 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7679 if (!bmap1
|| !bmap2
)
7681 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7684 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7688 int isl_basic_set_is_equal(
7689 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7691 return isl_basic_map_is_equal(
7692 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7695 int isl_map_is_empty(struct isl_map
*map
)
7702 for (i
= 0; i
< map
->n
; ++i
) {
7703 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7712 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7714 return map
? map
->n
== 0 : -1;
7717 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7719 return isl_map_plain_is_empty(map
);
7722 int isl_set_plain_is_empty(struct isl_set
*set
)
7724 return set
? set
->n
== 0 : -1;
7727 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7729 return isl_set_plain_is_empty(set
);
7732 int isl_set_is_empty(struct isl_set
*set
)
7734 return isl_map_is_empty((struct isl_map
*)set
);
7737 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7742 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7745 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7750 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7753 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7759 is_subset
= isl_map_is_subset(map1
, map2
);
7762 is_subset
= isl_map_is_subset(map2
, map1
);
7766 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7768 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7771 int isl_basic_map_is_strict_subset(
7772 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7776 if (!bmap1
|| !bmap2
)
7778 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7781 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7782 if (is_subset
== -1)
7787 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7793 is_subset
= isl_map_is_subset(map1
, map2
);
7796 is_subset
= isl_map_is_subset(map2
, map1
);
7797 if (is_subset
== -1)
7802 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7804 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7807 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7811 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7814 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7818 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7821 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7828 for (i
= 0; i
< map
->n
; ++i
) {
7829 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7837 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7839 return isl_map_plain_is_universe((isl_map
*) set
);
7842 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7844 return isl_set_plain_is_universe(set
);
7847 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7849 struct isl_basic_set
*bset
= NULL
;
7850 struct isl_vec
*sample
= NULL
;
7857 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7860 if (isl_basic_map_is_universe(bmap
))
7863 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7864 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7865 copy
= isl_basic_map_remove_redundancies(copy
);
7866 empty
= isl_basic_map_plain_is_empty(copy
);
7867 isl_basic_map_free(copy
);
7871 total
= 1 + isl_basic_map_total_dim(bmap
);
7872 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7873 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7879 isl_vec_free(bmap
->sample
);
7880 bmap
->sample
= NULL
;
7881 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7884 sample
= isl_basic_set_sample_vec(bset
);
7887 empty
= sample
->size
== 0;
7888 isl_vec_free(bmap
->sample
);
7889 bmap
->sample
= sample
;
7891 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7896 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7900 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7903 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7905 return isl_basic_map_plain_is_empty(bmap
);
7908 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7912 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7915 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7917 return isl_basic_set_plain_is_empty(bset
);
7920 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7922 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7925 struct isl_map
*isl_basic_map_union(
7926 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7928 struct isl_map
*map
;
7929 if (!bmap1
|| !bmap2
)
7932 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7934 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7937 map
= isl_map_add_basic_map(map
, bmap1
);
7938 map
= isl_map_add_basic_map(map
, bmap2
);
7941 isl_basic_map_free(bmap1
);
7942 isl_basic_map_free(bmap2
);
7946 struct isl_set
*isl_basic_set_union(
7947 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7949 return (struct isl_set
*)isl_basic_map_union(
7950 (struct isl_basic_map
*)bset1
,
7951 (struct isl_basic_map
*)bset2
);
7954 /* Order divs such that any div only depends on previous divs */
7955 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7963 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7965 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7967 if (isl_int_is_zero(bmap
->div
[i
][0]))
7969 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7973 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7979 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7981 return (struct isl_basic_set
*)
7982 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7985 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7992 for (i
= 0; i
< map
->n
; ++i
) {
7993 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8004 /* Apply the expansion computed by isl_merge_divs.
8005 * The expansion itself is given by "exp" while the resulting
8006 * list of divs is given by "div".
8008 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8009 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8014 bset
= isl_basic_set_cow(bset
);
8018 if (div
->n_row
< bset
->n_div
)
8019 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8020 "not an expansion", goto error
);
8022 n_div
= bset
->n_div
;
8023 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8024 div
->n_row
- n_div
, 0,
8025 2 * (div
->n_row
- n_div
));
8027 for (i
= n_div
; i
< div
->n_row
; ++i
)
8028 if (isl_basic_set_alloc_div(bset
) < 0)
8032 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8033 if (j
>= 0 && exp
[j
] == i
) {
8035 isl_basic_map_swap_div(bset
, i
, j
);
8038 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8039 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8047 isl_basic_set_free(bset
);
8052 /* Look for a div in dst that corresponds to the div "div" in src.
8053 * The divs before "div" in src and dst are assumed to be the same.
8055 * Returns -1 if no corresponding div was found and the position
8056 * of the corresponding div in dst otherwise.
8058 static int find_div(struct isl_basic_map
*dst
,
8059 struct isl_basic_map
*src
, unsigned div
)
8063 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8065 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8066 for (i
= div
; i
< dst
->n_div
; ++i
)
8067 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8068 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8069 dst
->n_div
- div
) == -1)
8074 struct isl_basic_map
*isl_basic_map_align_divs(
8075 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8083 if (src
->n_div
== 0)
8086 for (i
= 0; i
< src
->n_div
; ++i
)
8087 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8089 src
= isl_basic_map_order_divs(src
);
8090 dst
= isl_basic_map_cow(dst
);
8093 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8094 src
->n_div
, 0, 2 * src
->n_div
);
8097 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8098 for (i
= 0; i
< src
->n_div
; ++i
) {
8099 int j
= find_div(dst
, src
, i
);
8101 j
= isl_basic_map_alloc_div(dst
);
8104 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8105 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8106 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8110 isl_basic_map_swap_div(dst
, i
, j
);
8114 isl_basic_map_free(dst
);
8118 struct isl_basic_set
*isl_basic_set_align_divs(
8119 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8121 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8122 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8125 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8133 map
= isl_map_compute_divs(map
);
8134 map
= isl_map_cow(map
);
8138 for (i
= 1; i
< map
->n
; ++i
)
8139 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8140 for (i
= 1; i
< map
->n
; ++i
) {
8141 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8143 return isl_map_free(map
);
8146 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8150 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8152 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8155 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8156 __isl_take isl_map
*map
)
8160 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8161 map
= isl_map_intersect_domain(map
, set
);
8162 set
= isl_map_range(map
);
8170 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8171 __isl_take isl_map
*map
)
8173 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8176 /* There is no need to cow as removing empty parts doesn't change
8177 * the meaning of the set.
8179 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8186 for (i
= map
->n
- 1; i
>= 0; --i
)
8187 remove_if_empty(map
, i
);
8192 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8194 return (struct isl_set
*)
8195 isl_map_remove_empty_parts((struct isl_map
*)set
);
8198 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8200 struct isl_basic_map
*bmap
;
8201 if (!map
|| map
->n
== 0)
8203 bmap
= map
->p
[map
->n
-1];
8204 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8205 return isl_basic_map_copy(bmap
);
8208 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8210 return (struct isl_basic_set
*)
8211 isl_map_copy_basic_map((struct isl_map
*)set
);
8214 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8215 __isl_keep isl_basic_map
*bmap
)
8221 for (i
= map
->n
-1; i
>= 0; --i
) {
8222 if (map
->p
[i
] != bmap
)
8224 map
= isl_map_cow(map
);
8227 isl_basic_map_free(map
->p
[i
]);
8228 if (i
!= map
->n
-1) {
8229 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8230 map
->p
[i
] = map
->p
[map
->n
-1];
8241 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8242 struct isl_basic_set
*bset
)
8244 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8245 (struct isl_basic_map
*)bset
);
8248 /* Given two basic sets bset1 and bset2, compute the maximal difference
8249 * between the values of dimension pos in bset1 and those in bset2
8250 * for any common value of the parameters and dimensions preceding pos.
8252 static enum isl_lp_result
basic_set_maximal_difference_at(
8253 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8254 int pos
, isl_int
*opt
)
8257 struct isl_basic_map
*bmap1
= NULL
;
8258 struct isl_basic_map
*bmap2
= NULL
;
8259 struct isl_ctx
*ctx
;
8260 struct isl_vec
*obj
;
8263 unsigned dim1
, dim2
;
8264 enum isl_lp_result res
;
8266 if (!bset1
|| !bset2
)
8267 return isl_lp_error
;
8269 nparam
= isl_basic_set_n_param(bset1
);
8270 dim1
= isl_basic_set_n_dim(bset1
);
8271 dim2
= isl_basic_set_n_dim(bset2
);
8272 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8273 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8274 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8275 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8276 if (!bmap1
|| !bmap2
)
8278 bmap1
= isl_basic_map_cow(bmap1
);
8279 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8280 pos
, (dim1
- pos
) + (dim2
- pos
),
8281 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8282 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8285 total
= isl_basic_map_total_dim(bmap1
);
8287 obj
= isl_vec_alloc(ctx
, 1 + total
);
8290 isl_seq_clr(obj
->block
.data
, 1 + total
);
8291 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8292 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8293 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8295 isl_basic_map_free(bmap1
);
8299 isl_basic_map_free(bmap2
);
8301 isl_basic_map_free(bmap1
);
8302 return isl_lp_error
;
8305 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8306 * for any common value of the parameters and dimensions preceding pos
8307 * in both basic sets, the values of dimension pos in bset1 are
8308 * smaller or larger than those in bset2.
8311 * 1 if bset1 follows bset2
8312 * -1 if bset1 precedes bset2
8313 * 0 if bset1 and bset2 are incomparable
8314 * -2 if some error occurred.
8316 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8317 struct isl_basic_set
*bset2
, int pos
)
8320 enum isl_lp_result res
;
8325 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8327 if (res
== isl_lp_empty
)
8329 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8330 res
== isl_lp_unbounded
)
8332 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8341 /* Given two basic sets bset1 and bset2, check whether
8342 * for any common value of the parameters and dimensions preceding pos
8343 * there is a value of dimension pos in bset1 that is larger
8344 * than a value of the same dimension in bset2.
8347 * 1 if there exists such a pair
8348 * 0 if there is no such pair, but there is a pair of equal values
8350 * -2 if some error occurred.
8352 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8353 __isl_keep isl_basic_set
*bset2
, int pos
)
8356 enum isl_lp_result res
;
8361 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8363 if (res
== isl_lp_empty
)
8365 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8366 res
== isl_lp_unbounded
)
8368 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8370 else if (res
== isl_lp_ok
)
8379 /* Given two sets set1 and set2, check whether
8380 * for any common value of the parameters and dimensions preceding pos
8381 * there is a value of dimension pos in set1 that is larger
8382 * than a value of the same dimension in set2.
8385 * 1 if there exists such a pair
8386 * 0 if there is no such pair, but there is a pair of equal values
8388 * -2 if some error occurred.
8390 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8391 __isl_keep isl_set
*set2
, int pos
)
8399 for (i
= 0; i
< set1
->n
; ++i
)
8400 for (j
= 0; j
< set2
->n
; ++j
) {
8402 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8403 if (f
== 1 || f
== -2)
8412 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8413 unsigned pos
, isl_int
*val
)
8421 total
= isl_basic_map_total_dim(bmap
);
8422 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8423 for (; d
+1 > pos
; --d
)
8424 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8428 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8430 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8432 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8435 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8441 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8442 unsigned pos
, isl_int
*val
)
8454 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8457 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8458 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8459 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8460 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8464 isl_int_set(*val
, v
);
8470 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8471 unsigned pos
, isl_int
*val
)
8473 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8477 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8480 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8483 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8484 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8486 if (pos
>= isl_basic_map_dim(bmap
, type
))
8488 return isl_basic_map_plain_has_fixed_var(bmap
,
8489 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8492 /* If "bmap" obviously lies on a hyperplane where the given dimension
8493 * has a fixed value, then return that value.
8494 * Otherwise return NaN.
8496 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8497 __isl_keep isl_basic_map
*bmap
,
8498 enum isl_dim_type type
, unsigned pos
)
8506 ctx
= isl_basic_map_get_ctx(bmap
);
8507 v
= isl_val_alloc(ctx
);
8510 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8512 return isl_val_free(v
);
8514 isl_int_set_si(v
->d
, 1);
8518 return isl_val_nan(ctx
);
8521 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8522 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8524 if (pos
>= isl_map_dim(map
, type
))
8526 return isl_map_plain_has_fixed_var(map
,
8527 map_offset(map
, type
) - 1 + pos
, val
);
8530 /* If "map" obviously lies on a hyperplane where the given dimension
8531 * has a fixed value, then return that value.
8532 * Otherwise return NaN.
8534 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8535 enum isl_dim_type type
, unsigned pos
)
8543 ctx
= isl_map_get_ctx(map
);
8544 v
= isl_val_alloc(ctx
);
8547 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8549 return isl_val_free(v
);
8551 isl_int_set_si(v
->d
, 1);
8555 return isl_val_nan(ctx
);
8558 /* If "set" obviously lies on a hyperplane where the given dimension
8559 * has a fixed value, then return that value.
8560 * Otherwise return NaN.
8562 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8563 enum isl_dim_type type
, unsigned pos
)
8565 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8568 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8569 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8571 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8574 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8575 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8577 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8580 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8581 * then return this fixed value in *val.
8583 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8584 unsigned dim
, isl_int
*val
)
8586 return isl_basic_set_plain_has_fixed_var(bset
,
8587 isl_basic_set_n_param(bset
) + dim
, val
);
8590 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8591 * then return this fixed value in *val.
8593 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8594 unsigned dim
, isl_int
*val
)
8596 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8599 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8600 unsigned dim
, isl_int
*val
)
8602 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8605 /* Check if input variable in has fixed value and if so and if val is not NULL,
8606 * then return this fixed value in *val.
8608 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8609 unsigned in
, isl_int
*val
)
8611 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8614 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8615 * and if val is not NULL, then return this lower bound in *val.
8617 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8618 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8620 int i
, i_eq
= -1, i_ineq
= -1;
8627 total
= isl_basic_set_total_dim(bset
);
8628 nparam
= isl_basic_set_n_param(bset
);
8629 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8630 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8636 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8637 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8639 if (i_eq
!= -1 || i_ineq
!= -1)
8643 if (i_eq
== -1 && i_ineq
== -1)
8645 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8646 /* The coefficient should always be one due to normalization. */
8647 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8649 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8651 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8652 total
- nparam
- dim
- 1) != -1)
8655 isl_int_neg(*val
, c
[0]);
8659 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8660 unsigned dim
, isl_int
*val
)
8672 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8676 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8678 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8679 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8681 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8685 isl_int_set(*val
, v
);
8696 /* uset_gist depends on constraints without existentially quantified
8697 * variables sorting first.
8699 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8701 const struct constraint
*c1
= (const struct constraint
*)p1
;
8702 const struct constraint
*c2
= (const struct constraint
*)p2
;
8704 unsigned size
= isl_min(c1
->size
, c2
->size
);
8706 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8707 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8712 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8715 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8716 struct isl_basic_map
*bmap
)
8719 struct constraint
*c
;
8724 if (bmap
->n_ineq
== 0)
8726 total
= isl_basic_map_total_dim(bmap
);
8727 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8730 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8732 c
[i
].c
= bmap
->ineq
[i
];
8734 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8735 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8736 bmap
->ineq
[i
] = c
[i
].c
;
8740 isl_basic_map_free(bmap
);
8744 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8745 __isl_take isl_basic_set
*bset
)
8747 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8748 (struct isl_basic_map
*)bset
);
8751 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8755 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8757 bmap
= isl_basic_map_remove_redundancies(bmap
);
8758 bmap
= isl_basic_map_sort_constraints(bmap
);
8760 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8764 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8766 return (struct isl_basic_set
*)isl_basic_map_normalize(
8767 (struct isl_basic_map
*)bset
);
8770 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8771 const __isl_keep isl_basic_map
*bmap2
)
8778 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8779 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8780 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8781 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8782 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8783 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8784 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8785 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8786 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8787 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8788 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8790 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8792 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8794 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8795 return bmap1
->n_eq
- bmap2
->n_eq
;
8796 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8797 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8798 if (bmap1
->n_div
!= bmap2
->n_div
)
8799 return bmap1
->n_div
- bmap2
->n_div
;
8800 total
= isl_basic_map_total_dim(bmap1
);
8801 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8802 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8806 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8807 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8811 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8812 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8819 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8820 const __isl_keep isl_basic_set
*bset2
)
8822 return isl_basic_map_plain_cmp(bset1
, bset2
);
8825 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8831 if (set1
->n
!= set2
->n
)
8832 return set1
->n
- set2
->n
;
8834 for (i
= 0; i
< set1
->n
; ++i
) {
8835 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8843 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8844 __isl_keep isl_basic_map
*bmap2
)
8846 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8849 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8850 __isl_keep isl_basic_set
*bset2
)
8852 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8853 (isl_basic_map
*)bset2
);
8856 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8858 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8859 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8861 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8864 /* We normalize in place, but if anything goes wrong we need
8865 * to return NULL, so we need to make sure we don't change the
8866 * meaning of any possible other copies of map.
8868 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8871 struct isl_basic_map
*bmap
;
8875 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8877 for (i
= 0; i
< map
->n
; ++i
) {
8878 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8881 isl_basic_map_free(map
->p
[i
]);
8884 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8885 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8886 map
= isl_map_remove_empty_parts(map
);
8889 for (i
= map
->n
- 1; i
>= 1; --i
) {
8890 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8892 isl_basic_map_free(map
->p
[i
-1]);
8893 for (j
= i
; j
< map
->n
; ++j
)
8894 map
->p
[j
-1] = map
->p
[j
];
8904 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8906 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8909 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8919 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8922 map1
= isl_map_copy(map1
);
8923 map2
= isl_map_copy(map2
);
8924 map1
= isl_map_normalize(map1
);
8925 map2
= isl_map_normalize(map2
);
8928 equal
= map1
->n
== map2
->n
;
8929 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8930 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8943 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8945 return isl_map_plain_is_equal(map1
, map2
);
8948 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8950 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8951 (struct isl_map
*)set2
);
8954 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8956 return isl_set_plain_is_equal(set1
, set2
);
8959 /* Return an interval that ranges from min to max (inclusive)
8961 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8962 isl_int min
, isl_int max
)
8965 struct isl_basic_set
*bset
= NULL
;
8967 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8971 k
= isl_basic_set_alloc_inequality(bset
);
8974 isl_int_set_si(bset
->ineq
[k
][1], 1);
8975 isl_int_neg(bset
->ineq
[k
][0], min
);
8977 k
= isl_basic_set_alloc_inequality(bset
);
8980 isl_int_set_si(bset
->ineq
[k
][1], -1);
8981 isl_int_set(bset
->ineq
[k
][0], max
);
8985 isl_basic_set_free(bset
);
8989 /* Return the Cartesian product of the basic sets in list (in the given order).
8991 __isl_give isl_basic_set
*isl_basic_set_list_product(
8992 __isl_take
struct isl_basic_set_list
*list
)
9000 struct isl_basic_set
*product
= NULL
;
9004 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9005 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9006 nparam
= isl_basic_set_n_param(list
->p
[0]);
9007 dim
= isl_basic_set_n_dim(list
->p
[0]);
9008 extra
= list
->p
[0]->n_div
;
9009 n_eq
= list
->p
[0]->n_eq
;
9010 n_ineq
= list
->p
[0]->n_ineq
;
9011 for (i
= 1; i
< list
->n
; ++i
) {
9012 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9013 isl_assert(list
->ctx
,
9014 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9015 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9016 extra
+= list
->p
[i
]->n_div
;
9017 n_eq
+= list
->p
[i
]->n_eq
;
9018 n_ineq
+= list
->p
[i
]->n_ineq
;
9020 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9025 for (i
= 0; i
< list
->n
; ++i
) {
9026 isl_basic_set_add_constraints(product
,
9027 isl_basic_set_copy(list
->p
[i
]), dim
);
9028 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9030 isl_basic_set_list_free(list
);
9033 isl_basic_set_free(product
);
9034 isl_basic_set_list_free(list
);
9038 struct isl_basic_map
*isl_basic_map_product(
9039 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9041 isl_space
*dim_result
= NULL
;
9042 struct isl_basic_map
*bmap
;
9043 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9044 struct isl_dim_map
*dim_map1
, *dim_map2
;
9046 if (!bmap1
|| !bmap2
)
9049 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9050 bmap2
->dim
, isl_dim_param
), goto error
);
9051 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9052 isl_space_copy(bmap2
->dim
));
9054 in1
= isl_basic_map_n_in(bmap1
);
9055 in2
= isl_basic_map_n_in(bmap2
);
9056 out1
= isl_basic_map_n_out(bmap1
);
9057 out2
= isl_basic_map_n_out(bmap2
);
9058 nparam
= isl_basic_map_n_param(bmap1
);
9060 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9061 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9062 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9063 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9064 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9065 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9066 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9067 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9068 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9069 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9070 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9072 bmap
= isl_basic_map_alloc_space(dim_result
,
9073 bmap1
->n_div
+ bmap2
->n_div
,
9074 bmap1
->n_eq
+ bmap2
->n_eq
,
9075 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9076 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9077 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9078 bmap
= isl_basic_map_simplify(bmap
);
9079 return isl_basic_map_finalize(bmap
);
9081 isl_basic_map_free(bmap1
);
9082 isl_basic_map_free(bmap2
);
9086 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9087 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9089 isl_basic_map
*prod
;
9091 prod
= isl_basic_map_product(bmap1
, bmap2
);
9092 prod
= isl_basic_map_flatten(prod
);
9096 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9097 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9099 return isl_basic_map_flat_range_product(bset1
, bset2
);
9102 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9103 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9105 isl_space
*space_result
= NULL
;
9106 isl_basic_map
*bmap
;
9107 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9108 struct isl_dim_map
*dim_map1
, *dim_map2
;
9110 if (!bmap1
|| !bmap2
)
9113 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9114 isl_space_copy(bmap2
->dim
));
9116 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9117 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9118 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9119 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9121 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9122 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9123 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9124 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9125 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9126 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9127 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9128 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9129 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9130 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9131 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9133 bmap
= isl_basic_map_alloc_space(space_result
,
9134 bmap1
->n_div
+ bmap2
->n_div
,
9135 bmap1
->n_eq
+ bmap2
->n_eq
,
9136 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9137 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9138 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9139 bmap
= isl_basic_map_simplify(bmap
);
9140 return isl_basic_map_finalize(bmap
);
9142 isl_basic_map_free(bmap1
);
9143 isl_basic_map_free(bmap2
);
9147 __isl_give isl_basic_map
*isl_basic_map_range_product(
9148 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9150 isl_space
*dim_result
= NULL
;
9151 isl_basic_map
*bmap
;
9152 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9153 struct isl_dim_map
*dim_map1
, *dim_map2
;
9155 if (!bmap1
|| !bmap2
)
9158 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9159 bmap2
->dim
, isl_dim_param
))
9160 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9161 "parameters don't match", goto error
);
9163 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9164 isl_space_copy(bmap2
->dim
));
9166 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9167 out1
= isl_basic_map_n_out(bmap1
);
9168 out2
= isl_basic_map_n_out(bmap2
);
9169 nparam
= isl_basic_map_n_param(bmap1
);
9171 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9172 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9173 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9174 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9175 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9176 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9177 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9178 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9179 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9180 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9181 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9183 bmap
= isl_basic_map_alloc_space(dim_result
,
9184 bmap1
->n_div
+ bmap2
->n_div
,
9185 bmap1
->n_eq
+ bmap2
->n_eq
,
9186 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9187 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9188 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9189 bmap
= isl_basic_map_simplify(bmap
);
9190 return isl_basic_map_finalize(bmap
);
9192 isl_basic_map_free(bmap1
);
9193 isl_basic_map_free(bmap2
);
9197 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9198 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9200 isl_basic_map
*prod
;
9202 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9203 prod
= isl_basic_map_flatten_range(prod
);
9207 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9208 __isl_take isl_map
*map2
,
9209 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9210 __isl_take isl_space
*right
),
9211 __isl_give isl_basic_map
*(*basic_map_product
)(
9212 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9215 struct isl_map
*result
;
9221 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9222 map2
->dim
, isl_dim_param
), goto error
);
9224 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9225 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9226 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9228 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9229 isl_space_copy(map2
->dim
)),
9230 map1
->n
* map2
->n
, flags
);
9233 for (i
= 0; i
< map1
->n
; ++i
)
9234 for (j
= 0; j
< map2
->n
; ++j
) {
9235 struct isl_basic_map
*part
;
9236 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9237 isl_basic_map_copy(map2
->p
[j
]));
9238 if (isl_basic_map_is_empty(part
))
9239 isl_basic_map_free(part
);
9241 result
= isl_map_add_basic_map(result
, part
);
9254 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9256 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9257 __isl_take isl_map
*map2
)
9259 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9262 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9263 __isl_take isl_map
*map2
)
9265 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9268 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9270 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9271 __isl_take isl_map
*map2
)
9275 prod
= isl_map_product(map1
, map2
);
9276 prod
= isl_map_flatten(prod
);
9280 /* Given two set A and B, construct its Cartesian product A x B.
9282 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9284 return isl_map_range_product(set1
, set2
);
9287 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9288 __isl_take isl_set
*set2
)
9290 return isl_map_flat_range_product(set1
, set2
);
9293 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9295 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9296 __isl_take isl_map
*map2
)
9298 return map_product(map1
, map2
, &isl_space_domain_product
,
9299 &isl_basic_map_domain_product
);
9302 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9304 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9305 __isl_take isl_map
*map2
)
9307 return map_product(map1
, map2
, &isl_space_range_product
,
9308 &isl_basic_map_range_product
);
9311 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9312 __isl_take isl_map
*map2
)
9314 return isl_map_align_params_map_map_and(map1
, map2
,
9315 &map_domain_product_aligned
);
9318 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9319 __isl_take isl_map
*map2
)
9321 return isl_map_align_params_map_map_and(map1
, map2
,
9322 &map_range_product_aligned
);
9325 /* Given a map A -> [B -> C], extract the map A -> B.
9327 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9334 if (!isl_space_range_is_wrapping(map
->dim
))
9335 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9336 "range is not a product", return isl_map_free(map
));
9338 space
= isl_map_get_space(map
);
9339 total
= isl_space_dim(space
, isl_dim_out
);
9340 space
= isl_space_range_factor_domain(space
);
9341 keep
= isl_space_dim(space
, isl_dim_out
);
9342 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9343 map
= isl_map_reset_space(map
, space
);
9348 /* Given a map A -> [B -> C], extract the map A -> C.
9350 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9357 if (!isl_space_range_is_wrapping(map
->dim
))
9358 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9359 "range is not a product", return isl_map_free(map
));
9361 space
= isl_map_get_space(map
);
9362 total
= isl_space_dim(space
, isl_dim_out
);
9363 space
= isl_space_range_factor_range(space
);
9364 keep
= isl_space_dim(space
, isl_dim_out
);
9365 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9366 map
= isl_map_reset_space(map
, space
);
9371 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9373 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9374 __isl_take isl_map
*map2
)
9378 prod
= isl_map_domain_product(map1
, map2
);
9379 prod
= isl_map_flatten_domain(prod
);
9383 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9385 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9386 __isl_take isl_map
*map2
)
9390 prod
= isl_map_range_product(map1
, map2
);
9391 prod
= isl_map_flatten_range(prod
);
9395 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9398 uint32_t hash
= isl_hash_init();
9403 bmap
= isl_basic_map_copy(bmap
);
9404 bmap
= isl_basic_map_normalize(bmap
);
9407 total
= isl_basic_map_total_dim(bmap
);
9408 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9409 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9411 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9412 isl_hash_hash(hash
, c_hash
);
9414 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9415 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9417 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9418 isl_hash_hash(hash
, c_hash
);
9420 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9421 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9423 if (isl_int_is_zero(bmap
->div
[i
][0]))
9425 isl_hash_byte(hash
, i
& 0xFF);
9426 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9427 isl_hash_hash(hash
, c_hash
);
9429 isl_basic_map_free(bmap
);
9433 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9435 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9438 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9445 map
= isl_map_copy(map
);
9446 map
= isl_map_normalize(map
);
9450 hash
= isl_hash_init();
9451 for (i
= 0; i
< map
->n
; ++i
) {
9453 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9454 isl_hash_hash(hash
, bmap_hash
);
9462 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9464 return isl_map_get_hash((isl_map
*)set
);
9467 /* Check if the value for dimension dim is completely determined
9468 * by the values of the other parameters and variables.
9469 * That is, check if dimension dim is involved in an equality.
9471 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9478 nparam
= isl_basic_set_n_param(bset
);
9479 for (i
= 0; i
< bset
->n_eq
; ++i
)
9480 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9485 /* Check if the value for dimension dim is completely determined
9486 * by the values of the other parameters and variables.
9487 * That is, check if dimension dim is involved in an equality
9488 * for each of the subsets.
9490 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9496 for (i
= 0; i
< set
->n
; ++i
) {
9498 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9505 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9507 return set
? set
->n
: 0;
9510 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9511 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9518 for (i
= 0; i
< map
->n
; ++i
)
9519 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9525 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9526 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9533 for (i
= 0; i
< set
->n
; ++i
)
9534 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9540 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9547 bset
= isl_basic_set_cow(bset
);
9551 dim
= isl_basic_set_get_space(bset
);
9552 dim
= isl_space_lift(dim
, bset
->n_div
);
9555 isl_space_free(bset
->dim
);
9557 bset
->extra
-= bset
->n_div
;
9560 bset
= isl_basic_set_finalize(bset
);
9564 isl_basic_set_free(bset
);
9568 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9574 set
= isl_set_align_divs(set
);
9579 set
= isl_set_cow(set
);
9583 n_div
= set
->p
[0]->n_div
;
9584 dim
= isl_set_get_space(set
);
9585 dim
= isl_space_lift(dim
, n_div
);
9588 isl_space_free(set
->dim
);
9591 for (i
= 0; i
< set
->n
; ++i
) {
9592 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9603 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9606 struct isl_basic_map
*bmap
;
9613 set
= isl_set_align_divs(set
);
9618 dim
= isl_set_get_space(set
);
9619 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9621 return isl_map_identity(isl_space_map_from_set(dim
));
9624 n_div
= set
->p
[0]->n_div
;
9625 dim
= isl_space_map_from_set(dim
);
9626 n_param
= isl_space_dim(dim
, isl_dim_param
);
9627 n_set
= isl_space_dim(dim
, isl_dim_in
);
9628 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9629 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9630 for (i
= 0; i
< n_set
; ++i
)
9631 bmap
= var_equal(bmap
, i
);
9633 total
= n_param
+ n_set
+ n_set
+ n_div
;
9634 for (i
= 0; i
< n_div
; ++i
) {
9635 k
= isl_basic_map_alloc_inequality(bmap
);
9638 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9639 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9640 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9641 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9642 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9643 set
->p
[0]->div
[i
][0]);
9645 l
= isl_basic_map_alloc_inequality(bmap
);
9648 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9649 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9650 set
->p
[0]->div
[i
][0]);
9651 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9655 bmap
= isl_basic_map_simplify(bmap
);
9656 bmap
= isl_basic_map_finalize(bmap
);
9657 return isl_map_from_basic_map(bmap
);
9660 isl_basic_map_free(bmap
);
9664 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9672 dim
= isl_basic_set_total_dim(bset
);
9673 size
+= bset
->n_eq
* (1 + dim
);
9674 size
+= bset
->n_ineq
* (1 + dim
);
9675 size
+= bset
->n_div
* (2 + dim
);
9680 int isl_set_size(__isl_keep isl_set
*set
)
9688 for (i
= 0; i
< set
->n
; ++i
)
9689 size
+= isl_basic_set_size(set
->p
[i
]);
9694 /* Check if there is any lower bound (if lower == 0) and/or upper
9695 * bound (if upper == 0) on the specified dim.
9697 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9698 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9705 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9707 pos
+= isl_basic_map_offset(bmap
, type
);
9709 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9710 if (isl_int_is_zero(bmap
->div
[i
][0]))
9712 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9716 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9717 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9720 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9721 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9728 return lower
&& upper
;
9731 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9732 enum isl_dim_type type
, unsigned pos
)
9734 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9737 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9738 enum isl_dim_type type
, unsigned pos
)
9740 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9743 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9744 enum isl_dim_type type
, unsigned pos
)
9746 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9749 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9750 enum isl_dim_type type
, unsigned pos
)
9757 for (i
= 0; i
< map
->n
; ++i
) {
9759 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9760 if (bounded
< 0 || !bounded
)
9767 /* Return 1 if the specified dim is involved in both an upper bound
9768 * and a lower bound.
9770 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9771 enum isl_dim_type type
, unsigned pos
)
9773 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9776 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9778 static int has_any_bound(__isl_keep isl_map
*map
,
9779 enum isl_dim_type type
, unsigned pos
,
9780 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9781 enum isl_dim_type type
, unsigned pos
))
9788 for (i
= 0; i
< map
->n
; ++i
) {
9790 bounded
= fn(map
->p
[i
], type
, pos
);
9791 if (bounded
< 0 || bounded
)
9798 /* Return 1 if the specified dim is involved in any lower bound.
9800 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9801 enum isl_dim_type type
, unsigned pos
)
9803 return has_any_bound(set
, type
, pos
,
9804 &isl_basic_map_dim_has_lower_bound
);
9807 /* Return 1 if the specified dim is involved in any upper bound.
9809 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9810 enum isl_dim_type type
, unsigned pos
)
9812 return has_any_bound(set
, type
, pos
,
9813 &isl_basic_map_dim_has_upper_bound
);
9816 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9818 static int has_bound(__isl_keep isl_map
*map
,
9819 enum isl_dim_type type
, unsigned pos
,
9820 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9821 enum isl_dim_type type
, unsigned pos
))
9828 for (i
= 0; i
< map
->n
; ++i
) {
9830 bounded
= fn(map
->p
[i
], type
, pos
);
9831 if (bounded
< 0 || !bounded
)
9838 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9840 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9841 enum isl_dim_type type
, unsigned pos
)
9843 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9846 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9848 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9849 enum isl_dim_type type
, unsigned pos
)
9851 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9854 /* For each of the "n" variables starting at "first", determine
9855 * the sign of the variable and put the results in the first "n"
9856 * elements of the array "signs".
9858 * 1 means that the variable is non-negative
9859 * -1 means that the variable is non-positive
9860 * 0 means the variable attains both positive and negative values.
9862 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9863 unsigned first
, unsigned n
, int *signs
)
9865 isl_vec
*bound
= NULL
;
9866 struct isl_tab
*tab
= NULL
;
9867 struct isl_tab_undo
*snap
;
9870 if (!bset
|| !signs
)
9873 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9874 tab
= isl_tab_from_basic_set(bset
, 0);
9878 isl_seq_clr(bound
->el
, bound
->size
);
9879 isl_int_set_si(bound
->el
[0], -1);
9881 snap
= isl_tab_snap(tab
);
9882 for (i
= 0; i
< n
; ++i
) {
9885 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9886 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9889 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9890 if (isl_tab_rollback(tab
, snap
) < 0)
9898 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9899 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9902 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9903 if (isl_tab_rollback(tab
, snap
) < 0)
9906 signs
[i
] = empty
? -1 : 0;
9910 isl_vec_free(bound
);
9914 isl_vec_free(bound
);
9918 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
9919 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
9921 if (!bset
|| !signs
)
9923 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
9926 first
+= pos(bset
->dim
, type
) - 1;
9927 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
9930 /* Check if the given basic map is obviously single-valued.
9931 * In particular, for each output dimension, check that there is
9932 * an equality that defines the output dimension in terms of
9933 * earlier dimensions.
9935 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9945 total
= 1 + isl_basic_map_total_dim(bmap
);
9946 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
9947 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
9949 for (i
= 0; i
< n_out
; ++i
) {
9950 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
9951 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
9953 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
9954 total
- (o_out
+ i
+ 1)) == -1)
9957 if (j
>= bmap
->n_eq
)
9964 /* Check if the given basic map is single-valued.
9969 * and check if the result is a subset of the identity mapping.
9971 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9974 isl_basic_map
*test
;
9978 sv
= isl_basic_map_plain_is_single_valued(bmap
);
9982 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
9983 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
9985 space
= isl_basic_map_get_space(bmap
);
9986 space
= isl_space_map_from_set(isl_space_range(space
));
9987 id
= isl_basic_map_identity(space
);
9989 sv
= isl_basic_map_is_subset(test
, id
);
9991 isl_basic_map_free(test
);
9992 isl_basic_map_free(id
);
9997 /* Check if the given map is obviously single-valued.
9999 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10008 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10011 /* Check if the given map is single-valued.
10012 * We simply compute
10016 * and check if the result is a subset of the identity mapping.
10018 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10025 sv
= isl_map_plain_is_single_valued(map
);
10029 test
= isl_map_reverse(isl_map_copy(map
));
10030 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10032 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10033 id
= isl_map_identity(dim
);
10035 sv
= isl_map_is_subset(test
, id
);
10037 isl_map_free(test
);
10043 int isl_map_is_injective(__isl_keep isl_map
*map
)
10047 map
= isl_map_copy(map
);
10048 map
= isl_map_reverse(map
);
10049 in
= isl_map_is_single_valued(map
);
10055 /* Check if the given map is obviously injective.
10057 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10061 map
= isl_map_copy(map
);
10062 map
= isl_map_reverse(map
);
10063 in
= isl_map_plain_is_single_valued(map
);
10069 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10073 sv
= isl_map_is_single_valued(map
);
10077 return isl_map_is_injective(map
);
10080 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10082 return isl_map_is_single_valued((isl_map
*)set
);
10085 int isl_map_is_translation(__isl_keep isl_map
*map
)
10090 delta
= isl_map_deltas(isl_map_copy(map
));
10091 ok
= isl_set_is_singleton(delta
);
10092 isl_set_free(delta
);
10097 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10099 if (isl_seq_first_non_zero(p
, pos
) != -1)
10101 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10106 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10115 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10118 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10119 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10120 for (j
= 0; j
< nvar
; ++j
) {
10121 int lower
= 0, upper
= 0;
10122 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10123 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10125 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10129 if (i
< bset
->n_eq
)
10131 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10132 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10134 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10136 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10141 if (!lower
|| !upper
)
10148 int isl_set_is_box(__isl_keep isl_set
*set
)
10155 return isl_basic_set_is_box(set
->p
[0]);
10158 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10163 return isl_space_is_wrapping(bset
->dim
);
10166 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10171 return isl_space_is_wrapping(set
->dim
);
10174 /* Is the range of "map" a wrapped relation?
10176 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10181 return isl_space_range_is_wrapping(map
->dim
);
10184 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10186 bmap
= isl_basic_map_cow(bmap
);
10190 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10194 bmap
= isl_basic_map_finalize(bmap
);
10196 return (isl_basic_set
*)bmap
;
10198 isl_basic_map_free(bmap
);
10202 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10206 map
= isl_map_cow(map
);
10210 for (i
= 0; i
< map
->n
; ++i
) {
10211 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10215 map
->dim
= isl_space_wrap(map
->dim
);
10219 return (isl_set
*)map
;
10225 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10227 bset
= isl_basic_set_cow(bset
);
10231 bset
->dim
= isl_space_unwrap(bset
->dim
);
10235 bset
= isl_basic_set_finalize(bset
);
10237 return (isl_basic_map
*)bset
;
10239 isl_basic_set_free(bset
);
10243 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10250 if (!isl_set_is_wrapping(set
))
10251 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10254 set
= isl_set_cow(set
);
10258 for (i
= 0; i
< set
->n
; ++i
) {
10259 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10264 set
->dim
= isl_space_unwrap(set
->dim
);
10268 return (isl_map
*)set
;
10274 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10275 enum isl_dim_type type
)
10280 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10283 bmap
= isl_basic_map_cow(bmap
);
10287 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10291 bmap
= isl_basic_map_finalize(bmap
);
10295 isl_basic_map_free(bmap
);
10299 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10300 enum isl_dim_type type
)
10307 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10310 map
= isl_map_cow(map
);
10314 for (i
= 0; i
< map
->n
; ++i
) {
10315 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10319 map
->dim
= isl_space_reset(map
->dim
, type
);
10329 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10334 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10337 bmap
= isl_basic_map_cow(bmap
);
10341 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10345 bmap
= isl_basic_map_finalize(bmap
);
10349 isl_basic_map_free(bmap
);
10353 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10355 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10358 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10359 __isl_take isl_basic_map
*bmap
)
10364 if (!bmap
->dim
->nested
[0])
10367 bmap
= isl_basic_map_cow(bmap
);
10371 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10375 bmap
= isl_basic_map_finalize(bmap
);
10379 isl_basic_map_free(bmap
);
10383 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10384 __isl_take isl_basic_map
*bmap
)
10389 if (!bmap
->dim
->nested
[1])
10392 bmap
= isl_basic_map_cow(bmap
);
10396 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10400 bmap
= isl_basic_map_finalize(bmap
);
10404 isl_basic_map_free(bmap
);
10408 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10415 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10418 map
= isl_map_cow(map
);
10422 for (i
= 0; i
< map
->n
; ++i
) {
10423 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10427 map
->dim
= isl_space_flatten(map
->dim
);
10437 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10439 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10442 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10444 isl_space
*dim
, *flat_dim
;
10447 dim
= isl_set_get_space(set
);
10448 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10449 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10450 map
= isl_map_intersect_domain(map
, set
);
10455 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10462 if (!map
->dim
->nested
[0])
10465 map
= isl_map_cow(map
);
10469 for (i
= 0; i
< map
->n
; ++i
) {
10470 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10474 map
->dim
= isl_space_flatten_domain(map
->dim
);
10484 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10491 if (!map
->dim
->nested
[1])
10494 map
= isl_map_cow(map
);
10498 for (i
= 0; i
< map
->n
; ++i
) {
10499 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10503 map
->dim
= isl_space_flatten_range(map
->dim
);
10513 /* Reorder the dimensions of "bmap" according to the given dim_map
10514 * and set the dimension specification to "dim".
10516 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10517 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10519 isl_basic_map
*res
;
10522 bmap
= isl_basic_map_cow(bmap
);
10523 if (!bmap
|| !dim
|| !dim_map
)
10526 flags
= bmap
->flags
;
10527 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10528 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10529 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10530 res
= isl_basic_map_alloc_space(dim
,
10531 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10532 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10534 res
->flags
= flags
;
10535 res
= isl_basic_map_finalize(res
);
10539 isl_basic_map_free(bmap
);
10540 isl_space_free(dim
);
10544 /* Reorder the dimensions of "map" according to given reordering.
10546 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10547 __isl_take isl_reordering
*r
)
10550 struct isl_dim_map
*dim_map
;
10552 map
= isl_map_cow(map
);
10553 dim_map
= isl_dim_map_from_reordering(r
);
10554 if (!map
|| !r
|| !dim_map
)
10557 for (i
= 0; i
< map
->n
; ++i
) {
10558 struct isl_dim_map
*dim_map_i
;
10560 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10562 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10563 isl_space_copy(r
->dim
), dim_map_i
);
10569 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10571 isl_reordering_free(r
);
10577 isl_reordering_free(r
);
10581 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10582 __isl_take isl_reordering
*r
)
10584 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10587 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10588 __isl_take isl_space
*model
)
10592 if (!map
|| !model
)
10595 ctx
= isl_space_get_ctx(model
);
10596 if (!isl_space_has_named_params(model
))
10597 isl_die(ctx
, isl_error_invalid
,
10598 "model has unnamed parameters", goto error
);
10599 if (!isl_space_has_named_params(map
->dim
))
10600 isl_die(ctx
, isl_error_invalid
,
10601 "relation has unnamed parameters", goto error
);
10602 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10603 isl_reordering
*exp
;
10605 model
= isl_space_drop_dims(model
, isl_dim_in
,
10606 0, isl_space_dim(model
, isl_dim_in
));
10607 model
= isl_space_drop_dims(model
, isl_dim_out
,
10608 0, isl_space_dim(model
, isl_dim_out
));
10609 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10610 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10611 map
= isl_map_realign(map
, exp
);
10614 isl_space_free(model
);
10617 isl_space_free(model
);
10622 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10623 __isl_take isl_space
*model
)
10625 return isl_map_align_params(set
, model
);
10628 /* Align the parameters of "bmap" to those of "model", introducing
10629 * additional parameters if needed.
10631 __isl_give isl_basic_map
*isl_basic_map_align_params(
10632 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10636 if (!bmap
|| !model
)
10639 ctx
= isl_space_get_ctx(model
);
10640 if (!isl_space_has_named_params(model
))
10641 isl_die(ctx
, isl_error_invalid
,
10642 "model has unnamed parameters", goto error
);
10643 if (!isl_space_has_named_params(bmap
->dim
))
10644 isl_die(ctx
, isl_error_invalid
,
10645 "relation has unnamed parameters", goto error
);
10646 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10647 isl_reordering
*exp
;
10648 struct isl_dim_map
*dim_map
;
10650 model
= isl_space_drop_dims(model
, isl_dim_in
,
10651 0, isl_space_dim(model
, isl_dim_in
));
10652 model
= isl_space_drop_dims(model
, isl_dim_out
,
10653 0, isl_space_dim(model
, isl_dim_out
));
10654 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10655 exp
= isl_reordering_extend_space(exp
,
10656 isl_basic_map_get_space(bmap
));
10657 dim_map
= isl_dim_map_from_reordering(exp
);
10658 bmap
= isl_basic_map_realign(bmap
,
10659 exp
? isl_space_copy(exp
->dim
) : NULL
,
10660 isl_dim_map_extend(dim_map
, bmap
));
10661 isl_reordering_free(exp
);
10665 isl_space_free(model
);
10668 isl_space_free(model
);
10669 isl_basic_map_free(bmap
);
10673 /* Align the parameters of "bset" to those of "model", introducing
10674 * additional parameters if needed.
10676 __isl_give isl_basic_set
*isl_basic_set_align_params(
10677 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10679 return isl_basic_map_align_params(bset
, model
);
10682 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10683 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10684 enum isl_dim_type c2
, enum isl_dim_type c3
,
10685 enum isl_dim_type c4
, enum isl_dim_type c5
)
10687 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10688 struct isl_mat
*mat
;
10694 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10695 isl_basic_map_total_dim(bmap
) + 1);
10698 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10699 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10700 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10701 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10702 isl_int_set(mat
->row
[i
][pos
],
10703 bmap
->eq
[i
][off
+ k
]);
10711 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10712 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10713 enum isl_dim_type c2
, enum isl_dim_type c3
,
10714 enum isl_dim_type c4
, enum isl_dim_type c5
)
10716 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10717 struct isl_mat
*mat
;
10723 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10724 isl_basic_map_total_dim(bmap
) + 1);
10727 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10728 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10729 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10730 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10731 isl_int_set(mat
->row
[i
][pos
],
10732 bmap
->ineq
[i
][off
+ k
]);
10740 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10741 __isl_take isl_space
*dim
,
10742 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10743 enum isl_dim_type c2
, enum isl_dim_type c3
,
10744 enum isl_dim_type c4
, enum isl_dim_type c5
)
10746 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10747 isl_basic_map
*bmap
;
10753 if (!dim
|| !eq
|| !ineq
)
10756 if (eq
->n_col
!= ineq
->n_col
)
10757 isl_die(dim
->ctx
, isl_error_invalid
,
10758 "equalities and inequalities matrices should have "
10759 "same number of columns", goto error
);
10761 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10763 if (eq
->n_col
< total
)
10764 isl_die(dim
->ctx
, isl_error_invalid
,
10765 "number of columns too small", goto error
);
10767 extra
= eq
->n_col
- total
;
10769 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10770 eq
->n_row
, ineq
->n_row
);
10773 for (i
= 0; i
< extra
; ++i
) {
10774 k
= isl_basic_map_alloc_div(bmap
);
10777 isl_int_set_si(bmap
->div
[k
][0], 0);
10779 for (i
= 0; i
< eq
->n_row
; ++i
) {
10780 l
= isl_basic_map_alloc_equality(bmap
);
10783 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10784 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10785 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10786 isl_int_set(bmap
->eq
[l
][off
+ k
],
10792 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10793 l
= isl_basic_map_alloc_inequality(bmap
);
10796 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10797 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10798 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10799 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10800 ineq
->row
[i
][pos
]);
10806 isl_space_free(dim
);
10808 isl_mat_free(ineq
);
10810 bmap
= isl_basic_map_simplify(bmap
);
10811 return isl_basic_map_finalize(bmap
);
10813 isl_space_free(dim
);
10815 isl_mat_free(ineq
);
10819 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10820 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10821 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10823 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10824 c1
, c2
, c3
, c4
, isl_dim_in
);
10827 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10828 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10829 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10831 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10832 c1
, c2
, c3
, c4
, isl_dim_in
);
10835 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10836 __isl_take isl_space
*dim
,
10837 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10838 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10840 return (isl_basic_set
*)
10841 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10842 c1
, c2
, c3
, c4
, isl_dim_in
);
10845 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10850 return isl_space_can_zip(bmap
->dim
);
10853 int isl_map_can_zip(__isl_keep isl_map
*map
)
10858 return isl_space_can_zip(map
->dim
);
10861 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10862 * (A -> C) -> (B -> D).
10864 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10873 if (!isl_basic_map_can_zip(bmap
))
10874 isl_die(bmap
->ctx
, isl_error_invalid
,
10875 "basic map cannot be zipped", goto error
);
10876 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10877 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10878 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10879 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10880 bmap
= isl_basic_map_cow(bmap
);
10881 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10884 bmap
->dim
= isl_space_zip(bmap
->dim
);
10889 isl_basic_map_free(bmap
);
10893 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10894 * (A -> C) -> (B -> D).
10896 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10903 if (!isl_map_can_zip(map
))
10904 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10907 map
= isl_map_cow(map
);
10911 for (i
= 0; i
< map
->n
; ++i
) {
10912 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
10917 map
->dim
= isl_space_zip(map
->dim
);
10927 /* Can we apply isl_basic_map_curry to "bmap"?
10928 * That is, does it have a nested relation in its domain?
10930 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
10935 return isl_space_can_curry(bmap
->dim
);
10938 /* Can we apply isl_map_curry to "map"?
10939 * That is, does it have a nested relation in its domain?
10941 int isl_map_can_curry(__isl_keep isl_map
*map
)
10946 return isl_space_can_curry(map
->dim
);
10949 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10952 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
10958 if (!isl_basic_map_can_curry(bmap
))
10959 isl_die(bmap
->ctx
, isl_error_invalid
,
10960 "basic map cannot be curried", goto error
);
10961 bmap
= isl_basic_map_cow(bmap
);
10964 bmap
->dim
= isl_space_curry(bmap
->dim
);
10969 isl_basic_map_free(bmap
);
10973 /* Given a map (A -> B) -> C, return the corresponding map
10976 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
10983 if (!isl_map_can_curry(map
))
10984 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
10987 map
= isl_map_cow(map
);
10991 for (i
= 0; i
< map
->n
; ++i
) {
10992 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
10997 map
->dim
= isl_space_curry(map
->dim
);
11007 /* Can we apply isl_basic_map_uncurry to "bmap"?
11008 * That is, does it have a nested relation in its domain?
11010 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11015 return isl_space_can_uncurry(bmap
->dim
);
11018 /* Can we apply isl_map_uncurry to "map"?
11019 * That is, does it have a nested relation in its domain?
11021 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11026 return isl_space_can_uncurry(map
->dim
);
11029 /* Given a basic map A -> (B -> C), return the corresponding basic map
11032 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11038 if (!isl_basic_map_can_uncurry(bmap
))
11039 isl_die(bmap
->ctx
, isl_error_invalid
,
11040 "basic map cannot be uncurried",
11041 return isl_basic_map_free(bmap
));
11042 bmap
= isl_basic_map_cow(bmap
);
11045 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11047 return isl_basic_map_free(bmap
);
11051 /* Given a map A -> (B -> C), return the corresponding map
11054 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11061 if (!isl_map_can_uncurry(map
))
11062 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11063 return isl_map_free(map
));
11065 map
= isl_map_cow(map
);
11069 for (i
= 0; i
< map
->n
; ++i
) {
11070 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11072 return isl_map_free(map
);
11075 map
->dim
= isl_space_uncurry(map
->dim
);
11077 return isl_map_free(map
);
11082 /* Construct a basic map mapping the domain of the affine expression
11083 * to a one-dimensional range prescribed by the affine expression.
11085 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11089 isl_local_space
*ls
;
11090 isl_basic_map
*bmap
;
11095 ls
= isl_aff_get_local_space(aff
);
11096 bmap
= isl_basic_map_from_local_space(ls
);
11097 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11098 k
= isl_basic_map_alloc_equality(bmap
);
11102 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11103 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11104 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11105 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11106 aff
->v
->size
- (pos
+ 1));
11109 bmap
= isl_basic_map_finalize(bmap
);
11113 isl_basic_map_free(bmap
);
11117 /* Construct a map mapping the domain of the affine expression
11118 * to a one-dimensional range prescribed by the affine expression.
11120 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11122 isl_basic_map
*bmap
;
11124 bmap
= isl_basic_map_from_aff(aff
);
11125 return isl_map_from_basic_map(bmap
);
11128 /* Construct a basic map mapping the domain the multi-affine expression
11129 * to its range, with each dimension in the range equated to the
11130 * corresponding affine expression.
11132 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11133 __isl_take isl_multi_aff
*maff
)
11137 isl_basic_map
*bmap
;
11142 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11143 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11144 "invalid space", return isl_multi_aff_free(maff
));
11146 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11147 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11149 for (i
= 0; i
< maff
->n
; ++i
) {
11151 isl_basic_map
*bmap_i
;
11153 aff
= isl_aff_copy(maff
->p
[i
]);
11154 bmap_i
= isl_basic_map_from_aff(aff
);
11156 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11159 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11161 isl_multi_aff_free(maff
);
11165 /* Construct a map mapping the domain the multi-affine expression
11166 * to its range, with each dimension in the range equated to the
11167 * corresponding affine expression.
11169 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11171 isl_basic_map
*bmap
;
11173 bmap
= isl_basic_map_from_multi_aff(maff
);
11174 return isl_map_from_basic_map(bmap
);
11177 /* Construct a basic map mapping a domain in the given space to
11178 * to an n-dimensional range, with n the number of elements in the list,
11179 * where each coordinate in the range is prescribed by the
11180 * corresponding affine expression.
11181 * The domains of all affine expressions in the list are assumed to match
11184 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11185 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11189 isl_basic_map
*bmap
;
11194 dim
= isl_space_from_domain(domain_dim
);
11195 bmap
= isl_basic_map_universe(dim
);
11197 for (i
= 0; i
< list
->n
; ++i
) {
11199 isl_basic_map
*bmap_i
;
11201 aff
= isl_aff_copy(list
->p
[i
]);
11202 bmap_i
= isl_basic_map_from_aff(aff
);
11204 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11207 isl_aff_list_free(list
);
11211 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11212 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11214 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11217 /* Construct a basic map where the given dimensions are equal to each other.
11219 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11220 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11222 isl_basic_map
*bmap
= NULL
;
11228 if (pos1
>= isl_space_dim(space
, type1
))
11229 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11230 "index out of bounds", goto error
);
11231 if (pos2
>= isl_space_dim(space
, type2
))
11232 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11233 "index out of bounds", goto error
);
11235 if (type1
== type2
&& pos1
== pos2
)
11236 return isl_basic_map_universe(space
);
11238 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11239 i
= isl_basic_map_alloc_equality(bmap
);
11242 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11243 pos1
+= isl_basic_map_offset(bmap
, type1
);
11244 pos2
+= isl_basic_map_offset(bmap
, type2
);
11245 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11246 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11247 bmap
= isl_basic_map_finalize(bmap
);
11248 isl_space_free(space
);
11251 isl_space_free(space
);
11252 isl_basic_map_free(bmap
);
11256 /* Add a constraint imposing that the given two dimensions are equal.
11258 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11259 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11263 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11265 bmap
= isl_basic_map_intersect(bmap
, eq
);
11270 /* Add a constraint imposing that the given two dimensions are equal.
11272 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11273 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11275 isl_basic_map
*bmap
;
11277 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11279 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11284 /* Add a constraint imposing that the given two dimensions have opposite values.
11286 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11287 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11289 isl_basic_map
*bmap
= NULL
;
11295 if (pos1
>= isl_map_dim(map
, type1
))
11296 isl_die(map
->ctx
, isl_error_invalid
,
11297 "index out of bounds", goto error
);
11298 if (pos2
>= isl_map_dim(map
, type2
))
11299 isl_die(map
->ctx
, isl_error_invalid
,
11300 "index out of bounds", goto error
);
11302 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11303 i
= isl_basic_map_alloc_equality(bmap
);
11306 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11307 pos1
+= isl_basic_map_offset(bmap
, type1
);
11308 pos2
+= isl_basic_map_offset(bmap
, type2
);
11309 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11310 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11311 bmap
= isl_basic_map_finalize(bmap
);
11313 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11317 isl_basic_map_free(bmap
);
11322 /* Add a constraint imposing that the value of the first dimension is
11323 * greater than or equal to that of the second.
11325 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11326 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11329 isl_local_space
*ls
;
11334 if (pos1
>= isl_basic_map_dim(bmap
, type1
))
11335 isl_die(bmap
->ctx
, isl_error_invalid
,
11336 "index out of bounds", return isl_basic_map_free(bmap
));
11337 if (pos2
>= isl_basic_map_dim(bmap
, type2
))
11338 isl_die(bmap
->ctx
, isl_error_invalid
,
11339 "index out of bounds", return isl_basic_map_free(bmap
));
11341 if (type1
== type2
&& pos1
== pos2
)
11344 ls
= isl_local_space_from_space(isl_basic_map_get_space(bmap
));
11345 c
= isl_inequality_alloc(ls
);
11346 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11347 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11348 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11353 /* Construct a basic map where the value of the first dimension is
11354 * greater than that of the second.
11356 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11357 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11359 isl_basic_map
*bmap
= NULL
;
11365 if (pos1
>= isl_space_dim(space
, type1
))
11366 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11367 "index out of bounds", goto error
);
11368 if (pos2
>= isl_space_dim(space
, type2
))
11369 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11370 "index out of bounds", goto error
);
11372 if (type1
== type2
&& pos1
== pos2
)
11373 return isl_basic_map_empty(space
);
11375 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11376 i
= isl_basic_map_alloc_inequality(bmap
);
11379 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11380 pos1
+= isl_basic_map_offset(bmap
, type1
);
11381 pos2
+= isl_basic_map_offset(bmap
, type2
);
11382 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11383 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11384 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11385 bmap
= isl_basic_map_finalize(bmap
);
11389 isl_space_free(space
);
11390 isl_basic_map_free(bmap
);
11394 /* Add a constraint imposing that the value of the first dimension is
11395 * greater than that of the second.
11397 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11398 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11402 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11404 bmap
= isl_basic_map_intersect(bmap
, gt
);
11409 /* Add a constraint imposing that the value of the first dimension is
11410 * greater than that of the second.
11412 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11413 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11415 isl_basic_map
*bmap
;
11417 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11419 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11424 /* Add a constraint imposing that the value of the first dimension is
11425 * smaller than that of the second.
11427 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11428 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11430 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11433 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11437 isl_local_space
*ls
;
11442 if (!isl_basic_map_divs_known(bmap
))
11443 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11444 "some divs are unknown", return NULL
);
11446 ls
= isl_basic_map_get_local_space(bmap
);
11447 div
= isl_local_space_get_div(ls
, pos
);
11448 isl_local_space_free(ls
);
11453 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11456 return isl_basic_map_get_div(bset
, pos
);
11459 /* Plug in "subs" for dimension "type", "pos" of "bset".
11461 * Let i be the dimension to replace and let "subs" be of the form
11465 * Any integer division with a non-zero coefficient for i,
11467 * floor((a i + g)/m)
11471 * floor((a f + d g)/(m d))
11473 * Constraints of the form
11481 * We currently require that "subs" is an integral expression.
11482 * Handling rational expressions may require us to add stride constraints
11483 * as we do in isl_basic_set_preimage_multi_aff.
11485 __isl_give isl_basic_set
*isl_basic_set_substitute(
11486 __isl_take isl_basic_set
*bset
,
11487 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11493 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11496 bset
= isl_basic_set_cow(bset
);
11497 if (!bset
|| !subs
)
11500 ctx
= isl_basic_set_get_ctx(bset
);
11501 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11502 isl_die(ctx
, isl_error_invalid
,
11503 "spaces don't match", goto error
);
11504 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11505 isl_die(ctx
, isl_error_unsupported
,
11506 "cannot handle divs yet", goto error
);
11507 if (!isl_int_is_one(subs
->v
->el
[0]))
11508 isl_die(ctx
, isl_error_invalid
,
11509 "can only substitute integer expressions", goto error
);
11511 pos
+= isl_basic_set_offset(bset
, type
);
11515 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11516 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11518 isl_int_set(v
, bset
->eq
[i
][pos
]);
11519 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11520 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11521 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11524 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11525 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11527 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11528 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11529 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11530 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11533 for (i
= 0; i
< bset
->n_div
; ++i
) {
11534 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11536 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11537 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11538 isl_seq_combine(bset
->div
[i
] + 1,
11539 subs
->v
->el
[0], bset
->div
[i
] + 1,
11540 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11541 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11546 bset
= isl_basic_set_simplify(bset
);
11547 return isl_basic_set_finalize(bset
);
11549 isl_basic_set_free(bset
);
11553 /* Plug in "subs" for dimension "type", "pos" of "set".
11555 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11556 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11560 if (set
&& isl_set_plain_is_empty(set
))
11563 set
= isl_set_cow(set
);
11567 for (i
= set
->n
- 1; i
>= 0; --i
) {
11568 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11569 if (remove_if_empty(set
, i
) < 0)
11579 /* Check if the range of "ma" is compatible with the domain or range
11580 * (depending on "type") of "bmap".
11581 * Return -1 if anything is wrong.
11583 static int check_basic_map_compatible_range_multi_aff(
11584 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11585 __isl_keep isl_multi_aff
*ma
)
11588 isl_space
*ma_space
;
11590 ma_space
= isl_multi_aff_get_space(ma
);
11591 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11592 isl_space_free(ma_space
);
11594 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11595 "spaces don't match", return -1);
11599 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11600 * coefficients before the transformed range of dimensions,
11601 * the "n_after" coefficients after the transformed range of dimensions
11602 * and the coefficients of the other divs in "bmap".
11604 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11605 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11610 isl_local_space
*ls
;
11615 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11619 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11620 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11621 for (i
= 0; i
< n_div
; ++i
) {
11622 int o_bmap
= 0, o_ls
= 0;
11624 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11625 o_bmap
+= 1 + 1 + n_param
;
11626 o_ls
+= 1 + 1 + n_param
;
11627 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11628 o_bmap
+= n_before
;
11629 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11630 ls
->div
->row
[i
] + o_ls
, n_set
);
11633 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11635 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11636 ls
->div
->row
[i
] + o_ls
, n_div
);
11639 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11640 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11644 isl_local_space_free(ls
);
11647 isl_local_space_free(ls
);
11651 /* How many stride constraints does "ma" enforce?
11652 * That is, how many of the affine expressions have a denominator
11653 * different from one?
11655 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11660 for (i
= 0; i
< ma
->n
; ++i
)
11661 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11667 /* For each affine expression in ma of the form
11669 * x_i = (f_i y + h_i)/m_i
11671 * with m_i different from one, add a constraint to "bmap"
11674 * f_i y + h_i = m_i alpha_i
11676 * with alpha_i an additional existentially quantified variable.
11678 static __isl_give isl_basic_map
*add_ma_strides(
11679 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11680 int n_before
, int n_after
)
11689 total
= isl_basic_map_total_dim(bmap
);
11690 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11691 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11692 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11693 for (i
= 0; i
< ma
->n
; ++i
) {
11694 int o_bmap
= 0, o_ma
= 1;
11696 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11698 div
= isl_basic_map_alloc_div(bmap
);
11699 k
= isl_basic_map_alloc_equality(bmap
);
11700 if (div
< 0 || k
< 0)
11702 isl_int_set_si(bmap
->div
[div
][0], 0);
11703 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11704 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11705 o_bmap
+= 1 + n_param
;
11706 o_ma
+= 1 + n_param
;
11707 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11708 o_bmap
+= n_before
;
11709 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11710 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11713 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11715 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11716 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11719 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11720 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11726 isl_basic_map_free(bmap
);
11730 /* Replace the domain or range space (depending on "type) of "space" by "set".
11732 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11733 enum isl_dim_type type
, __isl_take isl_space
*set
)
11735 if (type
== isl_dim_in
) {
11736 space
= isl_space_range(space
);
11737 space
= isl_space_map_from_domain_and_range(set
, space
);
11739 space
= isl_space_domain(space
);
11740 space
= isl_space_map_from_domain_and_range(space
, set
);
11746 /* Compute the preimage of the domain or range (depending on "type")
11747 * of "bmap" under the function represented by "ma".
11748 * In other words, plug in "ma" in the domain or range of "bmap".
11749 * The result is a basic map that lives in the same space as "bmap"
11750 * except that the domain or range has been replaced by
11751 * the domain space of "ma".
11753 * If bmap is represented by
11755 * A(p) + S u + B x + T v + C(divs) >= 0,
11757 * where u and x are input and output dimensions if type == isl_dim_out
11758 * while x and v are input and output dimensions if type == isl_dim_in,
11759 * and ma is represented by
11761 * x = D(p) + F(y) + G(divs')
11763 * then the result is
11765 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11767 * The divs in the input set are similarly adjusted.
11770 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11774 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11775 * B_i G(divs') + c_i(divs))/n_i)
11777 * If bmap is not a rational map and if F(y) involves any denominators
11779 * x_i = (f_i y + h_i)/m_i
11781 * then additional constraints are added to ensure that we only
11782 * map back integer points. That is we enforce
11784 * f_i y + h_i = m_i alpha_i
11786 * with alpha_i an additional existentially quantified variable.
11788 * We first copy over the divs from "ma".
11789 * Then we add the modified constraints and divs from "bmap".
11790 * Finally, we add the stride constraints, if needed.
11792 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
11793 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
11794 __isl_take isl_multi_aff
*ma
)
11798 isl_basic_map
*res
= NULL
;
11799 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
11800 isl_int f
, c1
, c2
, g
;
11801 int rational
, strides
;
11808 ma
= isl_multi_aff_align_divs(ma
);
11811 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
11814 if (type
== isl_dim_in
) {
11816 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
11818 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
11821 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
11822 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
11824 space
= isl_multi_aff_get_domain_space(ma
);
11825 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
11826 rational
= isl_basic_map_is_rational(bmap
);
11827 strides
= rational
? 0 : multi_aff_strides(ma
);
11828 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
11829 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
11831 res
= isl_basic_map_set_rational(res
);
11833 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
11834 if (isl_basic_map_alloc_div(res
) < 0)
11837 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
11840 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11841 k
= isl_basic_map_alloc_equality(res
);
11844 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
11845 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11848 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11849 k
= isl_basic_map_alloc_inequality(res
);
11852 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
11853 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11856 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11857 if (isl_int_is_zero(bmap
->div
[i
][0])) {
11858 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
11861 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
11862 n_before
, n_after
, n_div_ma
, n_div_bmap
,
11867 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
11873 isl_basic_map_free(bmap
);
11874 isl_multi_aff_free(ma
);
11875 res
= isl_basic_set_simplify(res
);
11876 return isl_basic_map_finalize(res
);
11882 isl_basic_map_free(bmap
);
11883 isl_multi_aff_free(ma
);
11884 isl_basic_map_free(res
);
11888 /* Compute the preimage of "bset" under the function represented by "ma".
11889 * In other words, plug in "ma" in "bset". The result is a basic set
11890 * that lives in the domain space of "ma".
11892 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
11893 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
11895 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
11898 /* Compute the preimage of the domain of "bmap" under the function
11899 * represented by "ma".
11900 * In other words, plug in "ma" in the domain of "bmap".
11901 * The result is a basic map that lives in the same space as "bmap"
11902 * except that the domain has been replaced by the domain space of "ma".
11904 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
11905 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
11907 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
11910 /* Compute the preimage of the range of "bmap" under the function
11911 * represented by "ma".
11912 * In other words, plug in "ma" in the range of "bmap".
11913 * The result is a basic map that lives in the same space as "bmap"
11914 * except that the range has been replaced by the domain space of "ma".
11916 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
11917 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
11919 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
11922 /* Check if the range of "ma" is compatible with the domain or range
11923 * (depending on "type") of "map".
11924 * Return -1 if anything is wrong.
11926 static int check_map_compatible_range_multi_aff(
11927 __isl_keep isl_map
*map
, enum isl_dim_type type
,
11928 __isl_keep isl_multi_aff
*ma
)
11931 isl_space
*ma_space
;
11933 ma_space
= isl_multi_aff_get_space(ma
);
11934 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
11935 isl_space_free(ma_space
);
11937 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11938 "spaces don't match", return -1);
11942 /* Compute the preimage of the domain or range (depending on "type")
11943 * of "map" under the function represented by "ma".
11944 * In other words, plug in "ma" in the domain or range of "map".
11945 * The result is a map that lives in the same space as "map"
11946 * except that the domain or range has been replaced by
11947 * the domain space of "ma".
11949 * The parameters are assumed to have been aligned.
11951 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
11952 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11957 map
= isl_map_cow(map
);
11958 ma
= isl_multi_aff_align_divs(ma
);
11961 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
11964 for (i
= 0; i
< map
->n
; ++i
) {
11965 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
11966 isl_multi_aff_copy(ma
));
11971 space
= isl_multi_aff_get_domain_space(ma
);
11972 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
11974 isl_space_free(map
->dim
);
11979 isl_multi_aff_free(ma
);
11981 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
11982 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
11985 isl_multi_aff_free(ma
);
11990 /* Compute the preimage of the domain or range (depending on "type")
11991 * of "map" under the function represented by "ma".
11992 * In other words, plug in "ma" in the domain or range of "map".
11993 * The result is a map that lives in the same space as "map"
11994 * except that the domain or range has been replaced by
11995 * the domain space of "ma".
11997 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
11998 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12003 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12004 return map_preimage_multi_aff(map
, type
, ma
);
12006 if (!isl_space_has_named_params(map
->dim
) ||
12007 !isl_space_has_named_params(ma
->space
))
12008 isl_die(map
->ctx
, isl_error_invalid
,
12009 "unaligned unnamed parameters", goto error
);
12010 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12011 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12013 return map_preimage_multi_aff(map
, type
, ma
);
12015 isl_multi_aff_free(ma
);
12016 return isl_map_free(map
);
12019 /* Compute the preimage of "set" under the function represented by "ma".
12020 * In other words, plug in "ma" "set". The result is a set
12021 * that lives in the domain space of "ma".
12023 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12024 __isl_take isl_multi_aff
*ma
)
12026 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12029 /* Compute the preimage of the domain of "map" under the function
12030 * represented by "ma".
12031 * In other words, plug in "ma" in the domain of "map".
12032 * The result is a map that lives in the same space as "map"
12033 * except that the domain has been replaced by the domain space of "ma".
12035 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12036 __isl_take isl_multi_aff
*ma
)
12038 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12041 /* Compute the preimage of "map" under the function represented by "pma".
12042 * In other words, plug in "pma" in the domain or range of "map".
12043 * The result is a map that lives in the same space as "map",
12044 * except that the space of type "type" has been replaced by
12045 * the domain space of "pma".
12047 * The parameters of "map" and "pma" are assumed to have been aligned.
12049 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12050 __isl_take isl_map
*map
, enum isl_dim_type type
,
12051 __isl_take isl_pw_multi_aff
*pma
)
12060 isl_pw_multi_aff_free(pma
);
12061 res
= isl_map_empty(isl_map_get_space(map
));
12066 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12067 isl_multi_aff_copy(pma
->p
[0].maff
));
12068 if (type
== isl_dim_in
)
12069 res
= isl_map_intersect_domain(res
,
12070 isl_map_copy(pma
->p
[0].set
));
12072 res
= isl_map_intersect_range(res
,
12073 isl_map_copy(pma
->p
[0].set
));
12075 for (i
= 1; i
< pma
->n
; ++i
) {
12078 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12079 isl_multi_aff_copy(pma
->p
[i
].maff
));
12080 if (type
== isl_dim_in
)
12081 res_i
= isl_map_intersect_domain(res_i
,
12082 isl_map_copy(pma
->p
[i
].set
));
12084 res_i
= isl_map_intersect_range(res_i
,
12085 isl_map_copy(pma
->p
[i
].set
));
12086 res
= isl_map_union(res
, res_i
);
12089 isl_pw_multi_aff_free(pma
);
12093 isl_pw_multi_aff_free(pma
);
12098 /* Compute the preimage of "map" under the function represented by "pma".
12099 * In other words, plug in "pma" in the domain or range of "map".
12100 * The result is a map that lives in the same space as "map",
12101 * except that the space of type "type" has been replaced by
12102 * the domain space of "pma".
12104 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12105 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12110 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12111 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12113 if (!isl_space_has_named_params(map
->dim
) ||
12114 !isl_space_has_named_params(pma
->dim
))
12115 isl_die(map
->ctx
, isl_error_invalid
,
12116 "unaligned unnamed parameters", goto error
);
12117 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12118 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12120 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12122 isl_pw_multi_aff_free(pma
);
12123 return isl_map_free(map
);
12126 /* Compute the preimage of "set" under the function represented by "pma".
12127 * In other words, plug in "pma" in "set". The result is a set
12128 * that lives in the domain space of "pma".
12130 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12131 __isl_take isl_pw_multi_aff
*pma
)
12133 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12136 /* Compute the preimage of "map" under the function represented by "pma".
12137 * In other words, plug in "pma" in the domain "map".
12138 * The result is a map that lives in the same space as "map",
12139 * except that domain space has been replaced by the domain space of "pma".
12141 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12142 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12144 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12147 /* Compute the preimage of "map" under the function represented by "mpa".
12148 * In other words, plug in "mpa" in the domain or range of "map".
12149 * The result is a map that lives in the same space as "map",
12150 * except that the space of type "type" has been replaced by
12151 * the domain space of "mpa".
12153 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12154 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12156 isl_pw_multi_aff
*pma
;
12158 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12159 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12162 /* Compute the preimage of "map" under the function represented by "mpa".
12163 * In other words, plug in "mpa" in the domain "map".
12164 * The result is a map that lives in the same space as "map",
12165 * except that domain space has been replaced by the domain space of "mpa".
12167 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12168 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12170 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12173 /* Compute the preimage of "set" by the function represented by "mpa".
12174 * In other words, plug in "mpa" in "set".
12176 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12177 __isl_take isl_multi_pw_aff
*mpa
)
12179 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);