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
)
127 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
130 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
132 return isl_set_dim(set
, isl_dim_set
);
135 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
137 return isl_set_dim(set
, isl_dim_param
);
140 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
142 return bmap
? bmap
->dim
->n_in
: 0;
145 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
147 return bmap
? bmap
->dim
->n_out
: 0;
150 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
152 return bmap
? bmap
->dim
->nparam
: 0;
155 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
157 return bmap
? bmap
->n_div
: 0;
160 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
162 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
165 unsigned isl_map_n_in(const struct isl_map
*map
)
167 return map
? map
->dim
->n_in
: 0;
170 unsigned isl_map_n_out(const struct isl_map
*map
)
172 return map
? map
->dim
->n_out
: 0;
175 unsigned isl_map_n_param(const struct isl_map
*map
)
177 return map
? map
->dim
->nparam
: 0;
180 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
185 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
188 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
191 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
192 struct isl_basic_set
*bset
)
197 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
200 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
203 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
208 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
211 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
214 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
215 struct isl_basic_set
*bset
)
220 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
223 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
226 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
228 return bmap
? bmap
->ctx
: NULL
;
231 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
233 return bset
? bset
->ctx
: NULL
;
236 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
238 return map
? map
->ctx
: NULL
;
241 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
243 return set
? set
->ctx
: NULL
;
246 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
250 return isl_space_copy(bmap
->dim
);
253 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
257 return isl_space_copy(bset
->dim
);
260 /* Extract the divs in "bmap" as a matrix.
262 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
273 ctx
= isl_basic_map_get_ctx(bmap
);
274 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
275 cols
= 1 + 1 + total
+ bmap
->n_div
;
276 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
280 for (i
= 0; i
< bmap
->n_div
; ++i
)
281 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
286 __isl_give isl_local_space
*isl_basic_map_get_local_space(
287 __isl_keep isl_basic_map
*bmap
)
294 div
= isl_basic_map_get_divs(bmap
);
295 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
298 __isl_give isl_local_space
*isl_basic_set_get_local_space(
299 __isl_keep isl_basic_set
*bset
)
301 return isl_basic_map_get_local_space(bset
);
304 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
305 __isl_take isl_local_space
*ls
)
314 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
315 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
316 n_div
, 0, 2 * n_div
);
318 for (i
= 0; i
< n_div
; ++i
)
319 if (isl_basic_map_alloc_div(bmap
) < 0)
322 for (i
= 0; i
< n_div
; ++i
) {
323 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
324 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
328 isl_local_space_free(ls
);
331 isl_local_space_free(ls
);
332 isl_basic_map_free(bmap
);
336 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
337 __isl_take isl_local_space
*ls
)
339 return isl_basic_map_from_local_space(ls
);
342 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
346 return isl_space_copy(map
->dim
);
349 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
353 return isl_space_copy(set
->dim
);
356 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
357 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
359 bmap
= isl_basic_map_cow(bmap
);
362 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
365 bmap
= isl_basic_map_finalize(bmap
);
368 isl_basic_map_free(bmap
);
372 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
373 __isl_take isl_basic_set
*bset
, const char *s
)
375 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
378 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
379 enum isl_dim_type type
)
381 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
384 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
385 enum isl_dim_type type
, const char *s
)
389 map
= isl_map_cow(map
);
393 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
397 for (i
= 0; i
< map
->n
; ++i
) {
398 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
409 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
410 enum isl_dim_type type
)
412 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
415 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
418 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
421 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
422 enum isl_dim_type type
, __isl_take isl_id
*id
)
424 map
= isl_map_cow(map
);
426 return isl_id_free(id
);
428 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
430 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
433 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
434 __isl_take isl_id
*id
)
436 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
439 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
440 enum isl_dim_type type
)
442 map
= isl_map_cow(map
);
446 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
448 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
451 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
453 return isl_map_reset_tuple_id(set
, isl_dim_set
);
456 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
458 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
461 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
462 enum isl_dim_type type
)
464 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
467 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
469 return isl_map_has_tuple_id(set
, isl_dim_set
);
472 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
474 return isl_map_get_tuple_id(set
, isl_dim_set
);
477 /* Does the set tuple have a name?
479 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
481 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
485 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
487 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
490 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
492 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
495 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
496 enum isl_dim_type type
, unsigned pos
)
498 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
501 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
502 enum isl_dim_type type
, unsigned pos
)
504 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
507 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
508 enum isl_dim_type type
, unsigned pos
)
510 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
513 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
514 enum isl_dim_type type
, unsigned pos
)
516 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
519 /* Does the given dimension have a name?
521 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
522 enum isl_dim_type type
, unsigned pos
)
524 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
527 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
528 __isl_take isl_basic_map
*bmap
,
529 enum isl_dim_type type
, unsigned pos
, const char *s
)
531 bmap
= isl_basic_map_cow(bmap
);
534 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
537 return isl_basic_map_finalize(bmap
);
539 isl_basic_map_free(bmap
);
543 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
544 enum isl_dim_type type
, unsigned pos
, const char *s
)
548 map
= isl_map_cow(map
);
552 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
556 for (i
= 0; i
< map
->n
; ++i
) {
557 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
568 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
569 __isl_take isl_basic_set
*bset
,
570 enum isl_dim_type type
, unsigned pos
, const char *s
)
572 return (isl_basic_set
*)isl_basic_map_set_dim_name(
573 (isl_basic_map
*)bset
, type
, pos
, s
);
576 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
577 enum isl_dim_type type
, unsigned pos
, const char *s
)
579 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
582 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
583 enum isl_dim_type type
, unsigned pos
)
585 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
588 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
589 enum isl_dim_type type
, unsigned pos
)
591 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
594 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
595 enum isl_dim_type type
, unsigned pos
)
597 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
600 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
601 enum isl_dim_type type
, unsigned pos
)
603 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
606 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
607 enum isl_dim_type type
, unsigned pos
)
609 return isl_map_has_dim_id(set
, type
, pos
);
612 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
613 enum isl_dim_type type
, unsigned pos
)
615 return isl_map_get_dim_id(set
, type
, pos
);
618 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
619 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
621 map
= isl_map_cow(map
);
623 return isl_id_free(id
);
625 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
627 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
630 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
631 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
633 return isl_map_set_dim_id(set
, type
, pos
, id
);
636 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
637 __isl_keep isl_id
*id
)
641 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
644 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
645 __isl_keep isl_id
*id
)
647 return isl_map_find_dim_by_id(set
, type
, id
);
650 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
655 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
658 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
661 return isl_map_find_dim_by_name(set
, type
, name
);
664 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
668 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
671 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
673 return isl_basic_map_is_rational(bset
);
676 /* Is this basic set a parameter domain?
678 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
682 return isl_space_is_params(bset
->dim
);
685 /* Is this set a parameter domain?
687 int isl_set_is_params(__isl_keep isl_set
*set
)
691 return isl_space_is_params(set
->dim
);
694 /* Is this map actually a parameter domain?
695 * Users should never call this function. Outside of isl,
696 * a map can never be a parameter domain.
698 int isl_map_is_params(__isl_keep isl_map
*map
)
702 return isl_space_is_params(map
->dim
);
705 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
706 struct isl_basic_map
*bmap
, unsigned extra
,
707 unsigned n_eq
, unsigned n_ineq
)
710 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
715 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
716 if (isl_blk_is_error(bmap
->block
))
719 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
724 bmap
->block2
= isl_blk_empty();
727 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
728 if (isl_blk_is_error(bmap
->block2
))
731 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
736 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
737 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
739 for (i
= 0; i
< extra
; ++i
)
740 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
744 bmap
->c_size
= n_eq
+ n_ineq
;
745 bmap
->eq
= bmap
->ineq
+ n_ineq
;
754 isl_basic_map_free(bmap
);
758 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
759 unsigned nparam
, unsigned dim
, unsigned extra
,
760 unsigned n_eq
, unsigned n_ineq
)
762 struct isl_basic_map
*bmap
;
765 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
769 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
770 return (struct isl_basic_set
*)bmap
;
773 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
774 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
776 struct isl_basic_map
*bmap
;
779 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
780 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
781 return (struct isl_basic_set
*)bmap
;
787 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
788 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
790 struct isl_basic_map
*bmap
;
794 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
799 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
805 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
806 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
807 unsigned n_eq
, unsigned n_ineq
)
809 struct isl_basic_map
*bmap
;
812 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
816 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
820 static void dup_constraints(
821 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
824 unsigned total
= isl_basic_map_total_dim(src
);
826 for (i
= 0; i
< src
->n_eq
; ++i
) {
827 int j
= isl_basic_map_alloc_equality(dst
);
828 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
831 for (i
= 0; i
< src
->n_ineq
; ++i
) {
832 int j
= isl_basic_map_alloc_inequality(dst
);
833 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
836 for (i
= 0; i
< src
->n_div
; ++i
) {
837 int j
= isl_basic_map_alloc_div(dst
);
838 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
840 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
843 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
845 struct isl_basic_map
*dup
;
849 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
850 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
853 dup_constraints(dup
, bmap
);
854 dup
->flags
= bmap
->flags
;
855 dup
->sample
= isl_vec_copy(bmap
->sample
);
859 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
861 struct isl_basic_map
*dup
;
863 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
864 return (struct isl_basic_set
*)dup
;
867 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
872 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
876 return isl_basic_set_dup(bset
);
879 struct isl_set
*isl_set_copy(struct isl_set
*set
)
888 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
893 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
897 bmap
= isl_basic_map_dup(bmap
);
899 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
903 struct isl_map
*isl_map_copy(struct isl_map
*map
)
912 void isl_basic_map_free(struct isl_basic_map
*bmap
)
920 isl_ctx_deref(bmap
->ctx
);
922 isl_blk_free(bmap
->ctx
, bmap
->block2
);
924 isl_blk_free(bmap
->ctx
, bmap
->block
);
925 isl_vec_free(bmap
->sample
);
926 isl_space_free(bmap
->dim
);
930 void isl_basic_set_free(struct isl_basic_set
*bset
)
932 isl_basic_map_free((struct isl_basic_map
*)bset
);
935 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
937 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
940 __isl_give isl_map
*isl_map_align_params_map_map_and(
941 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
942 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
943 __isl_take isl_map
*map2
))
947 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
948 return fn(map1
, map2
);
949 if (!isl_space_has_named_params(map1
->dim
) ||
950 !isl_space_has_named_params(map2
->dim
))
951 isl_die(map1
->ctx
, isl_error_invalid
,
952 "unaligned unnamed parameters", goto error
);
953 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
954 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
955 return fn(map1
, map2
);
962 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
963 __isl_keep isl_map
*map2
,
964 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
970 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
971 return fn(map1
, map2
);
972 if (!isl_space_has_named_params(map1
->dim
) ||
973 !isl_space_has_named_params(map2
->dim
))
974 isl_die(map1
->ctx
, isl_error_invalid
,
975 "unaligned unnamed parameters", return -1);
976 map1
= isl_map_copy(map1
);
977 map2
= isl_map_copy(map2
);
978 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
979 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
986 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
992 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
993 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
995 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
996 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
997 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
998 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
999 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1000 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1002 int j
= isl_basic_map_alloc_inequality(bmap
);
1006 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1007 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1014 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1015 bmap
->extra
- bmap
->n_div
);
1016 return bmap
->n_eq
++;
1019 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1021 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1024 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1028 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1033 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1035 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1038 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1043 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1045 if (pos
!= bmap
->n_eq
- 1) {
1047 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1048 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1054 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1056 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1059 void isl_basic_map_inequality_to_equality(
1060 struct isl_basic_map
*bmap
, unsigned pos
)
1064 t
= bmap
->ineq
[pos
];
1065 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1066 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1071 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1072 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1073 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1074 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1077 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1079 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1082 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1084 struct isl_ctx
*ctx
;
1088 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1089 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1090 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1091 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1092 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1093 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1094 1 + isl_basic_map_total_dim(bmap
),
1095 bmap
->extra
- bmap
->n_div
);
1096 return bmap
->n_ineq
++;
1099 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1101 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1104 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1108 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1113 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1115 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1118 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1123 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1125 if (pos
!= bmap
->n_ineq
- 1) {
1126 t
= bmap
->ineq
[pos
];
1127 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1128 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1129 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1135 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1137 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1140 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1145 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1148 k
= isl_basic_map_alloc_equality(bmap
);
1151 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1154 isl_basic_map_free(bmap
);
1158 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1161 return (isl_basic_set
*)
1162 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1165 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1170 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1173 k
= isl_basic_map_alloc_inequality(bmap
);
1176 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1179 isl_basic_map_free(bmap
);
1183 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1186 return (isl_basic_set
*)
1187 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1190 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1194 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1195 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1196 1 + 1 + isl_basic_map_total_dim(bmap
),
1197 bmap
->extra
- bmap
->n_div
);
1198 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1199 return bmap
->n_div
++;
1202 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1204 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1207 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1211 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1216 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1218 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1221 /* Copy constraint from src to dst, putting the vars of src at offset
1222 * dim_off in dst and the divs of src at offset div_off in dst.
1223 * If both sets are actually map, then dim_off applies to the input
1226 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1227 struct isl_basic_map
*src_map
, isl_int
*src
,
1228 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1230 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1231 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1232 unsigned src_in
= isl_basic_map_n_in(src_map
);
1233 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1234 unsigned src_out
= isl_basic_map_n_out(src_map
);
1235 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1236 isl_int_set(dst
[0], src
[0]);
1237 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1238 if (dst_nparam
> src_nparam
)
1239 isl_seq_clr(dst
+1+src_nparam
,
1240 dst_nparam
- src_nparam
);
1241 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1242 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1244 isl_min(dst_in
-in_off
, src_in
));
1245 if (dst_in
-in_off
> src_in
)
1246 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1247 dst_in
- in_off
- src_in
);
1248 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1249 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1250 src
+1+src_nparam
+src_in
,
1251 isl_min(dst_out
-out_off
, src_out
));
1252 if (dst_out
-out_off
> src_out
)
1253 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1254 dst_out
- out_off
- src_out
);
1255 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1256 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1257 src
+1+src_nparam
+src_in
+src_out
,
1258 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1259 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1260 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1261 div_off
+src_map
->n_div
,
1262 dst_map
->n_div
- div_off
- src_map
->n_div
);
1265 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1266 struct isl_basic_map
*src_map
, isl_int
*src
,
1267 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1269 isl_int_set(dst
[0], src
[0]);
1270 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1273 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1274 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1279 if (!bmap1
|| !bmap2
)
1282 div_off
= bmap1
->n_div
;
1284 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1285 int i1
= isl_basic_map_alloc_equality(bmap1
);
1288 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1289 i_pos
, o_pos
, div_off
);
1292 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1293 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1296 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1297 i_pos
, o_pos
, div_off
);
1300 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1301 int i1
= isl_basic_map_alloc_div(bmap1
);
1304 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1305 i_pos
, o_pos
, div_off
);
1308 isl_basic_map_free(bmap2
);
1313 isl_basic_map_free(bmap1
);
1314 isl_basic_map_free(bmap2
);
1318 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1319 struct isl_basic_set
*bset2
, unsigned pos
)
1321 return (struct isl_basic_set
*)
1322 add_constraints((struct isl_basic_map
*)bset1
,
1323 (struct isl_basic_map
*)bset2
, 0, pos
);
1326 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1327 __isl_take isl_space
*dim
, unsigned extra
,
1328 unsigned n_eq
, unsigned n_ineq
)
1330 struct isl_basic_map
*ext
;
1340 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1341 base
->extra
>= base
->n_div
+ extra
;
1343 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1344 room_for_ineq(base
, n_ineq
)) {
1345 isl_space_free(dim
);
1349 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1350 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1351 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1352 extra
+= base
->extra
;
1354 n_ineq
+= base
->n_ineq
;
1356 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1362 ext
->sample
= isl_vec_copy(base
->sample
);
1363 flags
= base
->flags
;
1364 ext
= add_constraints(ext
, base
, 0, 0);
1367 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1373 isl_space_free(dim
);
1374 isl_basic_map_free(base
);
1378 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1379 __isl_take isl_space
*dim
, unsigned extra
,
1380 unsigned n_eq
, unsigned n_ineq
)
1382 return (struct isl_basic_set
*)
1383 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1384 extra
, n_eq
, n_ineq
);
1387 struct isl_basic_map
*isl_basic_map_extend_constraints(
1388 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1392 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1396 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1397 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1398 unsigned n_eq
, unsigned n_ineq
)
1400 struct isl_basic_map
*bmap
;
1405 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1409 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1412 isl_basic_map_free(base
);
1416 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1417 unsigned nparam
, unsigned dim
, unsigned extra
,
1418 unsigned n_eq
, unsigned n_ineq
)
1420 return (struct isl_basic_set
*)
1421 isl_basic_map_extend((struct isl_basic_map
*)base
,
1422 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1425 struct isl_basic_set
*isl_basic_set_extend_constraints(
1426 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1428 return (struct isl_basic_set
*)
1429 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1433 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1435 return (struct isl_basic_set
*)
1436 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1439 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1444 if (bmap
->ref
> 1) {
1446 bmap
= isl_basic_map_dup(bmap
);
1449 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1453 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1461 return isl_set_dup(set
);
1464 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1472 return isl_map_dup(map
);
1475 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1476 unsigned a_len
, unsigned b_len
)
1478 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1479 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1480 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1483 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1484 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1492 isl_assert(bmap
->ctx
,
1493 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1495 if (n1
== 0 || n2
== 0)
1498 bmap
= isl_basic_map_cow(bmap
);
1502 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1503 if (isl_blk_is_error(blk
))
1506 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1508 bmap
->eq
[i
] + pos
, n1
, n2
);
1510 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1512 bmap
->ineq
[i
] + pos
, n1
, n2
);
1514 for (i
= 0; i
< bmap
->n_div
; ++i
)
1516 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1518 isl_blk_free(bmap
->ctx
, blk
);
1520 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1521 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1522 return isl_basic_map_finalize(bmap
);
1524 isl_basic_map_free(bmap
);
1528 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1529 __isl_take isl_basic_set
*bset
, unsigned n
)
1534 nparam
= isl_basic_set_n_param(bset
);
1535 dim
= isl_basic_set_n_dim(bset
);
1536 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1538 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1540 isl_basic_set_free(bset
);
1544 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1550 total
= isl_basic_map_total_dim(bmap
);
1551 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1552 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1554 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1556 i
= isl_basic_map_alloc_equality(bmap
);
1560 isl_int_set_si(bmap
->eq
[i
][0], 1);
1561 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1562 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1563 isl_vec_free(bmap
->sample
);
1564 bmap
->sample
= NULL
;
1565 return isl_basic_map_finalize(bmap
);
1567 isl_basic_map_free(bmap
);
1571 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1573 return (struct isl_basic_set
*)
1574 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1577 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1580 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1581 isl_int
*t
= bmap
->div
[a
];
1582 bmap
->div
[a
] = bmap
->div
[b
];
1585 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1586 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1588 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1589 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1591 for (i
= 0; i
< bmap
->n_div
; ++i
)
1592 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1596 /* Eliminate the specified n dimensions starting at first from the
1597 * constraints, without removing the dimensions from the space.
1598 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1600 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1601 enum isl_dim_type type
, unsigned first
, unsigned n
)
1610 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1611 isl_die(map
->ctx
, isl_error_invalid
,
1612 "index out of bounds", goto error
);
1614 map
= isl_map_cow(map
);
1618 for (i
= 0; i
< map
->n
; ++i
) {
1619 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1629 /* Eliminate the specified n dimensions starting at first from the
1630 * constraints, without removing the dimensions from the space.
1631 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1633 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1634 enum isl_dim_type type
, unsigned first
, unsigned n
)
1636 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1639 /* Eliminate the specified n dimensions starting at first from the
1640 * constraints, without removing the dimensions from the space.
1641 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1643 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1644 unsigned first
, unsigned n
)
1646 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1649 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1650 __isl_take isl_basic_map
*bmap
)
1654 bmap
= isl_basic_map_eliminate_vars(bmap
,
1655 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1659 return isl_basic_map_finalize(bmap
);
1662 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1663 __isl_take isl_basic_set
*bset
)
1665 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1666 (struct isl_basic_map
*)bset
);
1669 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1678 map
= isl_map_cow(map
);
1682 for (i
= 0; i
< map
->n
; ++i
) {
1683 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1693 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1695 return isl_map_remove_divs(set
);
1698 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1699 enum isl_dim_type type
, unsigned first
, unsigned n
)
1703 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1705 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1707 bmap
= isl_basic_map_eliminate_vars(bmap
,
1708 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1711 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1713 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1716 isl_basic_map_free(bmap
);
1720 /* Return true if the definition of the given div (recursively) involves
1721 * any of the given variables.
1723 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1724 unsigned first
, unsigned n
)
1727 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1729 if (isl_int_is_zero(bmap
->div
[div
][0]))
1731 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1734 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1735 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1737 if (div_involves_vars(bmap
, i
, first
, n
))
1744 /* Remove all divs (recursively) involving any of the given dimensions
1745 * in their definitions.
1747 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1748 __isl_take isl_basic_map
*bmap
,
1749 enum isl_dim_type type
, unsigned first
, unsigned n
)
1755 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1757 first
+= isl_basic_map_offset(bmap
, type
);
1759 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1760 if (!div_involves_vars(bmap
, i
, first
, n
))
1762 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1770 isl_basic_map_free(bmap
);
1774 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1775 enum isl_dim_type type
, unsigned first
, unsigned n
)
1784 map
= isl_map_cow(map
);
1788 for (i
= 0; i
< map
->n
; ++i
) {
1789 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1800 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1801 enum isl_dim_type type
, unsigned first
, unsigned n
)
1803 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1807 /* Does the desciption of "bmap" depend on the specified dimensions?
1808 * We also check whether the dimensions appear in any of the div definitions.
1809 * In principle there is no need for this check. If the dimensions appear
1810 * in a div definition, they also appear in the defining constraints of that
1813 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1814 enum isl_dim_type type
, unsigned first
, unsigned n
)
1821 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1822 isl_die(bmap
->ctx
, isl_error_invalid
,
1823 "index out of bounds", return -1);
1825 first
+= isl_basic_map_offset(bmap
, type
);
1826 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1827 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1829 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1830 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1832 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1833 if (isl_int_is_zero(bmap
->div
[i
][0]))
1835 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
1842 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1843 enum isl_dim_type type
, unsigned first
, unsigned n
)
1850 if (first
+ n
> isl_map_dim(map
, type
))
1851 isl_die(map
->ctx
, isl_error_invalid
,
1852 "index out of bounds", return -1);
1854 for (i
= 0; i
< map
->n
; ++i
) {
1855 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1857 if (involves
< 0 || involves
)
1864 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1865 enum isl_dim_type type
, unsigned first
, unsigned n
)
1867 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1870 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1871 enum isl_dim_type type
, unsigned first
, unsigned n
)
1873 return isl_map_involves_dims(set
, type
, first
, n
);
1876 /* Return true if the definition of the given div is unknown or depends
1879 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1882 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1884 if (isl_int_is_zero(bmap
->div
[div
][0]))
1887 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1888 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1890 if (div_is_unknown(bmap
, i
))
1897 /* Remove all divs that are unknown or defined in terms of unknown divs.
1899 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1900 __isl_take isl_basic_map
*bmap
)
1907 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1908 if (!div_is_unknown(bmap
, i
))
1910 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1919 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1928 map
= isl_map_cow(map
);
1932 for (i
= 0; i
< map
->n
; ++i
) {
1933 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1943 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1945 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1948 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1949 __isl_take isl_basic_set
*bset
,
1950 enum isl_dim_type type
, unsigned first
, unsigned n
)
1952 return (isl_basic_set
*)
1953 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1956 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1957 enum isl_dim_type type
, unsigned first
, unsigned n
)
1964 map
= isl_map_cow(map
);
1967 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1969 for (i
= 0; i
< map
->n
; ++i
) {
1970 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1971 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1975 map
= isl_map_drop(map
, type
, first
, n
);
1982 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1983 enum isl_dim_type type
, unsigned first
, unsigned n
)
1985 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1988 /* Project out n inputs starting at first using Fourier-Motzkin */
1989 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1990 unsigned first
, unsigned n
)
1992 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1995 static void dump_term(struct isl_basic_map
*bmap
,
1996 isl_int c
, int pos
, FILE *out
)
1999 unsigned in
= isl_basic_map_n_in(bmap
);
2000 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2001 unsigned nparam
= isl_basic_map_n_param(bmap
);
2003 isl_int_print(out
, c
, 0);
2005 if (!isl_int_is_one(c
))
2006 isl_int_print(out
, c
, 0);
2007 if (pos
< 1 + nparam
) {
2008 name
= isl_space_get_dim_name(bmap
->dim
,
2009 isl_dim_param
, pos
- 1);
2011 fprintf(out
, "%s", name
);
2013 fprintf(out
, "p%d", pos
- 1);
2014 } else if (pos
< 1 + nparam
+ in
)
2015 fprintf(out
, "i%d", pos
- 1 - nparam
);
2016 else if (pos
< 1 + nparam
+ dim
)
2017 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2019 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2023 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2024 int sign
, FILE *out
)
2028 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2032 for (i
= 0, first
= 1; i
< len
; ++i
) {
2033 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2036 fprintf(out
, " + ");
2038 isl_int_abs(v
, c
[i
]);
2039 dump_term(bmap
, v
, i
, out
);
2046 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2047 const char *op
, FILE *out
, int indent
)
2051 fprintf(out
, "%*s", indent
, "");
2053 dump_constraint_sign(bmap
, c
, 1, out
);
2054 fprintf(out
, " %s ", op
);
2055 dump_constraint_sign(bmap
, c
, -1, out
);
2059 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2060 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2062 fprintf(out
, "%*s", indent
, "");
2063 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2068 static void dump_constraints(struct isl_basic_map
*bmap
,
2069 isl_int
**c
, unsigned n
,
2070 const char *op
, FILE *out
, int indent
)
2074 for (i
= 0; i
< n
; ++i
)
2075 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2078 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2082 unsigned total
= isl_basic_map_total_dim(bmap
);
2084 for (j
= 0; j
< 1 + total
; ++j
) {
2085 if (isl_int_is_zero(exp
[j
]))
2087 if (!first
&& isl_int_is_pos(exp
[j
]))
2089 dump_term(bmap
, exp
[j
], j
, out
);
2094 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2098 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2099 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2101 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2102 fprintf(out
, "%*s", indent
, "");
2103 fprintf(out
, "e%d = [(", i
);
2104 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2106 isl_int_print(out
, bmap
->div
[i
][0], 0);
2107 fprintf(out
, "]\n");
2111 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2112 FILE *out
, int indent
)
2115 fprintf(out
, "null basic set\n");
2119 fprintf(out
, "%*s", indent
, "");
2120 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2121 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2122 bset
->extra
, bset
->flags
);
2123 dump((struct isl_basic_map
*)bset
, out
, indent
);
2126 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2127 FILE *out
, int indent
)
2130 fprintf(out
, "null basic map\n");
2134 fprintf(out
, "%*s", indent
, "");
2135 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2136 "flags: %x, n_name: %d\n",
2138 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2139 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2140 dump(bmap
, out
, indent
);
2143 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2148 total
= isl_basic_map_total_dim(bmap
);
2149 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2150 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2151 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2152 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2156 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2159 struct isl_set
*set
;
2163 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2164 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2165 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2166 sizeof(struct isl_set
) +
2167 (n
- 1) * sizeof(struct isl_basic_set
*));
2171 set
->ctx
= dim
->ctx
;
2172 isl_ctx_ref(set
->ctx
);
2180 isl_space_free(dim
);
2184 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2185 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2187 struct isl_set
*set
;
2190 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2194 set
= isl_set_alloc_space(dims
, n
, flags
);
2198 /* Make sure "map" has room for at least "n" more basic maps.
2200 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2203 struct isl_map
*grown
= NULL
;
2207 isl_assert(map
->ctx
, n
>= 0, goto error
);
2208 if (map
->n
+ n
<= map
->size
)
2210 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2213 for (i
= 0; i
< map
->n
; ++i
) {
2214 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2222 isl_map_free(grown
);
2227 /* Make sure "set" has room for at least "n" more basic sets.
2229 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2231 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2234 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2237 struct isl_set
*dup
;
2242 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2245 for (i
= 0; i
< set
->n
; ++i
)
2246 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2250 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2252 return isl_map_from_basic_map(bset
);
2255 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2257 struct isl_map
*map
;
2262 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2263 return isl_map_add_basic_map(map
, bmap
);
2266 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2267 __isl_take isl_basic_set
*bset
)
2269 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2270 (struct isl_basic_map
*)bset
);
2273 void isl_set_free(struct isl_set
*set
)
2283 isl_ctx_deref(set
->ctx
);
2284 for (i
= 0; i
< set
->n
; ++i
)
2285 isl_basic_set_free(set
->p
[i
]);
2286 isl_space_free(set
->dim
);
2290 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2295 fprintf(out
, "null set\n");
2299 fprintf(out
, "%*s", indent
, "");
2300 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2301 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2303 for (i
= 0; i
< set
->n
; ++i
) {
2304 fprintf(out
, "%*s", indent
, "");
2305 fprintf(out
, "basic set %d:\n", i
);
2306 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2310 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2315 fprintf(out
, "null map\n");
2319 fprintf(out
, "%*s", indent
, "");
2320 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2321 "flags: %x, n_name: %d\n",
2322 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2323 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2324 for (i
= 0; i
< map
->n
; ++i
) {
2325 fprintf(out
, "%*s", indent
, "");
2326 fprintf(out
, "basic map %d:\n", i
);
2327 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2331 struct isl_basic_map
*isl_basic_map_intersect_domain(
2332 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2334 struct isl_basic_map
*bmap_domain
;
2339 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2340 bset
->dim
, isl_dim_param
), goto error
);
2342 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2343 isl_assert(bset
->ctx
,
2344 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2346 bmap
= isl_basic_map_cow(bmap
);
2349 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2350 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2351 bmap_domain
= isl_basic_map_from_domain(bset
);
2352 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2354 bmap
= isl_basic_map_simplify(bmap
);
2355 return isl_basic_map_finalize(bmap
);
2357 isl_basic_map_free(bmap
);
2358 isl_basic_set_free(bset
);
2362 struct isl_basic_map
*isl_basic_map_intersect_range(
2363 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2365 struct isl_basic_map
*bmap_range
;
2370 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2371 bset
->dim
, isl_dim_param
), goto error
);
2373 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2374 isl_assert(bset
->ctx
,
2375 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2377 if (isl_basic_set_is_universe(bset
)) {
2378 isl_basic_set_free(bset
);
2382 bmap
= isl_basic_map_cow(bmap
);
2385 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2386 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2387 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2388 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2390 bmap
= isl_basic_map_simplify(bmap
);
2391 return isl_basic_map_finalize(bmap
);
2393 isl_basic_map_free(bmap
);
2394 isl_basic_set_free(bset
);
2398 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2404 total
= 1 + isl_basic_map_total_dim(bmap
);
2405 if (total
!= vec
->size
)
2410 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2411 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2412 if (!isl_int_is_zero(s
)) {
2418 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2419 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2420 if (isl_int_is_neg(s
)) {
2431 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2433 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2436 struct isl_basic_map
*isl_basic_map_intersect(
2437 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2439 struct isl_vec
*sample
= NULL
;
2441 if (!bmap1
|| !bmap2
)
2444 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2445 bmap2
->dim
, isl_dim_param
), goto error
);
2446 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2447 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2448 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2449 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2450 return isl_basic_map_intersect(bmap2
, bmap1
);
2452 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2453 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2454 isl_assert(bmap1
->ctx
,
2455 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2457 if (bmap1
->sample
&&
2458 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2459 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2460 sample
= isl_vec_copy(bmap1
->sample
);
2461 else if (bmap2
->sample
&&
2462 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2463 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2464 sample
= isl_vec_copy(bmap2
->sample
);
2466 bmap1
= isl_basic_map_cow(bmap1
);
2469 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2470 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2471 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2474 isl_vec_free(sample
);
2476 isl_vec_free(bmap1
->sample
);
2477 bmap1
->sample
= sample
;
2480 bmap1
= isl_basic_map_simplify(bmap1
);
2481 return isl_basic_map_finalize(bmap1
);
2484 isl_vec_free(sample
);
2485 isl_basic_map_free(bmap1
);
2486 isl_basic_map_free(bmap2
);
2490 struct isl_basic_set
*isl_basic_set_intersect(
2491 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2493 return (struct isl_basic_set
*)
2494 isl_basic_map_intersect(
2495 (struct isl_basic_map
*)bset1
,
2496 (struct isl_basic_map
*)bset2
);
2499 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2500 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2502 return isl_basic_set_intersect(bset1
, bset2
);
2505 /* Special case of isl_map_intersect, where both map1 and map2
2506 * are convex, without any divs and such that either map1 or map2
2507 * contains a single constraint. This constraint is then simply
2508 * added to the other map.
2510 static __isl_give isl_map
*map_intersect_add_constraint(
2511 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2513 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2514 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2515 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2516 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2518 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2519 return isl_map_intersect(map2
, map1
);
2521 isl_assert(map2
->ctx
,
2522 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2524 map1
= isl_map_cow(map1
);
2527 if (isl_map_plain_is_empty(map1
)) {
2531 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2532 if (map2
->p
[0]->n_eq
== 1)
2533 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2535 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2536 map2
->p
[0]->ineq
[0]);
2538 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2539 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2543 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2544 isl_basic_map_free(map1
->p
[0]);
2557 /* map2 may be either a parameter domain or a map living in the same
2560 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2561 __isl_take isl_map
*map2
)
2564 struct isl_map
*result
;
2570 if (isl_map_plain_is_empty(map1
) &&
2571 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2575 if (isl_map_plain_is_empty(map2
) &&
2576 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2581 if (map1
->n
== 1 && map2
->n
== 1 &&
2582 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2583 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2584 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2585 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2586 return map_intersect_add_constraint(map1
, map2
);
2588 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2589 isl_space_dim(map2
->dim
, isl_dim_param
))
2590 isl_assert(map1
->ctx
,
2591 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2593 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2594 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2595 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2597 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2598 map1
->n
* map2
->n
, flags
);
2601 for (i
= 0; i
< map1
->n
; ++i
)
2602 for (j
= 0; j
< map2
->n
; ++j
) {
2603 struct isl_basic_map
*part
;
2604 part
= isl_basic_map_intersect(
2605 isl_basic_map_copy(map1
->p
[i
]),
2606 isl_basic_map_copy(map2
->p
[j
]));
2607 if (isl_basic_map_is_empty(part
))
2608 isl_basic_map_free(part
);
2610 result
= isl_map_add_basic_map(result
, part
);
2623 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2624 __isl_take isl_map
*map2
)
2628 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2629 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2630 "spaces don't match", goto error
);
2631 return map_intersect_internal(map1
, map2
);
2638 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2639 __isl_take isl_map
*map2
)
2641 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2644 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2646 return (struct isl_set
*)
2647 isl_map_intersect((struct isl_map
*)set1
,
2648 (struct isl_map
*)set2
);
2651 /* map_intersect_internal accepts intersections
2652 * with parameter domains, so we can just call that function.
2654 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2655 __isl_take isl_set
*params
)
2657 return map_intersect_internal(map
, params
);
2660 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2661 __isl_take isl_map
*map2
)
2663 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2666 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2667 __isl_take isl_set
*params
)
2669 return isl_map_intersect_params(set
, params
);
2672 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2675 struct isl_basic_set
*bset
;
2680 bmap
= isl_basic_map_cow(bmap
);
2683 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2684 in
= isl_basic_map_n_in(bmap
);
2685 bset
= isl_basic_set_from_basic_map(bmap
);
2686 bset
= isl_basic_set_swap_vars(bset
, in
);
2687 return isl_basic_map_from_basic_set(bset
, dim
);
2690 static __isl_give isl_basic_map
*basic_map_space_reset(
2691 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2695 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2698 space
= isl_basic_map_get_space(bmap
);
2699 space
= isl_space_reset(space
, type
);
2700 bmap
= isl_basic_map_reset_space(bmap
, space
);
2704 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2705 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2708 struct isl_basic_map
*res
;
2709 struct isl_dim_map
*dim_map
;
2710 unsigned total
, off
;
2711 enum isl_dim_type t
;
2714 return basic_map_space_reset(bmap
, type
);
2719 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2721 total
= isl_basic_map_total_dim(bmap
) + n
;
2722 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2724 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2726 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2728 unsigned size
= isl_basic_map_dim(bmap
, t
);
2729 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2731 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2732 pos
, size
- pos
, off
+ pos
+ n
);
2734 off
+= isl_space_dim(res_dim
, t
);
2736 isl_dim_map_div(dim_map
, bmap
, off
);
2738 res
= isl_basic_map_alloc_space(res_dim
,
2739 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2740 if (isl_basic_map_is_rational(bmap
))
2741 res
= isl_basic_map_set_rational(res
);
2742 if (isl_basic_map_plain_is_empty(bmap
)) {
2743 isl_basic_map_free(bmap
);
2744 return isl_basic_map_set_to_empty(res
);
2746 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2747 return isl_basic_map_finalize(res
);
2750 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2751 enum isl_dim_type type
, unsigned n
)
2755 return isl_basic_map_insert(bmap
, type
,
2756 isl_basic_map_dim(bmap
, type
), n
);
2759 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2760 enum isl_dim_type type
, unsigned n
)
2764 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2765 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2767 isl_basic_set_free(bset
);
2771 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2772 enum isl_dim_type type
)
2776 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2779 space
= isl_map_get_space(map
);
2780 space
= isl_space_reset(space
, type
);
2781 map
= isl_map_reset_space(map
, space
);
2785 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2786 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2791 return map_space_reset(map
, type
);
2793 map
= isl_map_cow(map
);
2797 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2801 for (i
= 0; i
< map
->n
; ++i
) {
2802 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2813 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2814 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2816 return isl_map_insert_dims(set
, type
, pos
, n
);
2819 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2820 enum isl_dim_type type
, unsigned n
)
2824 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2827 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2828 enum isl_dim_type type
, unsigned n
)
2832 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2833 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2839 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2840 __isl_take isl_basic_map
*bmap
,
2841 enum isl_dim_type dst_type
, unsigned dst_pos
,
2842 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2844 struct isl_dim_map
*dim_map
;
2845 struct isl_basic_map
*res
;
2846 enum isl_dim_type t
;
2847 unsigned total
, off
;
2854 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2857 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2860 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2862 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2863 pos(bmap
->dim
, src_type
) + src_pos
+
2864 ((src_type
< dst_type
) ? n
: 0)) {
2865 bmap
= isl_basic_map_cow(bmap
);
2869 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2870 src_type
, src_pos
, n
);
2874 bmap
= isl_basic_map_finalize(bmap
);
2879 total
= isl_basic_map_total_dim(bmap
);
2880 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2883 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2884 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2885 if (t
== dst_type
) {
2886 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2889 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2892 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2893 dst_pos
, size
- dst_pos
, off
);
2894 off
+= size
- dst_pos
;
2895 } else if (t
== src_type
) {
2896 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2899 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2900 src_pos
+ n
, size
- src_pos
- n
, off
);
2901 off
+= size
- src_pos
- n
;
2903 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2907 isl_dim_map_div(dim_map
, bmap
, off
);
2909 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2910 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2911 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2913 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2914 src_type
, src_pos
, n
);
2918 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2919 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2920 bmap
= isl_basic_map_finalize(bmap
);
2924 isl_basic_map_free(bmap
);
2928 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2929 enum isl_dim_type dst_type
, unsigned dst_pos
,
2930 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2932 return (isl_basic_set
*)isl_basic_map_move_dims(
2933 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2936 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2937 enum isl_dim_type dst_type
, unsigned dst_pos
,
2938 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2942 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2943 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2944 src_type
, src_pos
, n
);
2950 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2951 enum isl_dim_type dst_type
, unsigned dst_pos
,
2952 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2961 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2964 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2967 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2969 map
= isl_map_cow(map
);
2973 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2977 for (i
= 0; i
< map
->n
; ++i
) {
2978 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2980 src_type
, src_pos
, n
);
2991 /* Move the specified dimensions to the last columns right before
2992 * the divs. Don't change the dimension specification of bmap.
2993 * That's the responsibility of the caller.
2995 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2996 enum isl_dim_type type
, unsigned first
, unsigned n
)
2998 struct isl_dim_map
*dim_map
;
2999 struct isl_basic_map
*res
;
3000 enum isl_dim_type t
;
3001 unsigned total
, off
;
3005 if (pos(bmap
->dim
, type
) + first
+ n
==
3006 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3009 total
= isl_basic_map_total_dim(bmap
);
3010 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3013 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3014 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3016 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3019 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3020 first
, n
, total
- bmap
->n_div
- n
);
3021 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3022 first
+ n
, size
- (first
+ n
), off
);
3023 off
+= size
- (first
+ n
);
3025 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3029 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3031 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3032 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3033 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3037 /* Turn the n dimensions of type type, starting at first
3038 * into existentially quantified variables.
3040 __isl_give isl_basic_map
*isl_basic_map_project_out(
3041 __isl_take isl_basic_map
*bmap
,
3042 enum isl_dim_type type
, unsigned first
, unsigned n
)
3050 return basic_map_space_reset(bmap
, type
);
3055 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3056 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3058 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3061 bmap
= move_last(bmap
, type
, first
, n
);
3062 bmap
= isl_basic_map_cow(bmap
);
3066 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3067 old
= bmap
->block2
.data
;
3068 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3069 (bmap
->extra
+ n
) * (1 + row_size
));
3070 if (!bmap
->block2
.data
)
3072 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3075 for (i
= 0; i
< n
; ++i
) {
3076 new_div
[i
] = bmap
->block2
.data
+
3077 (bmap
->extra
+ i
) * (1 + row_size
);
3078 isl_seq_clr(new_div
[i
], 1 + row_size
);
3080 for (i
= 0; i
< bmap
->extra
; ++i
)
3081 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3083 bmap
->div
= new_div
;
3087 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3090 bmap
= isl_basic_map_simplify(bmap
);
3091 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3092 return isl_basic_map_finalize(bmap
);
3094 isl_basic_map_free(bmap
);
3098 /* Turn the n dimensions of type type, starting at first
3099 * into existentially quantified variables.
3101 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3102 enum isl_dim_type type
, unsigned first
, unsigned n
)
3104 return (isl_basic_set
*)isl_basic_map_project_out(
3105 (isl_basic_map
*)bset
, type
, first
, n
);
3108 /* Turn the n dimensions of type type, starting at first
3109 * into existentially quantified variables.
3111 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3112 enum isl_dim_type type
, unsigned first
, unsigned n
)
3120 return map_space_reset(map
, type
);
3122 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3124 map
= isl_map_cow(map
);
3128 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3132 for (i
= 0; i
< map
->n
; ++i
) {
3133 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3144 /* Turn the n dimensions of type type, starting at first
3145 * into existentially quantified variables.
3147 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3148 enum isl_dim_type type
, unsigned first
, unsigned n
)
3150 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3153 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3157 for (i
= 0; i
< n
; ++i
) {
3158 j
= isl_basic_map_alloc_div(bmap
);
3161 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3165 isl_basic_map_free(bmap
);
3169 struct isl_basic_map
*isl_basic_map_apply_range(
3170 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3172 isl_space
*dim_result
= NULL
;
3173 struct isl_basic_map
*bmap
;
3174 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3175 struct isl_dim_map
*dim_map1
, *dim_map2
;
3177 if (!bmap1
|| !bmap2
)
3180 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3181 isl_space_copy(bmap2
->dim
));
3183 n_in
= isl_basic_map_n_in(bmap1
);
3184 n_out
= isl_basic_map_n_out(bmap2
);
3185 n
= isl_basic_map_n_out(bmap1
);
3186 nparam
= isl_basic_map_n_param(bmap1
);
3188 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3189 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3190 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3191 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3192 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3193 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3194 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3195 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3196 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3197 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3198 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3200 bmap
= isl_basic_map_alloc_space(dim_result
,
3201 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3202 bmap1
->n_eq
+ bmap2
->n_eq
,
3203 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3204 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3205 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3206 bmap
= add_divs(bmap
, n
);
3207 bmap
= isl_basic_map_simplify(bmap
);
3208 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3209 return isl_basic_map_finalize(bmap
);
3211 isl_basic_map_free(bmap1
);
3212 isl_basic_map_free(bmap2
);
3216 struct isl_basic_set
*isl_basic_set_apply(
3217 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3222 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3225 return (struct isl_basic_set
*)
3226 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3228 isl_basic_set_free(bset
);
3229 isl_basic_map_free(bmap
);
3233 struct isl_basic_map
*isl_basic_map_apply_domain(
3234 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3236 if (!bmap1
|| !bmap2
)
3239 isl_assert(bmap1
->ctx
,
3240 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3241 isl_assert(bmap1
->ctx
,
3242 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3245 bmap1
= isl_basic_map_reverse(bmap1
);
3246 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3247 return isl_basic_map_reverse(bmap1
);
3249 isl_basic_map_free(bmap1
);
3250 isl_basic_map_free(bmap2
);
3254 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3255 * A \cap B -> f(A) + f(B)
3257 struct isl_basic_map
*isl_basic_map_sum(
3258 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3260 unsigned n_in
, n_out
, nparam
, total
, pos
;
3261 struct isl_basic_map
*bmap
= NULL
;
3262 struct isl_dim_map
*dim_map1
, *dim_map2
;
3265 if (!bmap1
|| !bmap2
)
3268 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3271 nparam
= isl_basic_map_n_param(bmap1
);
3272 n_in
= isl_basic_map_n_in(bmap1
);
3273 n_out
= isl_basic_map_n_out(bmap1
);
3275 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3276 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3277 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3278 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3279 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3280 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3281 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3282 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3283 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3284 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3285 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3287 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3288 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3289 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3290 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3291 for (i
= 0; i
< n_out
; ++i
) {
3292 int j
= isl_basic_map_alloc_equality(bmap
);
3295 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3296 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3297 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3298 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3300 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3301 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3302 bmap
= add_divs(bmap
, 2 * n_out
);
3304 bmap
= isl_basic_map_simplify(bmap
);
3305 return isl_basic_map_finalize(bmap
);
3307 isl_basic_map_free(bmap
);
3308 isl_basic_map_free(bmap1
);
3309 isl_basic_map_free(bmap2
);
3313 /* Given two maps A -> f(A) and B -> g(B), construct a map
3314 * A \cap B -> f(A) + f(B)
3316 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3318 struct isl_map
*result
;
3324 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3326 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3327 map1
->n
* map2
->n
, 0);
3330 for (i
= 0; i
< map1
->n
; ++i
)
3331 for (j
= 0; j
< map2
->n
; ++j
) {
3332 struct isl_basic_map
*part
;
3333 part
= isl_basic_map_sum(
3334 isl_basic_map_copy(map1
->p
[i
]),
3335 isl_basic_map_copy(map2
->p
[j
]));
3336 if (isl_basic_map_is_empty(part
))
3337 isl_basic_map_free(part
);
3339 result
= isl_map_add_basic_map(result
, part
);
3352 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3353 __isl_take isl_set
*set2
)
3355 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3358 /* Given a basic map A -> f(A), construct A -> -f(A).
3360 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3365 bmap
= isl_basic_map_cow(bmap
);
3369 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3370 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3371 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3372 for (j
= 0; j
< n
; ++j
)
3373 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3374 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3375 for (j
= 0; j
< n
; ++j
)
3376 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3377 for (i
= 0; i
< bmap
->n_div
; ++i
)
3378 for (j
= 0; j
< n
; ++j
)
3379 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3380 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3381 return isl_basic_map_finalize(bmap
);
3384 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3386 return isl_basic_map_neg(bset
);
3389 /* Given a map A -> f(A), construct A -> -f(A).
3391 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3395 map
= isl_map_cow(map
);
3399 for (i
= 0; i
< map
->n
; ++i
) {
3400 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3411 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3413 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3416 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3417 * A -> floor(f(A)/d).
3419 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3422 unsigned n_in
, n_out
, nparam
, total
, pos
;
3423 struct isl_basic_map
*result
= NULL
;
3424 struct isl_dim_map
*dim_map
;
3430 nparam
= isl_basic_map_n_param(bmap
);
3431 n_in
= isl_basic_map_n_in(bmap
);
3432 n_out
= isl_basic_map_n_out(bmap
);
3434 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3435 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3436 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3437 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3438 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3439 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3441 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3442 bmap
->n_div
+ n_out
,
3443 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3444 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3445 result
= add_divs(result
, n_out
);
3446 for (i
= 0; i
< n_out
; ++i
) {
3448 j
= isl_basic_map_alloc_inequality(result
);
3451 isl_seq_clr(result
->ineq
[j
], 1+total
);
3452 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3453 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3454 j
= isl_basic_map_alloc_inequality(result
);
3457 isl_seq_clr(result
->ineq
[j
], 1+total
);
3458 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3459 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3460 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3463 result
= isl_basic_map_simplify(result
);
3464 return isl_basic_map_finalize(result
);
3466 isl_basic_map_free(result
);
3470 /* Given a map A -> f(A) and an integer d, construct a map
3471 * A -> floor(f(A)/d).
3473 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3477 map
= isl_map_cow(map
);
3481 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3482 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3483 for (i
= 0; i
< map
->n
; ++i
) {
3484 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3495 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3501 i
= isl_basic_map_alloc_equality(bmap
);
3504 nparam
= isl_basic_map_n_param(bmap
);
3505 n_in
= isl_basic_map_n_in(bmap
);
3506 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3507 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3508 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3509 return isl_basic_map_finalize(bmap
);
3511 isl_basic_map_free(bmap
);
3515 /* Add a constraints to "bmap" expressing i_pos < o_pos
3517 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3523 i
= isl_basic_map_alloc_inequality(bmap
);
3526 nparam
= isl_basic_map_n_param(bmap
);
3527 n_in
= isl_basic_map_n_in(bmap
);
3528 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3529 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3530 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3531 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3532 return isl_basic_map_finalize(bmap
);
3534 isl_basic_map_free(bmap
);
3538 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3540 static __isl_give isl_basic_map
*var_less_or_equal(
3541 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3547 i
= isl_basic_map_alloc_inequality(bmap
);
3550 nparam
= isl_basic_map_n_param(bmap
);
3551 n_in
= isl_basic_map_n_in(bmap
);
3552 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3553 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3554 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3555 return isl_basic_map_finalize(bmap
);
3557 isl_basic_map_free(bmap
);
3561 /* Add a constraints to "bmap" expressing i_pos > o_pos
3563 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3569 i
= isl_basic_map_alloc_inequality(bmap
);
3572 nparam
= isl_basic_map_n_param(bmap
);
3573 n_in
= isl_basic_map_n_in(bmap
);
3574 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3575 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3576 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3577 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3578 return isl_basic_map_finalize(bmap
);
3580 isl_basic_map_free(bmap
);
3584 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3586 static __isl_give isl_basic_map
*var_more_or_equal(
3587 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3593 i
= isl_basic_map_alloc_inequality(bmap
);
3596 nparam
= isl_basic_map_n_param(bmap
);
3597 n_in
= isl_basic_map_n_in(bmap
);
3598 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3599 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3600 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3601 return isl_basic_map_finalize(bmap
);
3603 isl_basic_map_free(bmap
);
3607 __isl_give isl_basic_map
*isl_basic_map_equal(
3608 __isl_take isl_space
*dim
, unsigned n_equal
)
3611 struct isl_basic_map
*bmap
;
3612 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3615 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3616 bmap
= var_equal(bmap
, i
);
3617 return isl_basic_map_finalize(bmap
);
3620 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3622 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3626 struct isl_basic_map
*bmap
;
3627 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3630 for (i
= 0; i
< pos
&& bmap
; ++i
)
3631 bmap
= var_equal(bmap
, i
);
3633 bmap
= var_less(bmap
, pos
);
3634 return isl_basic_map_finalize(bmap
);
3637 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3639 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3640 __isl_take isl_space
*dim
, unsigned pos
)
3643 isl_basic_map
*bmap
;
3645 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3646 for (i
= 0; i
< pos
; ++i
)
3647 bmap
= var_equal(bmap
, i
);
3648 bmap
= var_less_or_equal(bmap
, pos
);
3649 return isl_basic_map_finalize(bmap
);
3652 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3654 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3658 struct isl_basic_map
*bmap
;
3659 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3662 for (i
= 0; i
< pos
&& bmap
; ++i
)
3663 bmap
= var_equal(bmap
, i
);
3665 bmap
= var_more(bmap
, pos
);
3666 return isl_basic_map_finalize(bmap
);
3669 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3671 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3672 __isl_take isl_space
*dim
, unsigned pos
)
3675 isl_basic_map
*bmap
;
3677 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3678 for (i
= 0; i
< pos
; ++i
)
3679 bmap
= var_equal(bmap
, i
);
3680 bmap
= var_more_or_equal(bmap
, pos
);
3681 return isl_basic_map_finalize(bmap
);
3684 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3685 unsigned n
, int equal
)
3687 struct isl_map
*map
;
3690 if (n
== 0 && equal
)
3691 return isl_map_universe(dims
);
3693 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3695 for (i
= 0; i
+ 1 < n
; ++i
)
3696 map
= isl_map_add_basic_map(map
,
3697 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3700 map
= isl_map_add_basic_map(map
,
3701 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3703 map
= isl_map_add_basic_map(map
,
3704 isl_basic_map_less_at(dims
, n
- 1));
3706 isl_space_free(dims
);
3711 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3715 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3718 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3720 return map_lex_lte_first(dim
, n
, 0);
3723 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3725 return map_lex_lte_first(dim
, n
, 1);
3728 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3730 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3733 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3735 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3738 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3739 unsigned n
, int equal
)
3741 struct isl_map
*map
;
3744 if (n
== 0 && equal
)
3745 return isl_map_universe(dims
);
3747 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3749 for (i
= 0; i
+ 1 < n
; ++i
)
3750 map
= isl_map_add_basic_map(map
,
3751 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3754 map
= isl_map_add_basic_map(map
,
3755 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3757 map
= isl_map_add_basic_map(map
,
3758 isl_basic_map_more_at(dims
, n
- 1));
3760 isl_space_free(dims
);
3765 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3769 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3772 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3774 return map_lex_gte_first(dim
, n
, 0);
3777 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3779 return map_lex_gte_first(dim
, n
, 1);
3782 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3784 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3787 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3789 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3792 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3793 __isl_take isl_set
*set2
)
3796 map
= isl_map_lex_le(isl_set_get_space(set1
));
3797 map
= isl_map_intersect_domain(map
, set1
);
3798 map
= isl_map_intersect_range(map
, set2
);
3802 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3803 __isl_take isl_set
*set2
)
3806 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3807 map
= isl_map_intersect_domain(map
, set1
);
3808 map
= isl_map_intersect_range(map
, set2
);
3812 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3813 __isl_take isl_set
*set2
)
3816 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3817 map
= isl_map_intersect_domain(map
, set1
);
3818 map
= isl_map_intersect_range(map
, set2
);
3822 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3823 __isl_take isl_set
*set2
)
3826 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3827 map
= isl_map_intersect_domain(map
, set1
);
3828 map
= isl_map_intersect_range(map
, set2
);
3832 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3833 __isl_take isl_map
*map2
)
3836 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3837 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3838 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3842 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3843 __isl_take isl_map
*map2
)
3846 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3847 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3848 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3852 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3853 __isl_take isl_map
*map2
)
3856 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3857 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3858 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3862 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3863 __isl_take isl_map
*map2
)
3866 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3867 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3868 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3872 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3873 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3875 struct isl_basic_map
*bmap
;
3877 bset
= isl_basic_set_cow(bset
);
3881 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3882 isl_space_free(bset
->dim
);
3883 bmap
= (struct isl_basic_map
*) bset
;
3885 return isl_basic_map_finalize(bmap
);
3887 isl_basic_set_free(bset
);
3888 isl_space_free(dim
);
3892 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3896 if (bmap
->dim
->n_in
== 0)
3897 return (struct isl_basic_set
*)bmap
;
3898 bmap
= isl_basic_map_cow(bmap
);
3901 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3904 bmap
= isl_basic_map_finalize(bmap
);
3905 return (struct isl_basic_set
*)bmap
;
3907 isl_basic_map_free(bmap
);
3911 /* For a div d = floor(f/m), add the constraints
3914 * -(f-(n-1)) + m d >= 0
3916 * Note that the second constraint is the negation of
3920 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3921 unsigned pos
, isl_int
*div
)
3924 unsigned total
= isl_basic_map_total_dim(bmap
);
3926 i
= isl_basic_map_alloc_inequality(bmap
);
3929 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3930 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3932 j
= isl_basic_map_alloc_inequality(bmap
);
3935 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3936 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3937 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3941 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3942 unsigned pos
, isl_int
*div
)
3944 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3948 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3950 unsigned total
= isl_basic_map_total_dim(bmap
);
3951 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3953 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3957 struct isl_basic_set
*isl_basic_map_underlying_set(
3958 struct isl_basic_map
*bmap
)
3962 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3964 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3965 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3966 return (struct isl_basic_set
*)bmap
;
3967 bmap
= isl_basic_map_cow(bmap
);
3970 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3973 bmap
->extra
-= bmap
->n_div
;
3975 bmap
= isl_basic_map_finalize(bmap
);
3976 return (struct isl_basic_set
*)bmap
;
3981 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3982 __isl_take isl_basic_set
*bset
)
3984 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3987 struct isl_basic_map
*isl_basic_map_overlying_set(
3988 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3990 struct isl_basic_map
*bmap
;
3991 struct isl_ctx
*ctx
;
3998 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3999 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4000 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4002 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4003 isl_basic_map_free(like
);
4004 return (struct isl_basic_map
*)bset
;
4006 bset
= isl_basic_set_cow(bset
);
4009 total
= bset
->dim
->n_out
+ bset
->extra
;
4010 bmap
= (struct isl_basic_map
*)bset
;
4011 isl_space_free(bmap
->dim
);
4012 bmap
->dim
= isl_space_copy(like
->dim
);
4015 bmap
->n_div
= like
->n_div
;
4016 bmap
->extra
+= like
->n_div
;
4020 ltotal
= total
- bmap
->extra
+ like
->extra
;
4023 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4024 bmap
->extra
* (1 + 1 + total
));
4025 if (isl_blk_is_error(bmap
->block2
))
4027 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4031 for (i
= 0; i
< bmap
->extra
; ++i
)
4032 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4033 for (i
= 0; i
< like
->n_div
; ++i
) {
4034 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4035 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4037 bmap
= isl_basic_map_extend_constraints(bmap
,
4038 0, 2 * like
->n_div
);
4039 for (i
= 0; i
< like
->n_div
; ++i
) {
4040 if (isl_int_is_zero(bmap
->div
[i
][0]))
4042 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4046 isl_basic_map_free(like
);
4047 bmap
= isl_basic_map_simplify(bmap
);
4048 bmap
= isl_basic_map_finalize(bmap
);
4051 isl_basic_map_free(like
);
4052 isl_basic_set_free(bset
);
4056 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4057 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4059 return (struct isl_basic_set
*)
4060 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4063 struct isl_set
*isl_set_from_underlying_set(
4064 struct isl_set
*set
, struct isl_basic_set
*like
)
4070 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4072 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4073 isl_basic_set_free(like
);
4076 set
= isl_set_cow(set
);
4079 for (i
= 0; i
< set
->n
; ++i
) {
4080 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4081 isl_basic_set_copy(like
));
4085 isl_space_free(set
->dim
);
4086 set
->dim
= isl_space_copy(like
->dim
);
4089 isl_basic_set_free(like
);
4092 isl_basic_set_free(like
);
4097 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4101 map
= isl_map_cow(map
);
4104 map
->dim
= isl_space_cow(map
->dim
);
4108 for (i
= 1; i
< map
->n
; ++i
)
4109 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4111 for (i
= 0; i
< map
->n
; ++i
) {
4112 map
->p
[i
] = (struct isl_basic_map
*)
4113 isl_basic_map_underlying_set(map
->p
[i
]);
4118 map
->dim
= isl_space_underlying(map
->dim
, 0);
4120 isl_space_free(map
->dim
);
4121 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4125 return (struct isl_set
*)map
;
4131 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4133 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4136 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4137 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4139 bmap
= isl_basic_map_cow(bmap
);
4143 isl_space_free(bmap
->dim
);
4146 bmap
= isl_basic_map_finalize(bmap
);
4150 isl_basic_map_free(bmap
);
4151 isl_space_free(dim
);
4155 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4156 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4158 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4162 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4163 __isl_take isl_space
*dim
)
4167 map
= isl_map_cow(map
);
4171 for (i
= 0; i
< map
->n
; ++i
) {
4172 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4173 isl_space_copy(dim
));
4177 isl_space_free(map
->dim
);
4183 isl_space_free(dim
);
4187 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4188 __isl_take isl_space
*dim
)
4190 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4193 /* Compute the parameter domain of the given basic set.
4195 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4200 if (isl_basic_set_is_params(bset
))
4203 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4204 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4205 space
= isl_basic_set_get_space(bset
);
4206 space
= isl_space_params(space
);
4207 bset
= isl_basic_set_reset_space(bset
, space
);
4211 /* Compute the parameter domain of the given set.
4213 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4218 if (isl_set_is_params(set
))
4221 n
= isl_set_dim(set
, isl_dim_set
);
4222 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4223 space
= isl_set_get_space(set
);
4224 space
= isl_space_params(space
);
4225 set
= isl_set_reset_space(set
, space
);
4229 /* Construct a zero-dimensional set with the given parameter domain.
4231 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4234 space
= isl_set_get_space(set
);
4235 space
= isl_space_set_from_params(space
);
4236 set
= isl_set_reset_space(set
, space
);
4240 /* Compute the parameter domain of the given map.
4242 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4247 n
= isl_map_dim(map
, isl_dim_in
);
4248 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4249 n
= isl_map_dim(map
, isl_dim_out
);
4250 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4251 space
= isl_map_get_space(map
);
4252 space
= isl_space_params(space
);
4253 map
= isl_map_reset_space(map
, space
);
4257 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4260 struct isl_basic_set
*domain
;
4266 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4268 n_in
= isl_basic_map_n_in(bmap
);
4269 n_out
= isl_basic_map_n_out(bmap
);
4270 domain
= isl_basic_set_from_basic_map(bmap
);
4271 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4273 domain
= isl_basic_set_reset_space(domain
, dim
);
4278 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4282 return isl_space_may_be_set(bmap
->dim
);
4285 /* Is this basic map actually a set?
4286 * Users should never call this function. Outside of isl,
4287 * the type should indicate whether something is a set or a map.
4289 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4293 return isl_space_is_set(bmap
->dim
);
4296 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4300 if (isl_basic_map_is_set(bmap
))
4302 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4305 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4306 __isl_take isl_basic_map
*bmap
)
4310 isl_basic_map
*domain
;
4311 int nparam
, n_in
, n_out
;
4314 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4315 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4316 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4318 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4319 domain
= isl_basic_map_universe(dim
);
4321 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4322 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4323 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4325 total
= isl_basic_map_total_dim(bmap
);
4327 for (i
= 0; i
< n_in
; ++i
) {
4328 k
= isl_basic_map_alloc_equality(bmap
);
4331 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4332 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4333 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4336 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4337 return isl_basic_map_finalize(bmap
);
4339 isl_basic_map_free(bmap
);
4343 __isl_give isl_basic_map
*isl_basic_map_range_map(
4344 __isl_take isl_basic_map
*bmap
)
4348 isl_basic_map
*range
;
4349 int nparam
, n_in
, n_out
;
4352 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4353 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4354 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4356 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4357 range
= isl_basic_map_universe(dim
);
4359 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4360 bmap
= isl_basic_map_apply_range(bmap
, range
);
4361 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4363 total
= isl_basic_map_total_dim(bmap
);
4365 for (i
= 0; i
< n_out
; ++i
) {
4366 k
= isl_basic_map_alloc_equality(bmap
);
4369 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4370 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4371 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4374 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4375 return isl_basic_map_finalize(bmap
);
4377 isl_basic_map_free(bmap
);
4381 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4385 return isl_space_may_be_set(map
->dim
);
4388 /* Is this map actually a set?
4389 * Users should never call this function. Outside of isl,
4390 * the type should indicate whether something is a set or a map.
4392 int isl_map_is_set(__isl_keep isl_map
*map
)
4396 return isl_space_is_set(map
->dim
);
4399 struct isl_set
*isl_map_range(struct isl_map
*map
)
4402 struct isl_set
*set
;
4406 if (isl_map_is_set(map
))
4407 return (isl_set
*)map
;
4409 map
= isl_map_cow(map
);
4413 set
= (struct isl_set
*) map
;
4414 set
->dim
= isl_space_range(set
->dim
);
4417 for (i
= 0; i
< map
->n
; ++i
) {
4418 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4422 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4423 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4430 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4433 isl_space
*domain_dim
;
4435 map
= isl_map_cow(map
);
4439 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4440 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4441 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4444 for (i
= 0; i
< map
->n
; ++i
) {
4445 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4449 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4450 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4457 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4460 isl_space
*range_dim
;
4462 map
= isl_map_cow(map
);
4466 range_dim
= isl_space_range(isl_map_get_space(map
));
4467 range_dim
= isl_space_from_range(range_dim
);
4468 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4469 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4472 for (i
= 0; i
< map
->n
; ++i
) {
4473 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4477 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4478 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4485 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4486 __isl_take isl_space
*dim
)
4489 struct isl_map
*map
= NULL
;
4491 set
= isl_set_cow(set
);
4494 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4495 map
= (struct isl_map
*)set
;
4496 for (i
= 0; i
< set
->n
; ++i
) {
4497 map
->p
[i
] = isl_basic_map_from_basic_set(
4498 set
->p
[i
], isl_space_copy(dim
));
4502 isl_space_free(map
->dim
);
4506 isl_space_free(dim
);
4511 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4512 __isl_take isl_basic_set
*bset
)
4514 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4517 __isl_give isl_basic_map
*isl_basic_map_from_range(
4518 __isl_take isl_basic_set
*bset
)
4521 space
= isl_basic_set_get_space(bset
);
4522 space
= isl_space_from_range(space
);
4523 bset
= isl_basic_set_reset_space(bset
, space
);
4524 return (isl_basic_map
*)bset
;
4527 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4530 space
= isl_set_get_space(set
);
4531 space
= isl_space_from_range(space
);
4532 set
= isl_set_reset_space(set
, space
);
4533 return (struct isl_map
*)set
;
4536 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4538 return isl_map_reverse(isl_map_from_range(set
));
4541 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4542 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4544 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4547 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4548 __isl_take isl_set
*range
)
4550 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4553 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4556 struct isl_set
*set
= NULL
;
4560 map
= isl_map_cow(map
);
4563 map
->dim
= isl_space_as_set_space(map
->dim
);
4566 set
= (struct isl_set
*)map
;
4567 for (i
= 0; i
< map
->n
; ++i
) {
4568 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4578 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4581 struct isl_map
*map
;
4586 isl_die(dim
->ctx
, isl_error_internal
,
4587 "negative number of basic maps", goto error
);
4588 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4589 sizeof(struct isl_map
) +
4590 (n
- 1) * sizeof(struct isl_basic_map
*));
4594 map
->ctx
= dim
->ctx
;
4595 isl_ctx_ref(map
->ctx
);
4603 isl_space_free(dim
);
4607 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4608 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4611 struct isl_map
*map
;
4614 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4618 map
= isl_map_alloc_space(dims
, n
, flags
);
4622 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4624 struct isl_basic_map
*bmap
;
4625 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4626 bmap
= isl_basic_map_set_to_empty(bmap
);
4630 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4632 struct isl_basic_set
*bset
;
4633 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4634 bset
= isl_basic_set_set_to_empty(bset
);
4638 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4640 struct isl_basic_map
*bmap
;
4643 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4644 bmap
= isl_basic_map_set_to_empty(bmap
);
4648 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4650 struct isl_basic_map
*bmap
;
4653 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4654 bmap
= isl_basic_map_set_to_empty(bmap
);
4658 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4660 struct isl_basic_set
*bset
;
4663 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4664 bset
= isl_basic_set_set_to_empty(bset
);
4668 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4670 struct isl_basic_map
*bmap
;
4671 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4672 bmap
= isl_basic_map_finalize(bmap
);
4676 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4678 struct isl_basic_set
*bset
;
4679 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4680 bset
= isl_basic_set_finalize(bset
);
4684 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4687 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4688 isl_basic_map
*bmap
;
4690 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4691 for (i
= 0; i
< total
; ++i
) {
4692 int k
= isl_basic_map_alloc_inequality(bmap
);
4695 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4696 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4700 isl_basic_map_free(bmap
);
4704 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4706 return isl_basic_map_nat_universe(dim
);
4709 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4711 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4714 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4716 return isl_map_nat_universe(dim
);
4719 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4720 __isl_keep isl_basic_map
*model
)
4724 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4727 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4731 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4734 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4735 __isl_keep isl_set
*model
)
4739 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4742 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4744 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4747 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4751 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4754 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4758 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4761 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4763 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4766 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4770 return isl_set_empty(isl_space_copy(model
->dim
));
4773 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4775 struct isl_map
*map
;
4778 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4779 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4783 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4785 struct isl_set
*set
;
4788 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4789 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4793 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4797 return isl_set_universe(isl_space_copy(model
->dim
));
4800 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4803 struct isl_map
*dup
;
4807 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4808 for (i
= 0; i
< map
->n
; ++i
)
4809 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4813 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4814 __isl_take isl_basic_map
*bmap
)
4818 if (isl_basic_map_plain_is_empty(bmap
)) {
4819 isl_basic_map_free(bmap
);
4822 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4823 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4824 map
->p
[map
->n
] = bmap
;
4826 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4832 isl_basic_map_free(bmap
);
4836 void isl_map_free(struct isl_map
*map
)
4846 isl_ctx_deref(map
->ctx
);
4847 for (i
= 0; i
< map
->n
; ++i
)
4848 isl_basic_map_free(map
->p
[i
]);
4849 isl_space_free(map
->dim
);
4853 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4854 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4858 base
= isl_map_cow(base
);
4862 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4865 for (i
= 0; i
< base
->n
; ++i
) {
4866 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4867 isl_space_copy(base
->dim
), 0, 0, 0);
4877 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4878 unsigned nparam
, unsigned dim
)
4880 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4884 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4885 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4889 bmap
= isl_basic_map_cow(bmap
);
4890 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4891 j
= isl_basic_map_alloc_equality(bmap
);
4894 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4895 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4896 isl_int_set_si(bmap
->eq
[j
][0], value
);
4897 bmap
= isl_basic_map_simplify(bmap
);
4898 return isl_basic_map_finalize(bmap
);
4900 isl_basic_map_free(bmap
);
4904 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4905 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4909 bmap
= isl_basic_map_cow(bmap
);
4910 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4911 j
= isl_basic_map_alloc_equality(bmap
);
4914 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4915 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4916 isl_int_set(bmap
->eq
[j
][0], value
);
4917 bmap
= isl_basic_map_simplify(bmap
);
4918 return isl_basic_map_finalize(bmap
);
4920 isl_basic_map_free(bmap
);
4924 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4925 enum isl_dim_type type
, unsigned pos
, int value
)
4929 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4930 return isl_basic_map_fix_pos_si(bmap
,
4931 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4933 isl_basic_map_free(bmap
);
4937 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4938 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4942 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4943 return isl_basic_map_fix_pos(bmap
,
4944 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4946 isl_basic_map_free(bmap
);
4950 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4951 enum isl_dim_type type
, unsigned pos
, int value
)
4953 return (struct isl_basic_set
*)
4954 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4958 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4959 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4961 return (struct isl_basic_set
*)
4962 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4966 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4967 unsigned input
, int value
)
4969 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4972 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4973 unsigned dim
, int value
)
4975 return (struct isl_basic_set
*)
4976 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4977 isl_dim_set
, dim
, value
);
4980 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
4982 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
4989 isl_basic_map_free(map
->p
[i
]);
4990 if (i
!= map
->n
- 1) {
4991 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4992 map
->p
[i
] = map
->p
[map
->n
- 1];
4999 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5000 enum isl_dim_type type
, unsigned pos
, int value
)
5004 map
= isl_map_cow(map
);
5008 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5009 for (i
= map
->n
- 1; i
>= 0; --i
) {
5010 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5011 if (remove_if_empty(map
, i
) < 0)
5014 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5021 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5022 enum isl_dim_type type
, unsigned pos
, int value
)
5024 return (struct isl_set
*)
5025 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5028 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5029 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5033 map
= isl_map_cow(map
);
5037 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5038 for (i
= 0; i
< map
->n
; ++i
) {
5039 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5043 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5050 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5051 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5053 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5056 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5057 unsigned input
, int value
)
5059 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5062 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5064 return (struct isl_set
*)
5065 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5068 static __isl_give isl_basic_map
*basic_map_bound_si(
5069 __isl_take isl_basic_map
*bmap
,
5070 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5076 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5077 pos
+= isl_basic_map_offset(bmap
, type
);
5078 bmap
= isl_basic_map_cow(bmap
);
5079 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5080 j
= isl_basic_map_alloc_inequality(bmap
);
5083 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5085 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5086 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5088 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5089 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5091 bmap
= isl_basic_map_simplify(bmap
);
5092 return isl_basic_map_finalize(bmap
);
5094 isl_basic_map_free(bmap
);
5098 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5099 __isl_take isl_basic_map
*bmap
,
5100 enum isl_dim_type type
, unsigned pos
, int value
)
5102 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5105 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5106 unsigned dim
, isl_int value
)
5110 bset
= isl_basic_set_cow(bset
);
5111 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5112 j
= isl_basic_set_alloc_inequality(bset
);
5115 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5116 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5117 isl_int_neg(bset
->ineq
[j
][0], value
);
5118 bset
= isl_basic_set_simplify(bset
);
5119 return isl_basic_set_finalize(bset
);
5121 isl_basic_set_free(bset
);
5125 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5126 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5130 map
= isl_map_cow(map
);
5134 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5135 for (i
= 0; i
< map
->n
; ++i
) {
5136 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5137 type
, pos
, value
, upper
);
5141 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5148 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5149 enum isl_dim_type type
, unsigned pos
, int value
)
5151 return map_bound_si(map
, type
, pos
, value
, 0);
5154 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5155 enum isl_dim_type type
, unsigned pos
, int value
)
5157 return map_bound_si(map
, type
, pos
, value
, 1);
5160 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5161 enum isl_dim_type type
, unsigned pos
, int value
)
5163 return (struct isl_set
*)
5164 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5167 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5168 enum isl_dim_type type
, unsigned pos
, int value
)
5170 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5173 /* Bound the given variable of "bmap" from below (or above is "upper"
5174 * is set) to "value".
5176 static __isl_give isl_basic_map
*basic_map_bound(
5177 __isl_take isl_basic_map
*bmap
,
5178 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5184 if (pos
>= isl_basic_map_dim(bmap
, type
))
5185 isl_die(bmap
->ctx
, isl_error_invalid
,
5186 "index out of bounds", goto error
);
5187 pos
+= isl_basic_map_offset(bmap
, type
);
5188 bmap
= isl_basic_map_cow(bmap
);
5189 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5190 j
= isl_basic_map_alloc_inequality(bmap
);
5193 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5195 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5196 isl_int_set(bmap
->ineq
[j
][0], value
);
5198 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5199 isl_int_neg(bmap
->ineq
[j
][0], value
);
5201 bmap
= isl_basic_map_simplify(bmap
);
5202 return isl_basic_map_finalize(bmap
);
5204 isl_basic_map_free(bmap
);
5208 /* Bound the given variable of "map" from below (or above is "upper"
5209 * is set) to "value".
5211 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5212 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5216 map
= isl_map_cow(map
);
5220 if (pos
>= isl_map_dim(map
, type
))
5221 isl_die(map
->ctx
, isl_error_invalid
,
5222 "index out of bounds", goto error
);
5223 for (i
= map
->n
- 1; i
>= 0; --i
) {
5224 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5225 if (remove_if_empty(map
, i
) < 0)
5228 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5235 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5236 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5238 return map_bound(map
, type
, pos
, value
, 0);
5241 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5242 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5244 return map_bound(map
, type
, pos
, value
, 1);
5247 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5248 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5250 return isl_map_lower_bound(set
, type
, pos
, value
);
5253 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5254 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5256 return isl_map_upper_bound(set
, type
, pos
, value
);
5259 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5264 set
= isl_set_cow(set
);
5268 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5269 for (i
= 0; i
< set
->n
; ++i
) {
5270 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5280 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5284 map
= isl_map_cow(map
);
5288 map
->dim
= isl_space_reverse(map
->dim
);
5291 for (i
= 0; i
< map
->n
; ++i
) {
5292 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5296 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5303 static struct isl_map
*isl_basic_map_partial_lexopt(
5304 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5305 struct isl_set
**empty
, int max
)
5309 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5310 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5312 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5314 isl_basic_map_free(bmap
);
5315 isl_basic_set_free(dom
);
5321 struct isl_map
*isl_basic_map_partial_lexmax(
5322 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5323 struct isl_set
**empty
)
5325 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5328 struct isl_map
*isl_basic_map_partial_lexmin(
5329 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5330 struct isl_set
**empty
)
5332 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5335 struct isl_set
*isl_basic_set_partial_lexmin(
5336 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5337 struct isl_set
**empty
)
5339 return (struct isl_set
*)
5340 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5344 struct isl_set
*isl_basic_set_partial_lexmax(
5345 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5346 struct isl_set
**empty
)
5348 return (struct isl_set
*)
5349 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5353 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5354 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5355 __isl_give isl_set
**empty
)
5357 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5360 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5361 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5362 __isl_give isl_set
**empty
)
5364 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5367 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5368 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5369 __isl_give isl_set
**empty
)
5371 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5374 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5375 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5376 __isl_give isl_set
**empty
)
5378 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5381 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5382 __isl_take isl_basic_map
*bmap
, int max
)
5384 isl_basic_set
*dom
= NULL
;
5385 isl_space
*dom_space
;
5389 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5390 dom
= isl_basic_set_universe(dom_space
);
5391 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5393 isl_basic_map_free(bmap
);
5397 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5398 __isl_take isl_basic_map
*bmap
)
5400 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5403 /* Given a basic map "bmap", compute the lexicographically minimal
5404 * (or maximal) image element for each domain element in dom.
5405 * Set *empty to those elements in dom that do not have an image element.
5407 * We first make sure the basic sets in dom are disjoint and then
5408 * simply collect the results over each of the basic sets separately.
5409 * We could probably improve the efficiency a bit by moving the union
5410 * domain down into the parametric integer programming.
5412 static __isl_give isl_map
*basic_map_partial_lexopt(
5413 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5414 __isl_give isl_set
**empty
, int max
)
5417 struct isl_map
*res
;
5419 dom
= isl_set_make_disjoint(dom
);
5423 if (isl_set_plain_is_empty(dom
)) {
5424 res
= isl_map_empty_like_basic_map(bmap
);
5425 *empty
= isl_set_empty_like(dom
);
5427 isl_basic_map_free(bmap
);
5431 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5432 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5434 for (i
= 1; i
< dom
->n
; ++i
) {
5435 struct isl_map
*res_i
;
5436 struct isl_set
*empty_i
;
5438 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5439 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5441 res
= isl_map_union_disjoint(res
, res_i
);
5442 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5446 isl_basic_map_free(bmap
);
5451 isl_basic_map_free(bmap
);
5455 /* Given a map "map", compute the lexicographically minimal
5456 * (or maximal) image element for each domain element in dom.
5457 * Set *empty to those elements in dom that do not have an image element.
5459 * We first compute the lexicographically minimal or maximal element
5460 * in the first basic map. This results in a partial solution "res"
5461 * and a subset "todo" of dom that still need to be handled.
5462 * We then consider each of the remaining maps in "map" and successively
5463 * improve both "res" and "todo".
5465 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5466 * Assume we are computing the lexicographical maximum.
5467 * We first compute the lexicographically maximal element in basic map i.
5468 * This results in a partial solution res_i and a subset todo_i.
5469 * Then we combine these results with those obtain for the first k basic maps
5470 * to obtain a result that is valid for the first k+1 basic maps.
5471 * In particular, the set where there is no solution is the set where
5472 * there is no solution for the first k basic maps and also no solution
5473 * for the ith basic map, i.e.,
5475 * todo^i = todo^k * todo_i
5477 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5478 * solutions, arbitrarily breaking ties in favor of res^k.
5479 * That is, when res^k(a) >= res_i(a), we pick res^k and
5480 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5481 * the lexicographic order.)
5482 * In practice, we compute
5484 * res^k * (res_i . "<=")
5488 * res_i * (res^k . "<")
5490 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5491 * where only one of res^k and res_i provides a solution and we simply pick
5498 * Note that we only compute these intersections when dom(res^k) intersects
5499 * dom(res_i). Otherwise, the only effect of these intersections is to
5500 * potentially break up res^k and res_i into smaller pieces.
5501 * We want to avoid such splintering as much as possible.
5502 * In fact, an earlier implementation of this function would look for
5503 * better results in the domain of res^k and for extra results in todo^k,
5504 * but this would always result in a splintering according to todo^k,
5505 * even when the domain of basic map i is disjoint from the domains of
5506 * the previous basic maps.
5508 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5509 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5510 __isl_give isl_set
**empty
, int max
)
5513 struct isl_map
*res
;
5514 struct isl_set
*todo
;
5519 if (isl_map_plain_is_empty(map
)) {
5527 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5528 isl_set_copy(dom
), &todo
, max
);
5530 for (i
= 1; i
< map
->n
; ++i
) {
5534 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5536 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5537 isl_set_copy(dom
), &todo_i
, max
);
5540 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5541 le
= isl_map_lex_le(dim
);
5543 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5544 le
= isl_map_lex_ge(dim
);
5546 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5547 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5548 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5549 le
= isl_map_intersect(le
, isl_map_copy(res
));
5551 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5552 res
= isl_map_intersect_domain(res
,
5553 isl_set_copy(todo_i
));
5554 res_i
= isl_map_intersect_domain(res_i
,
5555 isl_set_copy(todo
));
5558 res
= isl_map_union_disjoint(res
, res_i
);
5559 res
= isl_map_union_disjoint(res
, lt
);
5560 res
= isl_map_union_disjoint(res
, le
);
5562 todo
= isl_set_intersect(todo
, todo_i
);
5582 /* Given a map "map", compute the lexicographically minimal
5583 * (or maximal) image element for each domain element in dom.
5584 * Set *empty to those elements in dom that do not have an image element.
5586 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5588 static __isl_give isl_map
*isl_map_partial_lexopt(
5589 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5590 __isl_give isl_set
**empty
, int max
)
5594 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5595 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5596 if (!isl_space_has_named_params(map
->dim
) ||
5597 !isl_space_has_named_params(dom
->dim
))
5598 isl_die(map
->ctx
, isl_error_invalid
,
5599 "unaligned unnamed parameters", goto error
);
5600 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5601 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5602 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5611 __isl_give isl_map
*isl_map_partial_lexmax(
5612 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5613 __isl_give isl_set
**empty
)
5615 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5618 __isl_give isl_map
*isl_map_partial_lexmin(
5619 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5620 __isl_give isl_set
**empty
)
5622 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5625 __isl_give isl_set
*isl_set_partial_lexmin(
5626 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5627 __isl_give isl_set
**empty
)
5629 return (struct isl_set
*)
5630 isl_map_partial_lexmin((struct isl_map
*)set
,
5634 __isl_give isl_set
*isl_set_partial_lexmax(
5635 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5636 __isl_give isl_set
**empty
)
5638 return (struct isl_set
*)
5639 isl_map_partial_lexmax((struct isl_map
*)set
,
5643 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5645 struct isl_basic_set
*dom
= NULL
;
5650 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5651 dom
= isl_basic_set_universe(dom_dim
);
5652 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5654 isl_basic_map_free(bmap
);
5658 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5660 return isl_basic_map_lexopt(bmap
, 0);
5663 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5665 return isl_basic_map_lexopt(bmap
, 1);
5668 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5670 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5673 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5675 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5678 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5680 struct isl_set
*dom
= NULL
;
5685 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5686 dom
= isl_set_universe(dom_dim
);
5687 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5693 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5695 return isl_map_lexopt(map
, 0);
5698 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5700 return isl_map_lexopt(map
, 1);
5703 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5705 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5708 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5710 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5713 /* Extract the first and only affine expression from list
5714 * and then add it to *pwaff with the given dom.
5715 * This domain is known to be disjoint from other domains
5716 * because of the way isl_basic_map_foreach_lexmax works.
5718 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5719 __isl_take isl_aff_list
*list
, void *user
)
5721 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5723 isl_pw_aff
**pwaff
= user
;
5724 isl_pw_aff
*pwaff_i
;
5726 if (isl_aff_list_n_aff(list
) != 1)
5727 isl_die(ctx
, isl_error_internal
,
5728 "expecting single element list", goto error
);
5730 aff
= isl_aff_list_get_aff(list
, 0);
5731 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5733 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5735 isl_aff_list_free(list
);
5739 isl_basic_set_free(dom
);
5740 isl_aff_list_free(list
);
5744 /* Given a basic map with one output dimension, compute the minimum or
5745 * maximum of that dimension as an isl_pw_aff.
5747 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5748 * call update_dim_opt on each leaf of the result.
5750 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5753 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5757 dim
= isl_space_from_domain(isl_space_domain(dim
));
5758 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5759 pwaff
= isl_pw_aff_empty(dim
);
5761 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5763 return isl_pw_aff_free(pwaff
);
5768 /* Compute the minimum or maximum of the given output dimension
5769 * as a function of the parameters and the input dimensions,
5770 * but independently of the other output dimensions.
5772 * We first project out the other output dimension and then compute
5773 * the "lexicographic" maximum in each basic map, combining the results
5774 * using isl_pw_aff_union_max.
5776 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5783 n_out
= isl_map_dim(map
, isl_dim_out
);
5784 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5785 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5790 isl_space
*dim
= isl_map_get_space(map
);
5791 dim
= isl_space_domain(isl_space_from_range(dim
));
5793 return isl_pw_aff_empty(dim
);
5796 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5797 for (i
= 1; i
< map
->n
; ++i
) {
5798 isl_pw_aff
*pwaff_i
;
5800 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5801 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5809 /* Compute the maximum of the given output dimension as a function of the
5810 * parameters and input dimensions, but independently of
5811 * the other output dimensions.
5813 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5815 return map_dim_opt(map
, pos
, 1);
5818 /* Compute the minimum or maximum of the given set dimension
5819 * as a function of the parameters,
5820 * but independently of the other set dimensions.
5822 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5825 return map_dim_opt(set
, pos
, max
);
5828 /* Compute the maximum of the given set dimension as a function of the
5829 * parameters, but independently of the other set dimensions.
5831 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5833 return set_dim_opt(set
, pos
, 1);
5836 /* Compute the minimum of the given set dimension as a function of the
5837 * parameters, but independently of the other set dimensions.
5839 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5841 return set_dim_opt(set
, pos
, 0);
5844 /* Apply a preimage specified by "mat" on the parameters of "bset".
5845 * bset is assumed to have only parameters and divs.
5847 static struct isl_basic_set
*basic_set_parameter_preimage(
5848 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5855 bset
->dim
= isl_space_cow(bset
->dim
);
5859 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5861 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5863 bset
->dim
->nparam
= 0;
5864 bset
->dim
->n_out
= nparam
;
5865 bset
= isl_basic_set_preimage(bset
, mat
);
5867 bset
->dim
->nparam
= bset
->dim
->n_out
;
5868 bset
->dim
->n_out
= 0;
5873 isl_basic_set_free(bset
);
5877 /* Apply a preimage specified by "mat" on the parameters of "set".
5878 * set is assumed to have only parameters and divs.
5880 static struct isl_set
*set_parameter_preimage(
5881 struct isl_set
*set
, struct isl_mat
*mat
)
5883 isl_space
*dim
= NULL
;
5889 dim
= isl_space_copy(set
->dim
);
5890 dim
= isl_space_cow(dim
);
5894 nparam
= isl_set_dim(set
, isl_dim_param
);
5896 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5899 dim
->n_out
= nparam
;
5900 isl_set_reset_space(set
, dim
);
5901 set
= isl_set_preimage(set
, mat
);
5904 dim
= isl_space_copy(set
->dim
);
5905 dim
= isl_space_cow(dim
);
5908 dim
->nparam
= dim
->n_out
;
5910 isl_set_reset_space(set
, dim
);
5913 isl_space_free(dim
);
5920 /* Intersect the basic set "bset" with the affine space specified by the
5921 * equalities in "eq".
5923 static struct isl_basic_set
*basic_set_append_equalities(
5924 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5932 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5937 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5938 for (i
= 0; i
< eq
->n_row
; ++i
) {
5939 k
= isl_basic_set_alloc_equality(bset
);
5942 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5943 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5947 bset
= isl_basic_set_gauss(bset
, NULL
);
5948 bset
= isl_basic_set_finalize(bset
);
5953 isl_basic_set_free(bset
);
5957 /* Intersect the set "set" with the affine space specified by the
5958 * equalities in "eq".
5960 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5968 for (i
= 0; i
< set
->n
; ++i
) {
5969 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5982 /* Project the given basic set onto its parameter domain, possibly introducing
5983 * new, explicit, existential variables in the constraints.
5984 * The input has parameters and (possibly implicit) existential variables.
5985 * The output has the same parameters, but only
5986 * explicit existentially quantified variables.
5988 * The actual projection is performed by pip, but pip doesn't seem
5989 * to like equalities very much, so we first remove the equalities
5990 * among the parameters by performing a variable compression on
5991 * the parameters. Afterward, an inverse transformation is performed
5992 * and the equalities among the parameters are inserted back in.
5994 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5998 struct isl_mat
*T
, *T2
;
5999 struct isl_set
*set
;
6000 unsigned nparam
, n_div
;
6002 bset
= isl_basic_set_cow(bset
);
6006 if (bset
->n_eq
== 0)
6007 return isl_basic_set_lexmin(bset
);
6009 isl_basic_set_gauss(bset
, NULL
);
6011 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6012 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6014 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6015 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6018 if (i
== bset
->n_eq
)
6019 return isl_basic_set_lexmin(bset
);
6021 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6023 eq
= isl_mat_cow(eq
);
6024 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6025 if (T
&& T
->n_col
== 0) {
6029 bset
= isl_basic_set_set_to_empty(bset
);
6030 return isl_set_from_basic_set(bset
);
6032 bset
= basic_set_parameter_preimage(bset
, T
);
6034 set
= isl_basic_set_lexmin(bset
);
6035 set
= set_parameter_preimage(set
, T2
);
6036 set
= set_append_equalities(set
, eq
);
6040 /* Compute an explicit representation for all the existentially
6041 * quantified variables.
6042 * The input and output dimensions are first turned into parameters.
6043 * compute_divs then returns a map with the same parameters and
6044 * no input or output dimensions and the dimension specification
6045 * is reset to that of the input.
6047 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6049 struct isl_basic_set
*bset
;
6050 struct isl_set
*set
;
6051 struct isl_map
*map
;
6052 isl_space
*dim
, *orig_dim
= NULL
;
6057 bmap
= isl_basic_map_cow(bmap
);
6061 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6062 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6063 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6064 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
6068 orig_dim
= bmap
->dim
;
6070 bset
= (struct isl_basic_set
*)bmap
;
6072 set
= parameter_compute_divs(bset
);
6073 map
= (struct isl_map
*)set
;
6074 map
= isl_map_reset_space(map
, orig_dim
);
6078 isl_basic_map_free(bmap
);
6082 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6090 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6091 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6092 if (isl_int_is_zero(bmap
->div
[i
][0]))
6094 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6100 static int map_divs_known(__isl_keep isl_map
*map
)
6107 for (i
= 0; i
< map
->n
; ++i
) {
6108 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6116 /* If bmap contains any unknown divs, then compute explicit
6117 * expressions for them. However, this computation may be
6118 * quite expensive, so first try to remove divs that aren't
6121 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6124 struct isl_map
*map
;
6126 known
= isl_basic_map_divs_known(bmap
);
6130 return isl_map_from_basic_map(bmap
);
6132 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6134 known
= isl_basic_map_divs_known(bmap
);
6138 return isl_map_from_basic_map(bmap
);
6140 map
= compute_divs(bmap
);
6143 isl_basic_map_free(bmap
);
6147 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6151 struct isl_map
*res
;
6158 known
= map_divs_known(map
);
6166 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6167 for (i
= 1 ; i
< map
->n
; ++i
) {
6169 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6170 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6171 res
= isl_map_union_disjoint(res
, r2
);
6173 res
= isl_map_union(res
, r2
);
6180 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6182 return (struct isl_set
*)
6183 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6186 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6188 return (struct isl_set
*)
6189 isl_map_compute_divs((struct isl_map
*)set
);
6192 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6195 struct isl_set
*set
;
6200 map
= isl_map_cow(map
);
6204 set
= (struct isl_set
*)map
;
6205 set
->dim
= isl_space_domain(set
->dim
);
6208 for (i
= 0; i
< map
->n
; ++i
) {
6209 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6213 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6214 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6221 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6222 __isl_take isl_map
*map2
)
6226 struct isl_map
*map
= NULL
;
6240 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6242 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6243 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6244 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6246 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6247 map1
->n
+ map2
->n
, flags
);
6250 for (i
= 0; i
< map1
->n
; ++i
) {
6251 map
= isl_map_add_basic_map(map
,
6252 isl_basic_map_copy(map1
->p
[i
]));
6256 for (i
= 0; i
< map2
->n
; ++i
) {
6257 map
= isl_map_add_basic_map(map
,
6258 isl_basic_map_copy(map2
->p
[i
]));
6272 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6273 __isl_take isl_map
*map2
)
6275 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6278 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6280 map1
= isl_map_union_disjoint(map1
, map2
);
6284 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6288 struct isl_set
*isl_set_union_disjoint(
6289 struct isl_set
*set1
, struct isl_set
*set2
)
6291 return (struct isl_set
*)
6292 isl_map_union_disjoint(
6293 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6296 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6298 return (struct isl_set
*)
6299 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6302 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6303 __isl_take isl_set
*set
)
6306 struct isl_map
*result
;
6312 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6313 isl_die(set
->ctx
, isl_error_invalid
,
6314 "parameters don't match", goto error
);
6316 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6317 !isl_map_compatible_range(map
, set
))
6318 isl_die(set
->ctx
, isl_error_invalid
,
6319 "incompatible spaces", goto error
);
6321 if (isl_set_plain_is_universe(set
)) {
6326 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6327 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6328 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6330 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6331 map
->n
* set
->n
, flags
);
6334 for (i
= 0; i
< map
->n
; ++i
)
6335 for (j
= 0; j
< set
->n
; ++j
) {
6336 result
= isl_map_add_basic_map(result
,
6337 isl_basic_map_intersect_range(
6338 isl_basic_map_copy(map
->p
[i
]),
6339 isl_basic_set_copy(set
->p
[j
])));
6352 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6353 __isl_take isl_set
*set
)
6355 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6358 struct isl_map
*isl_map_intersect_domain(
6359 struct isl_map
*map
, struct isl_set
*set
)
6361 return isl_map_reverse(
6362 isl_map_intersect_range(isl_map_reverse(map
), set
));
6365 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6366 __isl_take isl_map
*map2
)
6370 map1
= isl_map_reverse(map1
);
6371 map1
= isl_map_apply_range(map1
, map2
);
6372 return isl_map_reverse(map1
);
6379 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6380 __isl_take isl_map
*map2
)
6382 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6385 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6386 __isl_take isl_map
*map2
)
6388 isl_space
*dim_result
;
6389 struct isl_map
*result
;
6395 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6396 isl_space_copy(map2
->dim
));
6398 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6401 for (i
= 0; i
< map1
->n
; ++i
)
6402 for (j
= 0; j
< map2
->n
; ++j
) {
6403 result
= isl_map_add_basic_map(result
,
6404 isl_basic_map_apply_range(
6405 isl_basic_map_copy(map1
->p
[i
]),
6406 isl_basic_map_copy(map2
->p
[j
])));
6412 if (result
&& result
->n
<= 1)
6413 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6421 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6422 __isl_take isl_map
*map2
)
6424 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6428 * returns range - domain
6430 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6432 isl_space
*dims
, *target_dim
;
6433 struct isl_basic_set
*bset
;
6440 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6441 bmap
->dim
, isl_dim_out
),
6443 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6444 dim
= isl_basic_map_n_in(bmap
);
6445 nparam
= isl_basic_map_n_param(bmap
);
6446 bset
= isl_basic_set_from_basic_map(bmap
);
6447 bset
= isl_basic_set_cow(bset
);
6448 dims
= isl_basic_set_get_space(bset
);
6449 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6450 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6451 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6452 for (i
= 0; i
< dim
; ++i
) {
6453 int j
= isl_basic_map_alloc_equality(
6454 (struct isl_basic_map
*)bset
);
6457 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6458 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6459 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6460 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6462 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6463 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6466 isl_basic_map_free(bmap
);
6471 * returns range - domain
6473 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
6477 struct isl_set
*result
;
6482 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6483 map
->dim
, isl_dim_out
),
6485 dim
= isl_map_get_space(map
);
6486 dim
= isl_space_domain(dim
);
6487 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6490 for (i
= 0; i
< map
->n
; ++i
)
6491 result
= isl_set_add_basic_set(result
,
6492 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6501 * returns [domain -> range] -> range - domain
6503 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6504 __isl_take isl_basic_map
*bmap
)
6508 isl_basic_map
*domain
;
6512 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6513 isl_die(bmap
->ctx
, isl_error_invalid
,
6514 "domain and range don't match", goto error
);
6516 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6517 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6519 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6520 domain
= isl_basic_map_universe(dim
);
6522 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6523 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6524 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6526 total
= isl_basic_map_total_dim(bmap
);
6528 for (i
= 0; i
< n
; ++i
) {
6529 k
= isl_basic_map_alloc_equality(bmap
);
6532 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6533 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6534 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6535 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6538 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6539 return isl_basic_map_finalize(bmap
);
6541 isl_basic_map_free(bmap
);
6546 * returns [domain -> range] -> range - domain
6548 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6551 isl_space
*domain_dim
;
6556 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6557 isl_die(map
->ctx
, isl_error_invalid
,
6558 "domain and range don't match", goto error
);
6560 map
= isl_map_cow(map
);
6564 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6565 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6566 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6569 for (i
= 0; i
< map
->n
; ++i
) {
6570 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6574 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6581 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6583 struct isl_basic_map
*bmap
;
6591 nparam
= dims
->nparam
;
6593 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6597 for (i
= 0; i
< dim
; ++i
) {
6598 int j
= isl_basic_map_alloc_equality(bmap
);
6601 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6602 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6603 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6605 return isl_basic_map_finalize(bmap
);
6607 isl_basic_map_free(bmap
);
6611 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6615 if (dim
->n_in
!= dim
->n_out
)
6616 isl_die(dim
->ctx
, isl_error_invalid
,
6617 "number of input and output dimensions needs to be "
6618 "the same", goto error
);
6619 return basic_map_identity(dim
);
6621 isl_space_free(dim
);
6625 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6627 if (!model
|| !model
->dim
)
6629 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6632 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6634 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6637 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6639 if (!model
|| !model
->dim
)
6641 return isl_map_identity(isl_space_copy(model
->dim
));
6644 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6646 if (!model
|| !model
->dim
)
6648 return isl_map_identity(isl_space_copy(model
->dim
));
6651 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6653 isl_space
*dim
= isl_set_get_space(set
);
6655 id
= isl_map_identity(isl_space_map_from_set(dim
));
6656 return isl_map_intersect_range(id
, set
);
6659 /* Construct a basic set with all set dimensions having only non-negative
6662 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6663 __isl_take isl_space
*space
)
6668 struct isl_basic_set
*bset
;
6672 nparam
= space
->nparam
;
6674 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6677 for (i
= 0; i
< dim
; ++i
) {
6678 int k
= isl_basic_set_alloc_inequality(bset
);
6681 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6682 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6686 isl_basic_set_free(bset
);
6690 /* Construct the half-space x_pos >= 0.
6692 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6696 isl_basic_set
*nonneg
;
6698 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6699 k
= isl_basic_set_alloc_inequality(nonneg
);
6702 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6703 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6705 return isl_basic_set_finalize(nonneg
);
6707 isl_basic_set_free(nonneg
);
6711 /* Construct the half-space x_pos <= -1.
6713 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6718 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6719 k
= isl_basic_set_alloc_inequality(neg
);
6722 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6723 isl_int_set_si(neg
->ineq
[k
][0], -1);
6724 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6726 return isl_basic_set_finalize(neg
);
6728 isl_basic_set_free(neg
);
6732 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6733 enum isl_dim_type type
, unsigned first
, unsigned n
)
6736 isl_basic_set
*nonneg
;
6744 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6746 for (i
= 0; i
< n
; ++i
) {
6747 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6748 pos(set
->dim
, type
) + first
+ i
);
6749 neg
= neg_halfspace(isl_set_get_space(set
),
6750 pos(set
->dim
, type
) + first
+ i
);
6752 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6761 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6762 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6769 if (isl_set_plain_is_empty(set
)) {
6774 return fn(set
, signs
, user
);
6777 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6779 half
= isl_set_intersect(half
, isl_set_copy(set
));
6780 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6784 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6786 half
= isl_set_intersect(half
, set
);
6787 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6793 /* Call "fn" on the intersections of "set" with each of the orthants
6794 * (except for obviously empty intersections). The orthant is identified
6795 * by the signs array, with each entry having value 1 or -1 according
6796 * to the sign of the corresponding variable.
6798 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6799 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6809 if (isl_set_plain_is_empty(set
))
6812 nparam
= isl_set_dim(set
, isl_dim_param
);
6813 nvar
= isl_set_dim(set
, isl_dim_set
);
6815 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6817 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6825 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6827 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6830 int isl_basic_map_is_subset(
6831 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6834 struct isl_map
*map1
;
6835 struct isl_map
*map2
;
6837 if (!bmap1
|| !bmap2
)
6840 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6841 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6843 is_subset
= isl_map_is_subset(map1
, map2
);
6851 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
6852 __isl_keep isl_basic_set
*bset2
)
6854 return isl_basic_map_is_subset(bset1
, bset2
);
6857 int isl_basic_map_is_equal(
6858 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6862 if (!bmap1
|| !bmap2
)
6864 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6867 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6871 int isl_basic_set_is_equal(
6872 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6874 return isl_basic_map_is_equal(
6875 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6878 int isl_map_is_empty(struct isl_map
*map
)
6885 for (i
= 0; i
< map
->n
; ++i
) {
6886 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6895 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6897 return map
? map
->n
== 0 : -1;
6900 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6902 return isl_map_plain_is_empty(map
);
6905 int isl_set_plain_is_empty(struct isl_set
*set
)
6907 return set
? set
->n
== 0 : -1;
6910 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6912 return isl_set_plain_is_empty(set
);
6915 int isl_set_is_empty(struct isl_set
*set
)
6917 return isl_map_is_empty((struct isl_map
*)set
);
6920 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6925 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6928 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6933 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6936 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6942 is_subset
= isl_map_is_subset(map1
, map2
);
6945 is_subset
= isl_map_is_subset(map2
, map1
);
6949 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6951 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6954 int isl_basic_map_is_strict_subset(
6955 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6959 if (!bmap1
|| !bmap2
)
6961 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6964 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6965 if (is_subset
== -1)
6970 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6976 is_subset
= isl_map_is_subset(map1
, map2
);
6979 is_subset
= isl_map_is_subset(map2
, map1
);
6980 if (is_subset
== -1)
6985 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6987 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6990 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6994 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6997 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7001 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7004 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7011 for (i
= 0; i
< map
->n
; ++i
) {
7012 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7020 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7022 return isl_map_plain_is_universe((isl_map
*) set
);
7025 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7027 return isl_set_plain_is_universe(set
);
7030 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7032 struct isl_basic_set
*bset
= NULL
;
7033 struct isl_vec
*sample
= NULL
;
7040 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7043 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7044 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7045 copy
= isl_basic_map_remove_redundancies(copy
);
7046 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
7047 isl_basic_map_free(copy
);
7051 total
= 1 + isl_basic_map_total_dim(bmap
);
7052 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7053 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7059 isl_vec_free(bmap
->sample
);
7060 bmap
->sample
= NULL
;
7061 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7064 sample
= isl_basic_set_sample_vec(bset
);
7067 empty
= sample
->size
== 0;
7068 isl_vec_free(bmap
->sample
);
7069 bmap
->sample
= sample
;
7071 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7076 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7080 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7083 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7085 return isl_basic_map_plain_is_empty(bmap
);
7088 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7092 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7095 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7097 return isl_basic_set_plain_is_empty(bset
);
7100 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7102 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7105 struct isl_map
*isl_basic_map_union(
7106 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7108 struct isl_map
*map
;
7109 if (!bmap1
|| !bmap2
)
7112 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7114 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7117 map
= isl_map_add_basic_map(map
, bmap1
);
7118 map
= isl_map_add_basic_map(map
, bmap2
);
7121 isl_basic_map_free(bmap1
);
7122 isl_basic_map_free(bmap2
);
7126 struct isl_set
*isl_basic_set_union(
7127 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7129 return (struct isl_set
*)isl_basic_map_union(
7130 (struct isl_basic_map
*)bset1
,
7131 (struct isl_basic_map
*)bset2
);
7134 /* Order divs such that any div only depends on previous divs */
7135 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7143 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7145 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7147 if (isl_int_is_zero(bmap
->div
[i
][0]))
7149 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7153 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7159 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7161 return (struct isl_basic_set
*)
7162 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7165 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7172 for (i
= 0; i
< map
->n
; ++i
) {
7173 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7184 /* Apply the expansion computed by isl_merge_divs.
7185 * The expansion itself is given by "exp" while the resulting
7186 * list of divs is given by "div".
7188 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7189 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7194 bset
= isl_basic_set_cow(bset
);
7198 if (div
->n_row
< bset
->n_div
)
7199 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7200 "not an expansion", goto error
);
7202 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7203 div
->n_row
- bset
->n_div
, 0,
7204 2 * (div
->n_row
- bset
->n_div
));
7206 n_div
= bset
->n_div
;
7207 for (i
= n_div
; i
< div
->n_row
; ++i
)
7208 if (isl_basic_set_alloc_div(bset
) < 0)
7212 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7213 if (j
>= 0 && exp
[j
] == i
) {
7215 isl_basic_map_swap_div(bset
, i
, j
);
7218 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7219 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7227 isl_basic_set_free(bset
);
7232 /* Look for a div in dst that corresponds to the div "div" in src.
7233 * The divs before "div" in src and dst are assumed to be the same.
7235 * Returns -1 if no corresponding div was found and the position
7236 * of the corresponding div in dst otherwise.
7238 static int find_div(struct isl_basic_map
*dst
,
7239 struct isl_basic_map
*src
, unsigned div
)
7243 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7245 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7246 for (i
= div
; i
< dst
->n_div
; ++i
)
7247 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7248 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7249 dst
->n_div
- div
) == -1)
7254 struct isl_basic_map
*isl_basic_map_align_divs(
7255 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7258 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7263 if (src
->n_div
== 0)
7266 for (i
= 0; i
< src
->n_div
; ++i
)
7267 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7269 src
= isl_basic_map_order_divs(src
);
7270 dst
= isl_basic_map_cow(dst
);
7271 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7272 src
->n_div
, 0, 2 * src
->n_div
);
7275 for (i
= 0; i
< src
->n_div
; ++i
) {
7276 int j
= find_div(dst
, src
, i
);
7278 j
= isl_basic_map_alloc_div(dst
);
7281 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7282 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7283 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7287 isl_basic_map_swap_div(dst
, i
, j
);
7291 isl_basic_map_free(dst
);
7295 struct isl_basic_set
*isl_basic_set_align_divs(
7296 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7298 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7299 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7302 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7310 map
= isl_map_compute_divs(map
);
7311 map
= isl_map_cow(map
);
7315 for (i
= 1; i
< map
->n
; ++i
)
7316 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7317 for (i
= 1; i
< map
->n
; ++i
)
7318 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7320 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7324 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7326 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7329 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7330 __isl_take isl_map
*map
)
7334 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7335 map
= isl_map_intersect_domain(map
, set
);
7336 set
= isl_map_range(map
);
7344 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7345 __isl_take isl_map
*map
)
7347 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7350 /* There is no need to cow as removing empty parts doesn't change
7351 * the meaning of the set.
7353 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7360 for (i
= map
->n
- 1; i
>= 0; --i
)
7361 remove_if_empty(map
, i
);
7366 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7368 return (struct isl_set
*)
7369 isl_map_remove_empty_parts((struct isl_map
*)set
);
7372 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7374 struct isl_basic_map
*bmap
;
7375 if (!map
|| map
->n
== 0)
7377 bmap
= map
->p
[map
->n
-1];
7378 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7379 return isl_basic_map_copy(bmap
);
7382 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7384 return (struct isl_basic_set
*)
7385 isl_map_copy_basic_map((struct isl_map
*)set
);
7388 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7389 __isl_keep isl_basic_map
*bmap
)
7395 for (i
= map
->n
-1; i
>= 0; --i
) {
7396 if (map
->p
[i
] != bmap
)
7398 map
= isl_map_cow(map
);
7401 isl_basic_map_free(map
->p
[i
]);
7402 if (i
!= map
->n
-1) {
7403 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7404 map
->p
[i
] = map
->p
[map
->n
-1];
7415 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7416 struct isl_basic_set
*bset
)
7418 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7419 (struct isl_basic_map
*)bset
);
7422 /* Given two basic sets bset1 and bset2, compute the maximal difference
7423 * between the values of dimension pos in bset1 and those in bset2
7424 * for any common value of the parameters and dimensions preceding pos.
7426 static enum isl_lp_result
basic_set_maximal_difference_at(
7427 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7428 int pos
, isl_int
*opt
)
7431 struct isl_basic_map
*bmap1
= NULL
;
7432 struct isl_basic_map
*bmap2
= NULL
;
7433 struct isl_ctx
*ctx
;
7434 struct isl_vec
*obj
;
7437 unsigned dim1
, dim2
;
7438 enum isl_lp_result res
;
7440 if (!bset1
|| !bset2
)
7441 return isl_lp_error
;
7443 nparam
= isl_basic_set_n_param(bset1
);
7444 dim1
= isl_basic_set_n_dim(bset1
);
7445 dim2
= isl_basic_set_n_dim(bset2
);
7446 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7447 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7448 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7449 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7450 if (!bmap1
|| !bmap2
)
7452 bmap1
= isl_basic_map_cow(bmap1
);
7453 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7454 pos
, (dim1
- pos
) + (dim2
- pos
),
7455 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7456 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7459 total
= isl_basic_map_total_dim(bmap1
);
7461 obj
= isl_vec_alloc(ctx
, 1 + total
);
7462 isl_seq_clr(obj
->block
.data
, 1 + total
);
7463 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7464 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7467 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7469 isl_basic_map_free(bmap1
);
7473 isl_basic_map_free(bmap1
);
7474 isl_basic_map_free(bmap2
);
7475 return isl_lp_error
;
7478 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7479 * for any common value of the parameters and dimensions preceding pos
7480 * in both basic sets, the values of dimension pos in bset1 are
7481 * smaller or larger than those in bset2.
7484 * 1 if bset1 follows bset2
7485 * -1 if bset1 precedes bset2
7486 * 0 if bset1 and bset2 are incomparable
7487 * -2 if some error occurred.
7489 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7490 struct isl_basic_set
*bset2
, int pos
)
7493 enum isl_lp_result res
;
7498 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7500 if (res
== isl_lp_empty
)
7502 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7503 res
== isl_lp_unbounded
)
7505 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7514 /* Given two basic sets bset1 and bset2, check whether
7515 * for any common value of the parameters and dimensions preceding pos
7516 * there is a value of dimension pos in bset1 that is larger
7517 * than a value of the same dimension in bset2.
7520 * 1 if there exists such a pair
7521 * 0 if there is no such pair, but there is a pair of equal values
7523 * -2 if some error occurred.
7525 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7526 __isl_keep isl_basic_set
*bset2
, int pos
)
7529 enum isl_lp_result res
;
7534 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7536 if (res
== isl_lp_empty
)
7538 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7539 res
== isl_lp_unbounded
)
7541 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7543 else if (res
== isl_lp_ok
)
7552 /* Given two sets set1 and set2, check whether
7553 * for any common value of the parameters and dimensions preceding pos
7554 * there is a value of dimension pos in set1 that is larger
7555 * than a value of the same dimension in set2.
7558 * 1 if there exists such a pair
7559 * 0 if there is no such pair, but there is a pair of equal values
7561 * -2 if some error occurred.
7563 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7564 __isl_keep isl_set
*set2
, int pos
)
7572 for (i
= 0; i
< set1
->n
; ++i
)
7573 for (j
= 0; j
< set2
->n
; ++j
) {
7575 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7576 if (f
== 1 || f
== -2)
7585 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7586 unsigned pos
, isl_int
*val
)
7594 total
= isl_basic_map_total_dim(bmap
);
7595 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7596 for (; d
+1 > pos
; --d
)
7597 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7601 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7603 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7605 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7608 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7614 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7615 unsigned pos
, isl_int
*val
)
7627 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7630 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7631 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7632 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7633 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7637 isl_int_set(*val
, v
);
7643 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7644 unsigned pos
, isl_int
*val
)
7646 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7650 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7653 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7656 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7657 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7659 if (pos
>= isl_basic_map_dim(bmap
, type
))
7661 return isl_basic_map_plain_has_fixed_var(bmap
,
7662 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7665 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7666 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7668 if (pos
>= isl_map_dim(map
, type
))
7670 return isl_map_plain_has_fixed_var(map
,
7671 map_offset(map
, type
) - 1 + pos
, val
);
7674 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7675 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7677 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7680 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7681 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7683 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7686 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7687 * then return this fixed value in *val.
7689 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7690 unsigned dim
, isl_int
*val
)
7692 return isl_basic_set_plain_has_fixed_var(bset
,
7693 isl_basic_set_n_param(bset
) + dim
, val
);
7696 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7697 * then return this fixed value in *val.
7699 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7700 unsigned dim
, isl_int
*val
)
7702 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7705 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7706 unsigned dim
, isl_int
*val
)
7708 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7711 /* Check if input variable in has fixed value and if so and if val is not NULL,
7712 * then return this fixed value in *val.
7714 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7715 unsigned in
, isl_int
*val
)
7717 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7720 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7721 * and if val is not NULL, then return this lower bound in *val.
7723 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7724 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7726 int i
, i_eq
= -1, i_ineq
= -1;
7733 total
= isl_basic_set_total_dim(bset
);
7734 nparam
= isl_basic_set_n_param(bset
);
7735 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7736 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7742 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7743 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7745 if (i_eq
!= -1 || i_ineq
!= -1)
7749 if (i_eq
== -1 && i_ineq
== -1)
7751 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7752 /* The coefficient should always be one due to normalization. */
7753 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7755 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7757 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7758 total
- nparam
- dim
- 1) != -1)
7761 isl_int_neg(*val
, c
[0]);
7765 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7766 unsigned dim
, isl_int
*val
)
7778 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7782 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7784 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7785 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7787 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7791 isl_int_set(*val
, v
);
7802 /* uset_gist depends on constraints without existentially quantified
7803 * variables sorting first.
7805 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7807 const struct constraint
*c1
= (const struct constraint
*)p1
;
7808 const struct constraint
*c2
= (const struct constraint
*)p2
;
7810 unsigned size
= isl_min(c1
->size
, c2
->size
);
7812 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7813 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7818 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7821 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7822 struct isl_basic_map
*bmap
)
7825 struct constraint
*c
;
7830 total
= isl_basic_map_total_dim(bmap
);
7831 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7834 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7836 c
[i
].c
= bmap
->ineq
[i
];
7838 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7839 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7840 bmap
->ineq
[i
] = c
[i
].c
;
7844 isl_basic_map_free(bmap
);
7848 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7849 __isl_take isl_basic_set
*bset
)
7851 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7852 (struct isl_basic_map
*)bset
);
7855 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7859 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7861 bmap
= isl_basic_map_remove_redundancies(bmap
);
7862 bmap
= isl_basic_map_sort_constraints(bmap
);
7863 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7867 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7869 return (struct isl_basic_set
*)isl_basic_map_normalize(
7870 (struct isl_basic_map
*)bset
);
7873 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7874 const __isl_keep isl_basic_map
*bmap2
)
7881 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7882 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7883 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7884 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7885 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7886 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7887 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7888 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7889 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7890 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7891 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7893 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7895 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7897 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7898 return bmap1
->n_eq
- bmap2
->n_eq
;
7899 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7900 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7901 if (bmap1
->n_div
!= bmap2
->n_div
)
7902 return bmap1
->n_div
- bmap2
->n_div
;
7903 total
= isl_basic_map_total_dim(bmap1
);
7904 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7905 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7909 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7910 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7914 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7915 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7922 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7923 const __isl_keep isl_basic_set
*bset2
)
7925 return isl_basic_map_plain_cmp(bset1
, bset2
);
7928 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7929 const __isl_keep isl_set
*set2
)
7935 if (set1
->n
!= set2
->n
)
7936 return set1
->n
- set2
->n
;
7938 for (i
= 0; i
< set1
->n
; ++i
) {
7939 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7947 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7948 __isl_keep isl_basic_map
*bmap2
)
7950 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7953 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7954 __isl_keep isl_basic_set
*bset2
)
7956 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7957 (isl_basic_map
*)bset2
);
7960 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7962 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7963 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7965 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7968 /* We normalize in place, but if anything goes wrong we need
7969 * to return NULL, so we need to make sure we don't change the
7970 * meaning of any possible other copies of map.
7972 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7975 struct isl_basic_map
*bmap
;
7979 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7981 for (i
= 0; i
< map
->n
; ++i
) {
7982 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7985 isl_basic_map_free(map
->p
[i
]);
7988 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7989 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7990 map
= isl_map_remove_empty_parts(map
);
7993 for (i
= map
->n
- 1; i
>= 1; --i
) {
7994 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7996 isl_basic_map_free(map
->p
[i
-1]);
7997 for (j
= i
; j
< map
->n
; ++j
)
7998 map
->p
[j
-1] = map
->p
[j
];
8008 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8010 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8013 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8023 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8026 map1
= isl_map_copy(map1
);
8027 map2
= isl_map_copy(map2
);
8028 map1
= isl_map_normalize(map1
);
8029 map2
= isl_map_normalize(map2
);
8032 equal
= map1
->n
== map2
->n
;
8033 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8034 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8047 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8049 return isl_map_plain_is_equal(map1
, map2
);
8052 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8054 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8055 (struct isl_map
*)set2
);
8058 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8060 return isl_set_plain_is_equal(set1
, set2
);
8063 /* Return an interval that ranges from min to max (inclusive)
8065 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8066 isl_int min
, isl_int max
)
8069 struct isl_basic_set
*bset
= NULL
;
8071 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8075 k
= isl_basic_set_alloc_inequality(bset
);
8078 isl_int_set_si(bset
->ineq
[k
][1], 1);
8079 isl_int_neg(bset
->ineq
[k
][0], min
);
8081 k
= isl_basic_set_alloc_inequality(bset
);
8084 isl_int_set_si(bset
->ineq
[k
][1], -1);
8085 isl_int_set(bset
->ineq
[k
][0], max
);
8089 isl_basic_set_free(bset
);
8093 /* Return the Cartesian product of the basic sets in list (in the given order).
8095 __isl_give isl_basic_set
*isl_basic_set_list_product(
8096 __isl_take
struct isl_basic_set_list
*list
)
8104 struct isl_basic_set
*product
= NULL
;
8108 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8109 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8110 nparam
= isl_basic_set_n_param(list
->p
[0]);
8111 dim
= isl_basic_set_n_dim(list
->p
[0]);
8112 extra
= list
->p
[0]->n_div
;
8113 n_eq
= list
->p
[0]->n_eq
;
8114 n_ineq
= list
->p
[0]->n_ineq
;
8115 for (i
= 1; i
< list
->n
; ++i
) {
8116 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8117 isl_assert(list
->ctx
,
8118 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8119 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8120 extra
+= list
->p
[i
]->n_div
;
8121 n_eq
+= list
->p
[i
]->n_eq
;
8122 n_ineq
+= list
->p
[i
]->n_ineq
;
8124 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
8129 for (i
= 0; i
< list
->n
; ++i
) {
8130 isl_basic_set_add_constraints(product
,
8131 isl_basic_set_copy(list
->p
[i
]), dim
);
8132 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8134 isl_basic_set_list_free(list
);
8137 isl_basic_set_free(product
);
8138 isl_basic_set_list_free(list
);
8142 struct isl_basic_map
*isl_basic_map_product(
8143 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8145 isl_space
*dim_result
= NULL
;
8146 struct isl_basic_map
*bmap
;
8147 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8148 struct isl_dim_map
*dim_map1
, *dim_map2
;
8150 if (!bmap1
|| !bmap2
)
8153 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8154 bmap2
->dim
, isl_dim_param
), goto error
);
8155 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8156 isl_space_copy(bmap2
->dim
));
8158 in1
= isl_basic_map_n_in(bmap1
);
8159 in2
= isl_basic_map_n_in(bmap2
);
8160 out1
= isl_basic_map_n_out(bmap1
);
8161 out2
= isl_basic_map_n_out(bmap2
);
8162 nparam
= isl_basic_map_n_param(bmap1
);
8164 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8165 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8166 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8167 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8168 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8169 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8170 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8171 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8172 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8173 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8174 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8176 bmap
= isl_basic_map_alloc_space(dim_result
,
8177 bmap1
->n_div
+ bmap2
->n_div
,
8178 bmap1
->n_eq
+ bmap2
->n_eq
,
8179 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8180 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8181 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8182 bmap
= isl_basic_map_simplify(bmap
);
8183 return isl_basic_map_finalize(bmap
);
8185 isl_basic_map_free(bmap1
);
8186 isl_basic_map_free(bmap2
);
8190 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8191 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8193 isl_basic_map
*prod
;
8195 prod
= isl_basic_map_product(bmap1
, bmap2
);
8196 prod
= isl_basic_map_flatten(prod
);
8200 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8201 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8203 return isl_basic_map_flat_range_product(bset1
, bset2
);
8206 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8207 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8209 isl_space
*space_result
= NULL
;
8210 isl_basic_map
*bmap
;
8211 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8212 struct isl_dim_map
*dim_map1
, *dim_map2
;
8214 if (!bmap1
|| !bmap2
)
8217 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8218 isl_space_copy(bmap2
->dim
));
8220 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8221 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8222 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8223 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8225 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8226 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8227 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8228 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8229 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8230 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8231 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8232 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8233 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8234 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8235 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8237 bmap
= isl_basic_map_alloc_space(space_result
,
8238 bmap1
->n_div
+ bmap2
->n_div
,
8239 bmap1
->n_eq
+ bmap2
->n_eq
,
8240 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8241 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8242 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8243 bmap
= isl_basic_map_simplify(bmap
);
8244 return isl_basic_map_finalize(bmap
);
8246 isl_basic_map_free(bmap1
);
8247 isl_basic_map_free(bmap2
);
8251 __isl_give isl_basic_map
*isl_basic_map_range_product(
8252 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8254 isl_space
*dim_result
= NULL
;
8255 isl_basic_map
*bmap
;
8256 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8257 struct isl_dim_map
*dim_map1
, *dim_map2
;
8259 if (!bmap1
|| !bmap2
)
8262 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8263 isl_space_copy(bmap2
->dim
));
8265 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8266 out1
= isl_basic_map_n_out(bmap1
);
8267 out2
= isl_basic_map_n_out(bmap2
);
8268 nparam
= isl_basic_map_n_param(bmap1
);
8270 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8271 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8272 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8273 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8274 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8275 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8276 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8277 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8278 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8279 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8280 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8282 bmap
= isl_basic_map_alloc_space(dim_result
,
8283 bmap1
->n_div
+ bmap2
->n_div
,
8284 bmap1
->n_eq
+ bmap2
->n_eq
,
8285 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8286 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8287 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8288 bmap
= isl_basic_map_simplify(bmap
);
8289 return isl_basic_map_finalize(bmap
);
8291 isl_basic_map_free(bmap1
);
8292 isl_basic_map_free(bmap2
);
8296 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8297 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8299 isl_basic_map
*prod
;
8301 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8302 prod
= isl_basic_map_flatten_range(prod
);
8306 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8307 __isl_take isl_map
*map2
,
8308 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8309 __isl_take isl_space
*right
),
8310 __isl_give isl_basic_map
*(*basic_map_product
)(
8311 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8314 struct isl_map
*result
;
8320 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8321 map2
->dim
, isl_dim_param
), goto error
);
8323 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8324 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8325 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8327 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8328 isl_space_copy(map2
->dim
)),
8329 map1
->n
* map2
->n
, flags
);
8332 for (i
= 0; i
< map1
->n
; ++i
)
8333 for (j
= 0; j
< map2
->n
; ++j
) {
8334 struct isl_basic_map
*part
;
8335 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8336 isl_basic_map_copy(map2
->p
[j
]));
8337 if (isl_basic_map_is_empty(part
))
8338 isl_basic_map_free(part
);
8340 result
= isl_map_add_basic_map(result
, part
);
8353 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8355 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8356 __isl_take isl_map
*map2
)
8358 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8361 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8362 __isl_take isl_map
*map2
)
8364 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8367 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8369 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8370 __isl_take isl_map
*map2
)
8374 prod
= isl_map_product(map1
, map2
);
8375 prod
= isl_map_flatten(prod
);
8379 /* Given two set A and B, construct its Cartesian product A x B.
8381 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8383 return isl_map_range_product(set1
, set2
);
8386 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8387 __isl_take isl_set
*set2
)
8389 return isl_map_flat_range_product(set1
, set2
);
8392 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8394 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8395 __isl_take isl_map
*map2
)
8397 return map_product(map1
, map2
, &isl_space_domain_product
,
8398 &isl_basic_map_domain_product
);
8401 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8403 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8404 __isl_take isl_map
*map2
)
8406 return map_product(map1
, map2
, &isl_space_range_product
,
8407 &isl_basic_map_range_product
);
8410 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8411 __isl_take isl_map
*map2
)
8413 return isl_map_align_params_map_map_and(map1
, map2
,
8414 &map_domain_product_aligned
);
8417 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8418 __isl_take isl_map
*map2
)
8420 return isl_map_align_params_map_map_and(map1
, map2
,
8421 &map_range_product_aligned
);
8424 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8426 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8427 __isl_take isl_map
*map2
)
8431 prod
= isl_map_domain_product(map1
, map2
);
8432 prod
= isl_map_flatten_domain(prod
);
8436 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8438 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8439 __isl_take isl_map
*map2
)
8443 prod
= isl_map_range_product(map1
, map2
);
8444 prod
= isl_map_flatten_range(prod
);
8448 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8451 uint32_t hash
= isl_hash_init();
8456 bmap
= isl_basic_map_copy(bmap
);
8457 bmap
= isl_basic_map_normalize(bmap
);
8460 total
= isl_basic_map_total_dim(bmap
);
8461 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8462 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8464 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8465 isl_hash_hash(hash
, c_hash
);
8467 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8468 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8470 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8471 isl_hash_hash(hash
, c_hash
);
8473 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8474 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8476 if (isl_int_is_zero(bmap
->div
[i
][0]))
8478 isl_hash_byte(hash
, i
& 0xFF);
8479 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8480 isl_hash_hash(hash
, c_hash
);
8482 isl_basic_map_free(bmap
);
8486 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8488 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8491 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8498 map
= isl_map_copy(map
);
8499 map
= isl_map_normalize(map
);
8503 hash
= isl_hash_init();
8504 for (i
= 0; i
< map
->n
; ++i
) {
8506 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8507 isl_hash_hash(hash
, bmap_hash
);
8515 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8517 return isl_map_get_hash((isl_map
*)set
);
8520 /* Check if the value for dimension dim is completely determined
8521 * by the values of the other parameters and variables.
8522 * That is, check if dimension dim is involved in an equality.
8524 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8531 nparam
= isl_basic_set_n_param(bset
);
8532 for (i
= 0; i
< bset
->n_eq
; ++i
)
8533 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8538 /* Check if the value for dimension dim is completely determined
8539 * by the values of the other parameters and variables.
8540 * That is, check if dimension dim is involved in an equality
8541 * for each of the subsets.
8543 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8549 for (i
= 0; i
< set
->n
; ++i
) {
8551 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8558 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8560 return set
? set
->n
: 0;
8563 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8564 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8571 for (i
= 0; i
< map
->n
; ++i
)
8572 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8578 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8579 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8586 for (i
= 0; i
< set
->n
; ++i
)
8587 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8593 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8600 bset
= isl_basic_set_cow(bset
);
8604 dim
= isl_basic_set_get_space(bset
);
8605 dim
= isl_space_lift(dim
, bset
->n_div
);
8608 isl_space_free(bset
->dim
);
8610 bset
->extra
-= bset
->n_div
;
8613 bset
= isl_basic_set_finalize(bset
);
8617 isl_basic_set_free(bset
);
8621 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8627 set
= isl_set_align_divs(set
);
8632 set
= isl_set_cow(set
);
8636 n_div
= set
->p
[0]->n_div
;
8637 dim
= isl_set_get_space(set
);
8638 dim
= isl_space_lift(dim
, n_div
);
8641 isl_space_free(set
->dim
);
8644 for (i
= 0; i
< set
->n
; ++i
) {
8645 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8656 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8659 struct isl_basic_map
*bmap
;
8666 set
= isl_set_align_divs(set
);
8671 dim
= isl_set_get_space(set
);
8672 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8674 return isl_map_identity(isl_space_map_from_set(dim
));
8677 n_div
= set
->p
[0]->n_div
;
8678 dim
= isl_space_map_from_set(dim
);
8679 n_param
= isl_space_dim(dim
, isl_dim_param
);
8680 n_set
= isl_space_dim(dim
, isl_dim_in
);
8681 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8682 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8683 for (i
= 0; i
< n_set
; ++i
)
8684 bmap
= var_equal(bmap
, i
);
8686 total
= n_param
+ n_set
+ n_set
+ n_div
;
8687 for (i
= 0; i
< n_div
; ++i
) {
8688 k
= isl_basic_map_alloc_inequality(bmap
);
8691 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8692 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8693 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8694 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8695 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8696 set
->p
[0]->div
[i
][0]);
8698 l
= isl_basic_map_alloc_inequality(bmap
);
8701 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8702 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8703 set
->p
[0]->div
[i
][0]);
8704 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8708 bmap
= isl_basic_map_simplify(bmap
);
8709 bmap
= isl_basic_map_finalize(bmap
);
8710 return isl_map_from_basic_map(bmap
);
8713 isl_basic_map_free(bmap
);
8717 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8725 dim
= isl_basic_set_total_dim(bset
);
8726 size
+= bset
->n_eq
* (1 + dim
);
8727 size
+= bset
->n_ineq
* (1 + dim
);
8728 size
+= bset
->n_div
* (2 + dim
);
8733 int isl_set_size(__isl_keep isl_set
*set
)
8741 for (i
= 0; i
< set
->n
; ++i
)
8742 size
+= isl_basic_set_size(set
->p
[i
]);
8747 /* Check if there is any lower bound (if lower == 0) and/or upper
8748 * bound (if upper == 0) on the specified dim.
8750 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8751 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8758 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8760 pos
+= isl_basic_map_offset(bmap
, type
);
8762 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8763 if (isl_int_is_zero(bmap
->div
[i
][0]))
8765 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8769 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8770 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8773 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8774 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8781 return lower
&& upper
;
8784 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8785 enum isl_dim_type type
, unsigned pos
)
8787 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8790 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8791 enum isl_dim_type type
, unsigned pos
)
8793 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8796 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8797 enum isl_dim_type type
, unsigned pos
)
8799 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8802 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8803 enum isl_dim_type type
, unsigned pos
)
8810 for (i
= 0; i
< map
->n
; ++i
) {
8812 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8813 if (bounded
< 0 || !bounded
)
8820 /* Return 1 if the specified dim is involved in both an upper bound
8821 * and a lower bound.
8823 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8824 enum isl_dim_type type
, unsigned pos
)
8826 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8829 static int has_bound(__isl_keep isl_map
*map
,
8830 enum isl_dim_type type
, unsigned pos
,
8831 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8832 enum isl_dim_type type
, unsigned pos
))
8839 for (i
= 0; i
< map
->n
; ++i
) {
8841 bounded
= fn(map
->p
[i
], type
, pos
);
8842 if (bounded
< 0 || bounded
)
8849 /* Return 1 if the specified dim is involved in any lower bound.
8851 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8852 enum isl_dim_type type
, unsigned pos
)
8854 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8857 /* Return 1 if the specified dim is involved in any upper bound.
8859 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8860 enum isl_dim_type type
, unsigned pos
)
8862 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8865 /* For each of the "n" variables starting at "first", determine
8866 * the sign of the variable and put the results in the first "n"
8867 * elements of the array "signs".
8869 * 1 means that the variable is non-negative
8870 * -1 means that the variable is non-positive
8871 * 0 means the variable attains both positive and negative values.
8873 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8874 unsigned first
, unsigned n
, int *signs
)
8876 isl_vec
*bound
= NULL
;
8877 struct isl_tab
*tab
= NULL
;
8878 struct isl_tab_undo
*snap
;
8881 if (!bset
|| !signs
)
8884 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8885 tab
= isl_tab_from_basic_set(bset
, 0);
8889 isl_seq_clr(bound
->el
, bound
->size
);
8890 isl_int_set_si(bound
->el
[0], -1);
8892 snap
= isl_tab_snap(tab
);
8893 for (i
= 0; i
< n
; ++i
) {
8896 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8897 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8900 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8901 if (isl_tab_rollback(tab
, snap
) < 0)
8909 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8910 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8913 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8914 if (isl_tab_rollback(tab
, snap
) < 0)
8917 signs
[i
] = empty
? -1 : 0;
8921 isl_vec_free(bound
);
8925 isl_vec_free(bound
);
8929 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8930 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8932 if (!bset
|| !signs
)
8934 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8937 first
+= pos(bset
->dim
, type
) - 1;
8938 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8941 /* Check if the given basic map is obviously single-valued.
8942 * In particular, for each output dimension, check that there is
8943 * an equality that defines the output dimension in terms of
8944 * earlier dimensions.
8946 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8956 total
= 1 + isl_basic_map_total_dim(bmap
);
8957 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8958 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8960 for (i
= 0; i
< n_out
; ++i
) {
8961 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8962 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8964 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8965 total
- (o_out
+ i
+ 1)) == -1)
8968 if (j
>= bmap
->n_eq
)
8975 /* Check if the given basic map is single-valued.
8980 * and check if the result is a subset of the identity mapping.
8982 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8985 isl_basic_map
*test
;
8989 sv
= isl_basic_map_plain_is_single_valued(bmap
);
8993 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
8994 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
8996 space
= isl_basic_map_get_space(bmap
);
8997 space
= isl_space_map_from_set(isl_space_range(space
));
8998 id
= isl_basic_map_identity(space
);
9000 sv
= isl_basic_map_is_subset(test
, id
);
9002 isl_basic_map_free(test
);
9003 isl_basic_map_free(id
);
9008 /* Check if the given map is obviously single-valued.
9010 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9019 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9022 /* Check if the given map is single-valued.
9027 * and check if the result is a subset of the identity mapping.
9029 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9036 sv
= isl_map_plain_is_single_valued(map
);
9040 test
= isl_map_reverse(isl_map_copy(map
));
9041 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9043 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9044 id
= isl_map_identity(dim
);
9046 sv
= isl_map_is_subset(test
, id
);
9054 int isl_map_is_injective(__isl_keep isl_map
*map
)
9058 map
= isl_map_copy(map
);
9059 map
= isl_map_reverse(map
);
9060 in
= isl_map_is_single_valued(map
);
9066 /* Check if the given map is obviously injective.
9068 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9072 map
= isl_map_copy(map
);
9073 map
= isl_map_reverse(map
);
9074 in
= isl_map_plain_is_single_valued(map
);
9080 int isl_map_is_bijective(__isl_keep isl_map
*map
)
9084 sv
= isl_map_is_single_valued(map
);
9088 return isl_map_is_injective(map
);
9091 int isl_set_is_singleton(__isl_keep isl_set
*set
)
9093 return isl_map_is_single_valued((isl_map
*)set
);
9096 int isl_map_is_translation(__isl_keep isl_map
*map
)
9101 delta
= isl_map_deltas(isl_map_copy(map
));
9102 ok
= isl_set_is_singleton(delta
);
9103 isl_set_free(delta
);
9108 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
9110 if (isl_seq_first_non_zero(p
, pos
) != -1)
9112 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
9117 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
9126 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
9129 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
9130 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
9131 for (j
= 0; j
< nvar
; ++j
) {
9132 int lower
= 0, upper
= 0;
9133 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9134 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
9136 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
9142 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9143 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
9145 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
9147 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
9152 if (!lower
|| !upper
)
9159 int isl_set_is_box(__isl_keep isl_set
*set
)
9166 return isl_basic_set_is_box(set
->p
[0]);
9169 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9174 return isl_space_is_wrapping(bset
->dim
);
9177 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9182 return isl_space_is_wrapping(set
->dim
);
9185 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9187 bmap
= isl_basic_map_cow(bmap
);
9191 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9195 bmap
= isl_basic_map_finalize(bmap
);
9197 return (isl_basic_set
*)bmap
;
9199 isl_basic_map_free(bmap
);
9203 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9207 map
= isl_map_cow(map
);
9211 for (i
= 0; i
< map
->n
; ++i
) {
9212 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9216 map
->dim
= isl_space_wrap(map
->dim
);
9220 return (isl_set
*)map
;
9226 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9228 bset
= isl_basic_set_cow(bset
);
9232 bset
->dim
= isl_space_unwrap(bset
->dim
);
9236 bset
= isl_basic_set_finalize(bset
);
9238 return (isl_basic_map
*)bset
;
9240 isl_basic_set_free(bset
);
9244 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9251 if (!isl_set_is_wrapping(set
))
9252 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9255 set
= isl_set_cow(set
);
9259 for (i
= 0; i
< set
->n
; ++i
) {
9260 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9265 set
->dim
= isl_space_unwrap(set
->dim
);
9269 return (isl_map
*)set
;
9275 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9276 enum isl_dim_type type
)
9281 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9284 bmap
= isl_basic_map_cow(bmap
);
9288 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9292 bmap
= isl_basic_map_finalize(bmap
);
9296 isl_basic_map_free(bmap
);
9300 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9301 enum isl_dim_type type
)
9308 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9311 map
= isl_map_cow(map
);
9315 for (i
= 0; i
< map
->n
; ++i
) {
9316 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9320 map
->dim
= isl_space_reset(map
->dim
, type
);
9330 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9335 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9338 bmap
= isl_basic_map_cow(bmap
);
9342 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9346 bmap
= isl_basic_map_finalize(bmap
);
9350 isl_basic_map_free(bmap
);
9354 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9356 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9359 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9360 __isl_take isl_basic_map
*bmap
)
9365 if (!bmap
->dim
->nested
[0])
9368 bmap
= isl_basic_map_cow(bmap
);
9372 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9376 bmap
= isl_basic_map_finalize(bmap
);
9380 isl_basic_map_free(bmap
);
9384 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9385 __isl_take isl_basic_map
*bmap
)
9390 if (!bmap
->dim
->nested
[1])
9393 bmap
= isl_basic_map_cow(bmap
);
9397 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9401 bmap
= isl_basic_map_finalize(bmap
);
9405 isl_basic_map_free(bmap
);
9409 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9416 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9419 map
= isl_map_cow(map
);
9423 for (i
= 0; i
< map
->n
; ++i
) {
9424 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9428 map
->dim
= isl_space_flatten(map
->dim
);
9438 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9440 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9443 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9445 isl_space
*dim
, *flat_dim
;
9448 dim
= isl_set_get_space(set
);
9449 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9450 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9451 map
= isl_map_intersect_domain(map
, set
);
9456 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9463 if (!map
->dim
->nested
[0])
9466 map
= isl_map_cow(map
);
9470 for (i
= 0; i
< map
->n
; ++i
) {
9471 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9475 map
->dim
= isl_space_flatten_domain(map
->dim
);
9485 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9492 if (!map
->dim
->nested
[1])
9495 map
= isl_map_cow(map
);
9499 for (i
= 0; i
< map
->n
; ++i
) {
9500 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9504 map
->dim
= isl_space_flatten_range(map
->dim
);
9514 /* Reorder the dimensions of "bmap" according to the given dim_map
9515 * and set the dimension specification to "dim".
9517 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9518 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9523 bmap
= isl_basic_map_cow(bmap
);
9524 if (!bmap
|| !dim
|| !dim_map
)
9527 flags
= bmap
->flags
;
9528 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
9529 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
9530 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
9531 res
= isl_basic_map_alloc_space(dim
,
9532 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9533 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9536 res
= isl_basic_map_finalize(res
);
9540 isl_basic_map_free(bmap
);
9541 isl_space_free(dim
);
9545 /* Reorder the dimensions of "map" according to given reordering.
9547 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9548 __isl_take isl_reordering
*r
)
9551 struct isl_dim_map
*dim_map
;
9553 map
= isl_map_cow(map
);
9554 dim_map
= isl_dim_map_from_reordering(r
);
9555 if (!map
|| !r
|| !dim_map
)
9558 for (i
= 0; i
< map
->n
; ++i
) {
9559 struct isl_dim_map
*dim_map_i
;
9561 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9563 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9564 isl_space_copy(r
->dim
), dim_map_i
);
9570 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9572 isl_reordering_free(r
);
9578 isl_reordering_free(r
);
9582 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9583 __isl_take isl_reordering
*r
)
9585 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9588 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9589 __isl_take isl_space
*model
)
9596 ctx
= isl_space_get_ctx(model
);
9597 if (!isl_space_has_named_params(model
))
9598 isl_die(ctx
, isl_error_invalid
,
9599 "model has unnamed parameters", goto error
);
9600 if (!isl_space_has_named_params(map
->dim
))
9601 isl_die(ctx
, isl_error_invalid
,
9602 "relation has unnamed parameters", goto error
);
9603 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9604 isl_reordering
*exp
;
9606 model
= isl_space_drop_dims(model
, isl_dim_in
,
9607 0, isl_space_dim(model
, isl_dim_in
));
9608 model
= isl_space_drop_dims(model
, isl_dim_out
,
9609 0, isl_space_dim(model
, isl_dim_out
));
9610 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9611 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9612 map
= isl_map_realign(map
, exp
);
9615 isl_space_free(model
);
9618 isl_space_free(model
);
9623 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9624 __isl_take isl_space
*model
)
9626 return isl_map_align_params(set
, model
);
9629 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9630 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9631 enum isl_dim_type c2
, enum isl_dim_type c3
,
9632 enum isl_dim_type c4
, enum isl_dim_type c5
)
9634 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9635 struct isl_mat
*mat
;
9641 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9642 isl_basic_map_total_dim(bmap
) + 1);
9645 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9646 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9647 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9648 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9649 isl_int_set(mat
->row
[i
][pos
],
9650 bmap
->eq
[i
][off
+ k
]);
9658 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9659 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9660 enum isl_dim_type c2
, enum isl_dim_type c3
,
9661 enum isl_dim_type c4
, enum isl_dim_type c5
)
9663 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9664 struct isl_mat
*mat
;
9670 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9671 isl_basic_map_total_dim(bmap
) + 1);
9674 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9675 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9676 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9677 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9678 isl_int_set(mat
->row
[i
][pos
],
9679 bmap
->ineq
[i
][off
+ k
]);
9687 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9688 __isl_take isl_space
*dim
,
9689 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9690 enum isl_dim_type c2
, enum isl_dim_type c3
,
9691 enum isl_dim_type c4
, enum isl_dim_type c5
)
9693 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9694 isl_basic_map
*bmap
;
9700 if (!dim
|| !eq
|| !ineq
)
9703 if (eq
->n_col
!= ineq
->n_col
)
9704 isl_die(dim
->ctx
, isl_error_invalid
,
9705 "equalities and inequalities matrices should have "
9706 "same number of columns", goto error
);
9708 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9710 if (eq
->n_col
< total
)
9711 isl_die(dim
->ctx
, isl_error_invalid
,
9712 "number of columns too small", goto error
);
9714 extra
= eq
->n_col
- total
;
9716 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9717 eq
->n_row
, ineq
->n_row
);
9720 for (i
= 0; i
< extra
; ++i
) {
9721 k
= isl_basic_map_alloc_div(bmap
);
9724 isl_int_set_si(bmap
->div
[k
][0], 0);
9726 for (i
= 0; i
< eq
->n_row
; ++i
) {
9727 l
= isl_basic_map_alloc_equality(bmap
);
9730 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9731 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9732 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9733 isl_int_set(bmap
->eq
[l
][off
+ k
],
9739 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9740 l
= isl_basic_map_alloc_inequality(bmap
);
9743 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9744 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9745 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9746 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9753 isl_space_free(dim
);
9759 isl_space_free(dim
);
9765 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9766 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9767 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9769 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9770 c1
, c2
, c3
, c4
, isl_dim_in
);
9773 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9774 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9775 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9777 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9778 c1
, c2
, c3
, c4
, isl_dim_in
);
9781 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9782 __isl_take isl_space
*dim
,
9783 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9784 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9786 return (isl_basic_set
*)
9787 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9788 c1
, c2
, c3
, c4
, isl_dim_in
);
9791 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9796 return isl_space_can_zip(bmap
->dim
);
9799 int isl_map_can_zip(__isl_keep isl_map
*map
)
9804 return isl_space_can_zip(map
->dim
);
9807 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9808 * (A -> C) -> (B -> D).
9810 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9819 if (!isl_basic_map_can_zip(bmap
))
9820 isl_die(bmap
->ctx
, isl_error_invalid
,
9821 "basic map cannot be zipped", goto error
);
9822 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9823 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9824 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9825 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9826 bmap
= isl_basic_map_cow(bmap
);
9827 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9830 bmap
->dim
= isl_space_zip(bmap
->dim
);
9835 isl_basic_map_free(bmap
);
9839 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9840 * (A -> C) -> (B -> D).
9842 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9849 if (!isl_map_can_zip(map
))
9850 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9853 map
= isl_map_cow(map
);
9857 for (i
= 0; i
< map
->n
; ++i
) {
9858 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9863 map
->dim
= isl_space_zip(map
->dim
);
9873 /* Can we apply isl_basic_map_curry to "bmap"?
9874 * That is, does it have a nested relation in its domain?
9876 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
9881 return isl_space_can_curry(bmap
->dim
);
9884 /* Can we apply isl_map_curry to "map"?
9885 * That is, does it have a nested relation in its domain?
9887 int isl_map_can_curry(__isl_keep isl_map
*map
)
9892 return isl_space_can_curry(map
->dim
);
9895 /* Given a basic map (A -> B) -> C, return the corresponding basic map
9898 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
9904 if (!isl_basic_map_can_curry(bmap
))
9905 isl_die(bmap
->ctx
, isl_error_invalid
,
9906 "basic map cannot be curried", goto error
);
9907 bmap
->dim
= isl_space_curry(bmap
->dim
);
9912 isl_basic_map_free(bmap
);
9916 /* Given a map (A -> B) -> C, return the corresponding map
9919 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
9926 if (!isl_map_can_curry(map
))
9927 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
9930 map
= isl_map_cow(map
);
9934 for (i
= 0; i
< map
->n
; ++i
) {
9935 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
9940 map
->dim
= isl_space_curry(map
->dim
);
9950 /* Construct a basic map mapping the domain of the affine expression
9951 * to a one-dimensional range prescribed by the affine expression.
9953 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9957 isl_local_space
*ls
;
9958 isl_basic_map
*bmap
;
9963 ls
= isl_aff_get_local_space(aff
);
9964 bmap
= isl_basic_map_from_local_space(ls
);
9965 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9966 k
= isl_basic_map_alloc_equality(bmap
);
9970 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9971 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9972 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9973 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9974 aff
->v
->size
- (pos
+ 1));
9977 bmap
= isl_basic_map_finalize(bmap
);
9981 isl_basic_map_free(bmap
);
9985 /* Construct a map mapping the domain of the affine expression
9986 * to a one-dimensional range prescribed by the affine expression.
9988 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
9990 isl_basic_map
*bmap
;
9992 bmap
= isl_basic_map_from_aff(aff
);
9993 return isl_map_from_basic_map(bmap
);
9996 /* Construct a basic map mapping the domain the multi-affine expression
9997 * to its range, with each dimension in the range equated to the
9998 * corresponding affine expression.
10000 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
10001 __isl_take isl_multi_aff
*maff
)
10005 isl_basic_map
*bmap
;
10010 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
10011 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
10012 "invalid space", return isl_multi_aff_free(maff
));
10014 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
10015 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
10017 for (i
= 0; i
< maff
->n
; ++i
) {
10019 isl_basic_map
*bmap_i
;
10021 aff
= isl_aff_copy(maff
->p
[i
]);
10022 bmap_i
= isl_basic_map_from_aff(aff
);
10024 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10027 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
10029 isl_multi_aff_free(maff
);
10033 /* Construct a map mapping the domain the multi-affine expression
10034 * to its range, with each dimension in the range equated to the
10035 * corresponding affine expression.
10037 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
10039 isl_basic_map
*bmap
;
10041 bmap
= isl_basic_map_from_multi_aff(maff
);
10042 return isl_map_from_basic_map(bmap
);
10045 /* Construct a basic map mapping a domain in the given space to
10046 * to an n-dimensional range, with n the number of elements in the list,
10047 * where each coordinate in the range is prescribed by the
10048 * corresponding affine expression.
10049 * The domains of all affine expressions in the list are assumed to match
10052 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
10053 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
10057 isl_basic_map
*bmap
;
10062 dim
= isl_space_from_domain(domain_dim
);
10063 bmap
= isl_basic_map_universe(dim
);
10065 for (i
= 0; i
< list
->n
; ++i
) {
10067 isl_basic_map
*bmap_i
;
10069 aff
= isl_aff_copy(list
->p
[i
]);
10070 bmap_i
= isl_basic_map_from_aff(aff
);
10072 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10075 isl_aff_list_free(list
);
10079 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
10080 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10082 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
10085 /* Construct a basic map where the given dimensions are equal to each other.
10087 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
10088 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10090 isl_basic_map
*bmap
= NULL
;
10096 if (pos1
>= isl_space_dim(space
, type1
))
10097 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10098 "index out of bounds", goto error
);
10099 if (pos2
>= isl_space_dim(space
, type2
))
10100 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10101 "index out of bounds", goto error
);
10103 if (type1
== type2
&& pos1
== pos2
)
10104 return isl_basic_map_universe(space
);
10106 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
10107 i
= isl_basic_map_alloc_equality(bmap
);
10110 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10111 pos1
+= isl_basic_map_offset(bmap
, type1
);
10112 pos2
+= isl_basic_map_offset(bmap
, type2
);
10113 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
10114 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10115 bmap
= isl_basic_map_finalize(bmap
);
10116 isl_space_free(space
);
10119 isl_space_free(space
);
10120 isl_basic_map_free(bmap
);
10124 /* Add a constraint imposing that the given two dimensions are equal.
10126 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
10127 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10131 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
10133 bmap
= isl_basic_map_intersect(bmap
, eq
);
10138 /* Add a constraint imposing that the given two dimensions are equal.
10140 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
10141 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10143 isl_basic_map
*bmap
;
10145 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
10147 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10152 /* Add a constraint imposing that the given two dimensions have opposite values.
10154 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
10155 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10157 isl_basic_map
*bmap
= NULL
;
10163 if (pos1
>= isl_map_dim(map
, type1
))
10164 isl_die(map
->ctx
, isl_error_invalid
,
10165 "index out of bounds", goto error
);
10166 if (pos2
>= isl_map_dim(map
, type2
))
10167 isl_die(map
->ctx
, isl_error_invalid
,
10168 "index out of bounds", goto error
);
10170 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
10171 i
= isl_basic_map_alloc_equality(bmap
);
10174 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10175 pos1
+= isl_basic_map_offset(bmap
, type1
);
10176 pos2
+= isl_basic_map_offset(bmap
, type2
);
10177 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
10178 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10179 bmap
= isl_basic_map_finalize(bmap
);
10181 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10185 isl_basic_map_free(bmap
);
10190 /* Add a constraint imposing that the value of the first dimension is
10191 * greater than that of the second.
10193 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
10194 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10196 isl_basic_map
*bmap
= NULL
;
10202 if (pos1
>= isl_map_dim(map
, type1
))
10203 isl_die(map
->ctx
, isl_error_invalid
,
10204 "index out of bounds", goto error
);
10205 if (pos2
>= isl_map_dim(map
, type2
))
10206 isl_die(map
->ctx
, isl_error_invalid
,
10207 "index out of bounds", goto error
);
10209 if (type1
== type2
&& pos1
== pos2
) {
10210 isl_space
*space
= isl_map_get_space(map
);
10212 return isl_map_empty(space
);
10215 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 0, 1);
10216 i
= isl_basic_map_alloc_inequality(bmap
);
10219 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10220 pos1
+= isl_basic_map_offset(bmap
, type1
);
10221 pos2
+= isl_basic_map_offset(bmap
, type2
);
10222 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
10223 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
10224 isl_int_set_si(bmap
->ineq
[i
][0], -1);
10225 bmap
= isl_basic_map_finalize(bmap
);
10227 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10231 isl_basic_map_free(bmap
);
10236 /* Add a constraint imposing that the value of the first dimension is
10237 * smaller than that of the second.
10239 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
10240 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10242 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
10245 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
10249 isl_local_space
*ls
;
10254 if (!isl_basic_map_divs_known(bmap
))
10255 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
10256 "some divs are unknown", return NULL
);
10258 ls
= isl_basic_map_get_local_space(bmap
);
10259 div
= isl_local_space_get_div(ls
, pos
);
10260 isl_local_space_free(ls
);
10265 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
10268 return isl_basic_map_get_div(bset
, pos
);
10271 /* Plug in "subs" for dimension "type", "pos" of "bset".
10273 * Let i be the dimension to replace and let "subs" be of the form
10277 * Any integer division with a non-zero coefficient for i,
10279 * floor((a i + g)/m)
10283 * floor((a f + d g)/(m d))
10285 * Constraints of the form
10293 __isl_give isl_basic_set
*isl_basic_set_substitute(
10294 __isl_take isl_basic_set
*bset
,
10295 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10301 if (bset
&& isl_basic_set_plain_is_empty(bset
))
10304 bset
= isl_basic_set_cow(bset
);
10305 if (!bset
|| !subs
)
10308 ctx
= isl_basic_set_get_ctx(bset
);
10309 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
10310 isl_die(ctx
, isl_error_invalid
,
10311 "spaces don't match", goto error
);
10312 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
10313 isl_die(ctx
, isl_error_unsupported
,
10314 "cannot handle divs yet", goto error
);
10316 pos
+= isl_basic_set_offset(bset
, type
);
10320 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10321 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
10323 isl_int_set(v
, bset
->eq
[i
][pos
]);
10324 isl_int_set_si(bset
->eq
[i
][pos
], 0);
10325 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
10326 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10329 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10330 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
10332 isl_int_set(v
, bset
->ineq
[i
][pos
]);
10333 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
10334 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
10335 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10338 for (i
= 0; i
< bset
->n_div
; ++i
) {
10339 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
10341 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
10342 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
10343 isl_seq_combine(bset
->div
[i
] + 1,
10344 subs
->v
->el
[0], bset
->div
[i
] + 1,
10345 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10346 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
10351 bset
= isl_basic_set_simplify(bset
);
10352 return isl_basic_set_finalize(bset
);
10354 isl_basic_set_free(bset
);
10358 /* Plug in "subs" for dimension "type", "pos" of "set".
10360 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
10361 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10365 if (set
&& isl_set_plain_is_empty(set
))
10368 set
= isl_set_cow(set
);
10372 for (i
= set
->n
- 1; i
>= 0; --i
) {
10373 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
10374 if (remove_if_empty(set
, i
) < 0)