2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
38 case isl_dim_param
: return dim
->nparam
;
39 case isl_dim_in
: return dim
->n_in
;
40 case isl_dim_out
: return dim
->n_out
;
41 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
46 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return 1;
50 case isl_dim_in
: return 1 + dim
->nparam
;
51 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
57 enum isl_dim_type type
)
62 case isl_dim_cst
: return 1;
65 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
66 case isl_dim_div
: return bmap
->n_div
;
67 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
72 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
74 return map
? n(map
->dim
, type
) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
79 return set
? n(set
->dim
, type
) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
83 enum isl_dim_type type
)
85 isl_space
*dim
= bmap
->dim
;
87 case isl_dim_cst
: return 0;
88 case isl_dim_param
: return 1;
89 case isl_dim_in
: return 1 + dim
->nparam
;
90 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
91 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
96 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
97 enum isl_dim_type type
)
99 return isl_basic_map_offset(bset
, type
);
102 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
104 return pos(map
->dim
, type
);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
108 enum isl_dim_type type
)
110 return isl_basic_map_dim(bset
, type
);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
115 return isl_basic_set_dim(bset
, isl_dim_set
);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
120 return isl_basic_set_dim(bset
, isl_dim_param
);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
125 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
128 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
130 return isl_set_dim(set
, isl_dim_set
);
133 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
135 return isl_set_dim(set
, isl_dim_param
);
138 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
140 return bmap
? bmap
->dim
->n_in
: 0;
143 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
145 return bmap
? bmap
->dim
->n_out
: 0;
148 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
150 return bmap
? bmap
->dim
->nparam
: 0;
153 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
155 return bmap
? bmap
->n_div
: 0;
158 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
160 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
163 unsigned isl_map_n_in(const struct isl_map
*map
)
165 return map
? map
->dim
->n_in
: 0;
168 unsigned isl_map_n_out(const struct isl_map
*map
)
170 return map
? map
->dim
->n_out
: 0;
173 unsigned isl_map_n_param(const struct isl_map
*map
)
175 return map
? map
->dim
->nparam
: 0;
178 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
183 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
186 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
189 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
190 struct isl_basic_set
*bset
)
195 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
198 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
201 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
206 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
209 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
212 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
213 struct isl_basic_set
*bset
)
218 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
221 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
224 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
226 return bmap
? bmap
->ctx
: NULL
;
229 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
231 return bset
? bset
->ctx
: NULL
;
234 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
236 return map
? map
->ctx
: NULL
;
239 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
241 return set
? set
->ctx
: NULL
;
244 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
248 return isl_space_copy(bmap
->dim
);
251 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
255 return isl_space_copy(bset
->dim
);
258 __isl_give isl_local_space
*isl_basic_map_get_local_space(
259 __isl_keep isl_basic_map
*bmap
)
268 total
= isl_basic_map_total_dim(bmap
);
269 ls
= isl_local_space_alloc(isl_space_copy(bmap
->dim
), bmap
->n_div
);
273 for (i
= 0; i
< bmap
->n_div
; ++i
)
274 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
279 __isl_give isl_local_space
*isl_basic_set_get_local_space(
280 __isl_keep isl_basic_set
*bset
)
282 return isl_basic_map_get_local_space(bset
);
285 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
286 __isl_take isl_local_space
*ls
)
295 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
296 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
297 n_div
, 0, 2 * n_div
);
299 for (i
= 0; i
< n_div
; ++i
)
300 if (isl_basic_map_alloc_div(bmap
) < 0)
303 for (i
= 0; i
< n_div
; ++i
) {
304 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
305 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
309 isl_local_space_free(ls
);
312 isl_local_space_free(ls
);
313 isl_basic_map_free(bmap
);
317 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
318 __isl_take isl_local_space
*ls
)
320 return isl_basic_map_from_local_space(ls
);
323 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
327 return isl_space_copy(map
->dim
);
330 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
334 return isl_space_copy(set
->dim
);
337 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
338 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
340 bmap
= isl_basic_map_cow(bmap
);
343 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
346 bmap
= isl_basic_map_finalize(bmap
);
349 isl_basic_map_free(bmap
);
353 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
354 __isl_take isl_basic_set
*bset
, const char *s
)
356 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
359 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
360 enum isl_dim_type type
)
362 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
365 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
366 enum isl_dim_type type
, const char *s
)
370 map
= isl_map_cow(map
);
374 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
378 for (i
= 0; i
< map
->n
; ++i
) {
379 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
390 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
391 enum isl_dim_type type
)
393 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
396 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
399 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
402 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
403 enum isl_dim_type type
, __isl_take isl_id
*id
)
405 map
= isl_map_cow(map
);
407 return isl_id_free(id
);
409 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
411 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
414 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
415 __isl_take isl_id
*id
)
417 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
420 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
421 enum isl_dim_type type
)
423 map
= isl_map_cow(map
);
427 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
429 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
432 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
434 return isl_map_reset_tuple_id(set
, isl_dim_set
);
437 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
439 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
442 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
443 enum isl_dim_type type
)
445 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
448 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
450 return isl_map_has_tuple_id(set
, isl_dim_set
);
453 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
455 return isl_map_get_tuple_id(set
, isl_dim_set
);
458 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
460 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
463 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
465 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
468 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
469 enum isl_dim_type type
, unsigned pos
)
471 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
474 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
475 enum isl_dim_type type
, unsigned pos
)
477 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
480 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
481 enum isl_dim_type type
, unsigned pos
)
483 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
486 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
487 enum isl_dim_type type
, unsigned pos
)
489 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
492 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
493 __isl_take isl_basic_map
*bmap
,
494 enum isl_dim_type type
, unsigned pos
, const char *s
)
496 bmap
= isl_basic_map_cow(bmap
);
499 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
504 isl_basic_map_free(bmap
);
508 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
509 enum isl_dim_type type
, unsigned pos
, const char *s
)
513 map
= isl_map_cow(map
);
517 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
521 for (i
= 0; i
< map
->n
; ++i
) {
522 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
533 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
534 __isl_take isl_basic_set
*bset
,
535 enum isl_dim_type type
, unsigned pos
, const char *s
)
537 return (isl_basic_set
*)isl_basic_map_set_dim_name(
538 (isl_basic_map
*)bset
, type
, pos
, s
);
541 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
542 enum isl_dim_type type
, unsigned pos
, const char *s
)
544 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
547 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
548 enum isl_dim_type type
, unsigned pos
)
550 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
553 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
554 enum isl_dim_type type
, unsigned pos
)
556 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
559 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
560 enum isl_dim_type type
, unsigned pos
)
562 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
565 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
566 enum isl_dim_type type
, unsigned pos
)
568 return isl_map_has_dim_id(set
, type
, pos
);
571 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
572 enum isl_dim_type type
, unsigned pos
)
574 return isl_map_get_dim_id(set
, type
, pos
);
577 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
578 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
580 map
= isl_map_cow(map
);
582 return isl_id_free(id
);
584 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
586 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
589 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
590 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
592 return isl_map_set_dim_id(set
, type
, pos
, id
);
595 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
596 __isl_keep isl_id
*id
)
600 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
603 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
604 __isl_keep isl_id
*id
)
606 return isl_map_find_dim_by_id(set
, type
, id
);
609 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
614 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
617 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
620 return isl_map_find_dim_by_name(set
, type
, name
);
623 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
627 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
630 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
632 return isl_basic_map_is_rational(bset
);
635 /* Is this basic set a parameter domain?
637 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
641 return isl_space_is_params(bset
->dim
);
644 /* Is this set a parameter domain?
646 int isl_set_is_params(__isl_keep isl_set
*set
)
650 return isl_space_is_params(set
->dim
);
653 /* Is this map actually a parameter domain?
654 * Users should never call this function. Outside of isl,
655 * a map can never be a parameter domain.
657 int isl_map_is_params(__isl_keep isl_map
*map
)
661 return isl_space_is_params(map
->dim
);
664 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
665 struct isl_basic_map
*bmap
, unsigned extra
,
666 unsigned n_eq
, unsigned n_ineq
)
669 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
674 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
675 if (isl_blk_is_error(bmap
->block
))
678 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
683 bmap
->block2
= isl_blk_empty();
686 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
687 if (isl_blk_is_error(bmap
->block2
))
690 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
695 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
696 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
698 for (i
= 0; i
< extra
; ++i
)
699 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
703 bmap
->c_size
= n_eq
+ n_ineq
;
704 bmap
->eq
= bmap
->ineq
+ n_ineq
;
713 isl_basic_map_free(bmap
);
717 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
718 unsigned nparam
, unsigned dim
, unsigned extra
,
719 unsigned n_eq
, unsigned n_ineq
)
721 struct isl_basic_map
*bmap
;
724 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
728 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
729 return (struct isl_basic_set
*)bmap
;
732 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
733 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
735 struct isl_basic_map
*bmap
;
738 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
739 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
740 return (struct isl_basic_set
*)bmap
;
746 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
747 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
749 struct isl_basic_map
*bmap
;
753 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
758 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
764 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
765 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
766 unsigned n_eq
, unsigned n_ineq
)
768 struct isl_basic_map
*bmap
;
771 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
775 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
779 static void dup_constraints(
780 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
783 unsigned total
= isl_basic_map_total_dim(src
);
785 for (i
= 0; i
< src
->n_eq
; ++i
) {
786 int j
= isl_basic_map_alloc_equality(dst
);
787 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
790 for (i
= 0; i
< src
->n_ineq
; ++i
) {
791 int j
= isl_basic_map_alloc_inequality(dst
);
792 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
795 for (i
= 0; i
< src
->n_div
; ++i
) {
796 int j
= isl_basic_map_alloc_div(dst
);
797 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
799 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
802 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
804 struct isl_basic_map
*dup
;
808 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
809 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
812 dup_constraints(dup
, bmap
);
813 dup
->flags
= bmap
->flags
;
814 dup
->sample
= isl_vec_copy(bmap
->sample
);
818 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
820 struct isl_basic_map
*dup
;
822 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
823 return (struct isl_basic_set
*)dup
;
826 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
831 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
835 return isl_basic_set_dup(bset
);
838 struct isl_set
*isl_set_copy(struct isl_set
*set
)
847 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
852 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
856 bmap
= isl_basic_map_dup(bmap
);
858 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
862 struct isl_map
*isl_map_copy(struct isl_map
*map
)
871 void isl_basic_map_free(struct isl_basic_map
*bmap
)
879 isl_ctx_deref(bmap
->ctx
);
881 isl_blk_free(bmap
->ctx
, bmap
->block2
);
883 isl_blk_free(bmap
->ctx
, bmap
->block
);
884 isl_vec_free(bmap
->sample
);
885 isl_space_free(bmap
->dim
);
889 void isl_basic_set_free(struct isl_basic_set
*bset
)
891 isl_basic_map_free((struct isl_basic_map
*)bset
);
894 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
896 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
899 __isl_give isl_map
*isl_map_align_params_map_map_and(
900 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
901 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
902 __isl_take isl_map
*map2
))
906 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
907 return fn(map1
, map2
);
908 if (!isl_space_has_named_params(map1
->dim
) ||
909 !isl_space_has_named_params(map2
->dim
))
910 isl_die(map1
->ctx
, isl_error_invalid
,
911 "unaligned unnamed parameters", goto error
);
912 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
913 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
914 return fn(map1
, map2
);
921 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
922 __isl_keep isl_map
*map2
,
923 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
929 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
930 return fn(map1
, map2
);
931 if (!isl_space_has_named_params(map1
->dim
) ||
932 !isl_space_has_named_params(map2
->dim
))
933 isl_die(map1
->ctx
, isl_error_invalid
,
934 "unaligned unnamed parameters", return -1);
935 map1
= isl_map_copy(map1
);
936 map2
= isl_map_copy(map2
);
937 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
938 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
945 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
951 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
952 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
954 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
955 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
956 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
957 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
958 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
959 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
961 int j
= isl_basic_map_alloc_inequality(bmap
);
965 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
966 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
973 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
974 bmap
->extra
- bmap
->n_div
);
978 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
980 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
983 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
987 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
992 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
994 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
997 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1002 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1004 if (pos
!= bmap
->n_eq
- 1) {
1006 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1007 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1013 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1015 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1018 void isl_basic_map_inequality_to_equality(
1019 struct isl_basic_map
*bmap
, unsigned pos
)
1023 t
= bmap
->ineq
[pos
];
1024 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1025 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1030 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1031 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1032 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1033 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1036 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1038 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1041 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1043 struct isl_ctx
*ctx
;
1047 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1048 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1049 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1050 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1051 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1052 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1053 1 + isl_basic_map_total_dim(bmap
),
1054 bmap
->extra
- bmap
->n_div
);
1055 return bmap
->n_ineq
++;
1058 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1060 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1063 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1067 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1072 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1074 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1077 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1082 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1084 if (pos
!= bmap
->n_ineq
- 1) {
1085 t
= bmap
->ineq
[pos
];
1086 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1087 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1088 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1094 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1096 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1099 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1104 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1107 k
= isl_basic_map_alloc_equality(bmap
);
1110 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1113 isl_basic_map_free(bmap
);
1117 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1120 return (isl_basic_set
*)
1121 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1124 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1129 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1132 k
= isl_basic_map_alloc_inequality(bmap
);
1135 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1138 isl_basic_map_free(bmap
);
1142 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1145 return (isl_basic_set
*)
1146 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1149 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1153 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1154 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1155 1 + 1 + isl_basic_map_total_dim(bmap
),
1156 bmap
->extra
- bmap
->n_div
);
1157 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1158 return bmap
->n_div
++;
1161 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1163 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1166 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1170 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1175 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1177 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1180 /* Copy constraint from src to dst, putting the vars of src at offset
1181 * dim_off in dst and the divs of src at offset div_off in dst.
1182 * If both sets are actually map, then dim_off applies to the input
1185 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1186 struct isl_basic_map
*src_map
, isl_int
*src
,
1187 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1189 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1190 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1191 unsigned src_in
= isl_basic_map_n_in(src_map
);
1192 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1193 unsigned src_out
= isl_basic_map_n_out(src_map
);
1194 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1195 isl_int_set(dst
[0], src
[0]);
1196 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1197 if (dst_nparam
> src_nparam
)
1198 isl_seq_clr(dst
+1+src_nparam
,
1199 dst_nparam
- src_nparam
);
1200 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1201 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1203 isl_min(dst_in
-in_off
, src_in
));
1204 if (dst_in
-in_off
> src_in
)
1205 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1206 dst_in
- in_off
- src_in
);
1207 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1208 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1209 src
+1+src_nparam
+src_in
,
1210 isl_min(dst_out
-out_off
, src_out
));
1211 if (dst_out
-out_off
> src_out
)
1212 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1213 dst_out
- out_off
- src_out
);
1214 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1215 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1216 src
+1+src_nparam
+src_in
+src_out
,
1217 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1218 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1219 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1220 div_off
+src_map
->n_div
,
1221 dst_map
->n_div
- div_off
- src_map
->n_div
);
1224 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1225 struct isl_basic_map
*src_map
, isl_int
*src
,
1226 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1228 isl_int_set(dst
[0], src
[0]);
1229 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1232 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1233 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1238 if (!bmap1
|| !bmap2
)
1241 div_off
= bmap1
->n_div
;
1243 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1244 int i1
= isl_basic_map_alloc_equality(bmap1
);
1247 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1248 i_pos
, o_pos
, div_off
);
1251 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1252 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1255 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1256 i_pos
, o_pos
, div_off
);
1259 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1260 int i1
= isl_basic_map_alloc_div(bmap1
);
1263 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1264 i_pos
, o_pos
, div_off
);
1267 isl_basic_map_free(bmap2
);
1272 isl_basic_map_free(bmap1
);
1273 isl_basic_map_free(bmap2
);
1277 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1278 struct isl_basic_set
*bset2
, unsigned pos
)
1280 return (struct isl_basic_set
*)
1281 add_constraints((struct isl_basic_map
*)bset1
,
1282 (struct isl_basic_map
*)bset2
, 0, pos
);
1285 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1286 __isl_take isl_space
*dim
, unsigned extra
,
1287 unsigned n_eq
, unsigned n_ineq
)
1289 struct isl_basic_map
*ext
;
1299 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1300 base
->extra
>= base
->n_div
+ extra
;
1302 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1303 room_for_ineq(base
, n_ineq
)) {
1304 isl_space_free(dim
);
1308 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1309 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1310 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1311 extra
+= base
->extra
;
1313 n_ineq
+= base
->n_ineq
;
1315 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1321 ext
->sample
= isl_vec_copy(base
->sample
);
1322 flags
= base
->flags
;
1323 ext
= add_constraints(ext
, base
, 0, 0);
1326 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1332 isl_space_free(dim
);
1333 isl_basic_map_free(base
);
1337 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1338 __isl_take isl_space
*dim
, unsigned extra
,
1339 unsigned n_eq
, unsigned n_ineq
)
1341 return (struct isl_basic_set
*)
1342 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1343 extra
, n_eq
, n_ineq
);
1346 struct isl_basic_map
*isl_basic_map_extend_constraints(
1347 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1351 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1355 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1356 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1357 unsigned n_eq
, unsigned n_ineq
)
1359 struct isl_basic_map
*bmap
;
1364 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1368 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1371 isl_basic_map_free(base
);
1375 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1376 unsigned nparam
, unsigned dim
, unsigned extra
,
1377 unsigned n_eq
, unsigned n_ineq
)
1379 return (struct isl_basic_set
*)
1380 isl_basic_map_extend((struct isl_basic_map
*)base
,
1381 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1384 struct isl_basic_set
*isl_basic_set_extend_constraints(
1385 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1387 return (struct isl_basic_set
*)
1388 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1392 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1394 return (struct isl_basic_set
*)
1395 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1398 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1403 if (bmap
->ref
> 1) {
1405 bmap
= isl_basic_map_dup(bmap
);
1408 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1412 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1420 return isl_set_dup(set
);
1423 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1431 return isl_map_dup(map
);
1434 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1435 unsigned a_len
, unsigned b_len
)
1437 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1438 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1439 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1442 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1443 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1451 isl_assert(bmap
->ctx
,
1452 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1454 if (n1
== 0 || n2
== 0)
1457 bmap
= isl_basic_map_cow(bmap
);
1461 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1462 if (isl_blk_is_error(blk
))
1465 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1467 bmap
->eq
[i
] + pos
, n1
, n2
);
1469 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1471 bmap
->ineq
[i
] + pos
, n1
, n2
);
1473 for (i
= 0; i
< bmap
->n_div
; ++i
)
1475 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1477 isl_blk_free(bmap
->ctx
, blk
);
1479 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1480 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1481 return isl_basic_map_finalize(bmap
);
1483 isl_basic_map_free(bmap
);
1487 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1488 __isl_take isl_basic_set
*bset
, unsigned n
)
1493 nparam
= isl_basic_set_n_param(bset
);
1494 dim
= isl_basic_set_n_dim(bset
);
1495 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1497 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1499 isl_basic_set_free(bset
);
1503 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1509 total
= isl_basic_map_total_dim(bmap
);
1510 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1511 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1513 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1515 i
= isl_basic_map_alloc_equality(bmap
);
1519 isl_int_set_si(bmap
->eq
[i
][0], 1);
1520 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1521 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1522 isl_vec_free(bmap
->sample
);
1523 bmap
->sample
= NULL
;
1524 return isl_basic_map_finalize(bmap
);
1526 isl_basic_map_free(bmap
);
1530 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1532 return (struct isl_basic_set
*)
1533 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1536 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1539 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1540 isl_int
*t
= bmap
->div
[a
];
1541 bmap
->div
[a
] = bmap
->div
[b
];
1544 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1545 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1547 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1548 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1550 for (i
= 0; i
< bmap
->n_div
; ++i
)
1551 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1552 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1555 /* Eliminate the specified n dimensions starting at first from the
1556 * constraints using Fourier-Motzkin. The dimensions themselves
1559 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1560 enum isl_dim_type type
, unsigned first
, unsigned n
)
1569 map
= isl_map_cow(map
);
1573 for (i
= 0; i
< map
->n
; ++i
) {
1574 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1584 /* Eliminate the specified n dimensions starting at first from the
1585 * constraints using Fourier-Motzkin. The dimensions themselves
1588 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1589 enum isl_dim_type type
, unsigned first
, unsigned n
)
1591 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1594 /* Eliminate the specified n dimensions starting at first from the
1595 * constraints using Fourier-Motzkin. The dimensions themselves
1598 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1599 unsigned first
, unsigned n
)
1601 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1604 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1605 __isl_take isl_basic_map
*bmap
)
1609 bmap
= isl_basic_map_eliminate_vars(bmap
,
1610 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1614 return isl_basic_map_finalize(bmap
);
1617 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1618 __isl_take isl_basic_set
*bset
)
1620 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1621 (struct isl_basic_map
*)bset
);
1624 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1633 map
= isl_map_cow(map
);
1637 for (i
= 0; i
< map
->n
; ++i
) {
1638 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1648 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1650 return isl_map_remove_divs(set
);
1653 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1654 enum isl_dim_type type
, unsigned first
, unsigned n
)
1658 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1660 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1662 bmap
= isl_basic_map_eliminate_vars(bmap
,
1663 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1666 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1668 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1671 isl_basic_map_free(bmap
);
1675 /* Return true if the definition of the given div (recursively) involves
1676 * any of the given variables.
1678 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1679 unsigned first
, unsigned n
)
1682 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1684 if (isl_int_is_zero(bmap
->div
[div
][0]))
1686 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1689 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1690 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1692 if (div_involves_vars(bmap
, i
, first
, n
))
1699 /* Remove all divs (recursively) involving any of the given dimensions
1700 * in their definitions.
1702 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1703 __isl_take isl_basic_map
*bmap
,
1704 enum isl_dim_type type
, unsigned first
, unsigned n
)
1710 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1712 first
+= isl_basic_map_offset(bmap
, type
);
1714 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1715 if (!div_involves_vars(bmap
, i
, first
, n
))
1717 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1725 isl_basic_map_free(bmap
);
1729 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1730 enum isl_dim_type type
, unsigned first
, unsigned n
)
1739 map
= isl_map_cow(map
);
1743 for (i
= 0; i
< map
->n
; ++i
) {
1744 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1755 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1756 enum isl_dim_type type
, unsigned first
, unsigned n
)
1758 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1762 /* Does the desciption of "bmap" depend on the specified dimensions?
1763 * We also check whether the dimensions appear in any of the div definitions.
1764 * In principle there is no need for this check. If the dimensions appear
1765 * in a div definition, they also appear in the defining constraints of that
1768 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1769 enum isl_dim_type type
, unsigned first
, unsigned n
)
1776 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1777 isl_die(bmap
->ctx
, isl_error_invalid
,
1778 "index out of bounds", return -1);
1780 first
+= isl_basic_map_offset(bmap
, type
);
1781 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1782 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1784 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1785 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1787 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1788 if (isl_int_is_zero(bmap
->div
[i
][0]))
1790 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
1797 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1798 enum isl_dim_type type
, unsigned first
, unsigned n
)
1805 if (first
+ n
> isl_map_dim(map
, type
))
1806 isl_die(map
->ctx
, isl_error_invalid
,
1807 "index out of bounds", return -1);
1809 for (i
= 0; i
< map
->n
; ++i
) {
1810 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1812 if (involves
< 0 || involves
)
1819 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1820 enum isl_dim_type type
, unsigned first
, unsigned n
)
1822 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1825 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1826 enum isl_dim_type type
, unsigned first
, unsigned n
)
1828 return isl_map_involves_dims(set
, type
, first
, n
);
1831 /* Return true if the definition of the given div is unknown or depends
1834 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1837 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1839 if (isl_int_is_zero(bmap
->div
[div
][0]))
1842 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1843 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1845 if (div_is_unknown(bmap
, i
))
1852 /* Remove all divs that are unknown or defined in terms of unknown divs.
1854 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1855 __isl_take isl_basic_map
*bmap
)
1862 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1863 if (!div_is_unknown(bmap
, i
))
1865 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1874 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1883 map
= isl_map_cow(map
);
1887 for (i
= 0; i
< map
->n
; ++i
) {
1888 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1898 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1900 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1903 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1904 __isl_take isl_basic_set
*bset
,
1905 enum isl_dim_type type
, unsigned first
, unsigned n
)
1907 return (isl_basic_set
*)
1908 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1911 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1912 enum isl_dim_type type
, unsigned first
, unsigned n
)
1919 map
= isl_map_cow(map
);
1922 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1924 for (i
= 0; i
< map
->n
; ++i
) {
1925 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1926 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1930 map
= isl_map_drop(map
, type
, first
, n
);
1937 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1938 enum isl_dim_type type
, unsigned first
, unsigned n
)
1940 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1943 /* Project out n inputs starting at first using Fourier-Motzkin */
1944 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1945 unsigned first
, unsigned n
)
1947 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1950 static void dump_term(struct isl_basic_map
*bmap
,
1951 isl_int c
, int pos
, FILE *out
)
1954 unsigned in
= isl_basic_map_n_in(bmap
);
1955 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1956 unsigned nparam
= isl_basic_map_n_param(bmap
);
1958 isl_int_print(out
, c
, 0);
1960 if (!isl_int_is_one(c
))
1961 isl_int_print(out
, c
, 0);
1962 if (pos
< 1 + nparam
) {
1963 name
= isl_space_get_dim_name(bmap
->dim
,
1964 isl_dim_param
, pos
- 1);
1966 fprintf(out
, "%s", name
);
1968 fprintf(out
, "p%d", pos
- 1);
1969 } else if (pos
< 1 + nparam
+ in
)
1970 fprintf(out
, "i%d", pos
- 1 - nparam
);
1971 else if (pos
< 1 + nparam
+ dim
)
1972 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1974 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1978 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1979 int sign
, FILE *out
)
1983 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1987 for (i
= 0, first
= 1; i
< len
; ++i
) {
1988 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1991 fprintf(out
, " + ");
1993 isl_int_abs(v
, c
[i
]);
1994 dump_term(bmap
, v
, i
, out
);
2001 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2002 const char *op
, FILE *out
, int indent
)
2006 fprintf(out
, "%*s", indent
, "");
2008 dump_constraint_sign(bmap
, c
, 1, out
);
2009 fprintf(out
, " %s ", op
);
2010 dump_constraint_sign(bmap
, c
, -1, out
);
2014 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2015 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2017 fprintf(out
, "%*s", indent
, "");
2018 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2023 static void dump_constraints(struct isl_basic_map
*bmap
,
2024 isl_int
**c
, unsigned n
,
2025 const char *op
, FILE *out
, int indent
)
2029 for (i
= 0; i
< n
; ++i
)
2030 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2033 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2037 unsigned total
= isl_basic_map_total_dim(bmap
);
2039 for (j
= 0; j
< 1 + total
; ++j
) {
2040 if (isl_int_is_zero(exp
[j
]))
2042 if (!first
&& isl_int_is_pos(exp
[j
]))
2044 dump_term(bmap
, exp
[j
], j
, out
);
2049 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2053 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2054 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2056 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2057 fprintf(out
, "%*s", indent
, "");
2058 fprintf(out
, "e%d = [(", i
);
2059 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2061 isl_int_print(out
, bmap
->div
[i
][0], 0);
2062 fprintf(out
, "]\n");
2066 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2067 FILE *out
, int indent
)
2070 fprintf(out
, "null basic set\n");
2074 fprintf(out
, "%*s", indent
, "");
2075 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2076 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2077 bset
->extra
, bset
->flags
);
2078 dump((struct isl_basic_map
*)bset
, out
, indent
);
2081 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2082 FILE *out
, int indent
)
2085 fprintf(out
, "null basic map\n");
2089 fprintf(out
, "%*s", indent
, "");
2090 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2091 "flags: %x, n_name: %d\n",
2093 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2094 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2095 dump(bmap
, out
, indent
);
2098 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2103 total
= isl_basic_map_total_dim(bmap
);
2104 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2105 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2106 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2107 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2111 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2114 struct isl_set
*set
;
2118 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2119 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2120 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2121 sizeof(struct isl_set
) +
2122 (n
- 1) * sizeof(struct isl_basic_set
*));
2126 set
->ctx
= dim
->ctx
;
2127 isl_ctx_ref(set
->ctx
);
2135 isl_space_free(dim
);
2139 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2140 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2142 struct isl_set
*set
;
2145 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2149 set
= isl_set_alloc_space(dims
, n
, flags
);
2153 /* Make sure "map" has room for at least "n" more basic maps.
2155 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2158 struct isl_map
*grown
= NULL
;
2162 isl_assert(map
->ctx
, n
>= 0, goto error
);
2163 if (map
->n
+ n
<= map
->size
)
2165 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2168 for (i
= 0; i
< map
->n
; ++i
) {
2169 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2177 isl_map_free(grown
);
2182 /* Make sure "set" has room for at least "n" more basic sets.
2184 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2186 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2189 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2192 struct isl_set
*dup
;
2197 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2200 for (i
= 0; i
< set
->n
; ++i
)
2201 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2205 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2207 return isl_map_from_basic_map(bset
);
2210 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2212 struct isl_map
*map
;
2217 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2218 return isl_map_add_basic_map(map
, bmap
);
2221 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2222 __isl_take isl_basic_set
*bset
)
2224 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2225 (struct isl_basic_map
*)bset
);
2228 void isl_set_free(struct isl_set
*set
)
2238 isl_ctx_deref(set
->ctx
);
2239 for (i
= 0; i
< set
->n
; ++i
)
2240 isl_basic_set_free(set
->p
[i
]);
2241 isl_space_free(set
->dim
);
2245 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2250 fprintf(out
, "null set\n");
2254 fprintf(out
, "%*s", indent
, "");
2255 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2256 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2258 for (i
= 0; i
< set
->n
; ++i
) {
2259 fprintf(out
, "%*s", indent
, "");
2260 fprintf(out
, "basic set %d:\n", i
);
2261 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2265 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2270 fprintf(out
, "null map\n");
2274 fprintf(out
, "%*s", indent
, "");
2275 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2276 "flags: %x, n_name: %d\n",
2277 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2278 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2279 for (i
= 0; i
< map
->n
; ++i
) {
2280 fprintf(out
, "%*s", indent
, "");
2281 fprintf(out
, "basic map %d:\n", i
);
2282 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2286 struct isl_basic_map
*isl_basic_map_intersect_domain(
2287 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2289 struct isl_basic_map
*bmap_domain
;
2294 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2295 bset
->dim
, isl_dim_param
), goto error
);
2297 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2298 isl_assert(bset
->ctx
,
2299 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2301 bmap
= isl_basic_map_cow(bmap
);
2304 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2305 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2306 bmap_domain
= isl_basic_map_from_domain(bset
);
2307 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2309 bmap
= isl_basic_map_simplify(bmap
);
2310 return isl_basic_map_finalize(bmap
);
2312 isl_basic_map_free(bmap
);
2313 isl_basic_set_free(bset
);
2317 struct isl_basic_map
*isl_basic_map_intersect_range(
2318 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2320 struct isl_basic_map
*bmap_range
;
2325 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2326 bset
->dim
, isl_dim_param
), goto error
);
2328 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2329 isl_assert(bset
->ctx
,
2330 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2332 if (isl_basic_set_is_universe(bset
)) {
2333 isl_basic_set_free(bset
);
2337 bmap
= isl_basic_map_cow(bmap
);
2340 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2341 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2342 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2343 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2345 bmap
= isl_basic_map_simplify(bmap
);
2346 return isl_basic_map_finalize(bmap
);
2348 isl_basic_map_free(bmap
);
2349 isl_basic_set_free(bset
);
2353 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2359 total
= 1 + isl_basic_map_total_dim(bmap
);
2360 if (total
!= vec
->size
)
2365 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2366 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2367 if (!isl_int_is_zero(s
)) {
2373 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2374 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2375 if (isl_int_is_neg(s
)) {
2386 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2388 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2391 struct isl_basic_map
*isl_basic_map_intersect(
2392 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2394 struct isl_vec
*sample
= NULL
;
2396 if (!bmap1
|| !bmap2
)
2399 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2400 bmap2
->dim
, isl_dim_param
), goto error
);
2401 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2402 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2403 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2404 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2405 return isl_basic_map_intersect(bmap2
, bmap1
);
2407 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2408 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2409 isl_assert(bmap1
->ctx
,
2410 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2412 if (bmap1
->sample
&&
2413 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2414 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2415 sample
= isl_vec_copy(bmap1
->sample
);
2416 else if (bmap2
->sample
&&
2417 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2418 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2419 sample
= isl_vec_copy(bmap2
->sample
);
2421 bmap1
= isl_basic_map_cow(bmap1
);
2424 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2425 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2426 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2429 isl_vec_free(sample
);
2431 isl_vec_free(bmap1
->sample
);
2432 bmap1
->sample
= sample
;
2435 bmap1
= isl_basic_map_simplify(bmap1
);
2436 return isl_basic_map_finalize(bmap1
);
2439 isl_vec_free(sample
);
2440 isl_basic_map_free(bmap1
);
2441 isl_basic_map_free(bmap2
);
2445 struct isl_basic_set
*isl_basic_set_intersect(
2446 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2448 return (struct isl_basic_set
*)
2449 isl_basic_map_intersect(
2450 (struct isl_basic_map
*)bset1
,
2451 (struct isl_basic_map
*)bset2
);
2454 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2455 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2457 return isl_basic_set_intersect(bset1
, bset2
);
2460 /* Special case of isl_map_intersect, where both map1 and map2
2461 * are convex, without any divs and such that either map1 or map2
2462 * contains a single constraint. This constraint is then simply
2463 * added to the other map.
2465 static __isl_give isl_map
*map_intersect_add_constraint(
2466 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2468 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2469 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2470 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2471 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2473 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2474 return isl_map_intersect(map2
, map1
);
2476 isl_assert(map2
->ctx
,
2477 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2479 map1
= isl_map_cow(map1
);
2482 if (isl_map_plain_is_empty(map1
)) {
2486 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2487 if (map2
->p
[0]->n_eq
== 1)
2488 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2490 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2491 map2
->p
[0]->ineq
[0]);
2493 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2494 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2498 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2499 isl_basic_map_free(map1
->p
[0]);
2512 /* map2 may be either a parameter domain or a map living in the same
2515 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2516 __isl_take isl_map
*map2
)
2519 struct isl_map
*result
;
2525 if (isl_map_plain_is_empty(map1
) &&
2526 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2530 if (isl_map_plain_is_empty(map2
) &&
2531 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2536 if (map1
->n
== 1 && map2
->n
== 1 &&
2537 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2538 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2539 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2540 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2541 return map_intersect_add_constraint(map1
, map2
);
2543 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2544 isl_space_dim(map2
->dim
, isl_dim_param
))
2545 isl_assert(map1
->ctx
,
2546 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2548 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2549 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2550 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2552 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2553 map1
->n
* map2
->n
, flags
);
2556 for (i
= 0; i
< map1
->n
; ++i
)
2557 for (j
= 0; j
< map2
->n
; ++j
) {
2558 struct isl_basic_map
*part
;
2559 part
= isl_basic_map_intersect(
2560 isl_basic_map_copy(map1
->p
[i
]),
2561 isl_basic_map_copy(map2
->p
[j
]));
2562 if (isl_basic_map_is_empty(part
))
2563 isl_basic_map_free(part
);
2565 result
= isl_map_add_basic_map(result
, part
);
2578 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2579 __isl_take isl_map
*map2
)
2583 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2584 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2585 "spaces don't match", goto error
);
2586 return map_intersect_internal(map1
, map2
);
2593 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2594 __isl_take isl_map
*map2
)
2596 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2599 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2601 return (struct isl_set
*)
2602 isl_map_intersect((struct isl_map
*)set1
,
2603 (struct isl_map
*)set2
);
2606 /* map_intersect_internal accepts intersections
2607 * with parameter domains, so we can just call that function.
2609 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2610 __isl_take isl_set
*params
)
2612 return map_intersect_internal(map
, params
);
2615 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2616 __isl_take isl_map
*map2
)
2618 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2621 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2622 __isl_take isl_set
*params
)
2624 return isl_map_intersect_params(set
, params
);
2627 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2630 struct isl_basic_set
*bset
;
2635 bmap
= isl_basic_map_cow(bmap
);
2638 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2639 in
= isl_basic_map_n_in(bmap
);
2640 bset
= isl_basic_set_from_basic_map(bmap
);
2641 bset
= isl_basic_set_swap_vars(bset
, in
);
2642 return isl_basic_map_from_basic_set(bset
, dim
);
2645 static __isl_give isl_basic_map
*basic_map_space_reset(
2646 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2650 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2653 space
= isl_basic_map_get_space(bmap
);
2654 space
= isl_space_reset(space
, type
);
2655 bmap
= isl_basic_map_reset_space(bmap
, space
);
2659 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2660 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2663 struct isl_basic_map
*res
;
2664 struct isl_dim_map
*dim_map
;
2665 unsigned total
, off
;
2666 enum isl_dim_type t
;
2669 return basic_map_space_reset(bmap
, type
);
2674 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2676 total
= isl_basic_map_total_dim(bmap
) + n
;
2677 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2679 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2681 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2683 unsigned size
= isl_basic_map_dim(bmap
, t
);
2684 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2686 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2687 pos
, size
- pos
, off
+ pos
+ n
);
2689 off
+= isl_space_dim(res_dim
, t
);
2691 isl_dim_map_div(dim_map
, bmap
, off
);
2693 res
= isl_basic_map_alloc_space(res_dim
,
2694 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2695 if (isl_basic_map_is_rational(bmap
))
2696 res
= isl_basic_map_set_rational(res
);
2697 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2698 return isl_basic_map_finalize(res
);
2701 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2702 enum isl_dim_type type
, unsigned n
)
2706 return isl_basic_map_insert(bmap
, type
,
2707 isl_basic_map_dim(bmap
, type
), n
);
2710 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2711 enum isl_dim_type type
, unsigned n
)
2715 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2716 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2718 isl_basic_set_free(bset
);
2722 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2723 enum isl_dim_type type
)
2727 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2730 space
= isl_map_get_space(map
);
2731 space
= isl_space_reset(space
, type
);
2732 map
= isl_map_reset_space(map
, space
);
2736 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2737 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2742 return map_space_reset(map
, type
);
2744 map
= isl_map_cow(map
);
2748 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2752 for (i
= 0; i
< map
->n
; ++i
) {
2753 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2764 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2765 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2767 return isl_map_insert_dims(set
, type
, pos
, n
);
2770 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2771 enum isl_dim_type type
, unsigned n
)
2775 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2778 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2779 enum isl_dim_type type
, unsigned n
)
2783 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2784 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2790 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2791 __isl_take isl_basic_map
*bmap
,
2792 enum isl_dim_type dst_type
, unsigned dst_pos
,
2793 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2795 struct isl_dim_map
*dim_map
;
2796 struct isl_basic_map
*res
;
2797 enum isl_dim_type t
;
2798 unsigned total
, off
;
2805 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2808 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2811 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2813 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2814 pos(bmap
->dim
, src_type
) + src_pos
+
2815 ((src_type
< dst_type
) ? n
: 0)) {
2816 bmap
= isl_basic_map_cow(bmap
);
2820 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2821 src_type
, src_pos
, n
);
2825 bmap
= isl_basic_map_finalize(bmap
);
2830 total
= isl_basic_map_total_dim(bmap
);
2831 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2834 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2835 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2836 if (t
== dst_type
) {
2837 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2840 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2843 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2844 dst_pos
, size
- dst_pos
, off
);
2845 off
+= size
- dst_pos
;
2846 } else if (t
== src_type
) {
2847 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2850 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2851 src_pos
+ n
, size
- src_pos
- n
, off
);
2852 off
+= size
- src_pos
- n
;
2854 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2858 isl_dim_map_div(dim_map
, bmap
, off
);
2860 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2861 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2862 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2864 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2865 src_type
, src_pos
, n
);
2869 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2870 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2871 bmap
= isl_basic_map_finalize(bmap
);
2875 isl_basic_map_free(bmap
);
2879 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2880 enum isl_dim_type dst_type
, unsigned dst_pos
,
2881 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2883 return (isl_basic_set
*)isl_basic_map_move_dims(
2884 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2887 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2888 enum isl_dim_type dst_type
, unsigned dst_pos
,
2889 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2893 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2894 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2895 src_type
, src_pos
, n
);
2901 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2902 enum isl_dim_type dst_type
, unsigned dst_pos
,
2903 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2912 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2915 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2918 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2920 map
= isl_map_cow(map
);
2924 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2928 for (i
= 0; i
< map
->n
; ++i
) {
2929 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2931 src_type
, src_pos
, n
);
2942 /* Move the specified dimensions to the last columns right before
2943 * the divs. Don't change the dimension specification of bmap.
2944 * That's the responsibility of the caller.
2946 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2947 enum isl_dim_type type
, unsigned first
, unsigned n
)
2949 struct isl_dim_map
*dim_map
;
2950 struct isl_basic_map
*res
;
2951 enum isl_dim_type t
;
2952 unsigned total
, off
;
2956 if (pos(bmap
->dim
, type
) + first
+ n
==
2957 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2960 total
= isl_basic_map_total_dim(bmap
);
2961 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2964 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2965 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2967 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2970 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2971 first
, n
, total
- bmap
->n_div
- n
);
2972 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2973 first
+ n
, size
- (first
+ n
), off
);
2974 off
+= size
- (first
+ n
);
2976 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2980 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2982 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2983 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2984 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2988 /* Turn the n dimensions of type type, starting at first
2989 * into existentially quantified variables.
2991 __isl_give isl_basic_map
*isl_basic_map_project_out(
2992 __isl_take isl_basic_map
*bmap
,
2993 enum isl_dim_type type
, unsigned first
, unsigned n
)
3001 return basic_map_space_reset(bmap
, type
);
3006 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3007 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3009 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3012 bmap
= move_last(bmap
, type
, first
, n
);
3013 bmap
= isl_basic_map_cow(bmap
);
3017 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3018 old
= bmap
->block2
.data
;
3019 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3020 (bmap
->extra
+ n
) * (1 + row_size
));
3021 if (!bmap
->block2
.data
)
3023 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3026 for (i
= 0; i
< n
; ++i
) {
3027 new_div
[i
] = bmap
->block2
.data
+
3028 (bmap
->extra
+ i
) * (1 + row_size
);
3029 isl_seq_clr(new_div
[i
], 1 + row_size
);
3031 for (i
= 0; i
< bmap
->extra
; ++i
)
3032 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3034 bmap
->div
= new_div
;
3038 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3041 bmap
= isl_basic_map_simplify(bmap
);
3042 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3043 return isl_basic_map_finalize(bmap
);
3045 isl_basic_map_free(bmap
);
3049 /* Turn the n dimensions of type type, starting at first
3050 * into existentially quantified variables.
3052 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3053 enum isl_dim_type type
, unsigned first
, unsigned n
)
3055 return (isl_basic_set
*)isl_basic_map_project_out(
3056 (isl_basic_map
*)bset
, type
, first
, n
);
3059 /* Turn the n dimensions of type type, starting at first
3060 * into existentially quantified variables.
3062 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3063 enum isl_dim_type type
, unsigned first
, unsigned n
)
3071 return map_space_reset(map
, type
);
3073 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3075 map
= isl_map_cow(map
);
3079 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3083 for (i
= 0; i
< map
->n
; ++i
) {
3084 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3095 /* Turn the n dimensions of type type, starting at first
3096 * into existentially quantified variables.
3098 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3099 enum isl_dim_type type
, unsigned first
, unsigned n
)
3101 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3104 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3108 for (i
= 0; i
< n
; ++i
) {
3109 j
= isl_basic_map_alloc_div(bmap
);
3112 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3116 isl_basic_map_free(bmap
);
3120 struct isl_basic_map
*isl_basic_map_apply_range(
3121 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3123 isl_space
*dim_result
= NULL
;
3124 struct isl_basic_map
*bmap
;
3125 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3126 struct isl_dim_map
*dim_map1
, *dim_map2
;
3128 if (!bmap1
|| !bmap2
)
3131 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3132 isl_space_copy(bmap2
->dim
));
3134 n_in
= isl_basic_map_n_in(bmap1
);
3135 n_out
= isl_basic_map_n_out(bmap2
);
3136 n
= isl_basic_map_n_out(bmap1
);
3137 nparam
= isl_basic_map_n_param(bmap1
);
3139 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3140 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3141 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3142 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3143 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3144 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3145 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3146 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3147 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3148 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3149 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3151 bmap
= isl_basic_map_alloc_space(dim_result
,
3152 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3153 bmap1
->n_eq
+ bmap2
->n_eq
,
3154 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3155 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3156 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3157 bmap
= add_divs(bmap
, n
);
3158 bmap
= isl_basic_map_simplify(bmap
);
3159 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3160 return isl_basic_map_finalize(bmap
);
3162 isl_basic_map_free(bmap1
);
3163 isl_basic_map_free(bmap2
);
3167 struct isl_basic_set
*isl_basic_set_apply(
3168 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3173 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3176 return (struct isl_basic_set
*)
3177 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3179 isl_basic_set_free(bset
);
3180 isl_basic_map_free(bmap
);
3184 struct isl_basic_map
*isl_basic_map_apply_domain(
3185 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3187 if (!bmap1
|| !bmap2
)
3190 isl_assert(bmap1
->ctx
,
3191 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3192 isl_assert(bmap1
->ctx
,
3193 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3196 bmap1
= isl_basic_map_reverse(bmap1
);
3197 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3198 return isl_basic_map_reverse(bmap1
);
3200 isl_basic_map_free(bmap1
);
3201 isl_basic_map_free(bmap2
);
3205 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3206 * A \cap B -> f(A) + f(B)
3208 struct isl_basic_map
*isl_basic_map_sum(
3209 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3211 unsigned n_in
, n_out
, nparam
, total
, pos
;
3212 struct isl_basic_map
*bmap
= NULL
;
3213 struct isl_dim_map
*dim_map1
, *dim_map2
;
3216 if (!bmap1
|| !bmap2
)
3219 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3222 nparam
= isl_basic_map_n_param(bmap1
);
3223 n_in
= isl_basic_map_n_in(bmap1
);
3224 n_out
= isl_basic_map_n_out(bmap1
);
3226 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3227 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3228 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3229 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3230 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3231 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3232 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3233 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3234 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3235 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3236 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3238 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3239 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3240 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3241 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3242 for (i
= 0; i
< n_out
; ++i
) {
3243 int j
= isl_basic_map_alloc_equality(bmap
);
3246 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3247 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3248 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3249 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3251 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3252 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3253 bmap
= add_divs(bmap
, 2 * n_out
);
3255 bmap
= isl_basic_map_simplify(bmap
);
3256 return isl_basic_map_finalize(bmap
);
3258 isl_basic_map_free(bmap
);
3259 isl_basic_map_free(bmap1
);
3260 isl_basic_map_free(bmap2
);
3264 /* Given two maps A -> f(A) and B -> g(B), construct a map
3265 * A \cap B -> f(A) + f(B)
3267 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3269 struct isl_map
*result
;
3275 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3277 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3278 map1
->n
* map2
->n
, 0);
3281 for (i
= 0; i
< map1
->n
; ++i
)
3282 for (j
= 0; j
< map2
->n
; ++j
) {
3283 struct isl_basic_map
*part
;
3284 part
= isl_basic_map_sum(
3285 isl_basic_map_copy(map1
->p
[i
]),
3286 isl_basic_map_copy(map2
->p
[j
]));
3287 if (isl_basic_map_is_empty(part
))
3288 isl_basic_map_free(part
);
3290 result
= isl_map_add_basic_map(result
, part
);
3303 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3304 __isl_take isl_set
*set2
)
3306 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3309 /* Given a basic map A -> f(A), construct A -> -f(A).
3311 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3316 bmap
= isl_basic_map_cow(bmap
);
3320 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3321 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3322 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3323 for (j
= 0; j
< n
; ++j
)
3324 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3325 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3326 for (j
= 0; j
< n
; ++j
)
3327 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3328 for (i
= 0; i
< bmap
->n_div
; ++i
)
3329 for (j
= 0; j
< n
; ++j
)
3330 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3331 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3332 return isl_basic_map_finalize(bmap
);
3335 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3337 return isl_basic_map_neg(bset
);
3340 /* Given a map A -> f(A), construct A -> -f(A).
3342 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3346 map
= isl_map_cow(map
);
3350 for (i
= 0; i
< map
->n
; ++i
) {
3351 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3362 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3364 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3367 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3368 * A -> floor(f(A)/d).
3370 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3373 unsigned n_in
, n_out
, nparam
, total
, pos
;
3374 struct isl_basic_map
*result
= NULL
;
3375 struct isl_dim_map
*dim_map
;
3381 nparam
= isl_basic_map_n_param(bmap
);
3382 n_in
= isl_basic_map_n_in(bmap
);
3383 n_out
= isl_basic_map_n_out(bmap
);
3385 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3386 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3387 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3388 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3389 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3390 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3392 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3393 bmap
->n_div
+ n_out
,
3394 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3395 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3396 result
= add_divs(result
, n_out
);
3397 for (i
= 0; i
< n_out
; ++i
) {
3399 j
= isl_basic_map_alloc_inequality(result
);
3402 isl_seq_clr(result
->ineq
[j
], 1+total
);
3403 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3404 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3405 j
= isl_basic_map_alloc_inequality(result
);
3408 isl_seq_clr(result
->ineq
[j
], 1+total
);
3409 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3410 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3411 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3414 result
= isl_basic_map_simplify(result
);
3415 return isl_basic_map_finalize(result
);
3417 isl_basic_map_free(result
);
3421 /* Given a map A -> f(A) and an integer d, construct a map
3422 * A -> floor(f(A)/d).
3424 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3428 map
= isl_map_cow(map
);
3432 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3433 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3434 for (i
= 0; i
< map
->n
; ++i
) {
3435 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3446 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3452 i
= isl_basic_map_alloc_equality(bmap
);
3455 nparam
= isl_basic_map_n_param(bmap
);
3456 n_in
= isl_basic_map_n_in(bmap
);
3457 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3458 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3459 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3460 return isl_basic_map_finalize(bmap
);
3462 isl_basic_map_free(bmap
);
3466 /* Add a constraints to "bmap" expressing i_pos < o_pos
3468 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3474 i
= isl_basic_map_alloc_inequality(bmap
);
3477 nparam
= isl_basic_map_n_param(bmap
);
3478 n_in
= isl_basic_map_n_in(bmap
);
3479 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3480 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3481 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3482 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3483 return isl_basic_map_finalize(bmap
);
3485 isl_basic_map_free(bmap
);
3489 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3491 static __isl_give isl_basic_map
*var_less_or_equal(
3492 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3498 i
= isl_basic_map_alloc_inequality(bmap
);
3501 nparam
= isl_basic_map_n_param(bmap
);
3502 n_in
= isl_basic_map_n_in(bmap
);
3503 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3504 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3505 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3506 return isl_basic_map_finalize(bmap
);
3508 isl_basic_map_free(bmap
);
3512 /* Add a constraints to "bmap" expressing i_pos > o_pos
3514 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3520 i
= isl_basic_map_alloc_inequality(bmap
);
3523 nparam
= isl_basic_map_n_param(bmap
);
3524 n_in
= isl_basic_map_n_in(bmap
);
3525 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3526 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3527 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3528 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3529 return isl_basic_map_finalize(bmap
);
3531 isl_basic_map_free(bmap
);
3535 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3537 static __isl_give isl_basic_map
*var_more_or_equal(
3538 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3544 i
= isl_basic_map_alloc_inequality(bmap
);
3547 nparam
= isl_basic_map_n_param(bmap
);
3548 n_in
= isl_basic_map_n_in(bmap
);
3549 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3550 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3551 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3552 return isl_basic_map_finalize(bmap
);
3554 isl_basic_map_free(bmap
);
3558 __isl_give isl_basic_map
*isl_basic_map_equal(
3559 __isl_take isl_space
*dim
, unsigned n_equal
)
3562 struct isl_basic_map
*bmap
;
3563 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3566 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3567 bmap
= var_equal(bmap
, i
);
3568 return isl_basic_map_finalize(bmap
);
3571 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3573 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3577 struct isl_basic_map
*bmap
;
3578 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3581 for (i
= 0; i
< pos
&& bmap
; ++i
)
3582 bmap
= var_equal(bmap
, i
);
3584 bmap
= var_less(bmap
, pos
);
3585 return isl_basic_map_finalize(bmap
);
3588 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3590 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3591 __isl_take isl_space
*dim
, unsigned pos
)
3594 isl_basic_map
*bmap
;
3596 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3597 for (i
= 0; i
< pos
; ++i
)
3598 bmap
= var_equal(bmap
, i
);
3599 bmap
= var_less_or_equal(bmap
, pos
);
3600 return isl_basic_map_finalize(bmap
);
3603 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3605 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3609 struct isl_basic_map
*bmap
;
3610 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3613 for (i
= 0; i
< pos
&& bmap
; ++i
)
3614 bmap
= var_equal(bmap
, i
);
3616 bmap
= var_more(bmap
, pos
);
3617 return isl_basic_map_finalize(bmap
);
3620 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3622 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3623 __isl_take isl_space
*dim
, unsigned pos
)
3626 isl_basic_map
*bmap
;
3628 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3629 for (i
= 0; i
< pos
; ++i
)
3630 bmap
= var_equal(bmap
, i
);
3631 bmap
= var_more_or_equal(bmap
, pos
);
3632 return isl_basic_map_finalize(bmap
);
3635 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3636 unsigned n
, int equal
)
3638 struct isl_map
*map
;
3641 if (n
== 0 && equal
)
3642 return isl_map_universe(dims
);
3644 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3646 for (i
= 0; i
+ 1 < n
; ++i
)
3647 map
= isl_map_add_basic_map(map
,
3648 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3651 map
= isl_map_add_basic_map(map
,
3652 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3654 map
= isl_map_add_basic_map(map
,
3655 isl_basic_map_less_at(dims
, n
- 1));
3657 isl_space_free(dims
);
3662 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3666 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3669 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3671 return map_lex_lte_first(dim
, n
, 0);
3674 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3676 return map_lex_lte_first(dim
, n
, 1);
3679 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3681 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3684 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3686 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3689 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3690 unsigned n
, int equal
)
3692 struct isl_map
*map
;
3695 if (n
== 0 && equal
)
3696 return isl_map_universe(dims
);
3698 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3700 for (i
= 0; i
+ 1 < n
; ++i
)
3701 map
= isl_map_add_basic_map(map
,
3702 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3705 map
= isl_map_add_basic_map(map
,
3706 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3708 map
= isl_map_add_basic_map(map
,
3709 isl_basic_map_more_at(dims
, n
- 1));
3711 isl_space_free(dims
);
3716 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3720 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3723 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3725 return map_lex_gte_first(dim
, n
, 0);
3728 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3730 return map_lex_gte_first(dim
, n
, 1);
3733 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3735 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3738 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3740 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3743 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3744 __isl_take isl_set
*set2
)
3747 map
= isl_map_lex_le(isl_set_get_space(set1
));
3748 map
= isl_map_intersect_domain(map
, set1
);
3749 map
= isl_map_intersect_range(map
, set2
);
3753 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3754 __isl_take isl_set
*set2
)
3757 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3758 map
= isl_map_intersect_domain(map
, set1
);
3759 map
= isl_map_intersect_range(map
, set2
);
3763 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3764 __isl_take isl_set
*set2
)
3767 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3768 map
= isl_map_intersect_domain(map
, set1
);
3769 map
= isl_map_intersect_range(map
, set2
);
3773 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3774 __isl_take isl_set
*set2
)
3777 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3778 map
= isl_map_intersect_domain(map
, set1
);
3779 map
= isl_map_intersect_range(map
, set2
);
3783 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3784 __isl_take isl_map
*map2
)
3787 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3788 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3789 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3793 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3794 __isl_take isl_map
*map2
)
3797 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3798 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3799 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3803 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3804 __isl_take isl_map
*map2
)
3807 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3808 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3809 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3813 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3814 __isl_take isl_map
*map2
)
3817 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3818 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3819 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3823 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3824 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3826 struct isl_basic_map
*bmap
;
3828 bset
= isl_basic_set_cow(bset
);
3832 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3833 isl_space_free(bset
->dim
);
3834 bmap
= (struct isl_basic_map
*) bset
;
3836 return isl_basic_map_finalize(bmap
);
3838 isl_basic_set_free(bset
);
3839 isl_space_free(dim
);
3843 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3847 if (bmap
->dim
->n_in
== 0)
3848 return (struct isl_basic_set
*)bmap
;
3849 bmap
= isl_basic_map_cow(bmap
);
3852 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3855 bmap
= isl_basic_map_finalize(bmap
);
3856 return (struct isl_basic_set
*)bmap
;
3858 isl_basic_map_free(bmap
);
3862 /* For a div d = floor(f/m), add the constraints
3865 * -(f-(n-1)) + m d >= 0
3867 * Note that the second constraint is the negation of
3871 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3872 unsigned pos
, isl_int
*div
)
3875 unsigned total
= isl_basic_map_total_dim(bmap
);
3877 i
= isl_basic_map_alloc_inequality(bmap
);
3880 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3881 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3883 j
= isl_basic_map_alloc_inequality(bmap
);
3886 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3887 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3888 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3892 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3893 unsigned pos
, isl_int
*div
)
3895 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3899 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3901 unsigned total
= isl_basic_map_total_dim(bmap
);
3902 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3904 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3908 struct isl_basic_set
*isl_basic_map_underlying_set(
3909 struct isl_basic_map
*bmap
)
3913 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3915 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3916 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3917 return (struct isl_basic_set
*)bmap
;
3918 bmap
= isl_basic_map_cow(bmap
);
3921 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3924 bmap
->extra
-= bmap
->n_div
;
3926 bmap
= isl_basic_map_finalize(bmap
);
3927 return (struct isl_basic_set
*)bmap
;
3932 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3933 __isl_take isl_basic_set
*bset
)
3935 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3938 struct isl_basic_map
*isl_basic_map_overlying_set(
3939 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3941 struct isl_basic_map
*bmap
;
3942 struct isl_ctx
*ctx
;
3949 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3950 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3951 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3953 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3954 isl_basic_map_free(like
);
3955 return (struct isl_basic_map
*)bset
;
3957 bset
= isl_basic_set_cow(bset
);
3960 total
= bset
->dim
->n_out
+ bset
->extra
;
3961 bmap
= (struct isl_basic_map
*)bset
;
3962 isl_space_free(bmap
->dim
);
3963 bmap
->dim
= isl_space_copy(like
->dim
);
3966 bmap
->n_div
= like
->n_div
;
3967 bmap
->extra
+= like
->n_div
;
3971 ltotal
= total
- bmap
->extra
+ like
->extra
;
3974 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3975 bmap
->extra
* (1 + 1 + total
));
3976 if (isl_blk_is_error(bmap
->block2
))
3978 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3982 for (i
= 0; i
< bmap
->extra
; ++i
)
3983 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3984 for (i
= 0; i
< like
->n_div
; ++i
) {
3985 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3986 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3988 bmap
= isl_basic_map_extend_constraints(bmap
,
3989 0, 2 * like
->n_div
);
3990 for (i
= 0; i
< like
->n_div
; ++i
) {
3991 if (isl_int_is_zero(bmap
->div
[i
][0]))
3993 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3997 isl_basic_map_free(like
);
3998 bmap
= isl_basic_map_simplify(bmap
);
3999 bmap
= isl_basic_map_finalize(bmap
);
4002 isl_basic_map_free(like
);
4003 isl_basic_set_free(bset
);
4007 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4008 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4010 return (struct isl_basic_set
*)
4011 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4014 struct isl_set
*isl_set_from_underlying_set(
4015 struct isl_set
*set
, struct isl_basic_set
*like
)
4021 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4023 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4024 isl_basic_set_free(like
);
4027 set
= isl_set_cow(set
);
4030 for (i
= 0; i
< set
->n
; ++i
) {
4031 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4032 isl_basic_set_copy(like
));
4036 isl_space_free(set
->dim
);
4037 set
->dim
= isl_space_copy(like
->dim
);
4040 isl_basic_set_free(like
);
4043 isl_basic_set_free(like
);
4048 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4052 map
= isl_map_cow(map
);
4055 map
->dim
= isl_space_cow(map
->dim
);
4059 for (i
= 1; i
< map
->n
; ++i
)
4060 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4062 for (i
= 0; i
< map
->n
; ++i
) {
4063 map
->p
[i
] = (struct isl_basic_map
*)
4064 isl_basic_map_underlying_set(map
->p
[i
]);
4069 map
->dim
= isl_space_underlying(map
->dim
, 0);
4071 isl_space_free(map
->dim
);
4072 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4076 return (struct isl_set
*)map
;
4082 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4084 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4087 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4088 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4090 bmap
= isl_basic_map_cow(bmap
);
4094 isl_space_free(bmap
->dim
);
4097 bmap
= isl_basic_map_finalize(bmap
);
4101 isl_basic_map_free(bmap
);
4102 isl_space_free(dim
);
4106 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4107 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4109 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4113 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4114 __isl_take isl_space
*dim
)
4118 map
= isl_map_cow(map
);
4122 for (i
= 0; i
< map
->n
; ++i
) {
4123 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4124 isl_space_copy(dim
));
4128 isl_space_free(map
->dim
);
4134 isl_space_free(dim
);
4138 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4139 __isl_take isl_space
*dim
)
4141 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4144 /* Compute the parameter domain of the given basic set.
4146 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4151 if (isl_basic_set_is_params(bset
))
4154 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4155 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4156 space
= isl_basic_set_get_space(bset
);
4157 space
= isl_space_params(space
);
4158 bset
= isl_basic_set_reset_space(bset
, space
);
4162 /* Compute the parameter domain of the given set.
4164 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4169 if (isl_set_is_params(set
))
4172 n
= isl_set_dim(set
, isl_dim_set
);
4173 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4174 space
= isl_set_get_space(set
);
4175 space
= isl_space_params(space
);
4176 set
= isl_set_reset_space(set
, space
);
4180 /* Construct a zero-dimensional set with the given parameter domain.
4182 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4185 space
= isl_set_get_space(set
);
4186 space
= isl_space_set_from_params(space
);
4187 set
= isl_set_reset_space(set
, space
);
4191 /* Compute the parameter domain of the given map.
4193 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4198 n
= isl_map_dim(map
, isl_dim_in
);
4199 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4200 n
= isl_map_dim(map
, isl_dim_out
);
4201 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4202 space
= isl_map_get_space(map
);
4203 space
= isl_space_params(space
);
4204 map
= isl_map_reset_space(map
, space
);
4208 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4211 struct isl_basic_set
*domain
;
4217 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4219 n_in
= isl_basic_map_n_in(bmap
);
4220 n_out
= isl_basic_map_n_out(bmap
);
4221 domain
= isl_basic_set_from_basic_map(bmap
);
4222 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4224 domain
= isl_basic_set_reset_space(domain
, dim
);
4229 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4233 return isl_space_may_be_set(bmap
->dim
);
4236 /* Is this basic map actually a set?
4237 * Users should never call this function. Outside of isl,
4238 * the type should indicate whether something is a set or a map.
4240 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4244 return isl_space_is_set(bmap
->dim
);
4247 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4251 if (isl_basic_map_is_set(bmap
))
4253 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4256 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4257 __isl_take isl_basic_map
*bmap
)
4261 isl_basic_map
*domain
;
4262 int nparam
, n_in
, n_out
;
4265 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4266 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4267 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4269 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4270 domain
= isl_basic_map_universe(dim
);
4272 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4273 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4274 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4276 total
= isl_basic_map_total_dim(bmap
);
4278 for (i
= 0; i
< n_in
; ++i
) {
4279 k
= isl_basic_map_alloc_equality(bmap
);
4282 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4283 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4284 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4287 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4288 return isl_basic_map_finalize(bmap
);
4290 isl_basic_map_free(bmap
);
4294 __isl_give isl_basic_map
*isl_basic_map_range_map(
4295 __isl_take isl_basic_map
*bmap
)
4299 isl_basic_map
*range
;
4300 int nparam
, n_in
, n_out
;
4303 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4304 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4305 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4307 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4308 range
= isl_basic_map_universe(dim
);
4310 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4311 bmap
= isl_basic_map_apply_range(bmap
, range
);
4312 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4314 total
= isl_basic_map_total_dim(bmap
);
4316 for (i
= 0; i
< n_out
; ++i
) {
4317 k
= isl_basic_map_alloc_equality(bmap
);
4320 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4321 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4322 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4325 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4326 return isl_basic_map_finalize(bmap
);
4328 isl_basic_map_free(bmap
);
4332 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4336 return isl_space_may_be_set(map
->dim
);
4339 /* Is this map actually a set?
4340 * Users should never call this function. Outside of isl,
4341 * the type should indicate whether something is a set or a map.
4343 int isl_map_is_set(__isl_keep isl_map
*map
)
4347 return isl_space_is_set(map
->dim
);
4350 struct isl_set
*isl_map_range(struct isl_map
*map
)
4353 struct isl_set
*set
;
4357 if (isl_map_is_set(map
))
4358 return (isl_set
*)map
;
4360 map
= isl_map_cow(map
);
4364 set
= (struct isl_set
*) map
;
4365 set
->dim
= isl_space_range(set
->dim
);
4368 for (i
= 0; i
< map
->n
; ++i
) {
4369 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4373 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4374 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4381 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4384 isl_space
*domain_dim
;
4386 map
= isl_map_cow(map
);
4390 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4391 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4392 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4395 for (i
= 0; i
< map
->n
; ++i
) {
4396 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4400 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4401 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4408 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4411 isl_space
*range_dim
;
4413 map
= isl_map_cow(map
);
4417 range_dim
= isl_space_range(isl_map_get_space(map
));
4418 range_dim
= isl_space_from_range(range_dim
);
4419 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4420 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4423 for (i
= 0; i
< map
->n
; ++i
) {
4424 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4428 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4429 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4436 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4437 __isl_take isl_space
*dim
)
4440 struct isl_map
*map
= NULL
;
4442 set
= isl_set_cow(set
);
4445 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4446 map
= (struct isl_map
*)set
;
4447 for (i
= 0; i
< set
->n
; ++i
) {
4448 map
->p
[i
] = isl_basic_map_from_basic_set(
4449 set
->p
[i
], isl_space_copy(dim
));
4453 isl_space_free(map
->dim
);
4457 isl_space_free(dim
);
4462 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4463 __isl_take isl_basic_set
*bset
)
4465 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4468 __isl_give isl_basic_map
*isl_basic_map_from_range(
4469 __isl_take isl_basic_set
*bset
)
4472 space
= isl_basic_set_get_space(bset
);
4473 space
= isl_space_from_range(space
);
4474 bset
= isl_basic_set_reset_space(bset
, space
);
4475 return (isl_basic_map
*)bset
;
4478 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4481 space
= isl_set_get_space(set
);
4482 space
= isl_space_from_range(space
);
4483 set
= isl_set_reset_space(set
, space
);
4484 return (struct isl_map
*)set
;
4487 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4489 return isl_map_reverse(isl_map_from_range(set
));
4492 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4493 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4495 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4498 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4499 __isl_take isl_set
*range
)
4501 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4504 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4507 struct isl_set
*set
= NULL
;
4511 map
= isl_map_cow(map
);
4514 map
->dim
= isl_space_as_set_space(map
->dim
);
4517 set
= (struct isl_set
*)map
;
4518 for (i
= 0; i
< map
->n
; ++i
) {
4519 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4529 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4532 struct isl_map
*map
;
4537 isl_die(dim
->ctx
, isl_error_internal
,
4538 "negative number of basic maps", goto error
);
4539 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4540 sizeof(struct isl_map
) +
4541 (n
- 1) * sizeof(struct isl_basic_map
*));
4545 map
->ctx
= dim
->ctx
;
4546 isl_ctx_ref(map
->ctx
);
4554 isl_space_free(dim
);
4558 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4559 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4562 struct isl_map
*map
;
4565 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4569 map
= isl_map_alloc_space(dims
, n
, flags
);
4573 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4575 struct isl_basic_map
*bmap
;
4576 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4577 bmap
= isl_basic_map_set_to_empty(bmap
);
4581 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4583 struct isl_basic_set
*bset
;
4584 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4585 bset
= isl_basic_set_set_to_empty(bset
);
4589 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4591 struct isl_basic_map
*bmap
;
4594 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4595 bmap
= isl_basic_map_set_to_empty(bmap
);
4599 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4601 struct isl_basic_map
*bmap
;
4604 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4605 bmap
= isl_basic_map_set_to_empty(bmap
);
4609 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4611 struct isl_basic_set
*bset
;
4614 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4615 bset
= isl_basic_set_set_to_empty(bset
);
4619 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4621 struct isl_basic_map
*bmap
;
4622 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4623 bmap
= isl_basic_map_finalize(bmap
);
4627 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4629 struct isl_basic_set
*bset
;
4630 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4631 bset
= isl_basic_set_finalize(bset
);
4635 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4638 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4639 isl_basic_map
*bmap
;
4641 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4642 for (i
= 0; i
< total
; ++i
) {
4643 int k
= isl_basic_map_alloc_inequality(bmap
);
4646 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4647 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4651 isl_basic_map_free(bmap
);
4655 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4657 return isl_basic_map_nat_universe(dim
);
4660 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4662 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4665 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4667 return isl_map_nat_universe(dim
);
4670 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4671 __isl_keep isl_basic_map
*model
)
4675 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4678 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4682 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4685 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4686 __isl_keep isl_set
*model
)
4690 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4693 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4695 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4698 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4702 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4705 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4709 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4712 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4714 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4717 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4721 return isl_set_empty(isl_space_copy(model
->dim
));
4724 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4726 struct isl_map
*map
;
4729 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4730 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4734 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4736 struct isl_set
*set
;
4739 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4740 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4744 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4748 return isl_set_universe(isl_space_copy(model
->dim
));
4751 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4754 struct isl_map
*dup
;
4758 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4759 for (i
= 0; i
< map
->n
; ++i
)
4760 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4764 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4765 __isl_take isl_basic_map
*bmap
)
4769 if (isl_basic_map_plain_is_empty(bmap
)) {
4770 isl_basic_map_free(bmap
);
4773 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4774 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4775 map
->p
[map
->n
] = bmap
;
4777 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4783 isl_basic_map_free(bmap
);
4787 void isl_map_free(struct isl_map
*map
)
4797 isl_ctx_deref(map
->ctx
);
4798 for (i
= 0; i
< map
->n
; ++i
)
4799 isl_basic_map_free(map
->p
[i
]);
4800 isl_space_free(map
->dim
);
4804 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4805 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4809 base
= isl_map_cow(base
);
4813 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4816 for (i
= 0; i
< base
->n
; ++i
) {
4817 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4818 isl_space_copy(base
->dim
), 0, 0, 0);
4828 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4829 unsigned nparam
, unsigned dim
)
4831 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4835 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4836 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4840 bmap
= isl_basic_map_cow(bmap
);
4841 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4842 j
= isl_basic_map_alloc_equality(bmap
);
4845 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4846 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4847 isl_int_set_si(bmap
->eq
[j
][0], value
);
4848 bmap
= isl_basic_map_simplify(bmap
);
4849 return isl_basic_map_finalize(bmap
);
4851 isl_basic_map_free(bmap
);
4855 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4856 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4860 bmap
= isl_basic_map_cow(bmap
);
4861 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4862 j
= isl_basic_map_alloc_equality(bmap
);
4865 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4866 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4867 isl_int_set(bmap
->eq
[j
][0], value
);
4868 bmap
= isl_basic_map_simplify(bmap
);
4869 return isl_basic_map_finalize(bmap
);
4871 isl_basic_map_free(bmap
);
4875 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4876 enum isl_dim_type type
, unsigned pos
, int value
)
4880 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4881 return isl_basic_map_fix_pos_si(bmap
,
4882 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4884 isl_basic_map_free(bmap
);
4888 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4889 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4893 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4894 return isl_basic_map_fix_pos(bmap
,
4895 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4897 isl_basic_map_free(bmap
);
4901 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4902 enum isl_dim_type type
, unsigned pos
, int value
)
4904 return (struct isl_basic_set
*)
4905 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4909 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4910 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4912 return (struct isl_basic_set
*)
4913 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4917 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4918 unsigned input
, int value
)
4920 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4923 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4924 unsigned dim
, int value
)
4926 return (struct isl_basic_set
*)
4927 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4928 isl_dim_set
, dim
, value
);
4931 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
4933 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
4940 isl_basic_map_free(map
->p
[i
]);
4941 if (i
!= map
->n
- 1) {
4942 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4943 map
->p
[i
] = map
->p
[map
->n
- 1];
4950 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4951 enum isl_dim_type type
, unsigned pos
, int value
)
4955 map
= isl_map_cow(map
);
4959 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4960 for (i
= map
->n
- 1; i
>= 0; --i
) {
4961 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4962 if (remove_if_empty(map
, i
) < 0)
4965 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4972 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4973 enum isl_dim_type type
, unsigned pos
, int value
)
4975 return (struct isl_set
*)
4976 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4979 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4980 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4984 map
= isl_map_cow(map
);
4988 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4989 for (i
= 0; i
< map
->n
; ++i
) {
4990 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4994 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5001 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5002 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5004 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5007 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5008 unsigned input
, int value
)
5010 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5013 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5015 return (struct isl_set
*)
5016 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5019 static __isl_give isl_basic_map
*basic_map_bound_si(
5020 __isl_take isl_basic_map
*bmap
,
5021 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5027 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5028 pos
+= isl_basic_map_offset(bmap
, type
);
5029 bmap
= isl_basic_map_cow(bmap
);
5030 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5031 j
= isl_basic_map_alloc_inequality(bmap
);
5034 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5036 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5037 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5039 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5040 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5042 bmap
= isl_basic_map_simplify(bmap
);
5043 return isl_basic_map_finalize(bmap
);
5045 isl_basic_map_free(bmap
);
5049 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5050 __isl_take isl_basic_map
*bmap
,
5051 enum isl_dim_type type
, unsigned pos
, int value
)
5053 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5056 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5057 unsigned dim
, isl_int value
)
5061 bset
= isl_basic_set_cow(bset
);
5062 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5063 j
= isl_basic_set_alloc_inequality(bset
);
5066 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5067 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5068 isl_int_neg(bset
->ineq
[j
][0], value
);
5069 bset
= isl_basic_set_simplify(bset
);
5070 return isl_basic_set_finalize(bset
);
5072 isl_basic_set_free(bset
);
5076 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5077 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5081 map
= isl_map_cow(map
);
5085 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5086 for (i
= 0; i
< map
->n
; ++i
) {
5087 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5088 type
, pos
, value
, upper
);
5092 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5099 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5100 enum isl_dim_type type
, unsigned pos
, int value
)
5102 return map_bound_si(map
, type
, pos
, value
, 0);
5105 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5106 enum isl_dim_type type
, unsigned pos
, int value
)
5108 return map_bound_si(map
, type
, pos
, value
, 1);
5111 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5112 enum isl_dim_type type
, unsigned pos
, int value
)
5114 return (struct isl_set
*)
5115 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5118 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5119 enum isl_dim_type type
, unsigned pos
, int value
)
5121 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5124 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5129 set
= isl_set_cow(set
);
5133 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5134 for (i
= 0; i
< set
->n
; ++i
) {
5135 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5145 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5149 map
= isl_map_cow(map
);
5153 map
->dim
= isl_space_reverse(map
->dim
);
5156 for (i
= 0; i
< map
->n
; ++i
) {
5157 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5161 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5168 static struct isl_map
*isl_basic_map_partial_lexopt(
5169 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5170 struct isl_set
**empty
, int max
)
5174 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5175 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5177 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5179 isl_basic_map_free(bmap
);
5180 isl_basic_set_free(dom
);
5186 struct isl_map
*isl_basic_map_partial_lexmax(
5187 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5188 struct isl_set
**empty
)
5190 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5193 struct isl_map
*isl_basic_map_partial_lexmin(
5194 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5195 struct isl_set
**empty
)
5197 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5200 struct isl_set
*isl_basic_set_partial_lexmin(
5201 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5202 struct isl_set
**empty
)
5204 return (struct isl_set
*)
5205 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5209 struct isl_set
*isl_basic_set_partial_lexmax(
5210 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5211 struct isl_set
**empty
)
5213 return (struct isl_set
*)
5214 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5218 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5219 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5220 __isl_give isl_set
**empty
)
5222 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5225 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5226 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5227 __isl_give isl_set
**empty
)
5229 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5232 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5233 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5234 __isl_give isl_set
**empty
)
5236 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5239 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5240 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5241 __isl_give isl_set
**empty
)
5243 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5246 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5247 __isl_take isl_basic_map
*bmap
, int max
)
5249 isl_basic_set
*dom
= NULL
;
5250 isl_space
*dom_space
;
5254 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5255 dom
= isl_basic_set_universe(dom_space
);
5256 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5258 isl_basic_map_free(bmap
);
5262 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5263 __isl_take isl_basic_map
*bmap
)
5265 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5268 /* Given a basic map "bmap", compute the lexicographically minimal
5269 * (or maximal) image element for each domain element in dom.
5270 * Set *empty to those elements in dom that do not have an image element.
5272 * We first make sure the basic sets in dom are disjoint and then
5273 * simply collect the results over each of the basic sets separately.
5274 * We could probably improve the efficiency a bit by moving the union
5275 * domain down into the parametric integer programming.
5277 static __isl_give isl_map
*basic_map_partial_lexopt(
5278 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5279 __isl_give isl_set
**empty
, int max
)
5282 struct isl_map
*res
;
5284 dom
= isl_set_make_disjoint(dom
);
5288 if (isl_set_plain_is_empty(dom
)) {
5289 res
= isl_map_empty_like_basic_map(bmap
);
5290 *empty
= isl_set_empty_like(dom
);
5292 isl_basic_map_free(bmap
);
5296 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5297 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5299 for (i
= 1; i
< dom
->n
; ++i
) {
5300 struct isl_map
*res_i
;
5301 struct isl_set
*empty_i
;
5303 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5304 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5306 res
= isl_map_union_disjoint(res
, res_i
);
5307 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5311 isl_basic_map_free(bmap
);
5316 isl_basic_map_free(bmap
);
5320 /* Given a map "map", compute the lexicographically minimal
5321 * (or maximal) image element for each domain element in dom.
5322 * Set *empty to those elements in dom that do not have an image element.
5324 * We first compute the lexicographically minimal or maximal element
5325 * in the first basic map. This results in a partial solution "res"
5326 * and a subset "todo" of dom that still need to be handled.
5327 * We then consider each of the remaining maps in "map" and successively
5328 * improve both "res" and "todo".
5330 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5331 * Assume we are computing the lexicographical maximum.
5332 * We first compute the lexicographically maximal element in basic map i.
5333 * This results in a partial solution res_i and a subset todo_i.
5334 * Then we combine these results with those obtain for the first k basic maps
5335 * to obtain a result that is valid for the first k+1 basic maps.
5336 * In particular, the set where there is no solution is the set where
5337 * there is no solution for the first k basic maps and also no solution
5338 * for the ith basic map, i.e.,
5340 * todo^i = todo^k * todo_i
5342 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5343 * solutions, arbitrarily breaking ties in favor of res^k.
5344 * That is, when res^k(a) >= res_i(a), we pick res^k and
5345 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5346 * the lexicographic order.)
5347 * In practice, we compute
5349 * res^k * (res_i . "<=")
5353 * res_i * (res^k . "<")
5355 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5356 * where only one of res^k and res_i provides a solution and we simply pick
5363 * Note that we only compute these intersections when dom(res^k) intersects
5364 * dom(res_i). Otherwise, the only effect of these intersections is to
5365 * potentially break up res^k and res_i into smaller pieces.
5366 * We want to avoid such splintering as much as possible.
5367 * In fact, an earlier implementation of this function would look for
5368 * better results in the domain of res^k and for extra results in todo^k,
5369 * but this would always result in a splintering according to todo^k,
5370 * even when the domain of basic map i is disjoint from the domains of
5371 * the previous basic maps.
5373 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5374 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5375 __isl_give isl_set
**empty
, int max
)
5378 struct isl_map
*res
;
5379 struct isl_set
*todo
;
5384 if (isl_map_plain_is_empty(map
)) {
5392 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5393 isl_set_copy(dom
), &todo
, max
);
5395 for (i
= 1; i
< map
->n
; ++i
) {
5399 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5401 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5402 isl_set_copy(dom
), &todo_i
, max
);
5405 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5406 le
= isl_map_lex_le(dim
);
5408 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5409 le
= isl_map_lex_ge(dim
);
5411 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5412 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5413 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5414 le
= isl_map_intersect(le
, isl_map_copy(res
));
5416 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5417 res
= isl_map_intersect_domain(res
,
5418 isl_set_copy(todo_i
));
5419 res_i
= isl_map_intersect_domain(res_i
,
5420 isl_set_copy(todo
));
5423 res
= isl_map_union_disjoint(res
, res_i
);
5424 res
= isl_map_union_disjoint(res
, lt
);
5425 res
= isl_map_union_disjoint(res
, le
);
5427 todo
= isl_set_intersect(todo
, todo_i
);
5447 /* Given a map "map", compute the lexicographically minimal
5448 * (or maximal) image element for each domain element in dom.
5449 * Set *empty to those elements in dom that do not have an image element.
5451 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5453 static __isl_give isl_map
*isl_map_partial_lexopt(
5454 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5455 __isl_give isl_set
**empty
, int max
)
5459 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5460 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5461 if (!isl_space_has_named_params(map
->dim
) ||
5462 !isl_space_has_named_params(dom
->dim
))
5463 isl_die(map
->ctx
, isl_error_invalid
,
5464 "unaligned unnamed parameters", goto error
);
5465 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5466 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5467 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5476 __isl_give isl_map
*isl_map_partial_lexmax(
5477 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5478 __isl_give isl_set
**empty
)
5480 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5483 __isl_give isl_map
*isl_map_partial_lexmin(
5484 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5485 __isl_give isl_set
**empty
)
5487 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5490 __isl_give isl_set
*isl_set_partial_lexmin(
5491 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5492 __isl_give isl_set
**empty
)
5494 return (struct isl_set
*)
5495 isl_map_partial_lexmin((struct isl_map
*)set
,
5499 __isl_give isl_set
*isl_set_partial_lexmax(
5500 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5501 __isl_give isl_set
**empty
)
5503 return (struct isl_set
*)
5504 isl_map_partial_lexmax((struct isl_map
*)set
,
5508 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5510 struct isl_basic_set
*dom
= NULL
;
5515 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5516 dom
= isl_basic_set_universe(dom_dim
);
5517 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5519 isl_basic_map_free(bmap
);
5523 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5525 return isl_basic_map_lexopt(bmap
, 0);
5528 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5530 return isl_basic_map_lexopt(bmap
, 1);
5533 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5535 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5538 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5540 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5543 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5545 struct isl_set
*dom
= NULL
;
5550 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5551 dom
= isl_set_universe(dom_dim
);
5552 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5558 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5560 return isl_map_lexopt(map
, 0);
5563 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5565 return isl_map_lexopt(map
, 1);
5568 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5570 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5573 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5575 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5578 /* Extract the first and only affine expression from list
5579 * and then add it to *pwaff with the given dom.
5580 * This domain is known to be disjoint from other domains
5581 * because of the way isl_basic_map_foreach_lexmax works.
5583 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5584 __isl_take isl_aff_list
*list
, void *user
)
5586 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5588 isl_pw_aff
**pwaff
= user
;
5589 isl_pw_aff
*pwaff_i
;
5591 if (isl_aff_list_n_aff(list
) != 1)
5592 isl_die(ctx
, isl_error_internal
,
5593 "expecting single element list", goto error
);
5595 aff
= isl_aff_list_get_aff(list
, 0);
5596 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5598 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5600 isl_aff_list_free(list
);
5604 isl_basic_set_free(dom
);
5605 isl_aff_list_free(list
);
5609 /* Given a basic map with one output dimension, compute the minimum or
5610 * maximum of that dimension as an isl_pw_aff.
5612 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5613 * call update_dim_opt on each leaf of the result.
5615 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5618 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5622 dim
= isl_space_from_domain(isl_space_domain(dim
));
5623 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5624 pwaff
= isl_pw_aff_empty(dim
);
5626 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5628 return isl_pw_aff_free(pwaff
);
5633 /* Compute the minimum or maximum of the given output dimension
5634 * as a function of the parameters and the input dimensions,
5635 * but independently of the other output dimensions.
5637 * We first project out the other output dimension and then compute
5638 * the "lexicographic" maximum in each basic map, combining the results
5639 * using isl_pw_aff_union_max.
5641 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5648 n_out
= isl_map_dim(map
, isl_dim_out
);
5649 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5650 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5655 isl_space
*dim
= isl_map_get_space(map
);
5656 dim
= isl_space_domain(isl_space_from_range(dim
));
5658 return isl_pw_aff_empty(dim
);
5661 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5662 for (i
= 1; i
< map
->n
; ++i
) {
5663 isl_pw_aff
*pwaff_i
;
5665 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5666 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5674 /* Compute the maximum of the given output dimension as a function of the
5675 * parameters and input dimensions, but independently of
5676 * the other output dimensions.
5678 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5680 return map_dim_opt(map
, pos
, 1);
5683 /* Compute the minimum or maximum of the given set dimension
5684 * as a function of the parameters,
5685 * but independently of the other set dimensions.
5687 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5690 return map_dim_opt(set
, pos
, max
);
5693 /* Compute the maximum of the given set dimension as a function of the
5694 * parameters, but independently of the other set dimensions.
5696 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5698 return set_dim_opt(set
, pos
, 1);
5701 /* Compute the minimum of the given set dimension as a function of the
5702 * parameters, but independently of the other set dimensions.
5704 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5706 return set_dim_opt(set
, pos
, 0);
5709 /* Apply a preimage specified by "mat" on the parameters of "bset".
5710 * bset is assumed to have only parameters and divs.
5712 static struct isl_basic_set
*basic_set_parameter_preimage(
5713 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5720 bset
->dim
= isl_space_cow(bset
->dim
);
5724 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5726 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5728 bset
->dim
->nparam
= 0;
5729 bset
->dim
->n_out
= nparam
;
5730 bset
= isl_basic_set_preimage(bset
, mat
);
5732 bset
->dim
->nparam
= bset
->dim
->n_out
;
5733 bset
->dim
->n_out
= 0;
5738 isl_basic_set_free(bset
);
5742 /* Apply a preimage specified by "mat" on the parameters of "set".
5743 * set is assumed to have only parameters and divs.
5745 static struct isl_set
*set_parameter_preimage(
5746 struct isl_set
*set
, struct isl_mat
*mat
)
5748 isl_space
*dim
= NULL
;
5754 dim
= isl_space_copy(set
->dim
);
5755 dim
= isl_space_cow(dim
);
5759 nparam
= isl_set_dim(set
, isl_dim_param
);
5761 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5764 dim
->n_out
= nparam
;
5765 isl_set_reset_space(set
, dim
);
5766 set
= isl_set_preimage(set
, mat
);
5769 dim
= isl_space_copy(set
->dim
);
5770 dim
= isl_space_cow(dim
);
5773 dim
->nparam
= dim
->n_out
;
5775 isl_set_reset_space(set
, dim
);
5778 isl_space_free(dim
);
5785 /* Intersect the basic set "bset" with the affine space specified by the
5786 * equalities in "eq".
5788 static struct isl_basic_set
*basic_set_append_equalities(
5789 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5797 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5802 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5803 for (i
= 0; i
< eq
->n_row
; ++i
) {
5804 k
= isl_basic_set_alloc_equality(bset
);
5807 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5808 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5812 bset
= isl_basic_set_gauss(bset
, NULL
);
5813 bset
= isl_basic_set_finalize(bset
);
5818 isl_basic_set_free(bset
);
5822 /* Intersect the set "set" with the affine space specified by the
5823 * equalities in "eq".
5825 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5833 for (i
= 0; i
< set
->n
; ++i
) {
5834 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5847 /* Project the given basic set onto its parameter domain, possibly introducing
5848 * new, explicit, existential variables in the constraints.
5849 * The input has parameters and (possibly implicit) existential variables.
5850 * The output has the same parameters, but only
5851 * explicit existentially quantified variables.
5853 * The actual projection is performed by pip, but pip doesn't seem
5854 * to like equalities very much, so we first remove the equalities
5855 * among the parameters by performing a variable compression on
5856 * the parameters. Afterward, an inverse transformation is performed
5857 * and the equalities among the parameters are inserted back in.
5859 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5863 struct isl_mat
*T
, *T2
;
5864 struct isl_set
*set
;
5865 unsigned nparam
, n_div
;
5867 bset
= isl_basic_set_cow(bset
);
5871 if (bset
->n_eq
== 0)
5872 return isl_basic_set_lexmin(bset
);
5874 isl_basic_set_gauss(bset
, NULL
);
5876 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5877 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5879 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5880 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5883 if (i
== bset
->n_eq
)
5884 return isl_basic_set_lexmin(bset
);
5886 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5888 eq
= isl_mat_cow(eq
);
5889 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5890 if (T
&& T
->n_col
== 0) {
5894 bset
= isl_basic_set_set_to_empty(bset
);
5895 return isl_set_from_basic_set(bset
);
5897 bset
= basic_set_parameter_preimage(bset
, T
);
5899 set
= isl_basic_set_lexmin(bset
);
5900 set
= set_parameter_preimage(set
, T2
);
5901 set
= set_append_equalities(set
, eq
);
5905 /* Compute an explicit representation for all the existentially
5906 * quantified variables.
5907 * The input and output dimensions are first turned into parameters.
5908 * compute_divs then returns a map with the same parameters and
5909 * no input or output dimensions and the dimension specification
5910 * is reset to that of the input.
5912 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5914 struct isl_basic_set
*bset
;
5915 struct isl_set
*set
;
5916 struct isl_map
*map
;
5917 isl_space
*dim
, *orig_dim
= NULL
;
5922 bmap
= isl_basic_map_cow(bmap
);
5926 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5927 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5928 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5929 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5933 orig_dim
= bmap
->dim
;
5935 bset
= (struct isl_basic_set
*)bmap
;
5937 set
= parameter_compute_divs(bset
);
5938 map
= (struct isl_map
*)set
;
5939 map
= isl_map_reset_space(map
, orig_dim
);
5943 isl_basic_map_free(bmap
);
5947 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5955 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5956 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5957 if (isl_int_is_zero(bmap
->div
[i
][0]))
5959 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5965 static int map_divs_known(__isl_keep isl_map
*map
)
5972 for (i
= 0; i
< map
->n
; ++i
) {
5973 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5981 /* If bmap contains any unknown divs, then compute explicit
5982 * expressions for them. However, this computation may be
5983 * quite expensive, so first try to remove divs that aren't
5986 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5989 struct isl_map
*map
;
5991 known
= isl_basic_map_divs_known(bmap
);
5995 return isl_map_from_basic_map(bmap
);
5997 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5999 known
= isl_basic_map_divs_known(bmap
);
6003 return isl_map_from_basic_map(bmap
);
6005 map
= compute_divs(bmap
);
6008 isl_basic_map_free(bmap
);
6012 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6016 struct isl_map
*res
;
6023 known
= map_divs_known(map
);
6031 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6032 for (i
= 1 ; i
< map
->n
; ++i
) {
6034 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6035 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6036 res
= isl_map_union_disjoint(res
, r2
);
6038 res
= isl_map_union(res
, r2
);
6045 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6047 return (struct isl_set
*)
6048 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6051 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6053 return (struct isl_set
*)
6054 isl_map_compute_divs((struct isl_map
*)set
);
6057 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6060 struct isl_set
*set
;
6065 map
= isl_map_cow(map
);
6069 set
= (struct isl_set
*)map
;
6070 set
->dim
= isl_space_domain(set
->dim
);
6073 for (i
= 0; i
< map
->n
; ++i
) {
6074 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6078 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6079 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6086 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6087 __isl_take isl_map
*map2
)
6091 struct isl_map
*map
= NULL
;
6105 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6107 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6108 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6109 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6111 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6112 map1
->n
+ map2
->n
, flags
);
6115 for (i
= 0; i
< map1
->n
; ++i
) {
6116 map
= isl_map_add_basic_map(map
,
6117 isl_basic_map_copy(map1
->p
[i
]));
6121 for (i
= 0; i
< map2
->n
; ++i
) {
6122 map
= isl_map_add_basic_map(map
,
6123 isl_basic_map_copy(map2
->p
[i
]));
6137 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6138 __isl_take isl_map
*map2
)
6140 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6143 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6145 map1
= isl_map_union_disjoint(map1
, map2
);
6149 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6153 struct isl_set
*isl_set_union_disjoint(
6154 struct isl_set
*set1
, struct isl_set
*set2
)
6156 return (struct isl_set
*)
6157 isl_map_union_disjoint(
6158 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6161 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6163 return (struct isl_set
*)
6164 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6167 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6168 __isl_take isl_set
*set
)
6171 struct isl_map
*result
;
6177 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6178 isl_die(set
->ctx
, isl_error_invalid
,
6179 "parameters don't match", goto error
);
6181 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6182 !isl_map_compatible_range(map
, set
))
6183 isl_die(set
->ctx
, isl_error_invalid
,
6184 "incompatible spaces", goto error
);
6186 if (isl_set_plain_is_universe(set
)) {
6191 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6192 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6193 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6195 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6196 map
->n
* set
->n
, flags
);
6199 for (i
= 0; i
< map
->n
; ++i
)
6200 for (j
= 0; j
< set
->n
; ++j
) {
6201 result
= isl_map_add_basic_map(result
,
6202 isl_basic_map_intersect_range(
6203 isl_basic_map_copy(map
->p
[i
]),
6204 isl_basic_set_copy(set
->p
[j
])));
6217 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6218 __isl_take isl_set
*set
)
6220 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6223 struct isl_map
*isl_map_intersect_domain(
6224 struct isl_map
*map
, struct isl_set
*set
)
6226 return isl_map_reverse(
6227 isl_map_intersect_range(isl_map_reverse(map
), set
));
6230 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6231 __isl_take isl_map
*map2
)
6235 map1
= isl_map_reverse(map1
);
6236 map1
= isl_map_apply_range(map1
, map2
);
6237 return isl_map_reverse(map1
);
6244 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6245 __isl_take isl_map
*map2
)
6247 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6250 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6251 __isl_take isl_map
*map2
)
6253 isl_space
*dim_result
;
6254 struct isl_map
*result
;
6260 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6261 isl_space_copy(map2
->dim
));
6263 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6266 for (i
= 0; i
< map1
->n
; ++i
)
6267 for (j
= 0; j
< map2
->n
; ++j
) {
6268 result
= isl_map_add_basic_map(result
,
6269 isl_basic_map_apply_range(
6270 isl_basic_map_copy(map1
->p
[i
]),
6271 isl_basic_map_copy(map2
->p
[j
])));
6277 if (result
&& result
->n
<= 1)
6278 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6286 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6287 __isl_take isl_map
*map2
)
6289 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6293 * returns range - domain
6295 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6297 isl_space
*dims
, *target_dim
;
6298 struct isl_basic_set
*bset
;
6305 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6306 bmap
->dim
, isl_dim_out
),
6308 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6309 dim
= isl_basic_map_n_in(bmap
);
6310 nparam
= isl_basic_map_n_param(bmap
);
6311 bset
= isl_basic_set_from_basic_map(bmap
);
6312 bset
= isl_basic_set_cow(bset
);
6313 dims
= isl_basic_set_get_space(bset
);
6314 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6315 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6316 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6317 for (i
= 0; i
< dim
; ++i
) {
6318 int j
= isl_basic_map_alloc_equality(
6319 (struct isl_basic_map
*)bset
);
6322 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6323 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6324 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6325 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6327 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6328 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6331 isl_basic_map_free(bmap
);
6336 * returns range - domain
6338 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6342 struct isl_set
*result
;
6347 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6348 map
->dim
, isl_dim_out
),
6350 dim
= isl_map_get_space(map
);
6351 dim
= isl_space_domain(dim
);
6352 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6355 for (i
= 0; i
< map
->n
; ++i
)
6356 result
= isl_set_add_basic_set(result
,
6357 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6366 * returns [domain -> range] -> range - domain
6368 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6369 __isl_take isl_basic_map
*bmap
)
6373 isl_basic_map
*domain
;
6377 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6378 isl_die(bmap
->ctx
, isl_error_invalid
,
6379 "domain and range don't match", goto error
);
6381 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6382 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6384 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6385 domain
= isl_basic_map_universe(dim
);
6387 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6388 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6389 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6391 total
= isl_basic_map_total_dim(bmap
);
6393 for (i
= 0; i
< n
; ++i
) {
6394 k
= isl_basic_map_alloc_equality(bmap
);
6397 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6398 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6399 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6400 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6403 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6404 return isl_basic_map_finalize(bmap
);
6406 isl_basic_map_free(bmap
);
6411 * returns [domain -> range] -> range - domain
6413 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6416 isl_space
*domain_dim
;
6421 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6422 isl_die(map
->ctx
, isl_error_invalid
,
6423 "domain and range don't match", goto error
);
6425 map
= isl_map_cow(map
);
6429 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6430 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6431 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6434 for (i
= 0; i
< map
->n
; ++i
) {
6435 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6439 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6446 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6448 struct isl_basic_map
*bmap
;
6456 nparam
= dims
->nparam
;
6458 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6462 for (i
= 0; i
< dim
; ++i
) {
6463 int j
= isl_basic_map_alloc_equality(bmap
);
6466 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6467 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6468 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6470 return isl_basic_map_finalize(bmap
);
6472 isl_basic_map_free(bmap
);
6476 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6480 if (dim
->n_in
!= dim
->n_out
)
6481 isl_die(dim
->ctx
, isl_error_invalid
,
6482 "number of input and output dimensions needs to be "
6483 "the same", goto error
);
6484 return basic_map_identity(dim
);
6486 isl_space_free(dim
);
6490 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6492 if (!model
|| !model
->dim
)
6494 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6497 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6499 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6502 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6504 if (!model
|| !model
->dim
)
6506 return isl_map_identity(isl_space_copy(model
->dim
));
6509 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6511 if (!model
|| !model
->dim
)
6513 return isl_map_identity(isl_space_copy(model
->dim
));
6516 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6518 isl_space
*dim
= isl_set_get_space(set
);
6520 id
= isl_map_identity(isl_space_map_from_set(dim
));
6521 return isl_map_intersect_range(id
, set
);
6524 /* Construct a basic set with all set dimensions having only non-negative
6527 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6528 __isl_take isl_space
*space
)
6533 struct isl_basic_set
*bset
;
6537 nparam
= space
->nparam
;
6539 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6542 for (i
= 0; i
< dim
; ++i
) {
6543 int k
= isl_basic_set_alloc_inequality(bset
);
6546 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6547 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6551 isl_basic_set_free(bset
);
6555 /* Construct the half-space x_pos >= 0.
6557 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6561 isl_basic_set
*nonneg
;
6563 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6564 k
= isl_basic_set_alloc_inequality(nonneg
);
6567 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6568 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6570 return isl_basic_set_finalize(nonneg
);
6572 isl_basic_set_free(nonneg
);
6576 /* Construct the half-space x_pos <= -1.
6578 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6583 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6584 k
= isl_basic_set_alloc_inequality(neg
);
6587 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6588 isl_int_set_si(neg
->ineq
[k
][0], -1);
6589 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6591 return isl_basic_set_finalize(neg
);
6593 isl_basic_set_free(neg
);
6597 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6598 enum isl_dim_type type
, unsigned first
, unsigned n
)
6601 isl_basic_set
*nonneg
;
6609 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6611 for (i
= 0; i
< n
; ++i
) {
6612 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6613 pos(set
->dim
, type
) + first
+ i
);
6614 neg
= neg_halfspace(isl_set_get_space(set
),
6615 pos(set
->dim
, type
) + first
+ i
);
6617 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6626 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6627 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6634 if (isl_set_plain_is_empty(set
)) {
6639 return fn(set
, signs
, user
);
6642 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6644 half
= isl_set_intersect(half
, isl_set_copy(set
));
6645 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6649 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6651 half
= isl_set_intersect(half
, set
);
6652 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6658 /* Call "fn" on the intersections of "set" with each of the orthants
6659 * (except for obviously empty intersections). The orthant is identified
6660 * by the signs array, with each entry having value 1 or -1 according
6661 * to the sign of the corresponding variable.
6663 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6664 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6674 if (isl_set_plain_is_empty(set
))
6677 nparam
= isl_set_dim(set
, isl_dim_param
);
6678 nvar
= isl_set_dim(set
, isl_dim_set
);
6680 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6682 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6690 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6692 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6695 int isl_basic_map_is_subset(
6696 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6699 struct isl_map
*map1
;
6700 struct isl_map
*map2
;
6702 if (!bmap1
|| !bmap2
)
6705 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6706 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6708 is_subset
= isl_map_is_subset(map1
, map2
);
6716 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
6717 __isl_keep isl_basic_set
*bset2
)
6719 return isl_basic_map_is_subset(bset1
, bset2
);
6722 int isl_basic_map_is_equal(
6723 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6727 if (!bmap1
|| !bmap2
)
6729 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6732 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6736 int isl_basic_set_is_equal(
6737 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6739 return isl_basic_map_is_equal(
6740 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6743 int isl_map_is_empty(struct isl_map
*map
)
6750 for (i
= 0; i
< map
->n
; ++i
) {
6751 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6760 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6762 return map
? map
->n
== 0 : -1;
6765 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6767 return isl_map_plain_is_empty(map
);
6770 int isl_set_plain_is_empty(struct isl_set
*set
)
6772 return set
? set
->n
== 0 : -1;
6775 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6777 return isl_set_plain_is_empty(set
);
6780 int isl_set_is_empty(struct isl_set
*set
)
6782 return isl_map_is_empty((struct isl_map
*)set
);
6785 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6790 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6793 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6798 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6801 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6807 is_subset
= isl_map_is_subset(map1
, map2
);
6810 is_subset
= isl_map_is_subset(map2
, map1
);
6814 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6816 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6819 int isl_basic_map_is_strict_subset(
6820 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6824 if (!bmap1
|| !bmap2
)
6826 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6829 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6830 if (is_subset
== -1)
6835 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6841 is_subset
= isl_map_is_subset(map1
, map2
);
6844 is_subset
= isl_map_is_subset(map2
, map1
);
6845 if (is_subset
== -1)
6850 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6852 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6855 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6859 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6862 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6866 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6869 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6876 for (i
= 0; i
< map
->n
; ++i
) {
6877 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6885 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6887 return isl_map_plain_is_universe((isl_map
*) set
);
6890 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6892 return isl_set_plain_is_universe(set
);
6895 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6897 struct isl_basic_set
*bset
= NULL
;
6898 struct isl_vec
*sample
= NULL
;
6905 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6908 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6909 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6910 copy
= isl_basic_map_remove_redundancies(copy
);
6911 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6912 isl_basic_map_free(copy
);
6916 total
= 1 + isl_basic_map_total_dim(bmap
);
6917 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6918 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6924 isl_vec_free(bmap
->sample
);
6925 bmap
->sample
= NULL
;
6926 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6929 sample
= isl_basic_set_sample_vec(bset
);
6932 empty
= sample
->size
== 0;
6933 isl_vec_free(bmap
->sample
);
6934 bmap
->sample
= sample
;
6936 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6941 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6945 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6948 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6950 return isl_basic_map_plain_is_empty(bmap
);
6953 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6957 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6960 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6962 return isl_basic_set_plain_is_empty(bset
);
6965 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6967 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6970 struct isl_map
*isl_basic_map_union(
6971 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6973 struct isl_map
*map
;
6974 if (!bmap1
|| !bmap2
)
6977 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6979 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6982 map
= isl_map_add_basic_map(map
, bmap1
);
6983 map
= isl_map_add_basic_map(map
, bmap2
);
6986 isl_basic_map_free(bmap1
);
6987 isl_basic_map_free(bmap2
);
6991 struct isl_set
*isl_basic_set_union(
6992 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6994 return (struct isl_set
*)isl_basic_map_union(
6995 (struct isl_basic_map
*)bset1
,
6996 (struct isl_basic_map
*)bset2
);
6999 /* Order divs such that any div only depends on previous divs */
7000 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7008 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7010 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7012 if (isl_int_is_zero(bmap
->div
[i
][0]))
7014 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7018 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7024 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7026 return (struct isl_basic_set
*)
7027 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7030 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7037 for (i
= 0; i
< map
->n
; ++i
) {
7038 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7049 /* Apply the expansion computed by isl_merge_divs.
7050 * The expansion itself is given by "exp" while the resulting
7051 * list of divs is given by "div".
7053 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7054 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7059 bset
= isl_basic_set_cow(bset
);
7063 if (div
->n_row
< bset
->n_div
)
7064 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7065 "not an expansion", goto error
);
7067 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7068 div
->n_row
- bset
->n_div
, 0,
7069 2 * (div
->n_row
- bset
->n_div
));
7071 n_div
= bset
->n_div
;
7072 for (i
= n_div
; i
< div
->n_row
; ++i
)
7073 if (isl_basic_set_alloc_div(bset
) < 0)
7077 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7078 if (j
>= 0 && exp
[j
] == i
) {
7080 isl_basic_map_swap_div(bset
, i
, j
);
7083 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7084 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7092 isl_basic_set_free(bset
);
7097 /* Look for a div in dst that corresponds to the div "div" in src.
7098 * The divs before "div" in src and dst are assumed to be the same.
7100 * Returns -1 if no corresponding div was found and the position
7101 * of the corresponding div in dst otherwise.
7103 static int find_div(struct isl_basic_map
*dst
,
7104 struct isl_basic_map
*src
, unsigned div
)
7108 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7110 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7111 for (i
= div
; i
< dst
->n_div
; ++i
)
7112 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7113 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7114 dst
->n_div
- div
) == -1)
7119 struct isl_basic_map
*isl_basic_map_align_divs(
7120 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7123 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7128 if (src
->n_div
== 0)
7131 for (i
= 0; i
< src
->n_div
; ++i
)
7132 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7134 src
= isl_basic_map_order_divs(src
);
7135 dst
= isl_basic_map_cow(dst
);
7136 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7137 src
->n_div
, 0, 2 * src
->n_div
);
7140 for (i
= 0; i
< src
->n_div
; ++i
) {
7141 int j
= find_div(dst
, src
, i
);
7143 j
= isl_basic_map_alloc_div(dst
);
7146 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7147 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7148 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7152 isl_basic_map_swap_div(dst
, i
, j
);
7156 isl_basic_map_free(dst
);
7160 struct isl_basic_set
*isl_basic_set_align_divs(
7161 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7163 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7164 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7167 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7175 map
= isl_map_compute_divs(map
);
7176 map
= isl_map_cow(map
);
7180 for (i
= 1; i
< map
->n
; ++i
)
7181 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7182 for (i
= 1; i
< map
->n
; ++i
)
7183 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7185 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7189 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7191 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7194 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7195 __isl_take isl_map
*map
)
7199 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7200 map
= isl_map_intersect_domain(map
, set
);
7201 set
= isl_map_range(map
);
7209 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7210 __isl_take isl_map
*map
)
7212 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7215 /* There is no need to cow as removing empty parts doesn't change
7216 * the meaning of the set.
7218 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7225 for (i
= map
->n
- 1; i
>= 0; --i
)
7226 remove_if_empty(map
, i
);
7231 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7233 return (struct isl_set
*)
7234 isl_map_remove_empty_parts((struct isl_map
*)set
);
7237 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7239 struct isl_basic_map
*bmap
;
7240 if (!map
|| map
->n
== 0)
7242 bmap
= map
->p
[map
->n
-1];
7243 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7244 return isl_basic_map_copy(bmap
);
7247 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7249 return (struct isl_basic_set
*)
7250 isl_map_copy_basic_map((struct isl_map
*)set
);
7253 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7254 __isl_keep isl_basic_map
*bmap
)
7260 for (i
= map
->n
-1; i
>= 0; --i
) {
7261 if (map
->p
[i
] != bmap
)
7263 map
= isl_map_cow(map
);
7266 isl_basic_map_free(map
->p
[i
]);
7267 if (i
!= map
->n
-1) {
7268 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7269 map
->p
[i
] = map
->p
[map
->n
-1];
7280 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7281 struct isl_basic_set
*bset
)
7283 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7284 (struct isl_basic_map
*)bset
);
7287 /* Given two basic sets bset1 and bset2, compute the maximal difference
7288 * between the values of dimension pos in bset1 and those in bset2
7289 * for any common value of the parameters and dimensions preceding pos.
7291 static enum isl_lp_result
basic_set_maximal_difference_at(
7292 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7293 int pos
, isl_int
*opt
)
7296 struct isl_basic_map
*bmap1
= NULL
;
7297 struct isl_basic_map
*bmap2
= NULL
;
7298 struct isl_ctx
*ctx
;
7299 struct isl_vec
*obj
;
7302 unsigned dim1
, dim2
;
7303 enum isl_lp_result res
;
7305 if (!bset1
|| !bset2
)
7306 return isl_lp_error
;
7308 nparam
= isl_basic_set_n_param(bset1
);
7309 dim1
= isl_basic_set_n_dim(bset1
);
7310 dim2
= isl_basic_set_n_dim(bset2
);
7311 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7312 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7313 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7314 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7315 if (!bmap1
|| !bmap2
)
7317 bmap1
= isl_basic_map_cow(bmap1
);
7318 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7319 pos
, (dim1
- pos
) + (dim2
- pos
),
7320 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7321 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7324 total
= isl_basic_map_total_dim(bmap1
);
7326 obj
= isl_vec_alloc(ctx
, 1 + total
);
7327 isl_seq_clr(obj
->block
.data
, 1 + total
);
7328 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7329 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7332 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7334 isl_basic_map_free(bmap1
);
7338 isl_basic_map_free(bmap1
);
7339 isl_basic_map_free(bmap2
);
7340 return isl_lp_error
;
7343 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7344 * for any common value of the parameters and dimensions preceding pos
7345 * in both basic sets, the values of dimension pos in bset1 are
7346 * smaller or larger than those in bset2.
7349 * 1 if bset1 follows bset2
7350 * -1 if bset1 precedes bset2
7351 * 0 if bset1 and bset2 are incomparable
7352 * -2 if some error occurred.
7354 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7355 struct isl_basic_set
*bset2
, int pos
)
7358 enum isl_lp_result res
;
7363 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7365 if (res
== isl_lp_empty
)
7367 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7368 res
== isl_lp_unbounded
)
7370 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7379 /* Given two basic sets bset1 and bset2, check whether
7380 * for any common value of the parameters and dimensions preceding pos
7381 * there is a value of dimension pos in bset1 that is larger
7382 * than a value of the same dimension in bset2.
7385 * 1 if there exists such a pair
7386 * 0 if there is no such pair, but there is a pair of equal values
7388 * -2 if some error occurred.
7390 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7391 __isl_keep isl_basic_set
*bset2
, int pos
)
7394 enum isl_lp_result res
;
7399 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7401 if (res
== isl_lp_empty
)
7403 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7404 res
== isl_lp_unbounded
)
7406 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7408 else if (res
== isl_lp_ok
)
7417 /* Given two sets set1 and set2, check whether
7418 * for any common value of the parameters and dimensions preceding pos
7419 * there is a value of dimension pos in set1 that is larger
7420 * than a value of the same dimension in set2.
7423 * 1 if there exists such a pair
7424 * 0 if there is no such pair, but there is a pair of equal values
7426 * -2 if some error occurred.
7428 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7429 __isl_keep isl_set
*set2
, int pos
)
7437 for (i
= 0; i
< set1
->n
; ++i
)
7438 for (j
= 0; j
< set2
->n
; ++j
) {
7440 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7441 if (f
== 1 || f
== -2)
7450 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7451 unsigned pos
, isl_int
*val
)
7459 total
= isl_basic_map_total_dim(bmap
);
7460 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7461 for (; d
+1 > pos
; --d
)
7462 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7466 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7468 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7470 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7473 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7479 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7480 unsigned pos
, isl_int
*val
)
7492 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7495 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7496 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7497 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7498 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7502 isl_int_set(*val
, v
);
7508 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7509 unsigned pos
, isl_int
*val
)
7511 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7515 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7518 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7521 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7522 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7524 if (pos
>= isl_basic_map_dim(bmap
, type
))
7526 return isl_basic_map_plain_has_fixed_var(bmap
,
7527 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7530 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7531 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7533 if (pos
>= isl_map_dim(map
, type
))
7535 return isl_map_plain_has_fixed_var(map
,
7536 map_offset(map
, type
) - 1 + pos
, val
);
7539 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7540 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7542 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7545 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7546 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7548 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7551 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7552 * then return this fixed value in *val.
7554 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7555 unsigned dim
, isl_int
*val
)
7557 return isl_basic_set_plain_has_fixed_var(bset
,
7558 isl_basic_set_n_param(bset
) + dim
, val
);
7561 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7562 * then return this fixed value in *val.
7564 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7565 unsigned dim
, isl_int
*val
)
7567 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7570 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7571 unsigned dim
, isl_int
*val
)
7573 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7576 /* Check if input variable in has fixed value and if so and if val is not NULL,
7577 * then return this fixed value in *val.
7579 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7580 unsigned in
, isl_int
*val
)
7582 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7585 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7586 * and if val is not NULL, then return this lower bound in *val.
7588 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7589 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7591 int i
, i_eq
= -1, i_ineq
= -1;
7598 total
= isl_basic_set_total_dim(bset
);
7599 nparam
= isl_basic_set_n_param(bset
);
7600 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7601 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7607 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7608 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7610 if (i_eq
!= -1 || i_ineq
!= -1)
7614 if (i_eq
== -1 && i_ineq
== -1)
7616 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7617 /* The coefficient should always be one due to normalization. */
7618 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7620 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7622 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7623 total
- nparam
- dim
- 1) != -1)
7626 isl_int_neg(*val
, c
[0]);
7630 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7631 unsigned dim
, isl_int
*val
)
7643 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7647 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7649 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7650 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7652 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7656 isl_int_set(*val
, v
);
7667 /* uset_gist depends on constraints without existentially quantified
7668 * variables sorting first.
7670 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7672 const struct constraint
*c1
= (const struct constraint
*)p1
;
7673 const struct constraint
*c2
= (const struct constraint
*)p2
;
7675 unsigned size
= isl_min(c1
->size
, c2
->size
);
7677 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7678 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7683 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7686 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7687 struct isl_basic_map
*bmap
)
7690 struct constraint
*c
;
7695 total
= isl_basic_map_total_dim(bmap
);
7696 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7699 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7701 c
[i
].c
= bmap
->ineq
[i
];
7703 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7704 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7705 bmap
->ineq
[i
] = c
[i
].c
;
7709 isl_basic_map_free(bmap
);
7713 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7714 __isl_take isl_basic_set
*bset
)
7716 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7717 (struct isl_basic_map
*)bset
);
7720 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7724 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7726 bmap
= isl_basic_map_remove_redundancies(bmap
);
7727 bmap
= isl_basic_map_sort_constraints(bmap
);
7728 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7732 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7734 return (struct isl_basic_set
*)isl_basic_map_normalize(
7735 (struct isl_basic_map
*)bset
);
7738 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7739 const __isl_keep isl_basic_map
*bmap2
)
7746 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7747 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7748 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7749 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7750 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7751 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7752 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7753 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7754 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7755 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7756 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7758 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7760 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7762 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7763 return bmap1
->n_eq
- bmap2
->n_eq
;
7764 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7765 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7766 if (bmap1
->n_div
!= bmap2
->n_div
)
7767 return bmap1
->n_div
- bmap2
->n_div
;
7768 total
= isl_basic_map_total_dim(bmap1
);
7769 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7770 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7774 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7775 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7779 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7780 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7787 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7788 const __isl_keep isl_basic_set
*bset2
)
7790 return isl_basic_map_plain_cmp(bset1
, bset2
);
7793 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7794 const __isl_keep isl_set
*set2
)
7800 if (set1
->n
!= set2
->n
)
7801 return set1
->n
- set2
->n
;
7803 for (i
= 0; i
< set1
->n
; ++i
) {
7804 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7812 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7813 __isl_keep isl_basic_map
*bmap2
)
7815 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7818 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7819 __isl_keep isl_basic_set
*bset2
)
7821 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7822 (isl_basic_map
*)bset2
);
7825 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7827 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7828 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7830 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7833 /* We normalize in place, but if anything goes wrong we need
7834 * to return NULL, so we need to make sure we don't change the
7835 * meaning of any possible other copies of map.
7837 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7840 struct isl_basic_map
*bmap
;
7844 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7846 for (i
= 0; i
< map
->n
; ++i
) {
7847 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7850 isl_basic_map_free(map
->p
[i
]);
7853 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7854 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7855 map
= isl_map_remove_empty_parts(map
);
7858 for (i
= map
->n
- 1; i
>= 1; --i
) {
7859 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7861 isl_basic_map_free(map
->p
[i
-1]);
7862 for (j
= i
; j
< map
->n
; ++j
)
7863 map
->p
[j
-1] = map
->p
[j
];
7873 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7875 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7878 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7888 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7891 map1
= isl_map_copy(map1
);
7892 map2
= isl_map_copy(map2
);
7893 map1
= isl_map_normalize(map1
);
7894 map2
= isl_map_normalize(map2
);
7897 equal
= map1
->n
== map2
->n
;
7898 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7899 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7912 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7914 return isl_map_plain_is_equal(map1
, map2
);
7917 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7919 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7920 (struct isl_map
*)set2
);
7923 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7925 return isl_set_plain_is_equal(set1
, set2
);
7928 /* Return an interval that ranges from min to max (inclusive)
7930 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7931 isl_int min
, isl_int max
)
7934 struct isl_basic_set
*bset
= NULL
;
7936 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7940 k
= isl_basic_set_alloc_inequality(bset
);
7943 isl_int_set_si(bset
->ineq
[k
][1], 1);
7944 isl_int_neg(bset
->ineq
[k
][0], min
);
7946 k
= isl_basic_set_alloc_inequality(bset
);
7949 isl_int_set_si(bset
->ineq
[k
][1], -1);
7950 isl_int_set(bset
->ineq
[k
][0], max
);
7954 isl_basic_set_free(bset
);
7958 /* Return the Cartesian product of the basic sets in list (in the given order).
7960 __isl_give isl_basic_set
*isl_basic_set_list_product(
7961 __isl_take
struct isl_basic_set_list
*list
)
7969 struct isl_basic_set
*product
= NULL
;
7973 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7974 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7975 nparam
= isl_basic_set_n_param(list
->p
[0]);
7976 dim
= isl_basic_set_n_dim(list
->p
[0]);
7977 extra
= list
->p
[0]->n_div
;
7978 n_eq
= list
->p
[0]->n_eq
;
7979 n_ineq
= list
->p
[0]->n_ineq
;
7980 for (i
= 1; i
< list
->n
; ++i
) {
7981 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7982 isl_assert(list
->ctx
,
7983 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7984 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7985 extra
+= list
->p
[i
]->n_div
;
7986 n_eq
+= list
->p
[i
]->n_eq
;
7987 n_ineq
+= list
->p
[i
]->n_ineq
;
7989 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7994 for (i
= 0; i
< list
->n
; ++i
) {
7995 isl_basic_set_add_constraints(product
,
7996 isl_basic_set_copy(list
->p
[i
]), dim
);
7997 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7999 isl_basic_set_list_free(list
);
8002 isl_basic_set_free(product
);
8003 isl_basic_set_list_free(list
);
8007 struct isl_basic_map
*isl_basic_map_product(
8008 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8010 isl_space
*dim_result
= NULL
;
8011 struct isl_basic_map
*bmap
;
8012 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8013 struct isl_dim_map
*dim_map1
, *dim_map2
;
8015 if (!bmap1
|| !bmap2
)
8018 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8019 bmap2
->dim
, isl_dim_param
), goto error
);
8020 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8021 isl_space_copy(bmap2
->dim
));
8023 in1
= isl_basic_map_n_in(bmap1
);
8024 in2
= isl_basic_map_n_in(bmap2
);
8025 out1
= isl_basic_map_n_out(bmap1
);
8026 out2
= isl_basic_map_n_out(bmap2
);
8027 nparam
= isl_basic_map_n_param(bmap1
);
8029 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8030 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8031 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8032 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8033 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8034 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8035 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8036 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8037 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8038 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8039 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8041 bmap
= isl_basic_map_alloc_space(dim_result
,
8042 bmap1
->n_div
+ bmap2
->n_div
,
8043 bmap1
->n_eq
+ bmap2
->n_eq
,
8044 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8045 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8046 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8047 bmap
= isl_basic_map_simplify(bmap
);
8048 return isl_basic_map_finalize(bmap
);
8050 isl_basic_map_free(bmap1
);
8051 isl_basic_map_free(bmap2
);
8055 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8056 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8058 isl_basic_map
*prod
;
8060 prod
= isl_basic_map_product(bmap1
, bmap2
);
8061 prod
= isl_basic_map_flatten(prod
);
8065 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8066 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8068 return isl_basic_map_flat_range_product(bset1
, bset2
);
8071 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8072 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8074 isl_space
*space_result
= NULL
;
8075 isl_basic_map
*bmap
;
8076 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8077 struct isl_dim_map
*dim_map1
, *dim_map2
;
8079 if (!bmap1
|| !bmap2
)
8082 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8083 isl_space_copy(bmap2
->dim
));
8085 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8086 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8087 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8088 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8090 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8091 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8092 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8093 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8094 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8095 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8096 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8097 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8098 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8099 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8100 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8102 bmap
= isl_basic_map_alloc_space(space_result
,
8103 bmap1
->n_div
+ bmap2
->n_div
,
8104 bmap1
->n_eq
+ bmap2
->n_eq
,
8105 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8106 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8107 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8108 bmap
= isl_basic_map_simplify(bmap
);
8109 return isl_basic_map_finalize(bmap
);
8111 isl_basic_map_free(bmap1
);
8112 isl_basic_map_free(bmap2
);
8116 __isl_give isl_basic_map
*isl_basic_map_range_product(
8117 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8119 isl_space
*dim_result
= NULL
;
8120 isl_basic_map
*bmap
;
8121 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8122 struct isl_dim_map
*dim_map1
, *dim_map2
;
8124 if (!bmap1
|| !bmap2
)
8127 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8128 isl_space_copy(bmap2
->dim
));
8130 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8131 out1
= isl_basic_map_n_out(bmap1
);
8132 out2
= isl_basic_map_n_out(bmap2
);
8133 nparam
= isl_basic_map_n_param(bmap1
);
8135 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8136 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8137 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8138 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8139 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8140 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8141 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8142 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8143 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8144 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8145 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8147 bmap
= isl_basic_map_alloc_space(dim_result
,
8148 bmap1
->n_div
+ bmap2
->n_div
,
8149 bmap1
->n_eq
+ bmap2
->n_eq
,
8150 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8151 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8152 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8153 bmap
= isl_basic_map_simplify(bmap
);
8154 return isl_basic_map_finalize(bmap
);
8156 isl_basic_map_free(bmap1
);
8157 isl_basic_map_free(bmap2
);
8161 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8162 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8164 isl_basic_map
*prod
;
8166 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8167 prod
= isl_basic_map_flatten_range(prod
);
8171 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8172 __isl_take isl_map
*map2
,
8173 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8174 __isl_take isl_space
*right
),
8175 __isl_give isl_basic_map
*(*basic_map_product
)(
8176 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8179 struct isl_map
*result
;
8185 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8186 map2
->dim
, isl_dim_param
), goto error
);
8188 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8189 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8190 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8192 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8193 isl_space_copy(map2
->dim
)),
8194 map1
->n
* map2
->n
, flags
);
8197 for (i
= 0; i
< map1
->n
; ++i
)
8198 for (j
= 0; j
< map2
->n
; ++j
) {
8199 struct isl_basic_map
*part
;
8200 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8201 isl_basic_map_copy(map2
->p
[j
]));
8202 if (isl_basic_map_is_empty(part
))
8203 isl_basic_map_free(part
);
8205 result
= isl_map_add_basic_map(result
, part
);
8218 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8220 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8221 __isl_take isl_map
*map2
)
8223 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8226 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8227 __isl_take isl_map
*map2
)
8229 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8232 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8234 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8235 __isl_take isl_map
*map2
)
8239 prod
= isl_map_product(map1
, map2
);
8240 prod
= isl_map_flatten(prod
);
8244 /* Given two set A and B, construct its Cartesian product A x B.
8246 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8248 return isl_map_range_product(set1
, set2
);
8251 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8252 __isl_take isl_set
*set2
)
8254 return isl_map_flat_range_product(set1
, set2
);
8257 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8259 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8260 __isl_take isl_map
*map2
)
8262 return map_product(map1
, map2
, &isl_space_domain_product
,
8263 &isl_basic_map_domain_product
);
8266 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8268 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8269 __isl_take isl_map
*map2
)
8271 return map_product(map1
, map2
, &isl_space_range_product
,
8272 &isl_basic_map_range_product
);
8275 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8276 __isl_take isl_map
*map2
)
8278 return isl_map_align_params_map_map_and(map1
, map2
,
8279 &map_domain_product_aligned
);
8282 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8283 __isl_take isl_map
*map2
)
8285 return isl_map_align_params_map_map_and(map1
, map2
,
8286 &map_range_product_aligned
);
8289 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8291 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8292 __isl_take isl_map
*map2
)
8296 prod
= isl_map_domain_product(map1
, map2
);
8297 prod
= isl_map_flatten_domain(prod
);
8301 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8303 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8304 __isl_take isl_map
*map2
)
8308 prod
= isl_map_range_product(map1
, map2
);
8309 prod
= isl_map_flatten_range(prod
);
8313 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8316 uint32_t hash
= isl_hash_init();
8321 bmap
= isl_basic_map_copy(bmap
);
8322 bmap
= isl_basic_map_normalize(bmap
);
8325 total
= isl_basic_map_total_dim(bmap
);
8326 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8327 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8329 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8330 isl_hash_hash(hash
, c_hash
);
8332 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8333 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8335 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8336 isl_hash_hash(hash
, c_hash
);
8338 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8339 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8341 if (isl_int_is_zero(bmap
->div
[i
][0]))
8343 isl_hash_byte(hash
, i
& 0xFF);
8344 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8345 isl_hash_hash(hash
, c_hash
);
8347 isl_basic_map_free(bmap
);
8351 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8353 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8356 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8363 map
= isl_map_copy(map
);
8364 map
= isl_map_normalize(map
);
8368 hash
= isl_hash_init();
8369 for (i
= 0; i
< map
->n
; ++i
) {
8371 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8372 isl_hash_hash(hash
, bmap_hash
);
8380 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8382 return isl_map_get_hash((isl_map
*)set
);
8385 /* Check if the value for dimension dim is completely determined
8386 * by the values of the other parameters and variables.
8387 * That is, check if dimension dim is involved in an equality.
8389 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8396 nparam
= isl_basic_set_n_param(bset
);
8397 for (i
= 0; i
< bset
->n_eq
; ++i
)
8398 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8403 /* Check if the value for dimension dim is completely determined
8404 * by the values of the other parameters and variables.
8405 * That is, check if dimension dim is involved in an equality
8406 * for each of the subsets.
8408 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8414 for (i
= 0; i
< set
->n
; ++i
) {
8416 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8423 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8425 return set
? set
->n
: 0;
8428 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8429 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8436 for (i
= 0; i
< map
->n
; ++i
)
8437 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8443 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8444 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8451 for (i
= 0; i
< set
->n
; ++i
)
8452 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8458 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8465 bset
= isl_basic_set_cow(bset
);
8469 dim
= isl_basic_set_get_space(bset
);
8470 dim
= isl_space_lift(dim
, bset
->n_div
);
8473 isl_space_free(bset
->dim
);
8475 bset
->extra
-= bset
->n_div
;
8478 bset
= isl_basic_set_finalize(bset
);
8482 isl_basic_set_free(bset
);
8486 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8492 set
= isl_set_align_divs(set
);
8497 set
= isl_set_cow(set
);
8501 n_div
= set
->p
[0]->n_div
;
8502 dim
= isl_set_get_space(set
);
8503 dim
= isl_space_lift(dim
, n_div
);
8506 isl_space_free(set
->dim
);
8509 for (i
= 0; i
< set
->n
; ++i
) {
8510 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8521 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8524 struct isl_basic_map
*bmap
;
8531 set
= isl_set_align_divs(set
);
8536 dim
= isl_set_get_space(set
);
8537 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8539 return isl_map_identity(isl_space_map_from_set(dim
));
8542 n_div
= set
->p
[0]->n_div
;
8543 dim
= isl_space_map_from_set(dim
);
8544 n_param
= isl_space_dim(dim
, isl_dim_param
);
8545 n_set
= isl_space_dim(dim
, isl_dim_in
);
8546 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8547 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8548 for (i
= 0; i
< n_set
; ++i
)
8549 bmap
= var_equal(bmap
, i
);
8551 total
= n_param
+ n_set
+ n_set
+ n_div
;
8552 for (i
= 0; i
< n_div
; ++i
) {
8553 k
= isl_basic_map_alloc_inequality(bmap
);
8556 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8557 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8558 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8559 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8560 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8561 set
->p
[0]->div
[i
][0]);
8563 l
= isl_basic_map_alloc_inequality(bmap
);
8566 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8567 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8568 set
->p
[0]->div
[i
][0]);
8569 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8573 bmap
= isl_basic_map_simplify(bmap
);
8574 bmap
= isl_basic_map_finalize(bmap
);
8575 return isl_map_from_basic_map(bmap
);
8578 isl_basic_map_free(bmap
);
8582 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8590 dim
= isl_basic_set_total_dim(bset
);
8591 size
+= bset
->n_eq
* (1 + dim
);
8592 size
+= bset
->n_ineq
* (1 + dim
);
8593 size
+= bset
->n_div
* (2 + dim
);
8598 int isl_set_size(__isl_keep isl_set
*set
)
8606 for (i
= 0; i
< set
->n
; ++i
)
8607 size
+= isl_basic_set_size(set
->p
[i
]);
8612 /* Check if there is any lower bound (if lower == 0) and/or upper
8613 * bound (if upper == 0) on the specified dim.
8615 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8616 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8623 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8625 pos
+= isl_basic_map_offset(bmap
, type
);
8627 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8628 if (isl_int_is_zero(bmap
->div
[i
][0]))
8630 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8634 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8635 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8638 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8639 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8646 return lower
&& upper
;
8649 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8650 enum isl_dim_type type
, unsigned pos
)
8652 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8655 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8656 enum isl_dim_type type
, unsigned pos
)
8658 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8661 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8662 enum isl_dim_type type
, unsigned pos
)
8664 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8667 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8668 enum isl_dim_type type
, unsigned pos
)
8675 for (i
= 0; i
< map
->n
; ++i
) {
8677 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8678 if (bounded
< 0 || !bounded
)
8685 /* Return 1 if the specified dim is involved in both an upper bound
8686 * and a lower bound.
8688 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8689 enum isl_dim_type type
, unsigned pos
)
8691 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8694 static int has_bound(__isl_keep isl_map
*map
,
8695 enum isl_dim_type type
, unsigned pos
,
8696 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8697 enum isl_dim_type type
, unsigned pos
))
8704 for (i
= 0; i
< map
->n
; ++i
) {
8706 bounded
= fn(map
->p
[i
], type
, pos
);
8707 if (bounded
< 0 || bounded
)
8714 /* Return 1 if the specified dim is involved in any lower bound.
8716 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8717 enum isl_dim_type type
, unsigned pos
)
8719 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8722 /* Return 1 if the specified dim is involved in any upper bound.
8724 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8725 enum isl_dim_type type
, unsigned pos
)
8727 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8730 /* For each of the "n" variables starting at "first", determine
8731 * the sign of the variable and put the results in the first "n"
8732 * elements of the array "signs".
8734 * 1 means that the variable is non-negative
8735 * -1 means that the variable is non-positive
8736 * 0 means the variable attains both positive and negative values.
8738 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8739 unsigned first
, unsigned n
, int *signs
)
8741 isl_vec
*bound
= NULL
;
8742 struct isl_tab
*tab
= NULL
;
8743 struct isl_tab_undo
*snap
;
8746 if (!bset
|| !signs
)
8749 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8750 tab
= isl_tab_from_basic_set(bset
, 0);
8754 isl_seq_clr(bound
->el
, bound
->size
);
8755 isl_int_set_si(bound
->el
[0], -1);
8757 snap
= isl_tab_snap(tab
);
8758 for (i
= 0; i
< n
; ++i
) {
8761 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8762 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8765 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8766 if (isl_tab_rollback(tab
, snap
) < 0)
8774 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8775 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8778 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8779 if (isl_tab_rollback(tab
, snap
) < 0)
8782 signs
[i
] = empty
? -1 : 0;
8786 isl_vec_free(bound
);
8790 isl_vec_free(bound
);
8794 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8795 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8797 if (!bset
|| !signs
)
8799 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8802 first
+= pos(bset
->dim
, type
) - 1;
8803 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8806 /* Check if the given basic map is obviously single-valued.
8807 * In particular, for each output dimension, check that there is
8808 * an equality that defines the output dimension in terms of
8809 * earlier dimensions.
8811 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8821 total
= 1 + isl_basic_map_total_dim(bmap
);
8822 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8823 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8825 for (i
= 0; i
< n_out
; ++i
) {
8826 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8827 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8829 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8830 total
- (o_out
+ i
+ 1)) == -1)
8833 if (j
>= bmap
->n_eq
)
8840 /* Check if the given map is obviously single-valued.
8842 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8851 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8854 /* Check if the given map is single-valued.
8859 * and check if the result is a subset of the identity mapping.
8861 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8868 sv
= isl_map_plain_is_single_valued(map
);
8872 test
= isl_map_reverse(isl_map_copy(map
));
8873 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8875 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8876 id
= isl_map_identity(dim
);
8878 sv
= isl_map_is_subset(test
, id
);
8886 int isl_map_is_injective(__isl_keep isl_map
*map
)
8890 map
= isl_map_copy(map
);
8891 map
= isl_map_reverse(map
);
8892 in
= isl_map_is_single_valued(map
);
8898 /* Check if the given map is obviously injective.
8900 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8904 map
= isl_map_copy(map
);
8905 map
= isl_map_reverse(map
);
8906 in
= isl_map_plain_is_single_valued(map
);
8912 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8916 sv
= isl_map_is_single_valued(map
);
8920 return isl_map_is_injective(map
);
8923 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8925 return isl_map_is_single_valued((isl_map
*)set
);
8928 int isl_map_is_translation(__isl_keep isl_map
*map
)
8933 delta
= isl_map_deltas(isl_map_copy(map
));
8934 ok
= isl_set_is_singleton(delta
);
8935 isl_set_free(delta
);
8940 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8942 if (isl_seq_first_non_zero(p
, pos
) != -1)
8944 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8949 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8958 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8961 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8962 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8963 for (j
= 0; j
< nvar
; ++j
) {
8964 int lower
= 0, upper
= 0;
8965 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8966 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8968 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8974 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8975 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8977 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8979 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8984 if (!lower
|| !upper
)
8991 int isl_set_is_box(__isl_keep isl_set
*set
)
8998 return isl_basic_set_is_box(set
->p
[0]);
9001 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9006 return isl_space_is_wrapping(bset
->dim
);
9009 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9014 return isl_space_is_wrapping(set
->dim
);
9017 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9019 bmap
= isl_basic_map_cow(bmap
);
9023 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9027 bmap
= isl_basic_map_finalize(bmap
);
9029 return (isl_basic_set
*)bmap
;
9031 isl_basic_map_free(bmap
);
9035 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9039 map
= isl_map_cow(map
);
9043 for (i
= 0; i
< map
->n
; ++i
) {
9044 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9048 map
->dim
= isl_space_wrap(map
->dim
);
9052 return (isl_set
*)map
;
9058 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9060 bset
= isl_basic_set_cow(bset
);
9064 bset
->dim
= isl_space_unwrap(bset
->dim
);
9068 bset
= isl_basic_set_finalize(bset
);
9070 return (isl_basic_map
*)bset
;
9072 isl_basic_set_free(bset
);
9076 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9083 if (!isl_set_is_wrapping(set
))
9084 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9087 set
= isl_set_cow(set
);
9091 for (i
= 0; i
< set
->n
; ++i
) {
9092 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9097 set
->dim
= isl_space_unwrap(set
->dim
);
9101 return (isl_map
*)set
;
9107 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9108 enum isl_dim_type type
)
9113 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9116 bmap
= isl_basic_map_cow(bmap
);
9120 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9124 bmap
= isl_basic_map_finalize(bmap
);
9128 isl_basic_map_free(bmap
);
9132 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9133 enum isl_dim_type type
)
9140 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9143 map
= isl_map_cow(map
);
9147 for (i
= 0; i
< map
->n
; ++i
) {
9148 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9152 map
->dim
= isl_space_reset(map
->dim
, type
);
9162 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9167 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9170 bmap
= isl_basic_map_cow(bmap
);
9174 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9178 bmap
= isl_basic_map_finalize(bmap
);
9182 isl_basic_map_free(bmap
);
9186 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9188 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9191 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9192 __isl_take isl_basic_map
*bmap
)
9197 if (!bmap
->dim
->nested
[0])
9200 bmap
= isl_basic_map_cow(bmap
);
9204 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9208 bmap
= isl_basic_map_finalize(bmap
);
9212 isl_basic_map_free(bmap
);
9216 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9217 __isl_take isl_basic_map
*bmap
)
9222 if (!bmap
->dim
->nested
[1])
9225 bmap
= isl_basic_map_cow(bmap
);
9229 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9233 bmap
= isl_basic_map_finalize(bmap
);
9237 isl_basic_map_free(bmap
);
9241 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9248 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9251 map
= isl_map_cow(map
);
9255 for (i
= 0; i
< map
->n
; ++i
) {
9256 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9260 map
->dim
= isl_space_flatten(map
->dim
);
9270 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9272 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9275 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9277 isl_space
*dim
, *flat_dim
;
9280 dim
= isl_set_get_space(set
);
9281 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9282 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9283 map
= isl_map_intersect_domain(map
, set
);
9288 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9295 if (!map
->dim
->nested
[0])
9298 map
= isl_map_cow(map
);
9302 for (i
= 0; i
< map
->n
; ++i
) {
9303 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9307 map
->dim
= isl_space_flatten_domain(map
->dim
);
9317 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9324 if (!map
->dim
->nested
[1])
9327 map
= isl_map_cow(map
);
9331 for (i
= 0; i
< map
->n
; ++i
) {
9332 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9336 map
->dim
= isl_space_flatten_range(map
->dim
);
9346 /* Reorder the dimensions of "bmap" according to the given dim_map
9347 * and set the dimension specification to "dim".
9349 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9350 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9355 bmap
= isl_basic_map_cow(bmap
);
9356 if (!bmap
|| !dim
|| !dim_map
)
9359 flags
= bmap
->flags
;
9360 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
9361 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
9362 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
9363 res
= isl_basic_map_alloc_space(dim
,
9364 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9365 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9368 res
= isl_basic_map_finalize(res
);
9372 isl_basic_map_free(bmap
);
9373 isl_space_free(dim
);
9377 /* Reorder the dimensions of "map" according to given reordering.
9379 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9380 __isl_take isl_reordering
*r
)
9383 struct isl_dim_map
*dim_map
;
9385 map
= isl_map_cow(map
);
9386 dim_map
= isl_dim_map_from_reordering(r
);
9387 if (!map
|| !r
|| !dim_map
)
9390 for (i
= 0; i
< map
->n
; ++i
) {
9391 struct isl_dim_map
*dim_map_i
;
9393 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9395 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9396 isl_space_copy(r
->dim
), dim_map_i
);
9402 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9404 isl_reordering_free(r
);
9410 isl_reordering_free(r
);
9414 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9415 __isl_take isl_reordering
*r
)
9417 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9420 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9421 __isl_take isl_space
*model
)
9428 ctx
= isl_space_get_ctx(model
);
9429 if (!isl_space_has_named_params(model
))
9430 isl_die(ctx
, isl_error_invalid
,
9431 "model has unnamed parameters", goto error
);
9432 if (!isl_space_has_named_params(map
->dim
))
9433 isl_die(ctx
, isl_error_invalid
,
9434 "relation has unnamed parameters", goto error
);
9435 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9436 isl_reordering
*exp
;
9438 model
= isl_space_drop_dims(model
, isl_dim_in
,
9439 0, isl_space_dim(model
, isl_dim_in
));
9440 model
= isl_space_drop_dims(model
, isl_dim_out
,
9441 0, isl_space_dim(model
, isl_dim_out
));
9442 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9443 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9444 map
= isl_map_realign(map
, exp
);
9447 isl_space_free(model
);
9450 isl_space_free(model
);
9455 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9456 __isl_take isl_space
*model
)
9458 return isl_map_align_params(set
, model
);
9461 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9462 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9463 enum isl_dim_type c2
, enum isl_dim_type c3
,
9464 enum isl_dim_type c4
, enum isl_dim_type c5
)
9466 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9467 struct isl_mat
*mat
;
9473 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9474 isl_basic_map_total_dim(bmap
) + 1);
9477 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9478 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9479 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9480 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9481 isl_int_set(mat
->row
[i
][pos
],
9482 bmap
->eq
[i
][off
+ k
]);
9490 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9491 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9492 enum isl_dim_type c2
, enum isl_dim_type c3
,
9493 enum isl_dim_type c4
, enum isl_dim_type c5
)
9495 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9496 struct isl_mat
*mat
;
9502 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9503 isl_basic_map_total_dim(bmap
) + 1);
9506 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9507 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9508 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9509 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9510 isl_int_set(mat
->row
[i
][pos
],
9511 bmap
->ineq
[i
][off
+ k
]);
9519 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9520 __isl_take isl_space
*dim
,
9521 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9522 enum isl_dim_type c2
, enum isl_dim_type c3
,
9523 enum isl_dim_type c4
, enum isl_dim_type c5
)
9525 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9526 isl_basic_map
*bmap
;
9532 if (!dim
|| !eq
|| !ineq
)
9535 if (eq
->n_col
!= ineq
->n_col
)
9536 isl_die(dim
->ctx
, isl_error_invalid
,
9537 "equalities and inequalities matrices should have "
9538 "same number of columns", goto error
);
9540 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9542 if (eq
->n_col
< total
)
9543 isl_die(dim
->ctx
, isl_error_invalid
,
9544 "number of columns too small", goto error
);
9546 extra
= eq
->n_col
- total
;
9548 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9549 eq
->n_row
, ineq
->n_row
);
9552 for (i
= 0; i
< extra
; ++i
) {
9553 k
= isl_basic_map_alloc_div(bmap
);
9556 isl_int_set_si(bmap
->div
[k
][0], 0);
9558 for (i
= 0; i
< eq
->n_row
; ++i
) {
9559 l
= isl_basic_map_alloc_equality(bmap
);
9562 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9563 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9564 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9565 isl_int_set(bmap
->eq
[l
][off
+ k
],
9571 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9572 l
= isl_basic_map_alloc_inequality(bmap
);
9575 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9576 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9577 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9578 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9585 isl_space_free(dim
);
9591 isl_space_free(dim
);
9597 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9598 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9599 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9601 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9602 c1
, c2
, c3
, c4
, isl_dim_in
);
9605 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9606 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9607 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9609 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9610 c1
, c2
, c3
, c4
, isl_dim_in
);
9613 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9614 __isl_take isl_space
*dim
,
9615 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9616 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9618 return (isl_basic_set
*)
9619 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9620 c1
, c2
, c3
, c4
, isl_dim_in
);
9623 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9628 return isl_space_can_zip(bmap
->dim
);
9631 int isl_map_can_zip(__isl_keep isl_map
*map
)
9636 return isl_space_can_zip(map
->dim
);
9639 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9640 * (A -> C) -> (B -> D).
9642 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9651 if (!isl_basic_map_can_zip(bmap
))
9652 isl_die(bmap
->ctx
, isl_error_invalid
,
9653 "basic map cannot be zipped", goto error
);
9654 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9655 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9656 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9657 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9658 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9661 bmap
->dim
= isl_space_zip(bmap
->dim
);
9666 isl_basic_map_free(bmap
);
9670 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9671 * (A -> C) -> (B -> D).
9673 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9680 if (!isl_map_can_zip(map
))
9681 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9684 map
= isl_map_cow(map
);
9688 for (i
= 0; i
< map
->n
; ++i
) {
9689 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9694 map
->dim
= isl_space_zip(map
->dim
);
9704 /* Construct a basic map mapping the domain of the affine expression
9705 * to a one-dimensional range prescribed by the affine expression.
9707 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9711 isl_local_space
*ls
;
9712 isl_basic_map
*bmap
;
9717 ls
= isl_aff_get_local_space(aff
);
9718 bmap
= isl_basic_map_from_local_space(ls
);
9719 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9720 k
= isl_basic_map_alloc_equality(bmap
);
9724 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9725 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9726 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9727 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9728 aff
->v
->size
- (pos
+ 1));
9731 bmap
= isl_basic_map_finalize(bmap
);
9735 isl_basic_map_free(bmap
);
9739 /* Construct a basic map mapping the domain the multi-affine expression
9740 * to its range, with each dimension in the range equated to the
9741 * corresponding affine expression.
9743 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9744 __isl_take isl_multi_aff
*maff
)
9748 isl_basic_map
*bmap
;
9753 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9754 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9755 "invalid space", return isl_multi_aff_free(maff
));
9757 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9758 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9760 for (i
= 0; i
< maff
->n
; ++i
) {
9762 isl_basic_map
*bmap_i
;
9764 aff
= isl_aff_copy(maff
->p
[i
]);
9765 bmap_i
= isl_basic_map_from_aff(aff
);
9767 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9770 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9772 isl_multi_aff_free(maff
);
9776 /* Construct a basic map mapping a domain in the given space to
9777 * to an n-dimensional range, with n the number of elements in the list,
9778 * where each coordinate in the range is prescribed by the
9779 * corresponding affine expression.
9780 * The domains of all affine expressions in the list are assumed to match
9783 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9784 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9788 isl_basic_map
*bmap
;
9793 dim
= isl_space_from_domain(domain_dim
);
9794 bmap
= isl_basic_map_universe(dim
);
9796 for (i
= 0; i
< list
->n
; ++i
) {
9798 isl_basic_map
*bmap_i
;
9800 aff
= isl_aff_copy(list
->p
[i
]);
9801 bmap_i
= isl_basic_map_from_aff(aff
);
9803 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9806 isl_aff_list_free(list
);
9810 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9811 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9813 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9816 /* Add a constraint imposing that the given two dimensions are equal.
9818 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9819 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9821 isl_basic_map
*bmap
= NULL
;
9827 if (pos1
>= isl_map_dim(map
, type1
))
9828 isl_die(map
->ctx
, isl_error_invalid
,
9829 "index out of bounds", goto error
);
9830 if (pos2
>= isl_map_dim(map
, type2
))
9831 isl_die(map
->ctx
, isl_error_invalid
,
9832 "index out of bounds", goto error
);
9834 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9835 i
= isl_basic_map_alloc_equality(bmap
);
9838 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9839 pos1
+= isl_basic_map_offset(bmap
, type1
);
9840 pos2
+= isl_basic_map_offset(bmap
, type2
);
9841 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9842 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9843 bmap
= isl_basic_map_finalize(bmap
);
9845 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9849 isl_basic_map_free(bmap
);
9854 /* Add a constraint imposing that the given two dimensions have opposite values.
9856 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9857 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9859 isl_basic_map
*bmap
= NULL
;
9865 if (pos1
>= isl_map_dim(map
, type1
))
9866 isl_die(map
->ctx
, isl_error_invalid
,
9867 "index out of bounds", goto error
);
9868 if (pos2
>= isl_map_dim(map
, type2
))
9869 isl_die(map
->ctx
, isl_error_invalid
,
9870 "index out of bounds", goto error
);
9872 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9873 i
= isl_basic_map_alloc_equality(bmap
);
9876 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9877 pos1
+= isl_basic_map_offset(bmap
, type1
);
9878 pos2
+= isl_basic_map_offset(bmap
, type2
);
9879 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9880 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9881 bmap
= isl_basic_map_finalize(bmap
);
9883 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9887 isl_basic_map_free(bmap
);
9892 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9896 isl_local_space
*ls
;
9901 if (!isl_basic_map_divs_known(bmap
))
9902 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9903 "some divs are unknown", return NULL
);
9905 ls
= isl_basic_map_get_local_space(bmap
);
9906 div
= isl_local_space_get_div(ls
, pos
);
9907 isl_local_space_free(ls
);
9912 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9915 return isl_basic_map_get_div(bset
, pos
);
9918 /* Plug in "subs" for dimension "type", "pos" of "bset".
9920 * Let i be the dimension to replace and let "subs" be of the form
9924 * Any integer division with a non-zero coefficient for i,
9926 * floor((a i + g)/m)
9930 * floor((a f + d g)/(m d))
9932 * Constraints of the form
9940 __isl_give isl_basic_set
*isl_basic_set_substitute(
9941 __isl_take isl_basic_set
*bset
,
9942 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9948 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9951 bset
= isl_basic_set_cow(bset
);
9955 ctx
= isl_basic_set_get_ctx(bset
);
9956 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9957 isl_die(ctx
, isl_error_invalid
,
9958 "spaces don't match", goto error
);
9959 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9960 isl_die(ctx
, isl_error_unsupported
,
9961 "cannot handle divs yet", goto error
);
9963 pos
+= isl_basic_set_offset(bset
, type
);
9967 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9968 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9970 isl_int_set(v
, bset
->eq
[i
][pos
]);
9971 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9972 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9973 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9976 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9977 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9979 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9980 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9981 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9982 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9985 for (i
= 0; i
< bset
->n_div
; ++i
) {
9986 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9988 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9989 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9990 isl_seq_combine(bset
->div
[i
] + 1,
9991 subs
->v
->el
[0], bset
->div
[i
] + 1,
9992 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9993 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9998 bset
= isl_basic_set_simplify(bset
);
9999 return isl_basic_set_finalize(bset
);
10001 isl_basic_set_free(bset
);
10005 /* Plug in "subs" for dimension "type", "pos" of "set".
10007 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
10008 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10012 if (set
&& isl_set_plain_is_empty(set
))
10015 set
= isl_set_cow(set
);
10019 for (i
= set
->n
- 1; i
>= 0; --i
) {
10020 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
10021 if (remove_if_empty(set
, i
) < 0)