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 /* Does the given dimension have a name?
494 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
495 enum isl_dim_type type
, unsigned pos
)
497 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
500 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
501 __isl_take isl_basic_map
*bmap
,
502 enum isl_dim_type type
, unsigned pos
, const char *s
)
504 bmap
= isl_basic_map_cow(bmap
);
507 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
512 isl_basic_map_free(bmap
);
516 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
517 enum isl_dim_type type
, unsigned pos
, const char *s
)
521 map
= isl_map_cow(map
);
525 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
529 for (i
= 0; i
< map
->n
; ++i
) {
530 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
541 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
542 __isl_take isl_basic_set
*bset
,
543 enum isl_dim_type type
, unsigned pos
, const char *s
)
545 return (isl_basic_set
*)isl_basic_map_set_dim_name(
546 (isl_basic_map
*)bset
, type
, pos
, s
);
549 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
550 enum isl_dim_type type
, unsigned pos
, const char *s
)
552 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
555 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
556 enum isl_dim_type type
, unsigned pos
)
558 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
561 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
562 enum isl_dim_type type
, unsigned pos
)
564 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
567 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
568 enum isl_dim_type type
, unsigned pos
)
570 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
573 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
574 enum isl_dim_type type
, unsigned pos
)
576 return isl_map_has_dim_id(set
, type
, pos
);
579 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
580 enum isl_dim_type type
, unsigned pos
)
582 return isl_map_get_dim_id(set
, type
, pos
);
585 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
586 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
588 map
= isl_map_cow(map
);
590 return isl_id_free(id
);
592 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
594 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
597 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
598 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
600 return isl_map_set_dim_id(set
, type
, pos
, id
);
603 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
604 __isl_keep isl_id
*id
)
608 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
611 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
612 __isl_keep isl_id
*id
)
614 return isl_map_find_dim_by_id(set
, type
, id
);
617 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
622 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
625 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
628 return isl_map_find_dim_by_name(set
, type
, name
);
631 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
635 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
638 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
640 return isl_basic_map_is_rational(bset
);
643 /* Is this basic set a parameter domain?
645 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
649 return isl_space_is_params(bset
->dim
);
652 /* Is this set a parameter domain?
654 int isl_set_is_params(__isl_keep isl_set
*set
)
658 return isl_space_is_params(set
->dim
);
661 /* Is this map actually a parameter domain?
662 * Users should never call this function. Outside of isl,
663 * a map can never be a parameter domain.
665 int isl_map_is_params(__isl_keep isl_map
*map
)
669 return isl_space_is_params(map
->dim
);
672 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
673 struct isl_basic_map
*bmap
, unsigned extra
,
674 unsigned n_eq
, unsigned n_ineq
)
677 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
682 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
683 if (isl_blk_is_error(bmap
->block
))
686 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
691 bmap
->block2
= isl_blk_empty();
694 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
695 if (isl_blk_is_error(bmap
->block2
))
698 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
703 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
704 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
706 for (i
= 0; i
< extra
; ++i
)
707 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
711 bmap
->c_size
= n_eq
+ n_ineq
;
712 bmap
->eq
= bmap
->ineq
+ n_ineq
;
721 isl_basic_map_free(bmap
);
725 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
726 unsigned nparam
, unsigned dim
, unsigned extra
,
727 unsigned n_eq
, unsigned n_ineq
)
729 struct isl_basic_map
*bmap
;
732 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
736 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
737 return (struct isl_basic_set
*)bmap
;
740 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
741 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
743 struct isl_basic_map
*bmap
;
746 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
747 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
748 return (struct isl_basic_set
*)bmap
;
754 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
755 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
757 struct isl_basic_map
*bmap
;
761 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
766 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
772 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
773 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
774 unsigned n_eq
, unsigned n_ineq
)
776 struct isl_basic_map
*bmap
;
779 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
783 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
787 static void dup_constraints(
788 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
791 unsigned total
= isl_basic_map_total_dim(src
);
793 for (i
= 0; i
< src
->n_eq
; ++i
) {
794 int j
= isl_basic_map_alloc_equality(dst
);
795 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
798 for (i
= 0; i
< src
->n_ineq
; ++i
) {
799 int j
= isl_basic_map_alloc_inequality(dst
);
800 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
803 for (i
= 0; i
< src
->n_div
; ++i
) {
804 int j
= isl_basic_map_alloc_div(dst
);
805 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
807 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
810 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
812 struct isl_basic_map
*dup
;
816 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
817 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
820 dup_constraints(dup
, bmap
);
821 dup
->flags
= bmap
->flags
;
822 dup
->sample
= isl_vec_copy(bmap
->sample
);
826 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
828 struct isl_basic_map
*dup
;
830 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
831 return (struct isl_basic_set
*)dup
;
834 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
839 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
843 return isl_basic_set_dup(bset
);
846 struct isl_set
*isl_set_copy(struct isl_set
*set
)
855 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
860 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
864 bmap
= isl_basic_map_dup(bmap
);
866 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
870 struct isl_map
*isl_map_copy(struct isl_map
*map
)
879 void isl_basic_map_free(struct isl_basic_map
*bmap
)
887 isl_ctx_deref(bmap
->ctx
);
889 isl_blk_free(bmap
->ctx
, bmap
->block2
);
891 isl_blk_free(bmap
->ctx
, bmap
->block
);
892 isl_vec_free(bmap
->sample
);
893 isl_space_free(bmap
->dim
);
897 void isl_basic_set_free(struct isl_basic_set
*bset
)
899 isl_basic_map_free((struct isl_basic_map
*)bset
);
902 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
904 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
907 __isl_give isl_map
*isl_map_align_params_map_map_and(
908 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
909 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
910 __isl_take isl_map
*map2
))
914 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
915 return fn(map1
, map2
);
916 if (!isl_space_has_named_params(map1
->dim
) ||
917 !isl_space_has_named_params(map2
->dim
))
918 isl_die(map1
->ctx
, isl_error_invalid
,
919 "unaligned unnamed parameters", goto error
);
920 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
921 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
922 return fn(map1
, map2
);
929 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
930 __isl_keep isl_map
*map2
,
931 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
937 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
938 return fn(map1
, map2
);
939 if (!isl_space_has_named_params(map1
->dim
) ||
940 !isl_space_has_named_params(map2
->dim
))
941 isl_die(map1
->ctx
, isl_error_invalid
,
942 "unaligned unnamed parameters", return -1);
943 map1
= isl_map_copy(map1
);
944 map2
= isl_map_copy(map2
);
945 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
946 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
953 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
959 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
960 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
962 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
963 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
964 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
965 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
966 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
967 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
969 int j
= isl_basic_map_alloc_inequality(bmap
);
973 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
974 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
981 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
982 bmap
->extra
- bmap
->n_div
);
986 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
988 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
991 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
995 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1000 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1002 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1005 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1010 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1012 if (pos
!= bmap
->n_eq
- 1) {
1014 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1015 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1021 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1023 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1026 void isl_basic_map_inequality_to_equality(
1027 struct isl_basic_map
*bmap
, unsigned pos
)
1031 t
= bmap
->ineq
[pos
];
1032 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1033 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1038 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1039 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1040 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1041 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1044 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1046 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1049 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1051 struct isl_ctx
*ctx
;
1055 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1056 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1057 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1058 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1059 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1060 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1061 1 + isl_basic_map_total_dim(bmap
),
1062 bmap
->extra
- bmap
->n_div
);
1063 return bmap
->n_ineq
++;
1066 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1068 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1071 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1075 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1080 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1082 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1085 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1090 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1092 if (pos
!= bmap
->n_ineq
- 1) {
1093 t
= bmap
->ineq
[pos
];
1094 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1095 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1096 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1102 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1104 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1107 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1112 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1115 k
= isl_basic_map_alloc_equality(bmap
);
1118 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1121 isl_basic_map_free(bmap
);
1125 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1128 return (isl_basic_set
*)
1129 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1132 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1137 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1140 k
= isl_basic_map_alloc_inequality(bmap
);
1143 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1146 isl_basic_map_free(bmap
);
1150 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1153 return (isl_basic_set
*)
1154 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1157 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1161 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1162 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1163 1 + 1 + isl_basic_map_total_dim(bmap
),
1164 bmap
->extra
- bmap
->n_div
);
1165 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1166 return bmap
->n_div
++;
1169 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1171 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1174 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1178 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1183 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1185 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1188 /* Copy constraint from src to dst, putting the vars of src at offset
1189 * dim_off in dst and the divs of src at offset div_off in dst.
1190 * If both sets are actually map, then dim_off applies to the input
1193 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1194 struct isl_basic_map
*src_map
, isl_int
*src
,
1195 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1197 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1198 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1199 unsigned src_in
= isl_basic_map_n_in(src_map
);
1200 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1201 unsigned src_out
= isl_basic_map_n_out(src_map
);
1202 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1203 isl_int_set(dst
[0], src
[0]);
1204 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1205 if (dst_nparam
> src_nparam
)
1206 isl_seq_clr(dst
+1+src_nparam
,
1207 dst_nparam
- src_nparam
);
1208 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1209 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1211 isl_min(dst_in
-in_off
, src_in
));
1212 if (dst_in
-in_off
> src_in
)
1213 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1214 dst_in
- in_off
- src_in
);
1215 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1216 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1217 src
+1+src_nparam
+src_in
,
1218 isl_min(dst_out
-out_off
, src_out
));
1219 if (dst_out
-out_off
> src_out
)
1220 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1221 dst_out
- out_off
- src_out
);
1222 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1223 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1224 src
+1+src_nparam
+src_in
+src_out
,
1225 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1226 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1227 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1228 div_off
+src_map
->n_div
,
1229 dst_map
->n_div
- div_off
- src_map
->n_div
);
1232 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1233 struct isl_basic_map
*src_map
, isl_int
*src
,
1234 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1236 isl_int_set(dst
[0], src
[0]);
1237 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1240 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1241 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1246 if (!bmap1
|| !bmap2
)
1249 div_off
= bmap1
->n_div
;
1251 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1252 int i1
= isl_basic_map_alloc_equality(bmap1
);
1255 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1256 i_pos
, o_pos
, div_off
);
1259 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1260 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1263 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1264 i_pos
, o_pos
, div_off
);
1267 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1268 int i1
= isl_basic_map_alloc_div(bmap1
);
1271 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1272 i_pos
, o_pos
, div_off
);
1275 isl_basic_map_free(bmap2
);
1280 isl_basic_map_free(bmap1
);
1281 isl_basic_map_free(bmap2
);
1285 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1286 struct isl_basic_set
*bset2
, unsigned pos
)
1288 return (struct isl_basic_set
*)
1289 add_constraints((struct isl_basic_map
*)bset1
,
1290 (struct isl_basic_map
*)bset2
, 0, pos
);
1293 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1294 __isl_take isl_space
*dim
, unsigned extra
,
1295 unsigned n_eq
, unsigned n_ineq
)
1297 struct isl_basic_map
*ext
;
1307 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1308 base
->extra
>= base
->n_div
+ extra
;
1310 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1311 room_for_ineq(base
, n_ineq
)) {
1312 isl_space_free(dim
);
1316 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1317 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1318 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1319 extra
+= base
->extra
;
1321 n_ineq
+= base
->n_ineq
;
1323 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1329 ext
->sample
= isl_vec_copy(base
->sample
);
1330 flags
= base
->flags
;
1331 ext
= add_constraints(ext
, base
, 0, 0);
1334 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1340 isl_space_free(dim
);
1341 isl_basic_map_free(base
);
1345 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1346 __isl_take isl_space
*dim
, unsigned extra
,
1347 unsigned n_eq
, unsigned n_ineq
)
1349 return (struct isl_basic_set
*)
1350 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1351 extra
, n_eq
, n_ineq
);
1354 struct isl_basic_map
*isl_basic_map_extend_constraints(
1355 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1359 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1363 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1364 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1365 unsigned n_eq
, unsigned n_ineq
)
1367 struct isl_basic_map
*bmap
;
1372 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1376 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1379 isl_basic_map_free(base
);
1383 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1384 unsigned nparam
, unsigned dim
, unsigned extra
,
1385 unsigned n_eq
, unsigned n_ineq
)
1387 return (struct isl_basic_set
*)
1388 isl_basic_map_extend((struct isl_basic_map
*)base
,
1389 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1392 struct isl_basic_set
*isl_basic_set_extend_constraints(
1393 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1395 return (struct isl_basic_set
*)
1396 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1400 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1402 return (struct isl_basic_set
*)
1403 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1406 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1411 if (bmap
->ref
> 1) {
1413 bmap
= isl_basic_map_dup(bmap
);
1416 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1420 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1428 return isl_set_dup(set
);
1431 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1439 return isl_map_dup(map
);
1442 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1443 unsigned a_len
, unsigned b_len
)
1445 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1446 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1447 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1450 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1451 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1459 isl_assert(bmap
->ctx
,
1460 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1462 if (n1
== 0 || n2
== 0)
1465 bmap
= isl_basic_map_cow(bmap
);
1469 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1470 if (isl_blk_is_error(blk
))
1473 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1475 bmap
->eq
[i
] + pos
, n1
, n2
);
1477 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1479 bmap
->ineq
[i
] + pos
, n1
, n2
);
1481 for (i
= 0; i
< bmap
->n_div
; ++i
)
1483 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1485 isl_blk_free(bmap
->ctx
, blk
);
1487 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1488 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1489 return isl_basic_map_finalize(bmap
);
1491 isl_basic_map_free(bmap
);
1495 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1496 __isl_take isl_basic_set
*bset
, unsigned n
)
1501 nparam
= isl_basic_set_n_param(bset
);
1502 dim
= isl_basic_set_n_dim(bset
);
1503 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1505 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1507 isl_basic_set_free(bset
);
1511 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1517 total
= isl_basic_map_total_dim(bmap
);
1518 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1519 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1521 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1523 i
= isl_basic_map_alloc_equality(bmap
);
1527 isl_int_set_si(bmap
->eq
[i
][0], 1);
1528 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1529 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1530 isl_vec_free(bmap
->sample
);
1531 bmap
->sample
= NULL
;
1532 return isl_basic_map_finalize(bmap
);
1534 isl_basic_map_free(bmap
);
1538 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1540 return (struct isl_basic_set
*)
1541 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1544 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1547 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1548 isl_int
*t
= bmap
->div
[a
];
1549 bmap
->div
[a
] = bmap
->div
[b
];
1552 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1553 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1555 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1556 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1558 for (i
= 0; i
< bmap
->n_div
; ++i
)
1559 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1560 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1563 /* Eliminate the specified n dimensions starting at first from the
1564 * constraints using Fourier-Motzkin. The dimensions themselves
1567 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1568 enum isl_dim_type type
, unsigned first
, unsigned n
)
1577 map
= isl_map_cow(map
);
1581 for (i
= 0; i
< map
->n
; ++i
) {
1582 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1592 /* Eliminate the specified n dimensions starting at first from the
1593 * constraints using Fourier-Motzkin. The dimensions themselves
1596 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1597 enum isl_dim_type type
, unsigned first
, unsigned n
)
1599 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1602 /* Eliminate the specified n dimensions starting at first from the
1603 * constraints using Fourier-Motzkin. The dimensions themselves
1606 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1607 unsigned first
, unsigned n
)
1609 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1612 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1613 __isl_take isl_basic_map
*bmap
)
1617 bmap
= isl_basic_map_eliminate_vars(bmap
,
1618 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1622 return isl_basic_map_finalize(bmap
);
1625 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1626 __isl_take isl_basic_set
*bset
)
1628 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1629 (struct isl_basic_map
*)bset
);
1632 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1641 map
= isl_map_cow(map
);
1645 for (i
= 0; i
< map
->n
; ++i
) {
1646 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1656 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1658 return isl_map_remove_divs(set
);
1661 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1662 enum isl_dim_type type
, unsigned first
, unsigned n
)
1666 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1668 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1670 bmap
= isl_basic_map_eliminate_vars(bmap
,
1671 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1674 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1676 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1679 isl_basic_map_free(bmap
);
1683 /* Return true if the definition of the given div (recursively) involves
1684 * any of the given variables.
1686 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1687 unsigned first
, unsigned n
)
1690 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1692 if (isl_int_is_zero(bmap
->div
[div
][0]))
1694 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1697 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1698 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1700 if (div_involves_vars(bmap
, i
, first
, n
))
1707 /* Remove all divs (recursively) involving any of the given dimensions
1708 * in their definitions.
1710 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1711 __isl_take isl_basic_map
*bmap
,
1712 enum isl_dim_type type
, unsigned first
, unsigned n
)
1718 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1720 first
+= isl_basic_map_offset(bmap
, type
);
1722 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1723 if (!div_involves_vars(bmap
, i
, first
, n
))
1725 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1733 isl_basic_map_free(bmap
);
1737 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1738 enum isl_dim_type type
, unsigned first
, unsigned n
)
1747 map
= isl_map_cow(map
);
1751 for (i
= 0; i
< map
->n
; ++i
) {
1752 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1763 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1764 enum isl_dim_type type
, unsigned first
, unsigned n
)
1766 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1770 /* Does the desciption of "bmap" depend on the specified dimensions?
1771 * We also check whether the dimensions appear in any of the div definitions.
1772 * In principle there is no need for this check. If the dimensions appear
1773 * in a div definition, they also appear in the defining constraints of that
1776 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1777 enum isl_dim_type type
, unsigned first
, unsigned n
)
1784 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1785 isl_die(bmap
->ctx
, isl_error_invalid
,
1786 "index out of bounds", return -1);
1788 first
+= isl_basic_map_offset(bmap
, type
);
1789 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1790 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1792 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1793 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1795 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1796 if (isl_int_is_zero(bmap
->div
[i
][0]))
1798 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
1805 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1806 enum isl_dim_type type
, unsigned first
, unsigned n
)
1813 if (first
+ n
> isl_map_dim(map
, type
))
1814 isl_die(map
->ctx
, isl_error_invalid
,
1815 "index out of bounds", return -1);
1817 for (i
= 0; i
< map
->n
; ++i
) {
1818 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1820 if (involves
< 0 || involves
)
1827 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1828 enum isl_dim_type type
, unsigned first
, unsigned n
)
1830 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1833 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1834 enum isl_dim_type type
, unsigned first
, unsigned n
)
1836 return isl_map_involves_dims(set
, type
, first
, n
);
1839 /* Return true if the definition of the given div is unknown or depends
1842 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1845 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1847 if (isl_int_is_zero(bmap
->div
[div
][0]))
1850 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1851 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1853 if (div_is_unknown(bmap
, i
))
1860 /* Remove all divs that are unknown or defined in terms of unknown divs.
1862 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1863 __isl_take isl_basic_map
*bmap
)
1870 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1871 if (!div_is_unknown(bmap
, i
))
1873 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1882 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1891 map
= isl_map_cow(map
);
1895 for (i
= 0; i
< map
->n
; ++i
) {
1896 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1906 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1908 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1911 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1912 __isl_take isl_basic_set
*bset
,
1913 enum isl_dim_type type
, unsigned first
, unsigned n
)
1915 return (isl_basic_set
*)
1916 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1919 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1920 enum isl_dim_type type
, unsigned first
, unsigned n
)
1927 map
= isl_map_cow(map
);
1930 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1932 for (i
= 0; i
< map
->n
; ++i
) {
1933 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1934 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1938 map
= isl_map_drop(map
, type
, first
, n
);
1945 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1946 enum isl_dim_type type
, unsigned first
, unsigned n
)
1948 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1951 /* Project out n inputs starting at first using Fourier-Motzkin */
1952 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1953 unsigned first
, unsigned n
)
1955 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1958 static void dump_term(struct isl_basic_map
*bmap
,
1959 isl_int c
, int pos
, FILE *out
)
1962 unsigned in
= isl_basic_map_n_in(bmap
);
1963 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1964 unsigned nparam
= isl_basic_map_n_param(bmap
);
1966 isl_int_print(out
, c
, 0);
1968 if (!isl_int_is_one(c
))
1969 isl_int_print(out
, c
, 0);
1970 if (pos
< 1 + nparam
) {
1971 name
= isl_space_get_dim_name(bmap
->dim
,
1972 isl_dim_param
, pos
- 1);
1974 fprintf(out
, "%s", name
);
1976 fprintf(out
, "p%d", pos
- 1);
1977 } else if (pos
< 1 + nparam
+ in
)
1978 fprintf(out
, "i%d", pos
- 1 - nparam
);
1979 else if (pos
< 1 + nparam
+ dim
)
1980 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1982 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1986 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1987 int sign
, FILE *out
)
1991 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1995 for (i
= 0, first
= 1; i
< len
; ++i
) {
1996 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1999 fprintf(out
, " + ");
2001 isl_int_abs(v
, c
[i
]);
2002 dump_term(bmap
, v
, i
, out
);
2009 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2010 const char *op
, FILE *out
, int indent
)
2014 fprintf(out
, "%*s", indent
, "");
2016 dump_constraint_sign(bmap
, c
, 1, out
);
2017 fprintf(out
, " %s ", op
);
2018 dump_constraint_sign(bmap
, c
, -1, out
);
2022 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2023 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2025 fprintf(out
, "%*s", indent
, "");
2026 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2031 static void dump_constraints(struct isl_basic_map
*bmap
,
2032 isl_int
**c
, unsigned n
,
2033 const char *op
, FILE *out
, int indent
)
2037 for (i
= 0; i
< n
; ++i
)
2038 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2041 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2045 unsigned total
= isl_basic_map_total_dim(bmap
);
2047 for (j
= 0; j
< 1 + total
; ++j
) {
2048 if (isl_int_is_zero(exp
[j
]))
2050 if (!first
&& isl_int_is_pos(exp
[j
]))
2052 dump_term(bmap
, exp
[j
], j
, out
);
2057 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2061 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2062 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2064 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2065 fprintf(out
, "%*s", indent
, "");
2066 fprintf(out
, "e%d = [(", i
);
2067 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2069 isl_int_print(out
, bmap
->div
[i
][0], 0);
2070 fprintf(out
, "]\n");
2074 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2075 FILE *out
, int indent
)
2078 fprintf(out
, "null basic set\n");
2082 fprintf(out
, "%*s", indent
, "");
2083 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2084 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2085 bset
->extra
, bset
->flags
);
2086 dump((struct isl_basic_map
*)bset
, out
, indent
);
2089 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2090 FILE *out
, int indent
)
2093 fprintf(out
, "null basic map\n");
2097 fprintf(out
, "%*s", indent
, "");
2098 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2099 "flags: %x, n_name: %d\n",
2101 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2102 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2103 dump(bmap
, out
, indent
);
2106 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2111 total
= isl_basic_map_total_dim(bmap
);
2112 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2113 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2114 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2115 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2119 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2122 struct isl_set
*set
;
2126 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2127 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2128 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2129 sizeof(struct isl_set
) +
2130 (n
- 1) * sizeof(struct isl_basic_set
*));
2134 set
->ctx
= dim
->ctx
;
2135 isl_ctx_ref(set
->ctx
);
2143 isl_space_free(dim
);
2147 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2148 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2150 struct isl_set
*set
;
2153 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2157 set
= isl_set_alloc_space(dims
, n
, flags
);
2161 /* Make sure "map" has room for at least "n" more basic maps.
2163 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2166 struct isl_map
*grown
= NULL
;
2170 isl_assert(map
->ctx
, n
>= 0, goto error
);
2171 if (map
->n
+ n
<= map
->size
)
2173 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2176 for (i
= 0; i
< map
->n
; ++i
) {
2177 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2185 isl_map_free(grown
);
2190 /* Make sure "set" has room for at least "n" more basic sets.
2192 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2194 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2197 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2200 struct isl_set
*dup
;
2205 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2208 for (i
= 0; i
< set
->n
; ++i
)
2209 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2213 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2215 return isl_map_from_basic_map(bset
);
2218 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2220 struct isl_map
*map
;
2225 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2226 return isl_map_add_basic_map(map
, bmap
);
2229 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2230 __isl_take isl_basic_set
*bset
)
2232 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2233 (struct isl_basic_map
*)bset
);
2236 void isl_set_free(struct isl_set
*set
)
2246 isl_ctx_deref(set
->ctx
);
2247 for (i
= 0; i
< set
->n
; ++i
)
2248 isl_basic_set_free(set
->p
[i
]);
2249 isl_space_free(set
->dim
);
2253 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2258 fprintf(out
, "null set\n");
2262 fprintf(out
, "%*s", indent
, "");
2263 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2264 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2266 for (i
= 0; i
< set
->n
; ++i
) {
2267 fprintf(out
, "%*s", indent
, "");
2268 fprintf(out
, "basic set %d:\n", i
);
2269 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2273 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2278 fprintf(out
, "null map\n");
2282 fprintf(out
, "%*s", indent
, "");
2283 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2284 "flags: %x, n_name: %d\n",
2285 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2286 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2287 for (i
= 0; i
< map
->n
; ++i
) {
2288 fprintf(out
, "%*s", indent
, "");
2289 fprintf(out
, "basic map %d:\n", i
);
2290 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2294 struct isl_basic_map
*isl_basic_map_intersect_domain(
2295 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2297 struct isl_basic_map
*bmap_domain
;
2302 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2303 bset
->dim
, isl_dim_param
), goto error
);
2305 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2306 isl_assert(bset
->ctx
,
2307 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2309 bmap
= isl_basic_map_cow(bmap
);
2312 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2313 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2314 bmap_domain
= isl_basic_map_from_domain(bset
);
2315 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2317 bmap
= isl_basic_map_simplify(bmap
);
2318 return isl_basic_map_finalize(bmap
);
2320 isl_basic_map_free(bmap
);
2321 isl_basic_set_free(bset
);
2325 struct isl_basic_map
*isl_basic_map_intersect_range(
2326 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2328 struct isl_basic_map
*bmap_range
;
2333 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2334 bset
->dim
, isl_dim_param
), goto error
);
2336 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2337 isl_assert(bset
->ctx
,
2338 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2340 if (isl_basic_set_is_universe(bset
)) {
2341 isl_basic_set_free(bset
);
2345 bmap
= isl_basic_map_cow(bmap
);
2348 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2349 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2350 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2351 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2353 bmap
= isl_basic_map_simplify(bmap
);
2354 return isl_basic_map_finalize(bmap
);
2356 isl_basic_map_free(bmap
);
2357 isl_basic_set_free(bset
);
2361 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2367 total
= 1 + isl_basic_map_total_dim(bmap
);
2368 if (total
!= vec
->size
)
2373 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2374 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2375 if (!isl_int_is_zero(s
)) {
2381 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2382 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2383 if (isl_int_is_neg(s
)) {
2394 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2396 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2399 struct isl_basic_map
*isl_basic_map_intersect(
2400 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2402 struct isl_vec
*sample
= NULL
;
2404 if (!bmap1
|| !bmap2
)
2407 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2408 bmap2
->dim
, isl_dim_param
), goto error
);
2409 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2410 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2411 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2412 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2413 return isl_basic_map_intersect(bmap2
, bmap1
);
2415 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2416 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2417 isl_assert(bmap1
->ctx
,
2418 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2420 if (bmap1
->sample
&&
2421 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2422 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2423 sample
= isl_vec_copy(bmap1
->sample
);
2424 else if (bmap2
->sample
&&
2425 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2426 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2427 sample
= isl_vec_copy(bmap2
->sample
);
2429 bmap1
= isl_basic_map_cow(bmap1
);
2432 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2433 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2434 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2437 isl_vec_free(sample
);
2439 isl_vec_free(bmap1
->sample
);
2440 bmap1
->sample
= sample
;
2443 bmap1
= isl_basic_map_simplify(bmap1
);
2444 return isl_basic_map_finalize(bmap1
);
2447 isl_vec_free(sample
);
2448 isl_basic_map_free(bmap1
);
2449 isl_basic_map_free(bmap2
);
2453 struct isl_basic_set
*isl_basic_set_intersect(
2454 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2456 return (struct isl_basic_set
*)
2457 isl_basic_map_intersect(
2458 (struct isl_basic_map
*)bset1
,
2459 (struct isl_basic_map
*)bset2
);
2462 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2463 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2465 return isl_basic_set_intersect(bset1
, bset2
);
2468 /* Special case of isl_map_intersect, where both map1 and map2
2469 * are convex, without any divs and such that either map1 or map2
2470 * contains a single constraint. This constraint is then simply
2471 * added to the other map.
2473 static __isl_give isl_map
*map_intersect_add_constraint(
2474 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2476 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2477 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2478 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2479 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2481 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2482 return isl_map_intersect(map2
, map1
);
2484 isl_assert(map2
->ctx
,
2485 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2487 map1
= isl_map_cow(map1
);
2490 if (isl_map_plain_is_empty(map1
)) {
2494 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2495 if (map2
->p
[0]->n_eq
== 1)
2496 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2498 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2499 map2
->p
[0]->ineq
[0]);
2501 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2502 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2506 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2507 isl_basic_map_free(map1
->p
[0]);
2520 /* map2 may be either a parameter domain or a map living in the same
2523 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2524 __isl_take isl_map
*map2
)
2527 struct isl_map
*result
;
2533 if (isl_map_plain_is_empty(map1
) &&
2534 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2538 if (isl_map_plain_is_empty(map2
) &&
2539 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2544 if (map1
->n
== 1 && map2
->n
== 1 &&
2545 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2546 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2547 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2548 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2549 return map_intersect_add_constraint(map1
, map2
);
2551 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2552 isl_space_dim(map2
->dim
, isl_dim_param
))
2553 isl_assert(map1
->ctx
,
2554 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2556 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2557 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2558 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2560 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2561 map1
->n
* map2
->n
, flags
);
2564 for (i
= 0; i
< map1
->n
; ++i
)
2565 for (j
= 0; j
< map2
->n
; ++j
) {
2566 struct isl_basic_map
*part
;
2567 part
= isl_basic_map_intersect(
2568 isl_basic_map_copy(map1
->p
[i
]),
2569 isl_basic_map_copy(map2
->p
[j
]));
2570 if (isl_basic_map_is_empty(part
))
2571 isl_basic_map_free(part
);
2573 result
= isl_map_add_basic_map(result
, part
);
2586 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2587 __isl_take isl_map
*map2
)
2591 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2592 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2593 "spaces don't match", goto error
);
2594 return map_intersect_internal(map1
, map2
);
2601 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2602 __isl_take isl_map
*map2
)
2604 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2607 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2609 return (struct isl_set
*)
2610 isl_map_intersect((struct isl_map
*)set1
,
2611 (struct isl_map
*)set2
);
2614 /* map_intersect_internal accepts intersections
2615 * with parameter domains, so we can just call that function.
2617 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2618 __isl_take isl_set
*params
)
2620 return map_intersect_internal(map
, params
);
2623 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2624 __isl_take isl_map
*map2
)
2626 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2629 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2630 __isl_take isl_set
*params
)
2632 return isl_map_intersect_params(set
, params
);
2635 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2638 struct isl_basic_set
*bset
;
2643 bmap
= isl_basic_map_cow(bmap
);
2646 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2647 in
= isl_basic_map_n_in(bmap
);
2648 bset
= isl_basic_set_from_basic_map(bmap
);
2649 bset
= isl_basic_set_swap_vars(bset
, in
);
2650 return isl_basic_map_from_basic_set(bset
, dim
);
2653 static __isl_give isl_basic_map
*basic_map_space_reset(
2654 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2658 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2661 space
= isl_basic_map_get_space(bmap
);
2662 space
= isl_space_reset(space
, type
);
2663 bmap
= isl_basic_map_reset_space(bmap
, space
);
2667 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2668 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2671 struct isl_basic_map
*res
;
2672 struct isl_dim_map
*dim_map
;
2673 unsigned total
, off
;
2674 enum isl_dim_type t
;
2677 return basic_map_space_reset(bmap
, type
);
2682 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2684 total
= isl_basic_map_total_dim(bmap
) + n
;
2685 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2687 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2689 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2691 unsigned size
= isl_basic_map_dim(bmap
, t
);
2692 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2694 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2695 pos
, size
- pos
, off
+ pos
+ n
);
2697 off
+= isl_space_dim(res_dim
, t
);
2699 isl_dim_map_div(dim_map
, bmap
, off
);
2701 res
= isl_basic_map_alloc_space(res_dim
,
2702 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2703 if (isl_basic_map_is_rational(bmap
))
2704 res
= isl_basic_map_set_rational(res
);
2705 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2706 return isl_basic_map_finalize(res
);
2709 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2710 enum isl_dim_type type
, unsigned n
)
2714 return isl_basic_map_insert(bmap
, type
,
2715 isl_basic_map_dim(bmap
, type
), n
);
2718 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2719 enum isl_dim_type type
, unsigned n
)
2723 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2724 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2726 isl_basic_set_free(bset
);
2730 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2731 enum isl_dim_type type
)
2735 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2738 space
= isl_map_get_space(map
);
2739 space
= isl_space_reset(space
, type
);
2740 map
= isl_map_reset_space(map
, space
);
2744 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2745 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2750 return map_space_reset(map
, type
);
2752 map
= isl_map_cow(map
);
2756 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2760 for (i
= 0; i
< map
->n
; ++i
) {
2761 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2772 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2773 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2775 return isl_map_insert_dims(set
, type
, pos
, n
);
2778 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2779 enum isl_dim_type type
, unsigned n
)
2783 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2786 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2787 enum isl_dim_type type
, unsigned n
)
2791 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2792 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2798 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2799 __isl_take isl_basic_map
*bmap
,
2800 enum isl_dim_type dst_type
, unsigned dst_pos
,
2801 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2803 struct isl_dim_map
*dim_map
;
2804 struct isl_basic_map
*res
;
2805 enum isl_dim_type t
;
2806 unsigned total
, off
;
2813 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2816 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2819 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2821 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2822 pos(bmap
->dim
, src_type
) + src_pos
+
2823 ((src_type
< dst_type
) ? n
: 0)) {
2824 bmap
= isl_basic_map_cow(bmap
);
2828 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2829 src_type
, src_pos
, n
);
2833 bmap
= isl_basic_map_finalize(bmap
);
2838 total
= isl_basic_map_total_dim(bmap
);
2839 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2842 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2843 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2844 if (t
== dst_type
) {
2845 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2848 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2851 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2852 dst_pos
, size
- dst_pos
, off
);
2853 off
+= size
- dst_pos
;
2854 } else if (t
== src_type
) {
2855 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2858 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2859 src_pos
+ n
, size
- src_pos
- n
, off
);
2860 off
+= size
- src_pos
- n
;
2862 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2866 isl_dim_map_div(dim_map
, bmap
, off
);
2868 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2869 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2870 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2872 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2873 src_type
, src_pos
, n
);
2877 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2878 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2879 bmap
= isl_basic_map_finalize(bmap
);
2883 isl_basic_map_free(bmap
);
2887 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2888 enum isl_dim_type dst_type
, unsigned dst_pos
,
2889 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2891 return (isl_basic_set
*)isl_basic_map_move_dims(
2892 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2895 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2896 enum isl_dim_type dst_type
, unsigned dst_pos
,
2897 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2901 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2902 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2903 src_type
, src_pos
, n
);
2909 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2910 enum isl_dim_type dst_type
, unsigned dst_pos
,
2911 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2920 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2923 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2926 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2928 map
= isl_map_cow(map
);
2932 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2936 for (i
= 0; i
< map
->n
; ++i
) {
2937 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2939 src_type
, src_pos
, n
);
2950 /* Move the specified dimensions to the last columns right before
2951 * the divs. Don't change the dimension specification of bmap.
2952 * That's the responsibility of the caller.
2954 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2955 enum isl_dim_type type
, unsigned first
, unsigned n
)
2957 struct isl_dim_map
*dim_map
;
2958 struct isl_basic_map
*res
;
2959 enum isl_dim_type t
;
2960 unsigned total
, off
;
2964 if (pos(bmap
->dim
, type
) + first
+ n
==
2965 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2968 total
= isl_basic_map_total_dim(bmap
);
2969 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2972 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2973 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2975 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2978 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2979 first
, n
, total
- bmap
->n_div
- n
);
2980 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2981 first
+ n
, size
- (first
+ n
), off
);
2982 off
+= size
- (first
+ n
);
2984 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2988 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2990 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2991 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2992 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2996 /* Turn the n dimensions of type type, starting at first
2997 * into existentially quantified variables.
2999 __isl_give isl_basic_map
*isl_basic_map_project_out(
3000 __isl_take isl_basic_map
*bmap
,
3001 enum isl_dim_type type
, unsigned first
, unsigned n
)
3009 return basic_map_space_reset(bmap
, type
);
3014 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3015 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3017 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3020 bmap
= move_last(bmap
, type
, first
, n
);
3021 bmap
= isl_basic_map_cow(bmap
);
3025 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3026 old
= bmap
->block2
.data
;
3027 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3028 (bmap
->extra
+ n
) * (1 + row_size
));
3029 if (!bmap
->block2
.data
)
3031 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3034 for (i
= 0; i
< n
; ++i
) {
3035 new_div
[i
] = bmap
->block2
.data
+
3036 (bmap
->extra
+ i
) * (1 + row_size
);
3037 isl_seq_clr(new_div
[i
], 1 + row_size
);
3039 for (i
= 0; i
< bmap
->extra
; ++i
)
3040 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3042 bmap
->div
= new_div
;
3046 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3049 bmap
= isl_basic_map_simplify(bmap
);
3050 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3051 return isl_basic_map_finalize(bmap
);
3053 isl_basic_map_free(bmap
);
3057 /* Turn the n dimensions of type type, starting at first
3058 * into existentially quantified variables.
3060 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3061 enum isl_dim_type type
, unsigned first
, unsigned n
)
3063 return (isl_basic_set
*)isl_basic_map_project_out(
3064 (isl_basic_map
*)bset
, type
, first
, n
);
3067 /* Turn the n dimensions of type type, starting at first
3068 * into existentially quantified variables.
3070 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3071 enum isl_dim_type type
, unsigned first
, unsigned n
)
3079 return map_space_reset(map
, type
);
3081 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3083 map
= isl_map_cow(map
);
3087 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3091 for (i
= 0; i
< map
->n
; ++i
) {
3092 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3103 /* Turn the n dimensions of type type, starting at first
3104 * into existentially quantified variables.
3106 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3107 enum isl_dim_type type
, unsigned first
, unsigned n
)
3109 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3112 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3116 for (i
= 0; i
< n
; ++i
) {
3117 j
= isl_basic_map_alloc_div(bmap
);
3120 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3124 isl_basic_map_free(bmap
);
3128 struct isl_basic_map
*isl_basic_map_apply_range(
3129 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3131 isl_space
*dim_result
= NULL
;
3132 struct isl_basic_map
*bmap
;
3133 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3134 struct isl_dim_map
*dim_map1
, *dim_map2
;
3136 if (!bmap1
|| !bmap2
)
3139 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3140 isl_space_copy(bmap2
->dim
));
3142 n_in
= isl_basic_map_n_in(bmap1
);
3143 n_out
= isl_basic_map_n_out(bmap2
);
3144 n
= isl_basic_map_n_out(bmap1
);
3145 nparam
= isl_basic_map_n_param(bmap1
);
3147 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3148 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3149 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3150 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3151 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3152 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3153 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3154 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3155 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3156 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3157 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3159 bmap
= isl_basic_map_alloc_space(dim_result
,
3160 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3161 bmap1
->n_eq
+ bmap2
->n_eq
,
3162 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3163 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3164 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3165 bmap
= add_divs(bmap
, n
);
3166 bmap
= isl_basic_map_simplify(bmap
);
3167 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3168 return isl_basic_map_finalize(bmap
);
3170 isl_basic_map_free(bmap1
);
3171 isl_basic_map_free(bmap2
);
3175 struct isl_basic_set
*isl_basic_set_apply(
3176 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3181 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3184 return (struct isl_basic_set
*)
3185 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3187 isl_basic_set_free(bset
);
3188 isl_basic_map_free(bmap
);
3192 struct isl_basic_map
*isl_basic_map_apply_domain(
3193 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3195 if (!bmap1
|| !bmap2
)
3198 isl_assert(bmap1
->ctx
,
3199 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3200 isl_assert(bmap1
->ctx
,
3201 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3204 bmap1
= isl_basic_map_reverse(bmap1
);
3205 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3206 return isl_basic_map_reverse(bmap1
);
3208 isl_basic_map_free(bmap1
);
3209 isl_basic_map_free(bmap2
);
3213 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3214 * A \cap B -> f(A) + f(B)
3216 struct isl_basic_map
*isl_basic_map_sum(
3217 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3219 unsigned n_in
, n_out
, nparam
, total
, pos
;
3220 struct isl_basic_map
*bmap
= NULL
;
3221 struct isl_dim_map
*dim_map1
, *dim_map2
;
3224 if (!bmap1
|| !bmap2
)
3227 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3230 nparam
= isl_basic_map_n_param(bmap1
);
3231 n_in
= isl_basic_map_n_in(bmap1
);
3232 n_out
= isl_basic_map_n_out(bmap1
);
3234 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3235 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3236 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3237 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3238 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3239 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3240 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3241 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3242 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3243 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3244 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3246 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3247 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3248 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3249 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3250 for (i
= 0; i
< n_out
; ++i
) {
3251 int j
= isl_basic_map_alloc_equality(bmap
);
3254 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3255 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3256 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3257 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3259 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3260 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3261 bmap
= add_divs(bmap
, 2 * n_out
);
3263 bmap
= isl_basic_map_simplify(bmap
);
3264 return isl_basic_map_finalize(bmap
);
3266 isl_basic_map_free(bmap
);
3267 isl_basic_map_free(bmap1
);
3268 isl_basic_map_free(bmap2
);
3272 /* Given two maps A -> f(A) and B -> g(B), construct a map
3273 * A \cap B -> f(A) + f(B)
3275 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3277 struct isl_map
*result
;
3283 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3285 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3286 map1
->n
* map2
->n
, 0);
3289 for (i
= 0; i
< map1
->n
; ++i
)
3290 for (j
= 0; j
< map2
->n
; ++j
) {
3291 struct isl_basic_map
*part
;
3292 part
= isl_basic_map_sum(
3293 isl_basic_map_copy(map1
->p
[i
]),
3294 isl_basic_map_copy(map2
->p
[j
]));
3295 if (isl_basic_map_is_empty(part
))
3296 isl_basic_map_free(part
);
3298 result
= isl_map_add_basic_map(result
, part
);
3311 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3312 __isl_take isl_set
*set2
)
3314 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3317 /* Given a basic map A -> f(A), construct A -> -f(A).
3319 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3324 bmap
= isl_basic_map_cow(bmap
);
3328 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3329 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3330 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3331 for (j
= 0; j
< n
; ++j
)
3332 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3333 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3334 for (j
= 0; j
< n
; ++j
)
3335 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3336 for (i
= 0; i
< bmap
->n_div
; ++i
)
3337 for (j
= 0; j
< n
; ++j
)
3338 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3339 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3340 return isl_basic_map_finalize(bmap
);
3343 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3345 return isl_basic_map_neg(bset
);
3348 /* Given a map A -> f(A), construct A -> -f(A).
3350 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3354 map
= isl_map_cow(map
);
3358 for (i
= 0; i
< map
->n
; ++i
) {
3359 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3370 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3372 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3375 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3376 * A -> floor(f(A)/d).
3378 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3381 unsigned n_in
, n_out
, nparam
, total
, pos
;
3382 struct isl_basic_map
*result
= NULL
;
3383 struct isl_dim_map
*dim_map
;
3389 nparam
= isl_basic_map_n_param(bmap
);
3390 n_in
= isl_basic_map_n_in(bmap
);
3391 n_out
= isl_basic_map_n_out(bmap
);
3393 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3394 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3395 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3396 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3397 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3398 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3400 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3401 bmap
->n_div
+ n_out
,
3402 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3403 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3404 result
= add_divs(result
, n_out
);
3405 for (i
= 0; i
< n_out
; ++i
) {
3407 j
= isl_basic_map_alloc_inequality(result
);
3410 isl_seq_clr(result
->ineq
[j
], 1+total
);
3411 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3412 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3413 j
= isl_basic_map_alloc_inequality(result
);
3416 isl_seq_clr(result
->ineq
[j
], 1+total
);
3417 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3418 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3419 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3422 result
= isl_basic_map_simplify(result
);
3423 return isl_basic_map_finalize(result
);
3425 isl_basic_map_free(result
);
3429 /* Given a map A -> f(A) and an integer d, construct a map
3430 * A -> floor(f(A)/d).
3432 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3436 map
= isl_map_cow(map
);
3440 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3441 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3442 for (i
= 0; i
< map
->n
; ++i
) {
3443 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3454 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3460 i
= isl_basic_map_alloc_equality(bmap
);
3463 nparam
= isl_basic_map_n_param(bmap
);
3464 n_in
= isl_basic_map_n_in(bmap
);
3465 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3466 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3467 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3468 return isl_basic_map_finalize(bmap
);
3470 isl_basic_map_free(bmap
);
3474 /* Add a constraints to "bmap" expressing i_pos < o_pos
3476 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3482 i
= isl_basic_map_alloc_inequality(bmap
);
3485 nparam
= isl_basic_map_n_param(bmap
);
3486 n_in
= isl_basic_map_n_in(bmap
);
3487 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3488 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3489 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3490 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3491 return isl_basic_map_finalize(bmap
);
3493 isl_basic_map_free(bmap
);
3497 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3499 static __isl_give isl_basic_map
*var_less_or_equal(
3500 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3506 i
= isl_basic_map_alloc_inequality(bmap
);
3509 nparam
= isl_basic_map_n_param(bmap
);
3510 n_in
= isl_basic_map_n_in(bmap
);
3511 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3512 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3513 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3514 return isl_basic_map_finalize(bmap
);
3516 isl_basic_map_free(bmap
);
3520 /* Add a constraints to "bmap" expressing i_pos > o_pos
3522 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3528 i
= isl_basic_map_alloc_inequality(bmap
);
3531 nparam
= isl_basic_map_n_param(bmap
);
3532 n_in
= isl_basic_map_n_in(bmap
);
3533 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3534 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3535 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3536 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3537 return isl_basic_map_finalize(bmap
);
3539 isl_basic_map_free(bmap
);
3543 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3545 static __isl_give isl_basic_map
*var_more_or_equal(
3546 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3552 i
= isl_basic_map_alloc_inequality(bmap
);
3555 nparam
= isl_basic_map_n_param(bmap
);
3556 n_in
= isl_basic_map_n_in(bmap
);
3557 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3558 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3559 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3560 return isl_basic_map_finalize(bmap
);
3562 isl_basic_map_free(bmap
);
3566 __isl_give isl_basic_map
*isl_basic_map_equal(
3567 __isl_take isl_space
*dim
, unsigned n_equal
)
3570 struct isl_basic_map
*bmap
;
3571 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3574 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3575 bmap
= var_equal(bmap
, i
);
3576 return isl_basic_map_finalize(bmap
);
3579 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3581 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3585 struct isl_basic_map
*bmap
;
3586 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3589 for (i
= 0; i
< pos
&& bmap
; ++i
)
3590 bmap
= var_equal(bmap
, i
);
3592 bmap
= var_less(bmap
, pos
);
3593 return isl_basic_map_finalize(bmap
);
3596 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3598 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3599 __isl_take isl_space
*dim
, unsigned pos
)
3602 isl_basic_map
*bmap
;
3604 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3605 for (i
= 0; i
< pos
; ++i
)
3606 bmap
= var_equal(bmap
, i
);
3607 bmap
= var_less_or_equal(bmap
, pos
);
3608 return isl_basic_map_finalize(bmap
);
3611 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3613 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3617 struct isl_basic_map
*bmap
;
3618 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3621 for (i
= 0; i
< pos
&& bmap
; ++i
)
3622 bmap
= var_equal(bmap
, i
);
3624 bmap
= var_more(bmap
, pos
);
3625 return isl_basic_map_finalize(bmap
);
3628 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3630 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3631 __isl_take isl_space
*dim
, unsigned pos
)
3634 isl_basic_map
*bmap
;
3636 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3637 for (i
= 0; i
< pos
; ++i
)
3638 bmap
= var_equal(bmap
, i
);
3639 bmap
= var_more_or_equal(bmap
, pos
);
3640 return isl_basic_map_finalize(bmap
);
3643 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3644 unsigned n
, int equal
)
3646 struct isl_map
*map
;
3649 if (n
== 0 && equal
)
3650 return isl_map_universe(dims
);
3652 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3654 for (i
= 0; i
+ 1 < n
; ++i
)
3655 map
= isl_map_add_basic_map(map
,
3656 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3659 map
= isl_map_add_basic_map(map
,
3660 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3662 map
= isl_map_add_basic_map(map
,
3663 isl_basic_map_less_at(dims
, n
- 1));
3665 isl_space_free(dims
);
3670 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3674 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3677 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3679 return map_lex_lte_first(dim
, n
, 0);
3682 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3684 return map_lex_lte_first(dim
, n
, 1);
3687 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3689 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3692 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3694 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3697 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3698 unsigned n
, int equal
)
3700 struct isl_map
*map
;
3703 if (n
== 0 && equal
)
3704 return isl_map_universe(dims
);
3706 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3708 for (i
= 0; i
+ 1 < n
; ++i
)
3709 map
= isl_map_add_basic_map(map
,
3710 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3713 map
= isl_map_add_basic_map(map
,
3714 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3716 map
= isl_map_add_basic_map(map
,
3717 isl_basic_map_more_at(dims
, n
- 1));
3719 isl_space_free(dims
);
3724 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3728 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3731 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3733 return map_lex_gte_first(dim
, n
, 0);
3736 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3738 return map_lex_gte_first(dim
, n
, 1);
3741 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3743 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3746 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3748 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3751 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3752 __isl_take isl_set
*set2
)
3755 map
= isl_map_lex_le(isl_set_get_space(set1
));
3756 map
= isl_map_intersect_domain(map
, set1
);
3757 map
= isl_map_intersect_range(map
, set2
);
3761 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3762 __isl_take isl_set
*set2
)
3765 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3766 map
= isl_map_intersect_domain(map
, set1
);
3767 map
= isl_map_intersect_range(map
, set2
);
3771 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3772 __isl_take isl_set
*set2
)
3775 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3776 map
= isl_map_intersect_domain(map
, set1
);
3777 map
= isl_map_intersect_range(map
, set2
);
3781 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3782 __isl_take isl_set
*set2
)
3785 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3786 map
= isl_map_intersect_domain(map
, set1
);
3787 map
= isl_map_intersect_range(map
, set2
);
3791 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3792 __isl_take isl_map
*map2
)
3795 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3796 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3797 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3801 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3802 __isl_take isl_map
*map2
)
3805 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3806 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3807 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3811 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3812 __isl_take isl_map
*map2
)
3815 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3816 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3817 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3821 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3822 __isl_take isl_map
*map2
)
3825 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3826 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3827 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3831 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3832 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3834 struct isl_basic_map
*bmap
;
3836 bset
= isl_basic_set_cow(bset
);
3840 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3841 isl_space_free(bset
->dim
);
3842 bmap
= (struct isl_basic_map
*) bset
;
3844 return isl_basic_map_finalize(bmap
);
3846 isl_basic_set_free(bset
);
3847 isl_space_free(dim
);
3851 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3855 if (bmap
->dim
->n_in
== 0)
3856 return (struct isl_basic_set
*)bmap
;
3857 bmap
= isl_basic_map_cow(bmap
);
3860 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3863 bmap
= isl_basic_map_finalize(bmap
);
3864 return (struct isl_basic_set
*)bmap
;
3866 isl_basic_map_free(bmap
);
3870 /* For a div d = floor(f/m), add the constraints
3873 * -(f-(n-1)) + m d >= 0
3875 * Note that the second constraint is the negation of
3879 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3880 unsigned pos
, isl_int
*div
)
3883 unsigned total
= isl_basic_map_total_dim(bmap
);
3885 i
= isl_basic_map_alloc_inequality(bmap
);
3888 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3889 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3891 j
= isl_basic_map_alloc_inequality(bmap
);
3894 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3895 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3896 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3900 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3901 unsigned pos
, isl_int
*div
)
3903 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3907 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3909 unsigned total
= isl_basic_map_total_dim(bmap
);
3910 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3912 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3916 struct isl_basic_set
*isl_basic_map_underlying_set(
3917 struct isl_basic_map
*bmap
)
3921 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3923 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3924 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3925 return (struct isl_basic_set
*)bmap
;
3926 bmap
= isl_basic_map_cow(bmap
);
3929 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3932 bmap
->extra
-= bmap
->n_div
;
3934 bmap
= isl_basic_map_finalize(bmap
);
3935 return (struct isl_basic_set
*)bmap
;
3940 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3941 __isl_take isl_basic_set
*bset
)
3943 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3946 struct isl_basic_map
*isl_basic_map_overlying_set(
3947 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3949 struct isl_basic_map
*bmap
;
3950 struct isl_ctx
*ctx
;
3957 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3958 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3959 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3961 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3962 isl_basic_map_free(like
);
3963 return (struct isl_basic_map
*)bset
;
3965 bset
= isl_basic_set_cow(bset
);
3968 total
= bset
->dim
->n_out
+ bset
->extra
;
3969 bmap
= (struct isl_basic_map
*)bset
;
3970 isl_space_free(bmap
->dim
);
3971 bmap
->dim
= isl_space_copy(like
->dim
);
3974 bmap
->n_div
= like
->n_div
;
3975 bmap
->extra
+= like
->n_div
;
3979 ltotal
= total
- bmap
->extra
+ like
->extra
;
3982 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3983 bmap
->extra
* (1 + 1 + total
));
3984 if (isl_blk_is_error(bmap
->block2
))
3986 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3990 for (i
= 0; i
< bmap
->extra
; ++i
)
3991 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3992 for (i
= 0; i
< like
->n_div
; ++i
) {
3993 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3994 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3996 bmap
= isl_basic_map_extend_constraints(bmap
,
3997 0, 2 * like
->n_div
);
3998 for (i
= 0; i
< like
->n_div
; ++i
) {
3999 if (isl_int_is_zero(bmap
->div
[i
][0]))
4001 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4005 isl_basic_map_free(like
);
4006 bmap
= isl_basic_map_simplify(bmap
);
4007 bmap
= isl_basic_map_finalize(bmap
);
4010 isl_basic_map_free(like
);
4011 isl_basic_set_free(bset
);
4015 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4016 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4018 return (struct isl_basic_set
*)
4019 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4022 struct isl_set
*isl_set_from_underlying_set(
4023 struct isl_set
*set
, struct isl_basic_set
*like
)
4029 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4031 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4032 isl_basic_set_free(like
);
4035 set
= isl_set_cow(set
);
4038 for (i
= 0; i
< set
->n
; ++i
) {
4039 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4040 isl_basic_set_copy(like
));
4044 isl_space_free(set
->dim
);
4045 set
->dim
= isl_space_copy(like
->dim
);
4048 isl_basic_set_free(like
);
4051 isl_basic_set_free(like
);
4056 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4060 map
= isl_map_cow(map
);
4063 map
->dim
= isl_space_cow(map
->dim
);
4067 for (i
= 1; i
< map
->n
; ++i
)
4068 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4070 for (i
= 0; i
< map
->n
; ++i
) {
4071 map
->p
[i
] = (struct isl_basic_map
*)
4072 isl_basic_map_underlying_set(map
->p
[i
]);
4077 map
->dim
= isl_space_underlying(map
->dim
, 0);
4079 isl_space_free(map
->dim
);
4080 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4084 return (struct isl_set
*)map
;
4090 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4092 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4095 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4096 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4098 bmap
= isl_basic_map_cow(bmap
);
4102 isl_space_free(bmap
->dim
);
4105 bmap
= isl_basic_map_finalize(bmap
);
4109 isl_basic_map_free(bmap
);
4110 isl_space_free(dim
);
4114 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4115 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4117 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4121 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4122 __isl_take isl_space
*dim
)
4126 map
= isl_map_cow(map
);
4130 for (i
= 0; i
< map
->n
; ++i
) {
4131 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4132 isl_space_copy(dim
));
4136 isl_space_free(map
->dim
);
4142 isl_space_free(dim
);
4146 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4147 __isl_take isl_space
*dim
)
4149 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4152 /* Compute the parameter domain of the given basic set.
4154 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4159 if (isl_basic_set_is_params(bset
))
4162 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4163 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4164 space
= isl_basic_set_get_space(bset
);
4165 space
= isl_space_params(space
);
4166 bset
= isl_basic_set_reset_space(bset
, space
);
4170 /* Compute the parameter domain of the given set.
4172 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4177 if (isl_set_is_params(set
))
4180 n
= isl_set_dim(set
, isl_dim_set
);
4181 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4182 space
= isl_set_get_space(set
);
4183 space
= isl_space_params(space
);
4184 set
= isl_set_reset_space(set
, space
);
4188 /* Construct a zero-dimensional set with the given parameter domain.
4190 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4193 space
= isl_set_get_space(set
);
4194 space
= isl_space_set_from_params(space
);
4195 set
= isl_set_reset_space(set
, space
);
4199 /* Compute the parameter domain of the given map.
4201 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4206 n
= isl_map_dim(map
, isl_dim_in
);
4207 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4208 n
= isl_map_dim(map
, isl_dim_out
);
4209 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4210 space
= isl_map_get_space(map
);
4211 space
= isl_space_params(space
);
4212 map
= isl_map_reset_space(map
, space
);
4216 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4219 struct isl_basic_set
*domain
;
4225 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4227 n_in
= isl_basic_map_n_in(bmap
);
4228 n_out
= isl_basic_map_n_out(bmap
);
4229 domain
= isl_basic_set_from_basic_map(bmap
);
4230 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4232 domain
= isl_basic_set_reset_space(domain
, dim
);
4237 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4241 return isl_space_may_be_set(bmap
->dim
);
4244 /* Is this basic map actually a set?
4245 * Users should never call this function. Outside of isl,
4246 * the type should indicate whether something is a set or a map.
4248 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4252 return isl_space_is_set(bmap
->dim
);
4255 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4259 if (isl_basic_map_is_set(bmap
))
4261 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4264 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4265 __isl_take isl_basic_map
*bmap
)
4269 isl_basic_map
*domain
;
4270 int nparam
, n_in
, n_out
;
4273 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4274 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4275 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4277 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4278 domain
= isl_basic_map_universe(dim
);
4280 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4281 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4282 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4284 total
= isl_basic_map_total_dim(bmap
);
4286 for (i
= 0; i
< n_in
; ++i
) {
4287 k
= isl_basic_map_alloc_equality(bmap
);
4290 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4291 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4292 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4295 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4296 return isl_basic_map_finalize(bmap
);
4298 isl_basic_map_free(bmap
);
4302 __isl_give isl_basic_map
*isl_basic_map_range_map(
4303 __isl_take isl_basic_map
*bmap
)
4307 isl_basic_map
*range
;
4308 int nparam
, n_in
, n_out
;
4311 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4312 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4313 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4315 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4316 range
= isl_basic_map_universe(dim
);
4318 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4319 bmap
= isl_basic_map_apply_range(bmap
, range
);
4320 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4322 total
= isl_basic_map_total_dim(bmap
);
4324 for (i
= 0; i
< n_out
; ++i
) {
4325 k
= isl_basic_map_alloc_equality(bmap
);
4328 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4329 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4330 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4333 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4334 return isl_basic_map_finalize(bmap
);
4336 isl_basic_map_free(bmap
);
4340 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4344 return isl_space_may_be_set(map
->dim
);
4347 /* Is this map actually a set?
4348 * Users should never call this function. Outside of isl,
4349 * the type should indicate whether something is a set or a map.
4351 int isl_map_is_set(__isl_keep isl_map
*map
)
4355 return isl_space_is_set(map
->dim
);
4358 struct isl_set
*isl_map_range(struct isl_map
*map
)
4361 struct isl_set
*set
;
4365 if (isl_map_is_set(map
))
4366 return (isl_set
*)map
;
4368 map
= isl_map_cow(map
);
4372 set
= (struct isl_set
*) map
;
4373 set
->dim
= isl_space_range(set
->dim
);
4376 for (i
= 0; i
< map
->n
; ++i
) {
4377 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4381 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4382 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4389 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4392 isl_space
*domain_dim
;
4394 map
= isl_map_cow(map
);
4398 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4399 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4400 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4403 for (i
= 0; i
< map
->n
; ++i
) {
4404 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4408 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4409 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4416 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4419 isl_space
*range_dim
;
4421 map
= isl_map_cow(map
);
4425 range_dim
= isl_space_range(isl_map_get_space(map
));
4426 range_dim
= isl_space_from_range(range_dim
);
4427 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4428 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4431 for (i
= 0; i
< map
->n
; ++i
) {
4432 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4436 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4437 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4444 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4445 __isl_take isl_space
*dim
)
4448 struct isl_map
*map
= NULL
;
4450 set
= isl_set_cow(set
);
4453 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4454 map
= (struct isl_map
*)set
;
4455 for (i
= 0; i
< set
->n
; ++i
) {
4456 map
->p
[i
] = isl_basic_map_from_basic_set(
4457 set
->p
[i
], isl_space_copy(dim
));
4461 isl_space_free(map
->dim
);
4465 isl_space_free(dim
);
4470 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4471 __isl_take isl_basic_set
*bset
)
4473 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4476 __isl_give isl_basic_map
*isl_basic_map_from_range(
4477 __isl_take isl_basic_set
*bset
)
4480 space
= isl_basic_set_get_space(bset
);
4481 space
= isl_space_from_range(space
);
4482 bset
= isl_basic_set_reset_space(bset
, space
);
4483 return (isl_basic_map
*)bset
;
4486 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4489 space
= isl_set_get_space(set
);
4490 space
= isl_space_from_range(space
);
4491 set
= isl_set_reset_space(set
, space
);
4492 return (struct isl_map
*)set
;
4495 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4497 return isl_map_reverse(isl_map_from_range(set
));
4500 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4501 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4503 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4506 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4507 __isl_take isl_set
*range
)
4509 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4512 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4515 struct isl_set
*set
= NULL
;
4519 map
= isl_map_cow(map
);
4522 map
->dim
= isl_space_as_set_space(map
->dim
);
4525 set
= (struct isl_set
*)map
;
4526 for (i
= 0; i
< map
->n
; ++i
) {
4527 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4537 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4540 struct isl_map
*map
;
4545 isl_die(dim
->ctx
, isl_error_internal
,
4546 "negative number of basic maps", goto error
);
4547 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4548 sizeof(struct isl_map
) +
4549 (n
- 1) * sizeof(struct isl_basic_map
*));
4553 map
->ctx
= dim
->ctx
;
4554 isl_ctx_ref(map
->ctx
);
4562 isl_space_free(dim
);
4566 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4567 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4570 struct isl_map
*map
;
4573 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4577 map
= isl_map_alloc_space(dims
, n
, flags
);
4581 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4583 struct isl_basic_map
*bmap
;
4584 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4585 bmap
= isl_basic_map_set_to_empty(bmap
);
4589 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4591 struct isl_basic_set
*bset
;
4592 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4593 bset
= isl_basic_set_set_to_empty(bset
);
4597 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4599 struct isl_basic_map
*bmap
;
4602 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4603 bmap
= isl_basic_map_set_to_empty(bmap
);
4607 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4609 struct isl_basic_map
*bmap
;
4612 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4613 bmap
= isl_basic_map_set_to_empty(bmap
);
4617 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4619 struct isl_basic_set
*bset
;
4622 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4623 bset
= isl_basic_set_set_to_empty(bset
);
4627 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4629 struct isl_basic_map
*bmap
;
4630 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4631 bmap
= isl_basic_map_finalize(bmap
);
4635 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4637 struct isl_basic_set
*bset
;
4638 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4639 bset
= isl_basic_set_finalize(bset
);
4643 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4646 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4647 isl_basic_map
*bmap
;
4649 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4650 for (i
= 0; i
< total
; ++i
) {
4651 int k
= isl_basic_map_alloc_inequality(bmap
);
4654 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4655 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4659 isl_basic_map_free(bmap
);
4663 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4665 return isl_basic_map_nat_universe(dim
);
4668 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4670 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4673 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4675 return isl_map_nat_universe(dim
);
4678 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4679 __isl_keep isl_basic_map
*model
)
4683 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4686 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4690 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4693 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4694 __isl_keep isl_set
*model
)
4698 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4701 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4703 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4706 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4710 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4713 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4717 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4720 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4722 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4725 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4729 return isl_set_empty(isl_space_copy(model
->dim
));
4732 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4734 struct isl_map
*map
;
4737 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4738 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4742 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4744 struct isl_set
*set
;
4747 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4748 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4752 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4756 return isl_set_universe(isl_space_copy(model
->dim
));
4759 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4762 struct isl_map
*dup
;
4766 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4767 for (i
= 0; i
< map
->n
; ++i
)
4768 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4772 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4773 __isl_take isl_basic_map
*bmap
)
4777 if (isl_basic_map_plain_is_empty(bmap
)) {
4778 isl_basic_map_free(bmap
);
4781 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4782 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4783 map
->p
[map
->n
] = bmap
;
4785 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4791 isl_basic_map_free(bmap
);
4795 void isl_map_free(struct isl_map
*map
)
4805 isl_ctx_deref(map
->ctx
);
4806 for (i
= 0; i
< map
->n
; ++i
)
4807 isl_basic_map_free(map
->p
[i
]);
4808 isl_space_free(map
->dim
);
4812 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4813 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4817 base
= isl_map_cow(base
);
4821 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4824 for (i
= 0; i
< base
->n
; ++i
) {
4825 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4826 isl_space_copy(base
->dim
), 0, 0, 0);
4836 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4837 unsigned nparam
, unsigned dim
)
4839 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4843 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4844 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4848 bmap
= isl_basic_map_cow(bmap
);
4849 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4850 j
= isl_basic_map_alloc_equality(bmap
);
4853 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4854 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4855 isl_int_set_si(bmap
->eq
[j
][0], value
);
4856 bmap
= isl_basic_map_simplify(bmap
);
4857 return isl_basic_map_finalize(bmap
);
4859 isl_basic_map_free(bmap
);
4863 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4864 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4868 bmap
= isl_basic_map_cow(bmap
);
4869 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4870 j
= isl_basic_map_alloc_equality(bmap
);
4873 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4874 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4875 isl_int_set(bmap
->eq
[j
][0], value
);
4876 bmap
= isl_basic_map_simplify(bmap
);
4877 return isl_basic_map_finalize(bmap
);
4879 isl_basic_map_free(bmap
);
4883 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4884 enum isl_dim_type type
, unsigned pos
, int value
)
4888 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4889 return isl_basic_map_fix_pos_si(bmap
,
4890 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4892 isl_basic_map_free(bmap
);
4896 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4897 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4901 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4902 return isl_basic_map_fix_pos(bmap
,
4903 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4905 isl_basic_map_free(bmap
);
4909 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4910 enum isl_dim_type type
, unsigned pos
, int value
)
4912 return (struct isl_basic_set
*)
4913 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4917 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4918 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4920 return (struct isl_basic_set
*)
4921 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4925 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4926 unsigned input
, int value
)
4928 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4931 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4932 unsigned dim
, int value
)
4934 return (struct isl_basic_set
*)
4935 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4936 isl_dim_set
, dim
, value
);
4939 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
4941 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
4948 isl_basic_map_free(map
->p
[i
]);
4949 if (i
!= map
->n
- 1) {
4950 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4951 map
->p
[i
] = map
->p
[map
->n
- 1];
4958 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4959 enum isl_dim_type type
, unsigned pos
, int value
)
4963 map
= isl_map_cow(map
);
4967 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4968 for (i
= map
->n
- 1; i
>= 0; --i
) {
4969 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4970 if (remove_if_empty(map
, i
) < 0)
4973 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4980 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4981 enum isl_dim_type type
, unsigned pos
, int value
)
4983 return (struct isl_set
*)
4984 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4987 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4988 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4992 map
= isl_map_cow(map
);
4996 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4997 for (i
= 0; i
< map
->n
; ++i
) {
4998 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5002 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5009 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5010 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5012 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5015 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5016 unsigned input
, int value
)
5018 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5021 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5023 return (struct isl_set
*)
5024 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5027 static __isl_give isl_basic_map
*basic_map_bound_si(
5028 __isl_take isl_basic_map
*bmap
,
5029 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5035 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5036 pos
+= isl_basic_map_offset(bmap
, type
);
5037 bmap
= isl_basic_map_cow(bmap
);
5038 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5039 j
= isl_basic_map_alloc_inequality(bmap
);
5042 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5044 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5045 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5047 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5048 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5050 bmap
= isl_basic_map_simplify(bmap
);
5051 return isl_basic_map_finalize(bmap
);
5053 isl_basic_map_free(bmap
);
5057 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5058 __isl_take isl_basic_map
*bmap
,
5059 enum isl_dim_type type
, unsigned pos
, int value
)
5061 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5064 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5065 unsigned dim
, isl_int value
)
5069 bset
= isl_basic_set_cow(bset
);
5070 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5071 j
= isl_basic_set_alloc_inequality(bset
);
5074 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5075 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5076 isl_int_neg(bset
->ineq
[j
][0], value
);
5077 bset
= isl_basic_set_simplify(bset
);
5078 return isl_basic_set_finalize(bset
);
5080 isl_basic_set_free(bset
);
5084 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5085 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5089 map
= isl_map_cow(map
);
5093 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5094 for (i
= 0; i
< map
->n
; ++i
) {
5095 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5096 type
, pos
, value
, upper
);
5100 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5107 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5108 enum isl_dim_type type
, unsigned pos
, int value
)
5110 return map_bound_si(map
, type
, pos
, value
, 0);
5113 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5114 enum isl_dim_type type
, unsigned pos
, int value
)
5116 return map_bound_si(map
, type
, pos
, value
, 1);
5119 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5120 enum isl_dim_type type
, unsigned pos
, int value
)
5122 return (struct isl_set
*)
5123 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5126 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5127 enum isl_dim_type type
, unsigned pos
, int value
)
5129 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5132 /* Bound the given variable of "bmap" from below (or above is "upper"
5133 * is set) to "value".
5135 static __isl_give isl_basic_map
*basic_map_bound(
5136 __isl_take isl_basic_map
*bmap
,
5137 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5143 if (pos
>= isl_basic_map_dim(bmap
, type
))
5144 isl_die(bmap
->ctx
, isl_error_invalid
,
5145 "index out of bounds", goto error
);
5146 pos
+= isl_basic_map_offset(bmap
, type
);
5147 bmap
= isl_basic_map_cow(bmap
);
5148 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5149 j
= isl_basic_map_alloc_inequality(bmap
);
5152 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5154 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5155 isl_int_set(bmap
->ineq
[j
][0], value
);
5157 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5158 isl_int_neg(bmap
->ineq
[j
][0], value
);
5160 bmap
= isl_basic_map_simplify(bmap
);
5161 return isl_basic_map_finalize(bmap
);
5163 isl_basic_map_free(bmap
);
5167 /* Bound the given variable of "map" from below (or above is "upper"
5168 * is set) to "value".
5170 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5171 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5175 map
= isl_map_cow(map
);
5179 if (pos
>= isl_map_dim(map
, type
))
5180 isl_die(map
->ctx
, isl_error_invalid
,
5181 "index out of bounds", goto error
);
5182 for (i
= map
->n
- 1; i
>= 0; --i
) {
5183 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5184 if (remove_if_empty(map
, i
) < 0)
5187 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5194 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5195 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5197 return map_bound(map
, type
, pos
, value
, 0);
5200 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5201 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5203 return map_bound(map
, type
, pos
, value
, 1);
5206 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5207 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5209 return isl_map_lower_bound(set
, type
, pos
, value
);
5212 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5213 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5215 return isl_map_upper_bound(set
, type
, pos
, value
);
5218 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5223 set
= isl_set_cow(set
);
5227 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5228 for (i
= 0; i
< set
->n
; ++i
) {
5229 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5239 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5243 map
= isl_map_cow(map
);
5247 map
->dim
= isl_space_reverse(map
->dim
);
5250 for (i
= 0; i
< map
->n
; ++i
) {
5251 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5255 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5262 static struct isl_map
*isl_basic_map_partial_lexopt(
5263 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5264 struct isl_set
**empty
, int max
)
5268 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5269 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5271 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5273 isl_basic_map_free(bmap
);
5274 isl_basic_set_free(dom
);
5280 struct isl_map
*isl_basic_map_partial_lexmax(
5281 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5282 struct isl_set
**empty
)
5284 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5287 struct isl_map
*isl_basic_map_partial_lexmin(
5288 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5289 struct isl_set
**empty
)
5291 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5294 struct isl_set
*isl_basic_set_partial_lexmin(
5295 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5296 struct isl_set
**empty
)
5298 return (struct isl_set
*)
5299 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5303 struct isl_set
*isl_basic_set_partial_lexmax(
5304 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5305 struct isl_set
**empty
)
5307 return (struct isl_set
*)
5308 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5312 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5313 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5314 __isl_give isl_set
**empty
)
5316 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5319 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5320 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5321 __isl_give isl_set
**empty
)
5323 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5326 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5327 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5328 __isl_give isl_set
**empty
)
5330 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5333 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5334 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5335 __isl_give isl_set
**empty
)
5337 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5340 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5341 __isl_take isl_basic_map
*bmap
, int max
)
5343 isl_basic_set
*dom
= NULL
;
5344 isl_space
*dom_space
;
5348 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5349 dom
= isl_basic_set_universe(dom_space
);
5350 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5352 isl_basic_map_free(bmap
);
5356 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5357 __isl_take isl_basic_map
*bmap
)
5359 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5362 /* Given a basic map "bmap", compute the lexicographically minimal
5363 * (or maximal) image element for each domain element in dom.
5364 * Set *empty to those elements in dom that do not have an image element.
5366 * We first make sure the basic sets in dom are disjoint and then
5367 * simply collect the results over each of the basic sets separately.
5368 * We could probably improve the efficiency a bit by moving the union
5369 * domain down into the parametric integer programming.
5371 static __isl_give isl_map
*basic_map_partial_lexopt(
5372 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5373 __isl_give isl_set
**empty
, int max
)
5376 struct isl_map
*res
;
5378 dom
= isl_set_make_disjoint(dom
);
5382 if (isl_set_plain_is_empty(dom
)) {
5383 res
= isl_map_empty_like_basic_map(bmap
);
5384 *empty
= isl_set_empty_like(dom
);
5386 isl_basic_map_free(bmap
);
5390 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5391 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5393 for (i
= 1; i
< dom
->n
; ++i
) {
5394 struct isl_map
*res_i
;
5395 struct isl_set
*empty_i
;
5397 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5398 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5400 res
= isl_map_union_disjoint(res
, res_i
);
5401 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5405 isl_basic_map_free(bmap
);
5410 isl_basic_map_free(bmap
);
5414 /* Given a map "map", compute the lexicographically minimal
5415 * (or maximal) image element for each domain element in dom.
5416 * Set *empty to those elements in dom that do not have an image element.
5418 * We first compute the lexicographically minimal or maximal element
5419 * in the first basic map. This results in a partial solution "res"
5420 * and a subset "todo" of dom that still need to be handled.
5421 * We then consider each of the remaining maps in "map" and successively
5422 * improve both "res" and "todo".
5424 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5425 * Assume we are computing the lexicographical maximum.
5426 * We first compute the lexicographically maximal element in basic map i.
5427 * This results in a partial solution res_i and a subset todo_i.
5428 * Then we combine these results with those obtain for the first k basic maps
5429 * to obtain a result that is valid for the first k+1 basic maps.
5430 * In particular, the set where there is no solution is the set where
5431 * there is no solution for the first k basic maps and also no solution
5432 * for the ith basic map, i.e.,
5434 * todo^i = todo^k * todo_i
5436 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5437 * solutions, arbitrarily breaking ties in favor of res^k.
5438 * That is, when res^k(a) >= res_i(a), we pick res^k and
5439 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5440 * the lexicographic order.)
5441 * In practice, we compute
5443 * res^k * (res_i . "<=")
5447 * res_i * (res^k . "<")
5449 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5450 * where only one of res^k and res_i provides a solution and we simply pick
5457 * Note that we only compute these intersections when dom(res^k) intersects
5458 * dom(res_i). Otherwise, the only effect of these intersections is to
5459 * potentially break up res^k and res_i into smaller pieces.
5460 * We want to avoid such splintering as much as possible.
5461 * In fact, an earlier implementation of this function would look for
5462 * better results in the domain of res^k and for extra results in todo^k,
5463 * but this would always result in a splintering according to todo^k,
5464 * even when the domain of basic map i is disjoint from the domains of
5465 * the previous basic maps.
5467 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5468 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5469 __isl_give isl_set
**empty
, int max
)
5472 struct isl_map
*res
;
5473 struct isl_set
*todo
;
5478 if (isl_map_plain_is_empty(map
)) {
5486 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5487 isl_set_copy(dom
), &todo
, max
);
5489 for (i
= 1; i
< map
->n
; ++i
) {
5493 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5495 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5496 isl_set_copy(dom
), &todo_i
, max
);
5499 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5500 le
= isl_map_lex_le(dim
);
5502 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5503 le
= isl_map_lex_ge(dim
);
5505 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5506 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5507 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5508 le
= isl_map_intersect(le
, isl_map_copy(res
));
5510 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5511 res
= isl_map_intersect_domain(res
,
5512 isl_set_copy(todo_i
));
5513 res_i
= isl_map_intersect_domain(res_i
,
5514 isl_set_copy(todo
));
5517 res
= isl_map_union_disjoint(res
, res_i
);
5518 res
= isl_map_union_disjoint(res
, lt
);
5519 res
= isl_map_union_disjoint(res
, le
);
5521 todo
= isl_set_intersect(todo
, todo_i
);
5541 /* Given a map "map", compute the lexicographically minimal
5542 * (or maximal) image element for each domain element in dom.
5543 * Set *empty to those elements in dom that do not have an image element.
5545 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5547 static __isl_give isl_map
*isl_map_partial_lexopt(
5548 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5549 __isl_give isl_set
**empty
, int max
)
5553 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5554 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5555 if (!isl_space_has_named_params(map
->dim
) ||
5556 !isl_space_has_named_params(dom
->dim
))
5557 isl_die(map
->ctx
, isl_error_invalid
,
5558 "unaligned unnamed parameters", goto error
);
5559 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5560 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5561 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5570 __isl_give isl_map
*isl_map_partial_lexmax(
5571 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5572 __isl_give isl_set
**empty
)
5574 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5577 __isl_give isl_map
*isl_map_partial_lexmin(
5578 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5579 __isl_give isl_set
**empty
)
5581 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5584 __isl_give isl_set
*isl_set_partial_lexmin(
5585 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5586 __isl_give isl_set
**empty
)
5588 return (struct isl_set
*)
5589 isl_map_partial_lexmin((struct isl_map
*)set
,
5593 __isl_give isl_set
*isl_set_partial_lexmax(
5594 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5595 __isl_give isl_set
**empty
)
5597 return (struct isl_set
*)
5598 isl_map_partial_lexmax((struct isl_map
*)set
,
5602 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5604 struct isl_basic_set
*dom
= NULL
;
5609 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5610 dom
= isl_basic_set_universe(dom_dim
);
5611 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5613 isl_basic_map_free(bmap
);
5617 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5619 return isl_basic_map_lexopt(bmap
, 0);
5622 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5624 return isl_basic_map_lexopt(bmap
, 1);
5627 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5629 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5632 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5634 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5637 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5639 struct isl_set
*dom
= NULL
;
5644 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5645 dom
= isl_set_universe(dom_dim
);
5646 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5652 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5654 return isl_map_lexopt(map
, 0);
5657 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5659 return isl_map_lexopt(map
, 1);
5662 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5664 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5667 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5669 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5672 /* Extract the first and only affine expression from list
5673 * and then add it to *pwaff with the given dom.
5674 * This domain is known to be disjoint from other domains
5675 * because of the way isl_basic_map_foreach_lexmax works.
5677 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5678 __isl_take isl_aff_list
*list
, void *user
)
5680 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5682 isl_pw_aff
**pwaff
= user
;
5683 isl_pw_aff
*pwaff_i
;
5685 if (isl_aff_list_n_aff(list
) != 1)
5686 isl_die(ctx
, isl_error_internal
,
5687 "expecting single element list", goto error
);
5689 aff
= isl_aff_list_get_aff(list
, 0);
5690 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5692 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5694 isl_aff_list_free(list
);
5698 isl_basic_set_free(dom
);
5699 isl_aff_list_free(list
);
5703 /* Given a basic map with one output dimension, compute the minimum or
5704 * maximum of that dimension as an isl_pw_aff.
5706 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5707 * call update_dim_opt on each leaf of the result.
5709 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5712 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5716 dim
= isl_space_from_domain(isl_space_domain(dim
));
5717 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5718 pwaff
= isl_pw_aff_empty(dim
);
5720 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5722 return isl_pw_aff_free(pwaff
);
5727 /* Compute the minimum or maximum of the given output dimension
5728 * as a function of the parameters and the input dimensions,
5729 * but independently of the other output dimensions.
5731 * We first project out the other output dimension and then compute
5732 * the "lexicographic" maximum in each basic map, combining the results
5733 * using isl_pw_aff_union_max.
5735 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5742 n_out
= isl_map_dim(map
, isl_dim_out
);
5743 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5744 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5749 isl_space
*dim
= isl_map_get_space(map
);
5750 dim
= isl_space_domain(isl_space_from_range(dim
));
5752 return isl_pw_aff_empty(dim
);
5755 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5756 for (i
= 1; i
< map
->n
; ++i
) {
5757 isl_pw_aff
*pwaff_i
;
5759 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5760 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5768 /* Compute the maximum of the given output dimension as a function of the
5769 * parameters and input dimensions, but independently of
5770 * the other output dimensions.
5772 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5774 return map_dim_opt(map
, pos
, 1);
5777 /* Compute the minimum or maximum of the given set dimension
5778 * as a function of the parameters,
5779 * but independently of the other set dimensions.
5781 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5784 return map_dim_opt(set
, pos
, max
);
5787 /* Compute the maximum of the given set dimension as a function of the
5788 * parameters, but independently of the other set dimensions.
5790 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5792 return set_dim_opt(set
, pos
, 1);
5795 /* Compute the minimum of the given set dimension as a function of the
5796 * parameters, but independently of the other set dimensions.
5798 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5800 return set_dim_opt(set
, pos
, 0);
5803 /* Apply a preimage specified by "mat" on the parameters of "bset".
5804 * bset is assumed to have only parameters and divs.
5806 static struct isl_basic_set
*basic_set_parameter_preimage(
5807 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5814 bset
->dim
= isl_space_cow(bset
->dim
);
5818 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5820 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5822 bset
->dim
->nparam
= 0;
5823 bset
->dim
->n_out
= nparam
;
5824 bset
= isl_basic_set_preimage(bset
, mat
);
5826 bset
->dim
->nparam
= bset
->dim
->n_out
;
5827 bset
->dim
->n_out
= 0;
5832 isl_basic_set_free(bset
);
5836 /* Apply a preimage specified by "mat" on the parameters of "set".
5837 * set is assumed to have only parameters and divs.
5839 static struct isl_set
*set_parameter_preimage(
5840 struct isl_set
*set
, struct isl_mat
*mat
)
5842 isl_space
*dim
= NULL
;
5848 dim
= isl_space_copy(set
->dim
);
5849 dim
= isl_space_cow(dim
);
5853 nparam
= isl_set_dim(set
, isl_dim_param
);
5855 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5858 dim
->n_out
= nparam
;
5859 isl_set_reset_space(set
, dim
);
5860 set
= isl_set_preimage(set
, mat
);
5863 dim
= isl_space_copy(set
->dim
);
5864 dim
= isl_space_cow(dim
);
5867 dim
->nparam
= dim
->n_out
;
5869 isl_set_reset_space(set
, dim
);
5872 isl_space_free(dim
);
5879 /* Intersect the basic set "bset" with the affine space specified by the
5880 * equalities in "eq".
5882 static struct isl_basic_set
*basic_set_append_equalities(
5883 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5891 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5896 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5897 for (i
= 0; i
< eq
->n_row
; ++i
) {
5898 k
= isl_basic_set_alloc_equality(bset
);
5901 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5902 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5906 bset
= isl_basic_set_gauss(bset
, NULL
);
5907 bset
= isl_basic_set_finalize(bset
);
5912 isl_basic_set_free(bset
);
5916 /* Intersect the set "set" with the affine space specified by the
5917 * equalities in "eq".
5919 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5927 for (i
= 0; i
< set
->n
; ++i
) {
5928 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5941 /* Project the given basic set onto its parameter domain, possibly introducing
5942 * new, explicit, existential variables in the constraints.
5943 * The input has parameters and (possibly implicit) existential variables.
5944 * The output has the same parameters, but only
5945 * explicit existentially quantified variables.
5947 * The actual projection is performed by pip, but pip doesn't seem
5948 * to like equalities very much, so we first remove the equalities
5949 * among the parameters by performing a variable compression on
5950 * the parameters. Afterward, an inverse transformation is performed
5951 * and the equalities among the parameters are inserted back in.
5953 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5957 struct isl_mat
*T
, *T2
;
5958 struct isl_set
*set
;
5959 unsigned nparam
, n_div
;
5961 bset
= isl_basic_set_cow(bset
);
5965 if (bset
->n_eq
== 0)
5966 return isl_basic_set_lexmin(bset
);
5968 isl_basic_set_gauss(bset
, NULL
);
5970 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5971 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5973 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5974 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5977 if (i
== bset
->n_eq
)
5978 return isl_basic_set_lexmin(bset
);
5980 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5982 eq
= isl_mat_cow(eq
);
5983 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5984 if (T
&& T
->n_col
== 0) {
5988 bset
= isl_basic_set_set_to_empty(bset
);
5989 return isl_set_from_basic_set(bset
);
5991 bset
= basic_set_parameter_preimage(bset
, T
);
5993 set
= isl_basic_set_lexmin(bset
);
5994 set
= set_parameter_preimage(set
, T2
);
5995 set
= set_append_equalities(set
, eq
);
5999 /* Compute an explicit representation for all the existentially
6000 * quantified variables.
6001 * The input and output dimensions are first turned into parameters.
6002 * compute_divs then returns a map with the same parameters and
6003 * no input or output dimensions and the dimension specification
6004 * is reset to that of the input.
6006 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6008 struct isl_basic_set
*bset
;
6009 struct isl_set
*set
;
6010 struct isl_map
*map
;
6011 isl_space
*dim
, *orig_dim
= NULL
;
6016 bmap
= isl_basic_map_cow(bmap
);
6020 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6021 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6022 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6023 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
6027 orig_dim
= bmap
->dim
;
6029 bset
= (struct isl_basic_set
*)bmap
;
6031 set
= parameter_compute_divs(bset
);
6032 map
= (struct isl_map
*)set
;
6033 map
= isl_map_reset_space(map
, orig_dim
);
6037 isl_basic_map_free(bmap
);
6041 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6049 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6050 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6051 if (isl_int_is_zero(bmap
->div
[i
][0]))
6053 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6059 static int map_divs_known(__isl_keep isl_map
*map
)
6066 for (i
= 0; i
< map
->n
; ++i
) {
6067 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6075 /* If bmap contains any unknown divs, then compute explicit
6076 * expressions for them. However, this computation may be
6077 * quite expensive, so first try to remove divs that aren't
6080 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6083 struct isl_map
*map
;
6085 known
= isl_basic_map_divs_known(bmap
);
6089 return isl_map_from_basic_map(bmap
);
6091 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6093 known
= isl_basic_map_divs_known(bmap
);
6097 return isl_map_from_basic_map(bmap
);
6099 map
= compute_divs(bmap
);
6102 isl_basic_map_free(bmap
);
6106 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6110 struct isl_map
*res
;
6117 known
= map_divs_known(map
);
6125 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6126 for (i
= 1 ; i
< map
->n
; ++i
) {
6128 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6129 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6130 res
= isl_map_union_disjoint(res
, r2
);
6132 res
= isl_map_union(res
, r2
);
6139 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6141 return (struct isl_set
*)
6142 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6145 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6147 return (struct isl_set
*)
6148 isl_map_compute_divs((struct isl_map
*)set
);
6151 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6154 struct isl_set
*set
;
6159 map
= isl_map_cow(map
);
6163 set
= (struct isl_set
*)map
;
6164 set
->dim
= isl_space_domain(set
->dim
);
6167 for (i
= 0; i
< map
->n
; ++i
) {
6168 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6172 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6173 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6180 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6181 __isl_take isl_map
*map2
)
6185 struct isl_map
*map
= NULL
;
6199 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6201 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6202 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6203 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6205 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6206 map1
->n
+ map2
->n
, flags
);
6209 for (i
= 0; i
< map1
->n
; ++i
) {
6210 map
= isl_map_add_basic_map(map
,
6211 isl_basic_map_copy(map1
->p
[i
]));
6215 for (i
= 0; i
< map2
->n
; ++i
) {
6216 map
= isl_map_add_basic_map(map
,
6217 isl_basic_map_copy(map2
->p
[i
]));
6231 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6232 __isl_take isl_map
*map2
)
6234 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6237 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6239 map1
= isl_map_union_disjoint(map1
, map2
);
6243 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6247 struct isl_set
*isl_set_union_disjoint(
6248 struct isl_set
*set1
, struct isl_set
*set2
)
6250 return (struct isl_set
*)
6251 isl_map_union_disjoint(
6252 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6255 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6257 return (struct isl_set
*)
6258 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6261 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6262 __isl_take isl_set
*set
)
6265 struct isl_map
*result
;
6271 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6272 isl_die(set
->ctx
, isl_error_invalid
,
6273 "parameters don't match", goto error
);
6275 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6276 !isl_map_compatible_range(map
, set
))
6277 isl_die(set
->ctx
, isl_error_invalid
,
6278 "incompatible spaces", goto error
);
6280 if (isl_set_plain_is_universe(set
)) {
6285 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6286 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6287 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6289 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6290 map
->n
* set
->n
, flags
);
6293 for (i
= 0; i
< map
->n
; ++i
)
6294 for (j
= 0; j
< set
->n
; ++j
) {
6295 result
= isl_map_add_basic_map(result
,
6296 isl_basic_map_intersect_range(
6297 isl_basic_map_copy(map
->p
[i
]),
6298 isl_basic_set_copy(set
->p
[j
])));
6311 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6312 __isl_take isl_set
*set
)
6314 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6317 struct isl_map
*isl_map_intersect_domain(
6318 struct isl_map
*map
, struct isl_set
*set
)
6320 return isl_map_reverse(
6321 isl_map_intersect_range(isl_map_reverse(map
), set
));
6324 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6325 __isl_take isl_map
*map2
)
6329 map1
= isl_map_reverse(map1
);
6330 map1
= isl_map_apply_range(map1
, map2
);
6331 return isl_map_reverse(map1
);
6338 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6339 __isl_take isl_map
*map2
)
6341 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6344 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6345 __isl_take isl_map
*map2
)
6347 isl_space
*dim_result
;
6348 struct isl_map
*result
;
6354 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6355 isl_space_copy(map2
->dim
));
6357 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6360 for (i
= 0; i
< map1
->n
; ++i
)
6361 for (j
= 0; j
< map2
->n
; ++j
) {
6362 result
= isl_map_add_basic_map(result
,
6363 isl_basic_map_apply_range(
6364 isl_basic_map_copy(map1
->p
[i
]),
6365 isl_basic_map_copy(map2
->p
[j
])));
6371 if (result
&& result
->n
<= 1)
6372 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6380 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6381 __isl_take isl_map
*map2
)
6383 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6387 * returns range - domain
6389 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6391 isl_space
*dims
, *target_dim
;
6392 struct isl_basic_set
*bset
;
6399 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6400 bmap
->dim
, isl_dim_out
),
6402 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6403 dim
= isl_basic_map_n_in(bmap
);
6404 nparam
= isl_basic_map_n_param(bmap
);
6405 bset
= isl_basic_set_from_basic_map(bmap
);
6406 bset
= isl_basic_set_cow(bset
);
6407 dims
= isl_basic_set_get_space(bset
);
6408 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6409 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6410 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6411 for (i
= 0; i
< dim
; ++i
) {
6412 int j
= isl_basic_map_alloc_equality(
6413 (struct isl_basic_map
*)bset
);
6416 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6417 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6418 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6419 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6421 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6422 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6425 isl_basic_map_free(bmap
);
6430 * returns range - domain
6432 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6436 struct isl_set
*result
;
6441 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6442 map
->dim
, isl_dim_out
),
6444 dim
= isl_map_get_space(map
);
6445 dim
= isl_space_domain(dim
);
6446 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6449 for (i
= 0; i
< map
->n
; ++i
)
6450 result
= isl_set_add_basic_set(result
,
6451 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6460 * returns [domain -> range] -> range - domain
6462 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6463 __isl_take isl_basic_map
*bmap
)
6467 isl_basic_map
*domain
;
6471 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6472 isl_die(bmap
->ctx
, isl_error_invalid
,
6473 "domain and range don't match", goto error
);
6475 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6476 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6478 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6479 domain
= isl_basic_map_universe(dim
);
6481 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6482 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6483 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6485 total
= isl_basic_map_total_dim(bmap
);
6487 for (i
= 0; i
< n
; ++i
) {
6488 k
= isl_basic_map_alloc_equality(bmap
);
6491 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6492 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6493 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6494 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6497 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6498 return isl_basic_map_finalize(bmap
);
6500 isl_basic_map_free(bmap
);
6505 * returns [domain -> range] -> range - domain
6507 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6510 isl_space
*domain_dim
;
6515 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6516 isl_die(map
->ctx
, isl_error_invalid
,
6517 "domain and range don't match", goto error
);
6519 map
= isl_map_cow(map
);
6523 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6524 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6525 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6528 for (i
= 0; i
< map
->n
; ++i
) {
6529 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6533 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6540 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6542 struct isl_basic_map
*bmap
;
6550 nparam
= dims
->nparam
;
6552 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6556 for (i
= 0; i
< dim
; ++i
) {
6557 int j
= isl_basic_map_alloc_equality(bmap
);
6560 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6561 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6562 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6564 return isl_basic_map_finalize(bmap
);
6566 isl_basic_map_free(bmap
);
6570 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6574 if (dim
->n_in
!= dim
->n_out
)
6575 isl_die(dim
->ctx
, isl_error_invalid
,
6576 "number of input and output dimensions needs to be "
6577 "the same", goto error
);
6578 return basic_map_identity(dim
);
6580 isl_space_free(dim
);
6584 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6586 if (!model
|| !model
->dim
)
6588 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6591 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6593 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6596 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6598 if (!model
|| !model
->dim
)
6600 return isl_map_identity(isl_space_copy(model
->dim
));
6603 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6605 if (!model
|| !model
->dim
)
6607 return isl_map_identity(isl_space_copy(model
->dim
));
6610 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6612 isl_space
*dim
= isl_set_get_space(set
);
6614 id
= isl_map_identity(isl_space_map_from_set(dim
));
6615 return isl_map_intersect_range(id
, set
);
6618 /* Construct a basic set with all set dimensions having only non-negative
6621 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6622 __isl_take isl_space
*space
)
6627 struct isl_basic_set
*bset
;
6631 nparam
= space
->nparam
;
6633 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6636 for (i
= 0; i
< dim
; ++i
) {
6637 int k
= isl_basic_set_alloc_inequality(bset
);
6640 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6641 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6645 isl_basic_set_free(bset
);
6649 /* Construct the half-space x_pos >= 0.
6651 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6655 isl_basic_set
*nonneg
;
6657 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6658 k
= isl_basic_set_alloc_inequality(nonneg
);
6661 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6662 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6664 return isl_basic_set_finalize(nonneg
);
6666 isl_basic_set_free(nonneg
);
6670 /* Construct the half-space x_pos <= -1.
6672 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6677 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6678 k
= isl_basic_set_alloc_inequality(neg
);
6681 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6682 isl_int_set_si(neg
->ineq
[k
][0], -1);
6683 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6685 return isl_basic_set_finalize(neg
);
6687 isl_basic_set_free(neg
);
6691 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6692 enum isl_dim_type type
, unsigned first
, unsigned n
)
6695 isl_basic_set
*nonneg
;
6703 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6705 for (i
= 0; i
< n
; ++i
) {
6706 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6707 pos(set
->dim
, type
) + first
+ i
);
6708 neg
= neg_halfspace(isl_set_get_space(set
),
6709 pos(set
->dim
, type
) + first
+ i
);
6711 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6720 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6721 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6728 if (isl_set_plain_is_empty(set
)) {
6733 return fn(set
, signs
, user
);
6736 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6738 half
= isl_set_intersect(half
, isl_set_copy(set
));
6739 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6743 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6745 half
= isl_set_intersect(half
, set
);
6746 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6752 /* Call "fn" on the intersections of "set" with each of the orthants
6753 * (except for obviously empty intersections). The orthant is identified
6754 * by the signs array, with each entry having value 1 or -1 according
6755 * to the sign of the corresponding variable.
6757 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6758 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6768 if (isl_set_plain_is_empty(set
))
6771 nparam
= isl_set_dim(set
, isl_dim_param
);
6772 nvar
= isl_set_dim(set
, isl_dim_set
);
6774 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6776 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6784 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6786 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6789 int isl_basic_map_is_subset(
6790 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6793 struct isl_map
*map1
;
6794 struct isl_map
*map2
;
6796 if (!bmap1
|| !bmap2
)
6799 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6800 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6802 is_subset
= isl_map_is_subset(map1
, map2
);
6810 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
6811 __isl_keep isl_basic_set
*bset2
)
6813 return isl_basic_map_is_subset(bset1
, bset2
);
6816 int isl_basic_map_is_equal(
6817 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6821 if (!bmap1
|| !bmap2
)
6823 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6826 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6830 int isl_basic_set_is_equal(
6831 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6833 return isl_basic_map_is_equal(
6834 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6837 int isl_map_is_empty(struct isl_map
*map
)
6844 for (i
= 0; i
< map
->n
; ++i
) {
6845 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6854 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6856 return map
? map
->n
== 0 : -1;
6859 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6861 return isl_map_plain_is_empty(map
);
6864 int isl_set_plain_is_empty(struct isl_set
*set
)
6866 return set
? set
->n
== 0 : -1;
6869 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6871 return isl_set_plain_is_empty(set
);
6874 int isl_set_is_empty(struct isl_set
*set
)
6876 return isl_map_is_empty((struct isl_map
*)set
);
6879 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6884 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6887 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6892 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6895 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6901 is_subset
= isl_map_is_subset(map1
, map2
);
6904 is_subset
= isl_map_is_subset(map2
, map1
);
6908 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6910 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6913 int isl_basic_map_is_strict_subset(
6914 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6918 if (!bmap1
|| !bmap2
)
6920 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6923 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6924 if (is_subset
== -1)
6929 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6935 is_subset
= isl_map_is_subset(map1
, map2
);
6938 is_subset
= isl_map_is_subset(map2
, map1
);
6939 if (is_subset
== -1)
6944 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6946 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6949 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6953 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6956 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6960 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6963 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6970 for (i
= 0; i
< map
->n
; ++i
) {
6971 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6979 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6981 return isl_map_plain_is_universe((isl_map
*) set
);
6984 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6986 return isl_set_plain_is_universe(set
);
6989 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6991 struct isl_basic_set
*bset
= NULL
;
6992 struct isl_vec
*sample
= NULL
;
6999 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7002 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7003 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7004 copy
= isl_basic_map_remove_redundancies(copy
);
7005 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
7006 isl_basic_map_free(copy
);
7010 total
= 1 + isl_basic_map_total_dim(bmap
);
7011 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7012 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7018 isl_vec_free(bmap
->sample
);
7019 bmap
->sample
= NULL
;
7020 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7023 sample
= isl_basic_set_sample_vec(bset
);
7026 empty
= sample
->size
== 0;
7027 isl_vec_free(bmap
->sample
);
7028 bmap
->sample
= sample
;
7030 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7035 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7039 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7042 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7044 return isl_basic_map_plain_is_empty(bmap
);
7047 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7051 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7054 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7056 return isl_basic_set_plain_is_empty(bset
);
7059 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7061 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7064 struct isl_map
*isl_basic_map_union(
7065 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7067 struct isl_map
*map
;
7068 if (!bmap1
|| !bmap2
)
7071 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7073 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7076 map
= isl_map_add_basic_map(map
, bmap1
);
7077 map
= isl_map_add_basic_map(map
, bmap2
);
7080 isl_basic_map_free(bmap1
);
7081 isl_basic_map_free(bmap2
);
7085 struct isl_set
*isl_basic_set_union(
7086 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7088 return (struct isl_set
*)isl_basic_map_union(
7089 (struct isl_basic_map
*)bset1
,
7090 (struct isl_basic_map
*)bset2
);
7093 /* Order divs such that any div only depends on previous divs */
7094 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7102 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7104 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7106 if (isl_int_is_zero(bmap
->div
[i
][0]))
7108 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7112 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7118 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7120 return (struct isl_basic_set
*)
7121 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7124 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7131 for (i
= 0; i
< map
->n
; ++i
) {
7132 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7143 /* Apply the expansion computed by isl_merge_divs.
7144 * The expansion itself is given by "exp" while the resulting
7145 * list of divs is given by "div".
7147 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7148 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7153 bset
= isl_basic_set_cow(bset
);
7157 if (div
->n_row
< bset
->n_div
)
7158 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7159 "not an expansion", goto error
);
7161 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7162 div
->n_row
- bset
->n_div
, 0,
7163 2 * (div
->n_row
- bset
->n_div
));
7165 n_div
= bset
->n_div
;
7166 for (i
= n_div
; i
< div
->n_row
; ++i
)
7167 if (isl_basic_set_alloc_div(bset
) < 0)
7171 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7172 if (j
>= 0 && exp
[j
] == i
) {
7174 isl_basic_map_swap_div(bset
, i
, j
);
7177 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7178 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7186 isl_basic_set_free(bset
);
7191 /* Look for a div in dst that corresponds to the div "div" in src.
7192 * The divs before "div" in src and dst are assumed to be the same.
7194 * Returns -1 if no corresponding div was found and the position
7195 * of the corresponding div in dst otherwise.
7197 static int find_div(struct isl_basic_map
*dst
,
7198 struct isl_basic_map
*src
, unsigned div
)
7202 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7204 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7205 for (i
= div
; i
< dst
->n_div
; ++i
)
7206 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7207 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7208 dst
->n_div
- div
) == -1)
7213 struct isl_basic_map
*isl_basic_map_align_divs(
7214 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7217 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7222 if (src
->n_div
== 0)
7225 for (i
= 0; i
< src
->n_div
; ++i
)
7226 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7228 src
= isl_basic_map_order_divs(src
);
7229 dst
= isl_basic_map_cow(dst
);
7230 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7231 src
->n_div
, 0, 2 * src
->n_div
);
7234 for (i
= 0; i
< src
->n_div
; ++i
) {
7235 int j
= find_div(dst
, src
, i
);
7237 j
= isl_basic_map_alloc_div(dst
);
7240 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7241 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7242 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7246 isl_basic_map_swap_div(dst
, i
, j
);
7250 isl_basic_map_free(dst
);
7254 struct isl_basic_set
*isl_basic_set_align_divs(
7255 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7257 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7258 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7261 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7269 map
= isl_map_compute_divs(map
);
7270 map
= isl_map_cow(map
);
7274 for (i
= 1; i
< map
->n
; ++i
)
7275 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7276 for (i
= 1; i
< map
->n
; ++i
)
7277 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7279 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7283 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7285 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7288 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7289 __isl_take isl_map
*map
)
7293 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7294 map
= isl_map_intersect_domain(map
, set
);
7295 set
= isl_map_range(map
);
7303 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7304 __isl_take isl_map
*map
)
7306 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7309 /* There is no need to cow as removing empty parts doesn't change
7310 * the meaning of the set.
7312 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7319 for (i
= map
->n
- 1; i
>= 0; --i
)
7320 remove_if_empty(map
, i
);
7325 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7327 return (struct isl_set
*)
7328 isl_map_remove_empty_parts((struct isl_map
*)set
);
7331 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7333 struct isl_basic_map
*bmap
;
7334 if (!map
|| map
->n
== 0)
7336 bmap
= map
->p
[map
->n
-1];
7337 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7338 return isl_basic_map_copy(bmap
);
7341 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7343 return (struct isl_basic_set
*)
7344 isl_map_copy_basic_map((struct isl_map
*)set
);
7347 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7348 __isl_keep isl_basic_map
*bmap
)
7354 for (i
= map
->n
-1; i
>= 0; --i
) {
7355 if (map
->p
[i
] != bmap
)
7357 map
= isl_map_cow(map
);
7360 isl_basic_map_free(map
->p
[i
]);
7361 if (i
!= map
->n
-1) {
7362 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7363 map
->p
[i
] = map
->p
[map
->n
-1];
7374 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7375 struct isl_basic_set
*bset
)
7377 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7378 (struct isl_basic_map
*)bset
);
7381 /* Given two basic sets bset1 and bset2, compute the maximal difference
7382 * between the values of dimension pos in bset1 and those in bset2
7383 * for any common value of the parameters and dimensions preceding pos.
7385 static enum isl_lp_result
basic_set_maximal_difference_at(
7386 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7387 int pos
, isl_int
*opt
)
7390 struct isl_basic_map
*bmap1
= NULL
;
7391 struct isl_basic_map
*bmap2
= NULL
;
7392 struct isl_ctx
*ctx
;
7393 struct isl_vec
*obj
;
7396 unsigned dim1
, dim2
;
7397 enum isl_lp_result res
;
7399 if (!bset1
|| !bset2
)
7400 return isl_lp_error
;
7402 nparam
= isl_basic_set_n_param(bset1
);
7403 dim1
= isl_basic_set_n_dim(bset1
);
7404 dim2
= isl_basic_set_n_dim(bset2
);
7405 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7406 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7407 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7408 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7409 if (!bmap1
|| !bmap2
)
7411 bmap1
= isl_basic_map_cow(bmap1
);
7412 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7413 pos
, (dim1
- pos
) + (dim2
- pos
),
7414 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7415 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7418 total
= isl_basic_map_total_dim(bmap1
);
7420 obj
= isl_vec_alloc(ctx
, 1 + total
);
7421 isl_seq_clr(obj
->block
.data
, 1 + total
);
7422 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7423 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7426 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7428 isl_basic_map_free(bmap1
);
7432 isl_basic_map_free(bmap1
);
7433 isl_basic_map_free(bmap2
);
7434 return isl_lp_error
;
7437 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7438 * for any common value of the parameters and dimensions preceding pos
7439 * in both basic sets, the values of dimension pos in bset1 are
7440 * smaller or larger than those in bset2.
7443 * 1 if bset1 follows bset2
7444 * -1 if bset1 precedes bset2
7445 * 0 if bset1 and bset2 are incomparable
7446 * -2 if some error occurred.
7448 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7449 struct isl_basic_set
*bset2
, int pos
)
7452 enum isl_lp_result res
;
7457 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7459 if (res
== isl_lp_empty
)
7461 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7462 res
== isl_lp_unbounded
)
7464 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7473 /* Given two basic sets bset1 and bset2, check whether
7474 * for any common value of the parameters and dimensions preceding pos
7475 * there is a value of dimension pos in bset1 that is larger
7476 * than a value of the same dimension in bset2.
7479 * 1 if there exists such a pair
7480 * 0 if there is no such pair, but there is a pair of equal values
7482 * -2 if some error occurred.
7484 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7485 __isl_keep isl_basic_set
*bset2
, int pos
)
7488 enum isl_lp_result res
;
7493 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7495 if (res
== isl_lp_empty
)
7497 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7498 res
== isl_lp_unbounded
)
7500 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7502 else if (res
== isl_lp_ok
)
7511 /* Given two sets set1 and set2, check whether
7512 * for any common value of the parameters and dimensions preceding pos
7513 * there is a value of dimension pos in set1 that is larger
7514 * than a value of the same dimension in set2.
7517 * 1 if there exists such a pair
7518 * 0 if there is no such pair, but there is a pair of equal values
7520 * -2 if some error occurred.
7522 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7523 __isl_keep isl_set
*set2
, int pos
)
7531 for (i
= 0; i
< set1
->n
; ++i
)
7532 for (j
= 0; j
< set2
->n
; ++j
) {
7534 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7535 if (f
== 1 || f
== -2)
7544 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7545 unsigned pos
, isl_int
*val
)
7553 total
= isl_basic_map_total_dim(bmap
);
7554 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7555 for (; d
+1 > pos
; --d
)
7556 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7560 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7562 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7564 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7567 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7573 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7574 unsigned pos
, isl_int
*val
)
7586 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7589 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7590 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7591 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7592 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7596 isl_int_set(*val
, v
);
7602 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7603 unsigned pos
, isl_int
*val
)
7605 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7609 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7612 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7615 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7616 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7618 if (pos
>= isl_basic_map_dim(bmap
, type
))
7620 return isl_basic_map_plain_has_fixed_var(bmap
,
7621 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7624 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7625 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7627 if (pos
>= isl_map_dim(map
, type
))
7629 return isl_map_plain_has_fixed_var(map
,
7630 map_offset(map
, type
) - 1 + pos
, val
);
7633 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7634 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7636 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7639 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7640 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7642 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7645 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7646 * then return this fixed value in *val.
7648 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7649 unsigned dim
, isl_int
*val
)
7651 return isl_basic_set_plain_has_fixed_var(bset
,
7652 isl_basic_set_n_param(bset
) + dim
, val
);
7655 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7656 * then return this fixed value in *val.
7658 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7659 unsigned dim
, isl_int
*val
)
7661 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7664 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7665 unsigned dim
, isl_int
*val
)
7667 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7670 /* Check if input variable in has fixed value and if so and if val is not NULL,
7671 * then return this fixed value in *val.
7673 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7674 unsigned in
, isl_int
*val
)
7676 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7679 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7680 * and if val is not NULL, then return this lower bound in *val.
7682 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7683 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7685 int i
, i_eq
= -1, i_ineq
= -1;
7692 total
= isl_basic_set_total_dim(bset
);
7693 nparam
= isl_basic_set_n_param(bset
);
7694 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7695 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7701 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7702 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7704 if (i_eq
!= -1 || i_ineq
!= -1)
7708 if (i_eq
== -1 && i_ineq
== -1)
7710 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7711 /* The coefficient should always be one due to normalization. */
7712 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7714 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7716 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7717 total
- nparam
- dim
- 1) != -1)
7720 isl_int_neg(*val
, c
[0]);
7724 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7725 unsigned dim
, isl_int
*val
)
7737 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7741 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7743 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7744 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7746 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7750 isl_int_set(*val
, v
);
7761 /* uset_gist depends on constraints without existentially quantified
7762 * variables sorting first.
7764 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7766 const struct constraint
*c1
= (const struct constraint
*)p1
;
7767 const struct constraint
*c2
= (const struct constraint
*)p2
;
7769 unsigned size
= isl_min(c1
->size
, c2
->size
);
7771 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7772 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7777 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7780 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7781 struct isl_basic_map
*bmap
)
7784 struct constraint
*c
;
7789 total
= isl_basic_map_total_dim(bmap
);
7790 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7793 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7795 c
[i
].c
= bmap
->ineq
[i
];
7797 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7798 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7799 bmap
->ineq
[i
] = c
[i
].c
;
7803 isl_basic_map_free(bmap
);
7807 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7808 __isl_take isl_basic_set
*bset
)
7810 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7811 (struct isl_basic_map
*)bset
);
7814 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7818 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7820 bmap
= isl_basic_map_remove_redundancies(bmap
);
7821 bmap
= isl_basic_map_sort_constraints(bmap
);
7822 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7826 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7828 return (struct isl_basic_set
*)isl_basic_map_normalize(
7829 (struct isl_basic_map
*)bset
);
7832 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7833 const __isl_keep isl_basic_map
*bmap2
)
7840 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7841 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7842 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7843 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7844 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7845 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7846 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7847 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7848 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7849 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7850 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7852 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7854 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7856 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7857 return bmap1
->n_eq
- bmap2
->n_eq
;
7858 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7859 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7860 if (bmap1
->n_div
!= bmap2
->n_div
)
7861 return bmap1
->n_div
- bmap2
->n_div
;
7862 total
= isl_basic_map_total_dim(bmap1
);
7863 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7864 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7868 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7869 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7873 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7874 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7881 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7882 const __isl_keep isl_basic_set
*bset2
)
7884 return isl_basic_map_plain_cmp(bset1
, bset2
);
7887 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7888 const __isl_keep isl_set
*set2
)
7894 if (set1
->n
!= set2
->n
)
7895 return set1
->n
- set2
->n
;
7897 for (i
= 0; i
< set1
->n
; ++i
) {
7898 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7906 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7907 __isl_keep isl_basic_map
*bmap2
)
7909 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7912 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7913 __isl_keep isl_basic_set
*bset2
)
7915 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7916 (isl_basic_map
*)bset2
);
7919 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7921 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7922 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7924 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7927 /* We normalize in place, but if anything goes wrong we need
7928 * to return NULL, so we need to make sure we don't change the
7929 * meaning of any possible other copies of map.
7931 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7934 struct isl_basic_map
*bmap
;
7938 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7940 for (i
= 0; i
< map
->n
; ++i
) {
7941 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7944 isl_basic_map_free(map
->p
[i
]);
7947 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7948 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7949 map
= isl_map_remove_empty_parts(map
);
7952 for (i
= map
->n
- 1; i
>= 1; --i
) {
7953 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7955 isl_basic_map_free(map
->p
[i
-1]);
7956 for (j
= i
; j
< map
->n
; ++j
)
7957 map
->p
[j
-1] = map
->p
[j
];
7967 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7969 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7972 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7982 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7985 map1
= isl_map_copy(map1
);
7986 map2
= isl_map_copy(map2
);
7987 map1
= isl_map_normalize(map1
);
7988 map2
= isl_map_normalize(map2
);
7991 equal
= map1
->n
== map2
->n
;
7992 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7993 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8006 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8008 return isl_map_plain_is_equal(map1
, map2
);
8011 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8013 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8014 (struct isl_map
*)set2
);
8017 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8019 return isl_set_plain_is_equal(set1
, set2
);
8022 /* Return an interval that ranges from min to max (inclusive)
8024 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8025 isl_int min
, isl_int max
)
8028 struct isl_basic_set
*bset
= NULL
;
8030 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8034 k
= isl_basic_set_alloc_inequality(bset
);
8037 isl_int_set_si(bset
->ineq
[k
][1], 1);
8038 isl_int_neg(bset
->ineq
[k
][0], min
);
8040 k
= isl_basic_set_alloc_inequality(bset
);
8043 isl_int_set_si(bset
->ineq
[k
][1], -1);
8044 isl_int_set(bset
->ineq
[k
][0], max
);
8048 isl_basic_set_free(bset
);
8052 /* Return the Cartesian product of the basic sets in list (in the given order).
8054 __isl_give isl_basic_set
*isl_basic_set_list_product(
8055 __isl_take
struct isl_basic_set_list
*list
)
8063 struct isl_basic_set
*product
= NULL
;
8067 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8068 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8069 nparam
= isl_basic_set_n_param(list
->p
[0]);
8070 dim
= isl_basic_set_n_dim(list
->p
[0]);
8071 extra
= list
->p
[0]->n_div
;
8072 n_eq
= list
->p
[0]->n_eq
;
8073 n_ineq
= list
->p
[0]->n_ineq
;
8074 for (i
= 1; i
< list
->n
; ++i
) {
8075 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8076 isl_assert(list
->ctx
,
8077 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8078 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8079 extra
+= list
->p
[i
]->n_div
;
8080 n_eq
+= list
->p
[i
]->n_eq
;
8081 n_ineq
+= list
->p
[i
]->n_ineq
;
8083 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
8088 for (i
= 0; i
< list
->n
; ++i
) {
8089 isl_basic_set_add_constraints(product
,
8090 isl_basic_set_copy(list
->p
[i
]), dim
);
8091 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8093 isl_basic_set_list_free(list
);
8096 isl_basic_set_free(product
);
8097 isl_basic_set_list_free(list
);
8101 struct isl_basic_map
*isl_basic_map_product(
8102 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8104 isl_space
*dim_result
= NULL
;
8105 struct isl_basic_map
*bmap
;
8106 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8107 struct isl_dim_map
*dim_map1
, *dim_map2
;
8109 if (!bmap1
|| !bmap2
)
8112 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8113 bmap2
->dim
, isl_dim_param
), goto error
);
8114 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8115 isl_space_copy(bmap2
->dim
));
8117 in1
= isl_basic_map_n_in(bmap1
);
8118 in2
= isl_basic_map_n_in(bmap2
);
8119 out1
= isl_basic_map_n_out(bmap1
);
8120 out2
= isl_basic_map_n_out(bmap2
);
8121 nparam
= isl_basic_map_n_param(bmap1
);
8123 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8124 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8125 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8126 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8127 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8128 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8129 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8130 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8131 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8132 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8133 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8135 bmap
= isl_basic_map_alloc_space(dim_result
,
8136 bmap1
->n_div
+ bmap2
->n_div
,
8137 bmap1
->n_eq
+ bmap2
->n_eq
,
8138 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8139 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8140 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8141 bmap
= isl_basic_map_simplify(bmap
);
8142 return isl_basic_map_finalize(bmap
);
8144 isl_basic_map_free(bmap1
);
8145 isl_basic_map_free(bmap2
);
8149 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8150 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8152 isl_basic_map
*prod
;
8154 prod
= isl_basic_map_product(bmap1
, bmap2
);
8155 prod
= isl_basic_map_flatten(prod
);
8159 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8160 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8162 return isl_basic_map_flat_range_product(bset1
, bset2
);
8165 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8166 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8168 isl_space
*space_result
= NULL
;
8169 isl_basic_map
*bmap
;
8170 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8171 struct isl_dim_map
*dim_map1
, *dim_map2
;
8173 if (!bmap1
|| !bmap2
)
8176 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8177 isl_space_copy(bmap2
->dim
));
8179 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8180 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8181 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8182 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8184 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8185 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8186 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8187 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8188 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8189 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8190 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8191 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8192 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8193 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8194 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8196 bmap
= isl_basic_map_alloc_space(space_result
,
8197 bmap1
->n_div
+ bmap2
->n_div
,
8198 bmap1
->n_eq
+ bmap2
->n_eq
,
8199 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8200 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8201 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8202 bmap
= isl_basic_map_simplify(bmap
);
8203 return isl_basic_map_finalize(bmap
);
8205 isl_basic_map_free(bmap1
);
8206 isl_basic_map_free(bmap2
);
8210 __isl_give isl_basic_map
*isl_basic_map_range_product(
8211 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8213 isl_space
*dim_result
= NULL
;
8214 isl_basic_map
*bmap
;
8215 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8216 struct isl_dim_map
*dim_map1
, *dim_map2
;
8218 if (!bmap1
|| !bmap2
)
8221 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8222 isl_space_copy(bmap2
->dim
));
8224 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8225 out1
= isl_basic_map_n_out(bmap1
);
8226 out2
= isl_basic_map_n_out(bmap2
);
8227 nparam
= isl_basic_map_n_param(bmap1
);
8229 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8230 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8231 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8232 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8233 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8234 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8235 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8236 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8237 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8238 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8239 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8241 bmap
= isl_basic_map_alloc_space(dim_result
,
8242 bmap1
->n_div
+ bmap2
->n_div
,
8243 bmap1
->n_eq
+ bmap2
->n_eq
,
8244 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8245 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8246 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8247 bmap
= isl_basic_map_simplify(bmap
);
8248 return isl_basic_map_finalize(bmap
);
8250 isl_basic_map_free(bmap1
);
8251 isl_basic_map_free(bmap2
);
8255 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8256 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8258 isl_basic_map
*prod
;
8260 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8261 prod
= isl_basic_map_flatten_range(prod
);
8265 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8266 __isl_take isl_map
*map2
,
8267 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8268 __isl_take isl_space
*right
),
8269 __isl_give isl_basic_map
*(*basic_map_product
)(
8270 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8273 struct isl_map
*result
;
8279 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8280 map2
->dim
, isl_dim_param
), goto error
);
8282 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8283 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8284 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8286 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8287 isl_space_copy(map2
->dim
)),
8288 map1
->n
* map2
->n
, flags
);
8291 for (i
= 0; i
< map1
->n
; ++i
)
8292 for (j
= 0; j
< map2
->n
; ++j
) {
8293 struct isl_basic_map
*part
;
8294 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8295 isl_basic_map_copy(map2
->p
[j
]));
8296 if (isl_basic_map_is_empty(part
))
8297 isl_basic_map_free(part
);
8299 result
= isl_map_add_basic_map(result
, part
);
8312 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8314 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8315 __isl_take isl_map
*map2
)
8317 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8320 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8321 __isl_take isl_map
*map2
)
8323 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8326 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8328 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8329 __isl_take isl_map
*map2
)
8333 prod
= isl_map_product(map1
, map2
);
8334 prod
= isl_map_flatten(prod
);
8338 /* Given two set A and B, construct its Cartesian product A x B.
8340 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8342 return isl_map_range_product(set1
, set2
);
8345 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8346 __isl_take isl_set
*set2
)
8348 return isl_map_flat_range_product(set1
, set2
);
8351 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8353 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8354 __isl_take isl_map
*map2
)
8356 return map_product(map1
, map2
, &isl_space_domain_product
,
8357 &isl_basic_map_domain_product
);
8360 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8362 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8363 __isl_take isl_map
*map2
)
8365 return map_product(map1
, map2
, &isl_space_range_product
,
8366 &isl_basic_map_range_product
);
8369 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8370 __isl_take isl_map
*map2
)
8372 return isl_map_align_params_map_map_and(map1
, map2
,
8373 &map_domain_product_aligned
);
8376 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8377 __isl_take isl_map
*map2
)
8379 return isl_map_align_params_map_map_and(map1
, map2
,
8380 &map_range_product_aligned
);
8383 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8385 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8386 __isl_take isl_map
*map2
)
8390 prod
= isl_map_domain_product(map1
, map2
);
8391 prod
= isl_map_flatten_domain(prod
);
8395 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8397 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8398 __isl_take isl_map
*map2
)
8402 prod
= isl_map_range_product(map1
, map2
);
8403 prod
= isl_map_flatten_range(prod
);
8407 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8410 uint32_t hash
= isl_hash_init();
8415 bmap
= isl_basic_map_copy(bmap
);
8416 bmap
= isl_basic_map_normalize(bmap
);
8419 total
= isl_basic_map_total_dim(bmap
);
8420 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8421 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8423 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8424 isl_hash_hash(hash
, c_hash
);
8426 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8427 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8429 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8430 isl_hash_hash(hash
, c_hash
);
8432 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8433 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8435 if (isl_int_is_zero(bmap
->div
[i
][0]))
8437 isl_hash_byte(hash
, i
& 0xFF);
8438 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8439 isl_hash_hash(hash
, c_hash
);
8441 isl_basic_map_free(bmap
);
8445 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8447 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8450 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8457 map
= isl_map_copy(map
);
8458 map
= isl_map_normalize(map
);
8462 hash
= isl_hash_init();
8463 for (i
= 0; i
< map
->n
; ++i
) {
8465 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8466 isl_hash_hash(hash
, bmap_hash
);
8474 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8476 return isl_map_get_hash((isl_map
*)set
);
8479 /* Check if the value for dimension dim is completely determined
8480 * by the values of the other parameters and variables.
8481 * That is, check if dimension dim is involved in an equality.
8483 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8490 nparam
= isl_basic_set_n_param(bset
);
8491 for (i
= 0; i
< bset
->n_eq
; ++i
)
8492 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8497 /* Check if the value for dimension dim is completely determined
8498 * by the values of the other parameters and variables.
8499 * That is, check if dimension dim is involved in an equality
8500 * for each of the subsets.
8502 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8508 for (i
= 0; i
< set
->n
; ++i
) {
8510 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8517 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8519 return set
? set
->n
: 0;
8522 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8523 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8530 for (i
= 0; i
< map
->n
; ++i
)
8531 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8537 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8538 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8545 for (i
= 0; i
< set
->n
; ++i
)
8546 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8552 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8559 bset
= isl_basic_set_cow(bset
);
8563 dim
= isl_basic_set_get_space(bset
);
8564 dim
= isl_space_lift(dim
, bset
->n_div
);
8567 isl_space_free(bset
->dim
);
8569 bset
->extra
-= bset
->n_div
;
8572 bset
= isl_basic_set_finalize(bset
);
8576 isl_basic_set_free(bset
);
8580 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8586 set
= isl_set_align_divs(set
);
8591 set
= isl_set_cow(set
);
8595 n_div
= set
->p
[0]->n_div
;
8596 dim
= isl_set_get_space(set
);
8597 dim
= isl_space_lift(dim
, n_div
);
8600 isl_space_free(set
->dim
);
8603 for (i
= 0; i
< set
->n
; ++i
) {
8604 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8615 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8618 struct isl_basic_map
*bmap
;
8625 set
= isl_set_align_divs(set
);
8630 dim
= isl_set_get_space(set
);
8631 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8633 return isl_map_identity(isl_space_map_from_set(dim
));
8636 n_div
= set
->p
[0]->n_div
;
8637 dim
= isl_space_map_from_set(dim
);
8638 n_param
= isl_space_dim(dim
, isl_dim_param
);
8639 n_set
= isl_space_dim(dim
, isl_dim_in
);
8640 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8641 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8642 for (i
= 0; i
< n_set
; ++i
)
8643 bmap
= var_equal(bmap
, i
);
8645 total
= n_param
+ n_set
+ n_set
+ n_div
;
8646 for (i
= 0; i
< n_div
; ++i
) {
8647 k
= isl_basic_map_alloc_inequality(bmap
);
8650 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8651 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8652 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8653 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8654 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8655 set
->p
[0]->div
[i
][0]);
8657 l
= isl_basic_map_alloc_inequality(bmap
);
8660 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8661 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8662 set
->p
[0]->div
[i
][0]);
8663 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8667 bmap
= isl_basic_map_simplify(bmap
);
8668 bmap
= isl_basic_map_finalize(bmap
);
8669 return isl_map_from_basic_map(bmap
);
8672 isl_basic_map_free(bmap
);
8676 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8684 dim
= isl_basic_set_total_dim(bset
);
8685 size
+= bset
->n_eq
* (1 + dim
);
8686 size
+= bset
->n_ineq
* (1 + dim
);
8687 size
+= bset
->n_div
* (2 + dim
);
8692 int isl_set_size(__isl_keep isl_set
*set
)
8700 for (i
= 0; i
< set
->n
; ++i
)
8701 size
+= isl_basic_set_size(set
->p
[i
]);
8706 /* Check if there is any lower bound (if lower == 0) and/or upper
8707 * bound (if upper == 0) on the specified dim.
8709 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8710 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8717 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8719 pos
+= isl_basic_map_offset(bmap
, type
);
8721 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8722 if (isl_int_is_zero(bmap
->div
[i
][0]))
8724 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8728 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8729 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8732 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8733 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8740 return lower
&& upper
;
8743 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8744 enum isl_dim_type type
, unsigned pos
)
8746 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8749 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8750 enum isl_dim_type type
, unsigned pos
)
8752 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8755 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8756 enum isl_dim_type type
, unsigned pos
)
8758 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8761 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8762 enum isl_dim_type type
, unsigned pos
)
8769 for (i
= 0; i
< map
->n
; ++i
) {
8771 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8772 if (bounded
< 0 || !bounded
)
8779 /* Return 1 if the specified dim is involved in both an upper bound
8780 * and a lower bound.
8782 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8783 enum isl_dim_type type
, unsigned pos
)
8785 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8788 static int has_bound(__isl_keep isl_map
*map
,
8789 enum isl_dim_type type
, unsigned pos
,
8790 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8791 enum isl_dim_type type
, unsigned pos
))
8798 for (i
= 0; i
< map
->n
; ++i
) {
8800 bounded
= fn(map
->p
[i
], type
, pos
);
8801 if (bounded
< 0 || bounded
)
8808 /* Return 1 if the specified dim is involved in any lower bound.
8810 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8811 enum isl_dim_type type
, unsigned pos
)
8813 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8816 /* Return 1 if the specified dim is involved in any upper bound.
8818 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8819 enum isl_dim_type type
, unsigned pos
)
8821 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8824 /* For each of the "n" variables starting at "first", determine
8825 * the sign of the variable and put the results in the first "n"
8826 * elements of the array "signs".
8828 * 1 means that the variable is non-negative
8829 * -1 means that the variable is non-positive
8830 * 0 means the variable attains both positive and negative values.
8832 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8833 unsigned first
, unsigned n
, int *signs
)
8835 isl_vec
*bound
= NULL
;
8836 struct isl_tab
*tab
= NULL
;
8837 struct isl_tab_undo
*snap
;
8840 if (!bset
|| !signs
)
8843 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8844 tab
= isl_tab_from_basic_set(bset
, 0);
8848 isl_seq_clr(bound
->el
, bound
->size
);
8849 isl_int_set_si(bound
->el
[0], -1);
8851 snap
= isl_tab_snap(tab
);
8852 for (i
= 0; i
< n
; ++i
) {
8855 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8856 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8859 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8860 if (isl_tab_rollback(tab
, snap
) < 0)
8868 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8869 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8872 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8873 if (isl_tab_rollback(tab
, snap
) < 0)
8876 signs
[i
] = empty
? -1 : 0;
8880 isl_vec_free(bound
);
8884 isl_vec_free(bound
);
8888 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8889 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8891 if (!bset
|| !signs
)
8893 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8896 first
+= pos(bset
->dim
, type
) - 1;
8897 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8900 /* Check if the given basic map is obviously single-valued.
8901 * In particular, for each output dimension, check that there is
8902 * an equality that defines the output dimension in terms of
8903 * earlier dimensions.
8905 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8915 total
= 1 + isl_basic_map_total_dim(bmap
);
8916 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8917 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8919 for (i
= 0; i
< n_out
; ++i
) {
8920 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8921 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8923 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8924 total
- (o_out
+ i
+ 1)) == -1)
8927 if (j
>= bmap
->n_eq
)
8934 /* Check if the given map is obviously single-valued.
8936 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8945 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8948 /* Check if the given map is single-valued.
8953 * and check if the result is a subset of the identity mapping.
8955 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8962 sv
= isl_map_plain_is_single_valued(map
);
8966 test
= isl_map_reverse(isl_map_copy(map
));
8967 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8969 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8970 id
= isl_map_identity(dim
);
8972 sv
= isl_map_is_subset(test
, id
);
8980 int isl_map_is_injective(__isl_keep isl_map
*map
)
8984 map
= isl_map_copy(map
);
8985 map
= isl_map_reverse(map
);
8986 in
= isl_map_is_single_valued(map
);
8992 /* Check if the given map is obviously injective.
8994 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8998 map
= isl_map_copy(map
);
8999 map
= isl_map_reverse(map
);
9000 in
= isl_map_plain_is_single_valued(map
);
9006 int isl_map_is_bijective(__isl_keep isl_map
*map
)
9010 sv
= isl_map_is_single_valued(map
);
9014 return isl_map_is_injective(map
);
9017 int isl_set_is_singleton(__isl_keep isl_set
*set
)
9019 return isl_map_is_single_valued((isl_map
*)set
);
9022 int isl_map_is_translation(__isl_keep isl_map
*map
)
9027 delta
= isl_map_deltas(isl_map_copy(map
));
9028 ok
= isl_set_is_singleton(delta
);
9029 isl_set_free(delta
);
9034 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
9036 if (isl_seq_first_non_zero(p
, pos
) != -1)
9038 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
9043 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
9052 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
9055 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
9056 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
9057 for (j
= 0; j
< nvar
; ++j
) {
9058 int lower
= 0, upper
= 0;
9059 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9060 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
9062 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
9068 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9069 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
9071 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
9073 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
9078 if (!lower
|| !upper
)
9085 int isl_set_is_box(__isl_keep isl_set
*set
)
9092 return isl_basic_set_is_box(set
->p
[0]);
9095 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9100 return isl_space_is_wrapping(bset
->dim
);
9103 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9108 return isl_space_is_wrapping(set
->dim
);
9111 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9113 bmap
= isl_basic_map_cow(bmap
);
9117 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9121 bmap
= isl_basic_map_finalize(bmap
);
9123 return (isl_basic_set
*)bmap
;
9125 isl_basic_map_free(bmap
);
9129 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9133 map
= isl_map_cow(map
);
9137 for (i
= 0; i
< map
->n
; ++i
) {
9138 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9142 map
->dim
= isl_space_wrap(map
->dim
);
9146 return (isl_set
*)map
;
9152 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9154 bset
= isl_basic_set_cow(bset
);
9158 bset
->dim
= isl_space_unwrap(bset
->dim
);
9162 bset
= isl_basic_set_finalize(bset
);
9164 return (isl_basic_map
*)bset
;
9166 isl_basic_set_free(bset
);
9170 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9177 if (!isl_set_is_wrapping(set
))
9178 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9181 set
= isl_set_cow(set
);
9185 for (i
= 0; i
< set
->n
; ++i
) {
9186 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9191 set
->dim
= isl_space_unwrap(set
->dim
);
9195 return (isl_map
*)set
;
9201 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9202 enum isl_dim_type type
)
9207 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9210 bmap
= isl_basic_map_cow(bmap
);
9214 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9218 bmap
= isl_basic_map_finalize(bmap
);
9222 isl_basic_map_free(bmap
);
9226 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9227 enum isl_dim_type type
)
9234 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9237 map
= isl_map_cow(map
);
9241 for (i
= 0; i
< map
->n
; ++i
) {
9242 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9246 map
->dim
= isl_space_reset(map
->dim
, type
);
9256 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9261 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9264 bmap
= isl_basic_map_cow(bmap
);
9268 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9272 bmap
= isl_basic_map_finalize(bmap
);
9276 isl_basic_map_free(bmap
);
9280 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9282 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9285 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9286 __isl_take isl_basic_map
*bmap
)
9291 if (!bmap
->dim
->nested
[0])
9294 bmap
= isl_basic_map_cow(bmap
);
9298 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9302 bmap
= isl_basic_map_finalize(bmap
);
9306 isl_basic_map_free(bmap
);
9310 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9311 __isl_take isl_basic_map
*bmap
)
9316 if (!bmap
->dim
->nested
[1])
9319 bmap
= isl_basic_map_cow(bmap
);
9323 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9327 bmap
= isl_basic_map_finalize(bmap
);
9331 isl_basic_map_free(bmap
);
9335 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9342 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9345 map
= isl_map_cow(map
);
9349 for (i
= 0; i
< map
->n
; ++i
) {
9350 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9354 map
->dim
= isl_space_flatten(map
->dim
);
9364 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9366 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9369 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9371 isl_space
*dim
, *flat_dim
;
9374 dim
= isl_set_get_space(set
);
9375 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9376 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9377 map
= isl_map_intersect_domain(map
, set
);
9382 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9389 if (!map
->dim
->nested
[0])
9392 map
= isl_map_cow(map
);
9396 for (i
= 0; i
< map
->n
; ++i
) {
9397 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9401 map
->dim
= isl_space_flatten_domain(map
->dim
);
9411 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9418 if (!map
->dim
->nested
[1])
9421 map
= isl_map_cow(map
);
9425 for (i
= 0; i
< map
->n
; ++i
) {
9426 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9430 map
->dim
= isl_space_flatten_range(map
->dim
);
9440 /* Reorder the dimensions of "bmap" according to the given dim_map
9441 * and set the dimension specification to "dim".
9443 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9444 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9449 bmap
= isl_basic_map_cow(bmap
);
9450 if (!bmap
|| !dim
|| !dim_map
)
9453 flags
= bmap
->flags
;
9454 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
9455 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
9456 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
9457 res
= isl_basic_map_alloc_space(dim
,
9458 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9459 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9462 res
= isl_basic_map_finalize(res
);
9466 isl_basic_map_free(bmap
);
9467 isl_space_free(dim
);
9471 /* Reorder the dimensions of "map" according to given reordering.
9473 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9474 __isl_take isl_reordering
*r
)
9477 struct isl_dim_map
*dim_map
;
9479 map
= isl_map_cow(map
);
9480 dim_map
= isl_dim_map_from_reordering(r
);
9481 if (!map
|| !r
|| !dim_map
)
9484 for (i
= 0; i
< map
->n
; ++i
) {
9485 struct isl_dim_map
*dim_map_i
;
9487 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9489 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9490 isl_space_copy(r
->dim
), dim_map_i
);
9496 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9498 isl_reordering_free(r
);
9504 isl_reordering_free(r
);
9508 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9509 __isl_take isl_reordering
*r
)
9511 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9514 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9515 __isl_take isl_space
*model
)
9522 ctx
= isl_space_get_ctx(model
);
9523 if (!isl_space_has_named_params(model
))
9524 isl_die(ctx
, isl_error_invalid
,
9525 "model has unnamed parameters", goto error
);
9526 if (!isl_space_has_named_params(map
->dim
))
9527 isl_die(ctx
, isl_error_invalid
,
9528 "relation has unnamed parameters", goto error
);
9529 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9530 isl_reordering
*exp
;
9532 model
= isl_space_drop_dims(model
, isl_dim_in
,
9533 0, isl_space_dim(model
, isl_dim_in
));
9534 model
= isl_space_drop_dims(model
, isl_dim_out
,
9535 0, isl_space_dim(model
, isl_dim_out
));
9536 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9537 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9538 map
= isl_map_realign(map
, exp
);
9541 isl_space_free(model
);
9544 isl_space_free(model
);
9549 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9550 __isl_take isl_space
*model
)
9552 return isl_map_align_params(set
, model
);
9555 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9556 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9557 enum isl_dim_type c2
, enum isl_dim_type c3
,
9558 enum isl_dim_type c4
, enum isl_dim_type c5
)
9560 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9561 struct isl_mat
*mat
;
9567 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9568 isl_basic_map_total_dim(bmap
) + 1);
9571 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9572 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9573 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9574 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9575 isl_int_set(mat
->row
[i
][pos
],
9576 bmap
->eq
[i
][off
+ k
]);
9584 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9585 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9586 enum isl_dim_type c2
, enum isl_dim_type c3
,
9587 enum isl_dim_type c4
, enum isl_dim_type c5
)
9589 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9590 struct isl_mat
*mat
;
9596 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9597 isl_basic_map_total_dim(bmap
) + 1);
9600 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9601 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9602 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9603 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9604 isl_int_set(mat
->row
[i
][pos
],
9605 bmap
->ineq
[i
][off
+ k
]);
9613 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9614 __isl_take isl_space
*dim
,
9615 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9616 enum isl_dim_type c2
, enum isl_dim_type c3
,
9617 enum isl_dim_type c4
, enum isl_dim_type c5
)
9619 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9620 isl_basic_map
*bmap
;
9626 if (!dim
|| !eq
|| !ineq
)
9629 if (eq
->n_col
!= ineq
->n_col
)
9630 isl_die(dim
->ctx
, isl_error_invalid
,
9631 "equalities and inequalities matrices should have "
9632 "same number of columns", goto error
);
9634 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9636 if (eq
->n_col
< total
)
9637 isl_die(dim
->ctx
, isl_error_invalid
,
9638 "number of columns too small", goto error
);
9640 extra
= eq
->n_col
- total
;
9642 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9643 eq
->n_row
, ineq
->n_row
);
9646 for (i
= 0; i
< extra
; ++i
) {
9647 k
= isl_basic_map_alloc_div(bmap
);
9650 isl_int_set_si(bmap
->div
[k
][0], 0);
9652 for (i
= 0; i
< eq
->n_row
; ++i
) {
9653 l
= isl_basic_map_alloc_equality(bmap
);
9656 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9657 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9658 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9659 isl_int_set(bmap
->eq
[l
][off
+ k
],
9665 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9666 l
= isl_basic_map_alloc_inequality(bmap
);
9669 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9670 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9671 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9672 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9679 isl_space_free(dim
);
9685 isl_space_free(dim
);
9691 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9692 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9693 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9695 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9696 c1
, c2
, c3
, c4
, isl_dim_in
);
9699 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9700 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9701 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9703 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9704 c1
, c2
, c3
, c4
, isl_dim_in
);
9707 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9708 __isl_take isl_space
*dim
,
9709 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9710 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9712 return (isl_basic_set
*)
9713 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9714 c1
, c2
, c3
, c4
, isl_dim_in
);
9717 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9722 return isl_space_can_zip(bmap
->dim
);
9725 int isl_map_can_zip(__isl_keep isl_map
*map
)
9730 return isl_space_can_zip(map
->dim
);
9733 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9734 * (A -> C) -> (B -> D).
9736 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9745 if (!isl_basic_map_can_zip(bmap
))
9746 isl_die(bmap
->ctx
, isl_error_invalid
,
9747 "basic map cannot be zipped", goto error
);
9748 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9749 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9750 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9751 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9752 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9755 bmap
->dim
= isl_space_zip(bmap
->dim
);
9760 isl_basic_map_free(bmap
);
9764 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9765 * (A -> C) -> (B -> D).
9767 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9774 if (!isl_map_can_zip(map
))
9775 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9778 map
= isl_map_cow(map
);
9782 for (i
= 0; i
< map
->n
; ++i
) {
9783 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9788 map
->dim
= isl_space_zip(map
->dim
);
9798 /* Construct a basic map mapping the domain of the affine expression
9799 * to a one-dimensional range prescribed by the affine expression.
9801 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9805 isl_local_space
*ls
;
9806 isl_basic_map
*bmap
;
9811 ls
= isl_aff_get_local_space(aff
);
9812 bmap
= isl_basic_map_from_local_space(ls
);
9813 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9814 k
= isl_basic_map_alloc_equality(bmap
);
9818 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9819 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9820 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9821 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9822 aff
->v
->size
- (pos
+ 1));
9825 bmap
= isl_basic_map_finalize(bmap
);
9829 isl_basic_map_free(bmap
);
9833 /* Construct a basic map mapping the domain the multi-affine expression
9834 * to its range, with each dimension in the range equated to the
9835 * corresponding affine expression.
9837 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9838 __isl_take isl_multi_aff
*maff
)
9842 isl_basic_map
*bmap
;
9847 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9848 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9849 "invalid space", return isl_multi_aff_free(maff
));
9851 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9852 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9854 for (i
= 0; i
< maff
->n
; ++i
) {
9856 isl_basic_map
*bmap_i
;
9858 aff
= isl_aff_copy(maff
->p
[i
]);
9859 bmap_i
= isl_basic_map_from_aff(aff
);
9861 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9864 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9866 isl_multi_aff_free(maff
);
9870 /* Construct a basic map mapping a domain in the given space to
9871 * to an n-dimensional range, with n the number of elements in the list,
9872 * where each coordinate in the range is prescribed by the
9873 * corresponding affine expression.
9874 * The domains of all affine expressions in the list are assumed to match
9877 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9878 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9882 isl_basic_map
*bmap
;
9887 dim
= isl_space_from_domain(domain_dim
);
9888 bmap
= isl_basic_map_universe(dim
);
9890 for (i
= 0; i
< list
->n
; ++i
) {
9892 isl_basic_map
*bmap_i
;
9894 aff
= isl_aff_copy(list
->p
[i
]);
9895 bmap_i
= isl_basic_map_from_aff(aff
);
9897 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9900 isl_aff_list_free(list
);
9904 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9905 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9907 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9910 /* Add a constraint imposing that the given two dimensions are equal.
9912 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9913 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9915 isl_basic_map
*bmap
= NULL
;
9921 if (pos1
>= isl_map_dim(map
, type1
))
9922 isl_die(map
->ctx
, isl_error_invalid
,
9923 "index out of bounds", goto error
);
9924 if (pos2
>= isl_map_dim(map
, type2
))
9925 isl_die(map
->ctx
, isl_error_invalid
,
9926 "index out of bounds", goto error
);
9928 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9929 i
= isl_basic_map_alloc_equality(bmap
);
9932 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9933 pos1
+= isl_basic_map_offset(bmap
, type1
);
9934 pos2
+= isl_basic_map_offset(bmap
, type2
);
9935 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9936 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9937 bmap
= isl_basic_map_finalize(bmap
);
9939 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9943 isl_basic_map_free(bmap
);
9948 /* Add a constraint imposing that the given two dimensions have opposite values.
9950 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9951 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9953 isl_basic_map
*bmap
= NULL
;
9959 if (pos1
>= isl_map_dim(map
, type1
))
9960 isl_die(map
->ctx
, isl_error_invalid
,
9961 "index out of bounds", goto error
);
9962 if (pos2
>= isl_map_dim(map
, type2
))
9963 isl_die(map
->ctx
, isl_error_invalid
,
9964 "index out of bounds", goto error
);
9966 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9967 i
= isl_basic_map_alloc_equality(bmap
);
9970 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9971 pos1
+= isl_basic_map_offset(bmap
, type1
);
9972 pos2
+= isl_basic_map_offset(bmap
, type2
);
9973 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9974 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9975 bmap
= isl_basic_map_finalize(bmap
);
9977 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9981 isl_basic_map_free(bmap
);
9986 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9990 isl_local_space
*ls
;
9995 if (!isl_basic_map_divs_known(bmap
))
9996 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9997 "some divs are unknown", return NULL
);
9999 ls
= isl_basic_map_get_local_space(bmap
);
10000 div
= isl_local_space_get_div(ls
, pos
);
10001 isl_local_space_free(ls
);
10006 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
10009 return isl_basic_map_get_div(bset
, pos
);
10012 /* Plug in "subs" for dimension "type", "pos" of "bset".
10014 * Let i be the dimension to replace and let "subs" be of the form
10018 * Any integer division with a non-zero coefficient for i,
10020 * floor((a i + g)/m)
10024 * floor((a f + d g)/(m d))
10026 * Constraints of the form
10034 __isl_give isl_basic_set
*isl_basic_set_substitute(
10035 __isl_take isl_basic_set
*bset
,
10036 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10042 if (bset
&& isl_basic_set_plain_is_empty(bset
))
10045 bset
= isl_basic_set_cow(bset
);
10046 if (!bset
|| !subs
)
10049 ctx
= isl_basic_set_get_ctx(bset
);
10050 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
10051 isl_die(ctx
, isl_error_invalid
,
10052 "spaces don't match", goto error
);
10053 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
10054 isl_die(ctx
, isl_error_unsupported
,
10055 "cannot handle divs yet", goto error
);
10057 pos
+= isl_basic_set_offset(bset
, type
);
10061 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10062 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
10064 isl_int_set(v
, bset
->eq
[i
][pos
]);
10065 isl_int_set_si(bset
->eq
[i
][pos
], 0);
10066 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
10067 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10070 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10071 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
10073 isl_int_set(v
, bset
->ineq
[i
][pos
]);
10074 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
10075 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
10076 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10079 for (i
= 0; i
< bset
->n_div
; ++i
) {
10080 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
10082 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
10083 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
10084 isl_seq_combine(bset
->div
[i
] + 1,
10085 subs
->v
->el
[0], bset
->div
[i
] + 1,
10086 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10087 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
10092 bset
= isl_basic_set_simplify(bset
);
10093 return isl_basic_set_finalize(bset
);
10095 isl_basic_set_free(bset
);
10099 /* Plug in "subs" for dimension "type", "pos" of "set".
10101 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
10102 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10106 if (set
&& isl_set_plain_is_empty(set
))
10109 set
= isl_set_cow(set
);
10113 for (i
= set
->n
- 1; i
>= 0; --i
) {
10114 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
10115 if (remove_if_empty(set
, i
) < 0)