2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
38 case isl_dim_param
: return dim
->nparam
;
39 case isl_dim_in
: return dim
->n_in
;
40 case isl_dim_out
: return dim
->n_out
;
41 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
46 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return 1;
50 case isl_dim_in
: return 1 + dim
->nparam
;
51 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
57 enum isl_dim_type type
)
62 case isl_dim_cst
: return 1;
65 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
66 case isl_dim_div
: return bmap
->n_div
;
67 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
72 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
74 return map
? n(map
->dim
, type
) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
79 return set
? n(set
->dim
, type
) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
83 enum isl_dim_type type
)
85 isl_space
*dim
= bmap
->dim
;
87 case isl_dim_cst
: return 0;
88 case isl_dim_param
: return 1;
89 case isl_dim_in
: return 1 + dim
->nparam
;
90 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
91 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
96 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
97 enum isl_dim_type type
)
99 return isl_basic_map_offset(bset
, type
);
102 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
104 return pos(map
->dim
, type
);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
108 enum isl_dim_type type
)
110 return isl_basic_map_dim(bset
, type
);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
115 return isl_basic_set_dim(bset
, isl_dim_set
);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
120 return isl_basic_set_dim(bset
, isl_dim_param
);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
125 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
128 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
130 return isl_set_dim(set
, isl_dim_set
);
133 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
135 return isl_set_dim(set
, isl_dim_param
);
138 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
140 return bmap
? bmap
->dim
->n_in
: 0;
143 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
145 return bmap
? bmap
->dim
->n_out
: 0;
148 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
150 return bmap
? bmap
->dim
->nparam
: 0;
153 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
155 return bmap
? bmap
->n_div
: 0;
158 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
160 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
163 unsigned isl_map_n_in(const struct isl_map
*map
)
165 return map
? map
->dim
->n_in
: 0;
168 unsigned isl_map_n_out(const struct isl_map
*map
)
170 return map
? map
->dim
->n_out
: 0;
173 unsigned isl_map_n_param(const struct isl_map
*map
)
175 return map
? map
->dim
->nparam
: 0;
178 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
183 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
186 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
189 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
190 struct isl_basic_set
*bset
)
195 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
198 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
201 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
206 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
209 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
212 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
213 struct isl_basic_set
*bset
)
218 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
221 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
224 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
226 return bmap
? bmap
->ctx
: NULL
;
229 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
231 return bset
? bset
->ctx
: NULL
;
234 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
236 return map
? map
->ctx
: NULL
;
239 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
241 return set
? set
->ctx
: NULL
;
244 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
248 return isl_space_copy(bmap
->dim
);
251 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
255 return isl_space_copy(bset
->dim
);
258 __isl_give isl_local_space
*isl_basic_map_get_local_space(
259 __isl_keep isl_basic_map
*bmap
)
268 total
= isl_basic_map_total_dim(bmap
);
269 ls
= isl_local_space_alloc(isl_space_copy(bmap
->dim
), bmap
->n_div
);
273 for (i
= 0; i
< bmap
->n_div
; ++i
)
274 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
279 __isl_give isl_local_space
*isl_basic_set_get_local_space(
280 __isl_keep isl_basic_set
*bset
)
282 return isl_basic_map_get_local_space(bset
);
285 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
286 __isl_take isl_local_space
*ls
)
295 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
296 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
297 n_div
, 0, 2 * n_div
);
299 for (i
= 0; i
< n_div
; ++i
)
300 if (isl_basic_map_alloc_div(bmap
) < 0)
303 for (i
= 0; i
< n_div
; ++i
) {
304 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
305 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
309 isl_local_space_free(ls
);
312 isl_local_space_free(ls
);
313 isl_basic_map_free(bmap
);
317 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
318 __isl_take isl_local_space
*ls
)
320 return isl_basic_map_from_local_space(ls
);
323 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
327 return isl_space_copy(map
->dim
);
330 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
334 return isl_space_copy(set
->dim
);
337 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
338 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
340 bmap
= isl_basic_map_cow(bmap
);
343 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
346 bmap
= isl_basic_map_finalize(bmap
);
349 isl_basic_map_free(bmap
);
353 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
354 __isl_take isl_basic_set
*bset
, const char *s
)
356 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
359 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
360 enum isl_dim_type type
)
362 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
365 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
366 enum isl_dim_type type
, const char *s
)
370 map
= isl_map_cow(map
);
374 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
378 for (i
= 0; i
< map
->n
; ++i
) {
379 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
390 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
391 enum isl_dim_type type
)
393 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
396 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
399 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
402 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
403 enum isl_dim_type type
, __isl_take isl_id
*id
)
405 map
= isl_map_cow(map
);
407 return isl_id_free(id
);
409 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
411 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
414 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
415 __isl_take isl_id
*id
)
417 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
420 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
421 enum isl_dim_type type
)
423 map
= isl_map_cow(map
);
427 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
429 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
432 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
434 return isl_map_reset_tuple_id(set
, isl_dim_set
);
437 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
439 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
442 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
443 enum isl_dim_type type
)
445 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
448 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
450 return isl_map_has_tuple_id(set
, isl_dim_set
);
453 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
455 return isl_map_get_tuple_id(set
, isl_dim_set
);
458 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
460 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
463 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
465 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
468 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
469 enum isl_dim_type type
, unsigned pos
)
471 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
474 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
475 enum isl_dim_type type
, unsigned pos
)
477 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
480 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
481 enum isl_dim_type type
, unsigned pos
)
483 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
486 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
487 enum isl_dim_type type
, unsigned pos
)
489 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
492 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
493 __isl_take isl_basic_map
*bmap
,
494 enum isl_dim_type type
, unsigned pos
, const char *s
)
496 bmap
= isl_basic_map_cow(bmap
);
499 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
504 isl_basic_map_free(bmap
);
508 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
509 enum isl_dim_type type
, unsigned pos
, const char *s
)
513 map
= isl_map_cow(map
);
517 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
521 for (i
= 0; i
< map
->n
; ++i
) {
522 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
533 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
534 __isl_take isl_basic_set
*bset
,
535 enum isl_dim_type type
, unsigned pos
, const char *s
)
537 return (isl_basic_set
*)isl_basic_map_set_dim_name(
538 (isl_basic_map
*)bset
, type
, pos
, s
);
541 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
542 enum isl_dim_type type
, unsigned pos
, const char *s
)
544 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
547 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
548 enum isl_dim_type type
, unsigned pos
)
550 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
553 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
554 enum isl_dim_type type
, unsigned pos
)
556 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
559 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
560 enum isl_dim_type type
, unsigned pos
)
562 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
565 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
566 enum isl_dim_type type
, unsigned pos
)
568 return isl_map_has_dim_id(set
, type
, pos
);
571 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
572 enum isl_dim_type type
, unsigned pos
)
574 return isl_map_get_dim_id(set
, type
, pos
);
577 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
578 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
580 map
= isl_map_cow(map
);
582 return isl_id_free(id
);
584 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
586 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
589 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
590 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
592 return isl_map_set_dim_id(set
, type
, pos
, id
);
595 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
596 __isl_keep isl_id
*id
)
600 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
603 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
604 __isl_keep isl_id
*id
)
606 return isl_map_find_dim_by_id(set
, type
, id
);
609 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
614 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
617 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
620 return isl_map_find_dim_by_name(set
, type
, name
);
623 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
627 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
630 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
632 return isl_basic_map_is_rational(bset
);
635 /* Is this basic set a parameter domain?
637 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
641 return isl_space_is_params(bset
->dim
);
644 /* Is this set a parameter domain?
646 int isl_set_is_params(__isl_keep isl_set
*set
)
650 return isl_space_is_params(set
->dim
);
653 /* Is this map actually a parameter domain?
654 * Users should never call this function. Outside of isl,
655 * a map can never be a parameter domain.
657 int isl_map_is_params(__isl_keep isl_map
*map
)
661 return isl_space_is_params(map
->dim
);
664 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
665 struct isl_basic_map
*bmap
, unsigned extra
,
666 unsigned n_eq
, unsigned n_ineq
)
669 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
674 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
675 if (isl_blk_is_error(bmap
->block
))
678 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
683 bmap
->block2
= isl_blk_empty();
686 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
687 if (isl_blk_is_error(bmap
->block2
))
690 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
695 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
696 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
698 for (i
= 0; i
< extra
; ++i
)
699 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
703 bmap
->c_size
= n_eq
+ n_ineq
;
704 bmap
->eq
= bmap
->ineq
+ n_ineq
;
713 isl_basic_map_free(bmap
);
717 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
718 unsigned nparam
, unsigned dim
, unsigned extra
,
719 unsigned n_eq
, unsigned n_ineq
)
721 struct isl_basic_map
*bmap
;
724 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
728 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
729 return (struct isl_basic_set
*)bmap
;
732 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
733 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
735 struct isl_basic_map
*bmap
;
738 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
739 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
740 return (struct isl_basic_set
*)bmap
;
746 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
747 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
749 struct isl_basic_map
*bmap
;
753 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
758 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
764 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
765 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
766 unsigned n_eq
, unsigned n_ineq
)
768 struct isl_basic_map
*bmap
;
771 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
775 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
779 static void dup_constraints(
780 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
783 unsigned total
= isl_basic_map_total_dim(src
);
785 for (i
= 0; i
< src
->n_eq
; ++i
) {
786 int j
= isl_basic_map_alloc_equality(dst
);
787 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
790 for (i
= 0; i
< src
->n_ineq
; ++i
) {
791 int j
= isl_basic_map_alloc_inequality(dst
);
792 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
795 for (i
= 0; i
< src
->n_div
; ++i
) {
796 int j
= isl_basic_map_alloc_div(dst
);
797 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
799 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
802 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
804 struct isl_basic_map
*dup
;
808 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
809 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
812 dup_constraints(dup
, bmap
);
813 dup
->flags
= bmap
->flags
;
814 dup
->sample
= isl_vec_copy(bmap
->sample
);
818 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
820 struct isl_basic_map
*dup
;
822 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
823 return (struct isl_basic_set
*)dup
;
826 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
831 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
835 return isl_basic_set_dup(bset
);
838 struct isl_set
*isl_set_copy(struct isl_set
*set
)
847 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
852 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
856 bmap
= isl_basic_map_dup(bmap
);
858 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
862 struct isl_map
*isl_map_copy(struct isl_map
*map
)
871 void isl_basic_map_free(struct isl_basic_map
*bmap
)
879 isl_ctx_deref(bmap
->ctx
);
881 isl_blk_free(bmap
->ctx
, bmap
->block2
);
883 isl_blk_free(bmap
->ctx
, bmap
->block
);
884 isl_vec_free(bmap
->sample
);
885 isl_space_free(bmap
->dim
);
889 void isl_basic_set_free(struct isl_basic_set
*bset
)
891 isl_basic_map_free((struct isl_basic_map
*)bset
);
894 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
896 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
899 __isl_give isl_map
*isl_map_align_params_map_map_and(
900 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
901 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
902 __isl_take isl_map
*map2
))
906 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
907 return fn(map1
, map2
);
908 if (!isl_space_has_named_params(map1
->dim
) ||
909 !isl_space_has_named_params(map2
->dim
))
910 isl_die(map1
->ctx
, isl_error_invalid
,
911 "unaligned unnamed parameters", goto error
);
912 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
913 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
914 return fn(map1
, map2
);
921 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
922 __isl_keep isl_map
*map2
,
923 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
929 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
930 return fn(map1
, map2
);
931 if (!isl_space_has_named_params(map1
->dim
) ||
932 !isl_space_has_named_params(map2
->dim
))
933 isl_die(map1
->ctx
, isl_error_invalid
,
934 "unaligned unnamed parameters", return -1);
935 map1
= isl_map_copy(map1
);
936 map2
= isl_map_copy(map2
);
937 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
938 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
945 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
951 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
952 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
954 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
955 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
956 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
957 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
958 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
959 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
961 int j
= isl_basic_map_alloc_inequality(bmap
);
965 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
966 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
973 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
974 bmap
->extra
- bmap
->n_div
);
978 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
980 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
983 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
987 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
992 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
994 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
997 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1002 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1004 if (pos
!= bmap
->n_eq
- 1) {
1006 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1007 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1013 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1015 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1018 void isl_basic_map_inequality_to_equality(
1019 struct isl_basic_map
*bmap
, unsigned pos
)
1023 t
= bmap
->ineq
[pos
];
1024 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1025 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1030 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1031 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1032 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1033 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1036 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1038 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1041 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1043 struct isl_ctx
*ctx
;
1047 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1048 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1049 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1050 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1051 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1052 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1053 1 + isl_basic_map_total_dim(bmap
),
1054 bmap
->extra
- bmap
->n_div
);
1055 return bmap
->n_ineq
++;
1058 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1060 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1063 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1067 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1072 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1074 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1077 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1082 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1084 if (pos
!= bmap
->n_ineq
- 1) {
1085 t
= bmap
->ineq
[pos
];
1086 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1087 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1088 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1094 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1096 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1099 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1104 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1107 k
= isl_basic_map_alloc_equality(bmap
);
1110 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1113 isl_basic_map_free(bmap
);
1117 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1120 return (isl_basic_set
*)
1121 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1124 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1129 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1132 k
= isl_basic_map_alloc_inequality(bmap
);
1135 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1138 isl_basic_map_free(bmap
);
1142 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1145 return (isl_basic_set
*)
1146 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1149 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1153 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1154 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1155 1 + 1 + isl_basic_map_total_dim(bmap
),
1156 bmap
->extra
- bmap
->n_div
);
1157 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1158 return bmap
->n_div
++;
1161 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1163 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1166 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1170 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1175 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1177 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1180 /* Copy constraint from src to dst, putting the vars of src at offset
1181 * dim_off in dst and the divs of src at offset div_off in dst.
1182 * If both sets are actually map, then dim_off applies to the input
1185 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1186 struct isl_basic_map
*src_map
, isl_int
*src
,
1187 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1189 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1190 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1191 unsigned src_in
= isl_basic_map_n_in(src_map
);
1192 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1193 unsigned src_out
= isl_basic_map_n_out(src_map
);
1194 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1195 isl_int_set(dst
[0], src
[0]);
1196 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1197 if (dst_nparam
> src_nparam
)
1198 isl_seq_clr(dst
+1+src_nparam
,
1199 dst_nparam
- src_nparam
);
1200 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1201 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1203 isl_min(dst_in
-in_off
, src_in
));
1204 if (dst_in
-in_off
> src_in
)
1205 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1206 dst_in
- in_off
- src_in
);
1207 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1208 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1209 src
+1+src_nparam
+src_in
,
1210 isl_min(dst_out
-out_off
, src_out
));
1211 if (dst_out
-out_off
> src_out
)
1212 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1213 dst_out
- out_off
- src_out
);
1214 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1215 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1216 src
+1+src_nparam
+src_in
+src_out
,
1217 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1218 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1219 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1220 div_off
+src_map
->n_div
,
1221 dst_map
->n_div
- div_off
- src_map
->n_div
);
1224 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1225 struct isl_basic_map
*src_map
, isl_int
*src
,
1226 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1228 isl_int_set(dst
[0], src
[0]);
1229 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1232 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1233 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1238 if (!bmap1
|| !bmap2
)
1241 div_off
= bmap1
->n_div
;
1243 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1244 int i1
= isl_basic_map_alloc_equality(bmap1
);
1247 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1248 i_pos
, o_pos
, div_off
);
1251 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1252 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1255 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1256 i_pos
, o_pos
, div_off
);
1259 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1260 int i1
= isl_basic_map_alloc_div(bmap1
);
1263 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1264 i_pos
, o_pos
, div_off
);
1267 isl_basic_map_free(bmap2
);
1272 isl_basic_map_free(bmap1
);
1273 isl_basic_map_free(bmap2
);
1277 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1278 struct isl_basic_set
*bset2
, unsigned pos
)
1280 return (struct isl_basic_set
*)
1281 add_constraints((struct isl_basic_map
*)bset1
,
1282 (struct isl_basic_map
*)bset2
, 0, pos
);
1285 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1286 __isl_take isl_space
*dim
, unsigned extra
,
1287 unsigned n_eq
, unsigned n_ineq
)
1289 struct isl_basic_map
*ext
;
1299 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1300 base
->extra
>= base
->n_div
+ extra
;
1302 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1303 room_for_ineq(base
, n_ineq
)) {
1304 isl_space_free(dim
);
1308 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1309 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1310 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1311 extra
+= base
->extra
;
1313 n_ineq
+= base
->n_ineq
;
1315 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1321 ext
->sample
= isl_vec_copy(base
->sample
);
1322 flags
= base
->flags
;
1323 ext
= add_constraints(ext
, base
, 0, 0);
1326 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1332 isl_space_free(dim
);
1333 isl_basic_map_free(base
);
1337 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1338 __isl_take isl_space
*dim
, unsigned extra
,
1339 unsigned n_eq
, unsigned n_ineq
)
1341 return (struct isl_basic_set
*)
1342 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1343 extra
, n_eq
, n_ineq
);
1346 struct isl_basic_map
*isl_basic_map_extend_constraints(
1347 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1351 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1355 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1356 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1357 unsigned n_eq
, unsigned n_ineq
)
1359 struct isl_basic_map
*bmap
;
1364 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1368 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1371 isl_basic_map_free(base
);
1375 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1376 unsigned nparam
, unsigned dim
, unsigned extra
,
1377 unsigned n_eq
, unsigned n_ineq
)
1379 return (struct isl_basic_set
*)
1380 isl_basic_map_extend((struct isl_basic_map
*)base
,
1381 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1384 struct isl_basic_set
*isl_basic_set_extend_constraints(
1385 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1387 return (struct isl_basic_set
*)
1388 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1392 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1394 return (struct isl_basic_set
*)
1395 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1398 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1403 if (bmap
->ref
> 1) {
1405 bmap
= isl_basic_map_dup(bmap
);
1408 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1412 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1420 return isl_set_dup(set
);
1423 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1431 return isl_map_dup(map
);
1434 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1435 unsigned a_len
, unsigned b_len
)
1437 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1438 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1439 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1442 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1443 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1451 isl_assert(bmap
->ctx
,
1452 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1454 if (n1
== 0 || n2
== 0)
1457 bmap
= isl_basic_map_cow(bmap
);
1461 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1462 if (isl_blk_is_error(blk
))
1465 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1467 bmap
->eq
[i
] + pos
, n1
, n2
);
1469 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1471 bmap
->ineq
[i
] + pos
, n1
, n2
);
1473 for (i
= 0; i
< bmap
->n_div
; ++i
)
1475 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1477 isl_blk_free(bmap
->ctx
, blk
);
1479 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1480 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1481 return isl_basic_map_finalize(bmap
);
1483 isl_basic_map_free(bmap
);
1487 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1488 __isl_take isl_basic_set
*bset
, unsigned n
)
1493 nparam
= isl_basic_set_n_param(bset
);
1494 dim
= isl_basic_set_n_dim(bset
);
1495 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1497 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1499 isl_basic_set_free(bset
);
1503 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1509 total
= isl_basic_map_total_dim(bmap
);
1510 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1511 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1513 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1515 i
= isl_basic_map_alloc_equality(bmap
);
1519 isl_int_set_si(bmap
->eq
[i
][0], 1);
1520 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1521 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1522 isl_vec_free(bmap
->sample
);
1523 bmap
->sample
= NULL
;
1524 return isl_basic_map_finalize(bmap
);
1526 isl_basic_map_free(bmap
);
1530 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1532 return (struct isl_basic_set
*)
1533 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1536 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1539 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1540 isl_int
*t
= bmap
->div
[a
];
1541 bmap
->div
[a
] = bmap
->div
[b
];
1544 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1545 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1547 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1548 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1550 for (i
= 0; i
< bmap
->n_div
; ++i
)
1551 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1552 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1555 /* Eliminate the specified n dimensions starting at first from the
1556 * constraints using Fourier-Motzkin. The dimensions themselves
1559 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1560 enum isl_dim_type type
, unsigned first
, unsigned n
)
1569 map
= isl_map_cow(map
);
1573 for (i
= 0; i
< map
->n
; ++i
) {
1574 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1584 /* Eliminate the specified n dimensions starting at first from the
1585 * constraints using Fourier-Motzkin. The dimensions themselves
1588 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1589 enum isl_dim_type type
, unsigned first
, unsigned n
)
1591 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1594 /* Eliminate the specified n dimensions starting at first from the
1595 * constraints using Fourier-Motzkin. The dimensions themselves
1598 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1599 unsigned first
, unsigned n
)
1601 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1604 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1605 __isl_take isl_basic_map
*bmap
)
1609 bmap
= isl_basic_map_eliminate_vars(bmap
,
1610 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1614 return isl_basic_map_finalize(bmap
);
1617 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1618 __isl_take isl_basic_set
*bset
)
1620 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1621 (struct isl_basic_map
*)bset
);
1624 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1633 map
= isl_map_cow(map
);
1637 for (i
= 0; i
< map
->n
; ++i
) {
1638 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1648 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1650 return isl_map_remove_divs(set
);
1653 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1654 enum isl_dim_type type
, unsigned first
, unsigned n
)
1658 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1660 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1662 bmap
= isl_basic_map_eliminate_vars(bmap
,
1663 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1666 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1668 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1671 isl_basic_map_free(bmap
);
1675 /* Return true if the definition of the given div (recursively) involves
1676 * any of the given variables.
1678 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1679 unsigned first
, unsigned n
)
1682 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1684 if (isl_int_is_zero(bmap
->div
[div
][0]))
1686 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1689 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1690 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1692 if (div_involves_vars(bmap
, i
, first
, n
))
1699 /* Remove all divs (recursively) involving any of the given dimensions
1700 * in their definitions.
1702 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1703 __isl_take isl_basic_map
*bmap
,
1704 enum isl_dim_type type
, unsigned first
, unsigned n
)
1710 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1712 first
+= isl_basic_map_offset(bmap
, type
);
1714 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1715 if (!div_involves_vars(bmap
, i
, first
, n
))
1717 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1725 isl_basic_map_free(bmap
);
1729 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1730 enum isl_dim_type type
, unsigned first
, unsigned n
)
1739 map
= isl_map_cow(map
);
1743 for (i
= 0; i
< map
->n
; ++i
) {
1744 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1755 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1756 enum isl_dim_type type
, unsigned first
, unsigned n
)
1758 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1762 /* Does the desciption of "bmap" depend on the specified dimensions?
1763 * We also check whether the dimensions appear in any of the div definitions.
1764 * In principle there is no need for this check. If the dimensions appear
1765 * in a div definition, they also appear in the defining constraints of that
1768 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1769 enum isl_dim_type type
, unsigned first
, unsigned n
)
1776 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1777 isl_die(bmap
->ctx
, isl_error_invalid
,
1778 "index out of bounds", return -1);
1780 first
+= isl_basic_map_offset(bmap
, type
);
1781 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1782 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1784 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1785 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1787 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1788 if (isl_int_is_zero(bmap
->div
[i
][0]))
1790 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
1797 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1798 enum isl_dim_type type
, unsigned first
, unsigned n
)
1805 if (first
+ n
> isl_map_dim(map
, type
))
1806 isl_die(map
->ctx
, isl_error_invalid
,
1807 "index out of bounds", return -1);
1809 for (i
= 0; i
< map
->n
; ++i
) {
1810 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1812 if (involves
< 0 || involves
)
1819 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1820 enum isl_dim_type type
, unsigned first
, unsigned n
)
1822 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1825 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1826 enum isl_dim_type type
, unsigned first
, unsigned n
)
1828 return isl_map_involves_dims(set
, type
, first
, n
);
1831 /* Return true if the definition of the given div is unknown or depends
1834 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1837 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1839 if (isl_int_is_zero(bmap
->div
[div
][0]))
1842 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1843 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1845 if (div_is_unknown(bmap
, i
))
1852 /* Remove all divs that are unknown or defined in terms of unknown divs.
1854 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1855 __isl_take isl_basic_map
*bmap
)
1862 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1863 if (!div_is_unknown(bmap
, i
))
1865 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1874 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1883 map
= isl_map_cow(map
);
1887 for (i
= 0; i
< map
->n
; ++i
) {
1888 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1898 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1900 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1903 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1904 __isl_take isl_basic_set
*bset
,
1905 enum isl_dim_type type
, unsigned first
, unsigned n
)
1907 return (isl_basic_set
*)
1908 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1911 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1912 enum isl_dim_type type
, unsigned first
, unsigned n
)
1919 map
= isl_map_cow(map
);
1922 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1924 for (i
= 0; i
< map
->n
; ++i
) {
1925 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1926 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1930 map
= isl_map_drop(map
, type
, first
, n
);
1937 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1938 enum isl_dim_type type
, unsigned first
, unsigned n
)
1940 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1943 /* Project out n inputs starting at first using Fourier-Motzkin */
1944 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1945 unsigned first
, unsigned n
)
1947 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1950 static void dump_term(struct isl_basic_map
*bmap
,
1951 isl_int c
, int pos
, FILE *out
)
1954 unsigned in
= isl_basic_map_n_in(bmap
);
1955 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1956 unsigned nparam
= isl_basic_map_n_param(bmap
);
1958 isl_int_print(out
, c
, 0);
1960 if (!isl_int_is_one(c
))
1961 isl_int_print(out
, c
, 0);
1962 if (pos
< 1 + nparam
) {
1963 name
= isl_space_get_dim_name(bmap
->dim
,
1964 isl_dim_param
, pos
- 1);
1966 fprintf(out
, "%s", name
);
1968 fprintf(out
, "p%d", pos
- 1);
1969 } else if (pos
< 1 + nparam
+ in
)
1970 fprintf(out
, "i%d", pos
- 1 - nparam
);
1971 else if (pos
< 1 + nparam
+ dim
)
1972 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1974 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1978 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1979 int sign
, FILE *out
)
1983 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1987 for (i
= 0, first
= 1; i
< len
; ++i
) {
1988 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1991 fprintf(out
, " + ");
1993 isl_int_abs(v
, c
[i
]);
1994 dump_term(bmap
, v
, i
, out
);
2001 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2002 const char *op
, FILE *out
, int indent
)
2006 fprintf(out
, "%*s", indent
, "");
2008 dump_constraint_sign(bmap
, c
, 1, out
);
2009 fprintf(out
, " %s ", op
);
2010 dump_constraint_sign(bmap
, c
, -1, out
);
2014 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2015 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2017 fprintf(out
, "%*s", indent
, "");
2018 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2023 static void dump_constraints(struct isl_basic_map
*bmap
,
2024 isl_int
**c
, unsigned n
,
2025 const char *op
, FILE *out
, int indent
)
2029 for (i
= 0; i
< n
; ++i
)
2030 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2033 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2037 unsigned total
= isl_basic_map_total_dim(bmap
);
2039 for (j
= 0; j
< 1 + total
; ++j
) {
2040 if (isl_int_is_zero(exp
[j
]))
2042 if (!first
&& isl_int_is_pos(exp
[j
]))
2044 dump_term(bmap
, exp
[j
], j
, out
);
2049 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2053 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2054 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2056 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2057 fprintf(out
, "%*s", indent
, "");
2058 fprintf(out
, "e%d = [(", i
);
2059 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2061 isl_int_print(out
, bmap
->div
[i
][0], 0);
2062 fprintf(out
, "]\n");
2066 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2067 FILE *out
, int indent
)
2070 fprintf(out
, "null basic set\n");
2074 fprintf(out
, "%*s", indent
, "");
2075 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2076 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2077 bset
->extra
, bset
->flags
);
2078 dump((struct isl_basic_map
*)bset
, out
, indent
);
2081 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2082 FILE *out
, int indent
)
2085 fprintf(out
, "null basic map\n");
2089 fprintf(out
, "%*s", indent
, "");
2090 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2091 "flags: %x, n_name: %d\n",
2093 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2094 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2095 dump(bmap
, out
, indent
);
2098 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2103 total
= isl_basic_map_total_dim(bmap
);
2104 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2105 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2106 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2107 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2111 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2114 struct isl_set
*set
;
2118 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2119 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2120 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2121 sizeof(struct isl_set
) +
2122 (n
- 1) * sizeof(struct isl_basic_set
*));
2126 set
->ctx
= dim
->ctx
;
2127 isl_ctx_ref(set
->ctx
);
2135 isl_space_free(dim
);
2139 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2140 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2142 struct isl_set
*set
;
2145 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2149 set
= isl_set_alloc_space(dims
, n
, flags
);
2153 /* Make sure "map" has room for at least "n" more basic maps.
2155 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2158 struct isl_map
*grown
= NULL
;
2162 isl_assert(map
->ctx
, n
>= 0, goto error
);
2163 if (map
->n
+ n
<= map
->size
)
2165 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2168 for (i
= 0; i
< map
->n
; ++i
) {
2169 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2177 isl_map_free(grown
);
2182 /* Make sure "set" has room for at least "n" more basic sets.
2184 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2186 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2189 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2192 struct isl_set
*dup
;
2197 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2200 for (i
= 0; i
< set
->n
; ++i
)
2201 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2205 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2207 return isl_map_from_basic_map(bset
);
2210 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2212 struct isl_map
*map
;
2217 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2218 return isl_map_add_basic_map(map
, bmap
);
2221 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2222 __isl_take isl_basic_set
*bset
)
2224 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2225 (struct isl_basic_map
*)bset
);
2228 void isl_set_free(struct isl_set
*set
)
2238 isl_ctx_deref(set
->ctx
);
2239 for (i
= 0; i
< set
->n
; ++i
)
2240 isl_basic_set_free(set
->p
[i
]);
2241 isl_space_free(set
->dim
);
2245 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2250 fprintf(out
, "null set\n");
2254 fprintf(out
, "%*s", indent
, "");
2255 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2256 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2258 for (i
= 0; i
< set
->n
; ++i
) {
2259 fprintf(out
, "%*s", indent
, "");
2260 fprintf(out
, "basic set %d:\n", i
);
2261 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2265 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2270 fprintf(out
, "null map\n");
2274 fprintf(out
, "%*s", indent
, "");
2275 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2276 "flags: %x, n_name: %d\n",
2277 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2278 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2279 for (i
= 0; i
< map
->n
; ++i
) {
2280 fprintf(out
, "%*s", indent
, "");
2281 fprintf(out
, "basic map %d:\n", i
);
2282 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2286 struct isl_basic_map
*isl_basic_map_intersect_domain(
2287 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2289 struct isl_basic_map
*bmap_domain
;
2294 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2295 bset
->dim
, isl_dim_param
), goto error
);
2297 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2298 isl_assert(bset
->ctx
,
2299 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2301 bmap
= isl_basic_map_cow(bmap
);
2304 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2305 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2306 bmap_domain
= isl_basic_map_from_domain(bset
);
2307 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2309 bmap
= isl_basic_map_simplify(bmap
);
2310 return isl_basic_map_finalize(bmap
);
2312 isl_basic_map_free(bmap
);
2313 isl_basic_set_free(bset
);
2317 struct isl_basic_map
*isl_basic_map_intersect_range(
2318 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2320 struct isl_basic_map
*bmap_range
;
2325 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2326 bset
->dim
, isl_dim_param
), goto error
);
2328 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2329 isl_assert(bset
->ctx
,
2330 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2332 if (isl_basic_set_is_universe(bset
)) {
2333 isl_basic_set_free(bset
);
2337 bmap
= isl_basic_map_cow(bmap
);
2340 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2341 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2342 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2343 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2345 bmap
= isl_basic_map_simplify(bmap
);
2346 return isl_basic_map_finalize(bmap
);
2348 isl_basic_map_free(bmap
);
2349 isl_basic_set_free(bset
);
2353 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2359 total
= 1 + isl_basic_map_total_dim(bmap
);
2360 if (total
!= vec
->size
)
2365 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2366 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2367 if (!isl_int_is_zero(s
)) {
2373 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2374 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2375 if (isl_int_is_neg(s
)) {
2386 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2388 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2391 struct isl_basic_map
*isl_basic_map_intersect(
2392 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2394 struct isl_vec
*sample
= NULL
;
2396 if (!bmap1
|| !bmap2
)
2399 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2400 bmap2
->dim
, isl_dim_param
), goto error
);
2401 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2402 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2403 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2404 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2405 return isl_basic_map_intersect(bmap2
, bmap1
);
2407 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2408 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2409 isl_assert(bmap1
->ctx
,
2410 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2412 if (bmap1
->sample
&&
2413 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2414 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2415 sample
= isl_vec_copy(bmap1
->sample
);
2416 else if (bmap2
->sample
&&
2417 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2418 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2419 sample
= isl_vec_copy(bmap2
->sample
);
2421 bmap1
= isl_basic_map_cow(bmap1
);
2424 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2425 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2426 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2429 isl_vec_free(sample
);
2431 isl_vec_free(bmap1
->sample
);
2432 bmap1
->sample
= sample
;
2435 bmap1
= isl_basic_map_simplify(bmap1
);
2436 return isl_basic_map_finalize(bmap1
);
2439 isl_vec_free(sample
);
2440 isl_basic_map_free(bmap1
);
2441 isl_basic_map_free(bmap2
);
2445 struct isl_basic_set
*isl_basic_set_intersect(
2446 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2448 return (struct isl_basic_set
*)
2449 isl_basic_map_intersect(
2450 (struct isl_basic_map
*)bset1
,
2451 (struct isl_basic_map
*)bset2
);
2454 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2455 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2457 return isl_basic_set_intersect(bset1
, bset2
);
2460 /* Special case of isl_map_intersect, where both map1 and map2
2461 * are convex, without any divs and such that either map1 or map2
2462 * contains a single constraint. This constraint is then simply
2463 * added to the other map.
2465 static __isl_give isl_map
*map_intersect_add_constraint(
2466 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2468 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2469 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2470 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2471 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2473 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2474 return isl_map_intersect(map2
, map1
);
2476 isl_assert(map2
->ctx
,
2477 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2479 map1
= isl_map_cow(map1
);
2482 if (isl_map_plain_is_empty(map1
)) {
2486 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2487 if (map2
->p
[0]->n_eq
== 1)
2488 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2490 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2491 map2
->p
[0]->ineq
[0]);
2493 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2494 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2498 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2499 isl_basic_map_free(map1
->p
[0]);
2512 /* map2 may be either a parameter domain or a map living in the same
2515 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2516 __isl_take isl_map
*map2
)
2519 struct isl_map
*result
;
2525 if (isl_map_plain_is_empty(map1
) &&
2526 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2530 if (isl_map_plain_is_empty(map2
) &&
2531 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2536 if (map1
->n
== 1 && map2
->n
== 1 &&
2537 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2538 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2539 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2540 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2541 return map_intersect_add_constraint(map1
, map2
);
2543 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2544 isl_space_dim(map2
->dim
, isl_dim_param
))
2545 isl_assert(map1
->ctx
,
2546 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2548 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2549 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2550 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2552 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2553 map1
->n
* map2
->n
, flags
);
2556 for (i
= 0; i
< map1
->n
; ++i
)
2557 for (j
= 0; j
< map2
->n
; ++j
) {
2558 struct isl_basic_map
*part
;
2559 part
= isl_basic_map_intersect(
2560 isl_basic_map_copy(map1
->p
[i
]),
2561 isl_basic_map_copy(map2
->p
[j
]));
2562 if (isl_basic_map_is_empty(part
))
2563 isl_basic_map_free(part
);
2565 result
= isl_map_add_basic_map(result
, part
);
2578 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2579 __isl_take isl_map
*map2
)
2583 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2584 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2585 "spaces don't match", goto error
);
2586 return map_intersect_internal(map1
, map2
);
2593 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2594 __isl_take isl_map
*map2
)
2596 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2599 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2601 return (struct isl_set
*)
2602 isl_map_intersect((struct isl_map
*)set1
,
2603 (struct isl_map
*)set2
);
2606 /* map_intersect_internal accepts intersections
2607 * with parameter domains, so we can just call that function.
2609 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2610 __isl_take isl_set
*params
)
2612 return map_intersect_internal(map
, params
);
2615 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2616 __isl_take isl_map
*map2
)
2618 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2621 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2622 __isl_take isl_set
*params
)
2624 return isl_map_intersect_params(set
, params
);
2627 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2630 struct isl_basic_set
*bset
;
2635 bmap
= isl_basic_map_cow(bmap
);
2638 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2639 in
= isl_basic_map_n_in(bmap
);
2640 bset
= isl_basic_set_from_basic_map(bmap
);
2641 bset
= isl_basic_set_swap_vars(bset
, in
);
2642 return isl_basic_map_from_basic_set(bset
, dim
);
2645 static __isl_give isl_basic_map
*basic_map_space_reset(
2646 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2650 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2653 space
= isl_basic_map_get_space(bmap
);
2654 space
= isl_space_reset(space
, type
);
2655 bmap
= isl_basic_map_reset_space(bmap
, space
);
2659 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2660 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2663 struct isl_basic_map
*res
;
2664 struct isl_dim_map
*dim_map
;
2665 unsigned total
, off
;
2666 enum isl_dim_type t
;
2669 return basic_map_space_reset(bmap
, type
);
2674 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2676 total
= isl_basic_map_total_dim(bmap
) + n
;
2677 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2679 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2681 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2683 unsigned size
= isl_basic_map_dim(bmap
, t
);
2684 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2686 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2687 pos
, size
- pos
, off
+ pos
+ n
);
2689 off
+= isl_space_dim(res_dim
, t
);
2691 isl_dim_map_div(dim_map
, bmap
, off
);
2693 res
= isl_basic_map_alloc_space(res_dim
,
2694 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2695 if (isl_basic_map_is_rational(bmap
))
2696 res
= isl_basic_map_set_rational(res
);
2697 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2698 return isl_basic_map_finalize(res
);
2701 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2702 enum isl_dim_type type
, unsigned n
)
2706 return isl_basic_map_insert(bmap
, type
,
2707 isl_basic_map_dim(bmap
, type
), n
);
2710 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2711 enum isl_dim_type type
, unsigned n
)
2715 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2716 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2718 isl_basic_set_free(bset
);
2722 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2723 enum isl_dim_type type
)
2727 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2730 space
= isl_map_get_space(map
);
2731 space
= isl_space_reset(space
, type
);
2732 map
= isl_map_reset_space(map
, space
);
2736 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2737 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2742 return map_space_reset(map
, type
);
2744 map
= isl_map_cow(map
);
2748 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2752 for (i
= 0; i
< map
->n
; ++i
) {
2753 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2764 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2765 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2767 return isl_map_insert_dims(set
, type
, pos
, n
);
2770 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2771 enum isl_dim_type type
, unsigned n
)
2775 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2778 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2779 enum isl_dim_type type
, unsigned n
)
2783 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2784 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2790 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2791 __isl_take isl_basic_map
*bmap
,
2792 enum isl_dim_type dst_type
, unsigned dst_pos
,
2793 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2795 struct isl_dim_map
*dim_map
;
2796 struct isl_basic_map
*res
;
2797 enum isl_dim_type t
;
2798 unsigned total
, off
;
2805 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2808 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2811 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2813 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2814 pos(bmap
->dim
, src_type
) + src_pos
+
2815 ((src_type
< dst_type
) ? n
: 0)) {
2816 bmap
= isl_basic_map_cow(bmap
);
2820 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2821 src_type
, src_pos
, n
);
2825 bmap
= isl_basic_map_finalize(bmap
);
2830 total
= isl_basic_map_total_dim(bmap
);
2831 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2834 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2835 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2836 if (t
== dst_type
) {
2837 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2840 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2843 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2844 dst_pos
, size
- dst_pos
, off
);
2845 off
+= size
- dst_pos
;
2846 } else if (t
== src_type
) {
2847 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2850 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2851 src_pos
+ n
, size
- src_pos
- n
, off
);
2852 off
+= size
- src_pos
- n
;
2854 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2858 isl_dim_map_div(dim_map
, bmap
, off
);
2860 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2861 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2862 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2864 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2865 src_type
, src_pos
, n
);
2869 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2870 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2871 bmap
= isl_basic_map_finalize(bmap
);
2875 isl_basic_map_free(bmap
);
2879 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2880 enum isl_dim_type dst_type
, unsigned dst_pos
,
2881 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2883 return (isl_basic_set
*)isl_basic_map_move_dims(
2884 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2887 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2888 enum isl_dim_type dst_type
, unsigned dst_pos
,
2889 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2893 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2894 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2895 src_type
, src_pos
, n
);
2901 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2902 enum isl_dim_type dst_type
, unsigned dst_pos
,
2903 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2912 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2915 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2918 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2920 map
= isl_map_cow(map
);
2924 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2928 for (i
= 0; i
< map
->n
; ++i
) {
2929 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2931 src_type
, src_pos
, n
);
2942 /* Move the specified dimensions to the last columns right before
2943 * the divs. Don't change the dimension specification of bmap.
2944 * That's the responsibility of the caller.
2946 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2947 enum isl_dim_type type
, unsigned first
, unsigned n
)
2949 struct isl_dim_map
*dim_map
;
2950 struct isl_basic_map
*res
;
2951 enum isl_dim_type t
;
2952 unsigned total
, off
;
2956 if (pos(bmap
->dim
, type
) + first
+ n
==
2957 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2960 total
= isl_basic_map_total_dim(bmap
);
2961 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2964 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2965 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2967 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2970 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2971 first
, n
, total
- bmap
->n_div
- n
);
2972 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2973 first
+ n
, size
- (first
+ n
), off
);
2974 off
+= size
- (first
+ n
);
2976 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2980 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2982 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2983 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2984 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2988 /* Turn the n dimensions of type type, starting at first
2989 * into existentially quantified variables.
2991 __isl_give isl_basic_map
*isl_basic_map_project_out(
2992 __isl_take isl_basic_map
*bmap
,
2993 enum isl_dim_type type
, unsigned first
, unsigned n
)
3001 return basic_map_space_reset(bmap
, type
);
3006 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3007 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3009 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3012 bmap
= move_last(bmap
, type
, first
, n
);
3013 bmap
= isl_basic_map_cow(bmap
);
3017 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3018 old
= bmap
->block2
.data
;
3019 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3020 (bmap
->extra
+ n
) * (1 + row_size
));
3021 if (!bmap
->block2
.data
)
3023 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3026 for (i
= 0; i
< n
; ++i
) {
3027 new_div
[i
] = bmap
->block2
.data
+
3028 (bmap
->extra
+ i
) * (1 + row_size
);
3029 isl_seq_clr(new_div
[i
], 1 + row_size
);
3031 for (i
= 0; i
< bmap
->extra
; ++i
)
3032 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3034 bmap
->div
= new_div
;
3038 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3041 bmap
= isl_basic_map_simplify(bmap
);
3042 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3043 return isl_basic_map_finalize(bmap
);
3045 isl_basic_map_free(bmap
);
3049 /* Turn the n dimensions of type type, starting at first
3050 * into existentially quantified variables.
3052 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3053 enum isl_dim_type type
, unsigned first
, unsigned n
)
3055 return (isl_basic_set
*)isl_basic_map_project_out(
3056 (isl_basic_map
*)bset
, type
, first
, n
);
3059 /* Turn the n dimensions of type type, starting at first
3060 * into existentially quantified variables.
3062 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3063 enum isl_dim_type type
, unsigned first
, unsigned n
)
3071 return map_space_reset(map
, type
);
3073 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3075 map
= isl_map_cow(map
);
3079 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3083 for (i
= 0; i
< map
->n
; ++i
) {
3084 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3095 /* Turn the n dimensions of type type, starting at first
3096 * into existentially quantified variables.
3098 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3099 enum isl_dim_type type
, unsigned first
, unsigned n
)
3101 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3104 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3108 for (i
= 0; i
< n
; ++i
) {
3109 j
= isl_basic_map_alloc_div(bmap
);
3112 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3116 isl_basic_map_free(bmap
);
3120 struct isl_basic_map
*isl_basic_map_apply_range(
3121 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3123 isl_space
*dim_result
= NULL
;
3124 struct isl_basic_map
*bmap
;
3125 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3126 struct isl_dim_map
*dim_map1
, *dim_map2
;
3128 if (!bmap1
|| !bmap2
)
3131 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3132 isl_space_copy(bmap2
->dim
));
3134 n_in
= isl_basic_map_n_in(bmap1
);
3135 n_out
= isl_basic_map_n_out(bmap2
);
3136 n
= isl_basic_map_n_out(bmap1
);
3137 nparam
= isl_basic_map_n_param(bmap1
);
3139 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3140 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3141 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3142 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3143 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3144 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3145 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3146 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3147 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3148 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3149 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3151 bmap
= isl_basic_map_alloc_space(dim_result
,
3152 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3153 bmap1
->n_eq
+ bmap2
->n_eq
,
3154 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3155 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3156 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3157 bmap
= add_divs(bmap
, n
);
3158 bmap
= isl_basic_map_simplify(bmap
);
3159 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3160 return isl_basic_map_finalize(bmap
);
3162 isl_basic_map_free(bmap1
);
3163 isl_basic_map_free(bmap2
);
3167 struct isl_basic_set
*isl_basic_set_apply(
3168 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3173 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3176 return (struct isl_basic_set
*)
3177 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3179 isl_basic_set_free(bset
);
3180 isl_basic_map_free(bmap
);
3184 struct isl_basic_map
*isl_basic_map_apply_domain(
3185 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3187 if (!bmap1
|| !bmap2
)
3190 isl_assert(bmap1
->ctx
,
3191 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3192 isl_assert(bmap1
->ctx
,
3193 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3196 bmap1
= isl_basic_map_reverse(bmap1
);
3197 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3198 return isl_basic_map_reverse(bmap1
);
3200 isl_basic_map_free(bmap1
);
3201 isl_basic_map_free(bmap2
);
3205 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3206 * A \cap B -> f(A) + f(B)
3208 struct isl_basic_map
*isl_basic_map_sum(
3209 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3211 unsigned n_in
, n_out
, nparam
, total
, pos
;
3212 struct isl_basic_map
*bmap
= NULL
;
3213 struct isl_dim_map
*dim_map1
, *dim_map2
;
3216 if (!bmap1
|| !bmap2
)
3219 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3222 nparam
= isl_basic_map_n_param(bmap1
);
3223 n_in
= isl_basic_map_n_in(bmap1
);
3224 n_out
= isl_basic_map_n_out(bmap1
);
3226 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3227 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3228 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3229 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3230 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3231 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3232 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3233 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3234 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3235 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3236 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3238 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3239 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3240 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3241 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3242 for (i
= 0; i
< n_out
; ++i
) {
3243 int j
= isl_basic_map_alloc_equality(bmap
);
3246 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3247 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3248 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3249 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3251 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3252 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3253 bmap
= add_divs(bmap
, 2 * n_out
);
3255 bmap
= isl_basic_map_simplify(bmap
);
3256 return isl_basic_map_finalize(bmap
);
3258 isl_basic_map_free(bmap
);
3259 isl_basic_map_free(bmap1
);
3260 isl_basic_map_free(bmap2
);
3264 /* Given two maps A -> f(A) and B -> g(B), construct a map
3265 * A \cap B -> f(A) + f(B)
3267 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3269 struct isl_map
*result
;
3275 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3277 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3278 map1
->n
* map2
->n
, 0);
3281 for (i
= 0; i
< map1
->n
; ++i
)
3282 for (j
= 0; j
< map2
->n
; ++j
) {
3283 struct isl_basic_map
*part
;
3284 part
= isl_basic_map_sum(
3285 isl_basic_map_copy(map1
->p
[i
]),
3286 isl_basic_map_copy(map2
->p
[j
]));
3287 if (isl_basic_map_is_empty(part
))
3288 isl_basic_map_free(part
);
3290 result
= isl_map_add_basic_map(result
, part
);
3303 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3304 __isl_take isl_set
*set2
)
3306 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3309 /* Given a basic map A -> f(A), construct A -> -f(A).
3311 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3316 bmap
= isl_basic_map_cow(bmap
);
3320 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3321 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3322 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3323 for (j
= 0; j
< n
; ++j
)
3324 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3325 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3326 for (j
= 0; j
< n
; ++j
)
3327 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3328 for (i
= 0; i
< bmap
->n_div
; ++i
)
3329 for (j
= 0; j
< n
; ++j
)
3330 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3331 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3332 return isl_basic_map_finalize(bmap
);
3335 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3337 return isl_basic_map_neg(bset
);
3340 /* Given a map A -> f(A), construct A -> -f(A).
3342 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3346 map
= isl_map_cow(map
);
3350 for (i
= 0; i
< map
->n
; ++i
) {
3351 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3362 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3364 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3367 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3368 * A -> floor(f(A)/d).
3370 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3373 unsigned n_in
, n_out
, nparam
, total
, pos
;
3374 struct isl_basic_map
*result
= NULL
;
3375 struct isl_dim_map
*dim_map
;
3381 nparam
= isl_basic_map_n_param(bmap
);
3382 n_in
= isl_basic_map_n_in(bmap
);
3383 n_out
= isl_basic_map_n_out(bmap
);
3385 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3386 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3387 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3388 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3389 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3390 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3392 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3393 bmap
->n_div
+ n_out
,
3394 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3395 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3396 result
= add_divs(result
, n_out
);
3397 for (i
= 0; i
< n_out
; ++i
) {
3399 j
= isl_basic_map_alloc_inequality(result
);
3402 isl_seq_clr(result
->ineq
[j
], 1+total
);
3403 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3404 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3405 j
= isl_basic_map_alloc_inequality(result
);
3408 isl_seq_clr(result
->ineq
[j
], 1+total
);
3409 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3410 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3411 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3414 result
= isl_basic_map_simplify(result
);
3415 return isl_basic_map_finalize(result
);
3417 isl_basic_map_free(result
);
3421 /* Given a map A -> f(A) and an integer d, construct a map
3422 * A -> floor(f(A)/d).
3424 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3428 map
= isl_map_cow(map
);
3432 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3433 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3434 for (i
= 0; i
< map
->n
; ++i
) {
3435 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3446 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3452 i
= isl_basic_map_alloc_equality(bmap
);
3455 nparam
= isl_basic_map_n_param(bmap
);
3456 n_in
= isl_basic_map_n_in(bmap
);
3457 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3458 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3459 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3460 return isl_basic_map_finalize(bmap
);
3462 isl_basic_map_free(bmap
);
3466 /* Add a constraints to "bmap" expressing i_pos < o_pos
3468 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3474 i
= isl_basic_map_alloc_inequality(bmap
);
3477 nparam
= isl_basic_map_n_param(bmap
);
3478 n_in
= isl_basic_map_n_in(bmap
);
3479 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3480 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3481 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3482 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3483 return isl_basic_map_finalize(bmap
);
3485 isl_basic_map_free(bmap
);
3489 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3491 static __isl_give isl_basic_map
*var_less_or_equal(
3492 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3498 i
= isl_basic_map_alloc_inequality(bmap
);
3501 nparam
= isl_basic_map_n_param(bmap
);
3502 n_in
= isl_basic_map_n_in(bmap
);
3503 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3504 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3505 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3506 return isl_basic_map_finalize(bmap
);
3508 isl_basic_map_free(bmap
);
3512 /* Add a constraints to "bmap" expressing i_pos > o_pos
3514 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3520 i
= isl_basic_map_alloc_inequality(bmap
);
3523 nparam
= isl_basic_map_n_param(bmap
);
3524 n_in
= isl_basic_map_n_in(bmap
);
3525 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3526 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3527 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3528 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3529 return isl_basic_map_finalize(bmap
);
3531 isl_basic_map_free(bmap
);
3535 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3537 static __isl_give isl_basic_map
*var_more_or_equal(
3538 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3544 i
= isl_basic_map_alloc_inequality(bmap
);
3547 nparam
= isl_basic_map_n_param(bmap
);
3548 n_in
= isl_basic_map_n_in(bmap
);
3549 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3550 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3551 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3552 return isl_basic_map_finalize(bmap
);
3554 isl_basic_map_free(bmap
);
3558 __isl_give isl_basic_map
*isl_basic_map_equal(
3559 __isl_take isl_space
*dim
, unsigned n_equal
)
3562 struct isl_basic_map
*bmap
;
3563 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3566 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3567 bmap
= var_equal(bmap
, i
);
3568 return isl_basic_map_finalize(bmap
);
3571 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3573 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3577 struct isl_basic_map
*bmap
;
3578 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3581 for (i
= 0; i
< pos
&& bmap
; ++i
)
3582 bmap
= var_equal(bmap
, i
);
3584 bmap
= var_less(bmap
, pos
);
3585 return isl_basic_map_finalize(bmap
);
3588 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3590 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3591 __isl_take isl_space
*dim
, unsigned pos
)
3594 isl_basic_map
*bmap
;
3596 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3597 for (i
= 0; i
< pos
; ++i
)
3598 bmap
= var_equal(bmap
, i
);
3599 bmap
= var_less_or_equal(bmap
, pos
);
3600 return isl_basic_map_finalize(bmap
);
3603 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3605 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3609 struct isl_basic_map
*bmap
;
3610 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3613 for (i
= 0; i
< pos
&& bmap
; ++i
)
3614 bmap
= var_equal(bmap
, i
);
3616 bmap
= var_more(bmap
, pos
);
3617 return isl_basic_map_finalize(bmap
);
3620 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3622 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3623 __isl_take isl_space
*dim
, unsigned pos
)
3626 isl_basic_map
*bmap
;
3628 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3629 for (i
= 0; i
< pos
; ++i
)
3630 bmap
= var_equal(bmap
, i
);
3631 bmap
= var_more_or_equal(bmap
, pos
);
3632 return isl_basic_map_finalize(bmap
);
3635 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3636 unsigned n
, int equal
)
3638 struct isl_map
*map
;
3641 if (n
== 0 && equal
)
3642 return isl_map_universe(dims
);
3644 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3646 for (i
= 0; i
+ 1 < n
; ++i
)
3647 map
= isl_map_add_basic_map(map
,
3648 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3651 map
= isl_map_add_basic_map(map
,
3652 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3654 map
= isl_map_add_basic_map(map
,
3655 isl_basic_map_less_at(dims
, n
- 1));
3657 isl_space_free(dims
);
3662 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3666 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3669 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3671 return map_lex_lte_first(dim
, n
, 0);
3674 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3676 return map_lex_lte_first(dim
, n
, 1);
3679 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3681 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3684 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3686 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3689 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3690 unsigned n
, int equal
)
3692 struct isl_map
*map
;
3695 if (n
== 0 && equal
)
3696 return isl_map_universe(dims
);
3698 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3700 for (i
= 0; i
+ 1 < n
; ++i
)
3701 map
= isl_map_add_basic_map(map
,
3702 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3705 map
= isl_map_add_basic_map(map
,
3706 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3708 map
= isl_map_add_basic_map(map
,
3709 isl_basic_map_more_at(dims
, n
- 1));
3711 isl_space_free(dims
);
3716 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3720 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3723 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3725 return map_lex_gte_first(dim
, n
, 0);
3728 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3730 return map_lex_gte_first(dim
, n
, 1);
3733 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3735 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3738 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3740 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3743 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3744 __isl_take isl_set
*set2
)
3747 map
= isl_map_lex_le(isl_set_get_space(set1
));
3748 map
= isl_map_intersect_domain(map
, set1
);
3749 map
= isl_map_intersect_range(map
, set2
);
3753 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3754 __isl_take isl_set
*set2
)
3757 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3758 map
= isl_map_intersect_domain(map
, set1
);
3759 map
= isl_map_intersect_range(map
, set2
);
3763 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3764 __isl_take isl_set
*set2
)
3767 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3768 map
= isl_map_intersect_domain(map
, set1
);
3769 map
= isl_map_intersect_range(map
, set2
);
3773 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3774 __isl_take isl_set
*set2
)
3777 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3778 map
= isl_map_intersect_domain(map
, set1
);
3779 map
= isl_map_intersect_range(map
, set2
);
3783 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3784 __isl_take isl_map
*map2
)
3787 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3788 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3789 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3793 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3794 __isl_take isl_map
*map2
)
3797 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3798 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3799 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3803 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3804 __isl_take isl_map
*map2
)
3807 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3808 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3809 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3813 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3814 __isl_take isl_map
*map2
)
3817 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3818 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3819 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3823 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3824 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3826 struct isl_basic_map
*bmap
;
3828 bset
= isl_basic_set_cow(bset
);
3832 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3833 isl_space_free(bset
->dim
);
3834 bmap
= (struct isl_basic_map
*) bset
;
3836 return isl_basic_map_finalize(bmap
);
3838 isl_basic_set_free(bset
);
3839 isl_space_free(dim
);
3843 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3847 if (bmap
->dim
->n_in
== 0)
3848 return (struct isl_basic_set
*)bmap
;
3849 bmap
= isl_basic_map_cow(bmap
);
3852 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3855 bmap
= isl_basic_map_finalize(bmap
);
3856 return (struct isl_basic_set
*)bmap
;
3858 isl_basic_map_free(bmap
);
3862 /* For a div d = floor(f/m), add the constraints
3865 * -(f-(n-1)) + m d >= 0
3867 * Note that the second constraint is the negation of
3871 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3872 unsigned pos
, isl_int
*div
)
3875 unsigned total
= isl_basic_map_total_dim(bmap
);
3877 i
= isl_basic_map_alloc_inequality(bmap
);
3880 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3881 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3883 j
= isl_basic_map_alloc_inequality(bmap
);
3886 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3887 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3888 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3892 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3893 unsigned pos
, isl_int
*div
)
3895 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3899 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3901 unsigned total
= isl_basic_map_total_dim(bmap
);
3902 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3904 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3908 struct isl_basic_set
*isl_basic_map_underlying_set(
3909 struct isl_basic_map
*bmap
)
3913 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3915 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3916 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3917 return (struct isl_basic_set
*)bmap
;
3918 bmap
= isl_basic_map_cow(bmap
);
3921 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3924 bmap
->extra
-= bmap
->n_div
;
3926 bmap
= isl_basic_map_finalize(bmap
);
3927 return (struct isl_basic_set
*)bmap
;
3932 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3933 __isl_take isl_basic_set
*bset
)
3935 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3938 struct isl_basic_map
*isl_basic_map_overlying_set(
3939 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3941 struct isl_basic_map
*bmap
;
3942 struct isl_ctx
*ctx
;
3949 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3950 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3951 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3953 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3954 isl_basic_map_free(like
);
3955 return (struct isl_basic_map
*)bset
;
3957 bset
= isl_basic_set_cow(bset
);
3960 total
= bset
->dim
->n_out
+ bset
->extra
;
3961 bmap
= (struct isl_basic_map
*)bset
;
3962 isl_space_free(bmap
->dim
);
3963 bmap
->dim
= isl_space_copy(like
->dim
);
3966 bmap
->n_div
= like
->n_div
;
3967 bmap
->extra
+= like
->n_div
;
3971 ltotal
= total
- bmap
->extra
+ like
->extra
;
3974 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3975 bmap
->extra
* (1 + 1 + total
));
3976 if (isl_blk_is_error(bmap
->block2
))
3978 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3982 for (i
= 0; i
< bmap
->extra
; ++i
)
3983 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3984 for (i
= 0; i
< like
->n_div
; ++i
) {
3985 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3986 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3988 bmap
= isl_basic_map_extend_constraints(bmap
,
3989 0, 2 * like
->n_div
);
3990 for (i
= 0; i
< like
->n_div
; ++i
) {
3991 if (isl_int_is_zero(bmap
->div
[i
][0]))
3993 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3997 isl_basic_map_free(like
);
3998 bmap
= isl_basic_map_simplify(bmap
);
3999 bmap
= isl_basic_map_finalize(bmap
);
4002 isl_basic_map_free(like
);
4003 isl_basic_set_free(bset
);
4007 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4008 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4010 return (struct isl_basic_set
*)
4011 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4014 struct isl_set
*isl_set_from_underlying_set(
4015 struct isl_set
*set
, struct isl_basic_set
*like
)
4021 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4023 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4024 isl_basic_set_free(like
);
4027 set
= isl_set_cow(set
);
4030 for (i
= 0; i
< set
->n
; ++i
) {
4031 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4032 isl_basic_set_copy(like
));
4036 isl_space_free(set
->dim
);
4037 set
->dim
= isl_space_copy(like
->dim
);
4040 isl_basic_set_free(like
);
4043 isl_basic_set_free(like
);
4048 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4052 map
= isl_map_cow(map
);
4055 map
->dim
= isl_space_cow(map
->dim
);
4059 for (i
= 1; i
< map
->n
; ++i
)
4060 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4062 for (i
= 0; i
< map
->n
; ++i
) {
4063 map
->p
[i
] = (struct isl_basic_map
*)
4064 isl_basic_map_underlying_set(map
->p
[i
]);
4069 map
->dim
= isl_space_underlying(map
->dim
, 0);
4071 isl_space_free(map
->dim
);
4072 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4076 return (struct isl_set
*)map
;
4082 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4084 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4087 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4088 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4090 bmap
= isl_basic_map_cow(bmap
);
4094 isl_space_free(bmap
->dim
);
4097 bmap
= isl_basic_map_finalize(bmap
);
4101 isl_basic_map_free(bmap
);
4102 isl_space_free(dim
);
4106 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4107 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4109 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4113 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4114 __isl_take isl_space
*dim
)
4118 map
= isl_map_cow(map
);
4122 for (i
= 0; i
< map
->n
; ++i
) {
4123 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4124 isl_space_copy(dim
));
4128 isl_space_free(map
->dim
);
4134 isl_space_free(dim
);
4138 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4139 __isl_take isl_space
*dim
)
4141 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4144 /* Compute the parameter domain of the given basic set.
4146 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4151 if (isl_basic_set_is_params(bset
))
4154 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4155 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4156 space
= isl_basic_set_get_space(bset
);
4157 space
= isl_space_params(space
);
4158 bset
= isl_basic_set_reset_space(bset
, space
);
4162 /* Compute the parameter domain of the given set.
4164 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4169 if (isl_set_is_params(set
))
4172 n
= isl_set_dim(set
, isl_dim_set
);
4173 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4174 space
= isl_set_get_space(set
);
4175 space
= isl_space_params(space
);
4176 set
= isl_set_reset_space(set
, space
);
4180 /* Construct a zero-dimensional set with the given parameter domain.
4182 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4185 space
= isl_set_get_space(set
);
4186 space
= isl_space_set_from_params(space
);
4187 set
= isl_set_reset_space(set
, space
);
4191 /* Compute the parameter domain of the given map.
4193 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4198 n
= isl_map_dim(map
, isl_dim_in
);
4199 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4200 n
= isl_map_dim(map
, isl_dim_out
);
4201 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4202 space
= isl_map_get_space(map
);
4203 space
= isl_space_params(space
);
4204 map
= isl_map_reset_space(map
, space
);
4208 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4211 struct isl_basic_set
*domain
;
4217 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4219 n_in
= isl_basic_map_n_in(bmap
);
4220 n_out
= isl_basic_map_n_out(bmap
);
4221 domain
= isl_basic_set_from_basic_map(bmap
);
4222 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4224 domain
= isl_basic_set_reset_space(domain
, dim
);
4229 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4233 return isl_space_may_be_set(bmap
->dim
);
4236 /* Is this basic map actually a set?
4237 * Users should never call this function. Outside of isl,
4238 * the type should indicate whether something is a set or a map.
4240 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4244 return isl_space_is_set(bmap
->dim
);
4247 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4251 if (isl_basic_map_is_set(bmap
))
4253 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4256 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4257 __isl_take isl_basic_map
*bmap
)
4261 isl_basic_map
*domain
;
4262 int nparam
, n_in
, n_out
;
4265 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4266 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4267 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4269 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4270 domain
= isl_basic_map_universe(dim
);
4272 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4273 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4274 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4276 total
= isl_basic_map_total_dim(bmap
);
4278 for (i
= 0; i
< n_in
; ++i
) {
4279 k
= isl_basic_map_alloc_equality(bmap
);
4282 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4283 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4284 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4287 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4288 return isl_basic_map_finalize(bmap
);
4290 isl_basic_map_free(bmap
);
4294 __isl_give isl_basic_map
*isl_basic_map_range_map(
4295 __isl_take isl_basic_map
*bmap
)
4299 isl_basic_map
*range
;
4300 int nparam
, n_in
, n_out
;
4303 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4304 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4305 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4307 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4308 range
= isl_basic_map_universe(dim
);
4310 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4311 bmap
= isl_basic_map_apply_range(bmap
, range
);
4312 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4314 total
= isl_basic_map_total_dim(bmap
);
4316 for (i
= 0; i
< n_out
; ++i
) {
4317 k
= isl_basic_map_alloc_equality(bmap
);
4320 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4321 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4322 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4325 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4326 return isl_basic_map_finalize(bmap
);
4328 isl_basic_map_free(bmap
);
4332 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4336 return isl_space_may_be_set(map
->dim
);
4339 /* Is this map actually a set?
4340 * Users should never call this function. Outside of isl,
4341 * the type should indicate whether something is a set or a map.
4343 int isl_map_is_set(__isl_keep isl_map
*map
)
4347 return isl_space_is_set(map
->dim
);
4350 struct isl_set
*isl_map_range(struct isl_map
*map
)
4353 struct isl_set
*set
;
4357 if (isl_map_is_set(map
))
4358 return (isl_set
*)map
;
4360 map
= isl_map_cow(map
);
4364 set
= (struct isl_set
*) map
;
4365 set
->dim
= isl_space_range(set
->dim
);
4368 for (i
= 0; i
< map
->n
; ++i
) {
4369 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4373 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4374 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4381 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4384 isl_space
*domain_dim
;
4386 map
= isl_map_cow(map
);
4390 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4391 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4392 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4395 for (i
= 0; i
< map
->n
; ++i
) {
4396 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4400 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4401 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4408 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4411 isl_space
*range_dim
;
4413 map
= isl_map_cow(map
);
4417 range_dim
= isl_space_range(isl_map_get_space(map
));
4418 range_dim
= isl_space_from_range(range_dim
);
4419 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4420 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4423 for (i
= 0; i
< map
->n
; ++i
) {
4424 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4428 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4429 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4436 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4437 __isl_take isl_space
*dim
)
4440 struct isl_map
*map
= NULL
;
4442 set
= isl_set_cow(set
);
4445 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4446 map
= (struct isl_map
*)set
;
4447 for (i
= 0; i
< set
->n
; ++i
) {
4448 map
->p
[i
] = isl_basic_map_from_basic_set(
4449 set
->p
[i
], isl_space_copy(dim
));
4453 isl_space_free(map
->dim
);
4457 isl_space_free(dim
);
4462 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4463 __isl_take isl_basic_set
*bset
)
4465 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4468 __isl_give isl_basic_map
*isl_basic_map_from_range(
4469 __isl_take isl_basic_set
*bset
)
4472 space
= isl_basic_set_get_space(bset
);
4473 space
= isl_space_from_range(space
);
4474 bset
= isl_basic_set_reset_space(bset
, space
);
4475 return (isl_basic_map
*)bset
;
4478 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4481 space
= isl_set_get_space(set
);
4482 space
= isl_space_from_range(space
);
4483 set
= isl_set_reset_space(set
, space
);
4484 return (struct isl_map
*)set
;
4487 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4489 return isl_map_reverse(isl_map_from_range(set
));
4492 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4493 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4495 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4498 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4499 __isl_take isl_set
*range
)
4501 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4504 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4507 struct isl_set
*set
= NULL
;
4511 map
= isl_map_cow(map
);
4514 map
->dim
= isl_space_as_set_space(map
->dim
);
4517 set
= (struct isl_set
*)map
;
4518 for (i
= 0; i
< map
->n
; ++i
) {
4519 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4529 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4532 struct isl_map
*map
;
4537 isl_die(dim
->ctx
, isl_error_internal
,
4538 "negative number of basic maps", goto error
);
4539 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4540 sizeof(struct isl_map
) +
4541 (n
- 1) * sizeof(struct isl_basic_map
*));
4545 map
->ctx
= dim
->ctx
;
4546 isl_ctx_ref(map
->ctx
);
4554 isl_space_free(dim
);
4558 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4559 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4562 struct isl_map
*map
;
4565 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4569 map
= isl_map_alloc_space(dims
, n
, flags
);
4573 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4575 struct isl_basic_map
*bmap
;
4576 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4577 bmap
= isl_basic_map_set_to_empty(bmap
);
4581 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4583 struct isl_basic_set
*bset
;
4584 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4585 bset
= isl_basic_set_set_to_empty(bset
);
4589 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4591 struct isl_basic_map
*bmap
;
4594 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4595 bmap
= isl_basic_map_set_to_empty(bmap
);
4599 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4601 struct isl_basic_map
*bmap
;
4604 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4605 bmap
= isl_basic_map_set_to_empty(bmap
);
4609 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4611 struct isl_basic_set
*bset
;
4614 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4615 bset
= isl_basic_set_set_to_empty(bset
);
4619 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4621 struct isl_basic_map
*bmap
;
4622 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4623 bmap
= isl_basic_map_finalize(bmap
);
4627 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4629 struct isl_basic_set
*bset
;
4630 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4631 bset
= isl_basic_set_finalize(bset
);
4635 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4638 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4639 isl_basic_map
*bmap
;
4641 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4642 for (i
= 0; i
< total
; ++i
) {
4643 int k
= isl_basic_map_alloc_inequality(bmap
);
4646 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4647 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4651 isl_basic_map_free(bmap
);
4655 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4657 return isl_basic_map_nat_universe(dim
);
4660 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4662 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4665 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4667 return isl_map_nat_universe(dim
);
4670 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4671 __isl_keep isl_basic_map
*model
)
4675 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4678 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4682 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4685 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4686 __isl_keep isl_set
*model
)
4690 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4693 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4695 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4698 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4702 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4705 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4709 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4712 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4714 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4717 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4721 return isl_set_empty(isl_space_copy(model
->dim
));
4724 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4726 struct isl_map
*map
;
4729 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4730 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4734 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4736 struct isl_set
*set
;
4739 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4740 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4744 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4748 return isl_set_universe(isl_space_copy(model
->dim
));
4751 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4754 struct isl_map
*dup
;
4758 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4759 for (i
= 0; i
< map
->n
; ++i
)
4760 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4764 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4765 __isl_take isl_basic_map
*bmap
)
4769 if (isl_basic_map_plain_is_empty(bmap
)) {
4770 isl_basic_map_free(bmap
);
4773 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4774 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4775 map
->p
[map
->n
] = bmap
;
4777 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4783 isl_basic_map_free(bmap
);
4787 void isl_map_free(struct isl_map
*map
)
4797 isl_ctx_deref(map
->ctx
);
4798 for (i
= 0; i
< map
->n
; ++i
)
4799 isl_basic_map_free(map
->p
[i
]);
4800 isl_space_free(map
->dim
);
4804 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4805 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4809 base
= isl_map_cow(base
);
4813 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4816 for (i
= 0; i
< base
->n
; ++i
) {
4817 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4818 isl_space_copy(base
->dim
), 0, 0, 0);
4828 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4829 unsigned nparam
, unsigned dim
)
4831 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4835 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4836 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4840 bmap
= isl_basic_map_cow(bmap
);
4841 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4842 j
= isl_basic_map_alloc_equality(bmap
);
4845 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4846 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4847 isl_int_set_si(bmap
->eq
[j
][0], value
);
4848 bmap
= isl_basic_map_simplify(bmap
);
4849 return isl_basic_map_finalize(bmap
);
4851 isl_basic_map_free(bmap
);
4855 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4856 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4860 bmap
= isl_basic_map_cow(bmap
);
4861 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4862 j
= isl_basic_map_alloc_equality(bmap
);
4865 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4866 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4867 isl_int_set(bmap
->eq
[j
][0], value
);
4868 bmap
= isl_basic_map_simplify(bmap
);
4869 return isl_basic_map_finalize(bmap
);
4871 isl_basic_map_free(bmap
);
4875 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4876 enum isl_dim_type type
, unsigned pos
, int value
)
4880 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4881 return isl_basic_map_fix_pos_si(bmap
,
4882 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4884 isl_basic_map_free(bmap
);
4888 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4889 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4893 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4894 return isl_basic_map_fix_pos(bmap
,
4895 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4897 isl_basic_map_free(bmap
);
4901 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4902 enum isl_dim_type type
, unsigned pos
, int value
)
4904 return (struct isl_basic_set
*)
4905 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4909 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4910 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4912 return (struct isl_basic_set
*)
4913 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4917 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4918 unsigned input
, int value
)
4920 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4923 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4924 unsigned dim
, int value
)
4926 return (struct isl_basic_set
*)
4927 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4928 isl_dim_set
, dim
, value
);
4931 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4932 enum isl_dim_type type
, unsigned pos
, int value
)
4936 map
= isl_map_cow(map
);
4940 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4941 for (i
= 0; i
< map
->n
; ++i
) {
4942 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4946 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4953 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4954 enum isl_dim_type type
, unsigned pos
, int value
)
4956 return (struct isl_set
*)
4957 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4960 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4961 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4965 map
= isl_map_cow(map
);
4969 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4970 for (i
= 0; i
< map
->n
; ++i
) {
4971 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4975 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4982 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4983 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4985 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4988 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4989 unsigned input
, int value
)
4991 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4994 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4996 return (struct isl_set
*)
4997 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5000 static __isl_give isl_basic_map
*basic_map_bound_si(
5001 __isl_take isl_basic_map
*bmap
,
5002 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5008 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5009 pos
+= isl_basic_map_offset(bmap
, type
);
5010 bmap
= isl_basic_map_cow(bmap
);
5011 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5012 j
= isl_basic_map_alloc_inequality(bmap
);
5015 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5017 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5018 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5020 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5021 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5023 bmap
= isl_basic_map_simplify(bmap
);
5024 return isl_basic_map_finalize(bmap
);
5026 isl_basic_map_free(bmap
);
5030 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5031 __isl_take isl_basic_map
*bmap
,
5032 enum isl_dim_type type
, unsigned pos
, int value
)
5034 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5037 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5038 unsigned dim
, isl_int value
)
5042 bset
= isl_basic_set_cow(bset
);
5043 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5044 j
= isl_basic_set_alloc_inequality(bset
);
5047 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5048 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5049 isl_int_neg(bset
->ineq
[j
][0], value
);
5050 bset
= isl_basic_set_simplify(bset
);
5051 return isl_basic_set_finalize(bset
);
5053 isl_basic_set_free(bset
);
5057 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5058 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5062 map
= isl_map_cow(map
);
5066 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5067 for (i
= 0; i
< map
->n
; ++i
) {
5068 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5069 type
, pos
, value
, upper
);
5073 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5080 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5081 enum isl_dim_type type
, unsigned pos
, int value
)
5083 return map_bound_si(map
, type
, pos
, value
, 0);
5086 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5087 enum isl_dim_type type
, unsigned pos
, int value
)
5089 return map_bound_si(map
, type
, pos
, value
, 1);
5092 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5093 enum isl_dim_type type
, unsigned pos
, int value
)
5095 return (struct isl_set
*)
5096 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5099 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5100 enum isl_dim_type type
, unsigned pos
, int value
)
5102 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5105 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5110 set
= isl_set_cow(set
);
5114 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5115 for (i
= 0; i
< set
->n
; ++i
) {
5116 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5126 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5130 map
= isl_map_cow(map
);
5134 map
->dim
= isl_space_reverse(map
->dim
);
5137 for (i
= 0; i
< map
->n
; ++i
) {
5138 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5142 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5149 static struct isl_map
*isl_basic_map_partial_lexopt(
5150 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5151 struct isl_set
**empty
, int max
)
5155 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5156 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5158 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5160 isl_basic_map_free(bmap
);
5161 isl_basic_set_free(dom
);
5167 struct isl_map
*isl_basic_map_partial_lexmax(
5168 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5169 struct isl_set
**empty
)
5171 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5174 struct isl_map
*isl_basic_map_partial_lexmin(
5175 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5176 struct isl_set
**empty
)
5178 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5181 struct isl_set
*isl_basic_set_partial_lexmin(
5182 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5183 struct isl_set
**empty
)
5185 return (struct isl_set
*)
5186 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5190 struct isl_set
*isl_basic_set_partial_lexmax(
5191 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5192 struct isl_set
**empty
)
5194 return (struct isl_set
*)
5195 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5199 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5200 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5201 __isl_give isl_set
**empty
)
5203 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5206 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5207 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5208 __isl_give isl_set
**empty
)
5210 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5213 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5214 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5215 __isl_give isl_set
**empty
)
5217 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5220 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5221 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5222 __isl_give isl_set
**empty
)
5224 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5227 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5228 __isl_take isl_basic_map
*bmap
, int max
)
5230 isl_basic_set
*dom
= NULL
;
5231 isl_space
*dom_space
;
5235 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5236 dom
= isl_basic_set_universe(dom_space
);
5237 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5239 isl_basic_map_free(bmap
);
5243 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5244 __isl_take isl_basic_map
*bmap
)
5246 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5249 /* Given a basic map "bmap", compute the lexicographically minimal
5250 * (or maximal) image element for each domain element in dom.
5251 * Set *empty to those elements in dom that do not have an image element.
5253 * We first make sure the basic sets in dom are disjoint and then
5254 * simply collect the results over each of the basic sets separately.
5255 * We could probably improve the efficiency a bit by moving the union
5256 * domain down into the parametric integer programming.
5258 static __isl_give isl_map
*basic_map_partial_lexopt(
5259 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5260 __isl_give isl_set
**empty
, int max
)
5263 struct isl_map
*res
;
5265 dom
= isl_set_make_disjoint(dom
);
5269 if (isl_set_plain_is_empty(dom
)) {
5270 res
= isl_map_empty_like_basic_map(bmap
);
5271 *empty
= isl_set_empty_like(dom
);
5273 isl_basic_map_free(bmap
);
5277 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5278 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5280 for (i
= 1; i
< dom
->n
; ++i
) {
5281 struct isl_map
*res_i
;
5282 struct isl_set
*empty_i
;
5284 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5285 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5287 res
= isl_map_union_disjoint(res
, res_i
);
5288 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5292 isl_basic_map_free(bmap
);
5297 isl_basic_map_free(bmap
);
5301 /* Given a map "map", compute the lexicographically minimal
5302 * (or maximal) image element for each domain element in dom.
5303 * Set *empty to those elements in dom that do not have an image element.
5305 * We first compute the lexicographically minimal or maximal element
5306 * in the first basic map. This results in a partial solution "res"
5307 * and a subset "todo" of dom that still need to be handled.
5308 * We then consider each of the remaining maps in "map" and successively
5309 * improve both "res" and "todo".
5311 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5312 * Assume we are computing the lexicographical maximum.
5313 * We first compute the lexicographically maximal element in basic map i.
5314 * This results in a partial solution res_i and a subset todo_i.
5315 * Then we combine these results with those obtain for the first k basic maps
5316 * to obtain a result that is valid for the first k+1 basic maps.
5317 * In particular, the set where there is no solution is the set where
5318 * there is no solution for the first k basic maps and also no solution
5319 * for the ith basic map, i.e.,
5321 * todo^i = todo^k * todo_i
5323 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5324 * solutions, arbitrarily breaking ties in favor of res^k.
5325 * That is, when res^k(a) >= res_i(a), we pick res^k and
5326 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5327 * the lexicographic order.)
5328 * In practice, we compute
5330 * res^k * (res_i . "<=")
5334 * res_i * (res^k . "<")
5336 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5337 * where only one of res^k and res_i provides a solution and we simply pick
5344 * Note that we only compute these intersections when dom(res^k) intersects
5345 * dom(res_i). Otherwise, the only effect of these intersections is to
5346 * potentially break up res^k and res_i into smaller pieces.
5347 * We want to avoid such splintering as much as possible.
5348 * In fact, an earlier implementation of this function would look for
5349 * better results in the domain of res^k and for extra results in todo^k,
5350 * but this would always result in a splintering according to todo^k,
5351 * even when the domain of basic map i is disjoint from the domains of
5352 * the previous basic maps.
5354 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5355 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5356 __isl_give isl_set
**empty
, int max
)
5359 struct isl_map
*res
;
5360 struct isl_set
*todo
;
5365 if (isl_map_plain_is_empty(map
)) {
5373 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5374 isl_set_copy(dom
), &todo
, max
);
5376 for (i
= 1; i
< map
->n
; ++i
) {
5380 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5382 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5383 isl_set_copy(dom
), &todo_i
, max
);
5386 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5387 le
= isl_map_lex_le(dim
);
5389 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5390 le
= isl_map_lex_ge(dim
);
5392 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5393 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5394 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5395 le
= isl_map_intersect(le
, isl_map_copy(res
));
5397 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5398 res
= isl_map_intersect_domain(res
,
5399 isl_set_copy(todo_i
));
5400 res_i
= isl_map_intersect_domain(res_i
,
5401 isl_set_copy(todo
));
5404 res
= isl_map_union_disjoint(res
, res_i
);
5405 res
= isl_map_union_disjoint(res
, lt
);
5406 res
= isl_map_union_disjoint(res
, le
);
5408 todo
= isl_set_intersect(todo
, todo_i
);
5428 /* Given a map "map", compute the lexicographically minimal
5429 * (or maximal) image element for each domain element in dom.
5430 * Set *empty to those elements in dom that do not have an image element.
5432 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5434 static __isl_give isl_map
*isl_map_partial_lexopt(
5435 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5436 __isl_give isl_set
**empty
, int max
)
5440 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5441 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5442 if (!isl_space_has_named_params(map
->dim
) ||
5443 !isl_space_has_named_params(dom
->dim
))
5444 isl_die(map
->ctx
, isl_error_invalid
,
5445 "unaligned unnamed parameters", goto error
);
5446 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5447 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5448 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5457 __isl_give isl_map
*isl_map_partial_lexmax(
5458 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5459 __isl_give isl_set
**empty
)
5461 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5464 __isl_give isl_map
*isl_map_partial_lexmin(
5465 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5466 __isl_give isl_set
**empty
)
5468 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5471 __isl_give isl_set
*isl_set_partial_lexmin(
5472 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5473 __isl_give isl_set
**empty
)
5475 return (struct isl_set
*)
5476 isl_map_partial_lexmin((struct isl_map
*)set
,
5480 __isl_give isl_set
*isl_set_partial_lexmax(
5481 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5482 __isl_give isl_set
**empty
)
5484 return (struct isl_set
*)
5485 isl_map_partial_lexmax((struct isl_map
*)set
,
5489 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5491 struct isl_basic_set
*dom
= NULL
;
5496 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5497 dom
= isl_basic_set_universe(dom_dim
);
5498 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5500 isl_basic_map_free(bmap
);
5504 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5506 return isl_basic_map_lexopt(bmap
, 0);
5509 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5511 return isl_basic_map_lexopt(bmap
, 1);
5514 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5516 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5519 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5521 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5524 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5526 struct isl_set
*dom
= NULL
;
5531 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5532 dom
= isl_set_universe(dom_dim
);
5533 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5539 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5541 return isl_map_lexopt(map
, 0);
5544 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5546 return isl_map_lexopt(map
, 1);
5549 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5551 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5554 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5556 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5559 /* Extract the first and only affine expression from list
5560 * and then add it to *pwaff with the given dom.
5561 * This domain is known to be disjoint from other domains
5562 * because of the way isl_basic_map_foreach_lexmax works.
5564 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5565 __isl_take isl_aff_list
*list
, void *user
)
5567 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5569 isl_pw_aff
**pwaff
= user
;
5570 isl_pw_aff
*pwaff_i
;
5572 if (isl_aff_list_n_aff(list
) != 1)
5573 isl_die(ctx
, isl_error_internal
,
5574 "expecting single element list", goto error
);
5576 aff
= isl_aff_list_get_aff(list
, 0);
5577 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5579 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5581 isl_aff_list_free(list
);
5585 isl_basic_set_free(dom
);
5586 isl_aff_list_free(list
);
5590 /* Given a basic map with one output dimension, compute the minimum or
5591 * maximum of that dimension as an isl_pw_aff.
5593 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5594 * call update_dim_opt on each leaf of the result.
5596 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5599 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5603 dim
= isl_space_from_domain(isl_space_domain(dim
));
5604 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5605 pwaff
= isl_pw_aff_empty(dim
);
5607 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5609 return isl_pw_aff_free(pwaff
);
5614 /* Compute the minimum or maximum of the given output dimension
5615 * as a function of the parameters and the input dimensions,
5616 * but independently of the other output dimensions.
5618 * We first project out the other output dimension and then compute
5619 * the "lexicographic" maximum in each basic map, combining the results
5620 * using isl_pw_aff_union_max.
5622 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5629 n_out
= isl_map_dim(map
, isl_dim_out
);
5630 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5631 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5636 isl_space
*dim
= isl_map_get_space(map
);
5637 dim
= isl_space_domain(isl_space_from_range(dim
));
5639 return isl_pw_aff_empty(dim
);
5642 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5643 for (i
= 1; i
< map
->n
; ++i
) {
5644 isl_pw_aff
*pwaff_i
;
5646 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5647 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5655 /* Compute the maximum of the given output dimension as a function of the
5656 * parameters and input dimensions, but independently of
5657 * the other output dimensions.
5659 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5661 return map_dim_opt(map
, pos
, 1);
5664 /* Compute the minimum or maximum of the given set dimension
5665 * as a function of the parameters,
5666 * but independently of the other set dimensions.
5668 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5671 return map_dim_opt(set
, pos
, max
);
5674 /* Compute the maximum of the given set dimension as a function of the
5675 * parameters, but independently of the other set dimensions.
5677 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5679 return set_dim_opt(set
, pos
, 1);
5682 /* Compute the minimum of the given set dimension as a function of the
5683 * parameters, but independently of the other set dimensions.
5685 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5687 return set_dim_opt(set
, pos
, 0);
5690 /* Apply a preimage specified by "mat" on the parameters of "bset".
5691 * bset is assumed to have only parameters and divs.
5693 static struct isl_basic_set
*basic_set_parameter_preimage(
5694 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5701 bset
->dim
= isl_space_cow(bset
->dim
);
5705 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5707 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5709 bset
->dim
->nparam
= 0;
5710 bset
->dim
->n_out
= nparam
;
5711 bset
= isl_basic_set_preimage(bset
, mat
);
5713 bset
->dim
->nparam
= bset
->dim
->n_out
;
5714 bset
->dim
->n_out
= 0;
5719 isl_basic_set_free(bset
);
5723 /* Apply a preimage specified by "mat" on the parameters of "set".
5724 * set is assumed to have only parameters and divs.
5726 static struct isl_set
*set_parameter_preimage(
5727 struct isl_set
*set
, struct isl_mat
*mat
)
5729 isl_space
*dim
= NULL
;
5735 dim
= isl_space_copy(set
->dim
);
5736 dim
= isl_space_cow(dim
);
5740 nparam
= isl_set_dim(set
, isl_dim_param
);
5742 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5745 dim
->n_out
= nparam
;
5746 isl_set_reset_space(set
, dim
);
5747 set
= isl_set_preimage(set
, mat
);
5750 dim
= isl_space_copy(set
->dim
);
5751 dim
= isl_space_cow(dim
);
5754 dim
->nparam
= dim
->n_out
;
5756 isl_set_reset_space(set
, dim
);
5759 isl_space_free(dim
);
5766 /* Intersect the basic set "bset" with the affine space specified by the
5767 * equalities in "eq".
5769 static struct isl_basic_set
*basic_set_append_equalities(
5770 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5778 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5783 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5784 for (i
= 0; i
< eq
->n_row
; ++i
) {
5785 k
= isl_basic_set_alloc_equality(bset
);
5788 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5789 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5793 bset
= isl_basic_set_gauss(bset
, NULL
);
5794 bset
= isl_basic_set_finalize(bset
);
5799 isl_basic_set_free(bset
);
5803 /* Intersect the set "set" with the affine space specified by the
5804 * equalities in "eq".
5806 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5814 for (i
= 0; i
< set
->n
; ++i
) {
5815 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5828 /* Project the given basic set onto its parameter domain, possibly introducing
5829 * new, explicit, existential variables in the constraints.
5830 * The input has parameters and (possibly implicit) existential variables.
5831 * The output has the same parameters, but only
5832 * explicit existentially quantified variables.
5834 * The actual projection is performed by pip, but pip doesn't seem
5835 * to like equalities very much, so we first remove the equalities
5836 * among the parameters by performing a variable compression on
5837 * the parameters. Afterward, an inverse transformation is performed
5838 * and the equalities among the parameters are inserted back in.
5840 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5844 struct isl_mat
*T
, *T2
;
5845 struct isl_set
*set
;
5846 unsigned nparam
, n_div
;
5848 bset
= isl_basic_set_cow(bset
);
5852 if (bset
->n_eq
== 0)
5853 return isl_basic_set_lexmin(bset
);
5855 isl_basic_set_gauss(bset
, NULL
);
5857 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5858 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5860 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5861 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5864 if (i
== bset
->n_eq
)
5865 return isl_basic_set_lexmin(bset
);
5867 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5869 eq
= isl_mat_cow(eq
);
5870 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5871 if (T
&& T
->n_col
== 0) {
5875 bset
= isl_basic_set_set_to_empty(bset
);
5876 return isl_set_from_basic_set(bset
);
5878 bset
= basic_set_parameter_preimage(bset
, T
);
5880 set
= isl_basic_set_lexmin(bset
);
5881 set
= set_parameter_preimage(set
, T2
);
5882 set
= set_append_equalities(set
, eq
);
5886 /* Compute an explicit representation for all the existentially
5887 * quantified variables.
5888 * The input and output dimensions are first turned into parameters.
5889 * compute_divs then returns a map with the same parameters and
5890 * no input or output dimensions and the dimension specification
5891 * is reset to that of the input.
5893 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5895 struct isl_basic_set
*bset
;
5896 struct isl_set
*set
;
5897 struct isl_map
*map
;
5898 isl_space
*dim
, *orig_dim
= NULL
;
5903 bmap
= isl_basic_map_cow(bmap
);
5907 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5908 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5909 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5910 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5914 orig_dim
= bmap
->dim
;
5916 bset
= (struct isl_basic_set
*)bmap
;
5918 set
= parameter_compute_divs(bset
);
5919 map
= (struct isl_map
*)set
;
5920 map
= isl_map_reset_space(map
, orig_dim
);
5924 isl_basic_map_free(bmap
);
5928 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5936 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5937 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5938 if (isl_int_is_zero(bmap
->div
[i
][0]))
5940 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5946 static int map_divs_known(__isl_keep isl_map
*map
)
5953 for (i
= 0; i
< map
->n
; ++i
) {
5954 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5962 /* If bmap contains any unknown divs, then compute explicit
5963 * expressions for them. However, this computation may be
5964 * quite expensive, so first try to remove divs that aren't
5967 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5970 struct isl_map
*map
;
5972 known
= isl_basic_map_divs_known(bmap
);
5976 return isl_map_from_basic_map(bmap
);
5978 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5980 known
= isl_basic_map_divs_known(bmap
);
5984 return isl_map_from_basic_map(bmap
);
5986 map
= compute_divs(bmap
);
5989 isl_basic_map_free(bmap
);
5993 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5997 struct isl_map
*res
;
6004 known
= map_divs_known(map
);
6012 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6013 for (i
= 1 ; i
< map
->n
; ++i
) {
6015 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6016 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6017 res
= isl_map_union_disjoint(res
, r2
);
6019 res
= isl_map_union(res
, r2
);
6026 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6028 return (struct isl_set
*)
6029 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6032 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6034 return (struct isl_set
*)
6035 isl_map_compute_divs((struct isl_map
*)set
);
6038 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6041 struct isl_set
*set
;
6046 map
= isl_map_cow(map
);
6050 set
= (struct isl_set
*)map
;
6051 set
->dim
= isl_space_domain(set
->dim
);
6054 for (i
= 0; i
< map
->n
; ++i
) {
6055 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6059 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6060 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6067 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6068 __isl_take isl_map
*map2
)
6072 struct isl_map
*map
= NULL
;
6086 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6088 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6089 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6090 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6092 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6093 map1
->n
+ map2
->n
, flags
);
6096 for (i
= 0; i
< map1
->n
; ++i
) {
6097 map
= isl_map_add_basic_map(map
,
6098 isl_basic_map_copy(map1
->p
[i
]));
6102 for (i
= 0; i
< map2
->n
; ++i
) {
6103 map
= isl_map_add_basic_map(map
,
6104 isl_basic_map_copy(map2
->p
[i
]));
6118 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6119 __isl_take isl_map
*map2
)
6121 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6124 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6126 map1
= isl_map_union_disjoint(map1
, map2
);
6130 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6134 struct isl_set
*isl_set_union_disjoint(
6135 struct isl_set
*set1
, struct isl_set
*set2
)
6137 return (struct isl_set
*)
6138 isl_map_union_disjoint(
6139 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6142 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6144 return (struct isl_set
*)
6145 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6148 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6149 __isl_take isl_set
*set
)
6152 struct isl_map
*result
;
6158 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6159 isl_die(set
->ctx
, isl_error_invalid
,
6160 "parameters don't match", goto error
);
6162 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6163 !isl_map_compatible_range(map
, set
))
6164 isl_die(set
->ctx
, isl_error_invalid
,
6165 "incompatible spaces", goto error
);
6167 if (isl_set_plain_is_universe(set
)) {
6172 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6173 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6174 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6176 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6177 map
->n
* set
->n
, flags
);
6180 for (i
= 0; i
< map
->n
; ++i
)
6181 for (j
= 0; j
< set
->n
; ++j
) {
6182 result
= isl_map_add_basic_map(result
,
6183 isl_basic_map_intersect_range(
6184 isl_basic_map_copy(map
->p
[i
]),
6185 isl_basic_set_copy(set
->p
[j
])));
6198 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6199 __isl_take isl_set
*set
)
6201 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6204 struct isl_map
*isl_map_intersect_domain(
6205 struct isl_map
*map
, struct isl_set
*set
)
6207 return isl_map_reverse(
6208 isl_map_intersect_range(isl_map_reverse(map
), set
));
6211 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6212 __isl_take isl_map
*map2
)
6216 map1
= isl_map_reverse(map1
);
6217 map1
= isl_map_apply_range(map1
, map2
);
6218 return isl_map_reverse(map1
);
6225 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6226 __isl_take isl_map
*map2
)
6228 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6231 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6232 __isl_take isl_map
*map2
)
6234 isl_space
*dim_result
;
6235 struct isl_map
*result
;
6241 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6242 isl_space_copy(map2
->dim
));
6244 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6247 for (i
= 0; i
< map1
->n
; ++i
)
6248 for (j
= 0; j
< map2
->n
; ++j
) {
6249 result
= isl_map_add_basic_map(result
,
6250 isl_basic_map_apply_range(
6251 isl_basic_map_copy(map1
->p
[i
]),
6252 isl_basic_map_copy(map2
->p
[j
])));
6258 if (result
&& result
->n
<= 1)
6259 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6267 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6268 __isl_take isl_map
*map2
)
6270 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6274 * returns range - domain
6276 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6278 isl_space
*dims
, *target_dim
;
6279 struct isl_basic_set
*bset
;
6286 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6287 bmap
->dim
, isl_dim_out
),
6289 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6290 dim
= isl_basic_map_n_in(bmap
);
6291 nparam
= isl_basic_map_n_param(bmap
);
6292 bset
= isl_basic_set_from_basic_map(bmap
);
6293 bset
= isl_basic_set_cow(bset
);
6294 dims
= isl_basic_set_get_space(bset
);
6295 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6296 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6297 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6298 for (i
= 0; i
< dim
; ++i
) {
6299 int j
= isl_basic_map_alloc_equality(
6300 (struct isl_basic_map
*)bset
);
6303 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6304 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6305 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6306 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6308 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6309 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6312 isl_basic_map_free(bmap
);
6317 * returns range - domain
6319 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6323 struct isl_set
*result
;
6328 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6329 map
->dim
, isl_dim_out
),
6331 dim
= isl_map_get_space(map
);
6332 dim
= isl_space_domain(dim
);
6333 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6336 for (i
= 0; i
< map
->n
; ++i
)
6337 result
= isl_set_add_basic_set(result
,
6338 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6347 * returns [domain -> range] -> range - domain
6349 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6350 __isl_take isl_basic_map
*bmap
)
6354 isl_basic_map
*domain
;
6358 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6359 isl_die(bmap
->ctx
, isl_error_invalid
,
6360 "domain and range don't match", goto error
);
6362 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6363 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6365 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6366 domain
= isl_basic_map_universe(dim
);
6368 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6369 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6370 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6372 total
= isl_basic_map_total_dim(bmap
);
6374 for (i
= 0; i
< n
; ++i
) {
6375 k
= isl_basic_map_alloc_equality(bmap
);
6378 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6379 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6380 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6381 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6384 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6385 return isl_basic_map_finalize(bmap
);
6387 isl_basic_map_free(bmap
);
6392 * returns [domain -> range] -> range - domain
6394 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6397 isl_space
*domain_dim
;
6402 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6403 isl_die(map
->ctx
, isl_error_invalid
,
6404 "domain and range don't match", goto error
);
6406 map
= isl_map_cow(map
);
6410 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6411 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6412 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6415 for (i
= 0; i
< map
->n
; ++i
) {
6416 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6420 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6427 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6429 struct isl_basic_map
*bmap
;
6437 nparam
= dims
->nparam
;
6439 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6443 for (i
= 0; i
< dim
; ++i
) {
6444 int j
= isl_basic_map_alloc_equality(bmap
);
6447 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6448 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6449 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6451 return isl_basic_map_finalize(bmap
);
6453 isl_basic_map_free(bmap
);
6457 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6461 if (dim
->n_in
!= dim
->n_out
)
6462 isl_die(dim
->ctx
, isl_error_invalid
,
6463 "number of input and output dimensions needs to be "
6464 "the same", goto error
);
6465 return basic_map_identity(dim
);
6467 isl_space_free(dim
);
6471 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6473 if (!model
|| !model
->dim
)
6475 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6478 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6480 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6483 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6485 if (!model
|| !model
->dim
)
6487 return isl_map_identity(isl_space_copy(model
->dim
));
6490 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6492 if (!model
|| !model
->dim
)
6494 return isl_map_identity(isl_space_copy(model
->dim
));
6497 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6499 isl_space
*dim
= isl_set_get_space(set
);
6501 id
= isl_map_identity(isl_space_map_from_set(dim
));
6502 return isl_map_intersect_range(id
, set
);
6505 /* Construct a basic set with all set dimensions having only non-negative
6508 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6509 __isl_take isl_space
*space
)
6514 struct isl_basic_set
*bset
;
6518 nparam
= space
->nparam
;
6520 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6523 for (i
= 0; i
< dim
; ++i
) {
6524 int k
= isl_basic_set_alloc_inequality(bset
);
6527 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6528 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6532 isl_basic_set_free(bset
);
6536 /* Construct the half-space x_pos >= 0.
6538 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6542 isl_basic_set
*nonneg
;
6544 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6545 k
= isl_basic_set_alloc_inequality(nonneg
);
6548 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6549 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6551 return isl_basic_set_finalize(nonneg
);
6553 isl_basic_set_free(nonneg
);
6557 /* Construct the half-space x_pos <= -1.
6559 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6564 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6565 k
= isl_basic_set_alloc_inequality(neg
);
6568 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6569 isl_int_set_si(neg
->ineq
[k
][0], -1);
6570 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6572 return isl_basic_set_finalize(neg
);
6574 isl_basic_set_free(neg
);
6578 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6579 enum isl_dim_type type
, unsigned first
, unsigned n
)
6582 isl_basic_set
*nonneg
;
6590 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6592 for (i
= 0; i
< n
; ++i
) {
6593 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6594 pos(set
->dim
, type
) + first
+ i
);
6595 neg
= neg_halfspace(isl_set_get_space(set
),
6596 pos(set
->dim
, type
) + first
+ i
);
6598 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6607 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6608 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6615 if (isl_set_plain_is_empty(set
)) {
6620 return fn(set
, signs
, user
);
6623 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6625 half
= isl_set_intersect(half
, isl_set_copy(set
));
6626 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6630 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6632 half
= isl_set_intersect(half
, set
);
6633 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6639 /* Call "fn" on the intersections of "set" with each of the orthants
6640 * (except for obviously empty intersections). The orthant is identified
6641 * by the signs array, with each entry having value 1 or -1 according
6642 * to the sign of the corresponding variable.
6644 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6645 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6655 if (isl_set_plain_is_empty(set
))
6658 nparam
= isl_set_dim(set
, isl_dim_param
);
6659 nvar
= isl_set_dim(set
, isl_dim_set
);
6661 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6663 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6671 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6673 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6676 int isl_basic_map_is_subset(
6677 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6680 struct isl_map
*map1
;
6681 struct isl_map
*map2
;
6683 if (!bmap1
|| !bmap2
)
6686 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6687 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6689 is_subset
= isl_map_is_subset(map1
, map2
);
6697 int isl_basic_map_is_equal(
6698 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6702 if (!bmap1
|| !bmap2
)
6704 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6707 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6711 int isl_basic_set_is_equal(
6712 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6714 return isl_basic_map_is_equal(
6715 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6718 int isl_map_is_empty(struct isl_map
*map
)
6725 for (i
= 0; i
< map
->n
; ++i
) {
6726 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6735 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6737 return map
? map
->n
== 0 : -1;
6740 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6742 return isl_map_plain_is_empty(map
);
6745 int isl_set_plain_is_empty(struct isl_set
*set
)
6747 return set
? set
->n
== 0 : -1;
6750 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6752 return isl_set_plain_is_empty(set
);
6755 int isl_set_is_empty(struct isl_set
*set
)
6757 return isl_map_is_empty((struct isl_map
*)set
);
6760 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6765 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6768 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6773 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6776 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6782 is_subset
= isl_map_is_subset(map1
, map2
);
6785 is_subset
= isl_map_is_subset(map2
, map1
);
6789 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6791 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6794 int isl_basic_map_is_strict_subset(
6795 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6799 if (!bmap1
|| !bmap2
)
6801 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6804 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6805 if (is_subset
== -1)
6810 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6816 is_subset
= isl_map_is_subset(map1
, map2
);
6819 is_subset
= isl_map_is_subset(map2
, map1
);
6820 if (is_subset
== -1)
6825 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6827 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6830 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6834 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6837 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6841 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6844 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6851 for (i
= 0; i
< map
->n
; ++i
) {
6852 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6860 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6862 return isl_map_plain_is_universe((isl_map
*) set
);
6865 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6867 return isl_set_plain_is_universe(set
);
6870 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6872 struct isl_basic_set
*bset
= NULL
;
6873 struct isl_vec
*sample
= NULL
;
6880 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6883 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6884 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6885 copy
= isl_basic_map_remove_redundancies(copy
);
6886 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6887 isl_basic_map_free(copy
);
6891 total
= 1 + isl_basic_map_total_dim(bmap
);
6892 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6893 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6899 isl_vec_free(bmap
->sample
);
6900 bmap
->sample
= NULL
;
6901 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6904 sample
= isl_basic_set_sample_vec(bset
);
6907 empty
= sample
->size
== 0;
6908 isl_vec_free(bmap
->sample
);
6909 bmap
->sample
= sample
;
6911 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6916 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6920 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6923 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6925 return isl_basic_map_plain_is_empty(bmap
);
6928 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6932 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6935 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6937 return isl_basic_set_plain_is_empty(bset
);
6940 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6942 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6945 struct isl_map
*isl_basic_map_union(
6946 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6948 struct isl_map
*map
;
6949 if (!bmap1
|| !bmap2
)
6952 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6954 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6957 map
= isl_map_add_basic_map(map
, bmap1
);
6958 map
= isl_map_add_basic_map(map
, bmap2
);
6961 isl_basic_map_free(bmap1
);
6962 isl_basic_map_free(bmap2
);
6966 struct isl_set
*isl_basic_set_union(
6967 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6969 return (struct isl_set
*)isl_basic_map_union(
6970 (struct isl_basic_map
*)bset1
,
6971 (struct isl_basic_map
*)bset2
);
6974 /* Order divs such that any div only depends on previous divs */
6975 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6983 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6985 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6987 if (isl_int_is_zero(bmap
->div
[i
][0]))
6989 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6993 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6999 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7001 return (struct isl_basic_set
*)
7002 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7005 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7012 for (i
= 0; i
< map
->n
; ++i
) {
7013 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7024 /* Apply the expansion computed by isl_merge_divs.
7025 * The expansion itself is given by "exp" while the resulting
7026 * list of divs is given by "div".
7028 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7029 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7034 bset
= isl_basic_set_cow(bset
);
7038 if (div
->n_row
< bset
->n_div
)
7039 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7040 "not an expansion", goto error
);
7042 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7043 div
->n_row
- bset
->n_div
, 0,
7044 2 * (div
->n_row
- bset
->n_div
));
7046 n_div
= bset
->n_div
;
7047 for (i
= n_div
; i
< div
->n_row
; ++i
)
7048 if (isl_basic_set_alloc_div(bset
) < 0)
7052 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7053 if (j
>= 0 && exp
[j
] == i
) {
7055 isl_basic_map_swap_div(bset
, i
, j
);
7058 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7059 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7067 isl_basic_set_free(bset
);
7072 /* Look for a div in dst that corresponds to the div "div" in src.
7073 * The divs before "div" in src and dst are assumed to be the same.
7075 * Returns -1 if no corresponding div was found and the position
7076 * of the corresponding div in dst otherwise.
7078 static int find_div(struct isl_basic_map
*dst
,
7079 struct isl_basic_map
*src
, unsigned div
)
7083 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7085 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7086 for (i
= div
; i
< dst
->n_div
; ++i
)
7087 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7088 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7089 dst
->n_div
- div
) == -1)
7094 struct isl_basic_map
*isl_basic_map_align_divs(
7095 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7098 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7103 if (src
->n_div
== 0)
7106 for (i
= 0; i
< src
->n_div
; ++i
)
7107 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7109 src
= isl_basic_map_order_divs(src
);
7110 dst
= isl_basic_map_cow(dst
);
7111 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7112 src
->n_div
, 0, 2 * src
->n_div
);
7115 for (i
= 0; i
< src
->n_div
; ++i
) {
7116 int j
= find_div(dst
, src
, i
);
7118 j
= isl_basic_map_alloc_div(dst
);
7121 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7122 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7123 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7127 isl_basic_map_swap_div(dst
, i
, j
);
7131 isl_basic_map_free(dst
);
7135 struct isl_basic_set
*isl_basic_set_align_divs(
7136 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7138 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7139 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7142 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7150 map
= isl_map_compute_divs(map
);
7151 map
= isl_map_cow(map
);
7155 for (i
= 1; i
< map
->n
; ++i
)
7156 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7157 for (i
= 1; i
< map
->n
; ++i
)
7158 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7160 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7164 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7166 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7169 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7170 __isl_take isl_map
*map
)
7174 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7175 map
= isl_map_intersect_domain(map
, set
);
7176 set
= isl_map_range(map
);
7184 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7185 __isl_take isl_map
*map
)
7187 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7190 /* There is no need to cow as removing empty parts doesn't change
7191 * the meaning of the set.
7193 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7200 for (i
= map
->n
-1; i
>= 0; --i
) {
7201 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7203 isl_basic_map_free(map
->p
[i
]);
7204 if (i
!= map
->n
-1) {
7205 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7206 map
->p
[i
] = map
->p
[map
->n
-1];
7214 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7216 return (struct isl_set
*)
7217 isl_map_remove_empty_parts((struct isl_map
*)set
);
7220 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7222 struct isl_basic_map
*bmap
;
7223 if (!map
|| map
->n
== 0)
7225 bmap
= map
->p
[map
->n
-1];
7226 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7227 return isl_basic_map_copy(bmap
);
7230 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7232 return (struct isl_basic_set
*)
7233 isl_map_copy_basic_map((struct isl_map
*)set
);
7236 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7237 __isl_keep isl_basic_map
*bmap
)
7243 for (i
= map
->n
-1; i
>= 0; --i
) {
7244 if (map
->p
[i
] != bmap
)
7246 map
= isl_map_cow(map
);
7249 isl_basic_map_free(map
->p
[i
]);
7250 if (i
!= map
->n
-1) {
7251 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7252 map
->p
[i
] = map
->p
[map
->n
-1];
7263 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7264 struct isl_basic_set
*bset
)
7266 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7267 (struct isl_basic_map
*)bset
);
7270 /* Given two basic sets bset1 and bset2, compute the maximal difference
7271 * between the values of dimension pos in bset1 and those in bset2
7272 * for any common value of the parameters and dimensions preceding pos.
7274 static enum isl_lp_result
basic_set_maximal_difference_at(
7275 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7276 int pos
, isl_int
*opt
)
7279 struct isl_basic_map
*bmap1
= NULL
;
7280 struct isl_basic_map
*bmap2
= NULL
;
7281 struct isl_ctx
*ctx
;
7282 struct isl_vec
*obj
;
7285 unsigned dim1
, dim2
;
7286 enum isl_lp_result res
;
7288 if (!bset1
|| !bset2
)
7289 return isl_lp_error
;
7291 nparam
= isl_basic_set_n_param(bset1
);
7292 dim1
= isl_basic_set_n_dim(bset1
);
7293 dim2
= isl_basic_set_n_dim(bset2
);
7294 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7295 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7296 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7297 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7298 if (!bmap1
|| !bmap2
)
7300 bmap1
= isl_basic_map_cow(bmap1
);
7301 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7302 pos
, (dim1
- pos
) + (dim2
- pos
),
7303 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7304 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7307 total
= isl_basic_map_total_dim(bmap1
);
7309 obj
= isl_vec_alloc(ctx
, 1 + total
);
7310 isl_seq_clr(obj
->block
.data
, 1 + total
);
7311 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7312 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7315 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7317 isl_basic_map_free(bmap1
);
7321 isl_basic_map_free(bmap1
);
7322 isl_basic_map_free(bmap2
);
7323 return isl_lp_error
;
7326 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7327 * for any common value of the parameters and dimensions preceding pos
7328 * in both basic sets, the values of dimension pos in bset1 are
7329 * smaller or larger than those in bset2.
7332 * 1 if bset1 follows bset2
7333 * -1 if bset1 precedes bset2
7334 * 0 if bset1 and bset2 are incomparable
7335 * -2 if some error occurred.
7337 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7338 struct isl_basic_set
*bset2
, int pos
)
7341 enum isl_lp_result res
;
7346 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7348 if (res
== isl_lp_empty
)
7350 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7351 res
== isl_lp_unbounded
)
7353 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7362 /* Given two basic sets bset1 and bset2, check whether
7363 * for any common value of the parameters and dimensions preceding pos
7364 * there is a value of dimension pos in bset1 that is larger
7365 * than a value of the same dimension in bset2.
7368 * 1 if there exists such a pair
7369 * 0 if there is no such pair, but there is a pair of equal values
7371 * -2 if some error occurred.
7373 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7374 __isl_keep isl_basic_set
*bset2
, int pos
)
7377 enum isl_lp_result res
;
7382 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7384 if (res
== isl_lp_empty
)
7386 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7387 res
== isl_lp_unbounded
)
7389 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7391 else if (res
== isl_lp_ok
)
7400 /* Given two sets set1 and set2, check whether
7401 * for any common value of the parameters and dimensions preceding pos
7402 * there is a value of dimension pos in set1 that is larger
7403 * than a value of the same dimension in set2.
7406 * 1 if there exists such a pair
7407 * 0 if there is no such pair, but there is a pair of equal values
7409 * -2 if some error occurred.
7411 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7412 __isl_keep isl_set
*set2
, int pos
)
7420 for (i
= 0; i
< set1
->n
; ++i
)
7421 for (j
= 0; j
< set2
->n
; ++j
) {
7423 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7424 if (f
== 1 || f
== -2)
7433 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7434 unsigned pos
, isl_int
*val
)
7442 total
= isl_basic_map_total_dim(bmap
);
7443 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7444 for (; d
+1 > pos
; --d
)
7445 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7449 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7451 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7453 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7456 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7462 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7463 unsigned pos
, isl_int
*val
)
7475 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7478 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7479 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7480 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7481 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7485 isl_int_set(*val
, v
);
7491 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7492 unsigned pos
, isl_int
*val
)
7494 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7498 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7501 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7504 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7505 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7507 if (pos
>= isl_basic_map_dim(bmap
, type
))
7509 return isl_basic_map_plain_has_fixed_var(bmap
,
7510 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7513 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7514 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7516 if (pos
>= isl_map_dim(map
, type
))
7518 return isl_map_plain_has_fixed_var(map
,
7519 map_offset(map
, type
) - 1 + pos
, val
);
7522 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7523 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7525 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7528 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7529 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7531 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7534 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7535 * then return this fixed value in *val.
7537 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7538 unsigned dim
, isl_int
*val
)
7540 return isl_basic_set_plain_has_fixed_var(bset
,
7541 isl_basic_set_n_param(bset
) + dim
, val
);
7544 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7545 * then return this fixed value in *val.
7547 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7548 unsigned dim
, isl_int
*val
)
7550 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7553 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7554 unsigned dim
, isl_int
*val
)
7556 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7559 /* Check if input variable in has fixed value and if so and if val is not NULL,
7560 * then return this fixed value in *val.
7562 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7563 unsigned in
, isl_int
*val
)
7565 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7568 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7569 * and if val is not NULL, then return this lower bound in *val.
7571 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7572 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7574 int i
, i_eq
= -1, i_ineq
= -1;
7581 total
= isl_basic_set_total_dim(bset
);
7582 nparam
= isl_basic_set_n_param(bset
);
7583 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7584 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7590 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7591 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7593 if (i_eq
!= -1 || i_ineq
!= -1)
7597 if (i_eq
== -1 && i_ineq
== -1)
7599 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7600 /* The coefficient should always be one due to normalization. */
7601 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7603 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7605 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7606 total
- nparam
- dim
- 1) != -1)
7609 isl_int_neg(*val
, c
[0]);
7613 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7614 unsigned dim
, isl_int
*val
)
7626 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7630 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7632 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7633 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7635 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7639 isl_int_set(*val
, v
);
7650 /* uset_gist depends on constraints without existentially quantified
7651 * variables sorting first.
7653 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7655 const struct constraint
*c1
= (const struct constraint
*)p1
;
7656 const struct constraint
*c2
= (const struct constraint
*)p2
;
7658 unsigned size
= isl_min(c1
->size
, c2
->size
);
7660 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7661 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7666 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7669 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7670 struct isl_basic_map
*bmap
)
7673 struct constraint
*c
;
7678 total
= isl_basic_map_total_dim(bmap
);
7679 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7682 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7684 c
[i
].c
= bmap
->ineq
[i
];
7686 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7687 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7688 bmap
->ineq
[i
] = c
[i
].c
;
7692 isl_basic_map_free(bmap
);
7696 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7697 __isl_take isl_basic_set
*bset
)
7699 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7700 (struct isl_basic_map
*)bset
);
7703 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7707 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7709 bmap
= isl_basic_map_remove_redundancies(bmap
);
7710 bmap
= isl_basic_map_sort_constraints(bmap
);
7711 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7715 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7717 return (struct isl_basic_set
*)isl_basic_map_normalize(
7718 (struct isl_basic_map
*)bset
);
7721 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7722 const __isl_keep isl_basic_map
*bmap2
)
7729 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7730 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7731 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7732 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7733 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7734 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7735 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7736 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7737 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7738 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7739 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7741 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7743 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7745 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7746 return bmap1
->n_eq
- bmap2
->n_eq
;
7747 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7748 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7749 if (bmap1
->n_div
!= bmap2
->n_div
)
7750 return bmap1
->n_div
- bmap2
->n_div
;
7751 total
= isl_basic_map_total_dim(bmap1
);
7752 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7753 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7757 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7758 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7762 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7763 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7770 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7771 const __isl_keep isl_basic_set
*bset2
)
7773 return isl_basic_map_plain_cmp(bset1
, bset2
);
7776 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7777 const __isl_keep isl_set
*set2
)
7783 if (set1
->n
!= set2
->n
)
7784 return set1
->n
- set2
->n
;
7786 for (i
= 0; i
< set1
->n
; ++i
) {
7787 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7795 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7796 __isl_keep isl_basic_map
*bmap2
)
7798 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7801 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7802 __isl_keep isl_basic_set
*bset2
)
7804 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7805 (isl_basic_map
*)bset2
);
7808 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7810 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7811 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7813 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7816 /* We normalize in place, but if anything goes wrong we need
7817 * to return NULL, so we need to make sure we don't change the
7818 * meaning of any possible other copies of map.
7820 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7823 struct isl_basic_map
*bmap
;
7827 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7829 for (i
= 0; i
< map
->n
; ++i
) {
7830 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7833 isl_basic_map_free(map
->p
[i
]);
7836 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7837 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7838 map
= isl_map_remove_empty_parts(map
);
7841 for (i
= map
->n
- 1; i
>= 1; --i
) {
7842 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7844 isl_basic_map_free(map
->p
[i
-1]);
7845 for (j
= i
; j
< map
->n
; ++j
)
7846 map
->p
[j
-1] = map
->p
[j
];
7856 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7858 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7861 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7871 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7874 map1
= isl_map_copy(map1
);
7875 map2
= isl_map_copy(map2
);
7876 map1
= isl_map_normalize(map1
);
7877 map2
= isl_map_normalize(map2
);
7880 equal
= map1
->n
== map2
->n
;
7881 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7882 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7895 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7897 return isl_map_plain_is_equal(map1
, map2
);
7900 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7902 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7903 (struct isl_map
*)set2
);
7906 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7908 return isl_set_plain_is_equal(set1
, set2
);
7911 /* Return an interval that ranges from min to max (inclusive)
7913 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7914 isl_int min
, isl_int max
)
7917 struct isl_basic_set
*bset
= NULL
;
7919 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7923 k
= isl_basic_set_alloc_inequality(bset
);
7926 isl_int_set_si(bset
->ineq
[k
][1], 1);
7927 isl_int_neg(bset
->ineq
[k
][0], min
);
7929 k
= isl_basic_set_alloc_inequality(bset
);
7932 isl_int_set_si(bset
->ineq
[k
][1], -1);
7933 isl_int_set(bset
->ineq
[k
][0], max
);
7937 isl_basic_set_free(bset
);
7941 /* Return the Cartesian product of the basic sets in list (in the given order).
7943 __isl_give isl_basic_set
*isl_basic_set_list_product(
7944 __isl_take
struct isl_basic_set_list
*list
)
7952 struct isl_basic_set
*product
= NULL
;
7956 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7957 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7958 nparam
= isl_basic_set_n_param(list
->p
[0]);
7959 dim
= isl_basic_set_n_dim(list
->p
[0]);
7960 extra
= list
->p
[0]->n_div
;
7961 n_eq
= list
->p
[0]->n_eq
;
7962 n_ineq
= list
->p
[0]->n_ineq
;
7963 for (i
= 1; i
< list
->n
; ++i
) {
7964 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7965 isl_assert(list
->ctx
,
7966 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7967 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7968 extra
+= list
->p
[i
]->n_div
;
7969 n_eq
+= list
->p
[i
]->n_eq
;
7970 n_ineq
+= list
->p
[i
]->n_ineq
;
7972 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7977 for (i
= 0; i
< list
->n
; ++i
) {
7978 isl_basic_set_add_constraints(product
,
7979 isl_basic_set_copy(list
->p
[i
]), dim
);
7980 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7982 isl_basic_set_list_free(list
);
7985 isl_basic_set_free(product
);
7986 isl_basic_set_list_free(list
);
7990 struct isl_basic_map
*isl_basic_map_product(
7991 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7993 isl_space
*dim_result
= NULL
;
7994 struct isl_basic_map
*bmap
;
7995 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7996 struct isl_dim_map
*dim_map1
, *dim_map2
;
7998 if (!bmap1
|| !bmap2
)
8001 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8002 bmap2
->dim
, isl_dim_param
), goto error
);
8003 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8004 isl_space_copy(bmap2
->dim
));
8006 in1
= isl_basic_map_n_in(bmap1
);
8007 in2
= isl_basic_map_n_in(bmap2
);
8008 out1
= isl_basic_map_n_out(bmap1
);
8009 out2
= isl_basic_map_n_out(bmap2
);
8010 nparam
= isl_basic_map_n_param(bmap1
);
8012 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8013 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8014 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8015 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8016 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8017 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8018 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8019 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8020 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8021 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8022 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8024 bmap
= isl_basic_map_alloc_space(dim_result
,
8025 bmap1
->n_div
+ bmap2
->n_div
,
8026 bmap1
->n_eq
+ bmap2
->n_eq
,
8027 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8028 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8029 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8030 bmap
= isl_basic_map_simplify(bmap
);
8031 return isl_basic_map_finalize(bmap
);
8033 isl_basic_map_free(bmap1
);
8034 isl_basic_map_free(bmap2
);
8038 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8039 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8041 isl_basic_map
*prod
;
8043 prod
= isl_basic_map_product(bmap1
, bmap2
);
8044 prod
= isl_basic_map_flatten(prod
);
8048 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8049 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8051 return isl_basic_map_flat_range_product(bset1
, bset2
);
8054 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8055 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8057 isl_space
*space_result
= NULL
;
8058 isl_basic_map
*bmap
;
8059 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8060 struct isl_dim_map
*dim_map1
, *dim_map2
;
8062 if (!bmap1
|| !bmap2
)
8065 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8066 isl_space_copy(bmap2
->dim
));
8068 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8069 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8070 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8071 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8073 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8074 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8075 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8076 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8077 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8078 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8079 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8080 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8081 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8082 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8083 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8085 bmap
= isl_basic_map_alloc_space(space_result
,
8086 bmap1
->n_div
+ bmap2
->n_div
,
8087 bmap1
->n_eq
+ bmap2
->n_eq
,
8088 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8089 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8090 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8091 bmap
= isl_basic_map_simplify(bmap
);
8092 return isl_basic_map_finalize(bmap
);
8094 isl_basic_map_free(bmap1
);
8095 isl_basic_map_free(bmap2
);
8099 __isl_give isl_basic_map
*isl_basic_map_range_product(
8100 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8102 isl_space
*dim_result
= NULL
;
8103 isl_basic_map
*bmap
;
8104 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8105 struct isl_dim_map
*dim_map1
, *dim_map2
;
8107 if (!bmap1
|| !bmap2
)
8110 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8111 isl_space_copy(bmap2
->dim
));
8113 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8114 out1
= isl_basic_map_n_out(bmap1
);
8115 out2
= isl_basic_map_n_out(bmap2
);
8116 nparam
= isl_basic_map_n_param(bmap1
);
8118 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8119 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8120 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8121 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8122 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8123 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8124 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8125 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8126 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8127 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8128 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8130 bmap
= isl_basic_map_alloc_space(dim_result
,
8131 bmap1
->n_div
+ bmap2
->n_div
,
8132 bmap1
->n_eq
+ bmap2
->n_eq
,
8133 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8134 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8135 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8136 bmap
= isl_basic_map_simplify(bmap
);
8137 return isl_basic_map_finalize(bmap
);
8139 isl_basic_map_free(bmap1
);
8140 isl_basic_map_free(bmap2
);
8144 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8145 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8147 isl_basic_map
*prod
;
8149 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8150 prod
= isl_basic_map_flatten_range(prod
);
8154 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8155 __isl_take isl_map
*map2
,
8156 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8157 __isl_take isl_space
*right
),
8158 __isl_give isl_basic_map
*(*basic_map_product
)(
8159 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8162 struct isl_map
*result
;
8168 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8169 map2
->dim
, isl_dim_param
), goto error
);
8171 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8172 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8173 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8175 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8176 isl_space_copy(map2
->dim
)),
8177 map1
->n
* map2
->n
, flags
);
8180 for (i
= 0; i
< map1
->n
; ++i
)
8181 for (j
= 0; j
< map2
->n
; ++j
) {
8182 struct isl_basic_map
*part
;
8183 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8184 isl_basic_map_copy(map2
->p
[j
]));
8185 if (isl_basic_map_is_empty(part
))
8186 isl_basic_map_free(part
);
8188 result
= isl_map_add_basic_map(result
, part
);
8201 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8203 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8204 __isl_take isl_map
*map2
)
8206 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8209 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8210 __isl_take isl_map
*map2
)
8212 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8215 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8217 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8218 __isl_take isl_map
*map2
)
8222 prod
= isl_map_product(map1
, map2
);
8223 prod
= isl_map_flatten(prod
);
8227 /* Given two set A and B, construct its Cartesian product A x B.
8229 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8231 return isl_map_range_product(set1
, set2
);
8234 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8235 __isl_take isl_set
*set2
)
8237 return isl_map_flat_range_product(set1
, set2
);
8240 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8242 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8243 __isl_take isl_map
*map2
)
8245 return map_product(map1
, map2
, &isl_space_domain_product
,
8246 &isl_basic_map_domain_product
);
8249 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8251 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8252 __isl_take isl_map
*map2
)
8254 return map_product(map1
, map2
, &isl_space_range_product
,
8255 &isl_basic_map_range_product
);
8258 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8259 __isl_take isl_map
*map2
)
8261 return isl_map_align_params_map_map_and(map1
, map2
,
8262 &map_domain_product_aligned
);
8265 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8266 __isl_take isl_map
*map2
)
8268 return isl_map_align_params_map_map_and(map1
, map2
,
8269 &map_range_product_aligned
);
8272 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8274 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8275 __isl_take isl_map
*map2
)
8279 prod
= isl_map_domain_product(map1
, map2
);
8280 prod
= isl_map_flatten_domain(prod
);
8284 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8286 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8287 __isl_take isl_map
*map2
)
8291 prod
= isl_map_range_product(map1
, map2
);
8292 prod
= isl_map_flatten_range(prod
);
8296 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8299 uint32_t hash
= isl_hash_init();
8304 bmap
= isl_basic_map_copy(bmap
);
8305 bmap
= isl_basic_map_normalize(bmap
);
8308 total
= isl_basic_map_total_dim(bmap
);
8309 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8310 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8312 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8313 isl_hash_hash(hash
, c_hash
);
8315 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8316 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8318 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8319 isl_hash_hash(hash
, c_hash
);
8321 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8322 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8324 if (isl_int_is_zero(bmap
->div
[i
][0]))
8326 isl_hash_byte(hash
, i
& 0xFF);
8327 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8328 isl_hash_hash(hash
, c_hash
);
8330 isl_basic_map_free(bmap
);
8334 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8336 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8339 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8346 map
= isl_map_copy(map
);
8347 map
= isl_map_normalize(map
);
8351 hash
= isl_hash_init();
8352 for (i
= 0; i
< map
->n
; ++i
) {
8354 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8355 isl_hash_hash(hash
, bmap_hash
);
8363 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8365 return isl_map_get_hash((isl_map
*)set
);
8368 /* Check if the value for dimension dim is completely determined
8369 * by the values of the other parameters and variables.
8370 * That is, check if dimension dim is involved in an equality.
8372 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8379 nparam
= isl_basic_set_n_param(bset
);
8380 for (i
= 0; i
< bset
->n_eq
; ++i
)
8381 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8386 /* Check if the value for dimension dim is completely determined
8387 * by the values of the other parameters and variables.
8388 * That is, check if dimension dim is involved in an equality
8389 * for each of the subsets.
8391 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8397 for (i
= 0; i
< set
->n
; ++i
) {
8399 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8406 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8408 return set
? set
->n
: 0;
8411 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8412 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8419 for (i
= 0; i
< map
->n
; ++i
)
8420 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8426 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8427 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8434 for (i
= 0; i
< set
->n
; ++i
)
8435 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8441 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8448 bset
= isl_basic_set_cow(bset
);
8452 dim
= isl_basic_set_get_space(bset
);
8453 dim
= isl_space_lift(dim
, bset
->n_div
);
8456 isl_space_free(bset
->dim
);
8458 bset
->extra
-= bset
->n_div
;
8461 bset
= isl_basic_set_finalize(bset
);
8465 isl_basic_set_free(bset
);
8469 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8475 set
= isl_set_align_divs(set
);
8480 set
= isl_set_cow(set
);
8484 n_div
= set
->p
[0]->n_div
;
8485 dim
= isl_set_get_space(set
);
8486 dim
= isl_space_lift(dim
, n_div
);
8489 isl_space_free(set
->dim
);
8492 for (i
= 0; i
< set
->n
; ++i
) {
8493 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8504 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8507 struct isl_basic_map
*bmap
;
8514 set
= isl_set_align_divs(set
);
8519 dim
= isl_set_get_space(set
);
8520 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8522 return isl_map_identity(isl_space_map_from_set(dim
));
8525 n_div
= set
->p
[0]->n_div
;
8526 dim
= isl_space_map_from_set(dim
);
8527 n_param
= isl_space_dim(dim
, isl_dim_param
);
8528 n_set
= isl_space_dim(dim
, isl_dim_in
);
8529 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8530 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8531 for (i
= 0; i
< n_set
; ++i
)
8532 bmap
= var_equal(bmap
, i
);
8534 total
= n_param
+ n_set
+ n_set
+ n_div
;
8535 for (i
= 0; i
< n_div
; ++i
) {
8536 k
= isl_basic_map_alloc_inequality(bmap
);
8539 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8540 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8541 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8542 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8543 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8544 set
->p
[0]->div
[i
][0]);
8546 l
= isl_basic_map_alloc_inequality(bmap
);
8549 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8550 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8551 set
->p
[0]->div
[i
][0]);
8552 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8556 bmap
= isl_basic_map_simplify(bmap
);
8557 bmap
= isl_basic_map_finalize(bmap
);
8558 return isl_map_from_basic_map(bmap
);
8561 isl_basic_map_free(bmap
);
8565 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8573 dim
= isl_basic_set_total_dim(bset
);
8574 size
+= bset
->n_eq
* (1 + dim
);
8575 size
+= bset
->n_ineq
* (1 + dim
);
8576 size
+= bset
->n_div
* (2 + dim
);
8581 int isl_set_size(__isl_keep isl_set
*set
)
8589 for (i
= 0; i
< set
->n
; ++i
)
8590 size
+= isl_basic_set_size(set
->p
[i
]);
8595 /* Check if there is any lower bound (if lower == 0) and/or upper
8596 * bound (if upper == 0) on the specified dim.
8598 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8599 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8606 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8608 pos
+= isl_basic_map_offset(bmap
, type
);
8610 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8611 if (isl_int_is_zero(bmap
->div
[i
][0]))
8613 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8617 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8618 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8621 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8622 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8629 return lower
&& upper
;
8632 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8633 enum isl_dim_type type
, unsigned pos
)
8635 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8638 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8639 enum isl_dim_type type
, unsigned pos
)
8641 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8644 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8645 enum isl_dim_type type
, unsigned pos
)
8647 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8650 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8651 enum isl_dim_type type
, unsigned pos
)
8658 for (i
= 0; i
< map
->n
; ++i
) {
8660 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8661 if (bounded
< 0 || !bounded
)
8668 /* Return 1 if the specified dim is involved in both an upper bound
8669 * and a lower bound.
8671 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8672 enum isl_dim_type type
, unsigned pos
)
8674 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8677 static int has_bound(__isl_keep isl_map
*map
,
8678 enum isl_dim_type type
, unsigned pos
,
8679 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8680 enum isl_dim_type type
, unsigned pos
))
8687 for (i
= 0; i
< map
->n
; ++i
) {
8689 bounded
= fn(map
->p
[i
], type
, pos
);
8690 if (bounded
< 0 || bounded
)
8697 /* Return 1 if the specified dim is involved in any lower bound.
8699 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8700 enum isl_dim_type type
, unsigned pos
)
8702 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8705 /* Return 1 if the specified dim is involved in any upper bound.
8707 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8708 enum isl_dim_type type
, unsigned pos
)
8710 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8713 /* For each of the "n" variables starting at "first", determine
8714 * the sign of the variable and put the results in the first "n"
8715 * elements of the array "signs".
8717 * 1 means that the variable is non-negative
8718 * -1 means that the variable is non-positive
8719 * 0 means the variable attains both positive and negative values.
8721 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8722 unsigned first
, unsigned n
, int *signs
)
8724 isl_vec
*bound
= NULL
;
8725 struct isl_tab
*tab
= NULL
;
8726 struct isl_tab_undo
*snap
;
8729 if (!bset
|| !signs
)
8732 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8733 tab
= isl_tab_from_basic_set(bset
, 0);
8737 isl_seq_clr(bound
->el
, bound
->size
);
8738 isl_int_set_si(bound
->el
[0], -1);
8740 snap
= isl_tab_snap(tab
);
8741 for (i
= 0; i
< n
; ++i
) {
8744 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8745 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8748 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8749 if (isl_tab_rollback(tab
, snap
) < 0)
8757 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8758 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8761 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8762 if (isl_tab_rollback(tab
, snap
) < 0)
8765 signs
[i
] = empty
? -1 : 0;
8769 isl_vec_free(bound
);
8773 isl_vec_free(bound
);
8777 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8778 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8780 if (!bset
|| !signs
)
8782 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8785 first
+= pos(bset
->dim
, type
) - 1;
8786 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8789 /* Check if the given basic map is obviously single-valued.
8790 * In particular, for each output dimension, check that there is
8791 * an equality that defines the output dimension in terms of
8792 * earlier dimensions.
8794 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8804 total
= 1 + isl_basic_map_total_dim(bmap
);
8805 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8806 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8808 for (i
= 0; i
< n_out
; ++i
) {
8809 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8810 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8812 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8813 total
- (o_out
+ i
+ 1)) == -1)
8816 if (j
>= bmap
->n_eq
)
8823 /* Check if the given map is obviously single-valued.
8825 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8834 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8837 /* Check if the given map is single-valued.
8842 * and check if the result is a subset of the identity mapping.
8844 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8851 sv
= isl_map_plain_is_single_valued(map
);
8855 test
= isl_map_reverse(isl_map_copy(map
));
8856 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8858 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8859 id
= isl_map_identity(dim
);
8861 sv
= isl_map_is_subset(test
, id
);
8869 int isl_map_is_injective(__isl_keep isl_map
*map
)
8873 map
= isl_map_copy(map
);
8874 map
= isl_map_reverse(map
);
8875 in
= isl_map_is_single_valued(map
);
8881 /* Check if the given map is obviously injective.
8883 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8887 map
= isl_map_copy(map
);
8888 map
= isl_map_reverse(map
);
8889 in
= isl_map_plain_is_single_valued(map
);
8895 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8899 sv
= isl_map_is_single_valued(map
);
8903 return isl_map_is_injective(map
);
8906 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8908 return isl_map_is_single_valued((isl_map
*)set
);
8911 int isl_map_is_translation(__isl_keep isl_map
*map
)
8916 delta
= isl_map_deltas(isl_map_copy(map
));
8917 ok
= isl_set_is_singleton(delta
);
8918 isl_set_free(delta
);
8923 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8925 if (isl_seq_first_non_zero(p
, pos
) != -1)
8927 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8932 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8941 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8944 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8945 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8946 for (j
= 0; j
< nvar
; ++j
) {
8947 int lower
= 0, upper
= 0;
8948 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8949 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8951 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8957 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8958 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8960 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8962 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8967 if (!lower
|| !upper
)
8974 int isl_set_is_box(__isl_keep isl_set
*set
)
8981 return isl_basic_set_is_box(set
->p
[0]);
8984 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8989 return isl_space_is_wrapping(bset
->dim
);
8992 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8997 return isl_space_is_wrapping(set
->dim
);
9000 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9002 bmap
= isl_basic_map_cow(bmap
);
9006 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9010 bmap
= isl_basic_map_finalize(bmap
);
9012 return (isl_basic_set
*)bmap
;
9014 isl_basic_map_free(bmap
);
9018 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9022 map
= isl_map_cow(map
);
9026 for (i
= 0; i
< map
->n
; ++i
) {
9027 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9031 map
->dim
= isl_space_wrap(map
->dim
);
9035 return (isl_set
*)map
;
9041 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9043 bset
= isl_basic_set_cow(bset
);
9047 bset
->dim
= isl_space_unwrap(bset
->dim
);
9051 bset
= isl_basic_set_finalize(bset
);
9053 return (isl_basic_map
*)bset
;
9055 isl_basic_set_free(bset
);
9059 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9066 if (!isl_set_is_wrapping(set
))
9067 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9070 set
= isl_set_cow(set
);
9074 for (i
= 0; i
< set
->n
; ++i
) {
9075 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9080 set
->dim
= isl_space_unwrap(set
->dim
);
9084 return (isl_map
*)set
;
9090 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9091 enum isl_dim_type type
)
9096 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9099 bmap
= isl_basic_map_cow(bmap
);
9103 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9107 bmap
= isl_basic_map_finalize(bmap
);
9111 isl_basic_map_free(bmap
);
9115 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9116 enum isl_dim_type type
)
9123 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9126 map
= isl_map_cow(map
);
9130 for (i
= 0; i
< map
->n
; ++i
) {
9131 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9135 map
->dim
= isl_space_reset(map
->dim
, type
);
9145 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9150 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9153 bmap
= isl_basic_map_cow(bmap
);
9157 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9161 bmap
= isl_basic_map_finalize(bmap
);
9165 isl_basic_map_free(bmap
);
9169 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9171 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9174 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9175 __isl_take isl_basic_map
*bmap
)
9180 if (!bmap
->dim
->nested
[0])
9183 bmap
= isl_basic_map_cow(bmap
);
9187 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9191 bmap
= isl_basic_map_finalize(bmap
);
9195 isl_basic_map_free(bmap
);
9199 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9200 __isl_take isl_basic_map
*bmap
)
9205 if (!bmap
->dim
->nested
[1])
9208 bmap
= isl_basic_map_cow(bmap
);
9212 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9216 bmap
= isl_basic_map_finalize(bmap
);
9220 isl_basic_map_free(bmap
);
9224 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9231 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9234 map
= isl_map_cow(map
);
9238 for (i
= 0; i
< map
->n
; ++i
) {
9239 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9243 map
->dim
= isl_space_flatten(map
->dim
);
9253 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9255 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9258 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9260 isl_space
*dim
, *flat_dim
;
9263 dim
= isl_set_get_space(set
);
9264 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9265 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9266 map
= isl_map_intersect_domain(map
, set
);
9271 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9278 if (!map
->dim
->nested
[0])
9281 map
= isl_map_cow(map
);
9285 for (i
= 0; i
< map
->n
; ++i
) {
9286 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9290 map
->dim
= isl_space_flatten_domain(map
->dim
);
9300 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9307 if (!map
->dim
->nested
[1])
9310 map
= isl_map_cow(map
);
9314 for (i
= 0; i
< map
->n
; ++i
) {
9315 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9319 map
->dim
= isl_space_flatten_range(map
->dim
);
9329 /* Reorder the dimensions of "bmap" according to the given dim_map
9330 * and set the dimension specification to "dim".
9332 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9333 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9337 bmap
= isl_basic_map_cow(bmap
);
9338 if (!bmap
|| !dim
|| !dim_map
)
9341 res
= isl_basic_map_alloc_space(dim
,
9342 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9343 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9344 res
= isl_basic_map_finalize(res
);
9348 isl_basic_map_free(bmap
);
9349 isl_space_free(dim
);
9353 /* Reorder the dimensions of "map" according to given reordering.
9355 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9356 __isl_take isl_reordering
*r
)
9359 struct isl_dim_map
*dim_map
;
9361 map
= isl_map_cow(map
);
9362 dim_map
= isl_dim_map_from_reordering(r
);
9363 if (!map
|| !r
|| !dim_map
)
9366 for (i
= 0; i
< map
->n
; ++i
) {
9367 struct isl_dim_map
*dim_map_i
;
9369 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9371 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9372 isl_space_copy(r
->dim
), dim_map_i
);
9378 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9380 isl_reordering_free(r
);
9386 isl_reordering_free(r
);
9390 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9391 __isl_take isl_reordering
*r
)
9393 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9396 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9397 __isl_take isl_space
*model
)
9404 ctx
= isl_space_get_ctx(model
);
9405 if (!isl_space_has_named_params(model
))
9406 isl_die(ctx
, isl_error_invalid
,
9407 "model has unnamed parameters", goto error
);
9408 if (!isl_space_has_named_params(map
->dim
))
9409 isl_die(ctx
, isl_error_invalid
,
9410 "relation has unnamed parameters", goto error
);
9411 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9412 isl_reordering
*exp
;
9414 model
= isl_space_drop_dims(model
, isl_dim_in
,
9415 0, isl_space_dim(model
, isl_dim_in
));
9416 model
= isl_space_drop_dims(model
, isl_dim_out
,
9417 0, isl_space_dim(model
, isl_dim_out
));
9418 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9419 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9420 map
= isl_map_realign(map
, exp
);
9423 isl_space_free(model
);
9426 isl_space_free(model
);
9431 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9432 __isl_take isl_space
*model
)
9434 return isl_map_align_params(set
, model
);
9437 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9438 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9439 enum isl_dim_type c2
, enum isl_dim_type c3
,
9440 enum isl_dim_type c4
, enum isl_dim_type c5
)
9442 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9443 struct isl_mat
*mat
;
9449 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9450 isl_basic_map_total_dim(bmap
) + 1);
9453 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9454 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9455 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9456 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9457 isl_int_set(mat
->row
[i
][pos
],
9458 bmap
->eq
[i
][off
+ k
]);
9466 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9467 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9468 enum isl_dim_type c2
, enum isl_dim_type c3
,
9469 enum isl_dim_type c4
, enum isl_dim_type c5
)
9471 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9472 struct isl_mat
*mat
;
9478 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9479 isl_basic_map_total_dim(bmap
) + 1);
9482 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9483 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9484 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9485 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9486 isl_int_set(mat
->row
[i
][pos
],
9487 bmap
->ineq
[i
][off
+ k
]);
9495 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9496 __isl_take isl_space
*dim
,
9497 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9498 enum isl_dim_type c2
, enum isl_dim_type c3
,
9499 enum isl_dim_type c4
, enum isl_dim_type c5
)
9501 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9502 isl_basic_map
*bmap
;
9508 if (!dim
|| !eq
|| !ineq
)
9511 if (eq
->n_col
!= ineq
->n_col
)
9512 isl_die(dim
->ctx
, isl_error_invalid
,
9513 "equalities and inequalities matrices should have "
9514 "same number of columns", goto error
);
9516 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9518 if (eq
->n_col
< total
)
9519 isl_die(dim
->ctx
, isl_error_invalid
,
9520 "number of columns too small", goto error
);
9522 extra
= eq
->n_col
- total
;
9524 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9525 eq
->n_row
, ineq
->n_row
);
9528 for (i
= 0; i
< extra
; ++i
) {
9529 k
= isl_basic_map_alloc_div(bmap
);
9532 isl_int_set_si(bmap
->div
[k
][0], 0);
9534 for (i
= 0; i
< eq
->n_row
; ++i
) {
9535 l
= isl_basic_map_alloc_equality(bmap
);
9538 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9539 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9540 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9541 isl_int_set(bmap
->eq
[l
][off
+ k
],
9547 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9548 l
= isl_basic_map_alloc_inequality(bmap
);
9551 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9552 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9553 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9554 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9561 isl_space_free(dim
);
9567 isl_space_free(dim
);
9573 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9574 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9575 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9577 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9578 c1
, c2
, c3
, c4
, isl_dim_in
);
9581 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9582 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9583 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9585 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9586 c1
, c2
, c3
, c4
, isl_dim_in
);
9589 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9590 __isl_take isl_space
*dim
,
9591 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9592 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9594 return (isl_basic_set
*)
9595 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9596 c1
, c2
, c3
, c4
, isl_dim_in
);
9599 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9604 return isl_space_can_zip(bmap
->dim
);
9607 int isl_map_can_zip(__isl_keep isl_map
*map
)
9612 return isl_space_can_zip(map
->dim
);
9615 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9616 * (A -> C) -> (B -> D).
9618 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9627 if (!isl_basic_map_can_zip(bmap
))
9628 isl_die(bmap
->ctx
, isl_error_invalid
,
9629 "basic map cannot be zipped", goto error
);
9630 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9631 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9632 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9633 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9634 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9637 bmap
->dim
= isl_space_zip(bmap
->dim
);
9642 isl_basic_map_free(bmap
);
9646 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9647 * (A -> C) -> (B -> D).
9649 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9656 if (!isl_map_can_zip(map
))
9657 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9660 map
= isl_map_cow(map
);
9664 for (i
= 0; i
< map
->n
; ++i
) {
9665 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9670 map
->dim
= isl_space_zip(map
->dim
);
9680 /* Construct a basic map mapping the domain of the affine expression
9681 * to a one-dimensional range prescribed by the affine expression.
9683 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9687 isl_local_space
*ls
;
9688 isl_basic_map
*bmap
;
9693 ls
= isl_aff_get_local_space(aff
);
9694 bmap
= isl_basic_map_from_local_space(ls
);
9695 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9696 k
= isl_basic_map_alloc_equality(bmap
);
9700 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9701 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9702 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9703 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9704 aff
->v
->size
- (pos
+ 1));
9707 bmap
= isl_basic_map_finalize(bmap
);
9711 isl_basic_map_free(bmap
);
9715 /* Construct a basic map mapping the domain the multi-affine expression
9716 * to its range, with each dimension in the range equated to the
9717 * corresponding affine expression.
9719 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9720 __isl_take isl_multi_aff
*maff
)
9724 isl_basic_map
*bmap
;
9729 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9730 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9731 "invalid space", return isl_multi_aff_free(maff
));
9733 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9734 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9736 for (i
= 0; i
< maff
->n
; ++i
) {
9738 isl_basic_map
*bmap_i
;
9740 aff
= isl_aff_copy(maff
->p
[i
]);
9741 bmap_i
= isl_basic_map_from_aff(aff
);
9743 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9746 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9748 isl_multi_aff_free(maff
);
9752 /* Construct a basic map mapping a domain in the given space to
9753 * to an n-dimensional range, with n the number of elements in the list,
9754 * where each coordinate in the range is prescribed by the
9755 * corresponding affine expression.
9756 * The domains of all affine expressions in the list are assumed to match
9759 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9760 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9764 isl_basic_map
*bmap
;
9769 dim
= isl_space_from_domain(domain_dim
);
9770 bmap
= isl_basic_map_universe(dim
);
9772 for (i
= 0; i
< list
->n
; ++i
) {
9774 isl_basic_map
*bmap_i
;
9776 aff
= isl_aff_copy(list
->p
[i
]);
9777 bmap_i
= isl_basic_map_from_aff(aff
);
9779 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9782 isl_aff_list_free(list
);
9786 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9787 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9789 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9792 /* Add a constraint imposing that the given two dimensions are equal.
9794 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9795 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9797 isl_basic_map
*bmap
= NULL
;
9803 if (pos1
>= isl_map_dim(map
, type1
))
9804 isl_die(map
->ctx
, isl_error_invalid
,
9805 "index out of bounds", goto error
);
9806 if (pos2
>= isl_map_dim(map
, type2
))
9807 isl_die(map
->ctx
, isl_error_invalid
,
9808 "index out of bounds", goto error
);
9810 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9811 i
= isl_basic_map_alloc_equality(bmap
);
9814 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9815 pos1
+= isl_basic_map_offset(bmap
, type1
);
9816 pos2
+= isl_basic_map_offset(bmap
, type2
);
9817 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9818 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9819 bmap
= isl_basic_map_finalize(bmap
);
9821 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9825 isl_basic_map_free(bmap
);
9830 /* Add a constraint imposing that the given two dimensions have opposite values.
9832 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9833 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9835 isl_basic_map
*bmap
= NULL
;
9841 if (pos1
>= isl_map_dim(map
, type1
))
9842 isl_die(map
->ctx
, isl_error_invalid
,
9843 "index out of bounds", goto error
);
9844 if (pos2
>= isl_map_dim(map
, type2
))
9845 isl_die(map
->ctx
, isl_error_invalid
,
9846 "index out of bounds", goto error
);
9848 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9849 i
= isl_basic_map_alloc_equality(bmap
);
9852 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9853 pos1
+= isl_basic_map_offset(bmap
, type1
);
9854 pos2
+= isl_basic_map_offset(bmap
, type2
);
9855 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9856 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9857 bmap
= isl_basic_map_finalize(bmap
);
9859 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9863 isl_basic_map_free(bmap
);
9868 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9872 isl_local_space
*ls
;
9877 if (!isl_basic_map_divs_known(bmap
))
9878 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9879 "some divs are unknown", return NULL
);
9881 ls
= isl_basic_map_get_local_space(bmap
);
9882 div
= isl_local_space_get_div(ls
, pos
);
9883 isl_local_space_free(ls
);
9888 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9891 return isl_basic_map_get_div(bset
, pos
);
9894 /* Plug in "subs" for dimension "type", "pos" of "bset".
9896 * Let i be the dimension to replace and let "subs" be of the form
9900 * Any integer division with a non-zero coefficient for i,
9902 * floor((a i + g)/m)
9906 * floor((a f + d g)/(m d))
9908 * Constraints of the form
9916 __isl_give isl_basic_set
*isl_basic_set_substitute(
9917 __isl_take isl_basic_set
*bset
,
9918 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9924 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9927 bset
= isl_basic_set_cow(bset
);
9931 ctx
= isl_basic_set_get_ctx(bset
);
9932 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9933 isl_die(ctx
, isl_error_invalid
,
9934 "spaces don't match", goto error
);
9935 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9936 isl_die(ctx
, isl_error_unsupported
,
9937 "cannot handle divs yet", goto error
);
9939 pos
+= isl_basic_set_offset(bset
, type
);
9943 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9944 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9946 isl_int_set(v
, bset
->eq
[i
][pos
]);
9947 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9948 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9949 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9952 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9953 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9955 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9956 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9957 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9958 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9961 for (i
= 0; i
< bset
->n_div
; ++i
) {
9962 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9964 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9965 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9966 isl_seq_combine(bset
->div
[i
] + 1,
9967 subs
->v
->el
[0], bset
->div
[i
] + 1,
9968 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9969 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9974 bset
= isl_basic_set_simplify(bset
);
9975 return isl_basic_set_finalize(bset
);
9977 isl_basic_set_free(bset
);
9981 /* Plug in "subs" for dimension "type", "pos" of "set".
9983 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9984 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9988 if (set
&& isl_set_plain_is_empty(set
))
9991 set
= isl_set_cow(set
);
9995 for (i
= set
->n
- 1; i
>= 0; --i
) {
9996 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9999 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
10000 isl_basic_set_free(set
->p
[i
]);
10001 if (i
!= set
->n
- 1)
10002 set
->p
[i
] = set
->p
[set
->n
- 1];