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 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
695 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
698 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
700 return isl_basic_map_is_rational(bset
);
703 /* Does "bmap" contain any rational points?
705 * If "bmap" has an equality for each dimension, equating the dimension
706 * to an integer constant, then it has no rational points, even if it
707 * is marked as rational.
709 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
711 int has_rational
= 1;
716 if (isl_basic_map_plain_is_empty(bmap
))
718 if (!isl_basic_map_is_rational(bmap
))
720 bmap
= isl_basic_map_copy(bmap
);
721 bmap
= isl_basic_map_implicit_equalities(bmap
);
724 total
= isl_basic_map_total_dim(bmap
);
725 if (bmap
->n_eq
== total
) {
727 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
728 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
731 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
732 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
734 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
742 isl_basic_map_free(bmap
);
747 /* Does "map" contain any rational points?
749 int isl_map_has_rational(__isl_keep isl_map
*map
)
756 for (i
= 0; i
< map
->n
; ++i
) {
757 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
758 if (has_rational
< 0)
766 /* Does "set" contain any rational points?
768 int isl_set_has_rational(__isl_keep isl_set
*set
)
770 return isl_map_has_rational(set
);
773 /* Is this basic set a parameter domain?
775 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
779 return isl_space_is_params(bset
->dim
);
782 /* Is this set a parameter domain?
784 int isl_set_is_params(__isl_keep isl_set
*set
)
788 return isl_space_is_params(set
->dim
);
791 /* Is this map actually a parameter domain?
792 * Users should never call this function. Outside of isl,
793 * a map can never be a parameter domain.
795 int isl_map_is_params(__isl_keep isl_map
*map
)
799 return isl_space_is_params(map
->dim
);
802 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
803 struct isl_basic_map
*bmap
, unsigned extra
,
804 unsigned n_eq
, unsigned n_ineq
)
807 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
812 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
813 if (isl_blk_is_error(bmap
->block
))
816 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
817 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
821 bmap
->block2
= isl_blk_empty();
824 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
825 if (isl_blk_is_error(bmap
->block2
))
828 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
833 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
834 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
836 for (i
= 0; i
< extra
; ++i
)
837 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
841 bmap
->c_size
= n_eq
+ n_ineq
;
842 bmap
->eq
= bmap
->ineq
+ n_ineq
;
851 isl_basic_map_free(bmap
);
855 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
856 unsigned nparam
, unsigned dim
, unsigned extra
,
857 unsigned n_eq
, unsigned n_ineq
)
859 struct isl_basic_map
*bmap
;
862 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
866 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
867 return (struct isl_basic_set
*)bmap
;
870 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
871 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
873 struct isl_basic_map
*bmap
;
876 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
877 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
878 return (struct isl_basic_set
*)bmap
;
884 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
885 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
887 struct isl_basic_map
*bmap
;
891 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
896 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
902 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
903 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
904 unsigned n_eq
, unsigned n_ineq
)
906 struct isl_basic_map
*bmap
;
909 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
913 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
917 static void dup_constraints(
918 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
921 unsigned total
= isl_basic_map_total_dim(src
);
923 for (i
= 0; i
< src
->n_eq
; ++i
) {
924 int j
= isl_basic_map_alloc_equality(dst
);
925 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
928 for (i
= 0; i
< src
->n_ineq
; ++i
) {
929 int j
= isl_basic_map_alloc_inequality(dst
);
930 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
933 for (i
= 0; i
< src
->n_div
; ++i
) {
934 int j
= isl_basic_map_alloc_div(dst
);
935 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
937 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
940 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
942 struct isl_basic_map
*dup
;
946 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
947 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
950 dup_constraints(dup
, bmap
);
951 dup
->flags
= bmap
->flags
;
952 dup
->sample
= isl_vec_copy(bmap
->sample
);
956 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
958 struct isl_basic_map
*dup
;
960 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
961 return (struct isl_basic_set
*)dup
;
964 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
969 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
973 return isl_basic_set_dup(bset
);
976 struct isl_set
*isl_set_copy(struct isl_set
*set
)
985 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
990 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
994 bmap
= isl_basic_map_dup(bmap
);
996 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1000 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1009 void *isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1014 if (--bmap
->ref
> 0)
1017 isl_ctx_deref(bmap
->ctx
);
1019 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1021 isl_blk_free(bmap
->ctx
, bmap
->block
);
1022 isl_vec_free(bmap
->sample
);
1023 isl_space_free(bmap
->dim
);
1029 void *isl_basic_set_free(struct isl_basic_set
*bset
)
1031 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1034 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1036 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1039 __isl_give isl_map
*isl_map_align_params_map_map_and(
1040 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1041 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1042 __isl_take isl_map
*map2
))
1046 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1047 return fn(map1
, map2
);
1048 if (!isl_space_has_named_params(map1
->dim
) ||
1049 !isl_space_has_named_params(map2
->dim
))
1050 isl_die(map1
->ctx
, isl_error_invalid
,
1051 "unaligned unnamed parameters", goto error
);
1052 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1053 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1054 return fn(map1
, map2
);
1061 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1062 __isl_keep isl_map
*map2
,
1063 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1069 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1070 return fn(map1
, map2
);
1071 if (!isl_space_has_named_params(map1
->dim
) ||
1072 !isl_space_has_named_params(map2
->dim
))
1073 isl_die(map1
->ctx
, isl_error_invalid
,
1074 "unaligned unnamed parameters", return -1);
1075 map1
= isl_map_copy(map1
);
1076 map2
= isl_map_copy(map2
);
1077 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1078 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1085 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1087 struct isl_ctx
*ctx
;
1091 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1092 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1094 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1095 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1096 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1097 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1098 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1099 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1101 int j
= isl_basic_map_alloc_inequality(bmap
);
1105 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1106 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1113 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1114 bmap
->extra
- bmap
->n_div
);
1115 return bmap
->n_eq
++;
1118 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1120 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1123 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1127 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1132 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1134 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1137 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1142 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1144 if (pos
!= bmap
->n_eq
- 1) {
1146 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1147 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1153 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1155 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1158 /* Turn inequality "pos" of "bmap" into an equality.
1160 * In particular, we move the inequality in front of the equalities
1161 * and move the last inequality in the position of the moved inequality.
1162 * Note that isl_tab_make_equalities_explicit depends on this particular
1163 * change in the ordering of the constraints.
1165 void isl_basic_map_inequality_to_equality(
1166 struct isl_basic_map
*bmap
, unsigned pos
)
1170 t
= bmap
->ineq
[pos
];
1171 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1172 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1177 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1178 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1179 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1180 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1183 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1185 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1188 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1190 struct isl_ctx
*ctx
;
1194 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1195 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1196 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1197 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1198 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1199 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1200 1 + isl_basic_map_total_dim(bmap
),
1201 bmap
->extra
- bmap
->n_div
);
1202 return bmap
->n_ineq
++;
1205 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1207 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1210 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1214 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1219 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1221 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1224 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1229 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1231 if (pos
!= bmap
->n_ineq
- 1) {
1232 t
= bmap
->ineq
[pos
];
1233 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1234 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1235 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1241 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1243 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1246 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1251 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1254 k
= isl_basic_map_alloc_equality(bmap
);
1257 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1260 isl_basic_map_free(bmap
);
1264 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1267 return (isl_basic_set
*)
1268 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1271 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1276 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1279 k
= isl_basic_map_alloc_inequality(bmap
);
1282 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1285 isl_basic_map_free(bmap
);
1289 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1292 return (isl_basic_set
*)
1293 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1296 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1300 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1301 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1302 1 + 1 + isl_basic_map_total_dim(bmap
),
1303 bmap
->extra
- bmap
->n_div
);
1304 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1305 return bmap
->n_div
++;
1308 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1310 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1313 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1317 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1322 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1324 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1327 /* Copy constraint from src to dst, putting the vars of src at offset
1328 * dim_off in dst and the divs of src at offset div_off in dst.
1329 * If both sets are actually map, then dim_off applies to the input
1332 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1333 struct isl_basic_map
*src_map
, isl_int
*src
,
1334 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1336 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1337 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1338 unsigned src_in
= isl_basic_map_n_in(src_map
);
1339 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1340 unsigned src_out
= isl_basic_map_n_out(src_map
);
1341 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1342 isl_int_set(dst
[0], src
[0]);
1343 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1344 if (dst_nparam
> src_nparam
)
1345 isl_seq_clr(dst
+1+src_nparam
,
1346 dst_nparam
- src_nparam
);
1347 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1348 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1350 isl_min(dst_in
-in_off
, src_in
));
1351 if (dst_in
-in_off
> src_in
)
1352 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1353 dst_in
- in_off
- src_in
);
1354 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1355 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1356 src
+1+src_nparam
+src_in
,
1357 isl_min(dst_out
-out_off
, src_out
));
1358 if (dst_out
-out_off
> src_out
)
1359 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1360 dst_out
- out_off
- src_out
);
1361 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1362 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1363 src
+1+src_nparam
+src_in
+src_out
,
1364 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1365 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1366 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1367 div_off
+src_map
->n_div
,
1368 dst_map
->n_div
- div_off
- src_map
->n_div
);
1371 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1372 struct isl_basic_map
*src_map
, isl_int
*src
,
1373 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1375 isl_int_set(dst
[0], src
[0]);
1376 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1379 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1380 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1385 if (!bmap1
|| !bmap2
)
1388 div_off
= bmap1
->n_div
;
1390 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1391 int i1
= isl_basic_map_alloc_equality(bmap1
);
1394 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1395 i_pos
, o_pos
, div_off
);
1398 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1399 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1402 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1403 i_pos
, o_pos
, div_off
);
1406 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1407 int i1
= isl_basic_map_alloc_div(bmap1
);
1410 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1411 i_pos
, o_pos
, div_off
);
1414 isl_basic_map_free(bmap2
);
1419 isl_basic_map_free(bmap1
);
1420 isl_basic_map_free(bmap2
);
1424 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1425 struct isl_basic_set
*bset2
, unsigned pos
)
1427 return (struct isl_basic_set
*)
1428 add_constraints((struct isl_basic_map
*)bset1
,
1429 (struct isl_basic_map
*)bset2
, 0, pos
);
1432 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1433 __isl_take isl_space
*dim
, unsigned extra
,
1434 unsigned n_eq
, unsigned n_ineq
)
1436 struct isl_basic_map
*ext
;
1446 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1447 base
->extra
>= base
->n_div
+ extra
;
1449 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1450 room_for_ineq(base
, n_ineq
)) {
1451 isl_space_free(dim
);
1455 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1456 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1457 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1458 extra
+= base
->extra
;
1460 n_ineq
+= base
->n_ineq
;
1462 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1468 ext
->sample
= isl_vec_copy(base
->sample
);
1469 flags
= base
->flags
;
1470 ext
= add_constraints(ext
, base
, 0, 0);
1473 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1479 isl_space_free(dim
);
1480 isl_basic_map_free(base
);
1484 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1485 __isl_take isl_space
*dim
, unsigned extra
,
1486 unsigned n_eq
, unsigned n_ineq
)
1488 return (struct isl_basic_set
*)
1489 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1490 extra
, n_eq
, n_ineq
);
1493 struct isl_basic_map
*isl_basic_map_extend_constraints(
1494 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1498 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1502 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1503 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1504 unsigned n_eq
, unsigned n_ineq
)
1506 struct isl_basic_map
*bmap
;
1511 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1515 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1518 isl_basic_map_free(base
);
1522 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1523 unsigned nparam
, unsigned dim
, unsigned extra
,
1524 unsigned n_eq
, unsigned n_ineq
)
1526 return (struct isl_basic_set
*)
1527 isl_basic_map_extend((struct isl_basic_map
*)base
,
1528 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1531 struct isl_basic_set
*isl_basic_set_extend_constraints(
1532 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1534 return (struct isl_basic_set
*)
1535 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1539 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1541 return (struct isl_basic_set
*)
1542 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1545 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1550 if (bmap
->ref
> 1) {
1552 bmap
= isl_basic_map_dup(bmap
);
1555 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1559 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1567 return isl_set_dup(set
);
1570 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1578 return isl_map_dup(map
);
1581 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1582 unsigned a_len
, unsigned b_len
)
1584 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1585 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1586 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1589 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1590 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1598 isl_assert(bmap
->ctx
,
1599 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1601 if (n1
== 0 || n2
== 0)
1604 bmap
= isl_basic_map_cow(bmap
);
1608 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1609 if (isl_blk_is_error(blk
))
1612 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1614 bmap
->eq
[i
] + pos
, n1
, n2
);
1616 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1618 bmap
->ineq
[i
] + pos
, n1
, n2
);
1620 for (i
= 0; i
< bmap
->n_div
; ++i
)
1622 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1624 isl_blk_free(bmap
->ctx
, blk
);
1626 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1627 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1628 return isl_basic_map_finalize(bmap
);
1630 isl_basic_map_free(bmap
);
1634 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1635 __isl_take isl_basic_set
*bset
, unsigned n
)
1643 nparam
= isl_basic_set_n_param(bset
);
1644 dim
= isl_basic_set_n_dim(bset
);
1645 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1647 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1649 isl_basic_set_free(bset
);
1653 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1659 total
= isl_basic_map_total_dim(bmap
);
1660 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1661 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1663 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1665 i
= isl_basic_map_alloc_equality(bmap
);
1669 isl_int_set_si(bmap
->eq
[i
][0], 1);
1670 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1671 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1672 isl_vec_free(bmap
->sample
);
1673 bmap
->sample
= NULL
;
1674 return isl_basic_map_finalize(bmap
);
1676 isl_basic_map_free(bmap
);
1680 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1682 return (struct isl_basic_set
*)
1683 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1686 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1689 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1691 isl_int
*t
= bmap
->div
[a
];
1692 bmap
->div
[a
] = bmap
->div
[b
];
1696 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1697 * div definitions accordingly.
1699 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1702 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1704 swap_div(bmap
, a
, b
);
1706 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1707 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1709 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1710 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1712 for (i
= 0; i
< bmap
->n_div
; ++i
)
1713 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1714 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1717 /* Eliminate the specified n dimensions starting at first from the
1718 * constraints, without removing the dimensions from the space.
1719 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1721 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1722 enum isl_dim_type type
, unsigned first
, unsigned n
)
1731 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1732 isl_die(map
->ctx
, isl_error_invalid
,
1733 "index out of bounds", goto error
);
1735 map
= isl_map_cow(map
);
1739 for (i
= 0; i
< map
->n
; ++i
) {
1740 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1750 /* Eliminate the specified n dimensions starting at first from the
1751 * constraints, without removing the dimensions from the space.
1752 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1754 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1755 enum isl_dim_type type
, unsigned first
, unsigned n
)
1757 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1760 /* Eliminate the specified n dimensions starting at first from the
1761 * constraints, without removing the dimensions from the space.
1762 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1764 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1765 unsigned first
, unsigned n
)
1767 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1770 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1771 __isl_take isl_basic_map
*bmap
)
1775 bmap
= isl_basic_map_eliminate_vars(bmap
,
1776 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1780 return isl_basic_map_finalize(bmap
);
1783 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1784 __isl_take isl_basic_set
*bset
)
1786 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1787 (struct isl_basic_map
*)bset
);
1790 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1799 map
= isl_map_cow(map
);
1803 for (i
= 0; i
< map
->n
; ++i
) {
1804 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1814 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1816 return isl_map_remove_divs(set
);
1819 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1820 enum isl_dim_type type
, unsigned first
, unsigned n
)
1824 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1826 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1828 bmap
= isl_basic_map_eliminate_vars(bmap
,
1829 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1832 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1834 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1837 isl_basic_map_free(bmap
);
1841 /* Return true if the definition of the given div (recursively) involves
1842 * any of the given variables.
1844 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1845 unsigned first
, unsigned n
)
1848 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1850 if (isl_int_is_zero(bmap
->div
[div
][0]))
1852 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1855 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1856 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1858 if (div_involves_vars(bmap
, i
, first
, n
))
1865 /* Try and add a lower and/or upper bound on "div" to "bmap"
1866 * based on inequality "i".
1867 * "total" is the total number of variables (excluding the divs).
1868 * "v" is a temporary object that can be used during the calculations.
1869 * If "lb" is set, then a lower bound should be constructed.
1870 * If "ub" is set, then an upper bound should be constructed.
1872 * The calling function has already checked that the inequality does not
1873 * reference "div", but we still need to check that the inequality is
1874 * of the right form. We'll consider the case where we want to construct
1875 * a lower bound. The construction of upper bounds is similar.
1877 * Let "div" be of the form
1879 * q = floor((a + f(x))/d)
1881 * We essentially check if constraint "i" is of the form
1885 * so that we can use it to derive a lower bound on "div".
1886 * However, we allow a slightly more general form
1890 * with the condition that the coefficients of g(x) - f(x) are all
1892 * Rewriting this constraint as
1896 * adding a + f(x) to both sides and dividing by d, we obtain
1898 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1900 * Taking the floor on both sides, we obtain
1902 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1906 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1908 * In the case of an upper bound, we construct the constraint
1910 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1913 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1914 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1915 unsigned total
, isl_int v
, int lb
, int ub
)
1919 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1921 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1922 bmap
->div
[div
][1 + 1 + j
]);
1923 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1926 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1927 bmap
->div
[div
][1 + 1 + j
]);
1928 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1934 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1936 int k
= isl_basic_map_alloc_inequality(bmap
);
1939 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1940 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1941 bmap
->div
[div
][1 + j
]);
1942 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1943 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1945 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1948 int k
= isl_basic_map_alloc_inequality(bmap
);
1951 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1952 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1953 bmap
->div
[div
][1 + j
]);
1954 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
1955 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1957 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
1962 isl_basic_map_free(bmap
);
1966 /* This function is called right before "div" is eliminated from "bmap"
1967 * using Fourier-Motzkin.
1968 * Look through the constraints of "bmap" for constraints on the argument
1969 * of the integer division and use them to construct constraints on the
1970 * integer division itself. These constraints can then be combined
1971 * during the Fourier-Motzkin elimination.
1972 * Note that it is only useful to introduce lower bounds on "div"
1973 * if "bmap" already contains upper bounds on "div" as the newly
1974 * introduce lower bounds can then be combined with the pre-existing
1975 * upper bounds. Similarly for upper bounds.
1976 * We therefore first check if "bmap" contains any lower and/or upper bounds
1979 * It is interesting to note that the introduction of these constraints
1980 * can indeed lead to more accurate results, even when compared to
1981 * deriving constraints on the argument of "div" from constraints on "div".
1982 * Consider, for example, the set
1984 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1986 * The second constraint can be rewritten as
1988 * 2 * [(-i-2j+3)/4] + k >= 0
1990 * from which we can derive
1992 * -i - 2j + 3 >= -2k
1998 * Combined with the first constraint, we obtain
2000 * -3 <= 3 + 2k or k >= -3
2002 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2003 * the first constraint, we obtain
2005 * [(i + 2j)/4] >= [-3/4] = -1
2007 * Combining this constraint with the second constraint, we obtain
2011 static __isl_give isl_basic_map
*insert_bounds_on_div(
2012 __isl_take isl_basic_map
*bmap
, int div
)
2015 int check_lb
, check_ub
;
2022 if (isl_int_is_zero(bmap
->div
[div
][0]))
2025 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2029 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2030 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2037 if (!check_lb
&& !check_ub
)
2042 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2043 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2046 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2047 check_lb
, check_ub
);
2055 /* Remove all divs (recursively) involving any of the given dimensions
2056 * in their definitions.
2058 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2059 __isl_take isl_basic_map
*bmap
,
2060 enum isl_dim_type type
, unsigned first
, unsigned n
)
2066 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2068 first
+= isl_basic_map_offset(bmap
, type
);
2070 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2071 if (!div_involves_vars(bmap
, i
, first
, n
))
2073 bmap
= insert_bounds_on_div(bmap
, i
);
2074 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2082 isl_basic_map_free(bmap
);
2086 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2087 __isl_take isl_basic_set
*bset
,
2088 enum isl_dim_type type
, unsigned first
, unsigned n
)
2090 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2093 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2094 enum isl_dim_type type
, unsigned first
, unsigned n
)
2103 map
= isl_map_cow(map
);
2107 for (i
= 0; i
< map
->n
; ++i
) {
2108 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2119 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2120 enum isl_dim_type type
, unsigned first
, unsigned n
)
2122 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2126 /* Does the desciption of "bmap" depend on the specified dimensions?
2127 * We also check whether the dimensions appear in any of the div definitions.
2128 * In principle there is no need for this check. If the dimensions appear
2129 * in a div definition, they also appear in the defining constraints of that
2132 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2133 enum isl_dim_type type
, unsigned first
, unsigned n
)
2140 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2141 isl_die(bmap
->ctx
, isl_error_invalid
,
2142 "index out of bounds", return -1);
2144 first
+= isl_basic_map_offset(bmap
, type
);
2145 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2146 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2148 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2149 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2151 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2152 if (isl_int_is_zero(bmap
->div
[i
][0]))
2154 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2161 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2162 enum isl_dim_type type
, unsigned first
, unsigned n
)
2169 if (first
+ n
> isl_map_dim(map
, type
))
2170 isl_die(map
->ctx
, isl_error_invalid
,
2171 "index out of bounds", return -1);
2173 for (i
= 0; i
< map
->n
; ++i
) {
2174 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2176 if (involves
< 0 || involves
)
2183 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2184 enum isl_dim_type type
, unsigned first
, unsigned n
)
2186 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2189 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2190 enum isl_dim_type type
, unsigned first
, unsigned n
)
2192 return isl_map_involves_dims(set
, type
, first
, n
);
2195 /* Return true if the definition of the given div is unknown or depends
2198 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2201 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2203 if (isl_int_is_zero(bmap
->div
[div
][0]))
2206 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2207 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2209 if (div_is_unknown(bmap
, i
))
2216 /* Remove all divs that are unknown or defined in terms of unknown divs.
2218 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2219 __isl_take isl_basic_map
*bmap
)
2226 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2227 if (!div_is_unknown(bmap
, i
))
2229 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2238 /* Remove all divs that are unknown or defined in terms of unknown divs.
2240 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2241 __isl_take isl_basic_set
*bset
)
2243 return isl_basic_map_remove_unknown_divs(bset
);
2246 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2255 map
= isl_map_cow(map
);
2259 for (i
= 0; i
< map
->n
; ++i
) {
2260 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2270 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2272 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2275 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2276 __isl_take isl_basic_set
*bset
,
2277 enum isl_dim_type type
, unsigned first
, unsigned n
)
2279 return (isl_basic_set
*)
2280 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2283 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2284 enum isl_dim_type type
, unsigned first
, unsigned n
)
2291 map
= isl_map_cow(map
);
2294 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2296 for (i
= 0; i
< map
->n
; ++i
) {
2297 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2298 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2302 map
= isl_map_drop(map
, type
, first
, n
);
2309 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2310 enum isl_dim_type type
, unsigned first
, unsigned n
)
2312 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2315 /* Project out n inputs starting at first using Fourier-Motzkin */
2316 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2317 unsigned first
, unsigned n
)
2319 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2322 static void dump_term(struct isl_basic_map
*bmap
,
2323 isl_int c
, int pos
, FILE *out
)
2326 unsigned in
= isl_basic_map_n_in(bmap
);
2327 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2328 unsigned nparam
= isl_basic_map_n_param(bmap
);
2330 isl_int_print(out
, c
, 0);
2332 if (!isl_int_is_one(c
))
2333 isl_int_print(out
, c
, 0);
2334 if (pos
< 1 + nparam
) {
2335 name
= isl_space_get_dim_name(bmap
->dim
,
2336 isl_dim_param
, pos
- 1);
2338 fprintf(out
, "%s", name
);
2340 fprintf(out
, "p%d", pos
- 1);
2341 } else if (pos
< 1 + nparam
+ in
)
2342 fprintf(out
, "i%d", pos
- 1 - nparam
);
2343 else if (pos
< 1 + nparam
+ dim
)
2344 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2346 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2350 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2351 int sign
, FILE *out
)
2355 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2359 for (i
= 0, first
= 1; i
< len
; ++i
) {
2360 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2363 fprintf(out
, " + ");
2365 isl_int_abs(v
, c
[i
]);
2366 dump_term(bmap
, v
, i
, out
);
2373 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2374 const char *op
, FILE *out
, int indent
)
2378 fprintf(out
, "%*s", indent
, "");
2380 dump_constraint_sign(bmap
, c
, 1, out
);
2381 fprintf(out
, " %s ", op
);
2382 dump_constraint_sign(bmap
, c
, -1, out
);
2386 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2387 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2389 fprintf(out
, "%*s", indent
, "");
2390 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2395 static void dump_constraints(struct isl_basic_map
*bmap
,
2396 isl_int
**c
, unsigned n
,
2397 const char *op
, FILE *out
, int indent
)
2401 for (i
= 0; i
< n
; ++i
)
2402 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2405 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2409 unsigned total
= isl_basic_map_total_dim(bmap
);
2411 for (j
= 0; j
< 1 + total
; ++j
) {
2412 if (isl_int_is_zero(exp
[j
]))
2414 if (!first
&& isl_int_is_pos(exp
[j
]))
2416 dump_term(bmap
, exp
[j
], j
, out
);
2421 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2425 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2426 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2428 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2429 fprintf(out
, "%*s", indent
, "");
2430 fprintf(out
, "e%d = [(", i
);
2431 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2433 isl_int_print(out
, bmap
->div
[i
][0], 0);
2434 fprintf(out
, "]\n");
2438 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2439 FILE *out
, int indent
)
2442 fprintf(out
, "null basic set\n");
2446 fprintf(out
, "%*s", indent
, "");
2447 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2448 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2449 bset
->extra
, bset
->flags
);
2450 dump((struct isl_basic_map
*)bset
, out
, indent
);
2453 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2454 FILE *out
, int indent
)
2457 fprintf(out
, "null basic map\n");
2461 fprintf(out
, "%*s", indent
, "");
2462 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2463 "flags: %x, n_name: %d\n",
2465 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2466 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2467 dump(bmap
, out
, indent
);
2470 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2475 total
= isl_basic_map_total_dim(bmap
);
2476 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2477 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2478 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2479 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2483 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2486 struct isl_set
*set
;
2490 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2491 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2492 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2493 sizeof(struct isl_set
) +
2494 (n
- 1) * sizeof(struct isl_basic_set
*));
2498 set
->ctx
= dim
->ctx
;
2499 isl_ctx_ref(set
->ctx
);
2507 isl_space_free(dim
);
2511 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2512 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2514 struct isl_set
*set
;
2517 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2521 set
= isl_set_alloc_space(dims
, n
, flags
);
2525 /* Make sure "map" has room for at least "n" more basic maps.
2527 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2530 struct isl_map
*grown
= NULL
;
2534 isl_assert(map
->ctx
, n
>= 0, goto error
);
2535 if (map
->n
+ n
<= map
->size
)
2537 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2540 for (i
= 0; i
< map
->n
; ++i
) {
2541 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2549 isl_map_free(grown
);
2554 /* Make sure "set" has room for at least "n" more basic sets.
2556 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2558 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2561 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2564 struct isl_set
*dup
;
2569 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2572 for (i
= 0; i
< set
->n
; ++i
)
2573 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2577 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2579 return isl_map_from_basic_map(bset
);
2582 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2584 struct isl_map
*map
;
2589 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2590 return isl_map_add_basic_map(map
, bmap
);
2593 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2594 __isl_take isl_basic_set
*bset
)
2596 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2597 (struct isl_basic_map
*)bset
);
2600 void *isl_set_free(__isl_take isl_set
*set
)
2610 isl_ctx_deref(set
->ctx
);
2611 for (i
= 0; i
< set
->n
; ++i
)
2612 isl_basic_set_free(set
->p
[i
]);
2613 isl_space_free(set
->dim
);
2619 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2624 fprintf(out
, "null set\n");
2628 fprintf(out
, "%*s", indent
, "");
2629 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2630 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2632 for (i
= 0; i
< set
->n
; ++i
) {
2633 fprintf(out
, "%*s", indent
, "");
2634 fprintf(out
, "basic set %d:\n", i
);
2635 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2639 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2644 fprintf(out
, "null map\n");
2648 fprintf(out
, "%*s", indent
, "");
2649 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2650 "flags: %x, n_name: %d\n",
2651 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2652 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2653 for (i
= 0; i
< map
->n
; ++i
) {
2654 fprintf(out
, "%*s", indent
, "");
2655 fprintf(out
, "basic map %d:\n", i
);
2656 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2660 struct isl_basic_map
*isl_basic_map_intersect_domain(
2661 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2663 struct isl_basic_map
*bmap_domain
;
2668 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2669 bset
->dim
, isl_dim_param
), goto error
);
2671 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2672 isl_assert(bset
->ctx
,
2673 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2675 bmap
= isl_basic_map_cow(bmap
);
2678 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2679 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2680 bmap_domain
= isl_basic_map_from_domain(bset
);
2681 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2683 bmap
= isl_basic_map_simplify(bmap
);
2684 return isl_basic_map_finalize(bmap
);
2686 isl_basic_map_free(bmap
);
2687 isl_basic_set_free(bset
);
2691 struct isl_basic_map
*isl_basic_map_intersect_range(
2692 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2694 struct isl_basic_map
*bmap_range
;
2699 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2700 bset
->dim
, isl_dim_param
), goto error
);
2702 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2703 isl_assert(bset
->ctx
,
2704 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2706 if (isl_basic_set_is_universe(bset
)) {
2707 isl_basic_set_free(bset
);
2711 bmap
= isl_basic_map_cow(bmap
);
2714 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2715 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2716 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2717 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2719 bmap
= isl_basic_map_simplify(bmap
);
2720 return isl_basic_map_finalize(bmap
);
2722 isl_basic_map_free(bmap
);
2723 isl_basic_set_free(bset
);
2727 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2736 total
= 1 + isl_basic_map_total_dim(bmap
);
2737 if (total
!= vec
->size
)
2742 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2743 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2744 if (!isl_int_is_zero(s
)) {
2750 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2751 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2752 if (isl_int_is_neg(s
)) {
2763 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2765 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2768 struct isl_basic_map
*isl_basic_map_intersect(
2769 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2771 struct isl_vec
*sample
= NULL
;
2773 if (!bmap1
|| !bmap2
)
2776 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2777 bmap2
->dim
, isl_dim_param
), goto error
);
2778 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2779 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2780 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2781 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2782 return isl_basic_map_intersect(bmap2
, bmap1
);
2784 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2785 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2786 isl_assert(bmap1
->ctx
,
2787 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2789 if (bmap1
->sample
&&
2790 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2791 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2792 sample
= isl_vec_copy(bmap1
->sample
);
2793 else if (bmap2
->sample
&&
2794 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2795 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2796 sample
= isl_vec_copy(bmap2
->sample
);
2798 bmap1
= isl_basic_map_cow(bmap1
);
2801 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2802 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2803 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2806 isl_vec_free(sample
);
2808 isl_vec_free(bmap1
->sample
);
2809 bmap1
->sample
= sample
;
2812 bmap1
= isl_basic_map_simplify(bmap1
);
2813 return isl_basic_map_finalize(bmap1
);
2816 isl_vec_free(sample
);
2817 isl_basic_map_free(bmap1
);
2818 isl_basic_map_free(bmap2
);
2822 struct isl_basic_set
*isl_basic_set_intersect(
2823 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2825 return (struct isl_basic_set
*)
2826 isl_basic_map_intersect(
2827 (struct isl_basic_map
*)bset1
,
2828 (struct isl_basic_map
*)bset2
);
2831 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2832 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2834 return isl_basic_set_intersect(bset1
, bset2
);
2837 /* Special case of isl_map_intersect, where both map1 and map2
2838 * are convex, without any divs and such that either map1 or map2
2839 * contains a single constraint. This constraint is then simply
2840 * added to the other map.
2842 static __isl_give isl_map
*map_intersect_add_constraint(
2843 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2845 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2846 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2847 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2848 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2850 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2851 return isl_map_intersect(map2
, map1
);
2853 isl_assert(map2
->ctx
,
2854 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2856 map1
= isl_map_cow(map1
);
2859 if (isl_map_plain_is_empty(map1
)) {
2863 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2864 if (map2
->p
[0]->n_eq
== 1)
2865 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2867 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2868 map2
->p
[0]->ineq
[0]);
2870 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2871 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2875 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2876 isl_basic_map_free(map1
->p
[0]);
2889 /* map2 may be either a parameter domain or a map living in the same
2892 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2893 __isl_take isl_map
*map2
)
2902 if ((isl_map_plain_is_empty(map1
) ||
2903 isl_map_plain_is_universe(map2
)) &&
2904 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2908 if ((isl_map_plain_is_empty(map2
) ||
2909 isl_map_plain_is_universe(map1
)) &&
2910 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2915 if (map1
->n
== 1 && map2
->n
== 1 &&
2916 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2917 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2918 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2919 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2920 return map_intersect_add_constraint(map1
, map2
);
2922 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2923 isl_space_dim(map2
->dim
, isl_dim_param
))
2924 isl_assert(map1
->ctx
,
2925 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2927 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2928 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2929 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2931 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2932 map1
->n
* map2
->n
, flags
);
2935 for (i
= 0; i
< map1
->n
; ++i
)
2936 for (j
= 0; j
< map2
->n
; ++j
) {
2937 struct isl_basic_map
*part
;
2938 part
= isl_basic_map_intersect(
2939 isl_basic_map_copy(map1
->p
[i
]),
2940 isl_basic_map_copy(map2
->p
[j
]));
2941 if (isl_basic_map_is_empty(part
) < 0)
2942 part
= isl_basic_map_free(part
);
2943 result
= isl_map_add_basic_map(result
, part
);
2956 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2957 __isl_take isl_map
*map2
)
2961 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2962 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2963 "spaces don't match", goto error
);
2964 return map_intersect_internal(map1
, map2
);
2971 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2972 __isl_take isl_map
*map2
)
2974 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2977 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2979 return (struct isl_set
*)
2980 isl_map_intersect((struct isl_map
*)set1
,
2981 (struct isl_map
*)set2
);
2984 /* map_intersect_internal accepts intersections
2985 * with parameter domains, so we can just call that function.
2987 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2988 __isl_take isl_set
*params
)
2990 return map_intersect_internal(map
, params
);
2993 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2994 __isl_take isl_map
*map2
)
2996 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2999 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3000 __isl_take isl_set
*params
)
3002 return isl_map_intersect_params(set
, params
);
3005 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3008 struct isl_basic_set
*bset
;
3013 bmap
= isl_basic_map_cow(bmap
);
3016 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3017 in
= isl_basic_map_n_in(bmap
);
3018 bset
= isl_basic_set_from_basic_map(bmap
);
3019 bset
= isl_basic_set_swap_vars(bset
, in
);
3020 return isl_basic_map_from_basic_set(bset
, dim
);
3023 static __isl_give isl_basic_map
*basic_map_space_reset(
3024 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3030 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3033 space
= isl_basic_map_get_space(bmap
);
3034 space
= isl_space_reset(space
, type
);
3035 bmap
= isl_basic_map_reset_space(bmap
, space
);
3039 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3040 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3041 unsigned pos
, unsigned n
)
3044 struct isl_basic_map
*res
;
3045 struct isl_dim_map
*dim_map
;
3046 unsigned total
, off
;
3047 enum isl_dim_type t
;
3050 return basic_map_space_reset(bmap
, type
);
3055 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3057 total
= isl_basic_map_total_dim(bmap
) + n
;
3058 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3060 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3062 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3064 unsigned size
= isl_basic_map_dim(bmap
, t
);
3065 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3067 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3068 pos
, size
- pos
, off
+ pos
+ n
);
3070 off
+= isl_space_dim(res_dim
, t
);
3072 isl_dim_map_div(dim_map
, bmap
, off
);
3074 res
= isl_basic_map_alloc_space(res_dim
,
3075 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3076 if (isl_basic_map_is_rational(bmap
))
3077 res
= isl_basic_map_set_rational(res
);
3078 if (isl_basic_map_plain_is_empty(bmap
)) {
3079 isl_basic_map_free(bmap
);
3081 return isl_basic_map_set_to_empty(res
);
3083 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3084 return isl_basic_map_finalize(res
);
3087 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3088 __isl_take isl_basic_set
*bset
,
3089 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3091 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3094 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3095 enum isl_dim_type type
, unsigned n
)
3099 return isl_basic_map_insert_dims(bmap
, type
,
3100 isl_basic_map_dim(bmap
, type
), n
);
3103 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3104 enum isl_dim_type type
, unsigned n
)
3108 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3109 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3111 isl_basic_set_free(bset
);
3115 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3116 enum isl_dim_type type
)
3120 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3123 space
= isl_map_get_space(map
);
3124 space
= isl_space_reset(space
, type
);
3125 map
= isl_map_reset_space(map
, space
);
3129 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3130 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3135 return map_space_reset(map
, type
);
3137 map
= isl_map_cow(map
);
3141 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3145 for (i
= 0; i
< map
->n
; ++i
) {
3146 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3157 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3158 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3160 return isl_map_insert_dims(set
, type
, pos
, n
);
3163 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3164 enum isl_dim_type type
, unsigned n
)
3168 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3171 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3172 enum isl_dim_type type
, unsigned n
)
3176 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3177 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3183 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3184 __isl_take isl_basic_map
*bmap
,
3185 enum isl_dim_type dst_type
, unsigned dst_pos
,
3186 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3188 struct isl_dim_map
*dim_map
;
3189 struct isl_basic_map
*res
;
3190 enum isl_dim_type t
;
3191 unsigned total
, off
;
3198 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3201 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3204 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3206 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3207 pos(bmap
->dim
, src_type
) + src_pos
+
3208 ((src_type
< dst_type
) ? n
: 0)) {
3209 bmap
= isl_basic_map_cow(bmap
);
3213 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3214 src_type
, src_pos
, n
);
3218 bmap
= isl_basic_map_finalize(bmap
);
3223 total
= isl_basic_map_total_dim(bmap
);
3224 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3227 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3228 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3229 if (t
== dst_type
) {
3230 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3233 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3236 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3237 dst_pos
, size
- dst_pos
, off
);
3238 off
+= size
- dst_pos
;
3239 } else if (t
== src_type
) {
3240 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3243 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3244 src_pos
+ n
, size
- src_pos
- n
, off
);
3245 off
+= size
- src_pos
- n
;
3247 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3251 isl_dim_map_div(dim_map
, bmap
, off
);
3253 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3254 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3255 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3259 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3260 src_type
, src_pos
, n
);
3264 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3265 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3266 bmap
= isl_basic_map_finalize(bmap
);
3270 isl_basic_map_free(bmap
);
3274 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3275 enum isl_dim_type dst_type
, unsigned dst_pos
,
3276 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3278 return (isl_basic_set
*)isl_basic_map_move_dims(
3279 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3282 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3283 enum isl_dim_type dst_type
, unsigned dst_pos
,
3284 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3288 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3289 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3290 src_type
, src_pos
, n
);
3296 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3297 enum isl_dim_type dst_type
, unsigned dst_pos
,
3298 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3307 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3310 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3313 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3315 map
= isl_map_cow(map
);
3319 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3323 for (i
= 0; i
< map
->n
; ++i
) {
3324 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3326 src_type
, src_pos
, n
);
3337 /* Move the specified dimensions to the last columns right before
3338 * the divs. Don't change the dimension specification of bmap.
3339 * That's the responsibility of the caller.
3341 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3342 enum isl_dim_type type
, unsigned first
, unsigned n
)
3344 struct isl_dim_map
*dim_map
;
3345 struct isl_basic_map
*res
;
3346 enum isl_dim_type t
;
3347 unsigned total
, off
;
3351 if (pos(bmap
->dim
, type
) + first
+ n
==
3352 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3355 total
= isl_basic_map_total_dim(bmap
);
3356 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3359 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3360 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3362 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3365 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3366 first
, n
, total
- bmap
->n_div
- n
);
3367 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3368 first
+ n
, size
- (first
+ n
), off
);
3369 off
+= size
- (first
+ n
);
3371 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3375 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3377 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3378 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3379 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3383 /* Insert "n" rows in the divs of "bmap".
3385 * The number of columns is not changed, which means that the last
3386 * dimensions of "bmap" are being reintepreted as the new divs.
3387 * The space of "bmap" is not adjusted, however, which means
3388 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3389 * from the space of "bmap" is the responsibility of the caller.
3391 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3399 bmap
= isl_basic_map_cow(bmap
);
3403 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3404 old
= bmap
->block2
.data
;
3405 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3406 (bmap
->extra
+ n
) * (1 + row_size
));
3407 if (!bmap
->block2
.data
)
3408 return isl_basic_map_free(bmap
);
3409 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3411 return isl_basic_map_free(bmap
);
3412 for (i
= 0; i
< n
; ++i
) {
3413 new_div
[i
] = bmap
->block2
.data
+
3414 (bmap
->extra
+ i
) * (1 + row_size
);
3415 isl_seq_clr(new_div
[i
], 1 + row_size
);
3417 for (i
= 0; i
< bmap
->extra
; ++i
)
3418 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3420 bmap
->div
= new_div
;
3427 /* Turn the n dimensions of type type, starting at first
3428 * into existentially quantified variables.
3430 __isl_give isl_basic_map
*isl_basic_map_project_out(
3431 __isl_take isl_basic_map
*bmap
,
3432 enum isl_dim_type type
, unsigned first
, unsigned n
)
3435 return basic_map_space_reset(bmap
, type
);
3440 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3441 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3443 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3446 bmap
= move_last(bmap
, type
, first
, n
);
3447 bmap
= isl_basic_map_cow(bmap
);
3448 bmap
= insert_div_rows(bmap
, n
);
3452 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3455 bmap
= isl_basic_map_simplify(bmap
);
3456 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3457 return isl_basic_map_finalize(bmap
);
3459 isl_basic_map_free(bmap
);
3463 /* Turn the n dimensions of type type, starting at first
3464 * into existentially quantified variables.
3466 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3467 enum isl_dim_type type
, unsigned first
, unsigned n
)
3469 return (isl_basic_set
*)isl_basic_map_project_out(
3470 (isl_basic_map
*)bset
, type
, first
, n
);
3473 /* Turn the n dimensions of type type, starting at first
3474 * into existentially quantified variables.
3476 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3477 enum isl_dim_type type
, unsigned first
, unsigned n
)
3485 return map_space_reset(map
, type
);
3487 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3489 map
= isl_map_cow(map
);
3493 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3497 for (i
= 0; i
< map
->n
; ++i
) {
3498 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3509 /* Turn the n dimensions of type type, starting at first
3510 * into existentially quantified variables.
3512 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3513 enum isl_dim_type type
, unsigned first
, unsigned n
)
3515 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3518 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3522 for (i
= 0; i
< n
; ++i
) {
3523 j
= isl_basic_map_alloc_div(bmap
);
3526 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3530 isl_basic_map_free(bmap
);
3534 struct isl_basic_map
*isl_basic_map_apply_range(
3535 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3537 isl_space
*dim_result
= NULL
;
3538 struct isl_basic_map
*bmap
;
3539 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3540 struct isl_dim_map
*dim_map1
, *dim_map2
;
3542 if (!bmap1
|| !bmap2
)
3544 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3545 bmap2
->dim
, isl_dim_param
))
3546 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3547 "parameters don't match", goto error
);
3548 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3549 bmap2
->dim
, isl_dim_in
))
3550 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3551 "spaces don't match", goto error
);
3553 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3554 isl_space_copy(bmap2
->dim
));
3556 n_in
= isl_basic_map_n_in(bmap1
);
3557 n_out
= isl_basic_map_n_out(bmap2
);
3558 n
= isl_basic_map_n_out(bmap1
);
3559 nparam
= isl_basic_map_n_param(bmap1
);
3561 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3562 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3563 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3564 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3565 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3566 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3567 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3568 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3569 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3570 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3571 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3573 bmap
= isl_basic_map_alloc_space(dim_result
,
3574 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3575 bmap1
->n_eq
+ bmap2
->n_eq
,
3576 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3577 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3578 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3579 bmap
= add_divs(bmap
, n
);
3580 bmap
= isl_basic_map_simplify(bmap
);
3581 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3582 return isl_basic_map_finalize(bmap
);
3584 isl_basic_map_free(bmap1
);
3585 isl_basic_map_free(bmap2
);
3589 struct isl_basic_set
*isl_basic_set_apply(
3590 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3595 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3598 return (struct isl_basic_set
*)
3599 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3601 isl_basic_set_free(bset
);
3602 isl_basic_map_free(bmap
);
3606 struct isl_basic_map
*isl_basic_map_apply_domain(
3607 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3609 if (!bmap1
|| !bmap2
)
3612 isl_assert(bmap1
->ctx
,
3613 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3614 isl_assert(bmap1
->ctx
,
3615 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3618 bmap1
= isl_basic_map_reverse(bmap1
);
3619 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3620 return isl_basic_map_reverse(bmap1
);
3622 isl_basic_map_free(bmap1
);
3623 isl_basic_map_free(bmap2
);
3627 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3628 * A \cap B -> f(A) + f(B)
3630 struct isl_basic_map
*isl_basic_map_sum(
3631 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3633 unsigned n_in
, n_out
, nparam
, total
, pos
;
3634 struct isl_basic_map
*bmap
= NULL
;
3635 struct isl_dim_map
*dim_map1
, *dim_map2
;
3638 if (!bmap1
|| !bmap2
)
3641 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3644 nparam
= isl_basic_map_n_param(bmap1
);
3645 n_in
= isl_basic_map_n_in(bmap1
);
3646 n_out
= isl_basic_map_n_out(bmap1
);
3648 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3649 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3650 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3651 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3652 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3653 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3654 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3655 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3656 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3657 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3658 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3660 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3661 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3662 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3663 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3664 for (i
= 0; i
< n_out
; ++i
) {
3665 int j
= isl_basic_map_alloc_equality(bmap
);
3668 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3669 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3670 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3671 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3673 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3674 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3675 bmap
= add_divs(bmap
, 2 * n_out
);
3677 bmap
= isl_basic_map_simplify(bmap
);
3678 return isl_basic_map_finalize(bmap
);
3680 isl_basic_map_free(bmap
);
3681 isl_basic_map_free(bmap1
);
3682 isl_basic_map_free(bmap2
);
3686 /* Given two maps A -> f(A) and B -> g(B), construct a map
3687 * A \cap B -> f(A) + f(B)
3689 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3691 struct isl_map
*result
;
3697 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3699 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3700 map1
->n
* map2
->n
, 0);
3703 for (i
= 0; i
< map1
->n
; ++i
)
3704 for (j
= 0; j
< map2
->n
; ++j
) {
3705 struct isl_basic_map
*part
;
3706 part
= isl_basic_map_sum(
3707 isl_basic_map_copy(map1
->p
[i
]),
3708 isl_basic_map_copy(map2
->p
[j
]));
3709 if (isl_basic_map_is_empty(part
))
3710 isl_basic_map_free(part
);
3712 result
= isl_map_add_basic_map(result
, part
);
3725 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3726 __isl_take isl_set
*set2
)
3728 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3731 /* Given a basic map A -> f(A), construct A -> -f(A).
3733 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3738 bmap
= isl_basic_map_cow(bmap
);
3742 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3743 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3744 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3745 for (j
= 0; j
< n
; ++j
)
3746 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3747 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3748 for (j
= 0; j
< n
; ++j
)
3749 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3750 for (i
= 0; i
< bmap
->n_div
; ++i
)
3751 for (j
= 0; j
< n
; ++j
)
3752 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3753 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3754 return isl_basic_map_finalize(bmap
);
3757 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3759 return isl_basic_map_neg(bset
);
3762 /* Given a map A -> f(A), construct A -> -f(A).
3764 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3768 map
= isl_map_cow(map
);
3772 for (i
= 0; i
< map
->n
; ++i
) {
3773 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3784 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3786 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3789 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3790 * A -> floor(f(A)/d).
3792 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3795 unsigned n_in
, n_out
, nparam
, total
, pos
;
3796 struct isl_basic_map
*result
= NULL
;
3797 struct isl_dim_map
*dim_map
;
3803 nparam
= isl_basic_map_n_param(bmap
);
3804 n_in
= isl_basic_map_n_in(bmap
);
3805 n_out
= isl_basic_map_n_out(bmap
);
3807 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3808 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3809 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3810 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3811 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3812 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3814 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3815 bmap
->n_div
+ n_out
,
3816 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3817 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3818 result
= add_divs(result
, n_out
);
3819 for (i
= 0; i
< n_out
; ++i
) {
3821 j
= isl_basic_map_alloc_inequality(result
);
3824 isl_seq_clr(result
->ineq
[j
], 1+total
);
3825 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3826 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3827 j
= isl_basic_map_alloc_inequality(result
);
3830 isl_seq_clr(result
->ineq
[j
], 1+total
);
3831 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3832 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3833 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3836 result
= isl_basic_map_simplify(result
);
3837 return isl_basic_map_finalize(result
);
3839 isl_basic_map_free(result
);
3843 /* Given a map A -> f(A) and an integer d, construct a map
3844 * A -> floor(f(A)/d).
3846 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3850 map
= isl_map_cow(map
);
3854 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3855 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3856 for (i
= 0; i
< map
->n
; ++i
) {
3857 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3868 /* Given a map A -> f(A) and an integer d, construct a map
3869 * A -> floor(f(A)/d).
3871 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3872 __isl_take isl_val
*d
)
3876 if (!isl_val_is_int(d
))
3877 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3878 "expecting integer denominator", goto error
);
3879 map
= isl_map_floordiv(map
, d
->n
);
3888 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3894 i
= isl_basic_map_alloc_equality(bmap
);
3897 nparam
= isl_basic_map_n_param(bmap
);
3898 n_in
= isl_basic_map_n_in(bmap
);
3899 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3900 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3901 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3902 return isl_basic_map_finalize(bmap
);
3904 isl_basic_map_free(bmap
);
3908 /* Add a constraints to "bmap" expressing i_pos < o_pos
3910 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3916 i
= isl_basic_map_alloc_inequality(bmap
);
3919 nparam
= isl_basic_map_n_param(bmap
);
3920 n_in
= isl_basic_map_n_in(bmap
);
3921 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3922 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3923 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3924 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3925 return isl_basic_map_finalize(bmap
);
3927 isl_basic_map_free(bmap
);
3931 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3933 static __isl_give isl_basic_map
*var_less_or_equal(
3934 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3940 i
= isl_basic_map_alloc_inequality(bmap
);
3943 nparam
= isl_basic_map_n_param(bmap
);
3944 n_in
= isl_basic_map_n_in(bmap
);
3945 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3946 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3947 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3948 return isl_basic_map_finalize(bmap
);
3950 isl_basic_map_free(bmap
);
3954 /* Add a constraints to "bmap" expressing i_pos > o_pos
3956 static struct isl_basic_map
*var_more(struct 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
][0], -1);
3969 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3970 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3971 return isl_basic_map_finalize(bmap
);
3973 isl_basic_map_free(bmap
);
3977 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3979 static __isl_give isl_basic_map
*var_more_or_equal(
3980 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3986 i
= isl_basic_map_alloc_inequality(bmap
);
3989 nparam
= isl_basic_map_n_param(bmap
);
3990 n_in
= isl_basic_map_n_in(bmap
);
3991 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3992 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3993 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3994 return isl_basic_map_finalize(bmap
);
3996 isl_basic_map_free(bmap
);
4000 __isl_give isl_basic_map
*isl_basic_map_equal(
4001 __isl_take isl_space
*dim
, unsigned n_equal
)
4004 struct isl_basic_map
*bmap
;
4005 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4008 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4009 bmap
= var_equal(bmap
, i
);
4010 return isl_basic_map_finalize(bmap
);
4013 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4015 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4019 struct isl_basic_map
*bmap
;
4020 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4023 for (i
= 0; i
< pos
&& bmap
; ++i
)
4024 bmap
= var_equal(bmap
, i
);
4026 bmap
= var_less(bmap
, pos
);
4027 return isl_basic_map_finalize(bmap
);
4030 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4032 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4033 __isl_take isl_space
*dim
, unsigned pos
)
4036 isl_basic_map
*bmap
;
4038 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4039 for (i
= 0; i
< pos
; ++i
)
4040 bmap
= var_equal(bmap
, i
);
4041 bmap
= var_less_or_equal(bmap
, pos
);
4042 return isl_basic_map_finalize(bmap
);
4045 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4047 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4051 struct isl_basic_map
*bmap
;
4052 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4055 for (i
= 0; i
< pos
&& bmap
; ++i
)
4056 bmap
= var_equal(bmap
, i
);
4058 bmap
= var_more(bmap
, pos
);
4059 return isl_basic_map_finalize(bmap
);
4062 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4064 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4065 __isl_take isl_space
*dim
, unsigned pos
)
4068 isl_basic_map
*bmap
;
4070 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4071 for (i
= 0; i
< pos
; ++i
)
4072 bmap
= var_equal(bmap
, i
);
4073 bmap
= var_more_or_equal(bmap
, pos
);
4074 return isl_basic_map_finalize(bmap
);
4077 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4078 unsigned n
, int equal
)
4080 struct isl_map
*map
;
4083 if (n
== 0 && equal
)
4084 return isl_map_universe(dims
);
4086 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4088 for (i
= 0; i
+ 1 < n
; ++i
)
4089 map
= isl_map_add_basic_map(map
,
4090 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4093 map
= isl_map_add_basic_map(map
,
4094 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4096 map
= isl_map_add_basic_map(map
,
4097 isl_basic_map_less_at(dims
, n
- 1));
4099 isl_space_free(dims
);
4104 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4108 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4111 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4113 return map_lex_lte_first(dim
, n
, 0);
4116 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4118 return map_lex_lte_first(dim
, n
, 1);
4121 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4123 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4126 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4128 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4131 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4132 unsigned n
, int equal
)
4134 struct isl_map
*map
;
4137 if (n
== 0 && equal
)
4138 return isl_map_universe(dims
);
4140 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4142 for (i
= 0; i
+ 1 < n
; ++i
)
4143 map
= isl_map_add_basic_map(map
,
4144 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4147 map
= isl_map_add_basic_map(map
,
4148 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4150 map
= isl_map_add_basic_map(map
,
4151 isl_basic_map_more_at(dims
, n
- 1));
4153 isl_space_free(dims
);
4158 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4162 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4165 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4167 return map_lex_gte_first(dim
, n
, 0);
4170 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4172 return map_lex_gte_first(dim
, n
, 1);
4175 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4177 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4180 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4182 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4185 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4186 __isl_take isl_set
*set2
)
4189 map
= isl_map_lex_le(isl_set_get_space(set1
));
4190 map
= isl_map_intersect_domain(map
, set1
);
4191 map
= isl_map_intersect_range(map
, set2
);
4195 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4196 __isl_take isl_set
*set2
)
4199 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4200 map
= isl_map_intersect_domain(map
, set1
);
4201 map
= isl_map_intersect_range(map
, set2
);
4205 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4206 __isl_take isl_set
*set2
)
4209 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4210 map
= isl_map_intersect_domain(map
, set1
);
4211 map
= isl_map_intersect_range(map
, set2
);
4215 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4216 __isl_take isl_set
*set2
)
4219 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4220 map
= isl_map_intersect_domain(map
, set1
);
4221 map
= isl_map_intersect_range(map
, set2
);
4225 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4226 __isl_take isl_map
*map2
)
4229 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4230 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4231 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4235 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4236 __isl_take isl_map
*map2
)
4239 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4240 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4241 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4245 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4246 __isl_take isl_map
*map2
)
4249 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4250 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4251 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4255 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4256 __isl_take isl_map
*map2
)
4259 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4260 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4261 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4265 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4266 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4268 struct isl_basic_map
*bmap
;
4270 bset
= isl_basic_set_cow(bset
);
4274 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4275 isl_space_free(bset
->dim
);
4276 bmap
= (struct isl_basic_map
*) bset
;
4278 return isl_basic_map_finalize(bmap
);
4280 isl_basic_set_free(bset
);
4281 isl_space_free(dim
);
4285 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4289 if (bmap
->dim
->n_in
== 0)
4290 return (struct isl_basic_set
*)bmap
;
4291 bmap
= isl_basic_map_cow(bmap
);
4294 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4297 bmap
= isl_basic_map_finalize(bmap
);
4298 return (struct isl_basic_set
*)bmap
;
4300 isl_basic_map_free(bmap
);
4304 /* For a div d = floor(f/m), add the constraints
4307 * -(f-(n-1)) + m d >= 0
4309 * Note that the second constraint is the negation of
4313 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4314 unsigned pos
, isl_int
*div
)
4317 unsigned total
= isl_basic_map_total_dim(bmap
);
4319 i
= isl_basic_map_alloc_inequality(bmap
);
4322 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4323 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4325 j
= isl_basic_map_alloc_inequality(bmap
);
4328 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
4329 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
4330 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
4334 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4335 unsigned pos
, isl_int
*div
)
4337 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4341 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4343 unsigned total
= isl_basic_map_total_dim(bmap
);
4344 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4346 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4350 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4352 return isl_basic_map_add_div_constraints(bset
, div
);
4355 struct isl_basic_set
*isl_basic_map_underlying_set(
4356 struct isl_basic_map
*bmap
)
4360 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4362 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4363 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4364 return (struct isl_basic_set
*)bmap
;
4365 bmap
= isl_basic_map_cow(bmap
);
4368 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4371 bmap
->extra
-= bmap
->n_div
;
4373 bmap
= isl_basic_map_finalize(bmap
);
4374 return (struct isl_basic_set
*)bmap
;
4376 isl_basic_map_free(bmap
);
4380 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4381 __isl_take isl_basic_set
*bset
)
4383 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4386 struct isl_basic_map
*isl_basic_map_overlying_set(
4387 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4389 struct isl_basic_map
*bmap
;
4390 struct isl_ctx
*ctx
;
4397 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4398 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4399 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4401 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4402 isl_basic_map_free(like
);
4403 return (struct isl_basic_map
*)bset
;
4405 bset
= isl_basic_set_cow(bset
);
4408 total
= bset
->dim
->n_out
+ bset
->extra
;
4409 bmap
= (struct isl_basic_map
*)bset
;
4410 isl_space_free(bmap
->dim
);
4411 bmap
->dim
= isl_space_copy(like
->dim
);
4414 bmap
->n_div
= like
->n_div
;
4415 bmap
->extra
+= like
->n_div
;
4419 ltotal
= total
- bmap
->extra
+ like
->extra
;
4422 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4423 bmap
->extra
* (1 + 1 + total
));
4424 if (isl_blk_is_error(bmap
->block2
))
4426 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4430 for (i
= 0; i
< bmap
->extra
; ++i
)
4431 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4432 for (i
= 0; i
< like
->n_div
; ++i
) {
4433 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4434 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4436 bmap
= isl_basic_map_extend_constraints(bmap
,
4437 0, 2 * like
->n_div
);
4438 for (i
= 0; i
< like
->n_div
; ++i
) {
4441 if (isl_int_is_zero(bmap
->div
[i
][0]))
4443 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4444 bmap
= isl_basic_map_free(bmap
);
4447 isl_basic_map_free(like
);
4448 bmap
= isl_basic_map_simplify(bmap
);
4449 bmap
= isl_basic_map_finalize(bmap
);
4452 isl_basic_map_free(like
);
4453 isl_basic_set_free(bset
);
4457 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4458 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4460 return (struct isl_basic_set
*)
4461 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4464 struct isl_set
*isl_set_from_underlying_set(
4465 struct isl_set
*set
, struct isl_basic_set
*like
)
4471 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4473 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4474 isl_basic_set_free(like
);
4477 set
= isl_set_cow(set
);
4480 for (i
= 0; i
< set
->n
; ++i
) {
4481 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4482 isl_basic_set_copy(like
));
4486 isl_space_free(set
->dim
);
4487 set
->dim
= isl_space_copy(like
->dim
);
4490 isl_basic_set_free(like
);
4493 isl_basic_set_free(like
);
4498 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4502 map
= isl_map_cow(map
);
4505 map
->dim
= isl_space_cow(map
->dim
);
4509 for (i
= 1; i
< map
->n
; ++i
)
4510 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4512 for (i
= 0; i
< map
->n
; ++i
) {
4513 map
->p
[i
] = (struct isl_basic_map
*)
4514 isl_basic_map_underlying_set(map
->p
[i
]);
4519 map
->dim
= isl_space_underlying(map
->dim
, 0);
4521 isl_space_free(map
->dim
);
4522 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4526 return (struct isl_set
*)map
;
4532 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4534 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4537 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4538 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4540 bmap
= isl_basic_map_cow(bmap
);
4544 isl_space_free(bmap
->dim
);
4547 bmap
= isl_basic_map_finalize(bmap
);
4551 isl_basic_map_free(bmap
);
4552 isl_space_free(dim
);
4556 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4557 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4559 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4563 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4564 __isl_take isl_space
*dim
)
4568 map
= isl_map_cow(map
);
4572 for (i
= 0; i
< map
->n
; ++i
) {
4573 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4574 isl_space_copy(dim
));
4578 isl_space_free(map
->dim
);
4584 isl_space_free(dim
);
4588 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4589 __isl_take isl_space
*dim
)
4591 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4594 /* Compute the parameter domain of the given basic set.
4596 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4601 if (isl_basic_set_is_params(bset
))
4604 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4605 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4606 space
= isl_basic_set_get_space(bset
);
4607 space
= isl_space_params(space
);
4608 bset
= isl_basic_set_reset_space(bset
, space
);
4612 /* Construct a zero-dimensional basic set with the given parameter domain.
4614 __isl_give isl_basic_set
*isl_basic_set_from_params(
4615 __isl_take isl_basic_set
*bset
)
4618 space
= isl_basic_set_get_space(bset
);
4619 space
= isl_space_set_from_params(space
);
4620 bset
= isl_basic_set_reset_space(bset
, space
);
4624 /* Compute the parameter domain of the given set.
4626 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4631 if (isl_set_is_params(set
))
4634 n
= isl_set_dim(set
, isl_dim_set
);
4635 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4636 space
= isl_set_get_space(set
);
4637 space
= isl_space_params(space
);
4638 set
= isl_set_reset_space(set
, space
);
4642 /* Construct a zero-dimensional set with the given parameter domain.
4644 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4647 space
= isl_set_get_space(set
);
4648 space
= isl_space_set_from_params(space
);
4649 set
= isl_set_reset_space(set
, space
);
4653 /* Compute the parameter domain of the given map.
4655 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4660 n
= isl_map_dim(map
, isl_dim_in
);
4661 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4662 n
= isl_map_dim(map
, isl_dim_out
);
4663 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4664 space
= isl_map_get_space(map
);
4665 space
= isl_space_params(space
);
4666 map
= isl_map_reset_space(map
, space
);
4670 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4673 struct isl_basic_set
*domain
;
4679 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4681 n_in
= isl_basic_map_n_in(bmap
);
4682 n_out
= isl_basic_map_n_out(bmap
);
4683 domain
= isl_basic_set_from_basic_map(bmap
);
4684 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4686 domain
= isl_basic_set_reset_space(domain
, dim
);
4691 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4695 return isl_space_may_be_set(bmap
->dim
);
4698 /* Is this basic map actually a set?
4699 * Users should never call this function. Outside of isl,
4700 * the type should indicate whether something is a set or a map.
4702 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4706 return isl_space_is_set(bmap
->dim
);
4709 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4713 if (isl_basic_map_is_set(bmap
))
4715 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4718 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4719 __isl_take isl_basic_map
*bmap
)
4723 isl_basic_map
*domain
;
4724 int nparam
, n_in
, n_out
;
4727 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4728 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4729 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4731 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4732 domain
= isl_basic_map_universe(dim
);
4734 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4735 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4736 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4738 total
= isl_basic_map_total_dim(bmap
);
4740 for (i
= 0; i
< n_in
; ++i
) {
4741 k
= isl_basic_map_alloc_equality(bmap
);
4744 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4745 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4746 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4749 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4750 return isl_basic_map_finalize(bmap
);
4752 isl_basic_map_free(bmap
);
4756 __isl_give isl_basic_map
*isl_basic_map_range_map(
4757 __isl_take isl_basic_map
*bmap
)
4761 isl_basic_map
*range
;
4762 int nparam
, n_in
, n_out
;
4765 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4766 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4767 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4769 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4770 range
= isl_basic_map_universe(dim
);
4772 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4773 bmap
= isl_basic_map_apply_range(bmap
, range
);
4774 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4776 total
= isl_basic_map_total_dim(bmap
);
4778 for (i
= 0; i
< n_out
; ++i
) {
4779 k
= isl_basic_map_alloc_equality(bmap
);
4782 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4783 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4784 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4787 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4788 return isl_basic_map_finalize(bmap
);
4790 isl_basic_map_free(bmap
);
4794 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4798 return isl_space_may_be_set(map
->dim
);
4801 /* Is this map actually a set?
4802 * Users should never call this function. Outside of isl,
4803 * the type should indicate whether something is a set or a map.
4805 int isl_map_is_set(__isl_keep isl_map
*map
)
4809 return isl_space_is_set(map
->dim
);
4812 struct isl_set
*isl_map_range(struct isl_map
*map
)
4815 struct isl_set
*set
;
4819 if (isl_map_is_set(map
))
4820 return (isl_set
*)map
;
4822 map
= isl_map_cow(map
);
4826 set
= (struct isl_set
*) map
;
4827 set
->dim
= isl_space_range(set
->dim
);
4830 for (i
= 0; i
< map
->n
; ++i
) {
4831 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4835 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4836 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4843 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4847 map
= isl_map_cow(map
);
4851 map
->dim
= isl_space_domain_map(map
->dim
);
4854 for (i
= 0; i
< map
->n
; ++i
) {
4855 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4859 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4860 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4867 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4870 isl_space
*range_dim
;
4872 map
= isl_map_cow(map
);
4876 range_dim
= isl_space_range(isl_map_get_space(map
));
4877 range_dim
= isl_space_from_range(range_dim
);
4878 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4879 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4882 for (i
= 0; i
< map
->n
; ++i
) {
4883 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4887 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4888 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4895 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4896 __isl_take isl_space
*dim
)
4899 struct isl_map
*map
= NULL
;
4901 set
= isl_set_cow(set
);
4904 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4905 map
= (struct isl_map
*)set
;
4906 for (i
= 0; i
< set
->n
; ++i
) {
4907 map
->p
[i
] = isl_basic_map_from_basic_set(
4908 set
->p
[i
], isl_space_copy(dim
));
4912 isl_space_free(map
->dim
);
4916 isl_space_free(dim
);
4921 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4922 __isl_take isl_basic_set
*bset
)
4924 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4927 __isl_give isl_basic_map
*isl_basic_map_from_range(
4928 __isl_take isl_basic_set
*bset
)
4931 space
= isl_basic_set_get_space(bset
);
4932 space
= isl_space_from_range(space
);
4933 bset
= isl_basic_set_reset_space(bset
, space
);
4934 return (isl_basic_map
*)bset
;
4937 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4940 space
= isl_set_get_space(set
);
4941 space
= isl_space_from_range(space
);
4942 set
= isl_set_reset_space(set
, space
);
4943 return (struct isl_map
*)set
;
4946 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4948 return isl_map_reverse(isl_map_from_range(set
));
4951 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4952 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4954 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4957 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4958 __isl_take isl_set
*range
)
4960 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4963 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4966 struct isl_set
*set
= NULL
;
4970 map
= isl_map_cow(map
);
4973 map
->dim
= isl_space_as_set_space(map
->dim
);
4976 set
= (struct isl_set
*)map
;
4977 for (i
= 0; i
< map
->n
; ++i
) {
4978 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4988 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4991 struct isl_map
*map
;
4996 isl_die(dim
->ctx
, isl_error_internal
,
4997 "negative number of basic maps", goto error
);
4998 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4999 sizeof(struct isl_map
) +
5000 (n
- 1) * sizeof(struct isl_basic_map
*));
5004 map
->ctx
= dim
->ctx
;
5005 isl_ctx_ref(map
->ctx
);
5013 isl_space_free(dim
);
5017 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5018 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5021 struct isl_map
*map
;
5024 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5028 map
= isl_map_alloc_space(dims
, n
, flags
);
5032 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5034 struct isl_basic_map
*bmap
;
5035 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5036 bmap
= isl_basic_map_set_to_empty(bmap
);
5040 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5042 struct isl_basic_set
*bset
;
5043 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5044 bset
= isl_basic_set_set_to_empty(bset
);
5048 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5050 struct isl_basic_map
*bmap
;
5053 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5054 bmap
= isl_basic_map_set_to_empty(bmap
);
5058 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5060 struct isl_basic_map
*bmap
;
5063 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5064 bmap
= isl_basic_map_set_to_empty(bmap
);
5068 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5070 struct isl_basic_set
*bset
;
5073 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5074 bset
= isl_basic_set_set_to_empty(bset
);
5078 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5080 struct isl_basic_map
*bmap
;
5081 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5082 bmap
= isl_basic_map_finalize(bmap
);
5086 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5088 struct isl_basic_set
*bset
;
5089 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5090 bset
= isl_basic_set_finalize(bset
);
5094 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5097 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5098 isl_basic_map
*bmap
;
5100 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5101 for (i
= 0; i
< total
; ++i
) {
5102 int k
= isl_basic_map_alloc_inequality(bmap
);
5105 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5106 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5110 isl_basic_map_free(bmap
);
5114 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5116 return isl_basic_map_nat_universe(dim
);
5119 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5121 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5124 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5126 return isl_map_nat_universe(dim
);
5129 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5130 __isl_keep isl_basic_map
*model
)
5134 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5137 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5141 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5144 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5145 __isl_keep isl_set
*model
)
5149 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5152 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5154 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5157 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5161 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5164 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5168 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5171 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5173 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5176 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5180 return isl_set_empty(isl_space_copy(model
->dim
));
5183 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5185 struct isl_map
*map
;
5188 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5189 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5193 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5195 struct isl_set
*set
;
5198 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5199 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5203 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5207 return isl_set_universe(isl_space_copy(model
->dim
));
5210 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5213 struct isl_map
*dup
;
5217 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5218 for (i
= 0; i
< map
->n
; ++i
)
5219 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5223 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5224 __isl_take isl_basic_map
*bmap
)
5228 if (isl_basic_map_plain_is_empty(bmap
)) {
5229 isl_basic_map_free(bmap
);
5232 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5233 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5234 map
->p
[map
->n
] = bmap
;
5236 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5242 isl_basic_map_free(bmap
);
5246 void *isl_map_free(struct isl_map
*map
)
5256 isl_ctx_deref(map
->ctx
);
5257 for (i
= 0; i
< map
->n
; ++i
)
5258 isl_basic_map_free(map
->p
[i
]);
5259 isl_space_free(map
->dim
);
5265 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5266 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5270 base
= isl_map_cow(base
);
5274 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5277 for (i
= 0; i
< base
->n
; ++i
) {
5278 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5279 isl_space_copy(base
->dim
), 0, 0, 0);
5289 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5290 unsigned nparam
, unsigned dim
)
5292 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5296 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5297 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5301 bmap
= isl_basic_map_cow(bmap
);
5302 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5303 j
= isl_basic_map_alloc_equality(bmap
);
5306 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5307 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5308 isl_int_set_si(bmap
->eq
[j
][0], value
);
5309 bmap
= isl_basic_map_simplify(bmap
);
5310 return isl_basic_map_finalize(bmap
);
5312 isl_basic_map_free(bmap
);
5316 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5317 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5321 bmap
= isl_basic_map_cow(bmap
);
5322 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5323 j
= isl_basic_map_alloc_equality(bmap
);
5326 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5327 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5328 isl_int_set(bmap
->eq
[j
][0], value
);
5329 bmap
= isl_basic_map_simplify(bmap
);
5330 return isl_basic_map_finalize(bmap
);
5332 isl_basic_map_free(bmap
);
5336 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5337 enum isl_dim_type type
, unsigned pos
, int value
)
5341 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5342 return isl_basic_map_fix_pos_si(bmap
,
5343 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5345 isl_basic_map_free(bmap
);
5349 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5350 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5354 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5355 return isl_basic_map_fix_pos(bmap
,
5356 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5358 isl_basic_map_free(bmap
);
5362 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5363 * to be equal to "v".
5365 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5366 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5370 if (!isl_val_is_int(v
))
5371 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5372 "expecting integer value", goto error
);
5373 if (pos
>= isl_basic_map_dim(bmap
, type
))
5374 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5375 "index out of bounds", goto error
);
5376 pos
+= isl_basic_map_offset(bmap
, type
);
5377 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5381 isl_basic_map_free(bmap
);
5386 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5387 * to be equal to "v".
5389 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5390 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5392 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5395 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5396 enum isl_dim_type type
, unsigned pos
, int value
)
5398 return (struct isl_basic_set
*)
5399 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5403 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5404 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5406 return (struct isl_basic_set
*)
5407 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5411 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5412 unsigned input
, int value
)
5414 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5417 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5418 unsigned dim
, int value
)
5420 return (struct isl_basic_set
*)
5421 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5422 isl_dim_set
, dim
, value
);
5425 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5427 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5434 isl_basic_map_free(map
->p
[i
]);
5435 if (i
!= map
->n
- 1) {
5436 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5437 map
->p
[i
] = map
->p
[map
->n
- 1];
5444 /* Perform "fn" on each basic map of "map", where we may not be holding
5445 * the only reference to "map".
5446 * In particular, "fn" should be a semantics preserving operation
5447 * that we want to apply to all copies of "map". We therefore need
5448 * to be careful not to modify "map" in a way that breaks "map"
5449 * in case anything goes wrong.
5451 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5452 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5454 struct isl_basic_map
*bmap
;
5460 for (i
= map
->n
- 1; i
>= 0; --i
) {
5461 bmap
= isl_basic_map_copy(map
->p
[i
]);
5465 isl_basic_map_free(map
->p
[i
]);
5467 if (remove_if_empty(map
, i
) < 0)
5477 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5478 enum isl_dim_type type
, unsigned pos
, int value
)
5482 map
= isl_map_cow(map
);
5486 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5487 for (i
= map
->n
- 1; i
>= 0; --i
) {
5488 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5489 if (remove_if_empty(map
, i
) < 0)
5492 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5499 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5500 enum isl_dim_type type
, unsigned pos
, int value
)
5502 return (struct isl_set
*)
5503 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5506 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5507 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5511 map
= isl_map_cow(map
);
5515 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5516 for (i
= 0; i
< map
->n
; ++i
) {
5517 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5521 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5528 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5529 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5531 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5534 /* Fix the value of the variable at position "pos" of type "type" of "map"
5535 * to be equal to "v".
5537 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5538 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5542 map
= isl_map_cow(map
);
5546 if (!isl_val_is_int(v
))
5547 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5548 "expecting integer value", goto error
);
5549 if (pos
>= isl_map_dim(map
, type
))
5550 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5551 "index out of bounds", goto error
);
5552 for (i
= map
->n
- 1; i
>= 0; --i
) {
5553 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5555 if (remove_if_empty(map
, i
) < 0)
5558 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5567 /* Fix the value of the variable at position "pos" of type "type" of "set"
5568 * to be equal to "v".
5570 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5571 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5573 return isl_map_fix_val(set
, type
, pos
, v
);
5576 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5577 unsigned input
, int value
)
5579 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5582 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5584 return (struct isl_set
*)
5585 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5588 static __isl_give isl_basic_map
*basic_map_bound_si(
5589 __isl_take isl_basic_map
*bmap
,
5590 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5596 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5597 pos
+= isl_basic_map_offset(bmap
, type
);
5598 bmap
= isl_basic_map_cow(bmap
);
5599 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5600 j
= isl_basic_map_alloc_inequality(bmap
);
5603 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5605 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5606 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5608 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5609 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5611 bmap
= isl_basic_map_simplify(bmap
);
5612 return isl_basic_map_finalize(bmap
);
5614 isl_basic_map_free(bmap
);
5618 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5619 __isl_take isl_basic_map
*bmap
,
5620 enum isl_dim_type type
, unsigned pos
, int value
)
5622 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5625 /* Constrain the values of the given dimension to be no greater than "value".
5627 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5628 __isl_take isl_basic_map
*bmap
,
5629 enum isl_dim_type type
, unsigned pos
, int value
)
5631 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5634 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5635 unsigned dim
, isl_int value
)
5639 bset
= isl_basic_set_cow(bset
);
5640 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5641 j
= isl_basic_set_alloc_inequality(bset
);
5644 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5645 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5646 isl_int_neg(bset
->ineq
[j
][0], value
);
5647 bset
= isl_basic_set_simplify(bset
);
5648 return isl_basic_set_finalize(bset
);
5650 isl_basic_set_free(bset
);
5654 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5655 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5659 map
= isl_map_cow(map
);
5663 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5664 for (i
= 0; i
< map
->n
; ++i
) {
5665 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5666 type
, pos
, value
, upper
);
5670 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5677 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5678 enum isl_dim_type type
, unsigned pos
, int value
)
5680 return map_bound_si(map
, type
, pos
, value
, 0);
5683 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5684 enum isl_dim_type type
, unsigned pos
, int value
)
5686 return map_bound_si(map
, type
, pos
, value
, 1);
5689 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5690 enum isl_dim_type type
, unsigned pos
, int value
)
5692 return (struct isl_set
*)
5693 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5696 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5697 enum isl_dim_type type
, unsigned pos
, int value
)
5699 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5702 /* Bound the given variable of "bmap" from below (or above is "upper"
5703 * is set) to "value".
5705 static __isl_give isl_basic_map
*basic_map_bound(
5706 __isl_take isl_basic_map
*bmap
,
5707 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5713 if (pos
>= isl_basic_map_dim(bmap
, type
))
5714 isl_die(bmap
->ctx
, isl_error_invalid
,
5715 "index out of bounds", goto error
);
5716 pos
+= isl_basic_map_offset(bmap
, type
);
5717 bmap
= isl_basic_map_cow(bmap
);
5718 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5719 j
= isl_basic_map_alloc_inequality(bmap
);
5722 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5724 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5725 isl_int_set(bmap
->ineq
[j
][0], value
);
5727 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5728 isl_int_neg(bmap
->ineq
[j
][0], value
);
5730 bmap
= isl_basic_map_simplify(bmap
);
5731 return isl_basic_map_finalize(bmap
);
5733 isl_basic_map_free(bmap
);
5737 /* Bound the given variable of "map" from below (or above is "upper"
5738 * is set) to "value".
5740 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5741 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5745 map
= isl_map_cow(map
);
5749 if (pos
>= isl_map_dim(map
, type
))
5750 isl_die(map
->ctx
, isl_error_invalid
,
5751 "index out of bounds", goto error
);
5752 for (i
= map
->n
- 1; i
>= 0; --i
) {
5753 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5754 if (remove_if_empty(map
, i
) < 0)
5757 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5764 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5765 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5767 return map_bound(map
, type
, pos
, value
, 0);
5770 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5771 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5773 return map_bound(map
, type
, pos
, value
, 1);
5776 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5777 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5779 return isl_map_lower_bound(set
, type
, pos
, value
);
5782 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5783 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5785 return isl_map_upper_bound(set
, type
, pos
, value
);
5788 /* Force the values of the variable at position "pos" of type "type" of "set"
5789 * to be no smaller than "value".
5791 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5792 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5796 if (!isl_val_is_int(value
))
5797 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5798 "expecting integer value", goto error
);
5799 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5800 isl_val_free(value
);
5803 isl_val_free(value
);
5808 /* Force the values of the variable at position "pos" of type "type" of "set"
5809 * to be no greater than "value".
5811 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5812 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5816 if (!isl_val_is_int(value
))
5817 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5818 "expecting integer value", goto error
);
5819 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5820 isl_val_free(value
);
5823 isl_val_free(value
);
5828 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5833 set
= isl_set_cow(set
);
5837 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5838 for (i
= 0; i
< set
->n
; ++i
) {
5839 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5849 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5853 map
= isl_map_cow(map
);
5857 map
->dim
= isl_space_reverse(map
->dim
);
5860 for (i
= 0; i
< map
->n
; ++i
) {
5861 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5865 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5872 static struct isl_map
*isl_basic_map_partial_lexopt(
5873 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5874 struct isl_set
**empty
, int max
)
5876 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5879 struct isl_map
*isl_basic_map_partial_lexmax(
5880 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5881 struct isl_set
**empty
)
5883 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5886 struct isl_map
*isl_basic_map_partial_lexmin(
5887 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5888 struct isl_set
**empty
)
5890 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5893 struct isl_set
*isl_basic_set_partial_lexmin(
5894 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5895 struct isl_set
**empty
)
5897 return (struct isl_set
*)
5898 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5902 struct isl_set
*isl_basic_set_partial_lexmax(
5903 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5904 struct isl_set
**empty
)
5906 return (struct isl_set
*)
5907 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5911 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5912 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5913 __isl_give isl_set
**empty
)
5915 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5918 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5919 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5920 __isl_give isl_set
**empty
)
5922 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5925 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5926 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5927 __isl_give isl_set
**empty
)
5929 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5932 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5933 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5934 __isl_give isl_set
**empty
)
5936 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5939 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5940 __isl_take isl_basic_map
*bmap
, int max
)
5942 isl_basic_set
*dom
= NULL
;
5943 isl_space
*dom_space
;
5947 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5948 dom
= isl_basic_set_universe(dom_space
);
5949 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5951 isl_basic_map_free(bmap
);
5955 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5956 __isl_take isl_basic_map
*bmap
)
5958 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5962 #define TYPE isl_pw_multi_aff
5964 #define SUFFIX _pw_multi_aff
5966 #define EMPTY isl_pw_multi_aff_empty
5968 #define ADD isl_pw_multi_aff_union_add
5969 #include "isl_map_lexopt_templ.c"
5971 /* Given a map "map", compute the lexicographically minimal
5972 * (or maximal) image element for each domain element in dom,
5973 * in the form of an isl_pw_multi_aff.
5974 * Set *empty to those elements in dom that do not have an image element.
5976 * We first compute the lexicographically minimal or maximal element
5977 * in the first basic map. This results in a partial solution "res"
5978 * and a subset "todo" of dom that still need to be handled.
5979 * We then consider each of the remaining maps in "map" and successively
5980 * update both "res" and "todo".
5982 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
5983 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5984 __isl_give isl_set
**empty
, int max
)
5987 isl_pw_multi_aff
*res
;
5993 if (isl_map_plain_is_empty(map
)) {
5998 return isl_pw_multi_aff_from_map(map
);
6001 res
= basic_map_partial_lexopt_pw_multi_aff(
6002 isl_basic_map_copy(map
->p
[0]),
6003 isl_set_copy(dom
), &todo
, max
);
6005 for (i
= 1; i
< map
->n
; ++i
) {
6006 isl_pw_multi_aff
*res_i
;
6009 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6010 isl_basic_map_copy(map
->p
[i
]),
6011 isl_set_copy(dom
), &todo_i
, max
);
6014 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6016 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6018 todo
= isl_set_intersect(todo
, todo_i
);
6039 #define TYPE isl_map
6043 #define EMPTY isl_map_empty
6045 #define ADD isl_map_union_disjoint
6046 #include "isl_map_lexopt_templ.c"
6048 /* Given a map "map", compute the lexicographically minimal
6049 * (or maximal) image element for each domain element in dom.
6050 * Set *empty to those elements in dom that do not have an image element.
6052 * We first compute the lexicographically minimal or maximal element
6053 * in the first basic map. This results in a partial solution "res"
6054 * and a subset "todo" of dom that still need to be handled.
6055 * We then consider each of the remaining maps in "map" and successively
6056 * update both "res" and "todo".
6058 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6059 * Assume we are computing the lexicographical maximum.
6060 * We first compute the lexicographically maximal element in basic map i.
6061 * This results in a partial solution res_i and a subset todo_i.
6062 * Then we combine these results with those obtain for the first k basic maps
6063 * to obtain a result that is valid for the first k+1 basic maps.
6064 * In particular, the set where there is no solution is the set where
6065 * there is no solution for the first k basic maps and also no solution
6066 * for the ith basic map, i.e.,
6068 * todo^i = todo^k * todo_i
6070 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6071 * solutions, arbitrarily breaking ties in favor of res^k.
6072 * That is, when res^k(a) >= res_i(a), we pick res^k and
6073 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6074 * the lexicographic order.)
6075 * In practice, we compute
6077 * res^k * (res_i . "<=")
6081 * res_i * (res^k . "<")
6083 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6084 * where only one of res^k and res_i provides a solution and we simply pick
6091 * Note that we only compute these intersections when dom(res^k) intersects
6092 * dom(res_i). Otherwise, the only effect of these intersections is to
6093 * potentially break up res^k and res_i into smaller pieces.
6094 * We want to avoid such splintering as much as possible.
6095 * In fact, an earlier implementation of this function would look for
6096 * better results in the domain of res^k and for extra results in todo^k,
6097 * but this would always result in a splintering according to todo^k,
6098 * even when the domain of basic map i is disjoint from the domains of
6099 * the previous basic maps.
6101 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6102 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6103 __isl_give isl_set
**empty
, int max
)
6106 struct isl_map
*res
;
6107 struct isl_set
*todo
;
6112 if (isl_map_plain_is_empty(map
)) {
6120 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6121 isl_set_copy(dom
), &todo
, max
);
6123 for (i
= 1; i
< map
->n
; ++i
) {
6127 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6129 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6130 isl_set_copy(dom
), &todo_i
, max
);
6133 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6134 le
= isl_map_lex_le(dim
);
6136 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6137 le
= isl_map_lex_ge(dim
);
6139 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6140 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6141 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6142 le
= isl_map_intersect(le
, isl_map_copy(res
));
6144 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6145 res
= isl_map_intersect_domain(res
,
6146 isl_set_copy(todo_i
));
6147 res_i
= isl_map_intersect_domain(res_i
,
6148 isl_set_copy(todo
));
6151 res
= isl_map_union_disjoint(res
, res_i
);
6152 res
= isl_map_union_disjoint(res
, lt
);
6153 res
= isl_map_union_disjoint(res
, le
);
6155 todo
= isl_set_intersect(todo
, todo_i
);
6175 __isl_give isl_map
*isl_map_partial_lexmax(
6176 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6177 __isl_give isl_set
**empty
)
6179 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6182 __isl_give isl_map
*isl_map_partial_lexmin(
6183 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6184 __isl_give isl_set
**empty
)
6186 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6189 __isl_give isl_set
*isl_set_partial_lexmin(
6190 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6191 __isl_give isl_set
**empty
)
6193 return (struct isl_set
*)
6194 isl_map_partial_lexmin((struct isl_map
*)set
,
6198 __isl_give isl_set
*isl_set_partial_lexmax(
6199 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6200 __isl_give isl_set
**empty
)
6202 return (struct isl_set
*)
6203 isl_map_partial_lexmax((struct isl_map
*)set
,
6207 /* Compute the lexicographic minimum (or maximum if "max" is set)
6208 * of "bmap" over its domain.
6210 * Since we are not interested in the part of the domain space where
6211 * there is no solution, we initialize the domain to those constraints
6212 * of "bmap" that only involve the parameters and the input dimensions.
6213 * This relieves the parametric programming engine from detecting those
6214 * inequalities and transferring them to the context. More importantly,
6215 * it ensures that those inequalities are transferred first and not
6216 * intermixed with inequalities that actually split the domain.
6218 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6222 isl_basic_map
*copy
;
6225 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6226 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6227 copy
= isl_basic_map_copy(bmap
);
6228 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6229 isl_dim_div
, 0, n_div
);
6230 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6231 isl_dim_out
, 0, n_out
);
6232 dom
= isl_basic_map_domain(copy
);
6233 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6236 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6238 return isl_basic_map_lexopt(bmap
, 0);
6241 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6243 return isl_basic_map_lexopt(bmap
, 1);
6246 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6248 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6251 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6253 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6256 /* Extract the first and only affine expression from list
6257 * and then add it to *pwaff with the given dom.
6258 * This domain is known to be disjoint from other domains
6259 * because of the way isl_basic_map_foreach_lexmax works.
6261 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6262 __isl_take isl_aff_list
*list
, void *user
)
6264 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6266 isl_pw_aff
**pwaff
= user
;
6267 isl_pw_aff
*pwaff_i
;
6271 if (isl_aff_list_n_aff(list
) != 1)
6272 isl_die(ctx
, isl_error_internal
,
6273 "expecting single element list", goto error
);
6275 aff
= isl_aff_list_get_aff(list
, 0);
6276 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6278 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6280 isl_aff_list_free(list
);
6284 isl_basic_set_free(dom
);
6285 isl_aff_list_free(list
);
6289 /* Given a basic map with one output dimension, compute the minimum or
6290 * maximum of that dimension as an isl_pw_aff.
6292 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6293 * call update_dim_opt on each leaf of the result.
6295 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6298 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6302 dim
= isl_space_from_domain(isl_space_domain(dim
));
6303 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6304 pwaff
= isl_pw_aff_empty(dim
);
6306 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6308 return isl_pw_aff_free(pwaff
);
6313 /* Compute the minimum or maximum of the given output dimension
6314 * as a function of the parameters and the input dimensions,
6315 * but independently of the other output dimensions.
6317 * We first project out the other output dimension and then compute
6318 * the "lexicographic" maximum in each basic map, combining the results
6319 * using isl_pw_aff_union_max.
6321 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6328 n_out
= isl_map_dim(map
, isl_dim_out
);
6329 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6330 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6335 isl_space
*dim
= isl_map_get_space(map
);
6336 dim
= isl_space_domain(isl_space_from_range(dim
));
6338 return isl_pw_aff_empty(dim
);
6341 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6342 for (i
= 1; i
< map
->n
; ++i
) {
6343 isl_pw_aff
*pwaff_i
;
6345 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6346 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6354 /* Compute the maximum of the given output dimension as a function of the
6355 * parameters and input dimensions, but independently of
6356 * the other output dimensions.
6358 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6360 return map_dim_opt(map
, pos
, 1);
6363 /* Compute the minimum or maximum of the given set dimension
6364 * as a function of the parameters,
6365 * but independently of the other set dimensions.
6367 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6370 return map_dim_opt(set
, pos
, max
);
6373 /* Compute the maximum of the given set dimension as a function of the
6374 * parameters, but independently of the other set dimensions.
6376 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6378 return set_dim_opt(set
, pos
, 1);
6381 /* Compute the minimum of the given set dimension as a function of the
6382 * parameters, but independently of the other set dimensions.
6384 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6386 return set_dim_opt(set
, pos
, 0);
6389 /* Apply a preimage specified by "mat" on the parameters of "bset".
6390 * bset is assumed to have only parameters and divs.
6392 static struct isl_basic_set
*basic_set_parameter_preimage(
6393 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6400 bset
->dim
= isl_space_cow(bset
->dim
);
6404 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6406 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6408 bset
->dim
->nparam
= 0;
6409 bset
->dim
->n_out
= nparam
;
6410 bset
= isl_basic_set_preimage(bset
, mat
);
6412 bset
->dim
->nparam
= bset
->dim
->n_out
;
6413 bset
->dim
->n_out
= 0;
6418 isl_basic_set_free(bset
);
6422 /* Apply a preimage specified by "mat" on the parameters of "set".
6423 * set is assumed to have only parameters and divs.
6425 static struct isl_set
*set_parameter_preimage(
6426 struct isl_set
*set
, struct isl_mat
*mat
)
6428 isl_space
*dim
= NULL
;
6434 dim
= isl_space_copy(set
->dim
);
6435 dim
= isl_space_cow(dim
);
6439 nparam
= isl_set_dim(set
, isl_dim_param
);
6441 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6444 dim
->n_out
= nparam
;
6445 isl_set_reset_space(set
, dim
);
6446 set
= isl_set_preimage(set
, mat
);
6449 dim
= isl_space_copy(set
->dim
);
6450 dim
= isl_space_cow(dim
);
6453 dim
->nparam
= dim
->n_out
;
6455 isl_set_reset_space(set
, dim
);
6458 isl_space_free(dim
);
6465 /* Intersect the basic set "bset" with the affine space specified by the
6466 * equalities in "eq".
6468 static struct isl_basic_set
*basic_set_append_equalities(
6469 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6477 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6482 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6483 for (i
= 0; i
< eq
->n_row
; ++i
) {
6484 k
= isl_basic_set_alloc_equality(bset
);
6487 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6488 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6492 bset
= isl_basic_set_gauss(bset
, NULL
);
6493 bset
= isl_basic_set_finalize(bset
);
6498 isl_basic_set_free(bset
);
6502 /* Intersect the set "set" with the affine space specified by the
6503 * equalities in "eq".
6505 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6513 for (i
= 0; i
< set
->n
; ++i
) {
6514 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6527 /* Given a basic set "bset" that only involves parameters and existentially
6528 * quantified variables, return the index of the first equality
6529 * that only involves parameters. If there is no such equality then
6530 * return bset->n_eq.
6532 * This function assumes that isl_basic_set_gauss has been called on "bset".
6534 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6537 unsigned nparam
, n_div
;
6542 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6543 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6545 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6546 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6553 /* Compute an explicit representation for the existentially quantified
6554 * variables in "bset" by computing the "minimal value" of the set
6555 * variables. Since there are no set variables, the computation of
6556 * the minimal value essentially computes an explicit representation
6557 * of the non-empty part(s) of "bset".
6559 * The input only involves parameters and existentially quantified variables.
6560 * All equalities among parameters have been removed.
6562 * Since the existentially quantified variables in the result are in general
6563 * going to be different from those in the input, we first replace
6564 * them by the minimal number of variables based on their equalities.
6565 * This should simplify the parametric integer programming.
6567 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6569 isl_morph
*morph1
, *morph2
;
6575 if (bset
->n_eq
== 0)
6576 return isl_basic_set_lexmin(bset
);
6578 morph1
= isl_basic_set_parameter_compression(bset
);
6579 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6580 bset
= isl_basic_set_lift(bset
);
6581 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6582 bset
= isl_morph_basic_set(morph2
, bset
);
6583 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6584 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6586 set
= isl_basic_set_lexmin(bset
);
6588 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6593 /* Project the given basic set onto its parameter domain, possibly introducing
6594 * new, explicit, existential variables in the constraints.
6595 * The input has parameters and (possibly implicit) existential variables.
6596 * The output has the same parameters, but only
6597 * explicit existentially quantified variables.
6599 * The actual projection is performed by pip, but pip doesn't seem
6600 * to like equalities very much, so we first remove the equalities
6601 * among the parameters by performing a variable compression on
6602 * the parameters. Afterward, an inverse transformation is performed
6603 * and the equalities among the parameters are inserted back in.
6605 * The variable compression on the parameters may uncover additional
6606 * equalities that were only implicit before. We therefore check
6607 * if there are any new parameter equalities in the result and
6608 * if so recurse. The removal of parameter equalities is required
6609 * for the parameter compression performed by base_compute_divs.
6611 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6615 struct isl_mat
*T
, *T2
;
6616 struct isl_set
*set
;
6619 bset
= isl_basic_set_cow(bset
);
6623 if (bset
->n_eq
== 0)
6624 return base_compute_divs(bset
);
6626 bset
= isl_basic_set_gauss(bset
, NULL
);
6629 if (isl_basic_set_plain_is_empty(bset
))
6630 return isl_set_from_basic_set(bset
);
6632 i
= first_parameter_equality(bset
);
6633 if (i
== bset
->n_eq
)
6634 return base_compute_divs(bset
);
6636 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6637 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6639 eq
= isl_mat_cow(eq
);
6640 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6641 if (T
&& T
->n_col
== 0) {
6645 bset
= isl_basic_set_set_to_empty(bset
);
6646 return isl_set_from_basic_set(bset
);
6648 bset
= basic_set_parameter_preimage(bset
, T
);
6650 i
= first_parameter_equality(bset
);
6653 else if (i
== bset
->n_eq
)
6654 set
= base_compute_divs(bset
);
6656 set
= parameter_compute_divs(bset
);
6657 set
= set_parameter_preimage(set
, T2
);
6658 set
= set_append_equalities(set
, eq
);
6662 /* Insert the divs from "ls" before those of "bmap".
6664 * The number of columns is not changed, which means that the last
6665 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6666 * The caller is responsible for removing the same number of dimensions
6667 * from the space of "bmap".
6669 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6670 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6676 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6680 old_n_div
= bmap
->n_div
;
6681 bmap
= insert_div_rows(bmap
, n_div
);
6685 for (i
= 0; i
< n_div
; ++i
) {
6686 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6687 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6693 /* Replace the space of "bmap" by the space and divs of "ls".
6695 * If "ls" has any divs, then we simplify the result since we may
6696 * have discovered some additional equalities that could simplify
6697 * the div expressions.
6699 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6700 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6704 bmap
= isl_basic_map_cow(bmap
);
6708 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6709 bmap
= insert_divs_from_local_space(bmap
, ls
);
6713 isl_space_free(bmap
->dim
);
6714 bmap
->dim
= isl_local_space_get_space(ls
);
6718 isl_local_space_free(ls
);
6720 bmap
= isl_basic_map_simplify(bmap
);
6721 bmap
= isl_basic_map_finalize(bmap
);
6724 isl_basic_map_free(bmap
);
6725 isl_local_space_free(ls
);
6729 /* Replace the space of "map" by the space and divs of "ls".
6731 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6732 __isl_take isl_local_space
*ls
)
6736 map
= isl_map_cow(map
);
6740 for (i
= 0; i
< map
->n
; ++i
) {
6741 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6742 isl_local_space_copy(ls
));
6746 isl_space_free(map
->dim
);
6747 map
->dim
= isl_local_space_get_space(ls
);
6751 isl_local_space_free(ls
);
6754 isl_local_space_free(ls
);
6759 /* Compute an explicit representation for the existentially
6760 * quantified variables for which do not know any explicit representation yet.
6762 * We first sort the existentially quantified variables so that the
6763 * existentially quantified variables for which we already have an explicit
6764 * representation are placed before those for which we do not.
6765 * The input dimensions, the output dimensions and the existentially
6766 * quantified variables for which we already have an explicit
6767 * representation are then turned into parameters.
6768 * compute_divs returns a map with the same parameters and
6769 * no input or output dimensions and the dimension specification
6770 * is reset to that of the input, including the existentially quantified
6771 * variables for which we already had an explicit representation.
6773 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6775 struct isl_basic_set
*bset
;
6776 struct isl_set
*set
;
6777 struct isl_map
*map
;
6779 isl_local_space
*ls
;
6786 bmap
= isl_basic_map_sort_divs(bmap
);
6787 bmap
= isl_basic_map_cow(bmap
);
6791 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6792 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6795 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6796 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6797 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6798 dim
= isl_space_set_alloc(bmap
->ctx
,
6799 nparam
+ n_in
+ n_out
+ n_known
, 0);
6803 ls
= isl_basic_map_get_local_space(bmap
);
6804 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6805 n_known
, bmap
->n_div
- n_known
);
6807 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6808 swap_div(bmap
, i
- n_known
, i
);
6809 bmap
->n_div
-= n_known
;
6810 bmap
->extra
-= n_known
;
6812 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6813 bset
= (struct isl_basic_set
*)bmap
;
6815 set
= parameter_compute_divs(bset
);
6816 map
= (struct isl_map
*)set
;
6817 map
= replace_space_by_local_space(map
, ls
);
6821 isl_basic_map_free(bmap
);
6825 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6833 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6834 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6835 if (isl_int_is_zero(bmap
->div
[i
][0]))
6837 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6843 static int map_divs_known(__isl_keep isl_map
*map
)
6850 for (i
= 0; i
< map
->n
; ++i
) {
6851 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6859 /* If bmap contains any unknown divs, then compute explicit
6860 * expressions for them. However, this computation may be
6861 * quite expensive, so first try to remove divs that aren't
6864 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6867 struct isl_map
*map
;
6869 known
= isl_basic_map_divs_known(bmap
);
6873 return isl_map_from_basic_map(bmap
);
6875 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6877 known
= isl_basic_map_divs_known(bmap
);
6881 return isl_map_from_basic_map(bmap
);
6883 map
= compute_divs(bmap
);
6886 isl_basic_map_free(bmap
);
6890 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6894 struct isl_map
*res
;
6901 known
= map_divs_known(map
);
6909 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6910 for (i
= 1 ; i
< map
->n
; ++i
) {
6912 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6913 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6914 res
= isl_map_union_disjoint(res
, r2
);
6916 res
= isl_map_union(res
, r2
);
6923 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6925 return (struct isl_set
*)
6926 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6929 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6931 return (struct isl_set
*)
6932 isl_map_compute_divs((struct isl_map
*)set
);
6935 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6938 struct isl_set
*set
;
6943 map
= isl_map_cow(map
);
6947 set
= (struct isl_set
*)map
;
6948 set
->dim
= isl_space_domain(set
->dim
);
6951 for (i
= 0; i
< map
->n
; ++i
) {
6952 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6956 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6957 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6964 /* Return the union of "map1" and "map2", where we assume for now that
6965 * "map1" and "map2" are disjoint. Note that the basic maps inside
6966 * "map1" or "map2" may not be disjoint from each other.
6967 * Also note that this function is also called from isl_map_union,
6968 * which takes care of handling the situation where "map1" and "map2"
6969 * may not be disjoint.
6971 * If one of the inputs is empty, we can simply return the other input.
6972 * Similarly, if one of the inputs is universal, then it is equal to the union.
6974 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6975 __isl_take isl_map
*map2
)
6979 struct isl_map
*map
= NULL
;
6994 is_universe
= isl_map_plain_is_universe(map1
);
6995 if (is_universe
< 0)
7002 is_universe
= isl_map_plain_is_universe(map2
);
7003 if (is_universe
< 0)
7010 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
7012 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7013 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7014 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7016 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7017 map1
->n
+ map2
->n
, flags
);
7020 for (i
= 0; i
< map1
->n
; ++i
) {
7021 map
= isl_map_add_basic_map(map
,
7022 isl_basic_map_copy(map1
->p
[i
]));
7026 for (i
= 0; i
< map2
->n
; ++i
) {
7027 map
= isl_map_add_basic_map(map
,
7028 isl_basic_map_copy(map2
->p
[i
]));
7042 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7043 __isl_take isl_map
*map2
)
7045 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7048 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
7050 map1
= isl_map_union_disjoint(map1
, map2
);
7054 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7058 struct isl_set
*isl_set_union_disjoint(
7059 struct isl_set
*set1
, struct isl_set
*set2
)
7061 return (struct isl_set
*)
7062 isl_map_union_disjoint(
7063 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7066 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7068 return (struct isl_set
*)
7069 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7072 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7075 * "map" and "set" are assumed to be compatible and non-NULL.
7077 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7078 __isl_take isl_set
*set
,
7079 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7080 __isl_take isl_basic_set
*bset
))
7083 struct isl_map
*result
;
7086 if (isl_set_plain_is_universe(set
)) {
7091 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7092 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7093 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7095 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7096 map
->n
* set
->n
, flags
);
7097 for (i
= 0; result
&& i
< map
->n
; ++i
)
7098 for (j
= 0; j
< set
->n
; ++j
) {
7099 result
= isl_map_add_basic_map(result
,
7100 fn(isl_basic_map_copy(map
->p
[i
]),
7101 isl_basic_set_copy(set
->p
[j
])));
7111 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7112 __isl_take isl_set
*set
)
7117 if (!isl_map_compatible_range(map
, set
))
7118 isl_die(set
->ctx
, isl_error_invalid
,
7119 "incompatible spaces", goto error
);
7121 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7128 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7129 __isl_take isl_set
*set
)
7131 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7134 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7135 __isl_take isl_set
*set
)
7140 if (!isl_map_compatible_domain(map
, set
))
7141 isl_die(set
->ctx
, isl_error_invalid
,
7142 "incompatible spaces", goto error
);
7144 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7151 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7152 __isl_take isl_set
*set
)
7154 return isl_map_align_params_map_map_and(map
, set
,
7155 &map_intersect_domain
);
7158 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7159 __isl_take isl_map
*map2
)
7163 map1
= isl_map_reverse(map1
);
7164 map1
= isl_map_apply_range(map1
, map2
);
7165 return isl_map_reverse(map1
);
7172 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7173 __isl_take isl_map
*map2
)
7175 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7178 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7179 __isl_take isl_map
*map2
)
7181 isl_space
*dim_result
;
7182 struct isl_map
*result
;
7188 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7189 isl_space_copy(map2
->dim
));
7191 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7194 for (i
= 0; i
< map1
->n
; ++i
)
7195 for (j
= 0; j
< map2
->n
; ++j
) {
7196 result
= isl_map_add_basic_map(result
,
7197 isl_basic_map_apply_range(
7198 isl_basic_map_copy(map1
->p
[i
]),
7199 isl_basic_map_copy(map2
->p
[j
])));
7205 if (result
&& result
->n
<= 1)
7206 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7214 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7215 __isl_take isl_map
*map2
)
7217 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7221 * returns range - domain
7223 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7225 isl_space
*dims
, *target_dim
;
7226 struct isl_basic_set
*bset
;
7233 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7234 bmap
->dim
, isl_dim_out
),
7236 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7237 dim
= isl_basic_map_n_in(bmap
);
7238 nparam
= isl_basic_map_n_param(bmap
);
7239 bset
= isl_basic_set_from_basic_map(bmap
);
7240 bset
= isl_basic_set_cow(bset
);
7241 dims
= isl_basic_set_get_space(bset
);
7242 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7243 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7244 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7245 for (i
= 0; i
< dim
; ++i
) {
7246 int j
= isl_basic_map_alloc_equality(
7247 (struct isl_basic_map
*)bset
);
7249 bset
= isl_basic_set_free(bset
);
7252 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7253 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7254 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7255 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7257 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7258 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7261 isl_basic_map_free(bmap
);
7266 * returns range - domain
7268 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7272 struct isl_set
*result
;
7277 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7278 map
->dim
, isl_dim_out
),
7280 dim
= isl_map_get_space(map
);
7281 dim
= isl_space_domain(dim
);
7282 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7285 for (i
= 0; i
< map
->n
; ++i
)
7286 result
= isl_set_add_basic_set(result
,
7287 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7296 * returns [domain -> range] -> range - domain
7298 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7299 __isl_take isl_basic_map
*bmap
)
7303 isl_basic_map
*domain
;
7307 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7308 isl_die(bmap
->ctx
, isl_error_invalid
,
7309 "domain and range don't match", goto error
);
7311 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7312 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7314 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7315 domain
= isl_basic_map_universe(dim
);
7317 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7318 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7319 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7321 total
= isl_basic_map_total_dim(bmap
);
7323 for (i
= 0; i
< n
; ++i
) {
7324 k
= isl_basic_map_alloc_equality(bmap
);
7327 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7328 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7329 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7330 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7333 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7334 return isl_basic_map_finalize(bmap
);
7336 isl_basic_map_free(bmap
);
7341 * returns [domain -> range] -> range - domain
7343 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7346 isl_space
*domain_dim
;
7351 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7352 isl_die(map
->ctx
, isl_error_invalid
,
7353 "domain and range don't match", goto error
);
7355 map
= isl_map_cow(map
);
7359 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7360 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7361 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7364 for (i
= 0; i
< map
->n
; ++i
) {
7365 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7369 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7376 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7378 struct isl_basic_map
*bmap
;
7386 nparam
= dims
->nparam
;
7388 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7392 for (i
= 0; i
< dim
; ++i
) {
7393 int j
= isl_basic_map_alloc_equality(bmap
);
7396 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7397 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7398 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7400 return isl_basic_map_finalize(bmap
);
7402 isl_basic_map_free(bmap
);
7406 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7410 if (dim
->n_in
!= dim
->n_out
)
7411 isl_die(dim
->ctx
, isl_error_invalid
,
7412 "number of input and output dimensions needs to be "
7413 "the same", goto error
);
7414 return basic_map_identity(dim
);
7416 isl_space_free(dim
);
7420 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7422 if (!model
|| !model
->dim
)
7424 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7427 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7429 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7432 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7434 if (!model
|| !model
->dim
)
7436 return isl_map_identity(isl_space_copy(model
->dim
));
7439 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7441 if (!model
|| !model
->dim
)
7443 return isl_map_identity(isl_space_copy(model
->dim
));
7446 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7448 isl_space
*dim
= isl_set_get_space(set
);
7450 id
= isl_map_identity(isl_space_map_from_set(dim
));
7451 return isl_map_intersect_range(id
, set
);
7454 /* Construct a basic set with all set dimensions having only non-negative
7457 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7458 __isl_take isl_space
*space
)
7463 struct isl_basic_set
*bset
;
7467 nparam
= space
->nparam
;
7469 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7472 for (i
= 0; i
< dim
; ++i
) {
7473 int k
= isl_basic_set_alloc_inequality(bset
);
7476 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7477 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7481 isl_basic_set_free(bset
);
7485 /* Construct the half-space x_pos >= 0.
7487 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7491 isl_basic_set
*nonneg
;
7493 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7494 k
= isl_basic_set_alloc_inequality(nonneg
);
7497 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7498 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7500 return isl_basic_set_finalize(nonneg
);
7502 isl_basic_set_free(nonneg
);
7506 /* Construct the half-space x_pos <= -1.
7508 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7513 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7514 k
= isl_basic_set_alloc_inequality(neg
);
7517 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7518 isl_int_set_si(neg
->ineq
[k
][0], -1);
7519 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7521 return isl_basic_set_finalize(neg
);
7523 isl_basic_set_free(neg
);
7527 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7528 enum isl_dim_type type
, unsigned first
, unsigned n
)
7531 isl_basic_set
*nonneg
;
7539 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7541 for (i
= 0; i
< n
; ++i
) {
7542 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7543 pos(set
->dim
, type
) + first
+ i
);
7544 neg
= neg_halfspace(isl_set_get_space(set
),
7545 pos(set
->dim
, type
) + first
+ i
);
7547 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7556 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7557 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7564 if (isl_set_plain_is_empty(set
)) {
7569 return fn(set
, signs
, user
);
7572 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7574 half
= isl_set_intersect(half
, isl_set_copy(set
));
7575 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7579 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7581 half
= isl_set_intersect(half
, set
);
7582 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7588 /* Call "fn" on the intersections of "set" with each of the orthants
7589 * (except for obviously empty intersections). The orthant is identified
7590 * by the signs array, with each entry having value 1 or -1 according
7591 * to the sign of the corresponding variable.
7593 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7594 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7604 if (isl_set_plain_is_empty(set
))
7607 nparam
= isl_set_dim(set
, isl_dim_param
);
7608 nvar
= isl_set_dim(set
, isl_dim_set
);
7610 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7612 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7620 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7622 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7625 int isl_basic_map_is_subset(
7626 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7629 struct isl_map
*map1
;
7630 struct isl_map
*map2
;
7632 if (!bmap1
|| !bmap2
)
7635 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7636 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7638 is_subset
= isl_map_is_subset(map1
, map2
);
7646 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7647 __isl_keep isl_basic_set
*bset2
)
7649 return isl_basic_map_is_subset(bset1
, bset2
);
7652 int isl_basic_map_is_equal(
7653 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7657 if (!bmap1
|| !bmap2
)
7659 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7662 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7666 int isl_basic_set_is_equal(
7667 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7669 return isl_basic_map_is_equal(
7670 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7673 int isl_map_is_empty(struct isl_map
*map
)
7680 for (i
= 0; i
< map
->n
; ++i
) {
7681 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7690 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7692 return map
? map
->n
== 0 : -1;
7695 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7697 return isl_map_plain_is_empty(map
);
7700 int isl_set_plain_is_empty(struct isl_set
*set
)
7702 return set
? set
->n
== 0 : -1;
7705 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7707 return isl_set_plain_is_empty(set
);
7710 int isl_set_is_empty(struct isl_set
*set
)
7712 return isl_map_is_empty((struct isl_map
*)set
);
7715 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7720 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7723 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7728 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7731 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7737 is_subset
= isl_map_is_subset(map1
, map2
);
7740 is_subset
= isl_map_is_subset(map2
, map1
);
7744 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7746 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7749 int isl_basic_map_is_strict_subset(
7750 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7754 if (!bmap1
|| !bmap2
)
7756 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7759 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7760 if (is_subset
== -1)
7765 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7771 is_subset
= isl_map_is_subset(map1
, map2
);
7774 is_subset
= isl_map_is_subset(map2
, map1
);
7775 if (is_subset
== -1)
7780 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7782 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7785 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7789 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7792 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7796 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7799 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7806 for (i
= 0; i
< map
->n
; ++i
) {
7807 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7815 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7817 return isl_map_plain_is_universe((isl_map
*) set
);
7820 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7822 return isl_set_plain_is_universe(set
);
7825 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7827 struct isl_basic_set
*bset
= NULL
;
7828 struct isl_vec
*sample
= NULL
;
7835 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7838 if (isl_basic_map_is_universe(bmap
))
7841 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7842 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7843 copy
= isl_basic_map_remove_redundancies(copy
);
7844 empty
= isl_basic_map_plain_is_empty(copy
);
7845 isl_basic_map_free(copy
);
7849 total
= 1 + isl_basic_map_total_dim(bmap
);
7850 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7851 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7857 isl_vec_free(bmap
->sample
);
7858 bmap
->sample
= NULL
;
7859 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7862 sample
= isl_basic_set_sample_vec(bset
);
7865 empty
= sample
->size
== 0;
7866 isl_vec_free(bmap
->sample
);
7867 bmap
->sample
= sample
;
7869 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7874 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7878 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7881 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7883 return isl_basic_map_plain_is_empty(bmap
);
7886 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7890 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7893 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7895 return isl_basic_set_plain_is_empty(bset
);
7898 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7900 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7903 struct isl_map
*isl_basic_map_union(
7904 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7906 struct isl_map
*map
;
7907 if (!bmap1
|| !bmap2
)
7910 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7912 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7915 map
= isl_map_add_basic_map(map
, bmap1
);
7916 map
= isl_map_add_basic_map(map
, bmap2
);
7919 isl_basic_map_free(bmap1
);
7920 isl_basic_map_free(bmap2
);
7924 struct isl_set
*isl_basic_set_union(
7925 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7927 return (struct isl_set
*)isl_basic_map_union(
7928 (struct isl_basic_map
*)bset1
,
7929 (struct isl_basic_map
*)bset2
);
7932 /* Order divs such that any div only depends on previous divs */
7933 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7941 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7943 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7945 if (isl_int_is_zero(bmap
->div
[i
][0]))
7947 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7951 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7957 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7959 return (struct isl_basic_set
*)
7960 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7963 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7970 for (i
= 0; i
< map
->n
; ++i
) {
7971 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7982 /* Apply the expansion computed by isl_merge_divs.
7983 * The expansion itself is given by "exp" while the resulting
7984 * list of divs is given by "div".
7986 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7987 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7992 bset
= isl_basic_set_cow(bset
);
7996 if (div
->n_row
< bset
->n_div
)
7997 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7998 "not an expansion", goto error
);
8000 n_div
= bset
->n_div
;
8001 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8002 div
->n_row
- n_div
, 0,
8003 2 * (div
->n_row
- n_div
));
8005 for (i
= n_div
; i
< div
->n_row
; ++i
)
8006 if (isl_basic_set_alloc_div(bset
) < 0)
8010 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8011 if (j
>= 0 && exp
[j
] == i
) {
8013 isl_basic_map_swap_div(bset
, i
, j
);
8016 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8017 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8025 isl_basic_set_free(bset
);
8030 /* Look for a div in dst that corresponds to the div "div" in src.
8031 * The divs before "div" in src and dst are assumed to be the same.
8033 * Returns -1 if no corresponding div was found and the position
8034 * of the corresponding div in dst otherwise.
8036 static int find_div(struct isl_basic_map
*dst
,
8037 struct isl_basic_map
*src
, unsigned div
)
8041 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8043 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8044 for (i
= div
; i
< dst
->n_div
; ++i
)
8045 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8046 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8047 dst
->n_div
- div
) == -1)
8052 struct isl_basic_map
*isl_basic_map_align_divs(
8053 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8061 if (src
->n_div
== 0)
8064 for (i
= 0; i
< src
->n_div
; ++i
)
8065 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8067 src
= isl_basic_map_order_divs(src
);
8068 dst
= isl_basic_map_cow(dst
);
8071 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8072 src
->n_div
, 0, 2 * src
->n_div
);
8075 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8076 for (i
= 0; i
< src
->n_div
; ++i
) {
8077 int j
= find_div(dst
, src
, i
);
8079 j
= isl_basic_map_alloc_div(dst
);
8082 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8083 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8084 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8088 isl_basic_map_swap_div(dst
, i
, j
);
8092 isl_basic_map_free(dst
);
8096 struct isl_basic_set
*isl_basic_set_align_divs(
8097 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8099 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8100 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8103 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8111 map
= isl_map_compute_divs(map
);
8112 map
= isl_map_cow(map
);
8116 for (i
= 1; i
< map
->n
; ++i
)
8117 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8118 for (i
= 1; i
< map
->n
; ++i
) {
8119 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8121 return isl_map_free(map
);
8124 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8128 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8130 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8133 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8134 __isl_take isl_map
*map
)
8138 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8139 map
= isl_map_intersect_domain(map
, set
);
8140 set
= isl_map_range(map
);
8148 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8149 __isl_take isl_map
*map
)
8151 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8154 /* There is no need to cow as removing empty parts doesn't change
8155 * the meaning of the set.
8157 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8164 for (i
= map
->n
- 1; i
>= 0; --i
)
8165 remove_if_empty(map
, i
);
8170 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8172 return (struct isl_set
*)
8173 isl_map_remove_empty_parts((struct isl_map
*)set
);
8176 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8178 struct isl_basic_map
*bmap
;
8179 if (!map
|| map
->n
== 0)
8181 bmap
= map
->p
[map
->n
-1];
8182 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8183 return isl_basic_map_copy(bmap
);
8186 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8188 return (struct isl_basic_set
*)
8189 isl_map_copy_basic_map((struct isl_map
*)set
);
8192 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8193 __isl_keep isl_basic_map
*bmap
)
8199 for (i
= map
->n
-1; i
>= 0; --i
) {
8200 if (map
->p
[i
] != bmap
)
8202 map
= isl_map_cow(map
);
8205 isl_basic_map_free(map
->p
[i
]);
8206 if (i
!= map
->n
-1) {
8207 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8208 map
->p
[i
] = map
->p
[map
->n
-1];
8219 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8220 struct isl_basic_set
*bset
)
8222 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8223 (struct isl_basic_map
*)bset
);
8226 /* Given two basic sets bset1 and bset2, compute the maximal difference
8227 * between the values of dimension pos in bset1 and those in bset2
8228 * for any common value of the parameters and dimensions preceding pos.
8230 static enum isl_lp_result
basic_set_maximal_difference_at(
8231 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8232 int pos
, isl_int
*opt
)
8235 struct isl_basic_map
*bmap1
= NULL
;
8236 struct isl_basic_map
*bmap2
= NULL
;
8237 struct isl_ctx
*ctx
;
8238 struct isl_vec
*obj
;
8241 unsigned dim1
, dim2
;
8242 enum isl_lp_result res
;
8244 if (!bset1
|| !bset2
)
8245 return isl_lp_error
;
8247 nparam
= isl_basic_set_n_param(bset1
);
8248 dim1
= isl_basic_set_n_dim(bset1
);
8249 dim2
= isl_basic_set_n_dim(bset2
);
8250 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8251 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8252 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8253 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8254 if (!bmap1
|| !bmap2
)
8256 bmap1
= isl_basic_map_cow(bmap1
);
8257 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8258 pos
, (dim1
- pos
) + (dim2
- pos
),
8259 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8260 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8263 total
= isl_basic_map_total_dim(bmap1
);
8265 obj
= isl_vec_alloc(ctx
, 1 + total
);
8268 isl_seq_clr(obj
->block
.data
, 1 + total
);
8269 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8270 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8271 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8273 isl_basic_map_free(bmap1
);
8277 isl_basic_map_free(bmap2
);
8279 isl_basic_map_free(bmap1
);
8280 return isl_lp_error
;
8283 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8284 * for any common value of the parameters and dimensions preceding pos
8285 * in both basic sets, the values of dimension pos in bset1 are
8286 * smaller or larger than those in bset2.
8289 * 1 if bset1 follows bset2
8290 * -1 if bset1 precedes bset2
8291 * 0 if bset1 and bset2 are incomparable
8292 * -2 if some error occurred.
8294 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8295 struct isl_basic_set
*bset2
, int pos
)
8298 enum isl_lp_result res
;
8303 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8305 if (res
== isl_lp_empty
)
8307 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8308 res
== isl_lp_unbounded
)
8310 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8319 /* Given two basic sets bset1 and bset2, check whether
8320 * for any common value of the parameters and dimensions preceding pos
8321 * there is a value of dimension pos in bset1 that is larger
8322 * than a value of the same dimension in bset2.
8325 * 1 if there exists such a pair
8326 * 0 if there is no such pair, but there is a pair of equal values
8328 * -2 if some error occurred.
8330 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8331 __isl_keep isl_basic_set
*bset2
, int pos
)
8334 enum isl_lp_result res
;
8339 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8341 if (res
== isl_lp_empty
)
8343 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8344 res
== isl_lp_unbounded
)
8346 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8348 else if (res
== isl_lp_ok
)
8357 /* Given two sets set1 and set2, check whether
8358 * for any common value of the parameters and dimensions preceding pos
8359 * there is a value of dimension pos in set1 that is larger
8360 * than a value of the same dimension in set2.
8363 * 1 if there exists such a pair
8364 * 0 if there is no such pair, but there is a pair of equal values
8366 * -2 if some error occurred.
8368 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8369 __isl_keep isl_set
*set2
, int pos
)
8377 for (i
= 0; i
< set1
->n
; ++i
)
8378 for (j
= 0; j
< set2
->n
; ++j
) {
8380 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8381 if (f
== 1 || f
== -2)
8390 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8391 unsigned pos
, isl_int
*val
)
8399 total
= isl_basic_map_total_dim(bmap
);
8400 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8401 for (; d
+1 > pos
; --d
)
8402 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8406 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8408 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8410 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8413 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8419 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8420 unsigned pos
, isl_int
*val
)
8432 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8435 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8436 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8437 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8438 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8442 isl_int_set(*val
, v
);
8448 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8449 unsigned pos
, isl_int
*val
)
8451 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8455 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8458 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8461 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8462 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8464 if (pos
>= isl_basic_map_dim(bmap
, type
))
8466 return isl_basic_map_plain_has_fixed_var(bmap
,
8467 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8470 /* If "bmap" obviously lies on a hyperplane where the given dimension
8471 * has a fixed value, then return that value.
8472 * Otherwise return NaN.
8474 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8475 __isl_keep isl_basic_map
*bmap
,
8476 enum isl_dim_type type
, unsigned pos
)
8484 ctx
= isl_basic_map_get_ctx(bmap
);
8485 v
= isl_val_alloc(ctx
);
8488 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8490 return isl_val_free(v
);
8492 isl_int_set_si(v
->d
, 1);
8496 return isl_val_nan(ctx
);
8499 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8500 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8502 if (pos
>= isl_map_dim(map
, type
))
8504 return isl_map_plain_has_fixed_var(map
,
8505 map_offset(map
, type
) - 1 + pos
, val
);
8508 /* If "map" obviously lies on a hyperplane where the given dimension
8509 * has a fixed value, then return that value.
8510 * Otherwise return NaN.
8512 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8513 enum isl_dim_type type
, unsigned pos
)
8521 ctx
= isl_map_get_ctx(map
);
8522 v
= isl_val_alloc(ctx
);
8525 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8527 return isl_val_free(v
);
8529 isl_int_set_si(v
->d
, 1);
8533 return isl_val_nan(ctx
);
8536 /* If "set" obviously lies on a hyperplane where the given dimension
8537 * has a fixed value, then return that value.
8538 * Otherwise return NaN.
8540 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8541 enum isl_dim_type type
, unsigned pos
)
8543 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8546 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8547 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8549 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8552 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8553 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8555 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8558 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8559 * then return this fixed value in *val.
8561 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8562 unsigned dim
, isl_int
*val
)
8564 return isl_basic_set_plain_has_fixed_var(bset
,
8565 isl_basic_set_n_param(bset
) + dim
, val
);
8568 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8569 * then return this fixed value in *val.
8571 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8572 unsigned dim
, isl_int
*val
)
8574 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8577 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8578 unsigned dim
, isl_int
*val
)
8580 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8583 /* Check if input variable in has fixed value and if so and if val is not NULL,
8584 * then return this fixed value in *val.
8586 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8587 unsigned in
, isl_int
*val
)
8589 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8592 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8593 * and if val is not NULL, then return this lower bound in *val.
8595 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8596 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8598 int i
, i_eq
= -1, i_ineq
= -1;
8605 total
= isl_basic_set_total_dim(bset
);
8606 nparam
= isl_basic_set_n_param(bset
);
8607 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8608 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8614 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8615 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8617 if (i_eq
!= -1 || i_ineq
!= -1)
8621 if (i_eq
== -1 && i_ineq
== -1)
8623 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8624 /* The coefficient should always be one due to normalization. */
8625 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8627 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8629 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8630 total
- nparam
- dim
- 1) != -1)
8633 isl_int_neg(*val
, c
[0]);
8637 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8638 unsigned dim
, isl_int
*val
)
8650 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8654 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8656 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8657 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8659 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8663 isl_int_set(*val
, v
);
8674 /* uset_gist depends on constraints without existentially quantified
8675 * variables sorting first.
8677 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8679 const struct constraint
*c1
= (const struct constraint
*)p1
;
8680 const struct constraint
*c2
= (const struct constraint
*)p2
;
8682 unsigned size
= isl_min(c1
->size
, c2
->size
);
8684 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8685 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8690 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8693 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8694 struct isl_basic_map
*bmap
)
8697 struct constraint
*c
;
8702 if (bmap
->n_ineq
== 0)
8704 total
= isl_basic_map_total_dim(bmap
);
8705 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8708 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8710 c
[i
].c
= bmap
->ineq
[i
];
8712 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8713 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8714 bmap
->ineq
[i
] = c
[i
].c
;
8718 isl_basic_map_free(bmap
);
8722 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8723 __isl_take isl_basic_set
*bset
)
8725 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8726 (struct isl_basic_map
*)bset
);
8729 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8733 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8735 bmap
= isl_basic_map_remove_redundancies(bmap
);
8736 bmap
= isl_basic_map_sort_constraints(bmap
);
8738 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8742 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8744 return (struct isl_basic_set
*)isl_basic_map_normalize(
8745 (struct isl_basic_map
*)bset
);
8748 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8749 const __isl_keep isl_basic_map
*bmap2
)
8756 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8757 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8758 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8759 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8760 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8761 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8762 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8763 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8764 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8765 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8766 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8768 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8770 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8772 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8773 return bmap1
->n_eq
- bmap2
->n_eq
;
8774 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8775 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8776 if (bmap1
->n_div
!= bmap2
->n_div
)
8777 return bmap1
->n_div
- bmap2
->n_div
;
8778 total
= isl_basic_map_total_dim(bmap1
);
8779 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8780 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8784 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8785 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8789 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8790 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8797 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8798 const __isl_keep isl_basic_set
*bset2
)
8800 return isl_basic_map_plain_cmp(bset1
, bset2
);
8803 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8809 if (set1
->n
!= set2
->n
)
8810 return set1
->n
- set2
->n
;
8812 for (i
= 0; i
< set1
->n
; ++i
) {
8813 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8821 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8822 __isl_keep isl_basic_map
*bmap2
)
8824 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8827 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8828 __isl_keep isl_basic_set
*bset2
)
8830 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8831 (isl_basic_map
*)bset2
);
8834 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8836 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8837 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8839 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8842 /* We normalize in place, but if anything goes wrong we need
8843 * to return NULL, so we need to make sure we don't change the
8844 * meaning of any possible other copies of map.
8846 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8849 struct isl_basic_map
*bmap
;
8853 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8855 for (i
= 0; i
< map
->n
; ++i
) {
8856 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8859 isl_basic_map_free(map
->p
[i
]);
8862 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8863 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8864 map
= isl_map_remove_empty_parts(map
);
8867 for (i
= map
->n
- 1; i
>= 1; --i
) {
8868 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8870 isl_basic_map_free(map
->p
[i
-1]);
8871 for (j
= i
; j
< map
->n
; ++j
)
8872 map
->p
[j
-1] = map
->p
[j
];
8882 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8884 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8887 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8897 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8900 map1
= isl_map_copy(map1
);
8901 map2
= isl_map_copy(map2
);
8902 map1
= isl_map_normalize(map1
);
8903 map2
= isl_map_normalize(map2
);
8906 equal
= map1
->n
== map2
->n
;
8907 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8908 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8921 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8923 return isl_map_plain_is_equal(map1
, map2
);
8926 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8928 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8929 (struct isl_map
*)set2
);
8932 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8934 return isl_set_plain_is_equal(set1
, set2
);
8937 /* Return an interval that ranges from min to max (inclusive)
8939 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8940 isl_int min
, isl_int max
)
8943 struct isl_basic_set
*bset
= NULL
;
8945 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8949 k
= isl_basic_set_alloc_inequality(bset
);
8952 isl_int_set_si(bset
->ineq
[k
][1], 1);
8953 isl_int_neg(bset
->ineq
[k
][0], min
);
8955 k
= isl_basic_set_alloc_inequality(bset
);
8958 isl_int_set_si(bset
->ineq
[k
][1], -1);
8959 isl_int_set(bset
->ineq
[k
][0], max
);
8963 isl_basic_set_free(bset
);
8967 /* Return the Cartesian product of the basic sets in list (in the given order).
8969 __isl_give isl_basic_set
*isl_basic_set_list_product(
8970 __isl_take
struct isl_basic_set_list
*list
)
8978 struct isl_basic_set
*product
= NULL
;
8982 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8983 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8984 nparam
= isl_basic_set_n_param(list
->p
[0]);
8985 dim
= isl_basic_set_n_dim(list
->p
[0]);
8986 extra
= list
->p
[0]->n_div
;
8987 n_eq
= list
->p
[0]->n_eq
;
8988 n_ineq
= list
->p
[0]->n_ineq
;
8989 for (i
= 1; i
< list
->n
; ++i
) {
8990 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8991 isl_assert(list
->ctx
,
8992 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8993 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8994 extra
+= list
->p
[i
]->n_div
;
8995 n_eq
+= list
->p
[i
]->n_eq
;
8996 n_ineq
+= list
->p
[i
]->n_ineq
;
8998 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9003 for (i
= 0; i
< list
->n
; ++i
) {
9004 isl_basic_set_add_constraints(product
,
9005 isl_basic_set_copy(list
->p
[i
]), dim
);
9006 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9008 isl_basic_set_list_free(list
);
9011 isl_basic_set_free(product
);
9012 isl_basic_set_list_free(list
);
9016 struct isl_basic_map
*isl_basic_map_product(
9017 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9019 isl_space
*dim_result
= NULL
;
9020 struct isl_basic_map
*bmap
;
9021 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9022 struct isl_dim_map
*dim_map1
, *dim_map2
;
9024 if (!bmap1
|| !bmap2
)
9027 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9028 bmap2
->dim
, isl_dim_param
), goto error
);
9029 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9030 isl_space_copy(bmap2
->dim
));
9032 in1
= isl_basic_map_n_in(bmap1
);
9033 in2
= isl_basic_map_n_in(bmap2
);
9034 out1
= isl_basic_map_n_out(bmap1
);
9035 out2
= isl_basic_map_n_out(bmap2
);
9036 nparam
= isl_basic_map_n_param(bmap1
);
9038 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9039 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9040 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9041 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9042 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9043 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9044 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9045 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9046 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9047 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9048 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9050 bmap
= isl_basic_map_alloc_space(dim_result
,
9051 bmap1
->n_div
+ bmap2
->n_div
,
9052 bmap1
->n_eq
+ bmap2
->n_eq
,
9053 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9054 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9055 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9056 bmap
= isl_basic_map_simplify(bmap
);
9057 return isl_basic_map_finalize(bmap
);
9059 isl_basic_map_free(bmap1
);
9060 isl_basic_map_free(bmap2
);
9064 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9065 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9067 isl_basic_map
*prod
;
9069 prod
= isl_basic_map_product(bmap1
, bmap2
);
9070 prod
= isl_basic_map_flatten(prod
);
9074 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9075 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9077 return isl_basic_map_flat_range_product(bset1
, bset2
);
9080 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9081 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9083 isl_space
*space_result
= NULL
;
9084 isl_basic_map
*bmap
;
9085 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9086 struct isl_dim_map
*dim_map1
, *dim_map2
;
9088 if (!bmap1
|| !bmap2
)
9091 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9092 isl_space_copy(bmap2
->dim
));
9094 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9095 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9096 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9097 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9099 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9100 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9101 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9102 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9103 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9104 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9105 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9106 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9107 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9108 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9109 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9111 bmap
= isl_basic_map_alloc_space(space_result
,
9112 bmap1
->n_div
+ bmap2
->n_div
,
9113 bmap1
->n_eq
+ bmap2
->n_eq
,
9114 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9115 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9116 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9117 bmap
= isl_basic_map_simplify(bmap
);
9118 return isl_basic_map_finalize(bmap
);
9120 isl_basic_map_free(bmap1
);
9121 isl_basic_map_free(bmap2
);
9125 __isl_give isl_basic_map
*isl_basic_map_range_product(
9126 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9128 isl_space
*dim_result
= NULL
;
9129 isl_basic_map
*bmap
;
9130 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9131 struct isl_dim_map
*dim_map1
, *dim_map2
;
9133 if (!bmap1
|| !bmap2
)
9136 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9137 bmap2
->dim
, isl_dim_param
))
9138 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9139 "parameters don't match", goto error
);
9141 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9142 isl_space_copy(bmap2
->dim
));
9144 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9145 out1
= isl_basic_map_n_out(bmap1
);
9146 out2
= isl_basic_map_n_out(bmap2
);
9147 nparam
= isl_basic_map_n_param(bmap1
);
9149 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9150 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9151 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9152 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9153 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9154 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9155 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9156 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9157 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9158 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9159 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9161 bmap
= isl_basic_map_alloc_space(dim_result
,
9162 bmap1
->n_div
+ bmap2
->n_div
,
9163 bmap1
->n_eq
+ bmap2
->n_eq
,
9164 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9165 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9166 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9167 bmap
= isl_basic_map_simplify(bmap
);
9168 return isl_basic_map_finalize(bmap
);
9170 isl_basic_map_free(bmap1
);
9171 isl_basic_map_free(bmap2
);
9175 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9176 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9178 isl_basic_map
*prod
;
9180 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9181 prod
= isl_basic_map_flatten_range(prod
);
9185 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9186 __isl_take isl_map
*map2
,
9187 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9188 __isl_take isl_space
*right
),
9189 __isl_give isl_basic_map
*(*basic_map_product
)(
9190 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9193 struct isl_map
*result
;
9199 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9200 map2
->dim
, isl_dim_param
), goto error
);
9202 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9203 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9204 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9206 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9207 isl_space_copy(map2
->dim
)),
9208 map1
->n
* map2
->n
, flags
);
9211 for (i
= 0; i
< map1
->n
; ++i
)
9212 for (j
= 0; j
< map2
->n
; ++j
) {
9213 struct isl_basic_map
*part
;
9214 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9215 isl_basic_map_copy(map2
->p
[j
]));
9216 if (isl_basic_map_is_empty(part
))
9217 isl_basic_map_free(part
);
9219 result
= isl_map_add_basic_map(result
, part
);
9232 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9234 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9235 __isl_take isl_map
*map2
)
9237 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9240 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9241 __isl_take isl_map
*map2
)
9243 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9246 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9248 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9249 __isl_take isl_map
*map2
)
9253 prod
= isl_map_product(map1
, map2
);
9254 prod
= isl_map_flatten(prod
);
9258 /* Given two set A and B, construct its Cartesian product A x B.
9260 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9262 return isl_map_range_product(set1
, set2
);
9265 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9266 __isl_take isl_set
*set2
)
9268 return isl_map_flat_range_product(set1
, set2
);
9271 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9273 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9274 __isl_take isl_map
*map2
)
9276 return map_product(map1
, map2
, &isl_space_domain_product
,
9277 &isl_basic_map_domain_product
);
9280 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9282 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9283 __isl_take isl_map
*map2
)
9285 return map_product(map1
, map2
, &isl_space_range_product
,
9286 &isl_basic_map_range_product
);
9289 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9290 __isl_take isl_map
*map2
)
9292 return isl_map_align_params_map_map_and(map1
, map2
,
9293 &map_domain_product_aligned
);
9296 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9297 __isl_take isl_map
*map2
)
9299 return isl_map_align_params_map_map_and(map1
, map2
,
9300 &map_range_product_aligned
);
9303 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9305 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9306 __isl_take isl_map
*map2
)
9310 prod
= isl_map_domain_product(map1
, map2
);
9311 prod
= isl_map_flatten_domain(prod
);
9315 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9317 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9318 __isl_take isl_map
*map2
)
9322 prod
= isl_map_range_product(map1
, map2
);
9323 prod
= isl_map_flatten_range(prod
);
9327 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9330 uint32_t hash
= isl_hash_init();
9335 bmap
= isl_basic_map_copy(bmap
);
9336 bmap
= isl_basic_map_normalize(bmap
);
9339 total
= isl_basic_map_total_dim(bmap
);
9340 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9341 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9343 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9344 isl_hash_hash(hash
, c_hash
);
9346 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9347 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9349 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9350 isl_hash_hash(hash
, c_hash
);
9352 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9353 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9355 if (isl_int_is_zero(bmap
->div
[i
][0]))
9357 isl_hash_byte(hash
, i
& 0xFF);
9358 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9359 isl_hash_hash(hash
, c_hash
);
9361 isl_basic_map_free(bmap
);
9365 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9367 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9370 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9377 map
= isl_map_copy(map
);
9378 map
= isl_map_normalize(map
);
9382 hash
= isl_hash_init();
9383 for (i
= 0; i
< map
->n
; ++i
) {
9385 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9386 isl_hash_hash(hash
, bmap_hash
);
9394 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9396 return isl_map_get_hash((isl_map
*)set
);
9399 /* Check if the value for dimension dim is completely determined
9400 * by the values of the other parameters and variables.
9401 * That is, check if dimension dim is involved in an equality.
9403 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9410 nparam
= isl_basic_set_n_param(bset
);
9411 for (i
= 0; i
< bset
->n_eq
; ++i
)
9412 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9417 /* Check if the value for dimension dim is completely determined
9418 * by the values of the other parameters and variables.
9419 * That is, check if dimension dim is involved in an equality
9420 * for each of the subsets.
9422 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9428 for (i
= 0; i
< set
->n
; ++i
) {
9430 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9437 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9439 return set
? set
->n
: 0;
9442 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9443 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9450 for (i
= 0; i
< map
->n
; ++i
)
9451 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9457 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9458 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9465 for (i
= 0; i
< set
->n
; ++i
)
9466 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9472 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9479 bset
= isl_basic_set_cow(bset
);
9483 dim
= isl_basic_set_get_space(bset
);
9484 dim
= isl_space_lift(dim
, bset
->n_div
);
9487 isl_space_free(bset
->dim
);
9489 bset
->extra
-= bset
->n_div
;
9492 bset
= isl_basic_set_finalize(bset
);
9496 isl_basic_set_free(bset
);
9500 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9506 set
= isl_set_align_divs(set
);
9511 set
= isl_set_cow(set
);
9515 n_div
= set
->p
[0]->n_div
;
9516 dim
= isl_set_get_space(set
);
9517 dim
= isl_space_lift(dim
, n_div
);
9520 isl_space_free(set
->dim
);
9523 for (i
= 0; i
< set
->n
; ++i
) {
9524 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9535 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9538 struct isl_basic_map
*bmap
;
9545 set
= isl_set_align_divs(set
);
9550 dim
= isl_set_get_space(set
);
9551 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9553 return isl_map_identity(isl_space_map_from_set(dim
));
9556 n_div
= set
->p
[0]->n_div
;
9557 dim
= isl_space_map_from_set(dim
);
9558 n_param
= isl_space_dim(dim
, isl_dim_param
);
9559 n_set
= isl_space_dim(dim
, isl_dim_in
);
9560 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9561 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9562 for (i
= 0; i
< n_set
; ++i
)
9563 bmap
= var_equal(bmap
, i
);
9565 total
= n_param
+ n_set
+ n_set
+ n_div
;
9566 for (i
= 0; i
< n_div
; ++i
) {
9567 k
= isl_basic_map_alloc_inequality(bmap
);
9570 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9571 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9572 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9573 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9574 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9575 set
->p
[0]->div
[i
][0]);
9577 l
= isl_basic_map_alloc_inequality(bmap
);
9580 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9581 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9582 set
->p
[0]->div
[i
][0]);
9583 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9587 bmap
= isl_basic_map_simplify(bmap
);
9588 bmap
= isl_basic_map_finalize(bmap
);
9589 return isl_map_from_basic_map(bmap
);
9592 isl_basic_map_free(bmap
);
9596 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9604 dim
= isl_basic_set_total_dim(bset
);
9605 size
+= bset
->n_eq
* (1 + dim
);
9606 size
+= bset
->n_ineq
* (1 + dim
);
9607 size
+= bset
->n_div
* (2 + dim
);
9612 int isl_set_size(__isl_keep isl_set
*set
)
9620 for (i
= 0; i
< set
->n
; ++i
)
9621 size
+= isl_basic_set_size(set
->p
[i
]);
9626 /* Check if there is any lower bound (if lower == 0) and/or upper
9627 * bound (if upper == 0) on the specified dim.
9629 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9630 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9637 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9639 pos
+= isl_basic_map_offset(bmap
, type
);
9641 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9642 if (isl_int_is_zero(bmap
->div
[i
][0]))
9644 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9648 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9649 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9652 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9653 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9660 return lower
&& upper
;
9663 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9664 enum isl_dim_type type
, unsigned pos
)
9666 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9669 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9670 enum isl_dim_type type
, unsigned pos
)
9672 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9675 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9676 enum isl_dim_type type
, unsigned pos
)
9678 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9681 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9682 enum isl_dim_type type
, unsigned pos
)
9689 for (i
= 0; i
< map
->n
; ++i
) {
9691 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9692 if (bounded
< 0 || !bounded
)
9699 /* Return 1 if the specified dim is involved in both an upper bound
9700 * and a lower bound.
9702 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9703 enum isl_dim_type type
, unsigned pos
)
9705 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9708 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9710 static int has_any_bound(__isl_keep isl_map
*map
,
9711 enum isl_dim_type type
, unsigned pos
,
9712 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9713 enum isl_dim_type type
, unsigned pos
))
9720 for (i
= 0; i
< map
->n
; ++i
) {
9722 bounded
= fn(map
->p
[i
], type
, pos
);
9723 if (bounded
< 0 || bounded
)
9730 /* Return 1 if the specified dim is involved in any lower bound.
9732 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9733 enum isl_dim_type type
, unsigned pos
)
9735 return has_any_bound(set
, type
, pos
,
9736 &isl_basic_map_dim_has_lower_bound
);
9739 /* Return 1 if the specified dim is involved in any upper bound.
9741 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9742 enum isl_dim_type type
, unsigned pos
)
9744 return has_any_bound(set
, type
, pos
,
9745 &isl_basic_map_dim_has_upper_bound
);
9748 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9750 static int has_bound(__isl_keep isl_map
*map
,
9751 enum isl_dim_type type
, unsigned pos
,
9752 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9753 enum isl_dim_type type
, unsigned pos
))
9760 for (i
= 0; i
< map
->n
; ++i
) {
9762 bounded
= fn(map
->p
[i
], type
, pos
);
9763 if (bounded
< 0 || !bounded
)
9770 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9772 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9773 enum isl_dim_type type
, unsigned pos
)
9775 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9778 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9780 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9781 enum isl_dim_type type
, unsigned pos
)
9783 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9786 /* For each of the "n" variables starting at "first", determine
9787 * the sign of the variable and put the results in the first "n"
9788 * elements of the array "signs".
9790 * 1 means that the variable is non-negative
9791 * -1 means that the variable is non-positive
9792 * 0 means the variable attains both positive and negative values.
9794 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9795 unsigned first
, unsigned n
, int *signs
)
9797 isl_vec
*bound
= NULL
;
9798 struct isl_tab
*tab
= NULL
;
9799 struct isl_tab_undo
*snap
;
9802 if (!bset
|| !signs
)
9805 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9806 tab
= isl_tab_from_basic_set(bset
, 0);
9810 isl_seq_clr(bound
->el
, bound
->size
);
9811 isl_int_set_si(bound
->el
[0], -1);
9813 snap
= isl_tab_snap(tab
);
9814 for (i
= 0; i
< n
; ++i
) {
9817 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9818 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9821 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9822 if (isl_tab_rollback(tab
, snap
) < 0)
9830 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9831 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9834 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9835 if (isl_tab_rollback(tab
, snap
) < 0)
9838 signs
[i
] = empty
? -1 : 0;
9842 isl_vec_free(bound
);
9846 isl_vec_free(bound
);
9850 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
9851 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
9853 if (!bset
|| !signs
)
9855 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
9858 first
+= pos(bset
->dim
, type
) - 1;
9859 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
9862 /* Check if the given basic map is obviously single-valued.
9863 * In particular, for each output dimension, check that there is
9864 * an equality that defines the output dimension in terms of
9865 * earlier dimensions.
9867 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9877 total
= 1 + isl_basic_map_total_dim(bmap
);
9878 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
9879 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
9881 for (i
= 0; i
< n_out
; ++i
) {
9882 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
9883 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
9885 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
9886 total
- (o_out
+ i
+ 1)) == -1)
9889 if (j
>= bmap
->n_eq
)
9896 /* Check if the given basic map is single-valued.
9901 * and check if the result is a subset of the identity mapping.
9903 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9906 isl_basic_map
*test
;
9910 sv
= isl_basic_map_plain_is_single_valued(bmap
);
9914 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
9915 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
9917 space
= isl_basic_map_get_space(bmap
);
9918 space
= isl_space_map_from_set(isl_space_range(space
));
9919 id
= isl_basic_map_identity(space
);
9921 sv
= isl_basic_map_is_subset(test
, id
);
9923 isl_basic_map_free(test
);
9924 isl_basic_map_free(id
);
9929 /* Check if the given map is obviously single-valued.
9931 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9940 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9943 /* Check if the given map is single-valued.
9948 * and check if the result is a subset of the identity mapping.
9950 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9957 sv
= isl_map_plain_is_single_valued(map
);
9961 test
= isl_map_reverse(isl_map_copy(map
));
9962 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9964 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9965 id
= isl_map_identity(dim
);
9967 sv
= isl_map_is_subset(test
, id
);
9975 int isl_map_is_injective(__isl_keep isl_map
*map
)
9979 map
= isl_map_copy(map
);
9980 map
= isl_map_reverse(map
);
9981 in
= isl_map_is_single_valued(map
);
9987 /* Check if the given map is obviously injective.
9989 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9993 map
= isl_map_copy(map
);
9994 map
= isl_map_reverse(map
);
9995 in
= isl_map_plain_is_single_valued(map
);
10001 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10005 sv
= isl_map_is_single_valued(map
);
10009 return isl_map_is_injective(map
);
10012 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10014 return isl_map_is_single_valued((isl_map
*)set
);
10017 int isl_map_is_translation(__isl_keep isl_map
*map
)
10022 delta
= isl_map_deltas(isl_map_copy(map
));
10023 ok
= isl_set_is_singleton(delta
);
10024 isl_set_free(delta
);
10029 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10031 if (isl_seq_first_non_zero(p
, pos
) != -1)
10033 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10038 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10047 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10050 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10051 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10052 for (j
= 0; j
< nvar
; ++j
) {
10053 int lower
= 0, upper
= 0;
10054 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10055 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10057 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10061 if (i
< bset
->n_eq
)
10063 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10064 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10066 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10068 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10073 if (!lower
|| !upper
)
10080 int isl_set_is_box(__isl_keep isl_set
*set
)
10087 return isl_basic_set_is_box(set
->p
[0]);
10090 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10095 return isl_space_is_wrapping(bset
->dim
);
10098 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10103 return isl_space_is_wrapping(set
->dim
);
10106 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10108 bmap
= isl_basic_map_cow(bmap
);
10112 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10116 bmap
= isl_basic_map_finalize(bmap
);
10118 return (isl_basic_set
*)bmap
;
10120 isl_basic_map_free(bmap
);
10124 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10128 map
= isl_map_cow(map
);
10132 for (i
= 0; i
< map
->n
; ++i
) {
10133 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10137 map
->dim
= isl_space_wrap(map
->dim
);
10141 return (isl_set
*)map
;
10147 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10149 bset
= isl_basic_set_cow(bset
);
10153 bset
->dim
= isl_space_unwrap(bset
->dim
);
10157 bset
= isl_basic_set_finalize(bset
);
10159 return (isl_basic_map
*)bset
;
10161 isl_basic_set_free(bset
);
10165 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10172 if (!isl_set_is_wrapping(set
))
10173 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10176 set
= isl_set_cow(set
);
10180 for (i
= 0; i
< set
->n
; ++i
) {
10181 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10186 set
->dim
= isl_space_unwrap(set
->dim
);
10190 return (isl_map
*)set
;
10196 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10197 enum isl_dim_type type
)
10202 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10205 bmap
= isl_basic_map_cow(bmap
);
10209 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10213 bmap
= isl_basic_map_finalize(bmap
);
10217 isl_basic_map_free(bmap
);
10221 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10222 enum isl_dim_type type
)
10229 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10232 map
= isl_map_cow(map
);
10236 for (i
= 0; i
< map
->n
; ++i
) {
10237 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10241 map
->dim
= isl_space_reset(map
->dim
, type
);
10251 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10256 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10259 bmap
= isl_basic_map_cow(bmap
);
10263 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10267 bmap
= isl_basic_map_finalize(bmap
);
10271 isl_basic_map_free(bmap
);
10275 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10277 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10280 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10281 __isl_take isl_basic_map
*bmap
)
10286 if (!bmap
->dim
->nested
[0])
10289 bmap
= isl_basic_map_cow(bmap
);
10293 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10297 bmap
= isl_basic_map_finalize(bmap
);
10301 isl_basic_map_free(bmap
);
10305 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10306 __isl_take isl_basic_map
*bmap
)
10311 if (!bmap
->dim
->nested
[1])
10314 bmap
= isl_basic_map_cow(bmap
);
10318 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10322 bmap
= isl_basic_map_finalize(bmap
);
10326 isl_basic_map_free(bmap
);
10330 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10337 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10340 map
= isl_map_cow(map
);
10344 for (i
= 0; i
< map
->n
; ++i
) {
10345 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10349 map
->dim
= isl_space_flatten(map
->dim
);
10359 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10361 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10364 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10366 isl_space
*dim
, *flat_dim
;
10369 dim
= isl_set_get_space(set
);
10370 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10371 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10372 map
= isl_map_intersect_domain(map
, set
);
10377 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10384 if (!map
->dim
->nested
[0])
10387 map
= isl_map_cow(map
);
10391 for (i
= 0; i
< map
->n
; ++i
) {
10392 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10396 map
->dim
= isl_space_flatten_domain(map
->dim
);
10406 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10413 if (!map
->dim
->nested
[1])
10416 map
= isl_map_cow(map
);
10420 for (i
= 0; i
< map
->n
; ++i
) {
10421 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10425 map
->dim
= isl_space_flatten_range(map
->dim
);
10435 /* Reorder the dimensions of "bmap" according to the given dim_map
10436 * and set the dimension specification to "dim".
10438 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10439 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10441 isl_basic_map
*res
;
10444 bmap
= isl_basic_map_cow(bmap
);
10445 if (!bmap
|| !dim
|| !dim_map
)
10448 flags
= bmap
->flags
;
10449 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10450 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10451 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10452 res
= isl_basic_map_alloc_space(dim
,
10453 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10454 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10456 res
->flags
= flags
;
10457 res
= isl_basic_map_finalize(res
);
10461 isl_basic_map_free(bmap
);
10462 isl_space_free(dim
);
10466 /* Reorder the dimensions of "map" according to given reordering.
10468 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10469 __isl_take isl_reordering
*r
)
10472 struct isl_dim_map
*dim_map
;
10474 map
= isl_map_cow(map
);
10475 dim_map
= isl_dim_map_from_reordering(r
);
10476 if (!map
|| !r
|| !dim_map
)
10479 for (i
= 0; i
< map
->n
; ++i
) {
10480 struct isl_dim_map
*dim_map_i
;
10482 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10484 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10485 isl_space_copy(r
->dim
), dim_map_i
);
10491 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10493 isl_reordering_free(r
);
10499 isl_reordering_free(r
);
10503 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10504 __isl_take isl_reordering
*r
)
10506 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10509 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10510 __isl_take isl_space
*model
)
10514 if (!map
|| !model
)
10517 ctx
= isl_space_get_ctx(model
);
10518 if (!isl_space_has_named_params(model
))
10519 isl_die(ctx
, isl_error_invalid
,
10520 "model has unnamed parameters", goto error
);
10521 if (!isl_space_has_named_params(map
->dim
))
10522 isl_die(ctx
, isl_error_invalid
,
10523 "relation has unnamed parameters", goto error
);
10524 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10525 isl_reordering
*exp
;
10527 model
= isl_space_drop_dims(model
, isl_dim_in
,
10528 0, isl_space_dim(model
, isl_dim_in
));
10529 model
= isl_space_drop_dims(model
, isl_dim_out
,
10530 0, isl_space_dim(model
, isl_dim_out
));
10531 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10532 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10533 map
= isl_map_realign(map
, exp
);
10536 isl_space_free(model
);
10539 isl_space_free(model
);
10544 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10545 __isl_take isl_space
*model
)
10547 return isl_map_align_params(set
, model
);
10550 /* Align the parameters of "bmap" to those of "model", introducing
10551 * additional parameters if needed.
10553 __isl_give isl_basic_map
*isl_basic_map_align_params(
10554 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10558 if (!bmap
|| !model
)
10561 ctx
= isl_space_get_ctx(model
);
10562 if (!isl_space_has_named_params(model
))
10563 isl_die(ctx
, isl_error_invalid
,
10564 "model has unnamed parameters", goto error
);
10565 if (!isl_space_has_named_params(bmap
->dim
))
10566 isl_die(ctx
, isl_error_invalid
,
10567 "relation has unnamed parameters", goto error
);
10568 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10569 isl_reordering
*exp
;
10570 struct isl_dim_map
*dim_map
;
10572 model
= isl_space_drop_dims(model
, isl_dim_in
,
10573 0, isl_space_dim(model
, isl_dim_in
));
10574 model
= isl_space_drop_dims(model
, isl_dim_out
,
10575 0, isl_space_dim(model
, isl_dim_out
));
10576 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10577 exp
= isl_reordering_extend_space(exp
,
10578 isl_basic_map_get_space(bmap
));
10579 dim_map
= isl_dim_map_from_reordering(exp
);
10580 bmap
= isl_basic_map_realign(bmap
,
10581 exp
? isl_space_copy(exp
->dim
) : NULL
,
10582 isl_dim_map_extend(dim_map
, bmap
));
10583 isl_reordering_free(exp
);
10587 isl_space_free(model
);
10590 isl_space_free(model
);
10591 isl_basic_map_free(bmap
);
10595 /* Align the parameters of "bset" to those of "model", introducing
10596 * additional parameters if needed.
10598 __isl_give isl_basic_set
*isl_basic_set_align_params(
10599 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10601 return isl_basic_map_align_params(bset
, model
);
10604 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10605 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10606 enum isl_dim_type c2
, enum isl_dim_type c3
,
10607 enum isl_dim_type c4
, enum isl_dim_type c5
)
10609 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10610 struct isl_mat
*mat
;
10616 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10617 isl_basic_map_total_dim(bmap
) + 1);
10620 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10621 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10622 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10623 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10624 isl_int_set(mat
->row
[i
][pos
],
10625 bmap
->eq
[i
][off
+ k
]);
10633 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10634 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10635 enum isl_dim_type c2
, enum isl_dim_type c3
,
10636 enum isl_dim_type c4
, enum isl_dim_type c5
)
10638 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10639 struct isl_mat
*mat
;
10645 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10646 isl_basic_map_total_dim(bmap
) + 1);
10649 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10650 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10651 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10652 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10653 isl_int_set(mat
->row
[i
][pos
],
10654 bmap
->ineq
[i
][off
+ k
]);
10662 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10663 __isl_take isl_space
*dim
,
10664 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10665 enum isl_dim_type c2
, enum isl_dim_type c3
,
10666 enum isl_dim_type c4
, enum isl_dim_type c5
)
10668 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10669 isl_basic_map
*bmap
;
10675 if (!dim
|| !eq
|| !ineq
)
10678 if (eq
->n_col
!= ineq
->n_col
)
10679 isl_die(dim
->ctx
, isl_error_invalid
,
10680 "equalities and inequalities matrices should have "
10681 "same number of columns", goto error
);
10683 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10685 if (eq
->n_col
< total
)
10686 isl_die(dim
->ctx
, isl_error_invalid
,
10687 "number of columns too small", goto error
);
10689 extra
= eq
->n_col
- total
;
10691 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10692 eq
->n_row
, ineq
->n_row
);
10695 for (i
= 0; i
< extra
; ++i
) {
10696 k
= isl_basic_map_alloc_div(bmap
);
10699 isl_int_set_si(bmap
->div
[k
][0], 0);
10701 for (i
= 0; i
< eq
->n_row
; ++i
) {
10702 l
= isl_basic_map_alloc_equality(bmap
);
10705 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10706 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10707 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10708 isl_int_set(bmap
->eq
[l
][off
+ k
],
10714 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10715 l
= isl_basic_map_alloc_inequality(bmap
);
10718 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10719 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10720 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10721 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10722 ineq
->row
[i
][pos
]);
10728 isl_space_free(dim
);
10730 isl_mat_free(ineq
);
10732 bmap
= isl_basic_map_simplify(bmap
);
10733 return isl_basic_map_finalize(bmap
);
10735 isl_space_free(dim
);
10737 isl_mat_free(ineq
);
10741 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10742 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10743 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10745 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10746 c1
, c2
, c3
, c4
, isl_dim_in
);
10749 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10750 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10751 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10753 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10754 c1
, c2
, c3
, c4
, isl_dim_in
);
10757 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10758 __isl_take isl_space
*dim
,
10759 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10760 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10762 return (isl_basic_set
*)
10763 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10764 c1
, c2
, c3
, c4
, isl_dim_in
);
10767 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10772 return isl_space_can_zip(bmap
->dim
);
10775 int isl_map_can_zip(__isl_keep isl_map
*map
)
10780 return isl_space_can_zip(map
->dim
);
10783 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10784 * (A -> C) -> (B -> D).
10786 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10795 if (!isl_basic_map_can_zip(bmap
))
10796 isl_die(bmap
->ctx
, isl_error_invalid
,
10797 "basic map cannot be zipped", goto error
);
10798 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10799 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10800 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10801 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10802 bmap
= isl_basic_map_cow(bmap
);
10803 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10806 bmap
->dim
= isl_space_zip(bmap
->dim
);
10811 isl_basic_map_free(bmap
);
10815 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10816 * (A -> C) -> (B -> D).
10818 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10825 if (!isl_map_can_zip(map
))
10826 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10829 map
= isl_map_cow(map
);
10833 for (i
= 0; i
< map
->n
; ++i
) {
10834 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
10839 map
->dim
= isl_space_zip(map
->dim
);
10849 /* Can we apply isl_basic_map_curry to "bmap"?
10850 * That is, does it have a nested relation in its domain?
10852 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
10857 return isl_space_can_curry(bmap
->dim
);
10860 /* Can we apply isl_map_curry to "map"?
10861 * That is, does it have a nested relation in its domain?
10863 int isl_map_can_curry(__isl_keep isl_map
*map
)
10868 return isl_space_can_curry(map
->dim
);
10871 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10874 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
10880 if (!isl_basic_map_can_curry(bmap
))
10881 isl_die(bmap
->ctx
, isl_error_invalid
,
10882 "basic map cannot be curried", goto error
);
10883 bmap
= isl_basic_map_cow(bmap
);
10886 bmap
->dim
= isl_space_curry(bmap
->dim
);
10891 isl_basic_map_free(bmap
);
10895 /* Given a map (A -> B) -> C, return the corresponding map
10898 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
10905 if (!isl_map_can_curry(map
))
10906 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
10909 map
= isl_map_cow(map
);
10913 for (i
= 0; i
< map
->n
; ++i
) {
10914 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
10919 map
->dim
= isl_space_curry(map
->dim
);
10929 /* Can we apply isl_basic_map_uncurry to "bmap"?
10930 * That is, does it have a nested relation in its domain?
10932 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
10937 return isl_space_can_uncurry(bmap
->dim
);
10940 /* Can we apply isl_map_uncurry to "map"?
10941 * That is, does it have a nested relation in its domain?
10943 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
10948 return isl_space_can_uncurry(map
->dim
);
10951 /* Given a basic map A -> (B -> C), return the corresponding basic map
10954 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
10960 if (!isl_basic_map_can_uncurry(bmap
))
10961 isl_die(bmap
->ctx
, isl_error_invalid
,
10962 "basic map cannot be uncurried",
10963 return isl_basic_map_free(bmap
));
10964 bmap
= isl_basic_map_cow(bmap
);
10967 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
10969 return isl_basic_map_free(bmap
);
10973 /* Given a map A -> (B -> C), return the corresponding map
10976 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
10983 if (!isl_map_can_uncurry(map
))
10984 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
10985 return isl_map_free(map
));
10987 map
= isl_map_cow(map
);
10991 for (i
= 0; i
< map
->n
; ++i
) {
10992 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
10994 return isl_map_free(map
);
10997 map
->dim
= isl_space_uncurry(map
->dim
);
10999 return isl_map_free(map
);
11004 /* Construct a basic map mapping the domain of the affine expression
11005 * to a one-dimensional range prescribed by the affine expression.
11007 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11011 isl_local_space
*ls
;
11012 isl_basic_map
*bmap
;
11017 ls
= isl_aff_get_local_space(aff
);
11018 bmap
= isl_basic_map_from_local_space(ls
);
11019 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11020 k
= isl_basic_map_alloc_equality(bmap
);
11024 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11025 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11026 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11027 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11028 aff
->v
->size
- (pos
+ 1));
11031 bmap
= isl_basic_map_finalize(bmap
);
11035 isl_basic_map_free(bmap
);
11039 /* Construct a map mapping the domain of the affine expression
11040 * to a one-dimensional range prescribed by the affine expression.
11042 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11044 isl_basic_map
*bmap
;
11046 bmap
= isl_basic_map_from_aff(aff
);
11047 return isl_map_from_basic_map(bmap
);
11050 /* Construct a basic map mapping the domain the multi-affine expression
11051 * to its range, with each dimension in the range equated to the
11052 * corresponding affine expression.
11054 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11055 __isl_take isl_multi_aff
*maff
)
11059 isl_basic_map
*bmap
;
11064 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11065 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11066 "invalid space", return isl_multi_aff_free(maff
));
11068 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11069 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11071 for (i
= 0; i
< maff
->n
; ++i
) {
11073 isl_basic_map
*bmap_i
;
11075 aff
= isl_aff_copy(maff
->p
[i
]);
11076 bmap_i
= isl_basic_map_from_aff(aff
);
11078 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11081 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11083 isl_multi_aff_free(maff
);
11087 /* Construct a map mapping the domain the multi-affine expression
11088 * to its range, with each dimension in the range equated to the
11089 * corresponding affine expression.
11091 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11093 isl_basic_map
*bmap
;
11095 bmap
= isl_basic_map_from_multi_aff(maff
);
11096 return isl_map_from_basic_map(bmap
);
11099 /* Construct a basic map mapping a domain in the given space to
11100 * to an n-dimensional range, with n the number of elements in the list,
11101 * where each coordinate in the range is prescribed by the
11102 * corresponding affine expression.
11103 * The domains of all affine expressions in the list are assumed to match
11106 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11107 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11111 isl_basic_map
*bmap
;
11116 dim
= isl_space_from_domain(domain_dim
);
11117 bmap
= isl_basic_map_universe(dim
);
11119 for (i
= 0; i
< list
->n
; ++i
) {
11121 isl_basic_map
*bmap_i
;
11123 aff
= isl_aff_copy(list
->p
[i
]);
11124 bmap_i
= isl_basic_map_from_aff(aff
);
11126 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11129 isl_aff_list_free(list
);
11133 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11134 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11136 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11139 /* Construct a basic map where the given dimensions are equal to each other.
11141 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11142 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11144 isl_basic_map
*bmap
= NULL
;
11150 if (pos1
>= isl_space_dim(space
, type1
))
11151 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11152 "index out of bounds", goto error
);
11153 if (pos2
>= isl_space_dim(space
, type2
))
11154 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11155 "index out of bounds", goto error
);
11157 if (type1
== type2
&& pos1
== pos2
)
11158 return isl_basic_map_universe(space
);
11160 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11161 i
= isl_basic_map_alloc_equality(bmap
);
11164 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11165 pos1
+= isl_basic_map_offset(bmap
, type1
);
11166 pos2
+= isl_basic_map_offset(bmap
, type2
);
11167 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11168 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11169 bmap
= isl_basic_map_finalize(bmap
);
11170 isl_space_free(space
);
11173 isl_space_free(space
);
11174 isl_basic_map_free(bmap
);
11178 /* Add a constraint imposing that the given two dimensions are equal.
11180 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11181 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11185 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11187 bmap
= isl_basic_map_intersect(bmap
, eq
);
11192 /* Add a constraint imposing that the given two dimensions are equal.
11194 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11195 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11197 isl_basic_map
*bmap
;
11199 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11201 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11206 /* Add a constraint imposing that the given two dimensions have opposite values.
11208 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11209 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11211 isl_basic_map
*bmap
= NULL
;
11217 if (pos1
>= isl_map_dim(map
, type1
))
11218 isl_die(map
->ctx
, isl_error_invalid
,
11219 "index out of bounds", goto error
);
11220 if (pos2
>= isl_map_dim(map
, type2
))
11221 isl_die(map
->ctx
, isl_error_invalid
,
11222 "index out of bounds", goto error
);
11224 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11225 i
= isl_basic_map_alloc_equality(bmap
);
11228 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11229 pos1
+= isl_basic_map_offset(bmap
, type1
);
11230 pos2
+= isl_basic_map_offset(bmap
, type2
);
11231 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11232 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11233 bmap
= isl_basic_map_finalize(bmap
);
11235 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11239 isl_basic_map_free(bmap
);
11244 /* Add a constraint imposing that the value of the first dimension is
11245 * greater than or equal to that of the second.
11247 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11248 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11251 isl_local_space
*ls
;
11256 if (pos1
>= isl_basic_map_dim(bmap
, type1
))
11257 isl_die(bmap
->ctx
, isl_error_invalid
,
11258 "index out of bounds", return isl_basic_map_free(bmap
));
11259 if (pos2
>= isl_basic_map_dim(bmap
, type2
))
11260 isl_die(bmap
->ctx
, isl_error_invalid
,
11261 "index out of bounds", return isl_basic_map_free(bmap
));
11263 if (type1
== type2
&& pos1
== pos2
)
11266 ls
= isl_local_space_from_space(isl_basic_map_get_space(bmap
));
11267 c
= isl_inequality_alloc(ls
);
11268 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11269 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11270 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11275 /* Construct a basic map where the value of the first dimension is
11276 * greater than that of the second.
11278 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11279 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11281 isl_basic_map
*bmap
= NULL
;
11287 if (pos1
>= isl_space_dim(space
, type1
))
11288 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11289 "index out of bounds", goto error
);
11290 if (pos2
>= isl_space_dim(space
, type2
))
11291 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11292 "index out of bounds", goto error
);
11294 if (type1
== type2
&& pos1
== pos2
)
11295 return isl_basic_map_empty(space
);
11297 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11298 i
= isl_basic_map_alloc_inequality(bmap
);
11301 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11302 pos1
+= isl_basic_map_offset(bmap
, type1
);
11303 pos2
+= isl_basic_map_offset(bmap
, type2
);
11304 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11305 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11306 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11307 bmap
= isl_basic_map_finalize(bmap
);
11311 isl_space_free(space
);
11312 isl_basic_map_free(bmap
);
11316 /* Add a constraint imposing that the value of the first dimension is
11317 * greater than that of the second.
11319 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11320 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11324 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11326 bmap
= isl_basic_map_intersect(bmap
, gt
);
11331 /* Add a constraint imposing that the value of the first dimension is
11332 * greater than that of the second.
11334 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11335 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11337 isl_basic_map
*bmap
;
11339 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11341 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11346 /* Add a constraint imposing that the value of the first dimension is
11347 * smaller than that of the second.
11349 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11350 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11352 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11355 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11359 isl_local_space
*ls
;
11364 if (!isl_basic_map_divs_known(bmap
))
11365 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11366 "some divs are unknown", return NULL
);
11368 ls
= isl_basic_map_get_local_space(bmap
);
11369 div
= isl_local_space_get_div(ls
, pos
);
11370 isl_local_space_free(ls
);
11375 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11378 return isl_basic_map_get_div(bset
, pos
);
11381 /* Plug in "subs" for dimension "type", "pos" of "bset".
11383 * Let i be the dimension to replace and let "subs" be of the form
11387 * Any integer division with a non-zero coefficient for i,
11389 * floor((a i + g)/m)
11393 * floor((a f + d g)/(m d))
11395 * Constraints of the form
11403 * We currently require that "subs" is an integral expression.
11404 * Handling rational expressions may require us to add stride constraints
11405 * as we do in isl_basic_set_preimage_multi_aff.
11407 __isl_give isl_basic_set
*isl_basic_set_substitute(
11408 __isl_take isl_basic_set
*bset
,
11409 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11415 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11418 bset
= isl_basic_set_cow(bset
);
11419 if (!bset
|| !subs
)
11422 ctx
= isl_basic_set_get_ctx(bset
);
11423 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11424 isl_die(ctx
, isl_error_invalid
,
11425 "spaces don't match", goto error
);
11426 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11427 isl_die(ctx
, isl_error_unsupported
,
11428 "cannot handle divs yet", goto error
);
11429 if (!isl_int_is_one(subs
->v
->el
[0]))
11430 isl_die(ctx
, isl_error_invalid
,
11431 "can only substitute integer expressions", goto error
);
11433 pos
+= isl_basic_set_offset(bset
, type
);
11437 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11438 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11440 isl_int_set(v
, bset
->eq
[i
][pos
]);
11441 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11442 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11443 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11446 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11447 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11449 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11450 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11451 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11452 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11455 for (i
= 0; i
< bset
->n_div
; ++i
) {
11456 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11458 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11459 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11460 isl_seq_combine(bset
->div
[i
] + 1,
11461 subs
->v
->el
[0], bset
->div
[i
] + 1,
11462 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11463 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11468 bset
= isl_basic_set_simplify(bset
);
11469 return isl_basic_set_finalize(bset
);
11471 isl_basic_set_free(bset
);
11475 /* Plug in "subs" for dimension "type", "pos" of "set".
11477 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11478 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11482 if (set
&& isl_set_plain_is_empty(set
))
11485 set
= isl_set_cow(set
);
11489 for (i
= set
->n
- 1; i
>= 0; --i
) {
11490 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11491 if (remove_if_empty(set
, i
) < 0)
11501 /* Check if the range of "ma" is compatible with the domain or range
11502 * (depending on "type") of "bmap".
11503 * Return -1 if anything is wrong.
11505 static int check_basic_map_compatible_range_multi_aff(
11506 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11507 __isl_keep isl_multi_aff
*ma
)
11510 isl_space
*ma_space
;
11512 ma_space
= isl_multi_aff_get_space(ma
);
11513 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11514 isl_space_free(ma_space
);
11516 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11517 "spaces don't match", return -1);
11521 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11522 * coefficients before the transformed range of dimensions,
11523 * the "n_after" coefficients after the transformed range of dimensions
11524 * and the coefficients of the other divs in "bmap".
11526 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11527 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11532 isl_local_space
*ls
;
11537 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11541 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11542 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11543 for (i
= 0; i
< n_div
; ++i
) {
11544 int o_bmap
= 0, o_ls
= 0;
11546 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11547 o_bmap
+= 1 + 1 + n_param
;
11548 o_ls
+= 1 + 1 + n_param
;
11549 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11550 o_bmap
+= n_before
;
11551 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11552 ls
->div
->row
[i
] + o_ls
, n_set
);
11555 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11557 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11558 ls
->div
->row
[i
] + o_ls
, n_div
);
11561 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11562 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11566 isl_local_space_free(ls
);
11569 isl_local_space_free(ls
);
11573 /* How many stride constraints does "ma" enforce?
11574 * That is, how many of the affine expressions have a denominator
11575 * different from one?
11577 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11582 for (i
= 0; i
< ma
->n
; ++i
)
11583 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11589 /* For each affine expression in ma of the form
11591 * x_i = (f_i y + h_i)/m_i
11593 * with m_i different from one, add a constraint to "bmap"
11596 * f_i y + h_i = m_i alpha_i
11598 * with alpha_i an additional existentially quantified variable.
11600 static __isl_give isl_basic_map
*add_ma_strides(
11601 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11602 int n_before
, int n_after
)
11611 total
= isl_basic_map_total_dim(bmap
);
11612 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11613 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11614 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11615 for (i
= 0; i
< ma
->n
; ++i
) {
11616 int o_bmap
= 0, o_ma
= 1;
11618 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11620 div
= isl_basic_map_alloc_div(bmap
);
11621 k
= isl_basic_map_alloc_equality(bmap
);
11622 if (div
< 0 || k
< 0)
11624 isl_int_set_si(bmap
->div
[div
][0], 0);
11625 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11626 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11627 o_bmap
+= 1 + n_param
;
11628 o_ma
+= 1 + n_param
;
11629 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11630 o_bmap
+= n_before
;
11631 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11632 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11635 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11637 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11638 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11641 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11642 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11648 isl_basic_map_free(bmap
);
11652 /* Replace the domain or range space (depending on "type) of "space" by "set".
11654 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11655 enum isl_dim_type type
, __isl_take isl_space
*set
)
11657 if (type
== isl_dim_in
) {
11658 space
= isl_space_range(space
);
11659 space
= isl_space_map_from_domain_and_range(set
, space
);
11661 space
= isl_space_domain(space
);
11662 space
= isl_space_map_from_domain_and_range(space
, set
);
11668 /* Compute the preimage of the domain or range (depending on "type")
11669 * of "bmap" under the function represented by "ma".
11670 * In other words, plug in "ma" in the domain or range of "bmap".
11671 * The result is a basic map that lives in the same space as "bmap"
11672 * except that the domain or range has been replaced by
11673 * the domain space of "ma".
11675 * If bmap is represented by
11677 * A(p) + S u + B x + T v + C(divs) >= 0,
11679 * where u and x are input and output dimensions if type == isl_dim_out
11680 * while x and v are input and output dimensions if type == isl_dim_in,
11681 * and ma is represented by
11683 * x = D(p) + F(y) + G(divs')
11685 * then the result is
11687 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11689 * The divs in the input set are similarly adjusted.
11692 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11696 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11697 * B_i G(divs') + c_i(divs))/n_i)
11699 * If bmap is not a rational map and if F(y) involves any denominators
11701 * x_i = (f_i y + h_i)/m_i
11703 * then additional constraints are added to ensure that we only
11704 * map back integer points. That is we enforce
11706 * f_i y + h_i = m_i alpha_i
11708 * with alpha_i an additional existentially quantified variable.
11710 * We first copy over the divs from "ma".
11711 * Then we add the modified constraints and divs from "bmap".
11712 * Finally, we add the stride constraints, if needed.
11714 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
11715 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
11716 __isl_take isl_multi_aff
*ma
)
11720 isl_basic_map
*res
= NULL
;
11721 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
11722 isl_int f
, c1
, c2
, g
;
11723 int rational
, strides
;
11730 ma
= isl_multi_aff_align_divs(ma
);
11733 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
11736 if (type
== isl_dim_in
) {
11738 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
11740 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
11743 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
11744 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
11746 space
= isl_multi_aff_get_domain_space(ma
);
11747 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
11748 rational
= isl_basic_map_is_rational(bmap
);
11749 strides
= rational
? 0 : multi_aff_strides(ma
);
11750 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
11751 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
11753 res
= isl_basic_map_set_rational(res
);
11755 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
11756 if (isl_basic_map_alloc_div(res
) < 0)
11759 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
11762 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11763 k
= isl_basic_map_alloc_equality(res
);
11766 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
11767 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11770 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11771 k
= isl_basic_map_alloc_inequality(res
);
11774 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
11775 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11778 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11779 if (isl_int_is_zero(bmap
->div
[i
][0])) {
11780 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
11783 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
11784 n_before
, n_after
, n_div_ma
, n_div_bmap
,
11789 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
11795 isl_basic_map_free(bmap
);
11796 isl_multi_aff_free(ma
);
11797 res
= isl_basic_set_simplify(res
);
11798 return isl_basic_map_finalize(res
);
11804 isl_basic_map_free(bmap
);
11805 isl_multi_aff_free(ma
);
11806 isl_basic_map_free(res
);
11810 /* Compute the preimage of "bset" under the function represented by "ma".
11811 * In other words, plug in "ma" in "bset". The result is a basic set
11812 * that lives in the domain space of "ma".
11814 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
11815 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
11817 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
11820 /* Compute the preimage of the domain of "bmap" under the function
11821 * represented by "ma".
11822 * In other words, plug in "ma" in the domain of "bmap".
11823 * The result is a basic map that lives in the same space as "bmap"
11824 * except that the domain has been replaced by the domain space of "ma".
11826 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
11827 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
11829 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
11832 /* Compute the preimage of the range of "bmap" under the function
11833 * represented by "ma".
11834 * In other words, plug in "ma" in the range of "bmap".
11835 * The result is a basic map that lives in the same space as "bmap"
11836 * except that the range has been replaced by the domain space of "ma".
11838 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
11839 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
11841 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
11844 /* Check if the range of "ma" is compatible with the domain or range
11845 * (depending on "type") of "map".
11846 * Return -1 if anything is wrong.
11848 static int check_map_compatible_range_multi_aff(
11849 __isl_keep isl_map
*map
, enum isl_dim_type type
,
11850 __isl_keep isl_multi_aff
*ma
)
11853 isl_space
*ma_space
;
11855 ma_space
= isl_multi_aff_get_space(ma
);
11856 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
11857 isl_space_free(ma_space
);
11859 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11860 "spaces don't match", return -1);
11864 /* Compute the preimage of the domain or range (depending on "type")
11865 * of "map" under the function represented by "ma".
11866 * In other words, plug in "ma" in the domain or range of "map".
11867 * The result is a map that lives in the same space as "map"
11868 * except that the domain or range has been replaced by
11869 * the domain space of "ma".
11871 * The parameters are assumed to have been aligned.
11873 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
11874 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11879 map
= isl_map_cow(map
);
11880 ma
= isl_multi_aff_align_divs(ma
);
11883 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
11886 for (i
= 0; i
< map
->n
; ++i
) {
11887 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
11888 isl_multi_aff_copy(ma
));
11893 space
= isl_multi_aff_get_domain_space(ma
);
11894 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
11896 isl_space_free(map
->dim
);
11901 isl_multi_aff_free(ma
);
11903 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
11904 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
11907 isl_multi_aff_free(ma
);
11912 /* Compute the preimage of the domain or range (depending on "type")
11913 * of "map" under the function represented by "ma".
11914 * In other words, plug in "ma" in the domain or range of "map".
11915 * The result is a map that lives in the same space as "map"
11916 * except that the domain or range has been replaced by
11917 * the domain space of "ma".
11919 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
11920 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11925 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
11926 return map_preimage_multi_aff(map
, type
, ma
);
11928 if (!isl_space_has_named_params(map
->dim
) ||
11929 !isl_space_has_named_params(ma
->space
))
11930 isl_die(map
->ctx
, isl_error_invalid
,
11931 "unaligned unnamed parameters", goto error
);
11932 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
11933 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
11935 return map_preimage_multi_aff(map
, type
, ma
);
11937 isl_multi_aff_free(ma
);
11938 return isl_map_free(map
);
11941 /* Compute the preimage of "set" under the function represented by "ma".
11942 * In other words, plug in "ma" "set". The result is a set
11943 * that lives in the domain space of "ma".
11945 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
11946 __isl_take isl_multi_aff
*ma
)
11948 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
11951 /* Compute the preimage of the domain of "map" under the function
11952 * represented by "ma".
11953 * In other words, plug in "ma" in the domain of "map".
11954 * The result is a map that lives in the same space as "map"
11955 * except that the domain has been replaced by the domain space of "ma".
11957 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
11958 __isl_take isl_multi_aff
*ma
)
11960 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
11963 /* Compute the preimage of "map" under the function represented by "pma".
11964 * In other words, plug in "pma" in the domain or range of "map".
11965 * The result is a map that lives in the same space as "map",
11966 * except that the space of type "type" has been replaced by
11967 * the domain space of "pma".
11969 * The parameters of "map" and "pma" are assumed to have been aligned.
11971 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
11972 __isl_take isl_map
*map
, enum isl_dim_type type
,
11973 __isl_take isl_pw_multi_aff
*pma
)
11982 isl_pw_multi_aff_free(pma
);
11983 res
= isl_map_empty(isl_map_get_space(map
));
11988 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
11989 isl_multi_aff_copy(pma
->p
[0].maff
));
11990 if (type
== isl_dim_in
)
11991 res
= isl_map_intersect_domain(res
,
11992 isl_map_copy(pma
->p
[0].set
));
11994 res
= isl_map_intersect_range(res
,
11995 isl_map_copy(pma
->p
[0].set
));
11997 for (i
= 1; i
< pma
->n
; ++i
) {
12000 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12001 isl_multi_aff_copy(pma
->p
[i
].maff
));
12002 if (type
== isl_dim_in
)
12003 res_i
= isl_map_intersect_domain(res_i
,
12004 isl_map_copy(pma
->p
[i
].set
));
12006 res_i
= isl_map_intersect_range(res_i
,
12007 isl_map_copy(pma
->p
[i
].set
));
12008 res
= isl_map_union(res
, res_i
);
12011 isl_pw_multi_aff_free(pma
);
12015 isl_pw_multi_aff_free(pma
);
12020 /* Compute the preimage of "map" under the function represented by "pma".
12021 * In other words, plug in "pma" in the domain or range of "map".
12022 * The result is a map that lives in the same space as "map",
12023 * except that the space of type "type" has been replaced by
12024 * the domain space of "pma".
12026 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12027 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12032 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12033 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12035 if (!isl_space_has_named_params(map
->dim
) ||
12036 !isl_space_has_named_params(pma
->dim
))
12037 isl_die(map
->ctx
, isl_error_invalid
,
12038 "unaligned unnamed parameters", goto error
);
12039 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12040 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12042 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12044 isl_pw_multi_aff_free(pma
);
12045 return isl_map_free(map
);
12048 /* Compute the preimage of "set" under the function represented by "pma".
12049 * In other words, plug in "pma" in "set". The result is a set
12050 * that lives in the domain space of "pma".
12052 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12053 __isl_take isl_pw_multi_aff
*pma
)
12055 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12058 /* Compute the preimage of "map" under the function represented by "pma".
12059 * In other words, plug in "pma" in the domain "map".
12060 * The result is a map that lives in the same space as "map",
12061 * except that domain space has been replaced by the domain space of "pma".
12063 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12064 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12066 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12069 /* Compute the preimage of "map" under the function represented by "mpa".
12070 * In other words, plug in "mpa" in the domain or range of "map".
12071 * The result is a map that lives in the same space as "map",
12072 * except that the space of type "type" has been replaced by
12073 * the domain space of "mpa".
12075 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12076 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12078 isl_pw_multi_aff
*pma
;
12080 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12081 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12084 /* Compute the preimage of "map" under the function represented by "mpa".
12085 * In other words, plug in "mpa" in the domain "map".
12086 * The result is a map that lives in the same space as "map",
12087 * except that domain space has been replaced by the domain space of "mpa".
12089 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12090 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12092 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12095 /* Compute the preimage of "set" by the function represented by "mpa".
12096 * In other words, plug in "mpa" in "set".
12098 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12099 __isl_take isl_multi_pw_aff
*mpa
)
12101 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);