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);
1871 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1880 map
= isl_map_cow(map
);
1884 for (i
= 0; i
< map
->n
; ++i
) {
1885 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1895 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1897 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1900 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1901 __isl_take isl_basic_set
*bset
,
1902 enum isl_dim_type type
, unsigned first
, unsigned n
)
1904 return (isl_basic_set
*)
1905 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1908 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1909 enum isl_dim_type type
, unsigned first
, unsigned n
)
1916 map
= isl_map_cow(map
);
1919 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1921 for (i
= 0; i
< map
->n
; ++i
) {
1922 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1923 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1927 map
= isl_map_drop(map
, type
, first
, n
);
1934 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1935 enum isl_dim_type type
, unsigned first
, unsigned n
)
1937 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1940 /* Project out n inputs starting at first using Fourier-Motzkin */
1941 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1942 unsigned first
, unsigned n
)
1944 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1947 static void dump_term(struct isl_basic_map
*bmap
,
1948 isl_int c
, int pos
, FILE *out
)
1951 unsigned in
= isl_basic_map_n_in(bmap
);
1952 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1953 unsigned nparam
= isl_basic_map_n_param(bmap
);
1955 isl_int_print(out
, c
, 0);
1957 if (!isl_int_is_one(c
))
1958 isl_int_print(out
, c
, 0);
1959 if (pos
< 1 + nparam
) {
1960 name
= isl_space_get_dim_name(bmap
->dim
,
1961 isl_dim_param
, pos
- 1);
1963 fprintf(out
, "%s", name
);
1965 fprintf(out
, "p%d", pos
- 1);
1966 } else if (pos
< 1 + nparam
+ in
)
1967 fprintf(out
, "i%d", pos
- 1 - nparam
);
1968 else if (pos
< 1 + nparam
+ dim
)
1969 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1971 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1975 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1976 int sign
, FILE *out
)
1980 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1984 for (i
= 0, first
= 1; i
< len
; ++i
) {
1985 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1988 fprintf(out
, " + ");
1990 isl_int_abs(v
, c
[i
]);
1991 dump_term(bmap
, v
, i
, out
);
1998 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1999 const char *op
, FILE *out
, int indent
)
2003 fprintf(out
, "%*s", indent
, "");
2005 dump_constraint_sign(bmap
, c
, 1, out
);
2006 fprintf(out
, " %s ", op
);
2007 dump_constraint_sign(bmap
, c
, -1, out
);
2011 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2012 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2014 fprintf(out
, "%*s", indent
, "");
2015 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2020 static void dump_constraints(struct isl_basic_map
*bmap
,
2021 isl_int
**c
, unsigned n
,
2022 const char *op
, FILE *out
, int indent
)
2026 for (i
= 0; i
< n
; ++i
)
2027 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2030 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2034 unsigned total
= isl_basic_map_total_dim(bmap
);
2036 for (j
= 0; j
< 1 + total
; ++j
) {
2037 if (isl_int_is_zero(exp
[j
]))
2039 if (!first
&& isl_int_is_pos(exp
[j
]))
2041 dump_term(bmap
, exp
[j
], j
, out
);
2046 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2050 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2051 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2053 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2054 fprintf(out
, "%*s", indent
, "");
2055 fprintf(out
, "e%d = [(", i
);
2056 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2058 isl_int_print(out
, bmap
->div
[i
][0], 0);
2059 fprintf(out
, "]\n");
2063 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2064 FILE *out
, int indent
)
2067 fprintf(out
, "null basic set\n");
2071 fprintf(out
, "%*s", indent
, "");
2072 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2073 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2074 bset
->extra
, bset
->flags
);
2075 dump((struct isl_basic_map
*)bset
, out
, indent
);
2078 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2079 FILE *out
, int indent
)
2082 fprintf(out
, "null basic map\n");
2086 fprintf(out
, "%*s", indent
, "");
2087 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2088 "flags: %x, n_name: %d\n",
2090 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2091 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2092 dump(bmap
, out
, indent
);
2095 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2100 total
= isl_basic_map_total_dim(bmap
);
2101 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2102 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2103 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2104 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2108 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2111 struct isl_set
*set
;
2115 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2116 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2117 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2118 sizeof(struct isl_set
) +
2119 (n
- 1) * sizeof(struct isl_basic_set
*));
2123 set
->ctx
= dim
->ctx
;
2124 isl_ctx_ref(set
->ctx
);
2132 isl_space_free(dim
);
2136 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2137 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2139 struct isl_set
*set
;
2142 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2146 set
= isl_set_alloc_space(dims
, n
, flags
);
2150 /* Make sure "map" has room for at least "n" more basic maps.
2152 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2155 struct isl_map
*grown
= NULL
;
2159 isl_assert(map
->ctx
, n
>= 0, goto error
);
2160 if (map
->n
+ n
<= map
->size
)
2162 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2165 for (i
= 0; i
< map
->n
; ++i
) {
2166 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2174 isl_map_free(grown
);
2179 /* Make sure "set" has room for at least "n" more basic sets.
2181 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2183 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2186 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2189 struct isl_set
*dup
;
2194 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2197 for (i
= 0; i
< set
->n
; ++i
)
2198 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2202 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2204 return isl_map_from_basic_map(bset
);
2207 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2209 struct isl_map
*map
;
2214 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2215 return isl_map_add_basic_map(map
, bmap
);
2218 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2219 __isl_take isl_basic_set
*bset
)
2221 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2222 (struct isl_basic_map
*)bset
);
2225 void isl_set_free(struct isl_set
*set
)
2235 isl_ctx_deref(set
->ctx
);
2236 for (i
= 0; i
< set
->n
; ++i
)
2237 isl_basic_set_free(set
->p
[i
]);
2238 isl_space_free(set
->dim
);
2242 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2247 fprintf(out
, "null set\n");
2251 fprintf(out
, "%*s", indent
, "");
2252 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2253 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2255 for (i
= 0; i
< set
->n
; ++i
) {
2256 fprintf(out
, "%*s", indent
, "");
2257 fprintf(out
, "basic set %d:\n", i
);
2258 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2262 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2267 fprintf(out
, "null map\n");
2271 fprintf(out
, "%*s", indent
, "");
2272 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2273 "flags: %x, n_name: %d\n",
2274 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2275 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2276 for (i
= 0; i
< map
->n
; ++i
) {
2277 fprintf(out
, "%*s", indent
, "");
2278 fprintf(out
, "basic map %d:\n", i
);
2279 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2283 struct isl_basic_map
*isl_basic_map_intersect_domain(
2284 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2286 struct isl_basic_map
*bmap_domain
;
2291 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2292 bset
->dim
, isl_dim_param
), goto error
);
2294 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2295 isl_assert(bset
->ctx
,
2296 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2298 bmap
= isl_basic_map_cow(bmap
);
2301 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2302 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2303 bmap_domain
= isl_basic_map_from_domain(bset
);
2304 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2306 bmap
= isl_basic_map_simplify(bmap
);
2307 return isl_basic_map_finalize(bmap
);
2309 isl_basic_map_free(bmap
);
2310 isl_basic_set_free(bset
);
2314 struct isl_basic_map
*isl_basic_map_intersect_range(
2315 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2317 struct isl_basic_map
*bmap_range
;
2322 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2323 bset
->dim
, isl_dim_param
), goto error
);
2325 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2326 isl_assert(bset
->ctx
,
2327 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2329 if (isl_basic_set_is_universe(bset
)) {
2330 isl_basic_set_free(bset
);
2334 bmap
= isl_basic_map_cow(bmap
);
2337 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2338 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2339 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2340 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2342 bmap
= isl_basic_map_simplify(bmap
);
2343 return isl_basic_map_finalize(bmap
);
2345 isl_basic_map_free(bmap
);
2346 isl_basic_set_free(bset
);
2350 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2356 total
= 1 + isl_basic_map_total_dim(bmap
);
2357 if (total
!= vec
->size
)
2362 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2363 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2364 if (!isl_int_is_zero(s
)) {
2370 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2371 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2372 if (isl_int_is_neg(s
)) {
2383 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2385 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2388 struct isl_basic_map
*isl_basic_map_intersect(
2389 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2391 struct isl_vec
*sample
= NULL
;
2393 if (!bmap1
|| !bmap2
)
2396 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2397 bmap2
->dim
, isl_dim_param
), goto error
);
2398 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2399 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2400 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2401 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2402 return isl_basic_map_intersect(bmap2
, bmap1
);
2404 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2405 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2406 isl_assert(bmap1
->ctx
,
2407 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2409 if (bmap1
->sample
&&
2410 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2411 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2412 sample
= isl_vec_copy(bmap1
->sample
);
2413 else if (bmap2
->sample
&&
2414 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2415 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2416 sample
= isl_vec_copy(bmap2
->sample
);
2418 bmap1
= isl_basic_map_cow(bmap1
);
2421 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2422 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2423 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2426 isl_vec_free(sample
);
2428 isl_vec_free(bmap1
->sample
);
2429 bmap1
->sample
= sample
;
2432 bmap1
= isl_basic_map_simplify(bmap1
);
2433 return isl_basic_map_finalize(bmap1
);
2436 isl_vec_free(sample
);
2437 isl_basic_map_free(bmap1
);
2438 isl_basic_map_free(bmap2
);
2442 struct isl_basic_set
*isl_basic_set_intersect(
2443 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2445 return (struct isl_basic_set
*)
2446 isl_basic_map_intersect(
2447 (struct isl_basic_map
*)bset1
,
2448 (struct isl_basic_map
*)bset2
);
2451 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2452 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2454 return isl_basic_set_intersect(bset1
, bset2
);
2457 /* Special case of isl_map_intersect, where both map1 and map2
2458 * are convex, without any divs and such that either map1 or map2
2459 * contains a single constraint. This constraint is then simply
2460 * added to the other map.
2462 static __isl_give isl_map
*map_intersect_add_constraint(
2463 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2465 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2466 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2467 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2468 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2470 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2471 return isl_map_intersect(map2
, map1
);
2473 isl_assert(map2
->ctx
,
2474 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2476 map1
= isl_map_cow(map1
);
2479 if (isl_map_plain_is_empty(map1
)) {
2483 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2484 if (map2
->p
[0]->n_eq
== 1)
2485 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2487 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2488 map2
->p
[0]->ineq
[0]);
2490 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2491 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2495 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2496 isl_basic_map_free(map1
->p
[0]);
2509 /* map2 may be either a parameter domain or a map living in the same
2512 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2513 __isl_take isl_map
*map2
)
2516 struct isl_map
*result
;
2522 if (isl_map_plain_is_empty(map1
) &&
2523 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2527 if (isl_map_plain_is_empty(map2
) &&
2528 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2533 if (map1
->n
== 1 && map2
->n
== 1 &&
2534 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2535 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2536 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2537 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2538 return map_intersect_add_constraint(map1
, map2
);
2540 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2541 isl_space_dim(map2
->dim
, isl_dim_param
))
2542 isl_assert(map1
->ctx
,
2543 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2545 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2546 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2547 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2549 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2550 map1
->n
* map2
->n
, flags
);
2553 for (i
= 0; i
< map1
->n
; ++i
)
2554 for (j
= 0; j
< map2
->n
; ++j
) {
2555 struct isl_basic_map
*part
;
2556 part
= isl_basic_map_intersect(
2557 isl_basic_map_copy(map1
->p
[i
]),
2558 isl_basic_map_copy(map2
->p
[j
]));
2559 if (isl_basic_map_is_empty(part
))
2560 isl_basic_map_free(part
);
2562 result
= isl_map_add_basic_map(result
, part
);
2575 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2576 __isl_take isl_map
*map2
)
2580 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2581 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2582 "spaces don't match", goto error
);
2583 return map_intersect_internal(map1
, map2
);
2590 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2591 __isl_take isl_map
*map2
)
2593 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2596 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2598 return (struct isl_set
*)
2599 isl_map_intersect((struct isl_map
*)set1
,
2600 (struct isl_map
*)set2
);
2603 /* map_intersect_internal accepts intersections
2604 * with parameter domains, so we can just call that function.
2606 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2607 __isl_take isl_set
*params
)
2609 return map_intersect_internal(map
, params
);
2612 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2613 __isl_take isl_map
*map2
)
2615 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2618 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2619 __isl_take isl_set
*params
)
2621 return isl_map_intersect_params(set
, params
);
2624 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2627 struct isl_basic_set
*bset
;
2632 bmap
= isl_basic_map_cow(bmap
);
2635 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2636 in
= isl_basic_map_n_in(bmap
);
2637 bset
= isl_basic_set_from_basic_map(bmap
);
2638 bset
= isl_basic_set_swap_vars(bset
, in
);
2639 return isl_basic_map_from_basic_set(bset
, dim
);
2642 static __isl_give isl_basic_map
*basic_map_space_reset(
2643 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2647 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2650 space
= isl_basic_map_get_space(bmap
);
2651 space
= isl_space_reset(space
, type
);
2652 bmap
= isl_basic_map_reset_space(bmap
, space
);
2656 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2657 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2660 struct isl_basic_map
*res
;
2661 struct isl_dim_map
*dim_map
;
2662 unsigned total
, off
;
2663 enum isl_dim_type t
;
2666 return basic_map_space_reset(bmap
, type
);
2671 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2673 total
= isl_basic_map_total_dim(bmap
) + n
;
2674 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2676 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2678 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2680 unsigned size
= isl_basic_map_dim(bmap
, t
);
2681 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2683 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2684 pos
, size
- pos
, off
+ pos
+ n
);
2686 off
+= isl_space_dim(res_dim
, t
);
2688 isl_dim_map_div(dim_map
, bmap
, off
);
2690 res
= isl_basic_map_alloc_space(res_dim
,
2691 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2692 if (isl_basic_map_is_rational(bmap
))
2693 res
= isl_basic_map_set_rational(res
);
2694 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2695 return isl_basic_map_finalize(res
);
2698 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2699 enum isl_dim_type type
, unsigned n
)
2703 return isl_basic_map_insert(bmap
, type
,
2704 isl_basic_map_dim(bmap
, type
), n
);
2707 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2708 enum isl_dim_type type
, unsigned n
)
2712 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2713 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2715 isl_basic_set_free(bset
);
2719 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2720 enum isl_dim_type type
)
2724 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2727 space
= isl_map_get_space(map
);
2728 space
= isl_space_reset(space
, type
);
2729 map
= isl_map_reset_space(map
, space
);
2733 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2734 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2739 return map_space_reset(map
, type
);
2741 map
= isl_map_cow(map
);
2745 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2749 for (i
= 0; i
< map
->n
; ++i
) {
2750 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2761 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2762 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2764 return isl_map_insert_dims(set
, type
, pos
, n
);
2767 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2768 enum isl_dim_type type
, unsigned n
)
2772 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2775 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2776 enum isl_dim_type type
, unsigned n
)
2780 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2781 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2787 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2788 __isl_take isl_basic_map
*bmap
,
2789 enum isl_dim_type dst_type
, unsigned dst_pos
,
2790 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2792 struct isl_dim_map
*dim_map
;
2793 struct isl_basic_map
*res
;
2794 enum isl_dim_type t
;
2795 unsigned total
, off
;
2802 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2805 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2808 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2810 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2811 pos(bmap
->dim
, src_type
) + src_pos
+
2812 ((src_type
< dst_type
) ? n
: 0)) {
2813 bmap
= isl_basic_map_cow(bmap
);
2817 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2818 src_type
, src_pos
, n
);
2822 bmap
= isl_basic_map_finalize(bmap
);
2827 total
= isl_basic_map_total_dim(bmap
);
2828 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2831 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2832 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2833 if (t
== dst_type
) {
2834 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2837 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2840 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2841 dst_pos
, size
- dst_pos
, off
);
2842 off
+= size
- dst_pos
;
2843 } else if (t
== src_type
) {
2844 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2847 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2848 src_pos
+ n
, size
- src_pos
- n
, off
);
2849 off
+= size
- src_pos
- n
;
2851 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2855 isl_dim_map_div(dim_map
, bmap
, off
);
2857 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2858 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2859 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2861 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2862 src_type
, src_pos
, n
);
2866 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2867 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2868 bmap
= isl_basic_map_finalize(bmap
);
2872 isl_basic_map_free(bmap
);
2876 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2877 enum isl_dim_type dst_type
, unsigned dst_pos
,
2878 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2880 return (isl_basic_set
*)isl_basic_map_move_dims(
2881 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2884 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2885 enum isl_dim_type dst_type
, unsigned dst_pos
,
2886 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2890 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2891 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2892 src_type
, src_pos
, n
);
2898 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2899 enum isl_dim_type dst_type
, unsigned dst_pos
,
2900 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2909 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2912 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2915 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2917 map
= isl_map_cow(map
);
2921 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2925 for (i
= 0; i
< map
->n
; ++i
) {
2926 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2928 src_type
, src_pos
, n
);
2939 /* Move the specified dimensions to the last columns right before
2940 * the divs. Don't change the dimension specification of bmap.
2941 * That's the responsibility of the caller.
2943 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2944 enum isl_dim_type type
, unsigned first
, unsigned n
)
2946 struct isl_dim_map
*dim_map
;
2947 struct isl_basic_map
*res
;
2948 enum isl_dim_type t
;
2949 unsigned total
, off
;
2953 if (pos(bmap
->dim
, type
) + first
+ n
==
2954 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2957 total
= isl_basic_map_total_dim(bmap
);
2958 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2961 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2962 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2964 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2967 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2968 first
, n
, total
- bmap
->n_div
- n
);
2969 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2970 first
+ n
, size
- (first
+ n
), off
);
2971 off
+= size
- (first
+ n
);
2973 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2977 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2979 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2980 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2981 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2985 /* Turn the n dimensions of type type, starting at first
2986 * into existentially quantified variables.
2988 __isl_give isl_basic_map
*isl_basic_map_project_out(
2989 __isl_take isl_basic_map
*bmap
,
2990 enum isl_dim_type type
, unsigned first
, unsigned n
)
3003 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3004 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3006 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3009 bmap
= move_last(bmap
, type
, first
, n
);
3010 bmap
= isl_basic_map_cow(bmap
);
3014 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3015 old
= bmap
->block2
.data
;
3016 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3017 (bmap
->extra
+ n
) * (1 + row_size
));
3018 if (!bmap
->block2
.data
)
3020 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3023 for (i
= 0; i
< n
; ++i
) {
3024 new_div
[i
] = bmap
->block2
.data
+
3025 (bmap
->extra
+ i
) * (1 + row_size
);
3026 isl_seq_clr(new_div
[i
], 1 + row_size
);
3028 for (i
= 0; i
< bmap
->extra
; ++i
)
3029 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3031 bmap
->div
= new_div
;
3035 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3038 bmap
= isl_basic_map_simplify(bmap
);
3039 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3040 return isl_basic_map_finalize(bmap
);
3042 isl_basic_map_free(bmap
);
3046 /* Turn the n dimensions of type type, starting at first
3047 * into existentially quantified variables.
3049 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3050 enum isl_dim_type type
, unsigned first
, unsigned n
)
3052 return (isl_basic_set
*)isl_basic_map_project_out(
3053 (isl_basic_map
*)bset
, type
, first
, n
);
3056 /* Turn the n dimensions of type type, starting at first
3057 * into existentially quantified variables.
3059 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3060 enum isl_dim_type type
, unsigned first
, unsigned n
)
3070 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3072 map
= isl_map_cow(map
);
3076 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3080 for (i
= 0; i
< map
->n
; ++i
) {
3081 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3092 /* Turn the n dimensions of type type, starting at first
3093 * into existentially quantified variables.
3095 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3096 enum isl_dim_type type
, unsigned first
, unsigned n
)
3098 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3101 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3105 for (i
= 0; i
< n
; ++i
) {
3106 j
= isl_basic_map_alloc_div(bmap
);
3109 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3113 isl_basic_map_free(bmap
);
3117 struct isl_basic_map
*isl_basic_map_apply_range(
3118 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3120 isl_space
*dim_result
= NULL
;
3121 struct isl_basic_map
*bmap
;
3122 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3123 struct isl_dim_map
*dim_map1
, *dim_map2
;
3125 if (!bmap1
|| !bmap2
)
3128 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3129 isl_space_copy(bmap2
->dim
));
3131 n_in
= isl_basic_map_n_in(bmap1
);
3132 n_out
= isl_basic_map_n_out(bmap2
);
3133 n
= isl_basic_map_n_out(bmap1
);
3134 nparam
= isl_basic_map_n_param(bmap1
);
3136 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3137 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3138 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3139 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3140 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3141 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3142 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3143 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3144 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3145 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3146 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3148 bmap
= isl_basic_map_alloc_space(dim_result
,
3149 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3150 bmap1
->n_eq
+ bmap2
->n_eq
,
3151 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3152 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3153 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3154 bmap
= add_divs(bmap
, n
);
3155 bmap
= isl_basic_map_simplify(bmap
);
3156 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3157 return isl_basic_map_finalize(bmap
);
3159 isl_basic_map_free(bmap1
);
3160 isl_basic_map_free(bmap2
);
3164 struct isl_basic_set
*isl_basic_set_apply(
3165 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3170 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3173 return (struct isl_basic_set
*)
3174 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3176 isl_basic_set_free(bset
);
3177 isl_basic_map_free(bmap
);
3181 struct isl_basic_map
*isl_basic_map_apply_domain(
3182 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3184 if (!bmap1
|| !bmap2
)
3187 isl_assert(bmap1
->ctx
,
3188 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3189 isl_assert(bmap1
->ctx
,
3190 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3193 bmap1
= isl_basic_map_reverse(bmap1
);
3194 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3195 return isl_basic_map_reverse(bmap1
);
3197 isl_basic_map_free(bmap1
);
3198 isl_basic_map_free(bmap2
);
3202 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3203 * A \cap B -> f(A) + f(B)
3205 struct isl_basic_map
*isl_basic_map_sum(
3206 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3208 unsigned n_in
, n_out
, nparam
, total
, pos
;
3209 struct isl_basic_map
*bmap
= NULL
;
3210 struct isl_dim_map
*dim_map1
, *dim_map2
;
3213 if (!bmap1
|| !bmap2
)
3216 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3219 nparam
= isl_basic_map_n_param(bmap1
);
3220 n_in
= isl_basic_map_n_in(bmap1
);
3221 n_out
= isl_basic_map_n_out(bmap1
);
3223 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3224 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3225 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3226 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3227 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3228 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3229 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3230 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3231 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3232 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3233 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3235 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3236 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3237 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3238 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3239 for (i
= 0; i
< n_out
; ++i
) {
3240 int j
= isl_basic_map_alloc_equality(bmap
);
3243 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3244 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3245 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3246 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3248 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3249 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3250 bmap
= add_divs(bmap
, 2 * n_out
);
3252 bmap
= isl_basic_map_simplify(bmap
);
3253 return isl_basic_map_finalize(bmap
);
3255 isl_basic_map_free(bmap
);
3256 isl_basic_map_free(bmap1
);
3257 isl_basic_map_free(bmap2
);
3261 /* Given two maps A -> f(A) and B -> g(B), construct a map
3262 * A \cap B -> f(A) + f(B)
3264 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3266 struct isl_map
*result
;
3272 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3274 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3275 map1
->n
* map2
->n
, 0);
3278 for (i
= 0; i
< map1
->n
; ++i
)
3279 for (j
= 0; j
< map2
->n
; ++j
) {
3280 struct isl_basic_map
*part
;
3281 part
= isl_basic_map_sum(
3282 isl_basic_map_copy(map1
->p
[i
]),
3283 isl_basic_map_copy(map2
->p
[j
]));
3284 if (isl_basic_map_is_empty(part
))
3285 isl_basic_map_free(part
);
3287 result
= isl_map_add_basic_map(result
, part
);
3300 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3301 __isl_take isl_set
*set2
)
3303 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3306 /* Given a basic map A -> f(A), construct A -> -f(A).
3308 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3313 bmap
= isl_basic_map_cow(bmap
);
3317 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3318 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3319 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3320 for (j
= 0; j
< n
; ++j
)
3321 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3322 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3323 for (j
= 0; j
< n
; ++j
)
3324 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3325 for (i
= 0; i
< bmap
->n_div
; ++i
)
3326 for (j
= 0; j
< n
; ++j
)
3327 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3328 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3329 return isl_basic_map_finalize(bmap
);
3332 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3334 return isl_basic_map_neg(bset
);
3337 /* Given a map A -> f(A), construct A -> -f(A).
3339 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3343 map
= isl_map_cow(map
);
3347 for (i
= 0; i
< map
->n
; ++i
) {
3348 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3359 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3361 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3364 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3365 * A -> floor(f(A)/d).
3367 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3370 unsigned n_in
, n_out
, nparam
, total
, pos
;
3371 struct isl_basic_map
*result
= NULL
;
3372 struct isl_dim_map
*dim_map
;
3378 nparam
= isl_basic_map_n_param(bmap
);
3379 n_in
= isl_basic_map_n_in(bmap
);
3380 n_out
= isl_basic_map_n_out(bmap
);
3382 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3383 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3384 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3385 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3386 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3387 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3389 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3390 bmap
->n_div
+ n_out
,
3391 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3392 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3393 result
= add_divs(result
, n_out
);
3394 for (i
= 0; i
< n_out
; ++i
) {
3396 j
= isl_basic_map_alloc_inequality(result
);
3399 isl_seq_clr(result
->ineq
[j
], 1+total
);
3400 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3401 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3402 j
= isl_basic_map_alloc_inequality(result
);
3405 isl_seq_clr(result
->ineq
[j
], 1+total
);
3406 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3407 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3408 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3411 result
= isl_basic_map_simplify(result
);
3412 return isl_basic_map_finalize(result
);
3414 isl_basic_map_free(result
);
3418 /* Given a map A -> f(A) and an integer d, construct a map
3419 * A -> floor(f(A)/d).
3421 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3425 map
= isl_map_cow(map
);
3429 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3430 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3431 for (i
= 0; i
< map
->n
; ++i
) {
3432 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3443 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3449 i
= isl_basic_map_alloc_equality(bmap
);
3452 nparam
= isl_basic_map_n_param(bmap
);
3453 n_in
= isl_basic_map_n_in(bmap
);
3454 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3455 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3456 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3457 return isl_basic_map_finalize(bmap
);
3459 isl_basic_map_free(bmap
);
3463 /* Add a constraints to "bmap" expressing i_pos < o_pos
3465 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3471 i
= isl_basic_map_alloc_inequality(bmap
);
3474 nparam
= isl_basic_map_n_param(bmap
);
3475 n_in
= isl_basic_map_n_in(bmap
);
3476 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3477 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3478 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3479 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3480 return isl_basic_map_finalize(bmap
);
3482 isl_basic_map_free(bmap
);
3486 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3488 static __isl_give isl_basic_map
*var_less_or_equal(
3489 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3495 i
= isl_basic_map_alloc_inequality(bmap
);
3498 nparam
= isl_basic_map_n_param(bmap
);
3499 n_in
= isl_basic_map_n_in(bmap
);
3500 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3501 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3502 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3503 return isl_basic_map_finalize(bmap
);
3505 isl_basic_map_free(bmap
);
3509 /* Add a constraints to "bmap" expressing i_pos > o_pos
3511 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3517 i
= isl_basic_map_alloc_inequality(bmap
);
3520 nparam
= isl_basic_map_n_param(bmap
);
3521 n_in
= isl_basic_map_n_in(bmap
);
3522 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3523 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3524 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3525 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3526 return isl_basic_map_finalize(bmap
);
3528 isl_basic_map_free(bmap
);
3532 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3534 static __isl_give isl_basic_map
*var_more_or_equal(
3535 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3541 i
= isl_basic_map_alloc_inequality(bmap
);
3544 nparam
= isl_basic_map_n_param(bmap
);
3545 n_in
= isl_basic_map_n_in(bmap
);
3546 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3547 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3548 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3549 return isl_basic_map_finalize(bmap
);
3551 isl_basic_map_free(bmap
);
3555 __isl_give isl_basic_map
*isl_basic_map_equal(
3556 __isl_take isl_space
*dim
, unsigned n_equal
)
3559 struct isl_basic_map
*bmap
;
3560 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3563 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3564 bmap
= var_equal(bmap
, i
);
3565 return isl_basic_map_finalize(bmap
);
3568 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3570 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3574 struct isl_basic_map
*bmap
;
3575 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3578 for (i
= 0; i
< pos
&& bmap
; ++i
)
3579 bmap
= var_equal(bmap
, i
);
3581 bmap
= var_less(bmap
, pos
);
3582 return isl_basic_map_finalize(bmap
);
3585 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3587 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3588 __isl_take isl_space
*dim
, unsigned pos
)
3591 isl_basic_map
*bmap
;
3593 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3594 for (i
= 0; i
< pos
; ++i
)
3595 bmap
= var_equal(bmap
, i
);
3596 bmap
= var_less_or_equal(bmap
, pos
);
3597 return isl_basic_map_finalize(bmap
);
3600 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3602 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3606 struct isl_basic_map
*bmap
;
3607 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3610 for (i
= 0; i
< pos
&& bmap
; ++i
)
3611 bmap
= var_equal(bmap
, i
);
3613 bmap
= var_more(bmap
, pos
);
3614 return isl_basic_map_finalize(bmap
);
3617 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3619 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3620 __isl_take isl_space
*dim
, unsigned pos
)
3623 isl_basic_map
*bmap
;
3625 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3626 for (i
= 0; i
< pos
; ++i
)
3627 bmap
= var_equal(bmap
, i
);
3628 bmap
= var_more_or_equal(bmap
, pos
);
3629 return isl_basic_map_finalize(bmap
);
3632 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3633 unsigned n
, int equal
)
3635 struct isl_map
*map
;
3638 if (n
== 0 && equal
)
3639 return isl_map_universe(dims
);
3641 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3643 for (i
= 0; i
+ 1 < n
; ++i
)
3644 map
= isl_map_add_basic_map(map
,
3645 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3648 map
= isl_map_add_basic_map(map
,
3649 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3651 map
= isl_map_add_basic_map(map
,
3652 isl_basic_map_less_at(dims
, n
- 1));
3654 isl_space_free(dims
);
3659 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3663 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3666 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3668 return map_lex_lte_first(dim
, n
, 0);
3671 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3673 return map_lex_lte_first(dim
, n
, 1);
3676 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3678 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3681 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3683 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3686 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3687 unsigned n
, int equal
)
3689 struct isl_map
*map
;
3692 if (n
== 0 && equal
)
3693 return isl_map_universe(dims
);
3695 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3697 for (i
= 0; i
+ 1 < n
; ++i
)
3698 map
= isl_map_add_basic_map(map
,
3699 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3702 map
= isl_map_add_basic_map(map
,
3703 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3705 map
= isl_map_add_basic_map(map
,
3706 isl_basic_map_more_at(dims
, n
- 1));
3708 isl_space_free(dims
);
3713 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3717 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3720 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3722 return map_lex_gte_first(dim
, n
, 0);
3725 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3727 return map_lex_gte_first(dim
, n
, 1);
3730 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3732 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3735 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3737 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3740 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3741 __isl_take isl_set
*set2
)
3744 map
= isl_map_lex_le(isl_set_get_space(set1
));
3745 map
= isl_map_intersect_domain(map
, set1
);
3746 map
= isl_map_intersect_range(map
, set2
);
3750 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3751 __isl_take isl_set
*set2
)
3754 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3755 map
= isl_map_intersect_domain(map
, set1
);
3756 map
= isl_map_intersect_range(map
, set2
);
3760 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3761 __isl_take isl_set
*set2
)
3764 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3765 map
= isl_map_intersect_domain(map
, set1
);
3766 map
= isl_map_intersect_range(map
, set2
);
3770 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3771 __isl_take isl_set
*set2
)
3774 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3775 map
= isl_map_intersect_domain(map
, set1
);
3776 map
= isl_map_intersect_range(map
, set2
);
3780 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3781 __isl_take isl_map
*map2
)
3784 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3785 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3786 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3790 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3791 __isl_take isl_map
*map2
)
3794 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3795 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3796 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3800 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3801 __isl_take isl_map
*map2
)
3804 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3805 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3806 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3810 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3811 __isl_take isl_map
*map2
)
3814 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3815 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3816 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3820 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3821 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3823 struct isl_basic_map
*bmap
;
3825 bset
= isl_basic_set_cow(bset
);
3829 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3830 isl_space_free(bset
->dim
);
3831 bmap
= (struct isl_basic_map
*) bset
;
3833 return isl_basic_map_finalize(bmap
);
3835 isl_basic_set_free(bset
);
3836 isl_space_free(dim
);
3840 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3844 if (bmap
->dim
->n_in
== 0)
3845 return (struct isl_basic_set
*)bmap
;
3846 bmap
= isl_basic_map_cow(bmap
);
3849 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3852 bmap
= isl_basic_map_finalize(bmap
);
3853 return (struct isl_basic_set
*)bmap
;
3855 isl_basic_map_free(bmap
);
3859 /* For a div d = floor(f/m), add the constraints
3862 * -(f-(n-1)) + m d >= 0
3864 * Note that the second constraint is the negation of
3868 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3869 unsigned pos
, isl_int
*div
)
3872 unsigned total
= isl_basic_map_total_dim(bmap
);
3874 i
= isl_basic_map_alloc_inequality(bmap
);
3877 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3878 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3880 j
= isl_basic_map_alloc_inequality(bmap
);
3883 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3884 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3885 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3889 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3890 unsigned pos
, isl_int
*div
)
3892 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3896 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3898 unsigned total
= isl_basic_map_total_dim(bmap
);
3899 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3901 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3905 struct isl_basic_set
*isl_basic_map_underlying_set(
3906 struct isl_basic_map
*bmap
)
3910 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3912 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3913 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3914 return (struct isl_basic_set
*)bmap
;
3915 bmap
= isl_basic_map_cow(bmap
);
3918 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3921 bmap
->extra
-= bmap
->n_div
;
3923 bmap
= isl_basic_map_finalize(bmap
);
3924 return (struct isl_basic_set
*)bmap
;
3929 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3930 __isl_take isl_basic_set
*bset
)
3932 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3935 struct isl_basic_map
*isl_basic_map_overlying_set(
3936 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3938 struct isl_basic_map
*bmap
;
3939 struct isl_ctx
*ctx
;
3946 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3947 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3948 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3950 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3951 isl_basic_map_free(like
);
3952 return (struct isl_basic_map
*)bset
;
3954 bset
= isl_basic_set_cow(bset
);
3957 total
= bset
->dim
->n_out
+ bset
->extra
;
3958 bmap
= (struct isl_basic_map
*)bset
;
3959 isl_space_free(bmap
->dim
);
3960 bmap
->dim
= isl_space_copy(like
->dim
);
3963 bmap
->n_div
= like
->n_div
;
3964 bmap
->extra
+= like
->n_div
;
3968 ltotal
= total
- bmap
->extra
+ like
->extra
;
3971 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3972 bmap
->extra
* (1 + 1 + total
));
3973 if (isl_blk_is_error(bmap
->block2
))
3975 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3979 for (i
= 0; i
< bmap
->extra
; ++i
)
3980 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3981 for (i
= 0; i
< like
->n_div
; ++i
) {
3982 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3983 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3985 bmap
= isl_basic_map_extend_constraints(bmap
,
3986 0, 2 * like
->n_div
);
3987 for (i
= 0; i
< like
->n_div
; ++i
) {
3988 if (isl_int_is_zero(bmap
->div
[i
][0]))
3990 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3994 isl_basic_map_free(like
);
3995 bmap
= isl_basic_map_simplify(bmap
);
3996 bmap
= isl_basic_map_finalize(bmap
);
3999 isl_basic_map_free(like
);
4000 isl_basic_set_free(bset
);
4004 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4005 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4007 return (struct isl_basic_set
*)
4008 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4011 struct isl_set
*isl_set_from_underlying_set(
4012 struct isl_set
*set
, struct isl_basic_set
*like
)
4018 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4020 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4021 isl_basic_set_free(like
);
4024 set
= isl_set_cow(set
);
4027 for (i
= 0; i
< set
->n
; ++i
) {
4028 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4029 isl_basic_set_copy(like
));
4033 isl_space_free(set
->dim
);
4034 set
->dim
= isl_space_copy(like
->dim
);
4037 isl_basic_set_free(like
);
4040 isl_basic_set_free(like
);
4045 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4049 map
= isl_map_cow(map
);
4052 map
->dim
= isl_space_cow(map
->dim
);
4056 for (i
= 1; i
< map
->n
; ++i
)
4057 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4059 for (i
= 0; i
< map
->n
; ++i
) {
4060 map
->p
[i
] = (struct isl_basic_map
*)
4061 isl_basic_map_underlying_set(map
->p
[i
]);
4066 map
->dim
= isl_space_underlying(map
->dim
, 0);
4068 isl_space_free(map
->dim
);
4069 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4073 return (struct isl_set
*)map
;
4079 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4081 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4084 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4085 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4087 bmap
= isl_basic_map_cow(bmap
);
4091 isl_space_free(bmap
->dim
);
4094 bmap
= isl_basic_map_finalize(bmap
);
4098 isl_basic_map_free(bmap
);
4099 isl_space_free(dim
);
4103 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4104 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4106 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4110 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4111 __isl_take isl_space
*dim
)
4115 map
= isl_map_cow(map
);
4119 for (i
= 0; i
< map
->n
; ++i
) {
4120 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4121 isl_space_copy(dim
));
4125 isl_space_free(map
->dim
);
4131 isl_space_free(dim
);
4135 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4136 __isl_take isl_space
*dim
)
4138 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4141 /* Compute the parameter domain of the given basic set.
4143 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4148 if (isl_basic_set_is_params(bset
))
4151 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4152 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4153 space
= isl_basic_set_get_space(bset
);
4154 space
= isl_space_params(space
);
4155 bset
= isl_basic_set_reset_space(bset
, space
);
4159 /* Compute the parameter domain of the given set.
4161 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4166 if (isl_set_is_params(set
))
4169 n
= isl_set_dim(set
, isl_dim_set
);
4170 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4171 space
= isl_set_get_space(set
);
4172 space
= isl_space_params(space
);
4173 set
= isl_set_reset_space(set
, space
);
4177 /* Construct a zero-dimensional set with the given parameter domain.
4179 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4182 space
= isl_set_get_space(set
);
4183 space
= isl_space_set_from_params(space
);
4184 set
= isl_set_reset_space(set
, space
);
4188 /* Compute the parameter domain of the given map.
4190 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4195 n
= isl_map_dim(map
, isl_dim_in
);
4196 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4197 n
= isl_map_dim(map
, isl_dim_out
);
4198 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4199 space
= isl_map_get_space(map
);
4200 space
= isl_space_params(space
);
4201 map
= isl_map_reset_space(map
, space
);
4205 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4208 struct isl_basic_set
*domain
;
4214 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4216 n_in
= isl_basic_map_n_in(bmap
);
4217 n_out
= isl_basic_map_n_out(bmap
);
4218 domain
= isl_basic_set_from_basic_map(bmap
);
4219 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4221 domain
= isl_basic_set_reset_space(domain
, dim
);
4226 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4230 return isl_space_may_be_set(bmap
->dim
);
4233 /* Is this basic map actually a set?
4234 * Users should never call this function. Outside of isl,
4235 * the type should indicate whether something is a set or a map.
4237 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4241 return isl_space_is_set(bmap
->dim
);
4244 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4248 if (isl_basic_map_is_set(bmap
))
4250 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4253 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4254 __isl_take isl_basic_map
*bmap
)
4258 isl_basic_map
*domain
;
4259 int nparam
, n_in
, n_out
;
4262 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4263 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4264 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4266 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4267 domain
= isl_basic_map_universe(dim
);
4269 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4270 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4271 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4273 total
= isl_basic_map_total_dim(bmap
);
4275 for (i
= 0; i
< n_in
; ++i
) {
4276 k
= isl_basic_map_alloc_equality(bmap
);
4279 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4280 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4281 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4284 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4285 return isl_basic_map_finalize(bmap
);
4287 isl_basic_map_free(bmap
);
4291 __isl_give isl_basic_map
*isl_basic_map_range_map(
4292 __isl_take isl_basic_map
*bmap
)
4296 isl_basic_map
*range
;
4297 int nparam
, n_in
, n_out
;
4300 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4301 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4302 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4304 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4305 range
= isl_basic_map_universe(dim
);
4307 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4308 bmap
= isl_basic_map_apply_range(bmap
, range
);
4309 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4311 total
= isl_basic_map_total_dim(bmap
);
4313 for (i
= 0; i
< n_out
; ++i
) {
4314 k
= isl_basic_map_alloc_equality(bmap
);
4317 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4318 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4319 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4322 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4323 return isl_basic_map_finalize(bmap
);
4325 isl_basic_map_free(bmap
);
4329 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4333 return isl_space_may_be_set(map
->dim
);
4336 /* Is this map actually a set?
4337 * Users should never call this function. Outside of isl,
4338 * the type should indicate whether something is a set or a map.
4340 int isl_map_is_set(__isl_keep isl_map
*map
)
4344 return isl_space_is_set(map
->dim
);
4347 struct isl_set
*isl_map_range(struct isl_map
*map
)
4350 struct isl_set
*set
;
4354 if (isl_map_is_set(map
))
4355 return (isl_set
*)map
;
4357 map
= isl_map_cow(map
);
4361 set
= (struct isl_set
*) map
;
4362 set
->dim
= isl_space_range(set
->dim
);
4365 for (i
= 0; i
< map
->n
; ++i
) {
4366 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4370 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4371 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4378 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4381 isl_space
*domain_dim
;
4383 map
= isl_map_cow(map
);
4387 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4388 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4389 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4392 for (i
= 0; i
< map
->n
; ++i
) {
4393 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4397 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4398 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4405 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4408 isl_space
*range_dim
;
4410 map
= isl_map_cow(map
);
4414 range_dim
= isl_space_range(isl_map_get_space(map
));
4415 range_dim
= isl_space_from_range(range_dim
);
4416 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4417 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4420 for (i
= 0; i
< map
->n
; ++i
) {
4421 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4425 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4426 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4433 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4434 __isl_take isl_space
*dim
)
4437 struct isl_map
*map
= NULL
;
4439 set
= isl_set_cow(set
);
4442 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4443 map
= (struct isl_map
*)set
;
4444 for (i
= 0; i
< set
->n
; ++i
) {
4445 map
->p
[i
] = isl_basic_map_from_basic_set(
4446 set
->p
[i
], isl_space_copy(dim
));
4450 isl_space_free(map
->dim
);
4454 isl_space_free(dim
);
4459 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4460 __isl_take isl_basic_set
*bset
)
4462 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4465 __isl_give isl_basic_map
*isl_basic_map_from_range(
4466 __isl_take isl_basic_set
*bset
)
4469 space
= isl_basic_set_get_space(bset
);
4470 space
= isl_space_from_range(space
);
4471 bset
= isl_basic_set_reset_space(bset
, space
);
4472 return (isl_basic_map
*)bset
;
4475 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4478 space
= isl_set_get_space(set
);
4479 space
= isl_space_from_range(space
);
4480 set
= isl_set_reset_space(set
, space
);
4481 return (struct isl_map
*)set
;
4484 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4486 return isl_map_reverse(isl_map_from_range(set
));
4489 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4490 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4492 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4495 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4496 __isl_take isl_set
*range
)
4498 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4501 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4504 struct isl_set
*set
= NULL
;
4508 map
= isl_map_cow(map
);
4511 map
->dim
= isl_space_as_set_space(map
->dim
);
4514 set
= (struct isl_set
*)map
;
4515 for (i
= 0; i
< map
->n
; ++i
) {
4516 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4526 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4529 struct isl_map
*map
;
4534 isl_die(dim
->ctx
, isl_error_internal
,
4535 "negative number of basic maps", goto error
);
4536 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4537 sizeof(struct isl_map
) +
4538 (n
- 1) * sizeof(struct isl_basic_map
*));
4542 map
->ctx
= dim
->ctx
;
4543 isl_ctx_ref(map
->ctx
);
4551 isl_space_free(dim
);
4555 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4556 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4559 struct isl_map
*map
;
4562 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4566 map
= isl_map_alloc_space(dims
, n
, flags
);
4570 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4572 struct isl_basic_map
*bmap
;
4573 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4574 bmap
= isl_basic_map_set_to_empty(bmap
);
4578 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4580 struct isl_basic_set
*bset
;
4581 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4582 bset
= isl_basic_set_set_to_empty(bset
);
4586 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4588 struct isl_basic_map
*bmap
;
4591 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4592 bmap
= isl_basic_map_set_to_empty(bmap
);
4596 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4598 struct isl_basic_map
*bmap
;
4601 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4602 bmap
= isl_basic_map_set_to_empty(bmap
);
4606 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4608 struct isl_basic_set
*bset
;
4611 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4612 bset
= isl_basic_set_set_to_empty(bset
);
4616 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4618 struct isl_basic_map
*bmap
;
4619 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4620 bmap
= isl_basic_map_finalize(bmap
);
4624 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4626 struct isl_basic_set
*bset
;
4627 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4628 bset
= isl_basic_set_finalize(bset
);
4632 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4635 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4636 isl_basic_map
*bmap
;
4638 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4639 for (i
= 0; i
< total
; ++i
) {
4640 int k
= isl_basic_map_alloc_inequality(bmap
);
4643 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4644 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4648 isl_basic_map_free(bmap
);
4652 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4654 return isl_basic_map_nat_universe(dim
);
4657 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4659 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4662 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4664 return isl_map_nat_universe(dim
);
4667 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4668 __isl_keep isl_basic_map
*model
)
4672 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4675 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4679 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4682 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4683 __isl_keep isl_set
*model
)
4687 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4690 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4692 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4695 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4699 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4702 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4706 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4709 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4711 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4714 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4718 return isl_set_empty(isl_space_copy(model
->dim
));
4721 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4723 struct isl_map
*map
;
4726 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4727 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4731 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4733 struct isl_set
*set
;
4736 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4737 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4741 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4745 return isl_set_universe(isl_space_copy(model
->dim
));
4748 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4751 struct isl_map
*dup
;
4755 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4756 for (i
= 0; i
< map
->n
; ++i
)
4757 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4761 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4762 __isl_take isl_basic_map
*bmap
)
4766 if (isl_basic_map_plain_is_empty(bmap
)) {
4767 isl_basic_map_free(bmap
);
4770 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4771 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4772 map
->p
[map
->n
] = bmap
;
4774 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4780 isl_basic_map_free(bmap
);
4784 void isl_map_free(struct isl_map
*map
)
4794 isl_ctx_deref(map
->ctx
);
4795 for (i
= 0; i
< map
->n
; ++i
)
4796 isl_basic_map_free(map
->p
[i
]);
4797 isl_space_free(map
->dim
);
4801 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4802 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4806 base
= isl_map_cow(base
);
4810 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4813 for (i
= 0; i
< base
->n
; ++i
) {
4814 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4815 isl_space_copy(base
->dim
), 0, 0, 0);
4825 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4826 unsigned nparam
, unsigned dim
)
4828 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4832 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4833 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4837 bmap
= isl_basic_map_cow(bmap
);
4838 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4839 j
= isl_basic_map_alloc_equality(bmap
);
4842 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4843 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4844 isl_int_set_si(bmap
->eq
[j
][0], value
);
4845 bmap
= isl_basic_map_simplify(bmap
);
4846 return isl_basic_map_finalize(bmap
);
4848 isl_basic_map_free(bmap
);
4852 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4853 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4857 bmap
= isl_basic_map_cow(bmap
);
4858 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4859 j
= isl_basic_map_alloc_equality(bmap
);
4862 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4863 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4864 isl_int_set(bmap
->eq
[j
][0], value
);
4865 bmap
= isl_basic_map_simplify(bmap
);
4866 return isl_basic_map_finalize(bmap
);
4868 isl_basic_map_free(bmap
);
4872 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4873 enum isl_dim_type type
, unsigned pos
, int value
)
4877 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4878 return isl_basic_map_fix_pos_si(bmap
,
4879 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4881 isl_basic_map_free(bmap
);
4885 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4886 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4890 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4891 return isl_basic_map_fix_pos(bmap
,
4892 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4894 isl_basic_map_free(bmap
);
4898 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4899 enum isl_dim_type type
, unsigned pos
, int value
)
4901 return (struct isl_basic_set
*)
4902 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4906 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4907 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4909 return (struct isl_basic_set
*)
4910 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4914 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4915 unsigned input
, int value
)
4917 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4920 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4921 unsigned dim
, int value
)
4923 return (struct isl_basic_set
*)
4924 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4925 isl_dim_set
, dim
, value
);
4928 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4929 enum isl_dim_type type
, unsigned pos
, int value
)
4933 map
= isl_map_cow(map
);
4937 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4938 for (i
= 0; i
< map
->n
; ++i
) {
4939 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4943 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4950 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4951 enum isl_dim_type type
, unsigned pos
, int value
)
4953 return (struct isl_set
*)
4954 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4957 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4958 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4962 map
= isl_map_cow(map
);
4966 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4967 for (i
= 0; i
< map
->n
; ++i
) {
4968 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4972 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4979 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4980 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4982 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4985 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4986 unsigned input
, int value
)
4988 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4991 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4993 return (struct isl_set
*)
4994 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4997 static __isl_give isl_basic_map
*basic_map_bound_si(
4998 __isl_take isl_basic_map
*bmap
,
4999 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5005 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5006 pos
+= isl_basic_map_offset(bmap
, type
);
5007 bmap
= isl_basic_map_cow(bmap
);
5008 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5009 j
= isl_basic_map_alloc_inequality(bmap
);
5012 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5014 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5015 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5017 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5018 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5020 bmap
= isl_basic_map_simplify(bmap
);
5021 return isl_basic_map_finalize(bmap
);
5023 isl_basic_map_free(bmap
);
5027 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5028 __isl_take isl_basic_map
*bmap
,
5029 enum isl_dim_type type
, unsigned pos
, int value
)
5031 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5034 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5035 unsigned dim
, isl_int value
)
5039 bset
= isl_basic_set_cow(bset
);
5040 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5041 j
= isl_basic_set_alloc_inequality(bset
);
5044 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5045 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5046 isl_int_neg(bset
->ineq
[j
][0], value
);
5047 bset
= isl_basic_set_simplify(bset
);
5048 return isl_basic_set_finalize(bset
);
5050 isl_basic_set_free(bset
);
5054 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5055 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5059 map
= isl_map_cow(map
);
5063 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5064 for (i
= 0; i
< map
->n
; ++i
) {
5065 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5066 type
, pos
, value
, upper
);
5070 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5077 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5078 enum isl_dim_type type
, unsigned pos
, int value
)
5080 return map_bound_si(map
, type
, pos
, value
, 0);
5083 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5084 enum isl_dim_type type
, unsigned pos
, int value
)
5086 return map_bound_si(map
, type
, pos
, value
, 1);
5089 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5090 enum isl_dim_type type
, unsigned pos
, int value
)
5092 return (struct isl_set
*)
5093 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5096 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5097 enum isl_dim_type type
, unsigned pos
, int value
)
5099 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5102 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5107 set
= isl_set_cow(set
);
5111 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5112 for (i
= 0; i
< set
->n
; ++i
) {
5113 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5123 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5127 map
= isl_map_cow(map
);
5131 map
->dim
= isl_space_reverse(map
->dim
);
5134 for (i
= 0; i
< map
->n
; ++i
) {
5135 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5139 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5146 static struct isl_map
*isl_basic_map_partial_lexopt(
5147 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5148 struct isl_set
**empty
, int max
)
5152 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5153 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5155 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5157 isl_basic_map_free(bmap
);
5158 isl_basic_set_free(dom
);
5164 struct isl_map
*isl_basic_map_partial_lexmax(
5165 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5166 struct isl_set
**empty
)
5168 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5171 struct isl_map
*isl_basic_map_partial_lexmin(
5172 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5173 struct isl_set
**empty
)
5175 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5178 struct isl_set
*isl_basic_set_partial_lexmin(
5179 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5180 struct isl_set
**empty
)
5182 return (struct isl_set
*)
5183 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5187 struct isl_set
*isl_basic_set_partial_lexmax(
5188 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5189 struct isl_set
**empty
)
5191 return (struct isl_set
*)
5192 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5196 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5197 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5198 __isl_give isl_set
**empty
)
5200 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5203 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5204 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5205 __isl_give isl_set
**empty
)
5207 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5210 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5211 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5212 __isl_give isl_set
**empty
)
5214 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5217 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5218 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5219 __isl_give isl_set
**empty
)
5221 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5224 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5225 __isl_take isl_basic_map
*bmap
, int max
)
5227 isl_basic_set
*dom
= NULL
;
5228 isl_space
*dom_space
;
5232 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5233 dom
= isl_basic_set_universe(dom_space
);
5234 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5236 isl_basic_map_free(bmap
);
5240 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5241 __isl_take isl_basic_map
*bmap
)
5243 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5246 /* Given a basic map "bmap", compute the lexicographically minimal
5247 * (or maximal) image element for each domain element in dom.
5248 * Set *empty to those elements in dom that do not have an image element.
5250 * We first make sure the basic sets in dom are disjoint and then
5251 * simply collect the results over each of the basic sets separately.
5252 * We could probably improve the efficiency a bit by moving the union
5253 * domain down into the parametric integer programming.
5255 static __isl_give isl_map
*basic_map_partial_lexopt(
5256 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5257 __isl_give isl_set
**empty
, int max
)
5260 struct isl_map
*res
;
5262 dom
= isl_set_make_disjoint(dom
);
5266 if (isl_set_plain_is_empty(dom
)) {
5267 res
= isl_map_empty_like_basic_map(bmap
);
5268 *empty
= isl_set_empty_like(dom
);
5270 isl_basic_map_free(bmap
);
5274 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5275 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5277 for (i
= 1; i
< dom
->n
; ++i
) {
5278 struct isl_map
*res_i
;
5279 struct isl_set
*empty_i
;
5281 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5282 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5284 res
= isl_map_union_disjoint(res
, res_i
);
5285 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5289 isl_basic_map_free(bmap
);
5294 isl_basic_map_free(bmap
);
5298 /* Given a map "map", compute the lexicographically minimal
5299 * (or maximal) image element for each domain element in dom.
5300 * Set *empty to those elements in dom that do not have an image element.
5302 * We first compute the lexicographically minimal or maximal element
5303 * in the first basic map. This results in a partial solution "res"
5304 * and a subset "todo" of dom that still need to be handled.
5305 * We then consider each of the remaining maps in "map" and successively
5306 * improve both "res" and "todo".
5308 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5309 * Assume we are computing the lexicographical maximum.
5310 * We first compute the lexicographically maximal element in basic map i.
5311 * This results in a partial solution res_i and a subset todo_i.
5312 * Then we combine these results with those obtain for the first k basic maps
5313 * to obtain a result that is valid for the first k+1 basic maps.
5314 * In particular, the set where there is no solution is the set where
5315 * there is no solution for the first k basic maps and also no solution
5316 * for the ith basic map, i.e.,
5318 * todo^i = todo^k * todo_i
5320 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5321 * solutions, arbitrarily breaking ties in favor of res^k.
5322 * That is, when res^k(a) >= res_i(a), we pick res^k and
5323 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5324 * the lexicographic order.)
5325 * In practice, we compute
5327 * res^k * (res_i . "<=")
5331 * res_i * (res^k . "<")
5333 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5334 * where only one of res^k and res_i provides a solution and we simply pick
5341 * Note that we only compute these intersections when dom(res^k) intersects
5342 * dom(res_i). Otherwise, the only effect of these intersections is to
5343 * potentially break up res^k and res_i into smaller pieces.
5344 * We want to avoid such splintering as much as possible.
5345 * In fact, an earlier implementation of this function would look for
5346 * better results in the domain of res^k and for extra results in todo^k,
5347 * but this would always result in a splintering according to todo^k,
5348 * even when the domain of basic map i is disjoint from the domains of
5349 * the previous basic maps.
5351 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5352 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5353 __isl_give isl_set
**empty
, int max
)
5356 struct isl_map
*res
;
5357 struct isl_set
*todo
;
5362 if (isl_map_plain_is_empty(map
)) {
5370 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5371 isl_set_copy(dom
), &todo
, max
);
5373 for (i
= 1; i
< map
->n
; ++i
) {
5377 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5379 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5380 isl_set_copy(dom
), &todo_i
, max
);
5383 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5384 le
= isl_map_lex_le(dim
);
5386 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5387 le
= isl_map_lex_ge(dim
);
5389 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5390 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5391 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5392 le
= isl_map_intersect(le
, isl_map_copy(res
));
5394 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5395 res
= isl_map_intersect_domain(res
,
5396 isl_set_copy(todo_i
));
5397 res_i
= isl_map_intersect_domain(res_i
,
5398 isl_set_copy(todo
));
5401 res
= isl_map_union_disjoint(res
, res_i
);
5402 res
= isl_map_union_disjoint(res
, lt
);
5403 res
= isl_map_union_disjoint(res
, le
);
5405 todo
= isl_set_intersect(todo
, todo_i
);
5425 /* Given a map "map", compute the lexicographically minimal
5426 * (or maximal) image element for each domain element in dom.
5427 * Set *empty to those elements in dom that do not have an image element.
5429 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5431 static __isl_give isl_map
*isl_map_partial_lexopt(
5432 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5433 __isl_give isl_set
**empty
, int max
)
5437 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5438 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5439 if (!isl_space_has_named_params(map
->dim
) ||
5440 !isl_space_has_named_params(dom
->dim
))
5441 isl_die(map
->ctx
, isl_error_invalid
,
5442 "unaligned unnamed parameters", goto error
);
5443 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5444 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5445 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5454 __isl_give isl_map
*isl_map_partial_lexmax(
5455 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5456 __isl_give isl_set
**empty
)
5458 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5461 __isl_give isl_map
*isl_map_partial_lexmin(
5462 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5463 __isl_give isl_set
**empty
)
5465 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5468 __isl_give isl_set
*isl_set_partial_lexmin(
5469 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5470 __isl_give isl_set
**empty
)
5472 return (struct isl_set
*)
5473 isl_map_partial_lexmin((struct isl_map
*)set
,
5477 __isl_give isl_set
*isl_set_partial_lexmax(
5478 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5479 __isl_give isl_set
**empty
)
5481 return (struct isl_set
*)
5482 isl_map_partial_lexmax((struct isl_map
*)set
,
5486 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5488 struct isl_basic_set
*dom
= NULL
;
5493 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5494 dom
= isl_basic_set_universe(dom_dim
);
5495 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5497 isl_basic_map_free(bmap
);
5501 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5503 return isl_basic_map_lexopt(bmap
, 0);
5506 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5508 return isl_basic_map_lexopt(bmap
, 1);
5511 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5513 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5516 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5518 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5521 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5523 struct isl_set
*dom
= NULL
;
5528 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5529 dom
= isl_set_universe(dom_dim
);
5530 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5536 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5538 return isl_map_lexopt(map
, 0);
5541 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5543 return isl_map_lexopt(map
, 1);
5546 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5548 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5551 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5553 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5556 /* Extract the first and only affine expression from list
5557 * and then add it to *pwaff with the given dom.
5558 * This domain is known to be disjoint from other domains
5559 * because of the way isl_basic_map_foreach_lexmax works.
5561 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5562 __isl_take isl_aff_list
*list
, void *user
)
5564 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5566 isl_pw_aff
**pwaff
= user
;
5567 isl_pw_aff
*pwaff_i
;
5569 if (isl_aff_list_n_aff(list
) != 1)
5570 isl_die(ctx
, isl_error_internal
,
5571 "expecting single element list", goto error
);
5573 aff
= isl_aff_list_get_aff(list
, 0);
5574 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5576 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5578 isl_aff_list_free(list
);
5582 isl_basic_set_free(dom
);
5583 isl_aff_list_free(list
);
5587 /* Given a basic map with one output dimension, compute the minimum or
5588 * maximum of that dimension as an isl_pw_aff.
5590 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5591 * call update_dim_opt on each leaf of the result.
5593 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5596 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5600 dim
= isl_space_from_domain(isl_space_domain(dim
));
5601 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5602 pwaff
= isl_pw_aff_empty(dim
);
5604 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5606 return isl_pw_aff_free(pwaff
);
5611 /* Compute the minimum or maximum of the given output dimension
5612 * as a function of the parameters and the input dimensions,
5613 * but independently of the other output dimensions.
5615 * We first project out the other output dimension and then compute
5616 * the "lexicographic" maximum in each basic map, combining the results
5617 * using isl_pw_aff_union_max.
5619 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5626 n_out
= isl_map_dim(map
, isl_dim_out
);
5627 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5628 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5633 isl_space
*dim
= isl_map_get_space(map
);
5634 dim
= isl_space_domain(isl_space_from_range(dim
));
5636 return isl_pw_aff_empty(dim
);
5639 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5640 for (i
= 1; i
< map
->n
; ++i
) {
5641 isl_pw_aff
*pwaff_i
;
5643 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5644 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5652 /* Compute the maximum of the given output dimension as a function of the
5653 * parameters and input dimensions, but independently of
5654 * the other output dimensions.
5656 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5658 return map_dim_opt(map
, pos
, 1);
5661 /* Compute the minimum or maximum of the given set dimension
5662 * as a function of the parameters,
5663 * but independently of the other set dimensions.
5665 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5668 return map_dim_opt(set
, pos
, max
);
5671 /* Compute the maximum of the given set dimension as a function of the
5672 * parameters, but independently of the other set dimensions.
5674 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5676 return set_dim_opt(set
, pos
, 1);
5679 /* Compute the minimum of the given set dimension as a function of the
5680 * parameters, but independently of the other set dimensions.
5682 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5684 return set_dim_opt(set
, pos
, 0);
5687 /* Apply a preimage specified by "mat" on the parameters of "bset".
5688 * bset is assumed to have only parameters and divs.
5690 static struct isl_basic_set
*basic_set_parameter_preimage(
5691 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5698 bset
->dim
= isl_space_cow(bset
->dim
);
5702 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5704 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5706 bset
->dim
->nparam
= 0;
5707 bset
->dim
->n_out
= nparam
;
5708 bset
= isl_basic_set_preimage(bset
, mat
);
5710 bset
->dim
->nparam
= bset
->dim
->n_out
;
5711 bset
->dim
->n_out
= 0;
5716 isl_basic_set_free(bset
);
5720 /* Apply a preimage specified by "mat" on the parameters of "set".
5721 * set is assumed to have only parameters and divs.
5723 static struct isl_set
*set_parameter_preimage(
5724 struct isl_set
*set
, struct isl_mat
*mat
)
5726 isl_space
*dim
= NULL
;
5732 dim
= isl_space_copy(set
->dim
);
5733 dim
= isl_space_cow(dim
);
5737 nparam
= isl_set_dim(set
, isl_dim_param
);
5739 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5742 dim
->n_out
= nparam
;
5743 isl_set_reset_space(set
, dim
);
5744 set
= isl_set_preimage(set
, mat
);
5747 dim
= isl_space_copy(set
->dim
);
5748 dim
= isl_space_cow(dim
);
5751 dim
->nparam
= dim
->n_out
;
5753 isl_set_reset_space(set
, dim
);
5756 isl_space_free(dim
);
5763 /* Intersect the basic set "bset" with the affine space specified by the
5764 * equalities in "eq".
5766 static struct isl_basic_set
*basic_set_append_equalities(
5767 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5775 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5780 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5781 for (i
= 0; i
< eq
->n_row
; ++i
) {
5782 k
= isl_basic_set_alloc_equality(bset
);
5785 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5786 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5790 bset
= isl_basic_set_gauss(bset
, NULL
);
5791 bset
= isl_basic_set_finalize(bset
);
5796 isl_basic_set_free(bset
);
5800 /* Intersect the set "set" with the affine space specified by the
5801 * equalities in "eq".
5803 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5811 for (i
= 0; i
< set
->n
; ++i
) {
5812 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5825 /* Project the given basic set onto its parameter domain, possibly introducing
5826 * new, explicit, existential variables in the constraints.
5827 * The input has parameters and (possibly implicit) existential variables.
5828 * The output has the same parameters, but only
5829 * explicit existentially quantified variables.
5831 * The actual projection is performed by pip, but pip doesn't seem
5832 * to like equalities very much, so we first remove the equalities
5833 * among the parameters by performing a variable compression on
5834 * the parameters. Afterward, an inverse transformation is performed
5835 * and the equalities among the parameters are inserted back in.
5837 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5841 struct isl_mat
*T
, *T2
;
5842 struct isl_set
*set
;
5843 unsigned nparam
, n_div
;
5845 bset
= isl_basic_set_cow(bset
);
5849 if (bset
->n_eq
== 0)
5850 return isl_basic_set_lexmin(bset
);
5852 isl_basic_set_gauss(bset
, NULL
);
5854 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5855 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5857 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5858 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5861 if (i
== bset
->n_eq
)
5862 return isl_basic_set_lexmin(bset
);
5864 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5866 eq
= isl_mat_cow(eq
);
5867 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5868 if (T
&& T
->n_col
== 0) {
5872 bset
= isl_basic_set_set_to_empty(bset
);
5873 return isl_set_from_basic_set(bset
);
5875 bset
= basic_set_parameter_preimage(bset
, T
);
5877 set
= isl_basic_set_lexmin(bset
);
5878 set
= set_parameter_preimage(set
, T2
);
5879 set
= set_append_equalities(set
, eq
);
5883 /* Compute an explicit representation for all the existentially
5884 * quantified variables.
5885 * The input and output dimensions are first turned into parameters.
5886 * compute_divs then returns a map with the same parameters and
5887 * no input or output dimensions and the dimension specification
5888 * is reset to that of the input.
5890 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5892 struct isl_basic_set
*bset
;
5893 struct isl_set
*set
;
5894 struct isl_map
*map
;
5895 isl_space
*dim
, *orig_dim
= NULL
;
5900 bmap
= isl_basic_map_cow(bmap
);
5904 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5905 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5906 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5907 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5911 orig_dim
= bmap
->dim
;
5913 bset
= (struct isl_basic_set
*)bmap
;
5915 set
= parameter_compute_divs(bset
);
5916 map
= (struct isl_map
*)set
;
5917 map
= isl_map_reset_space(map
, orig_dim
);
5921 isl_basic_map_free(bmap
);
5925 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5933 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5934 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5935 if (isl_int_is_zero(bmap
->div
[i
][0]))
5937 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5943 static int map_divs_known(__isl_keep isl_map
*map
)
5950 for (i
= 0; i
< map
->n
; ++i
) {
5951 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5959 /* If bmap contains any unknown divs, then compute explicit
5960 * expressions for them. However, this computation may be
5961 * quite expensive, so first try to remove divs that aren't
5964 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5967 struct isl_map
*map
;
5969 known
= isl_basic_map_divs_known(bmap
);
5973 return isl_map_from_basic_map(bmap
);
5975 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5977 known
= isl_basic_map_divs_known(bmap
);
5981 return isl_map_from_basic_map(bmap
);
5983 map
= compute_divs(bmap
);
5986 isl_basic_map_free(bmap
);
5990 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5994 struct isl_map
*res
;
6001 known
= map_divs_known(map
);
6009 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6010 for (i
= 1 ; i
< map
->n
; ++i
) {
6012 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6013 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6014 res
= isl_map_union_disjoint(res
, r2
);
6016 res
= isl_map_union(res
, r2
);
6023 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6025 return (struct isl_set
*)
6026 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6029 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6031 return (struct isl_set
*)
6032 isl_map_compute_divs((struct isl_map
*)set
);
6035 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6038 struct isl_set
*set
;
6043 map
= isl_map_cow(map
);
6047 set
= (struct isl_set
*)map
;
6048 set
->dim
= isl_space_domain(set
->dim
);
6051 for (i
= 0; i
< map
->n
; ++i
) {
6052 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6056 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6057 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6064 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6065 __isl_take isl_map
*map2
)
6069 struct isl_map
*map
= NULL
;
6083 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6085 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6086 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6087 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6089 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6090 map1
->n
+ map2
->n
, flags
);
6093 for (i
= 0; i
< map1
->n
; ++i
) {
6094 map
= isl_map_add_basic_map(map
,
6095 isl_basic_map_copy(map1
->p
[i
]));
6099 for (i
= 0; i
< map2
->n
; ++i
) {
6100 map
= isl_map_add_basic_map(map
,
6101 isl_basic_map_copy(map2
->p
[i
]));
6115 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6116 __isl_take isl_map
*map2
)
6118 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6121 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6123 map1
= isl_map_union_disjoint(map1
, map2
);
6127 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6131 struct isl_set
*isl_set_union_disjoint(
6132 struct isl_set
*set1
, struct isl_set
*set2
)
6134 return (struct isl_set
*)
6135 isl_map_union_disjoint(
6136 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6139 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6141 return (struct isl_set
*)
6142 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6145 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6146 __isl_take isl_set
*set
)
6149 struct isl_map
*result
;
6155 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6156 isl_die(set
->ctx
, isl_error_invalid
,
6157 "parameters don't match", goto error
);
6159 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6160 !isl_map_compatible_range(map
, set
))
6161 isl_die(set
->ctx
, isl_error_invalid
,
6162 "incompatible spaces", goto error
);
6164 if (isl_set_plain_is_universe(set
)) {
6169 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6170 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6171 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6173 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6174 map
->n
* set
->n
, flags
);
6177 for (i
= 0; i
< map
->n
; ++i
)
6178 for (j
= 0; j
< set
->n
; ++j
) {
6179 result
= isl_map_add_basic_map(result
,
6180 isl_basic_map_intersect_range(
6181 isl_basic_map_copy(map
->p
[i
]),
6182 isl_basic_set_copy(set
->p
[j
])));
6195 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6196 __isl_take isl_set
*set
)
6198 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6201 struct isl_map
*isl_map_intersect_domain(
6202 struct isl_map
*map
, struct isl_set
*set
)
6204 return isl_map_reverse(
6205 isl_map_intersect_range(isl_map_reverse(map
), set
));
6208 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6209 __isl_take isl_map
*map2
)
6213 map1
= isl_map_reverse(map1
);
6214 map1
= isl_map_apply_range(map1
, map2
);
6215 return isl_map_reverse(map1
);
6222 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6223 __isl_take isl_map
*map2
)
6225 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6228 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6229 __isl_take isl_map
*map2
)
6231 isl_space
*dim_result
;
6232 struct isl_map
*result
;
6238 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6239 isl_space_copy(map2
->dim
));
6241 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6244 for (i
= 0; i
< map1
->n
; ++i
)
6245 for (j
= 0; j
< map2
->n
; ++j
) {
6246 result
= isl_map_add_basic_map(result
,
6247 isl_basic_map_apply_range(
6248 isl_basic_map_copy(map1
->p
[i
]),
6249 isl_basic_map_copy(map2
->p
[j
])));
6255 if (result
&& result
->n
<= 1)
6256 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6264 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6265 __isl_take isl_map
*map2
)
6267 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6271 * returns range - domain
6273 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6275 isl_space
*dims
, *target_dim
;
6276 struct isl_basic_set
*bset
;
6283 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6284 bmap
->dim
, isl_dim_out
),
6286 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6287 dim
= isl_basic_map_n_in(bmap
);
6288 nparam
= isl_basic_map_n_param(bmap
);
6289 bset
= isl_basic_set_from_basic_map(bmap
);
6290 bset
= isl_basic_set_cow(bset
);
6291 dims
= isl_basic_set_get_space(bset
);
6292 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6293 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6294 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6295 for (i
= 0; i
< dim
; ++i
) {
6296 int j
= isl_basic_map_alloc_equality(
6297 (struct isl_basic_map
*)bset
);
6300 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6301 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6302 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6303 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6305 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6306 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6309 isl_basic_map_free(bmap
);
6314 * returns range - domain
6316 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6320 struct isl_set
*result
;
6325 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6326 map
->dim
, isl_dim_out
),
6328 dim
= isl_map_get_space(map
);
6329 dim
= isl_space_domain(dim
);
6330 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6333 for (i
= 0; i
< map
->n
; ++i
)
6334 result
= isl_set_add_basic_set(result
,
6335 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6344 * returns [domain -> range] -> range - domain
6346 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6347 __isl_take isl_basic_map
*bmap
)
6351 isl_basic_map
*domain
;
6355 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6356 isl_die(bmap
->ctx
, isl_error_invalid
,
6357 "domain and range don't match", goto error
);
6359 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6360 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6362 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6363 domain
= isl_basic_map_universe(dim
);
6365 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6366 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6367 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6369 total
= isl_basic_map_total_dim(bmap
);
6371 for (i
= 0; i
< n
; ++i
) {
6372 k
= isl_basic_map_alloc_equality(bmap
);
6375 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6376 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6377 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6378 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6381 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6382 return isl_basic_map_finalize(bmap
);
6384 isl_basic_map_free(bmap
);
6389 * returns [domain -> range] -> range - domain
6391 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6394 isl_space
*domain_dim
;
6399 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6400 isl_die(map
->ctx
, isl_error_invalid
,
6401 "domain and range don't match", goto error
);
6403 map
= isl_map_cow(map
);
6407 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6408 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6409 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6412 for (i
= 0; i
< map
->n
; ++i
) {
6413 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6417 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6424 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6426 struct isl_basic_map
*bmap
;
6434 nparam
= dims
->nparam
;
6436 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6440 for (i
= 0; i
< dim
; ++i
) {
6441 int j
= isl_basic_map_alloc_equality(bmap
);
6444 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6445 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6446 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6448 return isl_basic_map_finalize(bmap
);
6450 isl_basic_map_free(bmap
);
6454 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6458 if (dim
->n_in
!= dim
->n_out
)
6459 isl_die(dim
->ctx
, isl_error_invalid
,
6460 "number of input and output dimensions needs to be "
6461 "the same", goto error
);
6462 return basic_map_identity(dim
);
6464 isl_space_free(dim
);
6468 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6470 if (!model
|| !model
->dim
)
6472 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6475 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6477 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6480 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6482 if (!model
|| !model
->dim
)
6484 return isl_map_identity(isl_space_copy(model
->dim
));
6487 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6489 if (!model
|| !model
->dim
)
6491 return isl_map_identity(isl_space_copy(model
->dim
));
6494 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6496 isl_space
*dim
= isl_set_get_space(set
);
6498 id
= isl_map_identity(isl_space_map_from_set(dim
));
6499 return isl_map_intersect_range(id
, set
);
6502 /* Construct a basic set with all set dimensions having only non-negative
6505 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6506 __isl_take isl_space
*space
)
6511 struct isl_basic_set
*bset
;
6515 nparam
= space
->nparam
;
6517 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6520 for (i
= 0; i
< dim
; ++i
) {
6521 int k
= isl_basic_set_alloc_inequality(bset
);
6524 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6525 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6529 isl_basic_set_free(bset
);
6533 /* Construct the half-space x_pos >= 0.
6535 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6539 isl_basic_set
*nonneg
;
6541 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6542 k
= isl_basic_set_alloc_inequality(nonneg
);
6545 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6546 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6548 return isl_basic_set_finalize(nonneg
);
6550 isl_basic_set_free(nonneg
);
6554 /* Construct the half-space x_pos <= -1.
6556 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6561 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6562 k
= isl_basic_set_alloc_inequality(neg
);
6565 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6566 isl_int_set_si(neg
->ineq
[k
][0], -1);
6567 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6569 return isl_basic_set_finalize(neg
);
6571 isl_basic_set_free(neg
);
6575 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6576 enum isl_dim_type type
, unsigned first
, unsigned n
)
6579 isl_basic_set
*nonneg
;
6587 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6589 for (i
= 0; i
< n
; ++i
) {
6590 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6591 pos(set
->dim
, type
) + first
+ i
);
6592 neg
= neg_halfspace(isl_set_get_space(set
),
6593 pos(set
->dim
, type
) + first
+ i
);
6595 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6604 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6605 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6612 if (isl_set_plain_is_empty(set
)) {
6617 return fn(set
, signs
, user
);
6620 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6622 half
= isl_set_intersect(half
, isl_set_copy(set
));
6623 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6627 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6629 half
= isl_set_intersect(half
, set
);
6630 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6636 /* Call "fn" on the intersections of "set" with each of the orthants
6637 * (except for obviously empty intersections). The orthant is identified
6638 * by the signs array, with each entry having value 1 or -1 according
6639 * to the sign of the corresponding variable.
6641 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6642 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6652 if (isl_set_plain_is_empty(set
))
6655 nparam
= isl_set_dim(set
, isl_dim_param
);
6656 nvar
= isl_set_dim(set
, isl_dim_set
);
6658 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6660 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6668 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6670 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6673 int isl_basic_map_is_subset(
6674 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6677 struct isl_map
*map1
;
6678 struct isl_map
*map2
;
6680 if (!bmap1
|| !bmap2
)
6683 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6684 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6686 is_subset
= isl_map_is_subset(map1
, map2
);
6694 int isl_basic_map_is_equal(
6695 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6699 if (!bmap1
|| !bmap2
)
6701 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6704 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6708 int isl_basic_set_is_equal(
6709 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6711 return isl_basic_map_is_equal(
6712 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6715 int isl_map_is_empty(struct isl_map
*map
)
6722 for (i
= 0; i
< map
->n
; ++i
) {
6723 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6732 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6734 return map
? map
->n
== 0 : -1;
6737 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6739 return isl_map_plain_is_empty(map
);
6742 int isl_set_plain_is_empty(struct isl_set
*set
)
6744 return set
? set
->n
== 0 : -1;
6747 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6749 return isl_set_plain_is_empty(set
);
6752 int isl_set_is_empty(struct isl_set
*set
)
6754 return isl_map_is_empty((struct isl_map
*)set
);
6757 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6762 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6765 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6770 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6773 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6779 is_subset
= isl_map_is_subset(map1
, map2
);
6782 is_subset
= isl_map_is_subset(map2
, map1
);
6786 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6788 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6791 int isl_basic_map_is_strict_subset(
6792 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6796 if (!bmap1
|| !bmap2
)
6798 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6801 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6802 if (is_subset
== -1)
6807 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6813 is_subset
= isl_map_is_subset(map1
, map2
);
6816 is_subset
= isl_map_is_subset(map2
, map1
);
6817 if (is_subset
== -1)
6822 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6824 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6827 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6831 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6834 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6838 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6841 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6848 for (i
= 0; i
< map
->n
; ++i
) {
6849 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6857 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6859 return isl_map_plain_is_universe((isl_map
*) set
);
6862 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6864 return isl_set_plain_is_universe(set
);
6867 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6869 struct isl_basic_set
*bset
= NULL
;
6870 struct isl_vec
*sample
= NULL
;
6877 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6880 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6881 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6882 copy
= isl_basic_map_remove_redundancies(copy
);
6883 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6884 isl_basic_map_free(copy
);
6888 total
= 1 + isl_basic_map_total_dim(bmap
);
6889 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6890 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6896 isl_vec_free(bmap
->sample
);
6897 bmap
->sample
= NULL
;
6898 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6901 sample
= isl_basic_set_sample_vec(bset
);
6904 empty
= sample
->size
== 0;
6905 isl_vec_free(bmap
->sample
);
6906 bmap
->sample
= sample
;
6908 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6913 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6917 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6920 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6922 return isl_basic_map_plain_is_empty(bmap
);
6925 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6929 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6932 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6934 return isl_basic_set_plain_is_empty(bset
);
6937 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6939 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6942 struct isl_map
*isl_basic_map_union(
6943 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6945 struct isl_map
*map
;
6946 if (!bmap1
|| !bmap2
)
6949 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6951 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6954 map
= isl_map_add_basic_map(map
, bmap1
);
6955 map
= isl_map_add_basic_map(map
, bmap2
);
6958 isl_basic_map_free(bmap1
);
6959 isl_basic_map_free(bmap2
);
6963 struct isl_set
*isl_basic_set_union(
6964 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6966 return (struct isl_set
*)isl_basic_map_union(
6967 (struct isl_basic_map
*)bset1
,
6968 (struct isl_basic_map
*)bset2
);
6971 /* Order divs such that any div only depends on previous divs */
6972 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6980 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6982 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6984 if (isl_int_is_zero(bmap
->div
[i
][0]))
6986 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6990 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6996 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6998 return (struct isl_basic_set
*)
6999 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7002 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7009 for (i
= 0; i
< map
->n
; ++i
) {
7010 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7021 /* Apply the expansion computed by isl_merge_divs.
7022 * The expansion itself is given by "exp" while the resulting
7023 * list of divs is given by "div".
7025 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7026 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7031 bset
= isl_basic_set_cow(bset
);
7035 if (div
->n_row
< bset
->n_div
)
7036 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7037 "not an expansion", goto error
);
7039 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7040 div
->n_row
- bset
->n_div
, 0,
7041 2 * (div
->n_row
- bset
->n_div
));
7043 n_div
= bset
->n_div
;
7044 for (i
= n_div
; i
< div
->n_row
; ++i
)
7045 if (isl_basic_set_alloc_div(bset
) < 0)
7049 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7050 if (j
>= 0 && exp
[j
] == i
) {
7052 isl_basic_map_swap_div(bset
, i
, j
);
7055 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7056 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7064 isl_basic_set_free(bset
);
7069 /* Look for a div in dst that corresponds to the div "div" in src.
7070 * The divs before "div" in src and dst are assumed to be the same.
7072 * Returns -1 if no corresponding div was found and the position
7073 * of the corresponding div in dst otherwise.
7075 static int find_div(struct isl_basic_map
*dst
,
7076 struct isl_basic_map
*src
, unsigned div
)
7080 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7082 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7083 for (i
= div
; i
< dst
->n_div
; ++i
)
7084 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7085 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7086 dst
->n_div
- div
) == -1)
7091 struct isl_basic_map
*isl_basic_map_align_divs(
7092 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7095 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7100 if (src
->n_div
== 0)
7103 for (i
= 0; i
< src
->n_div
; ++i
)
7104 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7106 src
= isl_basic_map_order_divs(src
);
7107 dst
= isl_basic_map_cow(dst
);
7108 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7109 src
->n_div
, 0, 2 * src
->n_div
);
7112 for (i
= 0; i
< src
->n_div
; ++i
) {
7113 int j
= find_div(dst
, src
, i
);
7115 j
= isl_basic_map_alloc_div(dst
);
7118 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7119 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7120 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7124 isl_basic_map_swap_div(dst
, i
, j
);
7128 isl_basic_map_free(dst
);
7132 struct isl_basic_set
*isl_basic_set_align_divs(
7133 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7135 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7136 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7139 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7147 map
= isl_map_compute_divs(map
);
7148 map
= isl_map_cow(map
);
7152 for (i
= 1; i
< map
->n
; ++i
)
7153 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7154 for (i
= 1; i
< map
->n
; ++i
)
7155 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7157 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7161 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7163 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7166 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7167 __isl_take isl_map
*map
)
7171 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7172 map
= isl_map_intersect_domain(map
, set
);
7173 set
= isl_map_range(map
);
7181 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7182 __isl_take isl_map
*map
)
7184 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7187 /* There is no need to cow as removing empty parts doesn't change
7188 * the meaning of the set.
7190 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7197 for (i
= map
->n
-1; i
>= 0; --i
) {
7198 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7200 isl_basic_map_free(map
->p
[i
]);
7201 if (i
!= map
->n
-1) {
7202 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7203 map
->p
[i
] = map
->p
[map
->n
-1];
7211 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7213 return (struct isl_set
*)
7214 isl_map_remove_empty_parts((struct isl_map
*)set
);
7217 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7219 struct isl_basic_map
*bmap
;
7220 if (!map
|| map
->n
== 0)
7222 bmap
= map
->p
[map
->n
-1];
7223 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7224 return isl_basic_map_copy(bmap
);
7227 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7229 return (struct isl_basic_set
*)
7230 isl_map_copy_basic_map((struct isl_map
*)set
);
7233 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7234 __isl_keep isl_basic_map
*bmap
)
7240 for (i
= map
->n
-1; i
>= 0; --i
) {
7241 if (map
->p
[i
] != bmap
)
7243 map
= isl_map_cow(map
);
7246 isl_basic_map_free(map
->p
[i
]);
7247 if (i
!= map
->n
-1) {
7248 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7249 map
->p
[i
] = map
->p
[map
->n
-1];
7260 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7261 struct isl_basic_set
*bset
)
7263 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7264 (struct isl_basic_map
*)bset
);
7267 /* Given two basic sets bset1 and bset2, compute the maximal difference
7268 * between the values of dimension pos in bset1 and those in bset2
7269 * for any common value of the parameters and dimensions preceding pos.
7271 static enum isl_lp_result
basic_set_maximal_difference_at(
7272 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7273 int pos
, isl_int
*opt
)
7276 struct isl_basic_map
*bmap1
= NULL
;
7277 struct isl_basic_map
*bmap2
= NULL
;
7278 struct isl_ctx
*ctx
;
7279 struct isl_vec
*obj
;
7282 unsigned dim1
, dim2
;
7283 enum isl_lp_result res
;
7285 if (!bset1
|| !bset2
)
7286 return isl_lp_error
;
7288 nparam
= isl_basic_set_n_param(bset1
);
7289 dim1
= isl_basic_set_n_dim(bset1
);
7290 dim2
= isl_basic_set_n_dim(bset2
);
7291 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7292 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7293 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7294 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7295 if (!bmap1
|| !bmap2
)
7297 bmap1
= isl_basic_map_cow(bmap1
);
7298 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7299 pos
, (dim1
- pos
) + (dim2
- pos
),
7300 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7301 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7304 total
= isl_basic_map_total_dim(bmap1
);
7306 obj
= isl_vec_alloc(ctx
, 1 + total
);
7307 isl_seq_clr(obj
->block
.data
, 1 + total
);
7308 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7309 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7312 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7314 isl_basic_map_free(bmap1
);
7318 isl_basic_map_free(bmap1
);
7319 isl_basic_map_free(bmap2
);
7320 return isl_lp_error
;
7323 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7324 * for any common value of the parameters and dimensions preceding pos
7325 * in both basic sets, the values of dimension pos in bset1 are
7326 * smaller or larger than those in bset2.
7329 * 1 if bset1 follows bset2
7330 * -1 if bset1 precedes bset2
7331 * 0 if bset1 and bset2 are incomparable
7332 * -2 if some error occurred.
7334 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7335 struct isl_basic_set
*bset2
, int pos
)
7338 enum isl_lp_result res
;
7343 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7345 if (res
== isl_lp_empty
)
7347 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7348 res
== isl_lp_unbounded
)
7350 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7359 /* Given two basic sets bset1 and bset2, check whether
7360 * for any common value of the parameters and dimensions preceding pos
7361 * there is a value of dimension pos in bset1 that is larger
7362 * than a value of the same dimension in bset2.
7365 * 1 if there exists such a pair
7366 * 0 if there is no such pair, but there is a pair of equal values
7368 * -2 if some error occurred.
7370 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7371 __isl_keep isl_basic_set
*bset2
, int pos
)
7374 enum isl_lp_result res
;
7379 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7381 if (res
== isl_lp_empty
)
7383 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7384 res
== isl_lp_unbounded
)
7386 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7388 else if (res
== isl_lp_ok
)
7397 /* Given two sets set1 and set2, check whether
7398 * for any common value of the parameters and dimensions preceding pos
7399 * there is a value of dimension pos in set1 that is larger
7400 * than a value of the same dimension in set2.
7403 * 1 if there exists such a pair
7404 * 0 if there is no such pair, but there is a pair of equal values
7406 * -2 if some error occurred.
7408 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7409 __isl_keep isl_set
*set2
, int pos
)
7417 for (i
= 0; i
< set1
->n
; ++i
)
7418 for (j
= 0; j
< set2
->n
; ++j
) {
7420 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7421 if (f
== 1 || f
== -2)
7430 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7431 unsigned pos
, isl_int
*val
)
7439 total
= isl_basic_map_total_dim(bmap
);
7440 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7441 for (; d
+1 > pos
; --d
)
7442 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7446 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7448 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7450 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7453 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7459 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7460 unsigned pos
, isl_int
*val
)
7472 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7475 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7476 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7477 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7478 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7482 isl_int_set(*val
, v
);
7488 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7489 unsigned pos
, isl_int
*val
)
7491 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7495 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7498 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7501 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7502 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7504 if (pos
>= isl_basic_map_dim(bmap
, type
))
7506 return isl_basic_map_plain_has_fixed_var(bmap
,
7507 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7510 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7511 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7513 if (pos
>= isl_map_dim(map
, type
))
7515 return isl_map_plain_has_fixed_var(map
,
7516 map_offset(map
, type
) - 1 + pos
, val
);
7519 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7520 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7522 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7525 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7526 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7528 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7531 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7532 * then return this fixed value in *val.
7534 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7535 unsigned dim
, isl_int
*val
)
7537 return isl_basic_set_plain_has_fixed_var(bset
,
7538 isl_basic_set_n_param(bset
) + dim
, val
);
7541 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7542 * then return this fixed value in *val.
7544 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7545 unsigned dim
, isl_int
*val
)
7547 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7550 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7551 unsigned dim
, isl_int
*val
)
7553 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7556 /* Check if input variable in has fixed value and if so and if val is not NULL,
7557 * then return this fixed value in *val.
7559 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7560 unsigned in
, isl_int
*val
)
7562 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7565 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7566 * and if val is not NULL, then return this lower bound in *val.
7568 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7569 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7571 int i
, i_eq
= -1, i_ineq
= -1;
7578 total
= isl_basic_set_total_dim(bset
);
7579 nparam
= isl_basic_set_n_param(bset
);
7580 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7581 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7587 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7588 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7590 if (i_eq
!= -1 || i_ineq
!= -1)
7594 if (i_eq
== -1 && i_ineq
== -1)
7596 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7597 /* The coefficient should always be one due to normalization. */
7598 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7600 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7602 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7603 total
- nparam
- dim
- 1) != -1)
7606 isl_int_neg(*val
, c
[0]);
7610 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7611 unsigned dim
, isl_int
*val
)
7623 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7627 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7629 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7630 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7632 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7636 isl_int_set(*val
, v
);
7647 /* uset_gist depends on constraints without existentially quantified
7648 * variables sorting first.
7650 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7652 const struct constraint
*c1
= (const struct constraint
*)p1
;
7653 const struct constraint
*c2
= (const struct constraint
*)p2
;
7655 unsigned size
= isl_min(c1
->size
, c2
->size
);
7657 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7658 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7663 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7666 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7667 struct isl_basic_map
*bmap
)
7670 struct constraint
*c
;
7675 total
= isl_basic_map_total_dim(bmap
);
7676 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7679 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7681 c
[i
].c
= bmap
->ineq
[i
];
7683 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7684 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7685 bmap
->ineq
[i
] = c
[i
].c
;
7689 isl_basic_map_free(bmap
);
7693 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7694 __isl_take isl_basic_set
*bset
)
7696 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7697 (struct isl_basic_map
*)bset
);
7700 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7704 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7706 bmap
= isl_basic_map_remove_redundancies(bmap
);
7707 bmap
= isl_basic_map_sort_constraints(bmap
);
7708 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7712 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7714 return (struct isl_basic_set
*)isl_basic_map_normalize(
7715 (struct isl_basic_map
*)bset
);
7718 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7719 const __isl_keep isl_basic_map
*bmap2
)
7726 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7727 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7728 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7729 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7730 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7731 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7732 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7733 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7734 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7735 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7736 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7738 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7740 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7742 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7743 return bmap1
->n_eq
- bmap2
->n_eq
;
7744 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7745 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7746 if (bmap1
->n_div
!= bmap2
->n_div
)
7747 return bmap1
->n_div
- bmap2
->n_div
;
7748 total
= isl_basic_map_total_dim(bmap1
);
7749 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7750 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7754 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7755 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7759 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7760 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7767 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7768 const __isl_keep isl_basic_set
*bset2
)
7770 return isl_basic_map_plain_cmp(bset1
, bset2
);
7773 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7774 const __isl_keep isl_set
*set2
)
7780 if (set1
->n
!= set2
->n
)
7781 return set1
->n
- set2
->n
;
7783 for (i
= 0; i
< set1
->n
; ++i
) {
7784 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7792 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7793 __isl_keep isl_basic_map
*bmap2
)
7795 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7798 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7799 __isl_keep isl_basic_set
*bset2
)
7801 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7802 (isl_basic_map
*)bset2
);
7805 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7807 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7808 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7810 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7813 /* We normalize in place, but if anything goes wrong we need
7814 * to return NULL, so we need to make sure we don't change the
7815 * meaning of any possible other copies of map.
7817 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7820 struct isl_basic_map
*bmap
;
7824 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7826 for (i
= 0; i
< map
->n
; ++i
) {
7827 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7830 isl_basic_map_free(map
->p
[i
]);
7833 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7834 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7835 map
= isl_map_remove_empty_parts(map
);
7838 for (i
= map
->n
- 1; i
>= 1; --i
) {
7839 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7841 isl_basic_map_free(map
->p
[i
-1]);
7842 for (j
= i
; j
< map
->n
; ++j
)
7843 map
->p
[j
-1] = map
->p
[j
];
7853 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7855 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7858 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7868 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7871 map1
= isl_map_copy(map1
);
7872 map2
= isl_map_copy(map2
);
7873 map1
= isl_map_normalize(map1
);
7874 map2
= isl_map_normalize(map2
);
7877 equal
= map1
->n
== map2
->n
;
7878 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7879 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7892 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7894 return isl_map_plain_is_equal(map1
, map2
);
7897 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7899 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7900 (struct isl_map
*)set2
);
7903 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7905 return isl_set_plain_is_equal(set1
, set2
);
7908 /* Return an interval that ranges from min to max (inclusive)
7910 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7911 isl_int min
, isl_int max
)
7914 struct isl_basic_set
*bset
= NULL
;
7916 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7920 k
= isl_basic_set_alloc_inequality(bset
);
7923 isl_int_set_si(bset
->ineq
[k
][1], 1);
7924 isl_int_neg(bset
->ineq
[k
][0], min
);
7926 k
= isl_basic_set_alloc_inequality(bset
);
7929 isl_int_set_si(bset
->ineq
[k
][1], -1);
7930 isl_int_set(bset
->ineq
[k
][0], max
);
7934 isl_basic_set_free(bset
);
7938 /* Return the Cartesian product of the basic sets in list (in the given order).
7940 __isl_give isl_basic_set
*isl_basic_set_list_product(
7941 __isl_take
struct isl_basic_set_list
*list
)
7949 struct isl_basic_set
*product
= NULL
;
7953 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7954 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7955 nparam
= isl_basic_set_n_param(list
->p
[0]);
7956 dim
= isl_basic_set_n_dim(list
->p
[0]);
7957 extra
= list
->p
[0]->n_div
;
7958 n_eq
= list
->p
[0]->n_eq
;
7959 n_ineq
= list
->p
[0]->n_ineq
;
7960 for (i
= 1; i
< list
->n
; ++i
) {
7961 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7962 isl_assert(list
->ctx
,
7963 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7964 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7965 extra
+= list
->p
[i
]->n_div
;
7966 n_eq
+= list
->p
[i
]->n_eq
;
7967 n_ineq
+= list
->p
[i
]->n_ineq
;
7969 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7974 for (i
= 0; i
< list
->n
; ++i
) {
7975 isl_basic_set_add_constraints(product
,
7976 isl_basic_set_copy(list
->p
[i
]), dim
);
7977 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7979 isl_basic_set_list_free(list
);
7982 isl_basic_set_free(product
);
7983 isl_basic_set_list_free(list
);
7987 struct isl_basic_map
*isl_basic_map_product(
7988 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7990 isl_space
*dim_result
= NULL
;
7991 struct isl_basic_map
*bmap
;
7992 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7993 struct isl_dim_map
*dim_map1
, *dim_map2
;
7995 if (!bmap1
|| !bmap2
)
7998 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7999 bmap2
->dim
, isl_dim_param
), goto error
);
8000 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8001 isl_space_copy(bmap2
->dim
));
8003 in1
= isl_basic_map_n_in(bmap1
);
8004 in2
= isl_basic_map_n_in(bmap2
);
8005 out1
= isl_basic_map_n_out(bmap1
);
8006 out2
= isl_basic_map_n_out(bmap2
);
8007 nparam
= isl_basic_map_n_param(bmap1
);
8009 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8010 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8011 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8012 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8013 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8014 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8015 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8016 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8017 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8018 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8019 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8021 bmap
= isl_basic_map_alloc_space(dim_result
,
8022 bmap1
->n_div
+ bmap2
->n_div
,
8023 bmap1
->n_eq
+ bmap2
->n_eq
,
8024 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8025 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8026 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8027 bmap
= isl_basic_map_simplify(bmap
);
8028 return isl_basic_map_finalize(bmap
);
8030 isl_basic_map_free(bmap1
);
8031 isl_basic_map_free(bmap2
);
8035 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8036 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8038 isl_basic_map
*prod
;
8040 prod
= isl_basic_map_product(bmap1
, bmap2
);
8041 prod
= isl_basic_map_flatten(prod
);
8045 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8046 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8048 return isl_basic_map_flat_range_product(bset1
, bset2
);
8051 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8052 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8054 isl_space
*space_result
= NULL
;
8055 isl_basic_map
*bmap
;
8056 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8057 struct isl_dim_map
*dim_map1
, *dim_map2
;
8059 if (!bmap1
|| !bmap2
)
8062 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8063 isl_space_copy(bmap2
->dim
));
8065 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8066 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8067 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8068 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8070 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8071 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8072 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8073 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8074 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8075 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8076 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8077 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8078 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8079 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8080 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8082 bmap
= isl_basic_map_alloc_space(space_result
,
8083 bmap1
->n_div
+ bmap2
->n_div
,
8084 bmap1
->n_eq
+ bmap2
->n_eq
,
8085 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8086 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8087 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8088 bmap
= isl_basic_map_simplify(bmap
);
8089 return isl_basic_map_finalize(bmap
);
8091 isl_basic_map_free(bmap1
);
8092 isl_basic_map_free(bmap2
);
8096 __isl_give isl_basic_map
*isl_basic_map_range_product(
8097 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8099 isl_space
*dim_result
= NULL
;
8100 isl_basic_map
*bmap
;
8101 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8102 struct isl_dim_map
*dim_map1
, *dim_map2
;
8104 if (!bmap1
|| !bmap2
)
8107 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8108 isl_space_copy(bmap2
->dim
));
8110 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8111 out1
= isl_basic_map_n_out(bmap1
);
8112 out2
= isl_basic_map_n_out(bmap2
);
8113 nparam
= isl_basic_map_n_param(bmap1
);
8115 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8116 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8117 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8118 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8119 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8120 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8121 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8122 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8123 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8124 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8125 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8127 bmap
= isl_basic_map_alloc_space(dim_result
,
8128 bmap1
->n_div
+ bmap2
->n_div
,
8129 bmap1
->n_eq
+ bmap2
->n_eq
,
8130 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8131 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8132 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8133 bmap
= isl_basic_map_simplify(bmap
);
8134 return isl_basic_map_finalize(bmap
);
8136 isl_basic_map_free(bmap1
);
8137 isl_basic_map_free(bmap2
);
8141 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8142 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8144 isl_basic_map
*prod
;
8146 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8147 prod
= isl_basic_map_flatten_range(prod
);
8151 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8152 __isl_take isl_map
*map2
,
8153 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8154 __isl_take isl_space
*right
),
8155 __isl_give isl_basic_map
*(*basic_map_product
)(
8156 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8159 struct isl_map
*result
;
8165 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8166 map2
->dim
, isl_dim_param
), goto error
);
8168 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8169 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8170 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8172 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8173 isl_space_copy(map2
->dim
)),
8174 map1
->n
* map2
->n
, flags
);
8177 for (i
= 0; i
< map1
->n
; ++i
)
8178 for (j
= 0; j
< map2
->n
; ++j
) {
8179 struct isl_basic_map
*part
;
8180 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8181 isl_basic_map_copy(map2
->p
[j
]));
8182 if (isl_basic_map_is_empty(part
))
8183 isl_basic_map_free(part
);
8185 result
= isl_map_add_basic_map(result
, part
);
8198 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8200 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8201 __isl_take isl_map
*map2
)
8203 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8206 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8207 __isl_take isl_map
*map2
)
8209 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8212 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8214 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8215 __isl_take isl_map
*map2
)
8219 prod
= isl_map_product(map1
, map2
);
8220 prod
= isl_map_flatten(prod
);
8224 /* Given two set A and B, construct its Cartesian product A x B.
8226 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8228 return isl_map_range_product(set1
, set2
);
8231 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8232 __isl_take isl_set
*set2
)
8234 return isl_map_flat_range_product(set1
, set2
);
8237 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8239 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8240 __isl_take isl_map
*map2
)
8242 return map_product(map1
, map2
, &isl_space_domain_product
,
8243 &isl_basic_map_domain_product
);
8246 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8248 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8249 __isl_take isl_map
*map2
)
8251 return map_product(map1
, map2
, &isl_space_range_product
,
8252 &isl_basic_map_range_product
);
8255 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8256 __isl_take isl_map
*map2
)
8258 return isl_map_align_params_map_map_and(map1
, map2
,
8259 &map_domain_product_aligned
);
8262 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8263 __isl_take isl_map
*map2
)
8265 return isl_map_align_params_map_map_and(map1
, map2
,
8266 &map_range_product_aligned
);
8269 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8271 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8272 __isl_take isl_map
*map2
)
8276 prod
= isl_map_domain_product(map1
, map2
);
8277 prod
= isl_map_flatten_domain(prod
);
8281 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8283 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8284 __isl_take isl_map
*map2
)
8288 prod
= isl_map_range_product(map1
, map2
);
8289 prod
= isl_map_flatten_range(prod
);
8293 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8296 uint32_t hash
= isl_hash_init();
8301 bmap
= isl_basic_map_copy(bmap
);
8302 bmap
= isl_basic_map_normalize(bmap
);
8305 total
= isl_basic_map_total_dim(bmap
);
8306 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8307 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8309 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8310 isl_hash_hash(hash
, c_hash
);
8312 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8313 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8315 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8316 isl_hash_hash(hash
, c_hash
);
8318 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8319 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8321 if (isl_int_is_zero(bmap
->div
[i
][0]))
8323 isl_hash_byte(hash
, i
& 0xFF);
8324 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8325 isl_hash_hash(hash
, c_hash
);
8327 isl_basic_map_free(bmap
);
8331 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8333 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8336 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8343 map
= isl_map_copy(map
);
8344 map
= isl_map_normalize(map
);
8348 hash
= isl_hash_init();
8349 for (i
= 0; i
< map
->n
; ++i
) {
8351 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8352 isl_hash_hash(hash
, bmap_hash
);
8360 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8362 return isl_map_get_hash((isl_map
*)set
);
8365 /* Check if the value for dimension dim is completely determined
8366 * by the values of the other parameters and variables.
8367 * That is, check if dimension dim is involved in an equality.
8369 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8376 nparam
= isl_basic_set_n_param(bset
);
8377 for (i
= 0; i
< bset
->n_eq
; ++i
)
8378 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8383 /* Check if the value for dimension dim is completely determined
8384 * by the values of the other parameters and variables.
8385 * That is, check if dimension dim is involved in an equality
8386 * for each of the subsets.
8388 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8394 for (i
= 0; i
< set
->n
; ++i
) {
8396 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8403 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8405 return set
? set
->n
: 0;
8408 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8409 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8416 for (i
= 0; i
< map
->n
; ++i
)
8417 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8423 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8424 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8431 for (i
= 0; i
< set
->n
; ++i
)
8432 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8438 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8445 bset
= isl_basic_set_cow(bset
);
8449 dim
= isl_basic_set_get_space(bset
);
8450 dim
= isl_space_lift(dim
, bset
->n_div
);
8453 isl_space_free(bset
->dim
);
8455 bset
->extra
-= bset
->n_div
;
8458 bset
= isl_basic_set_finalize(bset
);
8462 isl_basic_set_free(bset
);
8466 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8472 set
= isl_set_align_divs(set
);
8477 set
= isl_set_cow(set
);
8481 n_div
= set
->p
[0]->n_div
;
8482 dim
= isl_set_get_space(set
);
8483 dim
= isl_space_lift(dim
, n_div
);
8486 isl_space_free(set
->dim
);
8489 for (i
= 0; i
< set
->n
; ++i
) {
8490 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8501 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8504 struct isl_basic_map
*bmap
;
8511 set
= isl_set_align_divs(set
);
8516 dim
= isl_set_get_space(set
);
8517 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8519 return isl_map_identity(isl_space_map_from_set(dim
));
8522 n_div
= set
->p
[0]->n_div
;
8523 dim
= isl_space_map_from_set(dim
);
8524 n_param
= isl_space_dim(dim
, isl_dim_param
);
8525 n_set
= isl_space_dim(dim
, isl_dim_in
);
8526 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8527 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8528 for (i
= 0; i
< n_set
; ++i
)
8529 bmap
= var_equal(bmap
, i
);
8531 total
= n_param
+ n_set
+ n_set
+ n_div
;
8532 for (i
= 0; i
< n_div
; ++i
) {
8533 k
= isl_basic_map_alloc_inequality(bmap
);
8536 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8537 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8538 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8539 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8540 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8541 set
->p
[0]->div
[i
][0]);
8543 l
= isl_basic_map_alloc_inequality(bmap
);
8546 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8547 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8548 set
->p
[0]->div
[i
][0]);
8549 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8553 bmap
= isl_basic_map_simplify(bmap
);
8554 bmap
= isl_basic_map_finalize(bmap
);
8555 return isl_map_from_basic_map(bmap
);
8558 isl_basic_map_free(bmap
);
8562 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8570 dim
= isl_basic_set_total_dim(bset
);
8571 size
+= bset
->n_eq
* (1 + dim
);
8572 size
+= bset
->n_ineq
* (1 + dim
);
8573 size
+= bset
->n_div
* (2 + dim
);
8578 int isl_set_size(__isl_keep isl_set
*set
)
8586 for (i
= 0; i
< set
->n
; ++i
)
8587 size
+= isl_basic_set_size(set
->p
[i
]);
8592 /* Check if there is any lower bound (if lower == 0) and/or upper
8593 * bound (if upper == 0) on the specified dim.
8595 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8596 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8603 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8605 pos
+= isl_basic_map_offset(bmap
, type
);
8607 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8608 if (isl_int_is_zero(bmap
->div
[i
][0]))
8610 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8614 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8615 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8618 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8619 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8626 return lower
&& upper
;
8629 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8630 enum isl_dim_type type
, unsigned pos
)
8632 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8635 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8636 enum isl_dim_type type
, unsigned pos
)
8638 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8641 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8642 enum isl_dim_type type
, unsigned pos
)
8644 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8647 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8648 enum isl_dim_type type
, unsigned pos
)
8655 for (i
= 0; i
< map
->n
; ++i
) {
8657 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8658 if (bounded
< 0 || !bounded
)
8665 /* Return 1 if the specified dim is involved in both an upper bound
8666 * and a lower bound.
8668 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8669 enum isl_dim_type type
, unsigned pos
)
8671 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8674 static int has_bound(__isl_keep isl_map
*map
,
8675 enum isl_dim_type type
, unsigned pos
,
8676 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8677 enum isl_dim_type type
, unsigned pos
))
8684 for (i
= 0; i
< map
->n
; ++i
) {
8686 bounded
= fn(map
->p
[i
], type
, pos
);
8687 if (bounded
< 0 || bounded
)
8694 /* Return 1 if the specified dim is involved in any lower bound.
8696 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8697 enum isl_dim_type type
, unsigned pos
)
8699 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8702 /* Return 1 if the specified dim is involved in any upper bound.
8704 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8705 enum isl_dim_type type
, unsigned pos
)
8707 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8710 /* For each of the "n" variables starting at "first", determine
8711 * the sign of the variable and put the results in the first "n"
8712 * elements of the array "signs".
8714 * 1 means that the variable is non-negative
8715 * -1 means that the variable is non-positive
8716 * 0 means the variable attains both positive and negative values.
8718 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8719 unsigned first
, unsigned n
, int *signs
)
8721 isl_vec
*bound
= NULL
;
8722 struct isl_tab
*tab
= NULL
;
8723 struct isl_tab_undo
*snap
;
8726 if (!bset
|| !signs
)
8729 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8730 tab
= isl_tab_from_basic_set(bset
);
8734 isl_seq_clr(bound
->el
, bound
->size
);
8735 isl_int_set_si(bound
->el
[0], -1);
8737 snap
= isl_tab_snap(tab
);
8738 for (i
= 0; i
< n
; ++i
) {
8741 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8742 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8745 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8746 if (isl_tab_rollback(tab
, snap
) < 0)
8754 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8755 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8758 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8759 if (isl_tab_rollback(tab
, snap
) < 0)
8762 signs
[i
] = empty
? -1 : 0;
8766 isl_vec_free(bound
);
8770 isl_vec_free(bound
);
8774 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8775 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8777 if (!bset
|| !signs
)
8779 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8782 first
+= pos(bset
->dim
, type
) - 1;
8783 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8786 /* Check if the given basic map is obviously single-valued.
8787 * In particular, for each output dimension, check that there is
8788 * an equality that defines the output dimension in terms of
8789 * earlier dimensions.
8791 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8801 total
= 1 + isl_basic_map_total_dim(bmap
);
8802 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8803 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8805 for (i
= 0; i
< n_out
; ++i
) {
8806 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8807 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8809 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8810 total
- (o_out
+ i
+ 1)) == -1)
8813 if (j
>= bmap
->n_eq
)
8820 /* Check if the given map is obviously single-valued.
8822 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8831 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8834 /* Check if the given map is single-valued.
8839 * and check if the result is a subset of the identity mapping.
8841 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8848 sv
= isl_map_plain_is_single_valued(map
);
8852 test
= isl_map_reverse(isl_map_copy(map
));
8853 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8855 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8856 id
= isl_map_identity(dim
);
8858 sv
= isl_map_is_subset(test
, id
);
8866 int isl_map_is_injective(__isl_keep isl_map
*map
)
8870 map
= isl_map_copy(map
);
8871 map
= isl_map_reverse(map
);
8872 in
= isl_map_is_single_valued(map
);
8878 /* Check if the given map is obviously injective.
8880 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8884 map
= isl_map_copy(map
);
8885 map
= isl_map_reverse(map
);
8886 in
= isl_map_plain_is_single_valued(map
);
8892 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8896 sv
= isl_map_is_single_valued(map
);
8900 return isl_map_is_injective(map
);
8903 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8905 return isl_map_is_single_valued((isl_map
*)set
);
8908 int isl_map_is_translation(__isl_keep isl_map
*map
)
8913 delta
= isl_map_deltas(isl_map_copy(map
));
8914 ok
= isl_set_is_singleton(delta
);
8915 isl_set_free(delta
);
8920 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8922 if (isl_seq_first_non_zero(p
, pos
) != -1)
8924 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8929 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8938 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8941 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8942 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8943 for (j
= 0; j
< nvar
; ++j
) {
8944 int lower
= 0, upper
= 0;
8945 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8946 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8948 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8954 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8955 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8957 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8959 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8964 if (!lower
|| !upper
)
8971 int isl_set_is_box(__isl_keep isl_set
*set
)
8978 return isl_basic_set_is_box(set
->p
[0]);
8981 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8986 return isl_space_is_wrapping(bset
->dim
);
8989 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8994 return isl_space_is_wrapping(set
->dim
);
8997 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8999 bmap
= isl_basic_map_cow(bmap
);
9003 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9007 bmap
= isl_basic_map_finalize(bmap
);
9009 return (isl_basic_set
*)bmap
;
9011 isl_basic_map_free(bmap
);
9015 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9019 map
= isl_map_cow(map
);
9023 for (i
= 0; i
< map
->n
; ++i
) {
9024 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9028 map
->dim
= isl_space_wrap(map
->dim
);
9032 return (isl_set
*)map
;
9038 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9040 bset
= isl_basic_set_cow(bset
);
9044 bset
->dim
= isl_space_unwrap(bset
->dim
);
9048 bset
= isl_basic_set_finalize(bset
);
9050 return (isl_basic_map
*)bset
;
9052 isl_basic_set_free(bset
);
9056 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9063 if (!isl_set_is_wrapping(set
))
9064 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9067 set
= isl_set_cow(set
);
9071 for (i
= 0; i
< set
->n
; ++i
) {
9072 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9077 set
->dim
= isl_space_unwrap(set
->dim
);
9081 return (isl_map
*)set
;
9087 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9088 enum isl_dim_type type
)
9093 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9096 bmap
= isl_basic_map_cow(bmap
);
9100 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9104 bmap
= isl_basic_map_finalize(bmap
);
9108 isl_basic_map_free(bmap
);
9112 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9113 enum isl_dim_type type
)
9120 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9123 map
= isl_map_cow(map
);
9127 for (i
= 0; i
< map
->n
; ++i
) {
9128 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9132 map
->dim
= isl_space_reset(map
->dim
, type
);
9142 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9147 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9150 bmap
= isl_basic_map_cow(bmap
);
9154 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9158 bmap
= isl_basic_map_finalize(bmap
);
9162 isl_basic_map_free(bmap
);
9166 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9168 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9171 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9172 __isl_take isl_basic_map
*bmap
)
9177 if (!bmap
->dim
->nested
[0])
9180 bmap
= isl_basic_map_cow(bmap
);
9184 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9188 bmap
= isl_basic_map_finalize(bmap
);
9192 isl_basic_map_free(bmap
);
9196 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9197 __isl_take isl_basic_map
*bmap
)
9202 if (!bmap
->dim
->nested
[1])
9205 bmap
= isl_basic_map_cow(bmap
);
9209 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9213 bmap
= isl_basic_map_finalize(bmap
);
9217 isl_basic_map_free(bmap
);
9221 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9228 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9231 map
= isl_map_cow(map
);
9235 for (i
= 0; i
< map
->n
; ++i
) {
9236 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9240 map
->dim
= isl_space_flatten(map
->dim
);
9250 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9252 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9255 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9257 isl_space
*dim
, *flat_dim
;
9260 dim
= isl_set_get_space(set
);
9261 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9262 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9263 map
= isl_map_intersect_domain(map
, set
);
9268 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9275 if (!map
->dim
->nested
[0])
9278 map
= isl_map_cow(map
);
9282 for (i
= 0; i
< map
->n
; ++i
) {
9283 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9287 map
->dim
= isl_space_flatten_domain(map
->dim
);
9297 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9304 if (!map
->dim
->nested
[1])
9307 map
= isl_map_cow(map
);
9311 for (i
= 0; i
< map
->n
; ++i
) {
9312 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9316 map
->dim
= isl_space_flatten_range(map
->dim
);
9326 /* Reorder the dimensions of "bmap" according to the given dim_map
9327 * and set the dimension specification to "dim".
9329 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9330 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9334 bmap
= isl_basic_map_cow(bmap
);
9335 if (!bmap
|| !dim
|| !dim_map
)
9338 res
= isl_basic_map_alloc_space(dim
,
9339 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9340 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9341 res
= isl_basic_map_finalize(res
);
9345 isl_basic_map_free(bmap
);
9346 isl_space_free(dim
);
9350 /* Reorder the dimensions of "map" according to given reordering.
9352 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9353 __isl_take isl_reordering
*r
)
9356 struct isl_dim_map
*dim_map
;
9358 map
= isl_map_cow(map
);
9359 dim_map
= isl_dim_map_from_reordering(r
);
9360 if (!map
|| !r
|| !dim_map
)
9363 for (i
= 0; i
< map
->n
; ++i
) {
9364 struct isl_dim_map
*dim_map_i
;
9366 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9368 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9369 isl_space_copy(r
->dim
), dim_map_i
);
9375 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9377 isl_reordering_free(r
);
9383 isl_reordering_free(r
);
9387 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9388 __isl_take isl_reordering
*r
)
9390 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9393 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9394 __isl_take isl_space
*model
)
9401 ctx
= isl_space_get_ctx(model
);
9402 if (!isl_space_has_named_params(model
))
9403 isl_die(ctx
, isl_error_invalid
,
9404 "model has unnamed parameters", goto error
);
9405 if (!isl_space_has_named_params(map
->dim
))
9406 isl_die(ctx
, isl_error_invalid
,
9407 "relation has unnamed parameters", goto error
);
9408 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9409 isl_reordering
*exp
;
9411 model
= isl_space_drop_dims(model
, isl_dim_in
,
9412 0, isl_space_dim(model
, isl_dim_in
));
9413 model
= isl_space_drop_dims(model
, isl_dim_out
,
9414 0, isl_space_dim(model
, isl_dim_out
));
9415 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9416 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9417 map
= isl_map_realign(map
, exp
);
9420 isl_space_free(model
);
9423 isl_space_free(model
);
9428 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9429 __isl_take isl_space
*model
)
9431 return isl_map_align_params(set
, model
);
9434 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9435 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9436 enum isl_dim_type c2
, enum isl_dim_type c3
,
9437 enum isl_dim_type c4
, enum isl_dim_type c5
)
9439 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9440 struct isl_mat
*mat
;
9446 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9447 isl_basic_map_total_dim(bmap
) + 1);
9450 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9451 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9452 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9453 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9454 isl_int_set(mat
->row
[i
][pos
],
9455 bmap
->eq
[i
][off
+ k
]);
9463 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9464 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9465 enum isl_dim_type c2
, enum isl_dim_type c3
,
9466 enum isl_dim_type c4
, enum isl_dim_type c5
)
9468 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9469 struct isl_mat
*mat
;
9475 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9476 isl_basic_map_total_dim(bmap
) + 1);
9479 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9480 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9481 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9482 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9483 isl_int_set(mat
->row
[i
][pos
],
9484 bmap
->ineq
[i
][off
+ k
]);
9492 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9493 __isl_take isl_space
*dim
,
9494 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9495 enum isl_dim_type c2
, enum isl_dim_type c3
,
9496 enum isl_dim_type c4
, enum isl_dim_type c5
)
9498 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9499 isl_basic_map
*bmap
;
9505 if (!dim
|| !eq
|| !ineq
)
9508 if (eq
->n_col
!= ineq
->n_col
)
9509 isl_die(dim
->ctx
, isl_error_invalid
,
9510 "equalities and inequalities matrices should have "
9511 "same number of columns", goto error
);
9513 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9515 if (eq
->n_col
< total
)
9516 isl_die(dim
->ctx
, isl_error_invalid
,
9517 "number of columns too small", goto error
);
9519 extra
= eq
->n_col
- total
;
9521 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9522 eq
->n_row
, ineq
->n_row
);
9525 for (i
= 0; i
< extra
; ++i
) {
9526 k
= isl_basic_map_alloc_div(bmap
);
9529 isl_int_set_si(bmap
->div
[k
][0], 0);
9531 for (i
= 0; i
< eq
->n_row
; ++i
) {
9532 l
= isl_basic_map_alloc_equality(bmap
);
9535 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9536 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9537 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9538 isl_int_set(bmap
->eq
[l
][off
+ k
],
9544 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9545 l
= isl_basic_map_alloc_inequality(bmap
);
9548 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9549 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9550 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9551 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9558 isl_space_free(dim
);
9564 isl_space_free(dim
);
9570 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9571 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9572 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9574 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9575 c1
, c2
, c3
, c4
, isl_dim_in
);
9578 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9579 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9580 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9582 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9583 c1
, c2
, c3
, c4
, isl_dim_in
);
9586 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9587 __isl_take isl_space
*dim
,
9588 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9589 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9591 return (isl_basic_set
*)
9592 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9593 c1
, c2
, c3
, c4
, isl_dim_in
);
9596 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9601 return isl_space_can_zip(bmap
->dim
);
9604 int isl_map_can_zip(__isl_keep isl_map
*map
)
9609 return isl_space_can_zip(map
->dim
);
9612 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9613 * (A -> C) -> (B -> D).
9615 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9624 if (!isl_basic_map_can_zip(bmap
))
9625 isl_die(bmap
->ctx
, isl_error_invalid
,
9626 "basic map cannot be zipped", goto error
);
9627 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9628 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9629 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9630 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9631 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9634 bmap
->dim
= isl_space_zip(bmap
->dim
);
9639 isl_basic_map_free(bmap
);
9643 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9644 * (A -> C) -> (B -> D).
9646 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9653 if (!isl_map_can_zip(map
))
9654 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9657 map
= isl_map_cow(map
);
9661 for (i
= 0; i
< map
->n
; ++i
) {
9662 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9667 map
->dim
= isl_space_zip(map
->dim
);
9677 /* Construct a basic map mapping the domain of the affine expression
9678 * to a one-dimensional range prescribed by the affine expression.
9680 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9684 isl_local_space
*ls
;
9685 isl_basic_map
*bmap
;
9690 ls
= isl_aff_get_local_space(aff
);
9691 bmap
= isl_basic_map_from_local_space(ls
);
9692 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9693 k
= isl_basic_map_alloc_equality(bmap
);
9697 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9698 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9699 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9700 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9701 aff
->v
->size
- (pos
+ 1));
9704 bmap
= isl_basic_map_finalize(bmap
);
9708 isl_basic_map_free(bmap
);
9712 /* Construct a basic map mapping the domain the multi-affine expression
9713 * to its range, with each dimension in the range equated to the
9714 * corresponding affine expression.
9716 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9717 __isl_take isl_multi_aff
*maff
)
9721 isl_basic_map
*bmap
;
9726 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9727 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9728 "invalid space", return isl_multi_aff_free(maff
));
9730 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9731 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9733 for (i
= 0; i
< maff
->n
; ++i
) {
9735 isl_basic_map
*bmap_i
;
9737 aff
= isl_aff_copy(maff
->p
[i
]);
9738 bmap_i
= isl_basic_map_from_aff(aff
);
9740 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9743 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9745 isl_multi_aff_free(maff
);
9749 /* Construct a basic map mapping a domain in the given space to
9750 * to an n-dimensional range, with n the number of elements in the list,
9751 * where each coordinate in the range is prescribed by the
9752 * corresponding affine expression.
9753 * The domains of all affine expressions in the list are assumed to match
9756 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9757 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9761 isl_basic_map
*bmap
;
9766 dim
= isl_space_from_domain(domain_dim
);
9767 bmap
= isl_basic_map_universe(dim
);
9769 for (i
= 0; i
< list
->n
; ++i
) {
9771 isl_basic_map
*bmap_i
;
9773 aff
= isl_aff_copy(list
->p
[i
]);
9774 bmap_i
= isl_basic_map_from_aff(aff
);
9776 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9779 isl_aff_list_free(list
);
9783 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9784 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9786 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9789 /* Add a constraint imposing that the given two dimensions are equal.
9791 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9792 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9794 isl_basic_map
*bmap
= NULL
;
9800 if (pos1
>= isl_map_dim(map
, type1
))
9801 isl_die(map
->ctx
, isl_error_invalid
,
9802 "index out of bounds", goto error
);
9803 if (pos2
>= isl_map_dim(map
, type2
))
9804 isl_die(map
->ctx
, isl_error_invalid
,
9805 "index out of bounds", goto error
);
9807 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9808 i
= isl_basic_map_alloc_equality(bmap
);
9811 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9812 pos1
+= isl_basic_map_offset(bmap
, type1
);
9813 pos2
+= isl_basic_map_offset(bmap
, type2
);
9814 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9815 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9816 bmap
= isl_basic_map_finalize(bmap
);
9818 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9822 isl_basic_map_free(bmap
);
9827 /* Add a constraint imposing that the given two dimensions have opposite values.
9829 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9830 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9832 isl_basic_map
*bmap
= NULL
;
9838 if (pos1
>= isl_map_dim(map
, type1
))
9839 isl_die(map
->ctx
, isl_error_invalid
,
9840 "index out of bounds", goto error
);
9841 if (pos2
>= isl_map_dim(map
, type2
))
9842 isl_die(map
->ctx
, isl_error_invalid
,
9843 "index out of bounds", goto error
);
9845 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9846 i
= isl_basic_map_alloc_equality(bmap
);
9849 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9850 pos1
+= isl_basic_map_offset(bmap
, type1
);
9851 pos2
+= isl_basic_map_offset(bmap
, type2
);
9852 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9853 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9854 bmap
= isl_basic_map_finalize(bmap
);
9856 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9860 isl_basic_map_free(bmap
);
9865 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9869 isl_local_space
*ls
;
9874 if (!isl_basic_map_divs_known(bmap
))
9875 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9876 "some divs are unknown", return NULL
);
9878 ls
= isl_basic_map_get_local_space(bmap
);
9879 div
= isl_local_space_get_div(ls
, pos
);
9880 isl_local_space_free(ls
);
9885 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9888 return isl_basic_map_get_div(bset
, pos
);
9891 /* Plug in "subs" for dimension "type", "pos" of "bset".
9893 * Let i be the dimension to replace and let "subs" be of the form
9897 * Any integer division with a non-zero coefficient for i,
9899 * floor((a i + g)/m)
9903 * floor((a f + d g)/(m d))
9905 * Constraints of the form
9913 __isl_give isl_basic_set
*isl_basic_set_substitute(
9914 __isl_take isl_basic_set
*bset
,
9915 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9921 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9924 bset
= isl_basic_set_cow(bset
);
9928 ctx
= isl_basic_set_get_ctx(bset
);
9929 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9930 isl_die(ctx
, isl_error_invalid
,
9931 "spaces don't match", goto error
);
9932 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9933 isl_die(ctx
, isl_error_unsupported
,
9934 "cannot handle divs yet", goto error
);
9936 pos
+= isl_basic_set_offset(bset
, type
);
9940 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9941 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9943 isl_int_set(v
, bset
->eq
[i
][pos
]);
9944 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9945 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9946 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9949 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9950 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9952 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9953 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9954 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9955 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9958 for (i
= 0; i
< bset
->n_div
; ++i
) {
9959 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9961 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9962 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9963 isl_seq_combine(bset
->div
[i
] + 1,
9964 subs
->v
->el
[0], bset
->div
[i
] + 1,
9965 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9966 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9971 bset
= isl_basic_set_simplify(bset
);
9972 return isl_basic_set_finalize(bset
);
9974 isl_basic_set_free(bset
);
9978 /* Plug in "subs" for dimension "type", "pos" of "set".
9980 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9981 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9985 if (set
&& isl_set_plain_is_empty(set
))
9988 set
= isl_set_cow(set
);
9992 for (i
= set
->n
- 1; i
>= 0; --i
) {
9993 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9996 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
9997 isl_basic_set_free(set
->p
[i
]);
9998 if (i
!= set
->n
- 1)
9999 set
->p
[i
] = set
->p
[set
->n
- 1];