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"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_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>
37 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
40 case isl_dim_param
: return dim
->nparam
;
41 case isl_dim_in
: return dim
->n_in
;
42 case isl_dim_out
: return dim
->n_out
;
43 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
48 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
51 case isl_dim_param
: return 1;
52 case isl_dim_in
: return 1 + dim
->nparam
;
53 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
59 enum isl_dim_type type
)
64 case isl_dim_cst
: return 1;
67 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
68 case isl_dim_div
: return bmap
->n_div
;
69 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
74 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
76 return map
? n(map
->dim
, type
) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
81 return set
? n(set
->dim
, type
) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
85 enum isl_dim_type type
)
87 isl_space
*dim
= bmap
->dim
;
89 case isl_dim_cst
: return 0;
90 case isl_dim_param
: return 1;
91 case isl_dim_in
: return 1 + dim
->nparam
;
92 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
93 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
98 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
99 enum isl_dim_type type
)
101 return isl_basic_map_offset(bset
, type
);
104 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
106 return pos(map
->dim
, type
);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
110 enum isl_dim_type type
)
112 return isl_basic_map_dim(bset
, type
);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
117 return isl_basic_set_dim(bset
, isl_dim_set
);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
122 return isl_basic_set_dim(bset
, isl_dim_param
);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
129 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
132 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_set
);
137 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
139 return isl_set_dim(set
, isl_dim_param
);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_in
: 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->n_out
: 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->dim
->nparam
: 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
159 return bmap
? bmap
->n_div
: 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
164 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
167 unsigned isl_map_n_in(const struct isl_map
*map
)
169 return map
? map
->dim
->n_in
: 0;
172 unsigned isl_map_n_out(const struct isl_map
*map
)
174 return map
? map
->dim
->n_out
: 0;
177 unsigned isl_map_n_param(const struct isl_map
*map
)
179 return map
? map
->dim
->nparam
: 0;
182 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
187 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
190 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
193 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
194 struct isl_basic_set
*bset
)
199 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
202 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
205 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
210 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
213 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
216 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
217 struct isl_basic_set
*bset
)
222 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
225 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
228 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
230 return bmap
? bmap
->ctx
: NULL
;
233 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
235 return bset
? bset
->ctx
: NULL
;
238 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
240 return map
? map
->ctx
: NULL
;
243 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
245 return set
? set
->ctx
: NULL
;
248 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
252 return isl_space_copy(bmap
->dim
);
255 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
259 return isl_space_copy(bset
->dim
);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
275 ctx
= isl_basic_map_get_ctx(bmap
);
276 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
277 cols
= 1 + 1 + total
+ bmap
->n_div
;
278 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
282 for (i
= 0; i
< bmap
->n_div
; ++i
)
283 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
288 /* Extract the divs in "bset" as a matrix.
290 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
292 return isl_basic_map_get_divs(bset
);
295 __isl_give isl_local_space
*isl_basic_map_get_local_space(
296 __isl_keep isl_basic_map
*bmap
)
303 div
= isl_basic_map_get_divs(bmap
);
304 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
307 __isl_give isl_local_space
*isl_basic_set_get_local_space(
308 __isl_keep isl_basic_set
*bset
)
310 return isl_basic_map_get_local_space(bset
);
313 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
314 __isl_take isl_local_space
*ls
)
323 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
324 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
325 n_div
, 0, 2 * n_div
);
327 for (i
= 0; i
< n_div
; ++i
)
328 if (isl_basic_map_alloc_div(bmap
) < 0)
331 for (i
= 0; i
< n_div
; ++i
) {
332 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
333 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
337 isl_local_space_free(ls
);
340 isl_local_space_free(ls
);
341 isl_basic_map_free(bmap
);
345 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
346 __isl_take isl_local_space
*ls
)
348 return isl_basic_map_from_local_space(ls
);
351 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
355 return isl_space_copy(map
->dim
);
358 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
362 return isl_space_copy(set
->dim
);
365 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
366 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
368 bmap
= isl_basic_map_cow(bmap
);
371 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
374 bmap
= isl_basic_map_finalize(bmap
);
377 isl_basic_map_free(bmap
);
381 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
382 __isl_take isl_basic_set
*bset
, const char *s
)
384 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
387 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
388 enum isl_dim_type type
)
390 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
393 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
394 enum isl_dim_type type
, const char *s
)
398 map
= isl_map_cow(map
);
402 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
406 for (i
= 0; i
< map
->n
; ++i
) {
407 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
418 /* Does the input or output tuple have a name?
420 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
422 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
425 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
426 enum isl_dim_type type
)
428 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
431 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
434 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
437 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
438 enum isl_dim_type type
, __isl_take isl_id
*id
)
440 map
= isl_map_cow(map
);
442 return isl_id_free(id
);
444 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
446 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
449 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
450 __isl_take isl_id
*id
)
452 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
455 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
456 enum isl_dim_type type
)
458 map
= isl_map_cow(map
);
462 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
464 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
467 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
469 return isl_map_reset_tuple_id(set
, isl_dim_set
);
472 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
474 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
477 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
478 enum isl_dim_type type
)
480 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
483 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
485 return isl_map_has_tuple_id(set
, isl_dim_set
);
488 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
490 return isl_map_get_tuple_id(set
, isl_dim_set
);
493 /* Does the set tuple have a name?
495 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
497 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
501 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
503 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
506 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
508 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
511 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
512 enum isl_dim_type type
, unsigned pos
)
514 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
517 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
518 enum isl_dim_type type
, unsigned pos
)
520 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
523 /* Does the given dimension have a name?
525 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
526 enum isl_dim_type type
, unsigned pos
)
528 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
531 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
532 enum isl_dim_type type
, unsigned pos
)
534 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
537 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
538 enum isl_dim_type type
, unsigned pos
)
540 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
543 /* Does the given dimension have a name?
545 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
546 enum isl_dim_type type
, unsigned pos
)
548 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
551 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
552 __isl_take isl_basic_map
*bmap
,
553 enum isl_dim_type type
, unsigned pos
, const char *s
)
555 bmap
= isl_basic_map_cow(bmap
);
558 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
561 return isl_basic_map_finalize(bmap
);
563 isl_basic_map_free(bmap
);
567 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
568 enum isl_dim_type type
, unsigned pos
, const char *s
)
572 map
= isl_map_cow(map
);
576 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
580 for (i
= 0; i
< map
->n
; ++i
) {
581 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
592 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
593 __isl_take isl_basic_set
*bset
,
594 enum isl_dim_type type
, unsigned pos
, const char *s
)
596 return (isl_basic_set
*)isl_basic_map_set_dim_name(
597 (isl_basic_map
*)bset
, type
, pos
, s
);
600 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
601 enum isl_dim_type type
, unsigned pos
, const char *s
)
603 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
606 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
607 enum isl_dim_type type
, unsigned pos
)
609 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
612 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
613 enum isl_dim_type type
, unsigned pos
)
615 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
618 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
619 enum isl_dim_type type
, unsigned pos
)
621 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
624 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
625 enum isl_dim_type type
, unsigned pos
)
627 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
630 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
631 enum isl_dim_type type
, unsigned pos
)
633 return isl_map_has_dim_id(set
, type
, pos
);
636 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
637 enum isl_dim_type type
, unsigned pos
)
639 return isl_map_get_dim_id(set
, type
, pos
);
642 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
643 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
645 map
= isl_map_cow(map
);
647 return isl_id_free(id
);
649 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
651 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
654 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
655 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
657 return isl_map_set_dim_id(set
, type
, pos
, id
);
660 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
661 __isl_keep isl_id
*id
)
665 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
668 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
669 __isl_keep isl_id
*id
)
671 return isl_map_find_dim_by_id(set
, type
, id
);
674 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
679 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
682 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
685 return isl_map_find_dim_by_name(set
, type
, name
);
688 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
692 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
695 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
697 return isl_basic_map_is_rational(bset
);
700 /* Does "bmap" contain any rational points?
702 * If "bmap" has an equality for each dimension, equating the dimension
703 * to an integer constant, then it has no rational points, even if it
704 * is marked as rational.
706 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
708 int has_rational
= 1;
713 if (isl_basic_map_plain_is_empty(bmap
))
715 if (!isl_basic_map_is_rational(bmap
))
717 bmap
= isl_basic_map_copy(bmap
);
718 bmap
= isl_basic_map_implicit_equalities(bmap
);
721 total
= isl_basic_map_total_dim(bmap
);
722 if (bmap
->n_eq
== total
) {
724 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
725 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
728 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
729 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
731 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
739 isl_basic_map_free(bmap
);
744 /* Does "map" contain any rational points?
746 int isl_map_has_rational(__isl_keep isl_map
*map
)
753 for (i
= 0; i
< map
->n
; ++i
) {
754 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
755 if (has_rational
< 0)
763 /* Does "set" contain any rational points?
765 int isl_set_has_rational(__isl_keep isl_set
*set
)
767 return isl_map_has_rational(set
);
770 /* Is this basic set a parameter domain?
772 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
776 return isl_space_is_params(bset
->dim
);
779 /* Is this set a parameter domain?
781 int isl_set_is_params(__isl_keep isl_set
*set
)
785 return isl_space_is_params(set
->dim
);
788 /* Is this map actually a parameter domain?
789 * Users should never call this function. Outside of isl,
790 * a map can never be a parameter domain.
792 int isl_map_is_params(__isl_keep isl_map
*map
)
796 return isl_space_is_params(map
->dim
);
799 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
800 struct isl_basic_map
*bmap
, unsigned extra
,
801 unsigned n_eq
, unsigned n_ineq
)
804 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
809 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
810 if (isl_blk_is_error(bmap
->block
))
813 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
818 bmap
->block2
= isl_blk_empty();
821 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
822 if (isl_blk_is_error(bmap
->block2
))
825 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
830 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
831 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
833 for (i
= 0; i
< extra
; ++i
)
834 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
838 bmap
->c_size
= n_eq
+ n_ineq
;
839 bmap
->eq
= bmap
->ineq
+ n_ineq
;
848 isl_basic_map_free(bmap
);
852 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
853 unsigned nparam
, unsigned dim
, unsigned extra
,
854 unsigned n_eq
, unsigned n_ineq
)
856 struct isl_basic_map
*bmap
;
859 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
863 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
864 return (struct isl_basic_set
*)bmap
;
867 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
868 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
870 struct isl_basic_map
*bmap
;
873 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
874 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
875 return (struct isl_basic_set
*)bmap
;
881 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
882 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
884 struct isl_basic_map
*bmap
;
888 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
893 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
899 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
900 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
901 unsigned n_eq
, unsigned n_ineq
)
903 struct isl_basic_map
*bmap
;
906 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
910 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
914 static void dup_constraints(
915 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
918 unsigned total
= isl_basic_map_total_dim(src
);
920 for (i
= 0; i
< src
->n_eq
; ++i
) {
921 int j
= isl_basic_map_alloc_equality(dst
);
922 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
925 for (i
= 0; i
< src
->n_ineq
; ++i
) {
926 int j
= isl_basic_map_alloc_inequality(dst
);
927 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
930 for (i
= 0; i
< src
->n_div
; ++i
) {
931 int j
= isl_basic_map_alloc_div(dst
);
932 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
934 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
937 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
939 struct isl_basic_map
*dup
;
943 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
944 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
947 dup_constraints(dup
, bmap
);
948 dup
->flags
= bmap
->flags
;
949 dup
->sample
= isl_vec_copy(bmap
->sample
);
953 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
955 struct isl_basic_map
*dup
;
957 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
958 return (struct isl_basic_set
*)dup
;
961 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
966 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
970 return isl_basic_set_dup(bset
);
973 struct isl_set
*isl_set_copy(struct isl_set
*set
)
982 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
987 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
991 bmap
= isl_basic_map_dup(bmap
);
993 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
997 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1006 void *isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1011 if (--bmap
->ref
> 0)
1014 isl_ctx_deref(bmap
->ctx
);
1016 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1018 isl_blk_free(bmap
->ctx
, bmap
->block
);
1019 isl_vec_free(bmap
->sample
);
1020 isl_space_free(bmap
->dim
);
1026 void *isl_basic_set_free(struct isl_basic_set
*bset
)
1028 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1031 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1033 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1036 __isl_give isl_map
*isl_map_align_params_map_map_and(
1037 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1038 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1039 __isl_take isl_map
*map2
))
1043 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1044 return fn(map1
, map2
);
1045 if (!isl_space_has_named_params(map1
->dim
) ||
1046 !isl_space_has_named_params(map2
->dim
))
1047 isl_die(map1
->ctx
, isl_error_invalid
,
1048 "unaligned unnamed parameters", goto error
);
1049 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1050 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1051 return fn(map1
, map2
);
1058 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1059 __isl_keep isl_map
*map2
,
1060 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1066 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1067 return fn(map1
, map2
);
1068 if (!isl_space_has_named_params(map1
->dim
) ||
1069 !isl_space_has_named_params(map2
->dim
))
1070 isl_die(map1
->ctx
, isl_error_invalid
,
1071 "unaligned unnamed parameters", return -1);
1072 map1
= isl_map_copy(map1
);
1073 map2
= isl_map_copy(map2
);
1074 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1075 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1082 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1084 struct isl_ctx
*ctx
;
1088 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1089 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1091 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1092 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1093 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1094 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1095 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1096 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1098 int j
= isl_basic_map_alloc_inequality(bmap
);
1102 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1103 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1110 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1111 bmap
->extra
- bmap
->n_div
);
1112 return bmap
->n_eq
++;
1115 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1117 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1120 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1124 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1129 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1131 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1134 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1139 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1141 if (pos
!= bmap
->n_eq
- 1) {
1143 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1144 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1150 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1152 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1155 /* Turn inequality "pos" of "bmap" into an equality.
1157 * In particular, we move the inequality in front of the equalities
1158 * and move the last inequality in the position of the moved inequality.
1159 * Note that isl_tab_make_equalities_explicit depends on this particular
1160 * change in the ordering of the constraints.
1162 void isl_basic_map_inequality_to_equality(
1163 struct isl_basic_map
*bmap
, unsigned pos
)
1167 t
= bmap
->ineq
[pos
];
1168 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1169 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1174 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1175 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1176 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1177 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1180 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1182 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1185 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1187 struct isl_ctx
*ctx
;
1191 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1192 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1193 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1194 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1195 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1196 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1197 1 + isl_basic_map_total_dim(bmap
),
1198 bmap
->extra
- bmap
->n_div
);
1199 return bmap
->n_ineq
++;
1202 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1204 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1207 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1211 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1216 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1218 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1221 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1226 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1228 if (pos
!= bmap
->n_ineq
- 1) {
1229 t
= bmap
->ineq
[pos
];
1230 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1231 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1232 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1238 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1240 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1243 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1248 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1251 k
= isl_basic_map_alloc_equality(bmap
);
1254 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1257 isl_basic_map_free(bmap
);
1261 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1264 return (isl_basic_set
*)
1265 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1268 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1273 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1276 k
= isl_basic_map_alloc_inequality(bmap
);
1279 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1282 isl_basic_map_free(bmap
);
1286 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1289 return (isl_basic_set
*)
1290 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1293 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1297 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1298 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1299 1 + 1 + isl_basic_map_total_dim(bmap
),
1300 bmap
->extra
- bmap
->n_div
);
1301 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1302 return bmap
->n_div
++;
1305 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1307 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1310 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1314 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1319 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1321 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1324 /* Copy constraint from src to dst, putting the vars of src at offset
1325 * dim_off in dst and the divs of src at offset div_off in dst.
1326 * If both sets are actually map, then dim_off applies to the input
1329 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1330 struct isl_basic_map
*src_map
, isl_int
*src
,
1331 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1333 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1334 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1335 unsigned src_in
= isl_basic_map_n_in(src_map
);
1336 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1337 unsigned src_out
= isl_basic_map_n_out(src_map
);
1338 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1339 isl_int_set(dst
[0], src
[0]);
1340 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1341 if (dst_nparam
> src_nparam
)
1342 isl_seq_clr(dst
+1+src_nparam
,
1343 dst_nparam
- src_nparam
);
1344 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1345 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1347 isl_min(dst_in
-in_off
, src_in
));
1348 if (dst_in
-in_off
> src_in
)
1349 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1350 dst_in
- in_off
- src_in
);
1351 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1352 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1353 src
+1+src_nparam
+src_in
,
1354 isl_min(dst_out
-out_off
, src_out
));
1355 if (dst_out
-out_off
> src_out
)
1356 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1357 dst_out
- out_off
- src_out
);
1358 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1359 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1360 src
+1+src_nparam
+src_in
+src_out
,
1361 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1362 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1363 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1364 div_off
+src_map
->n_div
,
1365 dst_map
->n_div
- div_off
- src_map
->n_div
);
1368 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1369 struct isl_basic_map
*src_map
, isl_int
*src
,
1370 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1372 isl_int_set(dst
[0], src
[0]);
1373 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1376 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1377 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1382 if (!bmap1
|| !bmap2
)
1385 div_off
= bmap1
->n_div
;
1387 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1388 int i1
= isl_basic_map_alloc_equality(bmap1
);
1391 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1392 i_pos
, o_pos
, div_off
);
1395 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1396 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1399 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1400 i_pos
, o_pos
, div_off
);
1403 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1404 int i1
= isl_basic_map_alloc_div(bmap1
);
1407 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1408 i_pos
, o_pos
, div_off
);
1411 isl_basic_map_free(bmap2
);
1416 isl_basic_map_free(bmap1
);
1417 isl_basic_map_free(bmap2
);
1421 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1422 struct isl_basic_set
*bset2
, unsigned pos
)
1424 return (struct isl_basic_set
*)
1425 add_constraints((struct isl_basic_map
*)bset1
,
1426 (struct isl_basic_map
*)bset2
, 0, pos
);
1429 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1430 __isl_take isl_space
*dim
, unsigned extra
,
1431 unsigned n_eq
, unsigned n_ineq
)
1433 struct isl_basic_map
*ext
;
1443 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1444 base
->extra
>= base
->n_div
+ extra
;
1446 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1447 room_for_ineq(base
, n_ineq
)) {
1448 isl_space_free(dim
);
1452 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1453 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1454 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1455 extra
+= base
->extra
;
1457 n_ineq
+= base
->n_ineq
;
1459 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1465 ext
->sample
= isl_vec_copy(base
->sample
);
1466 flags
= base
->flags
;
1467 ext
= add_constraints(ext
, base
, 0, 0);
1470 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1476 isl_space_free(dim
);
1477 isl_basic_map_free(base
);
1481 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1482 __isl_take isl_space
*dim
, unsigned extra
,
1483 unsigned n_eq
, unsigned n_ineq
)
1485 return (struct isl_basic_set
*)
1486 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1487 extra
, n_eq
, n_ineq
);
1490 struct isl_basic_map
*isl_basic_map_extend_constraints(
1491 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1495 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1499 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1500 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1501 unsigned n_eq
, unsigned n_ineq
)
1503 struct isl_basic_map
*bmap
;
1508 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1512 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1515 isl_basic_map_free(base
);
1519 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1520 unsigned nparam
, unsigned dim
, unsigned extra
,
1521 unsigned n_eq
, unsigned n_ineq
)
1523 return (struct isl_basic_set
*)
1524 isl_basic_map_extend((struct isl_basic_map
*)base
,
1525 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1528 struct isl_basic_set
*isl_basic_set_extend_constraints(
1529 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1531 return (struct isl_basic_set
*)
1532 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1536 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1538 return (struct isl_basic_set
*)
1539 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1542 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1547 if (bmap
->ref
> 1) {
1549 bmap
= isl_basic_map_dup(bmap
);
1552 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1556 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1564 return isl_set_dup(set
);
1567 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1575 return isl_map_dup(map
);
1578 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1579 unsigned a_len
, unsigned b_len
)
1581 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1582 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1583 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1586 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1587 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1595 isl_assert(bmap
->ctx
,
1596 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1598 if (n1
== 0 || n2
== 0)
1601 bmap
= isl_basic_map_cow(bmap
);
1605 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1606 if (isl_blk_is_error(blk
))
1609 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1611 bmap
->eq
[i
] + pos
, n1
, n2
);
1613 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1615 bmap
->ineq
[i
] + pos
, n1
, n2
);
1617 for (i
= 0; i
< bmap
->n_div
; ++i
)
1619 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1621 isl_blk_free(bmap
->ctx
, blk
);
1623 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1624 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1625 return isl_basic_map_finalize(bmap
);
1627 isl_basic_map_free(bmap
);
1631 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1632 __isl_take isl_basic_set
*bset
, unsigned n
)
1640 nparam
= isl_basic_set_n_param(bset
);
1641 dim
= isl_basic_set_n_dim(bset
);
1642 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1644 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1646 isl_basic_set_free(bset
);
1650 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1656 total
= isl_basic_map_total_dim(bmap
);
1657 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1658 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1660 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1662 i
= isl_basic_map_alloc_equality(bmap
);
1666 isl_int_set_si(bmap
->eq
[i
][0], 1);
1667 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1668 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1669 isl_vec_free(bmap
->sample
);
1670 bmap
->sample
= NULL
;
1671 return isl_basic_map_finalize(bmap
);
1673 isl_basic_map_free(bmap
);
1677 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1679 return (struct isl_basic_set
*)
1680 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1683 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1686 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1688 isl_int
*t
= bmap
->div
[a
];
1689 bmap
->div
[a
] = bmap
->div
[b
];
1693 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1694 * div definitions accordingly.
1696 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1699 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1701 swap_div(bmap
, a
, b
);
1703 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1704 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1706 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1707 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1709 for (i
= 0; i
< bmap
->n_div
; ++i
)
1710 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1711 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1714 /* Eliminate the specified n dimensions starting at first from the
1715 * constraints, without removing the dimensions from the space.
1716 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1718 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1719 enum isl_dim_type type
, unsigned first
, unsigned n
)
1728 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1729 isl_die(map
->ctx
, isl_error_invalid
,
1730 "index out of bounds", goto error
);
1732 map
= isl_map_cow(map
);
1736 for (i
= 0; i
< map
->n
; ++i
) {
1737 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1747 /* Eliminate the specified n dimensions starting at first from the
1748 * constraints, without removing the dimensions from the space.
1749 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1751 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1752 enum isl_dim_type type
, unsigned first
, unsigned n
)
1754 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1757 /* Eliminate the specified n dimensions starting at first from the
1758 * constraints, without removing the dimensions from the space.
1759 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1761 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1762 unsigned first
, unsigned n
)
1764 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1767 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1768 __isl_take isl_basic_map
*bmap
)
1772 bmap
= isl_basic_map_eliminate_vars(bmap
,
1773 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1777 return isl_basic_map_finalize(bmap
);
1780 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1781 __isl_take isl_basic_set
*bset
)
1783 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1784 (struct isl_basic_map
*)bset
);
1787 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1796 map
= isl_map_cow(map
);
1800 for (i
= 0; i
< map
->n
; ++i
) {
1801 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1811 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1813 return isl_map_remove_divs(set
);
1816 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1817 enum isl_dim_type type
, unsigned first
, unsigned n
)
1821 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1823 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1825 bmap
= isl_basic_map_eliminate_vars(bmap
,
1826 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1829 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1831 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1834 isl_basic_map_free(bmap
);
1838 /* Return true if the definition of the given div (recursively) involves
1839 * any of the given variables.
1841 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1842 unsigned first
, unsigned n
)
1845 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1847 if (isl_int_is_zero(bmap
->div
[div
][0]))
1849 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1852 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1853 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1855 if (div_involves_vars(bmap
, i
, first
, n
))
1862 /* Try and add a lower and/or upper bound on "div" to "bmap"
1863 * based on inequality "i".
1864 * "total" is the total number of variables (excluding the divs).
1865 * "v" is a temporary object that can be used during the calculations.
1866 * If "lb" is set, then a lower bound should be constructed.
1867 * If "ub" is set, then an upper bound should be constructed.
1869 * The calling function has already checked that the inequality does not
1870 * reference "div", but we still need to check that the inequality is
1871 * of the right form. We'll consider the case where we want to construct
1872 * a lower bound. The construction of upper bounds is similar.
1874 * Let "div" be of the form
1876 * q = floor((a + f(x))/d)
1878 * We essentially check if constraint "i" is of the form
1882 * so that we can use it to derive a lower bound on "div".
1883 * However, we allow a slightly more general form
1887 * with the condition that the coefficients of g(x) - f(x) are all
1889 * Rewriting this constraint as
1893 * adding a + f(x) to both sides and dividing by d, we obtain
1895 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1897 * Taking the floor on both sides, we obtain
1899 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1903 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1905 * In the case of an upper bound, we construct the constraint
1907 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1910 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1911 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1912 unsigned total
, isl_int v
, int lb
, int ub
)
1916 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1918 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1919 bmap
->div
[div
][1 + 1 + j
]);
1920 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1923 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1924 bmap
->div
[div
][1 + 1 + j
]);
1925 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1931 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1933 int k
= isl_basic_map_alloc_inequality(bmap
);
1936 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1937 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1938 bmap
->div
[div
][1 + j
]);
1939 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1940 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1942 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1945 int k
= isl_basic_map_alloc_inequality(bmap
);
1948 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1949 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1950 bmap
->div
[div
][1 + j
]);
1951 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
1952 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1954 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
1959 isl_basic_map_free(bmap
);
1963 /* This function is called right before "div" is eliminated from "bmap"
1964 * using Fourier-Motzkin.
1965 * Look through the constraints of "bmap" for constraints on the argument
1966 * of the integer division and use them to construct constraints on the
1967 * integer division itself. These constraints can then be combined
1968 * during the Fourier-Motzkin elimination.
1969 * Note that it is only useful to introduce lower bounds on "div"
1970 * if "bmap" already contains upper bounds on "div" as the newly
1971 * introduce lower bounds can then be combined with the pre-existing
1972 * upper bounds. Similarly for upper bounds.
1973 * We therefore first check if "bmap" contains any lower and/or upper bounds
1976 * It is interesting to note that the introduction of these constraints
1977 * can indeed lead to more accurate results, even when compared to
1978 * deriving constraints on the argument of "div" from constraints on "div".
1979 * Consider, for example, the set
1981 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1983 * The second constraint can be rewritten as
1985 * 2 * [(-i-2j+3)/4] + k >= 0
1987 * from which we can derive
1989 * -i - 2j + 3 >= -2k
1995 * Combined with the first constraint, we obtain
1997 * -3 <= 3 + 2k or k >= -3
1999 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2000 * the first constraint, we obtain
2002 * [(i + 2j)/4] >= [-3/4] = -1
2004 * Combining this constraint with the second constraint, we obtain
2008 static __isl_give isl_basic_map
*insert_bounds_on_div(
2009 __isl_take isl_basic_map
*bmap
, int div
)
2012 int check_lb
, check_ub
;
2019 if (isl_int_is_zero(bmap
->div
[div
][0]))
2022 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2026 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2027 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2034 if (!check_lb
&& !check_ub
)
2039 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2040 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2043 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2044 check_lb
, check_ub
);
2052 /* Remove all divs (recursively) involving any of the given dimensions
2053 * in their definitions.
2055 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2056 __isl_take isl_basic_map
*bmap
,
2057 enum isl_dim_type type
, unsigned first
, unsigned n
)
2063 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2065 first
+= isl_basic_map_offset(bmap
, type
);
2067 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2068 if (!div_involves_vars(bmap
, i
, first
, n
))
2070 bmap
= insert_bounds_on_div(bmap
, i
);
2071 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2079 isl_basic_map_free(bmap
);
2083 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2084 __isl_take isl_basic_set
*bset
,
2085 enum isl_dim_type type
, unsigned first
, unsigned n
)
2087 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2090 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2091 enum isl_dim_type type
, unsigned first
, unsigned n
)
2100 map
= isl_map_cow(map
);
2104 for (i
= 0; i
< map
->n
; ++i
) {
2105 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2116 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2117 enum isl_dim_type type
, unsigned first
, unsigned n
)
2119 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2123 /* Does the desciption of "bmap" depend on the specified dimensions?
2124 * We also check whether the dimensions appear in any of the div definitions.
2125 * In principle there is no need for this check. If the dimensions appear
2126 * in a div definition, they also appear in the defining constraints of that
2129 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2130 enum isl_dim_type type
, unsigned first
, unsigned n
)
2137 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2138 isl_die(bmap
->ctx
, isl_error_invalid
,
2139 "index out of bounds", return -1);
2141 first
+= isl_basic_map_offset(bmap
, type
);
2142 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2143 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2145 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2146 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2148 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2149 if (isl_int_is_zero(bmap
->div
[i
][0]))
2151 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2158 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2159 enum isl_dim_type type
, unsigned first
, unsigned n
)
2166 if (first
+ n
> isl_map_dim(map
, type
))
2167 isl_die(map
->ctx
, isl_error_invalid
,
2168 "index out of bounds", return -1);
2170 for (i
= 0; i
< map
->n
; ++i
) {
2171 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2173 if (involves
< 0 || involves
)
2180 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2181 enum isl_dim_type type
, unsigned first
, unsigned n
)
2183 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2186 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2187 enum isl_dim_type type
, unsigned first
, unsigned n
)
2189 return isl_map_involves_dims(set
, type
, first
, n
);
2192 /* Return true if the definition of the given div is unknown or depends
2195 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2198 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2200 if (isl_int_is_zero(bmap
->div
[div
][0]))
2203 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2204 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2206 if (div_is_unknown(bmap
, i
))
2213 /* Remove all divs that are unknown or defined in terms of unknown divs.
2215 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2216 __isl_take isl_basic_map
*bmap
)
2223 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2224 if (!div_is_unknown(bmap
, i
))
2226 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2235 /* Remove all divs that are unknown or defined in terms of unknown divs.
2237 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2238 __isl_take isl_basic_set
*bset
)
2240 return isl_basic_map_remove_unknown_divs(bset
);
2243 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2252 map
= isl_map_cow(map
);
2256 for (i
= 0; i
< map
->n
; ++i
) {
2257 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2267 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2269 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2272 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2273 __isl_take isl_basic_set
*bset
,
2274 enum isl_dim_type type
, unsigned first
, unsigned n
)
2276 return (isl_basic_set
*)
2277 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2280 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2281 enum isl_dim_type type
, unsigned first
, unsigned n
)
2288 map
= isl_map_cow(map
);
2291 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2293 for (i
= 0; i
< map
->n
; ++i
) {
2294 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2295 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2299 map
= isl_map_drop(map
, type
, first
, n
);
2306 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2307 enum isl_dim_type type
, unsigned first
, unsigned n
)
2309 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2312 /* Project out n inputs starting at first using Fourier-Motzkin */
2313 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2314 unsigned first
, unsigned n
)
2316 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2319 static void dump_term(struct isl_basic_map
*bmap
,
2320 isl_int c
, int pos
, FILE *out
)
2323 unsigned in
= isl_basic_map_n_in(bmap
);
2324 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2325 unsigned nparam
= isl_basic_map_n_param(bmap
);
2327 isl_int_print(out
, c
, 0);
2329 if (!isl_int_is_one(c
))
2330 isl_int_print(out
, c
, 0);
2331 if (pos
< 1 + nparam
) {
2332 name
= isl_space_get_dim_name(bmap
->dim
,
2333 isl_dim_param
, pos
- 1);
2335 fprintf(out
, "%s", name
);
2337 fprintf(out
, "p%d", pos
- 1);
2338 } else if (pos
< 1 + nparam
+ in
)
2339 fprintf(out
, "i%d", pos
- 1 - nparam
);
2340 else if (pos
< 1 + nparam
+ dim
)
2341 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2343 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2347 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2348 int sign
, FILE *out
)
2352 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2356 for (i
= 0, first
= 1; i
< len
; ++i
) {
2357 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2360 fprintf(out
, " + ");
2362 isl_int_abs(v
, c
[i
]);
2363 dump_term(bmap
, v
, i
, out
);
2370 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2371 const char *op
, FILE *out
, int indent
)
2375 fprintf(out
, "%*s", indent
, "");
2377 dump_constraint_sign(bmap
, c
, 1, out
);
2378 fprintf(out
, " %s ", op
);
2379 dump_constraint_sign(bmap
, c
, -1, out
);
2383 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2384 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2386 fprintf(out
, "%*s", indent
, "");
2387 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2392 static void dump_constraints(struct isl_basic_map
*bmap
,
2393 isl_int
**c
, unsigned n
,
2394 const char *op
, FILE *out
, int indent
)
2398 for (i
= 0; i
< n
; ++i
)
2399 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2402 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2406 unsigned total
= isl_basic_map_total_dim(bmap
);
2408 for (j
= 0; j
< 1 + total
; ++j
) {
2409 if (isl_int_is_zero(exp
[j
]))
2411 if (!first
&& isl_int_is_pos(exp
[j
]))
2413 dump_term(bmap
, exp
[j
], j
, out
);
2418 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2422 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2423 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2425 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2426 fprintf(out
, "%*s", indent
, "");
2427 fprintf(out
, "e%d = [(", i
);
2428 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2430 isl_int_print(out
, bmap
->div
[i
][0], 0);
2431 fprintf(out
, "]\n");
2435 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2436 FILE *out
, int indent
)
2439 fprintf(out
, "null basic set\n");
2443 fprintf(out
, "%*s", indent
, "");
2444 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2445 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2446 bset
->extra
, bset
->flags
);
2447 dump((struct isl_basic_map
*)bset
, out
, indent
);
2450 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2451 FILE *out
, int indent
)
2454 fprintf(out
, "null basic map\n");
2458 fprintf(out
, "%*s", indent
, "");
2459 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2460 "flags: %x, n_name: %d\n",
2462 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2463 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2464 dump(bmap
, out
, indent
);
2467 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2472 total
= isl_basic_map_total_dim(bmap
);
2473 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2474 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2475 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2476 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2480 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2483 struct isl_set
*set
;
2487 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2488 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2489 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2490 sizeof(struct isl_set
) +
2491 (n
- 1) * sizeof(struct isl_basic_set
*));
2495 set
->ctx
= dim
->ctx
;
2496 isl_ctx_ref(set
->ctx
);
2504 isl_space_free(dim
);
2508 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2509 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2511 struct isl_set
*set
;
2514 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2518 set
= isl_set_alloc_space(dims
, n
, flags
);
2522 /* Make sure "map" has room for at least "n" more basic maps.
2524 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2527 struct isl_map
*grown
= NULL
;
2531 isl_assert(map
->ctx
, n
>= 0, goto error
);
2532 if (map
->n
+ n
<= map
->size
)
2534 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2537 for (i
= 0; i
< map
->n
; ++i
) {
2538 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2546 isl_map_free(grown
);
2551 /* Make sure "set" has room for at least "n" more basic sets.
2553 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2555 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2558 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2561 struct isl_set
*dup
;
2566 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2569 for (i
= 0; i
< set
->n
; ++i
)
2570 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2574 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2576 return isl_map_from_basic_map(bset
);
2579 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2581 struct isl_map
*map
;
2586 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2587 return isl_map_add_basic_map(map
, bmap
);
2590 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2591 __isl_take isl_basic_set
*bset
)
2593 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2594 (struct isl_basic_map
*)bset
);
2597 void *isl_set_free(__isl_take isl_set
*set
)
2607 isl_ctx_deref(set
->ctx
);
2608 for (i
= 0; i
< set
->n
; ++i
)
2609 isl_basic_set_free(set
->p
[i
]);
2610 isl_space_free(set
->dim
);
2616 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2621 fprintf(out
, "null set\n");
2625 fprintf(out
, "%*s", indent
, "");
2626 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2627 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2629 for (i
= 0; i
< set
->n
; ++i
) {
2630 fprintf(out
, "%*s", indent
, "");
2631 fprintf(out
, "basic set %d:\n", i
);
2632 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2636 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2641 fprintf(out
, "null map\n");
2645 fprintf(out
, "%*s", indent
, "");
2646 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2647 "flags: %x, n_name: %d\n",
2648 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2649 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2650 for (i
= 0; i
< map
->n
; ++i
) {
2651 fprintf(out
, "%*s", indent
, "");
2652 fprintf(out
, "basic map %d:\n", i
);
2653 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2657 struct isl_basic_map
*isl_basic_map_intersect_domain(
2658 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2660 struct isl_basic_map
*bmap_domain
;
2665 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2666 bset
->dim
, isl_dim_param
), goto error
);
2668 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2669 isl_assert(bset
->ctx
,
2670 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2672 bmap
= isl_basic_map_cow(bmap
);
2675 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2676 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2677 bmap_domain
= isl_basic_map_from_domain(bset
);
2678 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2680 bmap
= isl_basic_map_simplify(bmap
);
2681 return isl_basic_map_finalize(bmap
);
2683 isl_basic_map_free(bmap
);
2684 isl_basic_set_free(bset
);
2688 struct isl_basic_map
*isl_basic_map_intersect_range(
2689 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2691 struct isl_basic_map
*bmap_range
;
2696 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2697 bset
->dim
, isl_dim_param
), goto error
);
2699 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2700 isl_assert(bset
->ctx
,
2701 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2703 if (isl_basic_set_is_universe(bset
)) {
2704 isl_basic_set_free(bset
);
2708 bmap
= isl_basic_map_cow(bmap
);
2711 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2712 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2713 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2714 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2716 bmap
= isl_basic_map_simplify(bmap
);
2717 return isl_basic_map_finalize(bmap
);
2719 isl_basic_map_free(bmap
);
2720 isl_basic_set_free(bset
);
2724 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2730 total
= 1 + isl_basic_map_total_dim(bmap
);
2731 if (total
!= vec
->size
)
2736 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2737 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2738 if (!isl_int_is_zero(s
)) {
2744 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2745 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2746 if (isl_int_is_neg(s
)) {
2757 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2759 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2762 struct isl_basic_map
*isl_basic_map_intersect(
2763 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2765 struct isl_vec
*sample
= NULL
;
2767 if (!bmap1
|| !bmap2
)
2770 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2771 bmap2
->dim
, isl_dim_param
), goto error
);
2772 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2773 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2774 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2775 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2776 return isl_basic_map_intersect(bmap2
, bmap1
);
2778 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2779 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2780 isl_assert(bmap1
->ctx
,
2781 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2783 if (bmap1
->sample
&&
2784 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2785 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2786 sample
= isl_vec_copy(bmap1
->sample
);
2787 else if (bmap2
->sample
&&
2788 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2789 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2790 sample
= isl_vec_copy(bmap2
->sample
);
2792 bmap1
= isl_basic_map_cow(bmap1
);
2795 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2796 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2797 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2800 isl_vec_free(sample
);
2802 isl_vec_free(bmap1
->sample
);
2803 bmap1
->sample
= sample
;
2806 bmap1
= isl_basic_map_simplify(bmap1
);
2807 return isl_basic_map_finalize(bmap1
);
2810 isl_vec_free(sample
);
2811 isl_basic_map_free(bmap1
);
2812 isl_basic_map_free(bmap2
);
2816 struct isl_basic_set
*isl_basic_set_intersect(
2817 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2819 return (struct isl_basic_set
*)
2820 isl_basic_map_intersect(
2821 (struct isl_basic_map
*)bset1
,
2822 (struct isl_basic_map
*)bset2
);
2825 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2826 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2828 return isl_basic_set_intersect(bset1
, bset2
);
2831 /* Special case of isl_map_intersect, where both map1 and map2
2832 * are convex, without any divs and such that either map1 or map2
2833 * contains a single constraint. This constraint is then simply
2834 * added to the other map.
2836 static __isl_give isl_map
*map_intersect_add_constraint(
2837 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2839 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2840 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2841 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2842 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2844 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2845 return isl_map_intersect(map2
, map1
);
2847 isl_assert(map2
->ctx
,
2848 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2850 map1
= isl_map_cow(map1
);
2853 if (isl_map_plain_is_empty(map1
)) {
2857 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2858 if (map2
->p
[0]->n_eq
== 1)
2859 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2861 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2862 map2
->p
[0]->ineq
[0]);
2864 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2865 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2869 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2870 isl_basic_map_free(map1
->p
[0]);
2883 /* map2 may be either a parameter domain or a map living in the same
2886 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2887 __isl_take isl_map
*map2
)
2896 if ((isl_map_plain_is_empty(map1
) ||
2897 isl_map_plain_is_universe(map2
)) &&
2898 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2902 if ((isl_map_plain_is_empty(map2
) ||
2903 isl_map_plain_is_universe(map1
)) &&
2904 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2909 if (map1
->n
== 1 && map2
->n
== 1 &&
2910 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2911 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2912 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2913 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2914 return map_intersect_add_constraint(map1
, map2
);
2916 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2917 isl_space_dim(map2
->dim
, isl_dim_param
))
2918 isl_assert(map1
->ctx
,
2919 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2921 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2922 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2923 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2925 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2926 map1
->n
* map2
->n
, flags
);
2929 for (i
= 0; i
< map1
->n
; ++i
)
2930 for (j
= 0; j
< map2
->n
; ++j
) {
2931 struct isl_basic_map
*part
;
2932 part
= isl_basic_map_intersect(
2933 isl_basic_map_copy(map1
->p
[i
]),
2934 isl_basic_map_copy(map2
->p
[j
]));
2935 if (isl_basic_map_is_empty(part
) < 0)
2936 part
= isl_basic_map_free(part
);
2937 result
= isl_map_add_basic_map(result
, part
);
2950 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2951 __isl_take isl_map
*map2
)
2955 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2956 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2957 "spaces don't match", goto error
);
2958 return map_intersect_internal(map1
, map2
);
2965 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2966 __isl_take isl_map
*map2
)
2968 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2971 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2973 return (struct isl_set
*)
2974 isl_map_intersect((struct isl_map
*)set1
,
2975 (struct isl_map
*)set2
);
2978 /* map_intersect_internal accepts intersections
2979 * with parameter domains, so we can just call that function.
2981 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2982 __isl_take isl_set
*params
)
2984 return map_intersect_internal(map
, params
);
2987 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2988 __isl_take isl_map
*map2
)
2990 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2993 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2994 __isl_take isl_set
*params
)
2996 return isl_map_intersect_params(set
, params
);
2999 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3002 struct isl_basic_set
*bset
;
3007 bmap
= isl_basic_map_cow(bmap
);
3010 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3011 in
= isl_basic_map_n_in(bmap
);
3012 bset
= isl_basic_set_from_basic_map(bmap
);
3013 bset
= isl_basic_set_swap_vars(bset
, in
);
3014 return isl_basic_map_from_basic_set(bset
, dim
);
3017 static __isl_give isl_basic_map
*basic_map_space_reset(
3018 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3024 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3027 space
= isl_basic_map_get_space(bmap
);
3028 space
= isl_space_reset(space
, type
);
3029 bmap
= isl_basic_map_reset_space(bmap
, space
);
3033 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3034 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3035 unsigned pos
, unsigned n
)
3038 struct isl_basic_map
*res
;
3039 struct isl_dim_map
*dim_map
;
3040 unsigned total
, off
;
3041 enum isl_dim_type t
;
3044 return basic_map_space_reset(bmap
, type
);
3049 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3051 total
= isl_basic_map_total_dim(bmap
) + n
;
3052 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3054 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3056 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3058 unsigned size
= isl_basic_map_dim(bmap
, t
);
3059 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3061 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3062 pos
, size
- pos
, off
+ pos
+ n
);
3064 off
+= isl_space_dim(res_dim
, t
);
3066 isl_dim_map_div(dim_map
, bmap
, off
);
3068 res
= isl_basic_map_alloc_space(res_dim
,
3069 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3070 if (isl_basic_map_is_rational(bmap
))
3071 res
= isl_basic_map_set_rational(res
);
3072 if (isl_basic_map_plain_is_empty(bmap
)) {
3073 isl_basic_map_free(bmap
);
3075 return isl_basic_map_set_to_empty(res
);
3077 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3078 return isl_basic_map_finalize(res
);
3081 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3082 __isl_take isl_basic_set
*bset
,
3083 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3085 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3088 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3089 enum isl_dim_type type
, unsigned n
)
3093 return isl_basic_map_insert_dims(bmap
, type
,
3094 isl_basic_map_dim(bmap
, type
), n
);
3097 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3098 enum isl_dim_type type
, unsigned n
)
3102 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3103 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3105 isl_basic_set_free(bset
);
3109 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3110 enum isl_dim_type type
)
3114 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3117 space
= isl_map_get_space(map
);
3118 space
= isl_space_reset(space
, type
);
3119 map
= isl_map_reset_space(map
, space
);
3123 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3124 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3129 return map_space_reset(map
, type
);
3131 map
= isl_map_cow(map
);
3135 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3139 for (i
= 0; i
< map
->n
; ++i
) {
3140 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3151 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3152 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3154 return isl_map_insert_dims(set
, type
, pos
, n
);
3157 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3158 enum isl_dim_type type
, unsigned n
)
3162 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3165 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3166 enum isl_dim_type type
, unsigned n
)
3170 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3171 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3177 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3178 __isl_take isl_basic_map
*bmap
,
3179 enum isl_dim_type dst_type
, unsigned dst_pos
,
3180 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3182 struct isl_dim_map
*dim_map
;
3183 struct isl_basic_map
*res
;
3184 enum isl_dim_type t
;
3185 unsigned total
, off
;
3192 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3195 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3198 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3200 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3201 pos(bmap
->dim
, src_type
) + src_pos
+
3202 ((src_type
< dst_type
) ? n
: 0)) {
3203 bmap
= isl_basic_map_cow(bmap
);
3207 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3208 src_type
, src_pos
, n
);
3212 bmap
= isl_basic_map_finalize(bmap
);
3217 total
= isl_basic_map_total_dim(bmap
);
3218 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3221 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3222 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3223 if (t
== dst_type
) {
3224 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3227 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3230 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3231 dst_pos
, size
- dst_pos
, off
);
3232 off
+= size
- dst_pos
;
3233 } else if (t
== src_type
) {
3234 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3237 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3238 src_pos
+ n
, size
- src_pos
- n
, off
);
3239 off
+= size
- src_pos
- n
;
3241 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3245 isl_dim_map_div(dim_map
, bmap
, off
);
3247 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3248 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3249 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3253 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3254 src_type
, src_pos
, n
);
3258 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3259 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3260 bmap
= isl_basic_map_finalize(bmap
);
3264 isl_basic_map_free(bmap
);
3268 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3269 enum isl_dim_type dst_type
, unsigned dst_pos
,
3270 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3272 return (isl_basic_set
*)isl_basic_map_move_dims(
3273 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3276 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3277 enum isl_dim_type dst_type
, unsigned dst_pos
,
3278 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3282 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3283 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3284 src_type
, src_pos
, n
);
3290 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3291 enum isl_dim_type dst_type
, unsigned dst_pos
,
3292 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3301 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3304 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3307 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3309 map
= isl_map_cow(map
);
3313 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3317 for (i
= 0; i
< map
->n
; ++i
) {
3318 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3320 src_type
, src_pos
, n
);
3331 /* Move the specified dimensions to the last columns right before
3332 * the divs. Don't change the dimension specification of bmap.
3333 * That's the responsibility of the caller.
3335 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3336 enum isl_dim_type type
, unsigned first
, unsigned n
)
3338 struct isl_dim_map
*dim_map
;
3339 struct isl_basic_map
*res
;
3340 enum isl_dim_type t
;
3341 unsigned total
, off
;
3345 if (pos(bmap
->dim
, type
) + first
+ n
==
3346 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3349 total
= isl_basic_map_total_dim(bmap
);
3350 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3353 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3354 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3356 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3359 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3360 first
, n
, total
- bmap
->n_div
- n
);
3361 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3362 first
+ n
, size
- (first
+ n
), off
);
3363 off
+= size
- (first
+ n
);
3365 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3369 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3371 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3372 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3373 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3377 /* Insert "n" rows in the divs of "bmap".
3379 * The number of columns is not changed, which means that the last
3380 * dimensions of "bmap" are being reintepreted as the new divs.
3381 * The space of "bmap" is not adjusted, however, which means
3382 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3383 * from the space of "bmap" is the responsibility of the caller.
3385 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3393 bmap
= isl_basic_map_cow(bmap
);
3397 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3398 old
= bmap
->block2
.data
;
3399 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3400 (bmap
->extra
+ n
) * (1 + row_size
));
3401 if (!bmap
->block2
.data
)
3402 return isl_basic_map_free(bmap
);
3403 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3405 return isl_basic_map_free(bmap
);
3406 for (i
= 0; i
< n
; ++i
) {
3407 new_div
[i
] = bmap
->block2
.data
+
3408 (bmap
->extra
+ i
) * (1 + row_size
);
3409 isl_seq_clr(new_div
[i
], 1 + row_size
);
3411 for (i
= 0; i
< bmap
->extra
; ++i
)
3412 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3414 bmap
->div
= new_div
;
3421 /* Turn the n dimensions of type type, starting at first
3422 * into existentially quantified variables.
3424 __isl_give isl_basic_map
*isl_basic_map_project_out(
3425 __isl_take isl_basic_map
*bmap
,
3426 enum isl_dim_type type
, unsigned first
, unsigned n
)
3429 return basic_map_space_reset(bmap
, type
);
3434 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3435 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3437 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3440 bmap
= move_last(bmap
, type
, first
, n
);
3441 bmap
= isl_basic_map_cow(bmap
);
3442 bmap
= insert_div_rows(bmap
, n
);
3446 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3449 bmap
= isl_basic_map_simplify(bmap
);
3450 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3451 return isl_basic_map_finalize(bmap
);
3453 isl_basic_map_free(bmap
);
3457 /* Turn the n dimensions of type type, starting at first
3458 * into existentially quantified variables.
3460 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3461 enum isl_dim_type type
, unsigned first
, unsigned n
)
3463 return (isl_basic_set
*)isl_basic_map_project_out(
3464 (isl_basic_map
*)bset
, type
, first
, n
);
3467 /* Turn the n dimensions of type type, starting at first
3468 * into existentially quantified variables.
3470 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3471 enum isl_dim_type type
, unsigned first
, unsigned n
)
3479 return map_space_reset(map
, type
);
3481 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3483 map
= isl_map_cow(map
);
3487 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3491 for (i
= 0; i
< map
->n
; ++i
) {
3492 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3503 /* Turn the n dimensions of type type, starting at first
3504 * into existentially quantified variables.
3506 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3507 enum isl_dim_type type
, unsigned first
, unsigned n
)
3509 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3512 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3516 for (i
= 0; i
< n
; ++i
) {
3517 j
= isl_basic_map_alloc_div(bmap
);
3520 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3524 isl_basic_map_free(bmap
);
3528 struct isl_basic_map
*isl_basic_map_apply_range(
3529 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3531 isl_space
*dim_result
= NULL
;
3532 struct isl_basic_map
*bmap
;
3533 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3534 struct isl_dim_map
*dim_map1
, *dim_map2
;
3536 if (!bmap1
|| !bmap2
)
3539 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3540 isl_space_copy(bmap2
->dim
));
3542 n_in
= isl_basic_map_n_in(bmap1
);
3543 n_out
= isl_basic_map_n_out(bmap2
);
3544 n
= isl_basic_map_n_out(bmap1
);
3545 nparam
= isl_basic_map_n_param(bmap1
);
3547 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3548 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3549 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3550 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3551 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3552 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3553 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3554 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3555 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3556 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3557 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3559 bmap
= isl_basic_map_alloc_space(dim_result
,
3560 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3561 bmap1
->n_eq
+ bmap2
->n_eq
,
3562 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3563 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3564 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3565 bmap
= add_divs(bmap
, n
);
3566 bmap
= isl_basic_map_simplify(bmap
);
3567 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3568 return isl_basic_map_finalize(bmap
);
3570 isl_basic_map_free(bmap1
);
3571 isl_basic_map_free(bmap2
);
3575 struct isl_basic_set
*isl_basic_set_apply(
3576 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3581 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3584 return (struct isl_basic_set
*)
3585 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3587 isl_basic_set_free(bset
);
3588 isl_basic_map_free(bmap
);
3592 struct isl_basic_map
*isl_basic_map_apply_domain(
3593 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3595 if (!bmap1
|| !bmap2
)
3598 isl_assert(bmap1
->ctx
,
3599 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3600 isl_assert(bmap1
->ctx
,
3601 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3604 bmap1
= isl_basic_map_reverse(bmap1
);
3605 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3606 return isl_basic_map_reverse(bmap1
);
3608 isl_basic_map_free(bmap1
);
3609 isl_basic_map_free(bmap2
);
3613 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3614 * A \cap B -> f(A) + f(B)
3616 struct isl_basic_map
*isl_basic_map_sum(
3617 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3619 unsigned n_in
, n_out
, nparam
, total
, pos
;
3620 struct isl_basic_map
*bmap
= NULL
;
3621 struct isl_dim_map
*dim_map1
, *dim_map2
;
3624 if (!bmap1
|| !bmap2
)
3627 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3630 nparam
= isl_basic_map_n_param(bmap1
);
3631 n_in
= isl_basic_map_n_in(bmap1
);
3632 n_out
= isl_basic_map_n_out(bmap1
);
3634 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3635 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3636 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3637 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3638 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3639 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3640 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3641 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3642 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3643 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3644 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3646 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3647 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3648 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3649 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3650 for (i
= 0; i
< n_out
; ++i
) {
3651 int j
= isl_basic_map_alloc_equality(bmap
);
3654 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3655 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3656 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3657 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3659 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3660 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3661 bmap
= add_divs(bmap
, 2 * n_out
);
3663 bmap
= isl_basic_map_simplify(bmap
);
3664 return isl_basic_map_finalize(bmap
);
3666 isl_basic_map_free(bmap
);
3667 isl_basic_map_free(bmap1
);
3668 isl_basic_map_free(bmap2
);
3672 /* Given two maps A -> f(A) and B -> g(B), construct a map
3673 * A \cap B -> f(A) + f(B)
3675 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3677 struct isl_map
*result
;
3683 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3685 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3686 map1
->n
* map2
->n
, 0);
3689 for (i
= 0; i
< map1
->n
; ++i
)
3690 for (j
= 0; j
< map2
->n
; ++j
) {
3691 struct isl_basic_map
*part
;
3692 part
= isl_basic_map_sum(
3693 isl_basic_map_copy(map1
->p
[i
]),
3694 isl_basic_map_copy(map2
->p
[j
]));
3695 if (isl_basic_map_is_empty(part
))
3696 isl_basic_map_free(part
);
3698 result
= isl_map_add_basic_map(result
, part
);
3711 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3712 __isl_take isl_set
*set2
)
3714 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3717 /* Given a basic map A -> f(A), construct A -> -f(A).
3719 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3724 bmap
= isl_basic_map_cow(bmap
);
3728 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3729 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3730 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3731 for (j
= 0; j
< n
; ++j
)
3732 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3733 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3734 for (j
= 0; j
< n
; ++j
)
3735 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3736 for (i
= 0; i
< bmap
->n_div
; ++i
)
3737 for (j
= 0; j
< n
; ++j
)
3738 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3739 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3740 return isl_basic_map_finalize(bmap
);
3743 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3745 return isl_basic_map_neg(bset
);
3748 /* Given a map A -> f(A), construct A -> -f(A).
3750 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3754 map
= isl_map_cow(map
);
3758 for (i
= 0; i
< map
->n
; ++i
) {
3759 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3770 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3772 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3775 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3776 * A -> floor(f(A)/d).
3778 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3781 unsigned n_in
, n_out
, nparam
, total
, pos
;
3782 struct isl_basic_map
*result
= NULL
;
3783 struct isl_dim_map
*dim_map
;
3789 nparam
= isl_basic_map_n_param(bmap
);
3790 n_in
= isl_basic_map_n_in(bmap
);
3791 n_out
= isl_basic_map_n_out(bmap
);
3793 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3794 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3795 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3796 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3797 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3798 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3800 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3801 bmap
->n_div
+ n_out
,
3802 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3803 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3804 result
= add_divs(result
, n_out
);
3805 for (i
= 0; i
< n_out
; ++i
) {
3807 j
= isl_basic_map_alloc_inequality(result
);
3810 isl_seq_clr(result
->ineq
[j
], 1+total
);
3811 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3812 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3813 j
= isl_basic_map_alloc_inequality(result
);
3816 isl_seq_clr(result
->ineq
[j
], 1+total
);
3817 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3818 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3819 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3822 result
= isl_basic_map_simplify(result
);
3823 return isl_basic_map_finalize(result
);
3825 isl_basic_map_free(result
);
3829 /* Given a map A -> f(A) and an integer d, construct a map
3830 * A -> floor(f(A)/d).
3832 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3836 map
= isl_map_cow(map
);
3840 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3841 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3842 for (i
= 0; i
< map
->n
; ++i
) {
3843 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3854 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3860 i
= isl_basic_map_alloc_equality(bmap
);
3863 nparam
= isl_basic_map_n_param(bmap
);
3864 n_in
= isl_basic_map_n_in(bmap
);
3865 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3866 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3867 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3868 return isl_basic_map_finalize(bmap
);
3870 isl_basic_map_free(bmap
);
3874 /* Add a constraints to "bmap" expressing i_pos < o_pos
3876 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3882 i
= isl_basic_map_alloc_inequality(bmap
);
3885 nparam
= isl_basic_map_n_param(bmap
);
3886 n_in
= isl_basic_map_n_in(bmap
);
3887 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3888 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3889 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3890 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3891 return isl_basic_map_finalize(bmap
);
3893 isl_basic_map_free(bmap
);
3897 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3899 static __isl_give isl_basic_map
*var_less_or_equal(
3900 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3906 i
= isl_basic_map_alloc_inequality(bmap
);
3909 nparam
= isl_basic_map_n_param(bmap
);
3910 n_in
= isl_basic_map_n_in(bmap
);
3911 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3912 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3913 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3914 return isl_basic_map_finalize(bmap
);
3916 isl_basic_map_free(bmap
);
3920 /* Add a constraints to "bmap" expressing i_pos > o_pos
3922 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3928 i
= isl_basic_map_alloc_inequality(bmap
);
3931 nparam
= isl_basic_map_n_param(bmap
);
3932 n_in
= isl_basic_map_n_in(bmap
);
3933 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3934 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3935 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3936 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3937 return isl_basic_map_finalize(bmap
);
3939 isl_basic_map_free(bmap
);
3943 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3945 static __isl_give isl_basic_map
*var_more_or_equal(
3946 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3952 i
= isl_basic_map_alloc_inequality(bmap
);
3955 nparam
= isl_basic_map_n_param(bmap
);
3956 n_in
= isl_basic_map_n_in(bmap
);
3957 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3958 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3959 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3960 return isl_basic_map_finalize(bmap
);
3962 isl_basic_map_free(bmap
);
3966 __isl_give isl_basic_map
*isl_basic_map_equal(
3967 __isl_take isl_space
*dim
, unsigned n_equal
)
3970 struct isl_basic_map
*bmap
;
3971 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3974 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3975 bmap
= var_equal(bmap
, i
);
3976 return isl_basic_map_finalize(bmap
);
3979 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3981 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3985 struct isl_basic_map
*bmap
;
3986 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3989 for (i
= 0; i
< pos
&& bmap
; ++i
)
3990 bmap
= var_equal(bmap
, i
);
3992 bmap
= var_less(bmap
, pos
);
3993 return isl_basic_map_finalize(bmap
);
3996 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3998 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3999 __isl_take isl_space
*dim
, unsigned pos
)
4002 isl_basic_map
*bmap
;
4004 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4005 for (i
= 0; i
< pos
; ++i
)
4006 bmap
= var_equal(bmap
, i
);
4007 bmap
= var_less_or_equal(bmap
, pos
);
4008 return isl_basic_map_finalize(bmap
);
4011 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4013 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4017 struct isl_basic_map
*bmap
;
4018 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4021 for (i
= 0; i
< pos
&& bmap
; ++i
)
4022 bmap
= var_equal(bmap
, i
);
4024 bmap
= var_more(bmap
, pos
);
4025 return isl_basic_map_finalize(bmap
);
4028 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4030 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4031 __isl_take isl_space
*dim
, unsigned pos
)
4034 isl_basic_map
*bmap
;
4036 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4037 for (i
= 0; i
< pos
; ++i
)
4038 bmap
= var_equal(bmap
, i
);
4039 bmap
= var_more_or_equal(bmap
, pos
);
4040 return isl_basic_map_finalize(bmap
);
4043 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4044 unsigned n
, int equal
)
4046 struct isl_map
*map
;
4049 if (n
== 0 && equal
)
4050 return isl_map_universe(dims
);
4052 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4054 for (i
= 0; i
+ 1 < n
; ++i
)
4055 map
= isl_map_add_basic_map(map
,
4056 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4059 map
= isl_map_add_basic_map(map
,
4060 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4062 map
= isl_map_add_basic_map(map
,
4063 isl_basic_map_less_at(dims
, n
- 1));
4065 isl_space_free(dims
);
4070 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4074 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4077 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4079 return map_lex_lte_first(dim
, n
, 0);
4082 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4084 return map_lex_lte_first(dim
, n
, 1);
4087 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4089 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4092 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4094 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4097 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4098 unsigned n
, int equal
)
4100 struct isl_map
*map
;
4103 if (n
== 0 && equal
)
4104 return isl_map_universe(dims
);
4106 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4108 for (i
= 0; i
+ 1 < n
; ++i
)
4109 map
= isl_map_add_basic_map(map
,
4110 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4113 map
= isl_map_add_basic_map(map
,
4114 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4116 map
= isl_map_add_basic_map(map
,
4117 isl_basic_map_more_at(dims
, n
- 1));
4119 isl_space_free(dims
);
4124 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4128 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4131 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4133 return map_lex_gte_first(dim
, n
, 0);
4136 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4138 return map_lex_gte_first(dim
, n
, 1);
4141 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4143 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4146 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4148 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4151 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4152 __isl_take isl_set
*set2
)
4155 map
= isl_map_lex_le(isl_set_get_space(set1
));
4156 map
= isl_map_intersect_domain(map
, set1
);
4157 map
= isl_map_intersect_range(map
, set2
);
4161 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4162 __isl_take isl_set
*set2
)
4165 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4166 map
= isl_map_intersect_domain(map
, set1
);
4167 map
= isl_map_intersect_range(map
, set2
);
4171 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4172 __isl_take isl_set
*set2
)
4175 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4176 map
= isl_map_intersect_domain(map
, set1
);
4177 map
= isl_map_intersect_range(map
, set2
);
4181 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4182 __isl_take isl_set
*set2
)
4185 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4186 map
= isl_map_intersect_domain(map
, set1
);
4187 map
= isl_map_intersect_range(map
, set2
);
4191 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4192 __isl_take isl_map
*map2
)
4195 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4196 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4197 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4201 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4202 __isl_take isl_map
*map2
)
4205 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4206 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4207 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4211 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4212 __isl_take isl_map
*map2
)
4215 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4216 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4217 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4221 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4222 __isl_take isl_map
*map2
)
4225 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4226 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4227 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4231 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4232 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4234 struct isl_basic_map
*bmap
;
4236 bset
= isl_basic_set_cow(bset
);
4240 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4241 isl_space_free(bset
->dim
);
4242 bmap
= (struct isl_basic_map
*) bset
;
4244 return isl_basic_map_finalize(bmap
);
4246 isl_basic_set_free(bset
);
4247 isl_space_free(dim
);
4251 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4255 if (bmap
->dim
->n_in
== 0)
4256 return (struct isl_basic_set
*)bmap
;
4257 bmap
= isl_basic_map_cow(bmap
);
4260 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4263 bmap
= isl_basic_map_finalize(bmap
);
4264 return (struct isl_basic_set
*)bmap
;
4266 isl_basic_map_free(bmap
);
4270 /* For a div d = floor(f/m), add the constraints
4273 * -(f-(n-1)) + m d >= 0
4275 * Note that the second constraint is the negation of
4279 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4280 unsigned pos
, isl_int
*div
)
4283 unsigned total
= isl_basic_map_total_dim(bmap
);
4285 i
= isl_basic_map_alloc_inequality(bmap
);
4288 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4289 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4291 j
= isl_basic_map_alloc_inequality(bmap
);
4294 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
4295 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
4296 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
4300 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4301 unsigned pos
, isl_int
*div
)
4303 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4307 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4309 unsigned total
= isl_basic_map_total_dim(bmap
);
4310 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4312 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4316 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4318 return isl_basic_map_add_div_constraints(bset
, div
);
4321 struct isl_basic_set
*isl_basic_map_underlying_set(
4322 struct isl_basic_map
*bmap
)
4326 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4328 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4329 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4330 return (struct isl_basic_set
*)bmap
;
4331 bmap
= isl_basic_map_cow(bmap
);
4334 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4337 bmap
->extra
-= bmap
->n_div
;
4339 bmap
= isl_basic_map_finalize(bmap
);
4340 return (struct isl_basic_set
*)bmap
;
4342 isl_basic_map_free(bmap
);
4346 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4347 __isl_take isl_basic_set
*bset
)
4349 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4352 struct isl_basic_map
*isl_basic_map_overlying_set(
4353 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4355 struct isl_basic_map
*bmap
;
4356 struct isl_ctx
*ctx
;
4363 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4364 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4365 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4367 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4368 isl_basic_map_free(like
);
4369 return (struct isl_basic_map
*)bset
;
4371 bset
= isl_basic_set_cow(bset
);
4374 total
= bset
->dim
->n_out
+ bset
->extra
;
4375 bmap
= (struct isl_basic_map
*)bset
;
4376 isl_space_free(bmap
->dim
);
4377 bmap
->dim
= isl_space_copy(like
->dim
);
4380 bmap
->n_div
= like
->n_div
;
4381 bmap
->extra
+= like
->n_div
;
4385 ltotal
= total
- bmap
->extra
+ like
->extra
;
4388 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4389 bmap
->extra
* (1 + 1 + total
));
4390 if (isl_blk_is_error(bmap
->block2
))
4392 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4396 for (i
= 0; i
< bmap
->extra
; ++i
)
4397 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4398 for (i
= 0; i
< like
->n_div
; ++i
) {
4399 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4400 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4402 bmap
= isl_basic_map_extend_constraints(bmap
,
4403 0, 2 * like
->n_div
);
4404 for (i
= 0; i
< like
->n_div
; ++i
) {
4407 if (isl_int_is_zero(bmap
->div
[i
][0]))
4409 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4410 bmap
= isl_basic_map_free(bmap
);
4413 isl_basic_map_free(like
);
4414 bmap
= isl_basic_map_simplify(bmap
);
4415 bmap
= isl_basic_map_finalize(bmap
);
4418 isl_basic_map_free(like
);
4419 isl_basic_set_free(bset
);
4423 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4424 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4426 return (struct isl_basic_set
*)
4427 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4430 struct isl_set
*isl_set_from_underlying_set(
4431 struct isl_set
*set
, struct isl_basic_set
*like
)
4437 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4439 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4440 isl_basic_set_free(like
);
4443 set
= isl_set_cow(set
);
4446 for (i
= 0; i
< set
->n
; ++i
) {
4447 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4448 isl_basic_set_copy(like
));
4452 isl_space_free(set
->dim
);
4453 set
->dim
= isl_space_copy(like
->dim
);
4456 isl_basic_set_free(like
);
4459 isl_basic_set_free(like
);
4464 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4468 map
= isl_map_cow(map
);
4471 map
->dim
= isl_space_cow(map
->dim
);
4475 for (i
= 1; i
< map
->n
; ++i
)
4476 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4478 for (i
= 0; i
< map
->n
; ++i
) {
4479 map
->p
[i
] = (struct isl_basic_map
*)
4480 isl_basic_map_underlying_set(map
->p
[i
]);
4485 map
->dim
= isl_space_underlying(map
->dim
, 0);
4487 isl_space_free(map
->dim
);
4488 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4492 return (struct isl_set
*)map
;
4498 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4500 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4503 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4504 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4506 bmap
= isl_basic_map_cow(bmap
);
4510 isl_space_free(bmap
->dim
);
4513 bmap
= isl_basic_map_finalize(bmap
);
4517 isl_basic_map_free(bmap
);
4518 isl_space_free(dim
);
4522 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4523 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4525 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4529 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4530 __isl_take isl_space
*dim
)
4534 map
= isl_map_cow(map
);
4538 for (i
= 0; i
< map
->n
; ++i
) {
4539 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4540 isl_space_copy(dim
));
4544 isl_space_free(map
->dim
);
4550 isl_space_free(dim
);
4554 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4555 __isl_take isl_space
*dim
)
4557 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4560 /* Compute the parameter domain of the given basic set.
4562 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4567 if (isl_basic_set_is_params(bset
))
4570 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4571 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4572 space
= isl_basic_set_get_space(bset
);
4573 space
= isl_space_params(space
);
4574 bset
= isl_basic_set_reset_space(bset
, space
);
4578 /* Construct a zero-dimensional basic set with the given parameter domain.
4580 __isl_give isl_basic_set
*isl_basic_set_from_params(
4581 __isl_take isl_basic_set
*bset
)
4584 space
= isl_basic_set_get_space(bset
);
4585 space
= isl_space_set_from_params(space
);
4586 bset
= isl_basic_set_reset_space(bset
, space
);
4590 /* Compute the parameter domain of the given set.
4592 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4597 if (isl_set_is_params(set
))
4600 n
= isl_set_dim(set
, isl_dim_set
);
4601 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4602 space
= isl_set_get_space(set
);
4603 space
= isl_space_params(space
);
4604 set
= isl_set_reset_space(set
, space
);
4608 /* Construct a zero-dimensional set with the given parameter domain.
4610 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4613 space
= isl_set_get_space(set
);
4614 space
= isl_space_set_from_params(space
);
4615 set
= isl_set_reset_space(set
, space
);
4619 /* Compute the parameter domain of the given map.
4621 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4626 n
= isl_map_dim(map
, isl_dim_in
);
4627 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4628 n
= isl_map_dim(map
, isl_dim_out
);
4629 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4630 space
= isl_map_get_space(map
);
4631 space
= isl_space_params(space
);
4632 map
= isl_map_reset_space(map
, space
);
4636 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4639 struct isl_basic_set
*domain
;
4645 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4647 n_in
= isl_basic_map_n_in(bmap
);
4648 n_out
= isl_basic_map_n_out(bmap
);
4649 domain
= isl_basic_set_from_basic_map(bmap
);
4650 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4652 domain
= isl_basic_set_reset_space(domain
, dim
);
4657 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4661 return isl_space_may_be_set(bmap
->dim
);
4664 /* Is this basic map actually a set?
4665 * Users should never call this function. Outside of isl,
4666 * the type should indicate whether something is a set or a map.
4668 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4672 return isl_space_is_set(bmap
->dim
);
4675 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4679 if (isl_basic_map_is_set(bmap
))
4681 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4684 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4685 __isl_take isl_basic_map
*bmap
)
4689 isl_basic_map
*domain
;
4690 int nparam
, n_in
, n_out
;
4693 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4694 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4695 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4697 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4698 domain
= isl_basic_map_universe(dim
);
4700 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4701 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4702 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4704 total
= isl_basic_map_total_dim(bmap
);
4706 for (i
= 0; i
< n_in
; ++i
) {
4707 k
= isl_basic_map_alloc_equality(bmap
);
4710 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4711 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4712 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4715 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4716 return isl_basic_map_finalize(bmap
);
4718 isl_basic_map_free(bmap
);
4722 __isl_give isl_basic_map
*isl_basic_map_range_map(
4723 __isl_take isl_basic_map
*bmap
)
4727 isl_basic_map
*range
;
4728 int nparam
, n_in
, n_out
;
4731 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4732 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4733 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4735 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4736 range
= isl_basic_map_universe(dim
);
4738 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4739 bmap
= isl_basic_map_apply_range(bmap
, range
);
4740 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4742 total
= isl_basic_map_total_dim(bmap
);
4744 for (i
= 0; i
< n_out
; ++i
) {
4745 k
= isl_basic_map_alloc_equality(bmap
);
4748 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4749 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4750 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4753 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4754 return isl_basic_map_finalize(bmap
);
4756 isl_basic_map_free(bmap
);
4760 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4764 return isl_space_may_be_set(map
->dim
);
4767 /* Is this map actually a set?
4768 * Users should never call this function. Outside of isl,
4769 * the type should indicate whether something is a set or a map.
4771 int isl_map_is_set(__isl_keep isl_map
*map
)
4775 return isl_space_is_set(map
->dim
);
4778 struct isl_set
*isl_map_range(struct isl_map
*map
)
4781 struct isl_set
*set
;
4785 if (isl_map_is_set(map
))
4786 return (isl_set
*)map
;
4788 map
= isl_map_cow(map
);
4792 set
= (struct isl_set
*) map
;
4793 set
->dim
= isl_space_range(set
->dim
);
4796 for (i
= 0; i
< map
->n
; ++i
) {
4797 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4801 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4802 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4809 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4812 isl_space
*domain_dim
;
4814 map
= isl_map_cow(map
);
4818 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4819 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4820 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4823 for (i
= 0; i
< map
->n
; ++i
) {
4824 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4828 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4829 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4836 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4839 isl_space
*range_dim
;
4841 map
= isl_map_cow(map
);
4845 range_dim
= isl_space_range(isl_map_get_space(map
));
4846 range_dim
= isl_space_from_range(range_dim
);
4847 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4848 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4851 for (i
= 0; i
< map
->n
; ++i
) {
4852 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4856 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4857 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4864 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4865 __isl_take isl_space
*dim
)
4868 struct isl_map
*map
= NULL
;
4870 set
= isl_set_cow(set
);
4873 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4874 map
= (struct isl_map
*)set
;
4875 for (i
= 0; i
< set
->n
; ++i
) {
4876 map
->p
[i
] = isl_basic_map_from_basic_set(
4877 set
->p
[i
], isl_space_copy(dim
));
4881 isl_space_free(map
->dim
);
4885 isl_space_free(dim
);
4890 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4891 __isl_take isl_basic_set
*bset
)
4893 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4896 __isl_give isl_basic_map
*isl_basic_map_from_range(
4897 __isl_take isl_basic_set
*bset
)
4900 space
= isl_basic_set_get_space(bset
);
4901 space
= isl_space_from_range(space
);
4902 bset
= isl_basic_set_reset_space(bset
, space
);
4903 return (isl_basic_map
*)bset
;
4906 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4909 space
= isl_set_get_space(set
);
4910 space
= isl_space_from_range(space
);
4911 set
= isl_set_reset_space(set
, space
);
4912 return (struct isl_map
*)set
;
4915 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4917 return isl_map_reverse(isl_map_from_range(set
));
4920 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4921 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4923 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4926 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4927 __isl_take isl_set
*range
)
4929 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4932 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4935 struct isl_set
*set
= NULL
;
4939 map
= isl_map_cow(map
);
4942 map
->dim
= isl_space_as_set_space(map
->dim
);
4945 set
= (struct isl_set
*)map
;
4946 for (i
= 0; i
< map
->n
; ++i
) {
4947 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4957 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4960 struct isl_map
*map
;
4965 isl_die(dim
->ctx
, isl_error_internal
,
4966 "negative number of basic maps", goto error
);
4967 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4968 sizeof(struct isl_map
) +
4969 (n
- 1) * sizeof(struct isl_basic_map
*));
4973 map
->ctx
= dim
->ctx
;
4974 isl_ctx_ref(map
->ctx
);
4982 isl_space_free(dim
);
4986 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4987 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4990 struct isl_map
*map
;
4993 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4997 map
= isl_map_alloc_space(dims
, n
, flags
);
5001 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5003 struct isl_basic_map
*bmap
;
5004 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5005 bmap
= isl_basic_map_set_to_empty(bmap
);
5009 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5011 struct isl_basic_set
*bset
;
5012 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5013 bset
= isl_basic_set_set_to_empty(bset
);
5017 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5019 struct isl_basic_map
*bmap
;
5022 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5023 bmap
= isl_basic_map_set_to_empty(bmap
);
5027 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5029 struct isl_basic_map
*bmap
;
5032 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5033 bmap
= isl_basic_map_set_to_empty(bmap
);
5037 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5039 struct isl_basic_set
*bset
;
5042 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5043 bset
= isl_basic_set_set_to_empty(bset
);
5047 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5049 struct isl_basic_map
*bmap
;
5050 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5051 bmap
= isl_basic_map_finalize(bmap
);
5055 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5057 struct isl_basic_set
*bset
;
5058 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5059 bset
= isl_basic_set_finalize(bset
);
5063 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5066 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5067 isl_basic_map
*bmap
;
5069 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5070 for (i
= 0; i
< total
; ++i
) {
5071 int k
= isl_basic_map_alloc_inequality(bmap
);
5074 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5075 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5079 isl_basic_map_free(bmap
);
5083 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5085 return isl_basic_map_nat_universe(dim
);
5088 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5090 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5093 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5095 return isl_map_nat_universe(dim
);
5098 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5099 __isl_keep isl_basic_map
*model
)
5103 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5106 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5110 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5113 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5114 __isl_keep isl_set
*model
)
5118 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5121 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5123 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5126 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5130 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5133 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5137 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5140 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5142 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5145 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5149 return isl_set_empty(isl_space_copy(model
->dim
));
5152 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5154 struct isl_map
*map
;
5157 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5158 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5162 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5164 struct isl_set
*set
;
5167 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5168 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5172 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5176 return isl_set_universe(isl_space_copy(model
->dim
));
5179 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5182 struct isl_map
*dup
;
5186 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5187 for (i
= 0; i
< map
->n
; ++i
)
5188 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5192 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5193 __isl_take isl_basic_map
*bmap
)
5197 if (isl_basic_map_plain_is_empty(bmap
)) {
5198 isl_basic_map_free(bmap
);
5201 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5202 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5203 map
->p
[map
->n
] = bmap
;
5205 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5211 isl_basic_map_free(bmap
);
5215 void *isl_map_free(struct isl_map
*map
)
5225 isl_ctx_deref(map
->ctx
);
5226 for (i
= 0; i
< map
->n
; ++i
)
5227 isl_basic_map_free(map
->p
[i
]);
5228 isl_space_free(map
->dim
);
5234 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5235 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5239 base
= isl_map_cow(base
);
5243 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5246 for (i
= 0; i
< base
->n
; ++i
) {
5247 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5248 isl_space_copy(base
->dim
), 0, 0, 0);
5258 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5259 unsigned nparam
, unsigned dim
)
5261 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5265 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5266 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5270 bmap
= isl_basic_map_cow(bmap
);
5271 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5272 j
= isl_basic_map_alloc_equality(bmap
);
5275 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5276 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5277 isl_int_set_si(bmap
->eq
[j
][0], value
);
5278 bmap
= isl_basic_map_simplify(bmap
);
5279 return isl_basic_map_finalize(bmap
);
5281 isl_basic_map_free(bmap
);
5285 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5286 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5290 bmap
= isl_basic_map_cow(bmap
);
5291 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5292 j
= isl_basic_map_alloc_equality(bmap
);
5295 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5296 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5297 isl_int_set(bmap
->eq
[j
][0], value
);
5298 bmap
= isl_basic_map_simplify(bmap
);
5299 return isl_basic_map_finalize(bmap
);
5301 isl_basic_map_free(bmap
);
5305 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5306 enum isl_dim_type type
, unsigned pos
, int value
)
5310 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5311 return isl_basic_map_fix_pos_si(bmap
,
5312 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5314 isl_basic_map_free(bmap
);
5318 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5319 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5323 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5324 return isl_basic_map_fix_pos(bmap
,
5325 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5327 isl_basic_map_free(bmap
);
5331 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5332 enum isl_dim_type type
, unsigned pos
, int value
)
5334 return (struct isl_basic_set
*)
5335 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5339 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5340 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5342 return (struct isl_basic_set
*)
5343 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5347 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5348 unsigned input
, int value
)
5350 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5353 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5354 unsigned dim
, int value
)
5356 return (struct isl_basic_set
*)
5357 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5358 isl_dim_set
, dim
, value
);
5361 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5363 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5370 isl_basic_map_free(map
->p
[i
]);
5371 if (i
!= map
->n
- 1) {
5372 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5373 map
->p
[i
] = map
->p
[map
->n
- 1];
5380 /* Perform "fn" on each basic map of "map", where we may not be holding
5381 * the only reference to "map".
5382 * In particular, "fn" should be a semantics preserving operation
5383 * that we want to apply to all copies of "map". We therefore need
5384 * to be careful not to modify "map" in a way that breaks "map"
5385 * in case anything goes wrong.
5387 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5388 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5390 struct isl_basic_map
*bmap
;
5396 for (i
= map
->n
- 1; i
>= 0; --i
) {
5397 bmap
= isl_basic_map_copy(map
->p
[i
]);
5401 isl_basic_map_free(map
->p
[i
]);
5403 if (remove_if_empty(map
, i
) < 0)
5413 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5414 enum isl_dim_type type
, unsigned pos
, int value
)
5418 map
= isl_map_cow(map
);
5422 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5423 for (i
= map
->n
- 1; i
>= 0; --i
) {
5424 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5425 if (remove_if_empty(map
, i
) < 0)
5428 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5435 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5436 enum isl_dim_type type
, unsigned pos
, int value
)
5438 return (struct isl_set
*)
5439 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5442 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5443 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5447 map
= isl_map_cow(map
);
5451 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5452 for (i
= 0; i
< map
->n
; ++i
) {
5453 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5457 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5464 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5465 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5467 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5470 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5471 unsigned input
, int value
)
5473 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5476 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5478 return (struct isl_set
*)
5479 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5482 static __isl_give isl_basic_map
*basic_map_bound_si(
5483 __isl_take isl_basic_map
*bmap
,
5484 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5490 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5491 pos
+= isl_basic_map_offset(bmap
, type
);
5492 bmap
= isl_basic_map_cow(bmap
);
5493 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5494 j
= isl_basic_map_alloc_inequality(bmap
);
5497 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5499 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5500 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5502 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5503 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5505 bmap
= isl_basic_map_simplify(bmap
);
5506 return isl_basic_map_finalize(bmap
);
5508 isl_basic_map_free(bmap
);
5512 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5513 __isl_take isl_basic_map
*bmap
,
5514 enum isl_dim_type type
, unsigned pos
, int value
)
5516 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5519 /* Constrain the values of the given dimension to be no greater than "value".
5521 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5522 __isl_take isl_basic_map
*bmap
,
5523 enum isl_dim_type type
, unsigned pos
, int value
)
5525 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5528 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5529 unsigned dim
, isl_int value
)
5533 bset
= isl_basic_set_cow(bset
);
5534 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5535 j
= isl_basic_set_alloc_inequality(bset
);
5538 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5539 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5540 isl_int_neg(bset
->ineq
[j
][0], value
);
5541 bset
= isl_basic_set_simplify(bset
);
5542 return isl_basic_set_finalize(bset
);
5544 isl_basic_set_free(bset
);
5548 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5549 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5553 map
= isl_map_cow(map
);
5557 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5558 for (i
= 0; i
< map
->n
; ++i
) {
5559 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5560 type
, pos
, value
, upper
);
5564 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5571 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5572 enum isl_dim_type type
, unsigned pos
, int value
)
5574 return map_bound_si(map
, type
, pos
, value
, 0);
5577 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5578 enum isl_dim_type type
, unsigned pos
, int value
)
5580 return map_bound_si(map
, type
, pos
, value
, 1);
5583 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5584 enum isl_dim_type type
, unsigned pos
, int value
)
5586 return (struct isl_set
*)
5587 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5590 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5591 enum isl_dim_type type
, unsigned pos
, int value
)
5593 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5596 /* Bound the given variable of "bmap" from below (or above is "upper"
5597 * is set) to "value".
5599 static __isl_give isl_basic_map
*basic_map_bound(
5600 __isl_take isl_basic_map
*bmap
,
5601 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5607 if (pos
>= isl_basic_map_dim(bmap
, type
))
5608 isl_die(bmap
->ctx
, isl_error_invalid
,
5609 "index out of bounds", goto error
);
5610 pos
+= isl_basic_map_offset(bmap
, type
);
5611 bmap
= isl_basic_map_cow(bmap
);
5612 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5613 j
= isl_basic_map_alloc_inequality(bmap
);
5616 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5618 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5619 isl_int_set(bmap
->ineq
[j
][0], value
);
5621 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5622 isl_int_neg(bmap
->ineq
[j
][0], value
);
5624 bmap
= isl_basic_map_simplify(bmap
);
5625 return isl_basic_map_finalize(bmap
);
5627 isl_basic_map_free(bmap
);
5631 /* Bound the given variable of "map" from below (or above is "upper"
5632 * is set) to "value".
5634 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5635 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5639 map
= isl_map_cow(map
);
5643 if (pos
>= isl_map_dim(map
, type
))
5644 isl_die(map
->ctx
, isl_error_invalid
,
5645 "index out of bounds", goto error
);
5646 for (i
= map
->n
- 1; i
>= 0; --i
) {
5647 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5648 if (remove_if_empty(map
, i
) < 0)
5651 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5658 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5659 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5661 return map_bound(map
, type
, pos
, value
, 0);
5664 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5665 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5667 return map_bound(map
, type
, pos
, value
, 1);
5670 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5671 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5673 return isl_map_lower_bound(set
, type
, pos
, value
);
5676 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5677 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5679 return isl_map_upper_bound(set
, type
, pos
, value
);
5682 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5687 set
= isl_set_cow(set
);
5691 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5692 for (i
= 0; i
< set
->n
; ++i
) {
5693 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5703 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5707 map
= isl_map_cow(map
);
5711 map
->dim
= isl_space_reverse(map
->dim
);
5714 for (i
= 0; i
< map
->n
; ++i
) {
5715 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5719 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5726 static struct isl_map
*isl_basic_map_partial_lexopt(
5727 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5728 struct isl_set
**empty
, int max
)
5732 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5733 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5735 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5737 isl_basic_map_free(bmap
);
5738 isl_basic_set_free(dom
);
5744 struct isl_map
*isl_basic_map_partial_lexmax(
5745 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5746 struct isl_set
**empty
)
5748 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5751 struct isl_map
*isl_basic_map_partial_lexmin(
5752 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5753 struct isl_set
**empty
)
5755 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5758 struct isl_set
*isl_basic_set_partial_lexmin(
5759 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5760 struct isl_set
**empty
)
5762 return (struct isl_set
*)
5763 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5767 struct isl_set
*isl_basic_set_partial_lexmax(
5768 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5769 struct isl_set
**empty
)
5771 return (struct isl_set
*)
5772 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5776 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5777 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5778 __isl_give isl_set
**empty
)
5780 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5783 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5784 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5785 __isl_give isl_set
**empty
)
5787 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5790 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5791 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5792 __isl_give isl_set
**empty
)
5794 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5797 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5798 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5799 __isl_give isl_set
**empty
)
5801 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5804 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5805 __isl_take isl_basic_map
*bmap
, int max
)
5807 isl_basic_set
*dom
= NULL
;
5808 isl_space
*dom_space
;
5812 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5813 dom
= isl_basic_set_universe(dom_space
);
5814 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5816 isl_basic_map_free(bmap
);
5820 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5821 __isl_take isl_basic_map
*bmap
)
5823 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5827 #define TYPE isl_pw_multi_aff
5829 #define SUFFIX _pw_multi_aff
5831 #define EMPTY isl_pw_multi_aff_empty
5833 #define ADD isl_pw_multi_aff_union_add
5834 #include "isl_map_lexopt_templ.c"
5836 /* Given a map "map", compute the lexicographically minimal
5837 * (or maximal) image element for each domain element in dom,
5838 * in the form of an isl_pw_multi_aff.
5839 * Set *empty to those elements in dom that do not have an image element.
5841 * We first compute the lexicographically minimal or maximal element
5842 * in the first basic map. This results in a partial solution "res"
5843 * and a subset "todo" of dom that still need to be handled.
5844 * We then consider each of the remaining maps in "map" and successively
5845 * update both "res" and "todo".
5847 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
5848 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5849 __isl_give isl_set
**empty
, int max
)
5852 isl_pw_multi_aff
*res
;
5858 if (isl_map_plain_is_empty(map
)) {
5863 return isl_pw_multi_aff_from_map(map
);
5866 res
= basic_map_partial_lexopt_pw_multi_aff(
5867 isl_basic_map_copy(map
->p
[0]),
5868 isl_set_copy(dom
), &todo
, max
);
5870 for (i
= 1; i
< map
->n
; ++i
) {
5871 isl_pw_multi_aff
*res_i
;
5874 res_i
= basic_map_partial_lexopt_pw_multi_aff(
5875 isl_basic_map_copy(map
->p
[i
]),
5876 isl_set_copy(dom
), &todo_i
, max
);
5879 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
5881 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
5883 todo
= isl_set_intersect(todo
, todo_i
);
5904 #define TYPE isl_map
5908 #define EMPTY isl_map_empty
5910 #define ADD isl_map_union_disjoint
5911 #include "isl_map_lexopt_templ.c"
5913 /* Given a map "map", compute the lexicographically minimal
5914 * (or maximal) image element for each domain element in dom.
5915 * Set *empty to those elements in dom that do not have an image element.
5917 * We first compute the lexicographically minimal or maximal element
5918 * in the first basic map. This results in a partial solution "res"
5919 * and a subset "todo" of dom that still need to be handled.
5920 * We then consider each of the remaining maps in "map" and successively
5921 * update both "res" and "todo".
5923 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5924 * Assume we are computing the lexicographical maximum.
5925 * We first compute the lexicographically maximal element in basic map i.
5926 * This results in a partial solution res_i and a subset todo_i.
5927 * Then we combine these results with those obtain for the first k basic maps
5928 * to obtain a result that is valid for the first k+1 basic maps.
5929 * In particular, the set where there is no solution is the set where
5930 * there is no solution for the first k basic maps and also no solution
5931 * for the ith basic map, i.e.,
5933 * todo^i = todo^k * todo_i
5935 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5936 * solutions, arbitrarily breaking ties in favor of res^k.
5937 * That is, when res^k(a) >= res_i(a), we pick res^k and
5938 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5939 * the lexicographic order.)
5940 * In practice, we compute
5942 * res^k * (res_i . "<=")
5946 * res_i * (res^k . "<")
5948 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5949 * where only one of res^k and res_i provides a solution and we simply pick
5956 * Note that we only compute these intersections when dom(res^k) intersects
5957 * dom(res_i). Otherwise, the only effect of these intersections is to
5958 * potentially break up res^k and res_i into smaller pieces.
5959 * We want to avoid such splintering as much as possible.
5960 * In fact, an earlier implementation of this function would look for
5961 * better results in the domain of res^k and for extra results in todo^k,
5962 * but this would always result in a splintering according to todo^k,
5963 * even when the domain of basic map i is disjoint from the domains of
5964 * the previous basic maps.
5966 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5967 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5968 __isl_give isl_set
**empty
, int max
)
5971 struct isl_map
*res
;
5972 struct isl_set
*todo
;
5977 if (isl_map_plain_is_empty(map
)) {
5985 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5986 isl_set_copy(dom
), &todo
, max
);
5988 for (i
= 1; i
< map
->n
; ++i
) {
5992 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5994 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5995 isl_set_copy(dom
), &todo_i
, max
);
5998 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5999 le
= isl_map_lex_le(dim
);
6001 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6002 le
= isl_map_lex_ge(dim
);
6004 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6005 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6006 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6007 le
= isl_map_intersect(le
, isl_map_copy(res
));
6009 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6010 res
= isl_map_intersect_domain(res
,
6011 isl_set_copy(todo_i
));
6012 res_i
= isl_map_intersect_domain(res_i
,
6013 isl_set_copy(todo
));
6016 res
= isl_map_union_disjoint(res
, res_i
);
6017 res
= isl_map_union_disjoint(res
, lt
);
6018 res
= isl_map_union_disjoint(res
, le
);
6020 todo
= isl_set_intersect(todo
, todo_i
);
6040 __isl_give isl_map
*isl_map_partial_lexmax(
6041 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6042 __isl_give isl_set
**empty
)
6044 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6047 __isl_give isl_map
*isl_map_partial_lexmin(
6048 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6049 __isl_give isl_set
**empty
)
6051 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6054 __isl_give isl_set
*isl_set_partial_lexmin(
6055 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6056 __isl_give isl_set
**empty
)
6058 return (struct isl_set
*)
6059 isl_map_partial_lexmin((struct isl_map
*)set
,
6063 __isl_give isl_set
*isl_set_partial_lexmax(
6064 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6065 __isl_give isl_set
**empty
)
6067 return (struct isl_set
*)
6068 isl_map_partial_lexmax((struct isl_map
*)set
,
6072 /* Compute the lexicographic minimum (or maximum if "max" is set)
6073 * of "bmap" over its domain.
6075 * Since we are not interested in the part of the domain space where
6076 * there is no solution, we initialize the domain to those constraints
6077 * of "bmap" that only involve the parameters and the input dimensions.
6078 * This relieves the parametric programming engine from detecting those
6079 * inequalities and transferring them to the context. More importantly,
6080 * it ensures that those inequalities are transferred first and not
6081 * intermixed with inequalities that actually split the domain.
6083 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6087 isl_basic_map
*copy
;
6090 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6091 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6092 copy
= isl_basic_map_copy(bmap
);
6093 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6094 isl_dim_div
, 0, n_div
);
6095 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6096 isl_dim_out
, 0, n_out
);
6097 dom
= isl_basic_map_domain(copy
);
6098 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6101 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6103 return isl_basic_map_lexopt(bmap
, 0);
6106 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6108 return isl_basic_map_lexopt(bmap
, 1);
6111 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6113 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6116 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6118 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6121 /* Extract the first and only affine expression from list
6122 * and then add it to *pwaff with the given dom.
6123 * This domain is known to be disjoint from other domains
6124 * because of the way isl_basic_map_foreach_lexmax works.
6126 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6127 __isl_take isl_aff_list
*list
, void *user
)
6129 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6131 isl_pw_aff
**pwaff
= user
;
6132 isl_pw_aff
*pwaff_i
;
6136 if (isl_aff_list_n_aff(list
) != 1)
6137 isl_die(ctx
, isl_error_internal
,
6138 "expecting single element list", goto error
);
6140 aff
= isl_aff_list_get_aff(list
, 0);
6141 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6143 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6145 isl_aff_list_free(list
);
6149 isl_basic_set_free(dom
);
6150 isl_aff_list_free(list
);
6154 /* Given a basic map with one output dimension, compute the minimum or
6155 * maximum of that dimension as an isl_pw_aff.
6157 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6158 * call update_dim_opt on each leaf of the result.
6160 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6163 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6167 dim
= isl_space_from_domain(isl_space_domain(dim
));
6168 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6169 pwaff
= isl_pw_aff_empty(dim
);
6171 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6173 return isl_pw_aff_free(pwaff
);
6178 /* Compute the minimum or maximum of the given output dimension
6179 * as a function of the parameters and the input dimensions,
6180 * but independently of the other output dimensions.
6182 * We first project out the other output dimension and then compute
6183 * the "lexicographic" maximum in each basic map, combining the results
6184 * using isl_pw_aff_union_max.
6186 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6193 n_out
= isl_map_dim(map
, isl_dim_out
);
6194 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6195 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6200 isl_space
*dim
= isl_map_get_space(map
);
6201 dim
= isl_space_domain(isl_space_from_range(dim
));
6203 return isl_pw_aff_empty(dim
);
6206 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6207 for (i
= 1; i
< map
->n
; ++i
) {
6208 isl_pw_aff
*pwaff_i
;
6210 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6211 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6219 /* Compute the maximum of the given output dimension as a function of the
6220 * parameters and input dimensions, but independently of
6221 * the other output dimensions.
6223 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6225 return map_dim_opt(map
, pos
, 1);
6228 /* Compute the minimum or maximum of the given set dimension
6229 * as a function of the parameters,
6230 * but independently of the other set dimensions.
6232 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6235 return map_dim_opt(set
, pos
, max
);
6238 /* Compute the maximum of the given set dimension as a function of the
6239 * parameters, but independently of the other set dimensions.
6241 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6243 return set_dim_opt(set
, pos
, 1);
6246 /* Compute the minimum of the given set dimension as a function of the
6247 * parameters, but independently of the other set dimensions.
6249 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6251 return set_dim_opt(set
, pos
, 0);
6254 /* Apply a preimage specified by "mat" on the parameters of "bset".
6255 * bset is assumed to have only parameters and divs.
6257 static struct isl_basic_set
*basic_set_parameter_preimage(
6258 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6265 bset
->dim
= isl_space_cow(bset
->dim
);
6269 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6271 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6273 bset
->dim
->nparam
= 0;
6274 bset
->dim
->n_out
= nparam
;
6275 bset
= isl_basic_set_preimage(bset
, mat
);
6277 bset
->dim
->nparam
= bset
->dim
->n_out
;
6278 bset
->dim
->n_out
= 0;
6283 isl_basic_set_free(bset
);
6287 /* Apply a preimage specified by "mat" on the parameters of "set".
6288 * set is assumed to have only parameters and divs.
6290 static struct isl_set
*set_parameter_preimage(
6291 struct isl_set
*set
, struct isl_mat
*mat
)
6293 isl_space
*dim
= NULL
;
6299 dim
= isl_space_copy(set
->dim
);
6300 dim
= isl_space_cow(dim
);
6304 nparam
= isl_set_dim(set
, isl_dim_param
);
6306 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6309 dim
->n_out
= nparam
;
6310 isl_set_reset_space(set
, dim
);
6311 set
= isl_set_preimage(set
, mat
);
6314 dim
= isl_space_copy(set
->dim
);
6315 dim
= isl_space_cow(dim
);
6318 dim
->nparam
= dim
->n_out
;
6320 isl_set_reset_space(set
, dim
);
6323 isl_space_free(dim
);
6330 /* Intersect the basic set "bset" with the affine space specified by the
6331 * equalities in "eq".
6333 static struct isl_basic_set
*basic_set_append_equalities(
6334 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6342 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6347 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6348 for (i
= 0; i
< eq
->n_row
; ++i
) {
6349 k
= isl_basic_set_alloc_equality(bset
);
6352 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6353 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6357 bset
= isl_basic_set_gauss(bset
, NULL
);
6358 bset
= isl_basic_set_finalize(bset
);
6363 isl_basic_set_free(bset
);
6367 /* Intersect the set "set" with the affine space specified by the
6368 * equalities in "eq".
6370 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6378 for (i
= 0; i
< set
->n
; ++i
) {
6379 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6392 /* Given a basic set "bset" that only involves parameters and existentially
6393 * quantified variables, return the index of the first equality
6394 * that only involves parameters. If there is no such equality then
6395 * return bset->n_eq.
6397 * This function assumes that isl_basic_set_gauss has been called on "bset".
6399 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6402 unsigned nparam
, n_div
;
6407 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6408 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6410 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6411 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6418 /* Compute an explicit representation for the existentially quantified
6419 * variables in "bset" by computing the "minimal value" of the set
6420 * variables. Since there are no set variables, the computation of
6421 * the minimal value essentially computes an explicit representation
6422 * of the non-empty part(s) of "bset".
6424 * The input only involves parameters and existentially quantified variables.
6425 * All equalities among parameters have been removed.
6427 * Since the existentially quantified variables in the result are in general
6428 * going to be different from those in the input, we first replace
6429 * them by the minimal number of variables based on their equalities.
6430 * This should simplify the parametric integer programming.
6432 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6434 isl_morph
*morph1
, *morph2
;
6440 if (bset
->n_eq
== 0)
6441 return isl_basic_set_lexmin(bset
);
6443 morph1
= isl_basic_set_parameter_compression(bset
);
6444 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6445 bset
= isl_basic_set_lift(bset
);
6446 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6447 bset
= isl_morph_basic_set(morph2
, bset
);
6448 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6449 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6451 set
= isl_basic_set_lexmin(bset
);
6453 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6458 /* Project the given basic set onto its parameter domain, possibly introducing
6459 * new, explicit, existential variables in the constraints.
6460 * The input has parameters and (possibly implicit) existential variables.
6461 * The output has the same parameters, but only
6462 * explicit existentially quantified variables.
6464 * The actual projection is performed by pip, but pip doesn't seem
6465 * to like equalities very much, so we first remove the equalities
6466 * among the parameters by performing a variable compression on
6467 * the parameters. Afterward, an inverse transformation is performed
6468 * and the equalities among the parameters are inserted back in.
6470 * The variable compression on the parameters may uncover additional
6471 * equalities that were only implicit before. We therefore check
6472 * if there are any new parameter equalities in the result and
6473 * if so recurse. The removal of parameter equalities is required
6474 * for the parameter compression performed by base_compute_divs.
6476 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6480 struct isl_mat
*T
, *T2
;
6481 struct isl_set
*set
;
6484 bset
= isl_basic_set_cow(bset
);
6488 if (bset
->n_eq
== 0)
6489 return base_compute_divs(bset
);
6491 bset
= isl_basic_set_gauss(bset
, NULL
);
6494 if (isl_basic_set_plain_is_empty(bset
))
6495 return isl_set_from_basic_set(bset
);
6497 i
= first_parameter_equality(bset
);
6498 if (i
== bset
->n_eq
)
6499 return base_compute_divs(bset
);
6501 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6502 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6504 eq
= isl_mat_cow(eq
);
6505 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6506 if (T
&& T
->n_col
== 0) {
6510 bset
= isl_basic_set_set_to_empty(bset
);
6511 return isl_set_from_basic_set(bset
);
6513 bset
= basic_set_parameter_preimage(bset
, T
);
6515 i
= first_parameter_equality(bset
);
6518 else if (i
== bset
->n_eq
)
6519 set
= base_compute_divs(bset
);
6521 set
= parameter_compute_divs(bset
);
6522 set
= set_parameter_preimage(set
, T2
);
6523 set
= set_append_equalities(set
, eq
);
6527 /* Insert the divs from "ls" before those of "bmap".
6529 * The number of columns is not changed, which means that the last
6530 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6531 * The caller is responsible for removing the same number of dimensions
6532 * from the space of "bmap".
6534 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6535 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6541 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6545 old_n_div
= bmap
->n_div
;
6546 bmap
= insert_div_rows(bmap
, n_div
);
6550 for (i
= 0; i
< n_div
; ++i
) {
6551 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6552 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6558 /* Replace the space of "bmap" by the space and divs of "ls".
6560 * If "ls" has any divs, then we simplify the result since we may
6561 * have discovered some additional equalities that could simplify
6562 * the div expressions.
6564 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6565 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6569 bmap
= isl_basic_map_cow(bmap
);
6573 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6574 bmap
= insert_divs_from_local_space(bmap
, ls
);
6578 isl_space_free(bmap
->dim
);
6579 bmap
->dim
= isl_local_space_get_space(ls
);
6583 isl_local_space_free(ls
);
6585 bmap
= isl_basic_map_simplify(bmap
);
6586 bmap
= isl_basic_map_finalize(bmap
);
6589 isl_basic_map_free(bmap
);
6590 isl_local_space_free(ls
);
6594 /* Replace the space of "map" by the space and divs of "ls".
6596 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6597 __isl_take isl_local_space
*ls
)
6601 map
= isl_map_cow(map
);
6605 for (i
= 0; i
< map
->n
; ++i
) {
6606 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6607 isl_local_space_copy(ls
));
6611 isl_space_free(map
->dim
);
6612 map
->dim
= isl_local_space_get_space(ls
);
6616 isl_local_space_free(ls
);
6619 isl_local_space_free(ls
);
6624 /* Compute an explicit representation for the existentially
6625 * quantified variables for which do not know any explicit representation yet.
6627 * We first sort the existentially quantified variables so that the
6628 * existentially quantified variables for which we already have an explicit
6629 * representation are placed before those for which we do not.
6630 * The input dimensions, the output dimensions and the existentially
6631 * quantified variables for which we already have an explicit
6632 * representation are then turned into parameters.
6633 * compute_divs returns a map with the same parameters and
6634 * no input or output dimensions and the dimension specification
6635 * is reset to that of the input, including the existentially quantified
6636 * variables for which we already had an explicit representation.
6638 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6640 struct isl_basic_set
*bset
;
6641 struct isl_set
*set
;
6642 struct isl_map
*map
;
6644 isl_local_space
*ls
;
6651 bmap
= isl_basic_map_sort_divs(bmap
);
6652 bmap
= isl_basic_map_cow(bmap
);
6656 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6657 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6660 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6661 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6662 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6663 dim
= isl_space_set_alloc(bmap
->ctx
,
6664 nparam
+ n_in
+ n_out
+ n_known
, 0);
6668 ls
= isl_basic_map_get_local_space(bmap
);
6669 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6670 n_known
, bmap
->n_div
- n_known
);
6672 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6673 swap_div(bmap
, i
- n_known
, i
);
6674 bmap
->n_div
-= n_known
;
6675 bmap
->extra
-= n_known
;
6677 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6678 bset
= (struct isl_basic_set
*)bmap
;
6680 set
= parameter_compute_divs(bset
);
6681 map
= (struct isl_map
*)set
;
6682 map
= replace_space_by_local_space(map
, ls
);
6686 isl_basic_map_free(bmap
);
6690 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6698 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6699 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6700 if (isl_int_is_zero(bmap
->div
[i
][0]))
6702 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6708 static int map_divs_known(__isl_keep isl_map
*map
)
6715 for (i
= 0; i
< map
->n
; ++i
) {
6716 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6724 /* If bmap contains any unknown divs, then compute explicit
6725 * expressions for them. However, this computation may be
6726 * quite expensive, so first try to remove divs that aren't
6729 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6732 struct isl_map
*map
;
6734 known
= isl_basic_map_divs_known(bmap
);
6738 return isl_map_from_basic_map(bmap
);
6740 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6742 known
= isl_basic_map_divs_known(bmap
);
6746 return isl_map_from_basic_map(bmap
);
6748 map
= compute_divs(bmap
);
6751 isl_basic_map_free(bmap
);
6755 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6759 struct isl_map
*res
;
6766 known
= map_divs_known(map
);
6774 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6775 for (i
= 1 ; i
< map
->n
; ++i
) {
6777 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6778 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6779 res
= isl_map_union_disjoint(res
, r2
);
6781 res
= isl_map_union(res
, r2
);
6788 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6790 return (struct isl_set
*)
6791 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6794 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6796 return (struct isl_set
*)
6797 isl_map_compute_divs((struct isl_map
*)set
);
6800 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6803 struct isl_set
*set
;
6808 map
= isl_map_cow(map
);
6812 set
= (struct isl_set
*)map
;
6813 set
->dim
= isl_space_domain(set
->dim
);
6816 for (i
= 0; i
< map
->n
; ++i
) {
6817 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6821 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6822 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6829 /* Return the union of "map1" and "map2", where we assume for now that
6830 * "map1" and "map2" are disjoint. Note that the basic maps inside
6831 * "map1" or "map2" may not be disjoint from each other.
6832 * Also note that this function is also called from isl_map_union,
6833 * which takes care of handling the situation where "map1" and "map2"
6834 * may not be disjoint.
6836 * If one of the inputs is empty, we can simply return the other input.
6837 * Similarly, if one of the inputs is universal, then it is equal to the union.
6839 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6840 __isl_take isl_map
*map2
)
6844 struct isl_map
*map
= NULL
;
6859 is_universe
= isl_map_plain_is_universe(map1
);
6860 if (is_universe
< 0)
6867 is_universe
= isl_map_plain_is_universe(map2
);
6868 if (is_universe
< 0)
6875 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6877 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6878 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6879 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6881 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6882 map1
->n
+ map2
->n
, flags
);
6885 for (i
= 0; i
< map1
->n
; ++i
) {
6886 map
= isl_map_add_basic_map(map
,
6887 isl_basic_map_copy(map1
->p
[i
]));
6891 for (i
= 0; i
< map2
->n
; ++i
) {
6892 map
= isl_map_add_basic_map(map
,
6893 isl_basic_map_copy(map2
->p
[i
]));
6907 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6908 __isl_take isl_map
*map2
)
6910 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6913 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6915 map1
= isl_map_union_disjoint(map1
, map2
);
6919 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6923 struct isl_set
*isl_set_union_disjoint(
6924 struct isl_set
*set1
, struct isl_set
*set2
)
6926 return (struct isl_set
*)
6927 isl_map_union_disjoint(
6928 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6931 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6933 return (struct isl_set
*)
6934 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6937 /* Apply "fn" to pairs of elements from "map" and "set" and collect
6940 * "map" and "set" are assumed to be compatible and non-NULL.
6942 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
6943 __isl_take isl_set
*set
,
6944 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
6945 __isl_take isl_basic_set
*bset
))
6948 struct isl_map
*result
;
6951 if (isl_set_plain_is_universe(set
)) {
6956 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6957 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6958 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6960 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6961 map
->n
* set
->n
, flags
);
6962 for (i
= 0; result
&& i
< map
->n
; ++i
)
6963 for (j
= 0; j
< set
->n
; ++j
) {
6964 result
= isl_map_add_basic_map(result
,
6965 fn(isl_basic_map_copy(map
->p
[i
]),
6966 isl_basic_set_copy(set
->p
[j
])));
6976 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6977 __isl_take isl_set
*set
)
6982 if (!isl_map_compatible_range(map
, set
))
6983 isl_die(set
->ctx
, isl_error_invalid
,
6984 "incompatible spaces", goto error
);
6986 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
6993 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6994 __isl_take isl_set
*set
)
6996 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6999 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7000 __isl_take isl_set
*set
)
7005 if (!isl_map_compatible_domain(map
, set
))
7006 isl_die(set
->ctx
, isl_error_invalid
,
7007 "incompatible spaces", goto error
);
7009 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7016 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7017 __isl_take isl_set
*set
)
7019 return isl_map_align_params_map_map_and(map
, set
,
7020 &map_intersect_domain
);
7023 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7024 __isl_take isl_map
*map2
)
7028 map1
= isl_map_reverse(map1
);
7029 map1
= isl_map_apply_range(map1
, map2
);
7030 return isl_map_reverse(map1
);
7037 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7038 __isl_take isl_map
*map2
)
7040 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7043 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7044 __isl_take isl_map
*map2
)
7046 isl_space
*dim_result
;
7047 struct isl_map
*result
;
7053 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7054 isl_space_copy(map2
->dim
));
7056 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7059 for (i
= 0; i
< map1
->n
; ++i
)
7060 for (j
= 0; j
< map2
->n
; ++j
) {
7061 result
= isl_map_add_basic_map(result
,
7062 isl_basic_map_apply_range(
7063 isl_basic_map_copy(map1
->p
[i
]),
7064 isl_basic_map_copy(map2
->p
[j
])));
7070 if (result
&& result
->n
<= 1)
7071 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7079 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7080 __isl_take isl_map
*map2
)
7082 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7086 * returns range - domain
7088 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7090 isl_space
*dims
, *target_dim
;
7091 struct isl_basic_set
*bset
;
7098 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7099 bmap
->dim
, isl_dim_out
),
7101 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7102 dim
= isl_basic_map_n_in(bmap
);
7103 nparam
= isl_basic_map_n_param(bmap
);
7104 bset
= isl_basic_set_from_basic_map(bmap
);
7105 bset
= isl_basic_set_cow(bset
);
7106 dims
= isl_basic_set_get_space(bset
);
7107 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7108 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7109 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7110 for (i
= 0; i
< dim
; ++i
) {
7111 int j
= isl_basic_map_alloc_equality(
7112 (struct isl_basic_map
*)bset
);
7114 bset
= isl_basic_set_free(bset
);
7117 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7118 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7119 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7120 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7122 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7123 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7126 isl_basic_map_free(bmap
);
7131 * returns range - domain
7133 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7137 struct isl_set
*result
;
7142 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7143 map
->dim
, isl_dim_out
),
7145 dim
= isl_map_get_space(map
);
7146 dim
= isl_space_domain(dim
);
7147 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7150 for (i
= 0; i
< map
->n
; ++i
)
7151 result
= isl_set_add_basic_set(result
,
7152 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7161 * returns [domain -> range] -> range - domain
7163 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7164 __isl_take isl_basic_map
*bmap
)
7168 isl_basic_map
*domain
;
7172 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7173 isl_die(bmap
->ctx
, isl_error_invalid
,
7174 "domain and range don't match", goto error
);
7176 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7177 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7179 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7180 domain
= isl_basic_map_universe(dim
);
7182 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7183 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7184 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7186 total
= isl_basic_map_total_dim(bmap
);
7188 for (i
= 0; i
< n
; ++i
) {
7189 k
= isl_basic_map_alloc_equality(bmap
);
7192 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7193 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7194 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7195 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7198 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7199 return isl_basic_map_finalize(bmap
);
7201 isl_basic_map_free(bmap
);
7206 * returns [domain -> range] -> range - domain
7208 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7211 isl_space
*domain_dim
;
7216 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7217 isl_die(map
->ctx
, isl_error_invalid
,
7218 "domain and range don't match", goto error
);
7220 map
= isl_map_cow(map
);
7224 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7225 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7226 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7229 for (i
= 0; i
< map
->n
; ++i
) {
7230 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7234 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7241 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7243 struct isl_basic_map
*bmap
;
7251 nparam
= dims
->nparam
;
7253 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7257 for (i
= 0; i
< dim
; ++i
) {
7258 int j
= isl_basic_map_alloc_equality(bmap
);
7261 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7262 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7263 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7265 return isl_basic_map_finalize(bmap
);
7267 isl_basic_map_free(bmap
);
7271 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7275 if (dim
->n_in
!= dim
->n_out
)
7276 isl_die(dim
->ctx
, isl_error_invalid
,
7277 "number of input and output dimensions needs to be "
7278 "the same", goto error
);
7279 return basic_map_identity(dim
);
7281 isl_space_free(dim
);
7285 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7287 if (!model
|| !model
->dim
)
7289 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7292 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7294 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7297 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7299 if (!model
|| !model
->dim
)
7301 return isl_map_identity(isl_space_copy(model
->dim
));
7304 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7306 if (!model
|| !model
->dim
)
7308 return isl_map_identity(isl_space_copy(model
->dim
));
7311 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7313 isl_space
*dim
= isl_set_get_space(set
);
7315 id
= isl_map_identity(isl_space_map_from_set(dim
));
7316 return isl_map_intersect_range(id
, set
);
7319 /* Construct a basic set with all set dimensions having only non-negative
7322 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7323 __isl_take isl_space
*space
)
7328 struct isl_basic_set
*bset
;
7332 nparam
= space
->nparam
;
7334 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7337 for (i
= 0; i
< dim
; ++i
) {
7338 int k
= isl_basic_set_alloc_inequality(bset
);
7341 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7342 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7346 isl_basic_set_free(bset
);
7350 /* Construct the half-space x_pos >= 0.
7352 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7356 isl_basic_set
*nonneg
;
7358 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7359 k
= isl_basic_set_alloc_inequality(nonneg
);
7362 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7363 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7365 return isl_basic_set_finalize(nonneg
);
7367 isl_basic_set_free(nonneg
);
7371 /* Construct the half-space x_pos <= -1.
7373 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7378 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7379 k
= isl_basic_set_alloc_inequality(neg
);
7382 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7383 isl_int_set_si(neg
->ineq
[k
][0], -1);
7384 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7386 return isl_basic_set_finalize(neg
);
7388 isl_basic_set_free(neg
);
7392 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7393 enum isl_dim_type type
, unsigned first
, unsigned n
)
7396 isl_basic_set
*nonneg
;
7404 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7406 for (i
= 0; i
< n
; ++i
) {
7407 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7408 pos(set
->dim
, type
) + first
+ i
);
7409 neg
= neg_halfspace(isl_set_get_space(set
),
7410 pos(set
->dim
, type
) + first
+ i
);
7412 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7421 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7422 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7429 if (isl_set_plain_is_empty(set
)) {
7434 return fn(set
, signs
, user
);
7437 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7439 half
= isl_set_intersect(half
, isl_set_copy(set
));
7440 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7444 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7446 half
= isl_set_intersect(half
, set
);
7447 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7453 /* Call "fn" on the intersections of "set" with each of the orthants
7454 * (except for obviously empty intersections). The orthant is identified
7455 * by the signs array, with each entry having value 1 or -1 according
7456 * to the sign of the corresponding variable.
7458 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7459 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7469 if (isl_set_plain_is_empty(set
))
7472 nparam
= isl_set_dim(set
, isl_dim_param
);
7473 nvar
= isl_set_dim(set
, isl_dim_set
);
7475 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7477 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7485 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7487 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7490 int isl_basic_map_is_subset(
7491 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7494 struct isl_map
*map1
;
7495 struct isl_map
*map2
;
7497 if (!bmap1
|| !bmap2
)
7500 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7501 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7503 is_subset
= isl_map_is_subset(map1
, map2
);
7511 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7512 __isl_keep isl_basic_set
*bset2
)
7514 return isl_basic_map_is_subset(bset1
, bset2
);
7517 int isl_basic_map_is_equal(
7518 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7522 if (!bmap1
|| !bmap2
)
7524 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7527 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7531 int isl_basic_set_is_equal(
7532 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7534 return isl_basic_map_is_equal(
7535 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7538 int isl_map_is_empty(struct isl_map
*map
)
7545 for (i
= 0; i
< map
->n
; ++i
) {
7546 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7555 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7557 return map
? map
->n
== 0 : -1;
7560 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7562 return isl_map_plain_is_empty(map
);
7565 int isl_set_plain_is_empty(struct isl_set
*set
)
7567 return set
? set
->n
== 0 : -1;
7570 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7572 return isl_set_plain_is_empty(set
);
7575 int isl_set_is_empty(struct isl_set
*set
)
7577 return isl_map_is_empty((struct isl_map
*)set
);
7580 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7585 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7588 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7593 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7596 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7602 is_subset
= isl_map_is_subset(map1
, map2
);
7605 is_subset
= isl_map_is_subset(map2
, map1
);
7609 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7611 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7614 int isl_basic_map_is_strict_subset(
7615 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7619 if (!bmap1
|| !bmap2
)
7621 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7624 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7625 if (is_subset
== -1)
7630 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7636 is_subset
= isl_map_is_subset(map1
, map2
);
7639 is_subset
= isl_map_is_subset(map2
, map1
);
7640 if (is_subset
== -1)
7645 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7647 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7650 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7654 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7657 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7661 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7664 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7671 for (i
= 0; i
< map
->n
; ++i
) {
7672 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7680 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7682 return isl_map_plain_is_universe((isl_map
*) set
);
7685 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7687 return isl_set_plain_is_universe(set
);
7690 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7692 struct isl_basic_set
*bset
= NULL
;
7693 struct isl_vec
*sample
= NULL
;
7700 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7703 if (isl_basic_map_is_universe(bmap
))
7706 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7707 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7708 copy
= isl_basic_map_remove_redundancies(copy
);
7709 empty
= isl_basic_map_plain_is_empty(copy
);
7710 isl_basic_map_free(copy
);
7714 total
= 1 + isl_basic_map_total_dim(bmap
);
7715 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7716 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7722 isl_vec_free(bmap
->sample
);
7723 bmap
->sample
= NULL
;
7724 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7727 sample
= isl_basic_set_sample_vec(bset
);
7730 empty
= sample
->size
== 0;
7731 isl_vec_free(bmap
->sample
);
7732 bmap
->sample
= sample
;
7734 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7739 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7743 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7746 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7748 return isl_basic_map_plain_is_empty(bmap
);
7751 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7755 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7758 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7760 return isl_basic_set_plain_is_empty(bset
);
7763 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7765 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7768 struct isl_map
*isl_basic_map_union(
7769 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7771 struct isl_map
*map
;
7772 if (!bmap1
|| !bmap2
)
7775 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7777 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7780 map
= isl_map_add_basic_map(map
, bmap1
);
7781 map
= isl_map_add_basic_map(map
, bmap2
);
7784 isl_basic_map_free(bmap1
);
7785 isl_basic_map_free(bmap2
);
7789 struct isl_set
*isl_basic_set_union(
7790 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7792 return (struct isl_set
*)isl_basic_map_union(
7793 (struct isl_basic_map
*)bset1
,
7794 (struct isl_basic_map
*)bset2
);
7797 /* Order divs such that any div only depends on previous divs */
7798 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7806 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7808 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7810 if (isl_int_is_zero(bmap
->div
[i
][0]))
7812 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7816 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7822 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7824 return (struct isl_basic_set
*)
7825 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7828 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7835 for (i
= 0; i
< map
->n
; ++i
) {
7836 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7847 /* Apply the expansion computed by isl_merge_divs.
7848 * The expansion itself is given by "exp" while the resulting
7849 * list of divs is given by "div".
7851 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7852 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7857 bset
= isl_basic_set_cow(bset
);
7861 if (div
->n_row
< bset
->n_div
)
7862 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7863 "not an expansion", goto error
);
7865 n_div
= bset
->n_div
;
7866 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7867 div
->n_row
- n_div
, 0,
7868 2 * (div
->n_row
- n_div
));
7870 for (i
= n_div
; i
< div
->n_row
; ++i
)
7871 if (isl_basic_set_alloc_div(bset
) < 0)
7875 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7876 if (j
>= 0 && exp
[j
] == i
) {
7878 isl_basic_map_swap_div(bset
, i
, j
);
7881 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7882 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7890 isl_basic_set_free(bset
);
7895 /* Look for a div in dst that corresponds to the div "div" in src.
7896 * The divs before "div" in src and dst are assumed to be the same.
7898 * Returns -1 if no corresponding div was found and the position
7899 * of the corresponding div in dst otherwise.
7901 static int find_div(struct isl_basic_map
*dst
,
7902 struct isl_basic_map
*src
, unsigned div
)
7906 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7908 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7909 for (i
= div
; i
< dst
->n_div
; ++i
)
7910 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7911 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7912 dst
->n_div
- div
) == -1)
7917 struct isl_basic_map
*isl_basic_map_align_divs(
7918 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7926 if (src
->n_div
== 0)
7929 for (i
= 0; i
< src
->n_div
; ++i
)
7930 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7932 src
= isl_basic_map_order_divs(src
);
7933 dst
= isl_basic_map_cow(dst
);
7934 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7935 src
->n_div
, 0, 2 * src
->n_div
);
7938 total
= isl_space_dim(src
->dim
, isl_dim_all
);
7939 for (i
= 0; i
< src
->n_div
; ++i
) {
7940 int j
= find_div(dst
, src
, i
);
7942 j
= isl_basic_map_alloc_div(dst
);
7945 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7946 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7947 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7951 isl_basic_map_swap_div(dst
, i
, j
);
7955 isl_basic_map_free(dst
);
7959 struct isl_basic_set
*isl_basic_set_align_divs(
7960 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7962 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7963 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7966 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7974 map
= isl_map_compute_divs(map
);
7975 map
= isl_map_cow(map
);
7979 for (i
= 1; i
< map
->n
; ++i
)
7980 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7981 for (i
= 1; i
< map
->n
; ++i
)
7982 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7984 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7988 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7990 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7993 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7994 __isl_take isl_map
*map
)
7998 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7999 map
= isl_map_intersect_domain(map
, set
);
8000 set
= isl_map_range(map
);
8008 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8009 __isl_take isl_map
*map
)
8011 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8014 /* There is no need to cow as removing empty parts doesn't change
8015 * the meaning of the set.
8017 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8024 for (i
= map
->n
- 1; i
>= 0; --i
)
8025 remove_if_empty(map
, i
);
8030 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8032 return (struct isl_set
*)
8033 isl_map_remove_empty_parts((struct isl_map
*)set
);
8036 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8038 struct isl_basic_map
*bmap
;
8039 if (!map
|| map
->n
== 0)
8041 bmap
= map
->p
[map
->n
-1];
8042 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8043 return isl_basic_map_copy(bmap
);
8046 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8048 return (struct isl_basic_set
*)
8049 isl_map_copy_basic_map((struct isl_map
*)set
);
8052 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8053 __isl_keep isl_basic_map
*bmap
)
8059 for (i
= map
->n
-1; i
>= 0; --i
) {
8060 if (map
->p
[i
] != bmap
)
8062 map
= isl_map_cow(map
);
8065 isl_basic_map_free(map
->p
[i
]);
8066 if (i
!= map
->n
-1) {
8067 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8068 map
->p
[i
] = map
->p
[map
->n
-1];
8079 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8080 struct isl_basic_set
*bset
)
8082 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8083 (struct isl_basic_map
*)bset
);
8086 /* Given two basic sets bset1 and bset2, compute the maximal difference
8087 * between the values of dimension pos in bset1 and those in bset2
8088 * for any common value of the parameters and dimensions preceding pos.
8090 static enum isl_lp_result
basic_set_maximal_difference_at(
8091 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8092 int pos
, isl_int
*opt
)
8095 struct isl_basic_map
*bmap1
= NULL
;
8096 struct isl_basic_map
*bmap2
= NULL
;
8097 struct isl_ctx
*ctx
;
8098 struct isl_vec
*obj
;
8101 unsigned dim1
, dim2
;
8102 enum isl_lp_result res
;
8104 if (!bset1
|| !bset2
)
8105 return isl_lp_error
;
8107 nparam
= isl_basic_set_n_param(bset1
);
8108 dim1
= isl_basic_set_n_dim(bset1
);
8109 dim2
= isl_basic_set_n_dim(bset2
);
8110 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8111 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8112 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8113 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8114 if (!bmap1
|| !bmap2
)
8116 bmap1
= isl_basic_map_cow(bmap1
);
8117 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8118 pos
, (dim1
- pos
) + (dim2
- pos
),
8119 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8120 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8123 total
= isl_basic_map_total_dim(bmap1
);
8125 obj
= isl_vec_alloc(ctx
, 1 + total
);
8128 isl_seq_clr(obj
->block
.data
, 1 + total
);
8129 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8130 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8131 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8133 isl_basic_map_free(bmap1
);
8137 isl_basic_map_free(bmap2
);
8139 isl_basic_map_free(bmap1
);
8140 return isl_lp_error
;
8143 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8144 * for any common value of the parameters and dimensions preceding pos
8145 * in both basic sets, the values of dimension pos in bset1 are
8146 * smaller or larger than those in bset2.
8149 * 1 if bset1 follows bset2
8150 * -1 if bset1 precedes bset2
8151 * 0 if bset1 and bset2 are incomparable
8152 * -2 if some error occurred.
8154 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8155 struct isl_basic_set
*bset2
, int pos
)
8158 enum isl_lp_result res
;
8163 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8165 if (res
== isl_lp_empty
)
8167 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8168 res
== isl_lp_unbounded
)
8170 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8179 /* Given two basic sets bset1 and bset2, check whether
8180 * for any common value of the parameters and dimensions preceding pos
8181 * there is a value of dimension pos in bset1 that is larger
8182 * than a value of the same dimension in bset2.
8185 * 1 if there exists such a pair
8186 * 0 if there is no such pair, but there is a pair of equal values
8188 * -2 if some error occurred.
8190 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8191 __isl_keep isl_basic_set
*bset2
, int pos
)
8194 enum isl_lp_result res
;
8199 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8201 if (res
== isl_lp_empty
)
8203 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8204 res
== isl_lp_unbounded
)
8206 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8208 else if (res
== isl_lp_ok
)
8217 /* Given two sets set1 and set2, check whether
8218 * for any common value of the parameters and dimensions preceding pos
8219 * there is a value of dimension pos in set1 that is larger
8220 * than a value of the same dimension in set2.
8223 * 1 if there exists such a pair
8224 * 0 if there is no such pair, but there is a pair of equal values
8226 * -2 if some error occurred.
8228 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8229 __isl_keep isl_set
*set2
, int pos
)
8237 for (i
= 0; i
< set1
->n
; ++i
)
8238 for (j
= 0; j
< set2
->n
; ++j
) {
8240 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8241 if (f
== 1 || f
== -2)
8250 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8251 unsigned pos
, isl_int
*val
)
8259 total
= isl_basic_map_total_dim(bmap
);
8260 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8261 for (; d
+1 > pos
; --d
)
8262 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8266 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8268 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8270 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8273 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8279 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8280 unsigned pos
, isl_int
*val
)
8292 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8295 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8296 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8297 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8298 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8302 isl_int_set(*val
, v
);
8308 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8309 unsigned pos
, isl_int
*val
)
8311 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8315 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8318 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8321 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8322 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8324 if (pos
>= isl_basic_map_dim(bmap
, type
))
8326 return isl_basic_map_plain_has_fixed_var(bmap
,
8327 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8330 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8331 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8333 if (pos
>= isl_map_dim(map
, type
))
8335 return isl_map_plain_has_fixed_var(map
,
8336 map_offset(map
, type
) - 1 + pos
, val
);
8339 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8340 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8342 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8345 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8346 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8348 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8351 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8352 * then return this fixed value in *val.
8354 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8355 unsigned dim
, isl_int
*val
)
8357 return isl_basic_set_plain_has_fixed_var(bset
,
8358 isl_basic_set_n_param(bset
) + dim
, val
);
8361 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8362 * then return this fixed value in *val.
8364 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8365 unsigned dim
, isl_int
*val
)
8367 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8370 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8371 unsigned dim
, isl_int
*val
)
8373 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8376 /* Check if input variable in has fixed value and if so and if val is not NULL,
8377 * then return this fixed value in *val.
8379 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8380 unsigned in
, isl_int
*val
)
8382 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8385 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8386 * and if val is not NULL, then return this lower bound in *val.
8388 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8389 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8391 int i
, i_eq
= -1, i_ineq
= -1;
8398 total
= isl_basic_set_total_dim(bset
);
8399 nparam
= isl_basic_set_n_param(bset
);
8400 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8401 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8407 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8408 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8410 if (i_eq
!= -1 || i_ineq
!= -1)
8414 if (i_eq
== -1 && i_ineq
== -1)
8416 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8417 /* The coefficient should always be one due to normalization. */
8418 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8420 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8422 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8423 total
- nparam
- dim
- 1) != -1)
8426 isl_int_neg(*val
, c
[0]);
8430 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8431 unsigned dim
, isl_int
*val
)
8443 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8447 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8449 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8450 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8452 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8456 isl_int_set(*val
, v
);
8467 /* uset_gist depends on constraints without existentially quantified
8468 * variables sorting first.
8470 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8472 const struct constraint
*c1
= (const struct constraint
*)p1
;
8473 const struct constraint
*c2
= (const struct constraint
*)p2
;
8475 unsigned size
= isl_min(c1
->size
, c2
->size
);
8477 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8478 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8483 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8486 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8487 struct isl_basic_map
*bmap
)
8490 struct constraint
*c
;
8495 total
= isl_basic_map_total_dim(bmap
);
8496 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8499 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8501 c
[i
].c
= bmap
->ineq
[i
];
8503 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8504 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8505 bmap
->ineq
[i
] = c
[i
].c
;
8509 isl_basic_map_free(bmap
);
8513 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8514 __isl_take isl_basic_set
*bset
)
8516 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8517 (struct isl_basic_map
*)bset
);
8520 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8524 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8526 bmap
= isl_basic_map_remove_redundancies(bmap
);
8527 bmap
= isl_basic_map_sort_constraints(bmap
);
8529 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8533 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8535 return (struct isl_basic_set
*)isl_basic_map_normalize(
8536 (struct isl_basic_map
*)bset
);
8539 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8540 const __isl_keep isl_basic_map
*bmap2
)
8547 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8548 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8549 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8550 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8551 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8552 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8553 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8554 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8555 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8556 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8557 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8559 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8561 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8563 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8564 return bmap1
->n_eq
- bmap2
->n_eq
;
8565 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8566 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8567 if (bmap1
->n_div
!= bmap2
->n_div
)
8568 return bmap1
->n_div
- bmap2
->n_div
;
8569 total
= isl_basic_map_total_dim(bmap1
);
8570 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8571 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8575 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8576 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8580 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8581 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8588 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8589 const __isl_keep isl_basic_set
*bset2
)
8591 return isl_basic_map_plain_cmp(bset1
, bset2
);
8594 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8600 if (set1
->n
!= set2
->n
)
8601 return set1
->n
- set2
->n
;
8603 for (i
= 0; i
< set1
->n
; ++i
) {
8604 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8612 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8613 __isl_keep isl_basic_map
*bmap2
)
8615 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8618 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8619 __isl_keep isl_basic_set
*bset2
)
8621 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8622 (isl_basic_map
*)bset2
);
8625 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8627 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8628 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8630 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8633 /* We normalize in place, but if anything goes wrong we need
8634 * to return NULL, so we need to make sure we don't change the
8635 * meaning of any possible other copies of map.
8637 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8640 struct isl_basic_map
*bmap
;
8644 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8646 for (i
= 0; i
< map
->n
; ++i
) {
8647 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8650 isl_basic_map_free(map
->p
[i
]);
8653 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8654 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8655 map
= isl_map_remove_empty_parts(map
);
8658 for (i
= map
->n
- 1; i
>= 1; --i
) {
8659 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8661 isl_basic_map_free(map
->p
[i
-1]);
8662 for (j
= i
; j
< map
->n
; ++j
)
8663 map
->p
[j
-1] = map
->p
[j
];
8673 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8675 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8678 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8688 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8691 map1
= isl_map_copy(map1
);
8692 map2
= isl_map_copy(map2
);
8693 map1
= isl_map_normalize(map1
);
8694 map2
= isl_map_normalize(map2
);
8697 equal
= map1
->n
== map2
->n
;
8698 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8699 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8712 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8714 return isl_map_plain_is_equal(map1
, map2
);
8717 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8719 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8720 (struct isl_map
*)set2
);
8723 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8725 return isl_set_plain_is_equal(set1
, set2
);
8728 /* Return an interval that ranges from min to max (inclusive)
8730 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8731 isl_int min
, isl_int max
)
8734 struct isl_basic_set
*bset
= NULL
;
8736 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8740 k
= isl_basic_set_alloc_inequality(bset
);
8743 isl_int_set_si(bset
->ineq
[k
][1], 1);
8744 isl_int_neg(bset
->ineq
[k
][0], min
);
8746 k
= isl_basic_set_alloc_inequality(bset
);
8749 isl_int_set_si(bset
->ineq
[k
][1], -1);
8750 isl_int_set(bset
->ineq
[k
][0], max
);
8754 isl_basic_set_free(bset
);
8758 /* Return the Cartesian product of the basic sets in list (in the given order).
8760 __isl_give isl_basic_set
*isl_basic_set_list_product(
8761 __isl_take
struct isl_basic_set_list
*list
)
8769 struct isl_basic_set
*product
= NULL
;
8773 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8774 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8775 nparam
= isl_basic_set_n_param(list
->p
[0]);
8776 dim
= isl_basic_set_n_dim(list
->p
[0]);
8777 extra
= list
->p
[0]->n_div
;
8778 n_eq
= list
->p
[0]->n_eq
;
8779 n_ineq
= list
->p
[0]->n_ineq
;
8780 for (i
= 1; i
< list
->n
; ++i
) {
8781 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8782 isl_assert(list
->ctx
,
8783 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8784 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8785 extra
+= list
->p
[i
]->n_div
;
8786 n_eq
+= list
->p
[i
]->n_eq
;
8787 n_ineq
+= list
->p
[i
]->n_ineq
;
8789 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
8794 for (i
= 0; i
< list
->n
; ++i
) {
8795 isl_basic_set_add_constraints(product
,
8796 isl_basic_set_copy(list
->p
[i
]), dim
);
8797 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8799 isl_basic_set_list_free(list
);
8802 isl_basic_set_free(product
);
8803 isl_basic_set_list_free(list
);
8807 struct isl_basic_map
*isl_basic_map_product(
8808 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8810 isl_space
*dim_result
= NULL
;
8811 struct isl_basic_map
*bmap
;
8812 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8813 struct isl_dim_map
*dim_map1
, *dim_map2
;
8815 if (!bmap1
|| !bmap2
)
8818 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8819 bmap2
->dim
, isl_dim_param
), goto error
);
8820 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8821 isl_space_copy(bmap2
->dim
));
8823 in1
= isl_basic_map_n_in(bmap1
);
8824 in2
= isl_basic_map_n_in(bmap2
);
8825 out1
= isl_basic_map_n_out(bmap1
);
8826 out2
= isl_basic_map_n_out(bmap2
);
8827 nparam
= isl_basic_map_n_param(bmap1
);
8829 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8830 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8831 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8832 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8833 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8834 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8835 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8836 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8837 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8838 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8839 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8841 bmap
= isl_basic_map_alloc_space(dim_result
,
8842 bmap1
->n_div
+ bmap2
->n_div
,
8843 bmap1
->n_eq
+ bmap2
->n_eq
,
8844 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8845 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8846 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8847 bmap
= isl_basic_map_simplify(bmap
);
8848 return isl_basic_map_finalize(bmap
);
8850 isl_basic_map_free(bmap1
);
8851 isl_basic_map_free(bmap2
);
8855 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8856 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8858 isl_basic_map
*prod
;
8860 prod
= isl_basic_map_product(bmap1
, bmap2
);
8861 prod
= isl_basic_map_flatten(prod
);
8865 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8866 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8868 return isl_basic_map_flat_range_product(bset1
, bset2
);
8871 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8872 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8874 isl_space
*space_result
= NULL
;
8875 isl_basic_map
*bmap
;
8876 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8877 struct isl_dim_map
*dim_map1
, *dim_map2
;
8879 if (!bmap1
|| !bmap2
)
8882 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8883 isl_space_copy(bmap2
->dim
));
8885 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8886 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8887 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8888 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8890 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8891 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8892 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8895 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8896 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8897 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8898 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8899 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8900 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8902 bmap
= isl_basic_map_alloc_space(space_result
,
8903 bmap1
->n_div
+ bmap2
->n_div
,
8904 bmap1
->n_eq
+ bmap2
->n_eq
,
8905 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8906 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8907 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8908 bmap
= isl_basic_map_simplify(bmap
);
8909 return isl_basic_map_finalize(bmap
);
8911 isl_basic_map_free(bmap1
);
8912 isl_basic_map_free(bmap2
);
8916 __isl_give isl_basic_map
*isl_basic_map_range_product(
8917 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8919 isl_space
*dim_result
= NULL
;
8920 isl_basic_map
*bmap
;
8921 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8922 struct isl_dim_map
*dim_map1
, *dim_map2
;
8924 if (!bmap1
|| !bmap2
)
8927 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
8928 bmap2
->dim
, isl_dim_param
))
8929 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
8930 "parameters don't match", goto error
);
8932 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8933 isl_space_copy(bmap2
->dim
));
8935 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8936 out1
= isl_basic_map_n_out(bmap1
);
8937 out2
= isl_basic_map_n_out(bmap2
);
8938 nparam
= isl_basic_map_n_param(bmap1
);
8940 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8941 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8942 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8943 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8944 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8945 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8946 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8947 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8948 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8949 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8950 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8952 bmap
= isl_basic_map_alloc_space(dim_result
,
8953 bmap1
->n_div
+ bmap2
->n_div
,
8954 bmap1
->n_eq
+ bmap2
->n_eq
,
8955 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8956 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8957 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8958 bmap
= isl_basic_map_simplify(bmap
);
8959 return isl_basic_map_finalize(bmap
);
8961 isl_basic_map_free(bmap1
);
8962 isl_basic_map_free(bmap2
);
8966 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8967 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8969 isl_basic_map
*prod
;
8971 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8972 prod
= isl_basic_map_flatten_range(prod
);
8976 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8977 __isl_take isl_map
*map2
,
8978 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8979 __isl_take isl_space
*right
),
8980 __isl_give isl_basic_map
*(*basic_map_product
)(
8981 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8984 struct isl_map
*result
;
8990 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8991 map2
->dim
, isl_dim_param
), goto error
);
8993 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8994 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8995 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8997 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8998 isl_space_copy(map2
->dim
)),
8999 map1
->n
* map2
->n
, flags
);
9002 for (i
= 0; i
< map1
->n
; ++i
)
9003 for (j
= 0; j
< map2
->n
; ++j
) {
9004 struct isl_basic_map
*part
;
9005 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9006 isl_basic_map_copy(map2
->p
[j
]));
9007 if (isl_basic_map_is_empty(part
))
9008 isl_basic_map_free(part
);
9010 result
= isl_map_add_basic_map(result
, part
);
9023 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9025 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9026 __isl_take isl_map
*map2
)
9028 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9031 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9032 __isl_take isl_map
*map2
)
9034 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9037 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9039 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9040 __isl_take isl_map
*map2
)
9044 prod
= isl_map_product(map1
, map2
);
9045 prod
= isl_map_flatten(prod
);
9049 /* Given two set A and B, construct its Cartesian product A x B.
9051 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9053 return isl_map_range_product(set1
, set2
);
9056 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9057 __isl_take isl_set
*set2
)
9059 return isl_map_flat_range_product(set1
, set2
);
9062 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9064 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9065 __isl_take isl_map
*map2
)
9067 return map_product(map1
, map2
, &isl_space_domain_product
,
9068 &isl_basic_map_domain_product
);
9071 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9073 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9074 __isl_take isl_map
*map2
)
9076 return map_product(map1
, map2
, &isl_space_range_product
,
9077 &isl_basic_map_range_product
);
9080 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9081 __isl_take isl_map
*map2
)
9083 return isl_map_align_params_map_map_and(map1
, map2
,
9084 &map_domain_product_aligned
);
9087 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9088 __isl_take isl_map
*map2
)
9090 return isl_map_align_params_map_map_and(map1
, map2
,
9091 &map_range_product_aligned
);
9094 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9096 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9097 __isl_take isl_map
*map2
)
9101 prod
= isl_map_domain_product(map1
, map2
);
9102 prod
= isl_map_flatten_domain(prod
);
9106 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9108 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9109 __isl_take isl_map
*map2
)
9113 prod
= isl_map_range_product(map1
, map2
);
9114 prod
= isl_map_flatten_range(prod
);
9118 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9121 uint32_t hash
= isl_hash_init();
9126 bmap
= isl_basic_map_copy(bmap
);
9127 bmap
= isl_basic_map_normalize(bmap
);
9130 total
= isl_basic_map_total_dim(bmap
);
9131 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9132 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9134 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9135 isl_hash_hash(hash
, c_hash
);
9137 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9138 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9140 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9141 isl_hash_hash(hash
, c_hash
);
9143 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9144 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9146 if (isl_int_is_zero(bmap
->div
[i
][0]))
9148 isl_hash_byte(hash
, i
& 0xFF);
9149 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9150 isl_hash_hash(hash
, c_hash
);
9152 isl_basic_map_free(bmap
);
9156 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9158 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9161 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9168 map
= isl_map_copy(map
);
9169 map
= isl_map_normalize(map
);
9173 hash
= isl_hash_init();
9174 for (i
= 0; i
< map
->n
; ++i
) {
9176 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9177 isl_hash_hash(hash
, bmap_hash
);
9185 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9187 return isl_map_get_hash((isl_map
*)set
);
9190 /* Check if the value for dimension dim is completely determined
9191 * by the values of the other parameters and variables.
9192 * That is, check if dimension dim is involved in an equality.
9194 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9201 nparam
= isl_basic_set_n_param(bset
);
9202 for (i
= 0; i
< bset
->n_eq
; ++i
)
9203 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9208 /* Check if the value for dimension dim is completely determined
9209 * by the values of the other parameters and variables.
9210 * That is, check if dimension dim is involved in an equality
9211 * for each of the subsets.
9213 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9219 for (i
= 0; i
< set
->n
; ++i
) {
9221 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9228 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9230 return set
? set
->n
: 0;
9233 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9234 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9241 for (i
= 0; i
< map
->n
; ++i
)
9242 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9248 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9249 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9256 for (i
= 0; i
< set
->n
; ++i
)
9257 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9263 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9270 bset
= isl_basic_set_cow(bset
);
9274 dim
= isl_basic_set_get_space(bset
);
9275 dim
= isl_space_lift(dim
, bset
->n_div
);
9278 isl_space_free(bset
->dim
);
9280 bset
->extra
-= bset
->n_div
;
9283 bset
= isl_basic_set_finalize(bset
);
9287 isl_basic_set_free(bset
);
9291 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9297 set
= isl_set_align_divs(set
);
9302 set
= isl_set_cow(set
);
9306 n_div
= set
->p
[0]->n_div
;
9307 dim
= isl_set_get_space(set
);
9308 dim
= isl_space_lift(dim
, n_div
);
9311 isl_space_free(set
->dim
);
9314 for (i
= 0; i
< set
->n
; ++i
) {
9315 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9326 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9329 struct isl_basic_map
*bmap
;
9336 set
= isl_set_align_divs(set
);
9341 dim
= isl_set_get_space(set
);
9342 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9344 return isl_map_identity(isl_space_map_from_set(dim
));
9347 n_div
= set
->p
[0]->n_div
;
9348 dim
= isl_space_map_from_set(dim
);
9349 n_param
= isl_space_dim(dim
, isl_dim_param
);
9350 n_set
= isl_space_dim(dim
, isl_dim_in
);
9351 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9352 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9353 for (i
= 0; i
< n_set
; ++i
)
9354 bmap
= var_equal(bmap
, i
);
9356 total
= n_param
+ n_set
+ n_set
+ n_div
;
9357 for (i
= 0; i
< n_div
; ++i
) {
9358 k
= isl_basic_map_alloc_inequality(bmap
);
9361 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9362 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9363 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9364 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9365 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9366 set
->p
[0]->div
[i
][0]);
9368 l
= isl_basic_map_alloc_inequality(bmap
);
9371 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9372 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9373 set
->p
[0]->div
[i
][0]);
9374 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9378 bmap
= isl_basic_map_simplify(bmap
);
9379 bmap
= isl_basic_map_finalize(bmap
);
9380 return isl_map_from_basic_map(bmap
);
9383 isl_basic_map_free(bmap
);
9387 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9395 dim
= isl_basic_set_total_dim(bset
);
9396 size
+= bset
->n_eq
* (1 + dim
);
9397 size
+= bset
->n_ineq
* (1 + dim
);
9398 size
+= bset
->n_div
* (2 + dim
);
9403 int isl_set_size(__isl_keep isl_set
*set
)
9411 for (i
= 0; i
< set
->n
; ++i
)
9412 size
+= isl_basic_set_size(set
->p
[i
]);
9417 /* Check if there is any lower bound (if lower == 0) and/or upper
9418 * bound (if upper == 0) on the specified dim.
9420 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9421 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9428 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9430 pos
+= isl_basic_map_offset(bmap
, type
);
9432 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9433 if (isl_int_is_zero(bmap
->div
[i
][0]))
9435 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9439 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9440 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9443 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9444 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9451 return lower
&& upper
;
9454 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9455 enum isl_dim_type type
, unsigned pos
)
9457 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9460 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9461 enum isl_dim_type type
, unsigned pos
)
9463 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9466 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9467 enum isl_dim_type type
, unsigned pos
)
9469 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9472 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9473 enum isl_dim_type type
, unsigned pos
)
9480 for (i
= 0; i
< map
->n
; ++i
) {
9482 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9483 if (bounded
< 0 || !bounded
)
9490 /* Return 1 if the specified dim is involved in both an upper bound
9491 * and a lower bound.
9493 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9494 enum isl_dim_type type
, unsigned pos
)
9496 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9499 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9501 static int has_any_bound(__isl_keep isl_map
*map
,
9502 enum isl_dim_type type
, unsigned pos
,
9503 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9504 enum isl_dim_type type
, unsigned pos
))
9511 for (i
= 0; i
< map
->n
; ++i
) {
9513 bounded
= fn(map
->p
[i
], type
, pos
);
9514 if (bounded
< 0 || bounded
)
9521 /* Return 1 if the specified dim is involved in any lower bound.
9523 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9524 enum isl_dim_type type
, unsigned pos
)
9526 return has_any_bound(set
, type
, pos
,
9527 &isl_basic_map_dim_has_lower_bound
);
9530 /* Return 1 if the specified dim is involved in any upper bound.
9532 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9533 enum isl_dim_type type
, unsigned pos
)
9535 return has_any_bound(set
, type
, pos
,
9536 &isl_basic_map_dim_has_upper_bound
);
9539 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9541 static int has_bound(__isl_keep isl_map
*map
,
9542 enum isl_dim_type type
, unsigned pos
,
9543 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9544 enum isl_dim_type type
, unsigned pos
))
9551 for (i
= 0; i
< map
->n
; ++i
) {
9553 bounded
= fn(map
->p
[i
], type
, pos
);
9554 if (bounded
< 0 || !bounded
)
9561 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9563 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9564 enum isl_dim_type type
, unsigned pos
)
9566 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9569 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9571 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9572 enum isl_dim_type type
, unsigned pos
)
9574 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9577 /* For each of the "n" variables starting at "first", determine
9578 * the sign of the variable and put the results in the first "n"
9579 * elements of the array "signs".
9581 * 1 means that the variable is non-negative
9582 * -1 means that the variable is non-positive
9583 * 0 means the variable attains both positive and negative values.
9585 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9586 unsigned first
, unsigned n
, int *signs
)
9588 isl_vec
*bound
= NULL
;
9589 struct isl_tab
*tab
= NULL
;
9590 struct isl_tab_undo
*snap
;
9593 if (!bset
|| !signs
)
9596 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9597 tab
= isl_tab_from_basic_set(bset
, 0);
9601 isl_seq_clr(bound
->el
, bound
->size
);
9602 isl_int_set_si(bound
->el
[0], -1);
9604 snap
= isl_tab_snap(tab
);
9605 for (i
= 0; i
< n
; ++i
) {
9608 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9609 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9612 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9613 if (isl_tab_rollback(tab
, snap
) < 0)
9621 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9622 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9625 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9626 if (isl_tab_rollback(tab
, snap
) < 0)
9629 signs
[i
] = empty
? -1 : 0;
9633 isl_vec_free(bound
);
9637 isl_vec_free(bound
);
9641 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
9642 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
9644 if (!bset
|| !signs
)
9646 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
9649 first
+= pos(bset
->dim
, type
) - 1;
9650 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
9653 /* Check if the given basic map is obviously single-valued.
9654 * In particular, for each output dimension, check that there is
9655 * an equality that defines the output dimension in terms of
9656 * earlier dimensions.
9658 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9668 total
= 1 + isl_basic_map_total_dim(bmap
);
9669 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
9670 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
9672 for (i
= 0; i
< n_out
; ++i
) {
9673 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
9674 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
9676 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
9677 total
- (o_out
+ i
+ 1)) == -1)
9680 if (j
>= bmap
->n_eq
)
9687 /* Check if the given basic map is single-valued.
9692 * and check if the result is a subset of the identity mapping.
9694 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9697 isl_basic_map
*test
;
9701 sv
= isl_basic_map_plain_is_single_valued(bmap
);
9705 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
9706 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
9708 space
= isl_basic_map_get_space(bmap
);
9709 space
= isl_space_map_from_set(isl_space_range(space
));
9710 id
= isl_basic_map_identity(space
);
9712 sv
= isl_basic_map_is_subset(test
, id
);
9714 isl_basic_map_free(test
);
9715 isl_basic_map_free(id
);
9720 /* Check if the given map is obviously single-valued.
9722 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9731 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9734 /* Check if the given map is single-valued.
9739 * and check if the result is a subset of the identity mapping.
9741 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9748 sv
= isl_map_plain_is_single_valued(map
);
9752 test
= isl_map_reverse(isl_map_copy(map
));
9753 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9755 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9756 id
= isl_map_identity(dim
);
9758 sv
= isl_map_is_subset(test
, id
);
9766 int isl_map_is_injective(__isl_keep isl_map
*map
)
9770 map
= isl_map_copy(map
);
9771 map
= isl_map_reverse(map
);
9772 in
= isl_map_is_single_valued(map
);
9778 /* Check if the given map is obviously injective.
9780 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9784 map
= isl_map_copy(map
);
9785 map
= isl_map_reverse(map
);
9786 in
= isl_map_plain_is_single_valued(map
);
9792 int isl_map_is_bijective(__isl_keep isl_map
*map
)
9796 sv
= isl_map_is_single_valued(map
);
9800 return isl_map_is_injective(map
);
9803 int isl_set_is_singleton(__isl_keep isl_set
*set
)
9805 return isl_map_is_single_valued((isl_map
*)set
);
9808 int isl_map_is_translation(__isl_keep isl_map
*map
)
9813 delta
= isl_map_deltas(isl_map_copy(map
));
9814 ok
= isl_set_is_singleton(delta
);
9815 isl_set_free(delta
);
9820 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
9822 if (isl_seq_first_non_zero(p
, pos
) != -1)
9824 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
9829 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
9838 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
9841 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
9842 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
9843 for (j
= 0; j
< nvar
; ++j
) {
9844 int lower
= 0, upper
= 0;
9845 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9846 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
9848 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
9854 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9855 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
9857 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
9859 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
9864 if (!lower
|| !upper
)
9871 int isl_set_is_box(__isl_keep isl_set
*set
)
9878 return isl_basic_set_is_box(set
->p
[0]);
9881 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9886 return isl_space_is_wrapping(bset
->dim
);
9889 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9894 return isl_space_is_wrapping(set
->dim
);
9897 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9899 bmap
= isl_basic_map_cow(bmap
);
9903 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9907 bmap
= isl_basic_map_finalize(bmap
);
9909 return (isl_basic_set
*)bmap
;
9911 isl_basic_map_free(bmap
);
9915 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9919 map
= isl_map_cow(map
);
9923 for (i
= 0; i
< map
->n
; ++i
) {
9924 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9928 map
->dim
= isl_space_wrap(map
->dim
);
9932 return (isl_set
*)map
;
9938 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9940 bset
= isl_basic_set_cow(bset
);
9944 bset
->dim
= isl_space_unwrap(bset
->dim
);
9948 bset
= isl_basic_set_finalize(bset
);
9950 return (isl_basic_map
*)bset
;
9952 isl_basic_set_free(bset
);
9956 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9963 if (!isl_set_is_wrapping(set
))
9964 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9967 set
= isl_set_cow(set
);
9971 for (i
= 0; i
< set
->n
; ++i
) {
9972 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9977 set
->dim
= isl_space_unwrap(set
->dim
);
9981 return (isl_map
*)set
;
9987 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9988 enum isl_dim_type type
)
9993 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9996 bmap
= isl_basic_map_cow(bmap
);
10000 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10004 bmap
= isl_basic_map_finalize(bmap
);
10008 isl_basic_map_free(bmap
);
10012 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10013 enum isl_dim_type type
)
10020 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10023 map
= isl_map_cow(map
);
10027 for (i
= 0; i
< map
->n
; ++i
) {
10028 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10032 map
->dim
= isl_space_reset(map
->dim
, type
);
10042 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10047 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10050 bmap
= isl_basic_map_cow(bmap
);
10054 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10058 bmap
= isl_basic_map_finalize(bmap
);
10062 isl_basic_map_free(bmap
);
10066 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10068 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10071 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10072 __isl_take isl_basic_map
*bmap
)
10077 if (!bmap
->dim
->nested
[0])
10080 bmap
= isl_basic_map_cow(bmap
);
10084 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10088 bmap
= isl_basic_map_finalize(bmap
);
10092 isl_basic_map_free(bmap
);
10096 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10097 __isl_take isl_basic_map
*bmap
)
10102 if (!bmap
->dim
->nested
[1])
10105 bmap
= isl_basic_map_cow(bmap
);
10109 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10113 bmap
= isl_basic_map_finalize(bmap
);
10117 isl_basic_map_free(bmap
);
10121 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10128 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10131 map
= isl_map_cow(map
);
10135 for (i
= 0; i
< map
->n
; ++i
) {
10136 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10140 map
->dim
= isl_space_flatten(map
->dim
);
10150 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10152 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10155 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10157 isl_space
*dim
, *flat_dim
;
10160 dim
= isl_set_get_space(set
);
10161 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10162 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10163 map
= isl_map_intersect_domain(map
, set
);
10168 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10175 if (!map
->dim
->nested
[0])
10178 map
= isl_map_cow(map
);
10182 for (i
= 0; i
< map
->n
; ++i
) {
10183 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10187 map
->dim
= isl_space_flatten_domain(map
->dim
);
10197 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10204 if (!map
->dim
->nested
[1])
10207 map
= isl_map_cow(map
);
10211 for (i
= 0; i
< map
->n
; ++i
) {
10212 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10216 map
->dim
= isl_space_flatten_range(map
->dim
);
10226 /* Reorder the dimensions of "bmap" according to the given dim_map
10227 * and set the dimension specification to "dim".
10229 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10230 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10232 isl_basic_map
*res
;
10235 bmap
= isl_basic_map_cow(bmap
);
10236 if (!bmap
|| !dim
|| !dim_map
)
10239 flags
= bmap
->flags
;
10240 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10241 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10242 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10243 res
= isl_basic_map_alloc_space(dim
,
10244 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10245 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10247 res
->flags
= flags
;
10248 res
= isl_basic_map_finalize(res
);
10252 isl_basic_map_free(bmap
);
10253 isl_space_free(dim
);
10257 /* Reorder the dimensions of "map" according to given reordering.
10259 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10260 __isl_take isl_reordering
*r
)
10263 struct isl_dim_map
*dim_map
;
10265 map
= isl_map_cow(map
);
10266 dim_map
= isl_dim_map_from_reordering(r
);
10267 if (!map
|| !r
|| !dim_map
)
10270 for (i
= 0; i
< map
->n
; ++i
) {
10271 struct isl_dim_map
*dim_map_i
;
10273 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10275 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10276 isl_space_copy(r
->dim
), dim_map_i
);
10282 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10284 isl_reordering_free(r
);
10290 isl_reordering_free(r
);
10294 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10295 __isl_take isl_reordering
*r
)
10297 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10300 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10301 __isl_take isl_space
*model
)
10305 if (!map
|| !model
)
10308 ctx
= isl_space_get_ctx(model
);
10309 if (!isl_space_has_named_params(model
))
10310 isl_die(ctx
, isl_error_invalid
,
10311 "model has unnamed parameters", goto error
);
10312 if (!isl_space_has_named_params(map
->dim
))
10313 isl_die(ctx
, isl_error_invalid
,
10314 "relation has unnamed parameters", goto error
);
10315 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10316 isl_reordering
*exp
;
10318 model
= isl_space_drop_dims(model
, isl_dim_in
,
10319 0, isl_space_dim(model
, isl_dim_in
));
10320 model
= isl_space_drop_dims(model
, isl_dim_out
,
10321 0, isl_space_dim(model
, isl_dim_out
));
10322 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10323 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10324 map
= isl_map_realign(map
, exp
);
10327 isl_space_free(model
);
10330 isl_space_free(model
);
10335 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10336 __isl_take isl_space
*model
)
10338 return isl_map_align_params(set
, model
);
10341 /* Align the parameters of "bmap" to those of "model", introducing
10342 * additional parameters if needed.
10344 __isl_give isl_basic_map
*isl_basic_map_align_params(
10345 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10349 if (!bmap
|| !model
)
10352 ctx
= isl_space_get_ctx(model
);
10353 if (!isl_space_has_named_params(model
))
10354 isl_die(ctx
, isl_error_invalid
,
10355 "model has unnamed parameters", goto error
);
10356 if (!isl_space_has_named_params(bmap
->dim
))
10357 isl_die(ctx
, isl_error_invalid
,
10358 "relation has unnamed parameters", goto error
);
10359 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10360 isl_reordering
*exp
;
10361 struct isl_dim_map
*dim_map
;
10363 model
= isl_space_drop_dims(model
, isl_dim_in
,
10364 0, isl_space_dim(model
, isl_dim_in
));
10365 model
= isl_space_drop_dims(model
, isl_dim_out
,
10366 0, isl_space_dim(model
, isl_dim_out
));
10367 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10368 exp
= isl_reordering_extend_space(exp
,
10369 isl_basic_map_get_space(bmap
));
10370 dim_map
= isl_dim_map_from_reordering(exp
);
10371 bmap
= isl_basic_map_realign(bmap
,
10372 exp
? isl_space_copy(exp
->dim
) : NULL
,
10373 isl_dim_map_extend(dim_map
, bmap
));
10374 isl_reordering_free(exp
);
10378 isl_space_free(model
);
10381 isl_space_free(model
);
10382 isl_basic_map_free(bmap
);
10386 /* Align the parameters of "bset" to those of "model", introducing
10387 * additional parameters if needed.
10389 __isl_give isl_basic_set
*isl_basic_set_align_params(
10390 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10392 return isl_basic_map_align_params(bset
, model
);
10395 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10396 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10397 enum isl_dim_type c2
, enum isl_dim_type c3
,
10398 enum isl_dim_type c4
, enum isl_dim_type c5
)
10400 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10401 struct isl_mat
*mat
;
10407 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10408 isl_basic_map_total_dim(bmap
) + 1);
10411 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10412 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10413 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10414 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10415 isl_int_set(mat
->row
[i
][pos
],
10416 bmap
->eq
[i
][off
+ k
]);
10424 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10425 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10426 enum isl_dim_type c2
, enum isl_dim_type c3
,
10427 enum isl_dim_type c4
, enum isl_dim_type c5
)
10429 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10430 struct isl_mat
*mat
;
10436 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10437 isl_basic_map_total_dim(bmap
) + 1);
10440 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10441 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10442 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10443 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10444 isl_int_set(mat
->row
[i
][pos
],
10445 bmap
->ineq
[i
][off
+ k
]);
10453 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10454 __isl_take isl_space
*dim
,
10455 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10456 enum isl_dim_type c2
, enum isl_dim_type c3
,
10457 enum isl_dim_type c4
, enum isl_dim_type c5
)
10459 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10460 isl_basic_map
*bmap
;
10466 if (!dim
|| !eq
|| !ineq
)
10469 if (eq
->n_col
!= ineq
->n_col
)
10470 isl_die(dim
->ctx
, isl_error_invalid
,
10471 "equalities and inequalities matrices should have "
10472 "same number of columns", goto error
);
10474 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10476 if (eq
->n_col
< total
)
10477 isl_die(dim
->ctx
, isl_error_invalid
,
10478 "number of columns too small", goto error
);
10480 extra
= eq
->n_col
- total
;
10482 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10483 eq
->n_row
, ineq
->n_row
);
10486 for (i
= 0; i
< extra
; ++i
) {
10487 k
= isl_basic_map_alloc_div(bmap
);
10490 isl_int_set_si(bmap
->div
[k
][0], 0);
10492 for (i
= 0; i
< eq
->n_row
; ++i
) {
10493 l
= isl_basic_map_alloc_equality(bmap
);
10496 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10497 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10498 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10499 isl_int_set(bmap
->eq
[l
][off
+ k
],
10505 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10506 l
= isl_basic_map_alloc_inequality(bmap
);
10509 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10510 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10511 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10512 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10513 ineq
->row
[i
][pos
]);
10519 isl_space_free(dim
);
10521 isl_mat_free(ineq
);
10523 bmap
= isl_basic_map_simplify(bmap
);
10524 return isl_basic_map_finalize(bmap
);
10526 isl_space_free(dim
);
10528 isl_mat_free(ineq
);
10532 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10533 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10534 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10536 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10537 c1
, c2
, c3
, c4
, isl_dim_in
);
10540 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10541 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10542 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10544 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10545 c1
, c2
, c3
, c4
, isl_dim_in
);
10548 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10549 __isl_take isl_space
*dim
,
10550 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10551 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10553 return (isl_basic_set
*)
10554 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10555 c1
, c2
, c3
, c4
, isl_dim_in
);
10558 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10563 return isl_space_can_zip(bmap
->dim
);
10566 int isl_map_can_zip(__isl_keep isl_map
*map
)
10571 return isl_space_can_zip(map
->dim
);
10574 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10575 * (A -> C) -> (B -> D).
10577 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10586 if (!isl_basic_map_can_zip(bmap
))
10587 isl_die(bmap
->ctx
, isl_error_invalid
,
10588 "basic map cannot be zipped", goto error
);
10589 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10590 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10591 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10592 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10593 bmap
= isl_basic_map_cow(bmap
);
10594 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10597 bmap
->dim
= isl_space_zip(bmap
->dim
);
10602 isl_basic_map_free(bmap
);
10606 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10607 * (A -> C) -> (B -> D).
10609 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10616 if (!isl_map_can_zip(map
))
10617 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10620 map
= isl_map_cow(map
);
10624 for (i
= 0; i
< map
->n
; ++i
) {
10625 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
10630 map
->dim
= isl_space_zip(map
->dim
);
10640 /* Can we apply isl_basic_map_curry to "bmap"?
10641 * That is, does it have a nested relation in its domain?
10643 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
10648 return isl_space_can_curry(bmap
->dim
);
10651 /* Can we apply isl_map_curry to "map"?
10652 * That is, does it have a nested relation in its domain?
10654 int isl_map_can_curry(__isl_keep isl_map
*map
)
10659 return isl_space_can_curry(map
->dim
);
10662 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10665 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
10671 if (!isl_basic_map_can_curry(bmap
))
10672 isl_die(bmap
->ctx
, isl_error_invalid
,
10673 "basic map cannot be curried", goto error
);
10674 bmap
= isl_basic_map_cow(bmap
);
10677 bmap
->dim
= isl_space_curry(bmap
->dim
);
10682 isl_basic_map_free(bmap
);
10686 /* Given a map (A -> B) -> C, return the corresponding map
10689 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
10696 if (!isl_map_can_curry(map
))
10697 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
10700 map
= isl_map_cow(map
);
10704 for (i
= 0; i
< map
->n
; ++i
) {
10705 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
10710 map
->dim
= isl_space_curry(map
->dim
);
10720 /* Can we apply isl_basic_map_uncurry to "bmap"?
10721 * That is, does it have a nested relation in its domain?
10723 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
10728 return isl_space_can_uncurry(bmap
->dim
);
10731 /* Can we apply isl_map_uncurry to "map"?
10732 * That is, does it have a nested relation in its domain?
10734 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
10739 return isl_space_can_uncurry(map
->dim
);
10742 /* Given a basic map A -> (B -> C), return the corresponding basic map
10745 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
10751 if (!isl_basic_map_can_uncurry(bmap
))
10752 isl_die(bmap
->ctx
, isl_error_invalid
,
10753 "basic map cannot be uncurried",
10754 return isl_basic_map_free(bmap
));
10755 bmap
= isl_basic_map_cow(bmap
);
10758 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
10760 return isl_basic_map_free(bmap
);
10764 /* Given a map A -> (B -> C), return the corresponding map
10767 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
10774 if (!isl_map_can_uncurry(map
))
10775 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
10776 return isl_map_free(map
));
10778 map
= isl_map_cow(map
);
10782 for (i
= 0; i
< map
->n
; ++i
) {
10783 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
10785 return isl_map_free(map
);
10788 map
->dim
= isl_space_uncurry(map
->dim
);
10790 return isl_map_free(map
);
10795 /* Construct a basic map mapping the domain of the affine expression
10796 * to a one-dimensional range prescribed by the affine expression.
10798 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
10802 isl_local_space
*ls
;
10803 isl_basic_map
*bmap
;
10808 ls
= isl_aff_get_local_space(aff
);
10809 bmap
= isl_basic_map_from_local_space(ls
);
10810 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
10811 k
= isl_basic_map_alloc_equality(bmap
);
10815 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
10816 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
10817 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
10818 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
10819 aff
->v
->size
- (pos
+ 1));
10822 bmap
= isl_basic_map_finalize(bmap
);
10826 isl_basic_map_free(bmap
);
10830 /* Construct a map mapping the domain of the affine expression
10831 * to a one-dimensional range prescribed by the affine expression.
10833 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
10835 isl_basic_map
*bmap
;
10837 bmap
= isl_basic_map_from_aff(aff
);
10838 return isl_map_from_basic_map(bmap
);
10841 /* Construct a basic map mapping the domain the multi-affine expression
10842 * to its range, with each dimension in the range equated to the
10843 * corresponding affine expression.
10845 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
10846 __isl_take isl_multi_aff
*maff
)
10850 isl_basic_map
*bmap
;
10855 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
10856 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
10857 "invalid space", return isl_multi_aff_free(maff
));
10859 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
10860 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
10862 for (i
= 0; i
< maff
->n
; ++i
) {
10864 isl_basic_map
*bmap_i
;
10866 aff
= isl_aff_copy(maff
->p
[i
]);
10867 bmap_i
= isl_basic_map_from_aff(aff
);
10869 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10872 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
10874 isl_multi_aff_free(maff
);
10878 /* Construct a map mapping the domain the multi-affine expression
10879 * to its range, with each dimension in the range equated to the
10880 * corresponding affine expression.
10882 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
10884 isl_basic_map
*bmap
;
10886 bmap
= isl_basic_map_from_multi_aff(maff
);
10887 return isl_map_from_basic_map(bmap
);
10890 /* Construct a basic map mapping a domain in the given space to
10891 * to an n-dimensional range, with n the number of elements in the list,
10892 * where each coordinate in the range is prescribed by the
10893 * corresponding affine expression.
10894 * The domains of all affine expressions in the list are assumed to match
10897 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
10898 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
10902 isl_basic_map
*bmap
;
10907 dim
= isl_space_from_domain(domain_dim
);
10908 bmap
= isl_basic_map_universe(dim
);
10910 for (i
= 0; i
< list
->n
; ++i
) {
10912 isl_basic_map
*bmap_i
;
10914 aff
= isl_aff_copy(list
->p
[i
]);
10915 bmap_i
= isl_basic_map_from_aff(aff
);
10917 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10920 isl_aff_list_free(list
);
10924 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
10925 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10927 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
10930 /* Construct a basic map where the given dimensions are equal to each other.
10932 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
10933 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10935 isl_basic_map
*bmap
= NULL
;
10941 if (pos1
>= isl_space_dim(space
, type1
))
10942 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10943 "index out of bounds", goto error
);
10944 if (pos2
>= isl_space_dim(space
, type2
))
10945 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10946 "index out of bounds", goto error
);
10948 if (type1
== type2
&& pos1
== pos2
)
10949 return isl_basic_map_universe(space
);
10951 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
10952 i
= isl_basic_map_alloc_equality(bmap
);
10955 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10956 pos1
+= isl_basic_map_offset(bmap
, type1
);
10957 pos2
+= isl_basic_map_offset(bmap
, type2
);
10958 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
10959 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10960 bmap
= isl_basic_map_finalize(bmap
);
10961 isl_space_free(space
);
10964 isl_space_free(space
);
10965 isl_basic_map_free(bmap
);
10969 /* Add a constraint imposing that the given two dimensions are equal.
10971 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
10972 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10976 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
10978 bmap
= isl_basic_map_intersect(bmap
, eq
);
10983 /* Add a constraint imposing that the given two dimensions are equal.
10985 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
10986 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10988 isl_basic_map
*bmap
;
10990 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
10992 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10997 /* Add a constraint imposing that the given two dimensions have opposite values.
10999 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11000 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11002 isl_basic_map
*bmap
= NULL
;
11008 if (pos1
>= isl_map_dim(map
, type1
))
11009 isl_die(map
->ctx
, isl_error_invalid
,
11010 "index out of bounds", goto error
);
11011 if (pos2
>= isl_map_dim(map
, type2
))
11012 isl_die(map
->ctx
, isl_error_invalid
,
11013 "index out of bounds", goto error
);
11015 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11016 i
= isl_basic_map_alloc_equality(bmap
);
11019 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11020 pos1
+= isl_basic_map_offset(bmap
, type1
);
11021 pos2
+= isl_basic_map_offset(bmap
, type2
);
11022 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11023 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11024 bmap
= isl_basic_map_finalize(bmap
);
11026 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11030 isl_basic_map_free(bmap
);
11035 /* Add a constraint imposing that the value of the first dimension is
11036 * greater than or equal to that of the second.
11038 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11039 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11042 isl_local_space
*ls
;
11047 if (pos1
>= isl_basic_map_dim(bmap
, type1
))
11048 isl_die(bmap
->ctx
, isl_error_invalid
,
11049 "index out of bounds", return isl_basic_map_free(bmap
));
11050 if (pos2
>= isl_basic_map_dim(bmap
, type2
))
11051 isl_die(bmap
->ctx
, isl_error_invalid
,
11052 "index out of bounds", return isl_basic_map_free(bmap
));
11054 if (type1
== type2
&& pos1
== pos2
)
11057 ls
= isl_local_space_from_space(isl_basic_map_get_space(bmap
));
11058 c
= isl_inequality_alloc(ls
);
11059 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11060 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11061 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11066 /* Construct a basic map where the value of the first dimension is
11067 * greater than that of the second.
11069 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11070 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11072 isl_basic_map
*bmap
= NULL
;
11078 if (pos1
>= isl_space_dim(space
, type1
))
11079 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11080 "index out of bounds", goto error
);
11081 if (pos2
>= isl_space_dim(space
, type2
))
11082 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11083 "index out of bounds", goto error
);
11085 if (type1
== type2
&& pos1
== pos2
)
11086 return isl_basic_map_empty(space
);
11088 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11089 i
= isl_basic_map_alloc_inequality(bmap
);
11092 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11093 pos1
+= isl_basic_map_offset(bmap
, type1
);
11094 pos2
+= isl_basic_map_offset(bmap
, type2
);
11095 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11096 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11097 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11098 bmap
= isl_basic_map_finalize(bmap
);
11102 isl_space_free(space
);
11103 isl_basic_map_free(bmap
);
11107 /* Add a constraint imposing that the value of the first dimension is
11108 * greater than that of the second.
11110 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11111 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11115 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11117 bmap
= isl_basic_map_intersect(bmap
, gt
);
11122 /* Add a constraint imposing that the value of the first dimension is
11123 * greater than that of the second.
11125 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11126 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11128 isl_basic_map
*bmap
;
11130 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11132 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11137 /* Add a constraint imposing that the value of the first dimension is
11138 * smaller than that of the second.
11140 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11141 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11143 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11146 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11150 isl_local_space
*ls
;
11155 if (!isl_basic_map_divs_known(bmap
))
11156 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11157 "some divs are unknown", return NULL
);
11159 ls
= isl_basic_map_get_local_space(bmap
);
11160 div
= isl_local_space_get_div(ls
, pos
);
11161 isl_local_space_free(ls
);
11166 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11169 return isl_basic_map_get_div(bset
, pos
);
11172 /* Plug in "subs" for dimension "type", "pos" of "bset".
11174 * Let i be the dimension to replace and let "subs" be of the form
11178 * Any integer division with a non-zero coefficient for i,
11180 * floor((a i + g)/m)
11184 * floor((a f + d g)/(m d))
11186 * Constraints of the form
11194 * We currently require that "subs" is an integral expression.
11195 * Handling rational expressions may require us to add stride constraints
11196 * as we do in isl_basic_set_preimage_multi_aff.
11198 __isl_give isl_basic_set
*isl_basic_set_substitute(
11199 __isl_take isl_basic_set
*bset
,
11200 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11206 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11209 bset
= isl_basic_set_cow(bset
);
11210 if (!bset
|| !subs
)
11213 ctx
= isl_basic_set_get_ctx(bset
);
11214 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11215 isl_die(ctx
, isl_error_invalid
,
11216 "spaces don't match", goto error
);
11217 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11218 isl_die(ctx
, isl_error_unsupported
,
11219 "cannot handle divs yet", goto error
);
11220 if (!isl_int_is_one(subs
->v
->el
[0]))
11221 isl_die(ctx
, isl_error_invalid
,
11222 "can only substitute integer expressions", goto error
);
11224 pos
+= isl_basic_set_offset(bset
, type
);
11228 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11229 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11231 isl_int_set(v
, bset
->eq
[i
][pos
]);
11232 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11233 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11234 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11237 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11238 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11240 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11241 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11242 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11243 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11246 for (i
= 0; i
< bset
->n_div
; ++i
) {
11247 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11249 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11250 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11251 isl_seq_combine(bset
->div
[i
] + 1,
11252 subs
->v
->el
[0], bset
->div
[i
] + 1,
11253 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11254 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11259 bset
= isl_basic_set_simplify(bset
);
11260 return isl_basic_set_finalize(bset
);
11262 isl_basic_set_free(bset
);
11266 /* Plug in "subs" for dimension "type", "pos" of "set".
11268 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11269 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11273 if (set
&& isl_set_plain_is_empty(set
))
11276 set
= isl_set_cow(set
);
11280 for (i
= set
->n
- 1; i
>= 0; --i
) {
11281 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11282 if (remove_if_empty(set
, i
) < 0)
11292 /* Check if the range of "ma" is compatible with the domain or range
11293 * (depending on "type") of "bmap".
11294 * Return -1 if anything is wrong.
11296 static int check_basic_map_compatible_range_multi_aff(
11297 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11298 __isl_keep isl_multi_aff
*ma
)
11301 isl_space
*ma_space
;
11303 ma_space
= isl_multi_aff_get_space(ma
);
11304 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11305 isl_space_free(ma_space
);
11307 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11308 "spaces don't match", return -1);
11312 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11313 * coefficients before the transformed range of dimensions,
11314 * the "n_after" coefficients after the transformed range of dimensions
11315 * and the coefficients of the other divs in "bmap".
11317 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11318 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11323 isl_local_space
*ls
;
11328 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11332 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11333 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11334 for (i
= 0; i
< n_div
; ++i
) {
11335 int o_bmap
= 0, o_ls
= 0;
11337 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11338 o_bmap
+= 1 + 1 + n_param
;
11339 o_ls
+= 1 + 1 + n_param
;
11340 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11341 o_bmap
+= n_before
;
11342 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11343 ls
->div
->row
[i
] + o_ls
, n_set
);
11346 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11348 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11349 ls
->div
->row
[i
] + o_ls
, n_div
);
11352 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11353 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11357 isl_local_space_free(ls
);
11360 isl_local_space_free(ls
);
11364 /* How many stride constraints does "ma" enforce?
11365 * That is, how many of the affine expressions have a denominator
11366 * different from one?
11368 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11373 for (i
= 0; i
< ma
->n
; ++i
)
11374 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11380 /* For each affine expression in ma of the form
11382 * x_i = (f_i y + h_i)/m_i
11384 * with m_i different from one, add a constraint to "bmap"
11387 * f_i y + h_i = m_i alpha_i
11389 * with alpha_i an additional existentially quantified variable.
11391 static __isl_give isl_basic_map
*add_ma_strides(
11392 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11393 int n_before
, int n_after
)
11402 total
= isl_basic_map_total_dim(bmap
);
11403 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11404 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11405 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11406 for (i
= 0; i
< ma
->n
; ++i
) {
11407 int o_bmap
= 0, o_ma
= 1;
11409 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11411 div
= isl_basic_map_alloc_div(bmap
);
11412 k
= isl_basic_map_alloc_equality(bmap
);
11413 if (div
< 0 || k
< 0)
11415 isl_int_set_si(bmap
->div
[div
][0], 0);
11416 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11417 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11418 o_bmap
+= 1 + n_param
;
11419 o_ma
+= 1 + n_param
;
11420 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11421 o_bmap
+= n_before
;
11422 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11423 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11426 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11428 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11429 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11432 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11433 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11439 isl_basic_map_free(bmap
);
11443 /* Replace the domain or range space (depending on "type) of "space" by "set".
11445 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11446 enum isl_dim_type type
, __isl_take isl_space
*set
)
11448 if (type
== isl_dim_in
) {
11449 space
= isl_space_range(space
);
11450 space
= isl_space_map_from_domain_and_range(set
, space
);
11452 space
= isl_space_domain(space
);
11453 space
= isl_space_map_from_domain_and_range(space
, set
);
11459 /* Compute the preimage of the domain or range (depending on "type")
11460 * of "bmap" under the function represented by "ma".
11461 * In other words, plug in "ma" in the domain or range of "bmap".
11462 * The result is a basic map that lives in the same space as "bmap"
11463 * except that the domain or range has been replaced by
11464 * the domain space of "ma".
11466 * If bmap is represented by
11468 * A(p) + S u + B x + T v + C(divs) >= 0,
11470 * where u and x are input and output dimensions if type == isl_dim_out
11471 * while x and v are input and output dimensions if type == isl_dim_in,
11472 * and ma is represented by
11474 * x = D(p) + F(y) + G(divs')
11476 * then the result is
11478 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11480 * The divs in the input set are similarly adjusted.
11483 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11487 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11488 * B_i G(divs') + c_i(divs))/n_i)
11490 * If bmap is not a rational map and if F(y) involves any denominators
11492 * x_i = (f_i y + h_i)/m_i
11494 * then additional constraints are added to ensure that we only
11495 * map back integer points. That is we enforce
11497 * f_i y + h_i = m_i alpha_i
11499 * with alpha_i an additional existentially quantified variable.
11501 * We first copy over the divs from "ma".
11502 * Then we add the modified constraints and divs from "bmap".
11503 * Finally, we add the stride constraints, if needed.
11505 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
11506 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
11507 __isl_take isl_multi_aff
*ma
)
11511 isl_basic_map
*res
= NULL
;
11512 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
11513 isl_int f
, c1
, c2
, g
;
11514 int rational
, strides
;
11521 ma
= isl_multi_aff_align_divs(ma
);
11524 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
11527 if (type
== isl_dim_in
) {
11529 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
11531 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
11534 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
11535 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
11537 space
= isl_multi_aff_get_domain_space(ma
);
11538 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
11539 rational
= isl_basic_map_is_rational(bmap
);
11540 strides
= rational
? 0 : multi_aff_strides(ma
);
11541 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
11542 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
11544 res
= isl_basic_map_set_rational(res
);
11546 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
11547 if (isl_basic_map_alloc_div(res
) < 0)
11550 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
11553 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11554 k
= isl_basic_map_alloc_equality(res
);
11557 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
11558 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11561 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11562 k
= isl_basic_map_alloc_inequality(res
);
11565 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
11566 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11569 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11570 if (isl_int_is_zero(bmap
->div
[i
][0])) {
11571 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
11574 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
11575 n_before
, n_after
, n_div_ma
, n_div_bmap
,
11580 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
11586 isl_basic_map_free(bmap
);
11587 isl_multi_aff_free(ma
);
11588 res
= isl_basic_set_simplify(res
);
11589 return isl_basic_map_finalize(res
);
11595 isl_basic_map_free(bmap
);
11596 isl_multi_aff_free(ma
);
11597 isl_basic_map_free(res
);
11601 /* Compute the preimage of "bset" under the function represented by "ma".
11602 * In other words, plug in "ma" in "bset". The result is a basic set
11603 * that lives in the domain space of "ma".
11605 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
11606 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
11608 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
11611 /* Check if the range of "ma" is compatible with the domain or range
11612 * (depending on "type") of "map".
11613 * Return -1 if anything is wrong.
11615 static int check_map_compatible_range_multi_aff(
11616 __isl_keep isl_map
*map
, enum isl_dim_type type
,
11617 __isl_keep isl_multi_aff
*ma
)
11620 isl_space
*ma_space
;
11622 ma_space
= isl_multi_aff_get_space(ma
);
11623 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
11624 isl_space_free(ma_space
);
11626 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11627 "spaces don't match", return -1);
11631 /* Compute the preimage of the domain or range (depending on "type")
11632 * of "map" under the function represented by "ma".
11633 * In other words, plug in "ma" in the domain or range of "map".
11634 * The result is a map that lives in the same space as "map"
11635 * except that the domain or range has been replaced by
11636 * the domain space of "ma".
11638 * The parameters are assumed to have been aligned.
11640 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
11641 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11646 map
= isl_map_cow(map
);
11647 ma
= isl_multi_aff_align_divs(ma
);
11650 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
11653 for (i
= 0; i
< map
->n
; ++i
) {
11654 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
11655 isl_multi_aff_copy(ma
));
11660 space
= isl_multi_aff_get_domain_space(ma
);
11661 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
11663 isl_space_free(map
->dim
);
11668 isl_multi_aff_free(ma
);
11670 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
11671 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
11674 isl_multi_aff_free(ma
);
11679 /* Compute the preimage of the domain or range (depending on "type")
11680 * of "map" under the function represented by "ma".
11681 * In other words, plug in "ma" in the domain or range of "map".
11682 * The result is a map that lives in the same space as "map"
11683 * except that the domain or range has been replaced by
11684 * the domain space of "ma".
11686 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
11687 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11692 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
11693 return map_preimage_multi_aff(map
, type
, ma
);
11695 if (!isl_space_has_named_params(map
->dim
) ||
11696 !isl_space_has_named_params(ma
->space
))
11697 isl_die(map
->ctx
, isl_error_invalid
,
11698 "unaligned unnamed parameters", goto error
);
11699 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
11700 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
11702 return map_preimage_multi_aff(map
, type
, ma
);
11704 isl_multi_aff_free(ma
);
11705 return isl_map_free(map
);
11708 /* Compute the preimage of "set" under the function represented by "ma".
11709 * In other words, plug in "ma" "set". The result is a set
11710 * that lives in the domain space of "ma".
11712 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
11713 __isl_take isl_multi_aff
*ma
)
11715 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
11718 /* Compute the preimage of the domain of "map" under the function
11719 * represented by "ma".
11720 * In other words, plug in "ma" in the domain of "map".
11721 * The result is a map that lives in the same space as "map"
11722 * except that the domain has been replaced by the domain space of "ma".
11724 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
11725 __isl_take isl_multi_aff
*ma
)
11727 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
11730 /* Compute the preimage of "set" under the function represented by "pma".
11731 * In other words, plug in "pma" in "set. The result is a set
11732 * that lives in the domain space of "pma".
11734 static __isl_give isl_set
*set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
11735 __isl_take isl_pw_multi_aff
*pma
)
11744 isl_pw_multi_aff_free(pma
);
11745 res
= isl_set_empty(isl_set_get_space(set
));
11750 res
= isl_set_preimage_multi_aff(isl_set_copy(set
),
11751 isl_multi_aff_copy(pma
->p
[0].maff
));
11752 res
= isl_set_intersect(res
, isl_set_copy(pma
->p
[0].set
));
11754 for (i
= 1; i
< pma
->n
; ++i
) {
11757 res_i
= isl_set_preimage_multi_aff(isl_set_copy(set
),
11758 isl_multi_aff_copy(pma
->p
[i
].maff
));
11759 res_i
= isl_set_intersect(res_i
, isl_set_copy(pma
->p
[i
].set
));
11760 res
= isl_set_union(res
, res_i
);
11763 isl_pw_multi_aff_free(pma
);
11767 isl_pw_multi_aff_free(pma
);
11772 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
11773 __isl_take isl_pw_multi_aff
*pma
)
11778 if (isl_space_match(set
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
11779 return set_preimage_pw_multi_aff(set
, pma
);
11781 if (!isl_space_has_named_params(set
->dim
) ||
11782 !isl_space_has_named_params(pma
->dim
))
11783 isl_die(set
->ctx
, isl_error_invalid
,
11784 "unaligned unnamed parameters", goto error
);
11785 set
= isl_set_align_params(set
, isl_pw_multi_aff_get_space(pma
));
11786 pma
= isl_pw_multi_aff_align_params(pma
, isl_set_get_space(set
));
11788 return set_preimage_pw_multi_aff(set
, pma
);
11790 isl_pw_multi_aff_free(pma
);
11791 return isl_set_free(set
);