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 using Fourier-Motzkin. The dimensions themselves
1600 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1601 enum isl_dim_type type
, unsigned first
, unsigned n
)
1610 map
= isl_map_cow(map
);
1614 for (i
= 0; i
< map
->n
; ++i
) {
1615 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1625 /* Eliminate the specified n dimensions starting at first from the
1626 * constraints using Fourier-Motzkin. The dimensions themselves
1629 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1630 enum isl_dim_type type
, unsigned first
, unsigned n
)
1632 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1635 /* Eliminate the specified n dimensions starting at first from the
1636 * constraints using Fourier-Motzkin. The dimensions themselves
1639 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1640 unsigned first
, unsigned n
)
1642 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1645 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1646 __isl_take isl_basic_map
*bmap
)
1650 bmap
= isl_basic_map_eliminate_vars(bmap
,
1651 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1655 return isl_basic_map_finalize(bmap
);
1658 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1659 __isl_take isl_basic_set
*bset
)
1661 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1662 (struct isl_basic_map
*)bset
);
1665 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1674 map
= isl_map_cow(map
);
1678 for (i
= 0; i
< map
->n
; ++i
) {
1679 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1689 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1691 return isl_map_remove_divs(set
);
1694 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1695 enum isl_dim_type type
, unsigned first
, unsigned n
)
1699 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1701 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1703 bmap
= isl_basic_map_eliminate_vars(bmap
,
1704 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1707 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1709 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1712 isl_basic_map_free(bmap
);
1716 /* Return true if the definition of the given div (recursively) involves
1717 * any of the given variables.
1719 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1720 unsigned first
, unsigned n
)
1723 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1725 if (isl_int_is_zero(bmap
->div
[div
][0]))
1727 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1730 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1731 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1733 if (div_involves_vars(bmap
, i
, first
, n
))
1740 /* Remove all divs (recursively) involving any of the given dimensions
1741 * in their definitions.
1743 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1744 __isl_take isl_basic_map
*bmap
,
1745 enum isl_dim_type type
, unsigned first
, unsigned n
)
1751 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1753 first
+= isl_basic_map_offset(bmap
, type
);
1755 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1756 if (!div_involves_vars(bmap
, i
, first
, n
))
1758 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1766 isl_basic_map_free(bmap
);
1770 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1771 enum isl_dim_type type
, unsigned first
, unsigned n
)
1780 map
= isl_map_cow(map
);
1784 for (i
= 0; i
< map
->n
; ++i
) {
1785 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1796 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1797 enum isl_dim_type type
, unsigned first
, unsigned n
)
1799 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1803 /* Does the desciption of "bmap" depend on the specified dimensions?
1804 * We also check whether the dimensions appear in any of the div definitions.
1805 * In principle there is no need for this check. If the dimensions appear
1806 * in a div definition, they also appear in the defining constraints of that
1809 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1810 enum isl_dim_type type
, unsigned first
, unsigned n
)
1817 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1818 isl_die(bmap
->ctx
, isl_error_invalid
,
1819 "index out of bounds", return -1);
1821 first
+= isl_basic_map_offset(bmap
, type
);
1822 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1823 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1825 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1826 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1828 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1829 if (isl_int_is_zero(bmap
->div
[i
][0]))
1831 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
1838 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1839 enum isl_dim_type type
, unsigned first
, unsigned n
)
1846 if (first
+ n
> isl_map_dim(map
, type
))
1847 isl_die(map
->ctx
, isl_error_invalid
,
1848 "index out of bounds", return -1);
1850 for (i
= 0; i
< map
->n
; ++i
) {
1851 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1853 if (involves
< 0 || involves
)
1860 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1861 enum isl_dim_type type
, unsigned first
, unsigned n
)
1863 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1866 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1867 enum isl_dim_type type
, unsigned first
, unsigned n
)
1869 return isl_map_involves_dims(set
, type
, first
, n
);
1872 /* Return true if the definition of the given div is unknown or depends
1875 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1878 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1880 if (isl_int_is_zero(bmap
->div
[div
][0]))
1883 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1884 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1886 if (div_is_unknown(bmap
, i
))
1893 /* Remove all divs that are unknown or defined in terms of unknown divs.
1895 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1896 __isl_take isl_basic_map
*bmap
)
1903 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1904 if (!div_is_unknown(bmap
, i
))
1906 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1915 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1924 map
= isl_map_cow(map
);
1928 for (i
= 0; i
< map
->n
; ++i
) {
1929 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1939 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1941 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1944 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1945 __isl_take isl_basic_set
*bset
,
1946 enum isl_dim_type type
, unsigned first
, unsigned n
)
1948 return (isl_basic_set
*)
1949 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1952 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1953 enum isl_dim_type type
, unsigned first
, unsigned n
)
1960 map
= isl_map_cow(map
);
1963 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1965 for (i
= 0; i
< map
->n
; ++i
) {
1966 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1967 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1971 map
= isl_map_drop(map
, type
, first
, n
);
1978 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1979 enum isl_dim_type type
, unsigned first
, unsigned n
)
1981 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1984 /* Project out n inputs starting at first using Fourier-Motzkin */
1985 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1986 unsigned first
, unsigned n
)
1988 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1991 static void dump_term(struct isl_basic_map
*bmap
,
1992 isl_int c
, int pos
, FILE *out
)
1995 unsigned in
= isl_basic_map_n_in(bmap
);
1996 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1997 unsigned nparam
= isl_basic_map_n_param(bmap
);
1999 isl_int_print(out
, c
, 0);
2001 if (!isl_int_is_one(c
))
2002 isl_int_print(out
, c
, 0);
2003 if (pos
< 1 + nparam
) {
2004 name
= isl_space_get_dim_name(bmap
->dim
,
2005 isl_dim_param
, pos
- 1);
2007 fprintf(out
, "%s", name
);
2009 fprintf(out
, "p%d", pos
- 1);
2010 } else if (pos
< 1 + nparam
+ in
)
2011 fprintf(out
, "i%d", pos
- 1 - nparam
);
2012 else if (pos
< 1 + nparam
+ dim
)
2013 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2015 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2019 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2020 int sign
, FILE *out
)
2024 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2028 for (i
= 0, first
= 1; i
< len
; ++i
) {
2029 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2032 fprintf(out
, " + ");
2034 isl_int_abs(v
, c
[i
]);
2035 dump_term(bmap
, v
, i
, out
);
2042 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2043 const char *op
, FILE *out
, int indent
)
2047 fprintf(out
, "%*s", indent
, "");
2049 dump_constraint_sign(bmap
, c
, 1, out
);
2050 fprintf(out
, " %s ", op
);
2051 dump_constraint_sign(bmap
, c
, -1, out
);
2055 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2056 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2058 fprintf(out
, "%*s", indent
, "");
2059 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2064 static void dump_constraints(struct isl_basic_map
*bmap
,
2065 isl_int
**c
, unsigned n
,
2066 const char *op
, FILE *out
, int indent
)
2070 for (i
= 0; i
< n
; ++i
)
2071 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2074 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2078 unsigned total
= isl_basic_map_total_dim(bmap
);
2080 for (j
= 0; j
< 1 + total
; ++j
) {
2081 if (isl_int_is_zero(exp
[j
]))
2083 if (!first
&& isl_int_is_pos(exp
[j
]))
2085 dump_term(bmap
, exp
[j
], j
, out
);
2090 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2094 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2095 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2097 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2098 fprintf(out
, "%*s", indent
, "");
2099 fprintf(out
, "e%d = [(", i
);
2100 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2102 isl_int_print(out
, bmap
->div
[i
][0], 0);
2103 fprintf(out
, "]\n");
2107 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2108 FILE *out
, int indent
)
2111 fprintf(out
, "null basic set\n");
2115 fprintf(out
, "%*s", indent
, "");
2116 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2117 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2118 bset
->extra
, bset
->flags
);
2119 dump((struct isl_basic_map
*)bset
, out
, indent
);
2122 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2123 FILE *out
, int indent
)
2126 fprintf(out
, "null basic map\n");
2130 fprintf(out
, "%*s", indent
, "");
2131 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2132 "flags: %x, n_name: %d\n",
2134 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2135 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2136 dump(bmap
, out
, indent
);
2139 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2144 total
= isl_basic_map_total_dim(bmap
);
2145 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2146 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2147 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2148 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2152 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2155 struct isl_set
*set
;
2159 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2160 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2161 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2162 sizeof(struct isl_set
) +
2163 (n
- 1) * sizeof(struct isl_basic_set
*));
2167 set
->ctx
= dim
->ctx
;
2168 isl_ctx_ref(set
->ctx
);
2176 isl_space_free(dim
);
2180 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2181 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2183 struct isl_set
*set
;
2186 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2190 set
= isl_set_alloc_space(dims
, n
, flags
);
2194 /* Make sure "map" has room for at least "n" more basic maps.
2196 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2199 struct isl_map
*grown
= NULL
;
2203 isl_assert(map
->ctx
, n
>= 0, goto error
);
2204 if (map
->n
+ n
<= map
->size
)
2206 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2209 for (i
= 0; i
< map
->n
; ++i
) {
2210 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2218 isl_map_free(grown
);
2223 /* Make sure "set" has room for at least "n" more basic sets.
2225 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2227 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2230 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2233 struct isl_set
*dup
;
2238 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2241 for (i
= 0; i
< set
->n
; ++i
)
2242 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2246 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2248 return isl_map_from_basic_map(bset
);
2251 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2253 struct isl_map
*map
;
2258 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2259 return isl_map_add_basic_map(map
, bmap
);
2262 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2263 __isl_take isl_basic_set
*bset
)
2265 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2266 (struct isl_basic_map
*)bset
);
2269 void isl_set_free(struct isl_set
*set
)
2279 isl_ctx_deref(set
->ctx
);
2280 for (i
= 0; i
< set
->n
; ++i
)
2281 isl_basic_set_free(set
->p
[i
]);
2282 isl_space_free(set
->dim
);
2286 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2291 fprintf(out
, "null set\n");
2295 fprintf(out
, "%*s", indent
, "");
2296 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2297 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2299 for (i
= 0; i
< set
->n
; ++i
) {
2300 fprintf(out
, "%*s", indent
, "");
2301 fprintf(out
, "basic set %d:\n", i
);
2302 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2306 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2311 fprintf(out
, "null map\n");
2315 fprintf(out
, "%*s", indent
, "");
2316 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2317 "flags: %x, n_name: %d\n",
2318 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2319 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2320 for (i
= 0; i
< map
->n
; ++i
) {
2321 fprintf(out
, "%*s", indent
, "");
2322 fprintf(out
, "basic map %d:\n", i
);
2323 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2327 struct isl_basic_map
*isl_basic_map_intersect_domain(
2328 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2330 struct isl_basic_map
*bmap_domain
;
2335 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2336 bset
->dim
, isl_dim_param
), goto error
);
2338 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2339 isl_assert(bset
->ctx
,
2340 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2342 bmap
= isl_basic_map_cow(bmap
);
2345 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2346 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2347 bmap_domain
= isl_basic_map_from_domain(bset
);
2348 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2350 bmap
= isl_basic_map_simplify(bmap
);
2351 return isl_basic_map_finalize(bmap
);
2353 isl_basic_map_free(bmap
);
2354 isl_basic_set_free(bset
);
2358 struct isl_basic_map
*isl_basic_map_intersect_range(
2359 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2361 struct isl_basic_map
*bmap_range
;
2366 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2367 bset
->dim
, isl_dim_param
), goto error
);
2369 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2370 isl_assert(bset
->ctx
,
2371 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2373 if (isl_basic_set_is_universe(bset
)) {
2374 isl_basic_set_free(bset
);
2378 bmap
= isl_basic_map_cow(bmap
);
2381 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2382 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2383 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2384 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2386 bmap
= isl_basic_map_simplify(bmap
);
2387 return isl_basic_map_finalize(bmap
);
2389 isl_basic_map_free(bmap
);
2390 isl_basic_set_free(bset
);
2394 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2400 total
= 1 + isl_basic_map_total_dim(bmap
);
2401 if (total
!= vec
->size
)
2406 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2407 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2408 if (!isl_int_is_zero(s
)) {
2414 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2415 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2416 if (isl_int_is_neg(s
)) {
2427 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2429 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2432 struct isl_basic_map
*isl_basic_map_intersect(
2433 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2435 struct isl_vec
*sample
= NULL
;
2437 if (!bmap1
|| !bmap2
)
2440 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2441 bmap2
->dim
, isl_dim_param
), goto error
);
2442 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2443 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2444 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2445 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2446 return isl_basic_map_intersect(bmap2
, bmap1
);
2448 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2449 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2450 isl_assert(bmap1
->ctx
,
2451 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2453 if (bmap1
->sample
&&
2454 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2455 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2456 sample
= isl_vec_copy(bmap1
->sample
);
2457 else if (bmap2
->sample
&&
2458 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2459 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2460 sample
= isl_vec_copy(bmap2
->sample
);
2462 bmap1
= isl_basic_map_cow(bmap1
);
2465 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2466 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2467 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2470 isl_vec_free(sample
);
2472 isl_vec_free(bmap1
->sample
);
2473 bmap1
->sample
= sample
;
2476 bmap1
= isl_basic_map_simplify(bmap1
);
2477 return isl_basic_map_finalize(bmap1
);
2480 isl_vec_free(sample
);
2481 isl_basic_map_free(bmap1
);
2482 isl_basic_map_free(bmap2
);
2486 struct isl_basic_set
*isl_basic_set_intersect(
2487 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2489 return (struct isl_basic_set
*)
2490 isl_basic_map_intersect(
2491 (struct isl_basic_map
*)bset1
,
2492 (struct isl_basic_map
*)bset2
);
2495 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2496 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2498 return isl_basic_set_intersect(bset1
, bset2
);
2501 /* Special case of isl_map_intersect, where both map1 and map2
2502 * are convex, without any divs and such that either map1 or map2
2503 * contains a single constraint. This constraint is then simply
2504 * added to the other map.
2506 static __isl_give isl_map
*map_intersect_add_constraint(
2507 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2509 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2510 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2511 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2512 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2514 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2515 return isl_map_intersect(map2
, map1
);
2517 isl_assert(map2
->ctx
,
2518 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2520 map1
= isl_map_cow(map1
);
2523 if (isl_map_plain_is_empty(map1
)) {
2527 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2528 if (map2
->p
[0]->n_eq
== 1)
2529 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2531 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2532 map2
->p
[0]->ineq
[0]);
2534 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2535 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2539 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2540 isl_basic_map_free(map1
->p
[0]);
2553 /* map2 may be either a parameter domain or a map living in the same
2556 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2557 __isl_take isl_map
*map2
)
2560 struct isl_map
*result
;
2566 if (isl_map_plain_is_empty(map1
) &&
2567 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2571 if (isl_map_plain_is_empty(map2
) &&
2572 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2577 if (map1
->n
== 1 && map2
->n
== 1 &&
2578 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2579 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2580 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2581 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2582 return map_intersect_add_constraint(map1
, map2
);
2584 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2585 isl_space_dim(map2
->dim
, isl_dim_param
))
2586 isl_assert(map1
->ctx
,
2587 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2589 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2590 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2591 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2593 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2594 map1
->n
* map2
->n
, flags
);
2597 for (i
= 0; i
< map1
->n
; ++i
)
2598 for (j
= 0; j
< map2
->n
; ++j
) {
2599 struct isl_basic_map
*part
;
2600 part
= isl_basic_map_intersect(
2601 isl_basic_map_copy(map1
->p
[i
]),
2602 isl_basic_map_copy(map2
->p
[j
]));
2603 if (isl_basic_map_is_empty(part
))
2604 isl_basic_map_free(part
);
2606 result
= isl_map_add_basic_map(result
, part
);
2619 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2620 __isl_take isl_map
*map2
)
2624 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2625 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2626 "spaces don't match", goto error
);
2627 return map_intersect_internal(map1
, map2
);
2634 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2635 __isl_take isl_map
*map2
)
2637 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2640 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2642 return (struct isl_set
*)
2643 isl_map_intersect((struct isl_map
*)set1
,
2644 (struct isl_map
*)set2
);
2647 /* map_intersect_internal accepts intersections
2648 * with parameter domains, so we can just call that function.
2650 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2651 __isl_take isl_set
*params
)
2653 return map_intersect_internal(map
, params
);
2656 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2657 __isl_take isl_map
*map2
)
2659 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2662 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2663 __isl_take isl_set
*params
)
2665 return isl_map_intersect_params(set
, params
);
2668 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2671 struct isl_basic_set
*bset
;
2676 bmap
= isl_basic_map_cow(bmap
);
2679 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2680 in
= isl_basic_map_n_in(bmap
);
2681 bset
= isl_basic_set_from_basic_map(bmap
);
2682 bset
= isl_basic_set_swap_vars(bset
, in
);
2683 return isl_basic_map_from_basic_set(bset
, dim
);
2686 static __isl_give isl_basic_map
*basic_map_space_reset(
2687 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2691 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2694 space
= isl_basic_map_get_space(bmap
);
2695 space
= isl_space_reset(space
, type
);
2696 bmap
= isl_basic_map_reset_space(bmap
, space
);
2700 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2701 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2704 struct isl_basic_map
*res
;
2705 struct isl_dim_map
*dim_map
;
2706 unsigned total
, off
;
2707 enum isl_dim_type t
;
2710 return basic_map_space_reset(bmap
, type
);
2715 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2717 total
= isl_basic_map_total_dim(bmap
) + n
;
2718 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2720 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2722 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2724 unsigned size
= isl_basic_map_dim(bmap
, t
);
2725 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2727 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2728 pos
, size
- pos
, off
+ pos
+ n
);
2730 off
+= isl_space_dim(res_dim
, t
);
2732 isl_dim_map_div(dim_map
, bmap
, off
);
2734 res
= isl_basic_map_alloc_space(res_dim
,
2735 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2736 if (isl_basic_map_is_rational(bmap
))
2737 res
= isl_basic_map_set_rational(res
);
2738 if (isl_basic_map_plain_is_empty(bmap
)) {
2739 isl_basic_map_free(bmap
);
2740 return isl_basic_map_set_to_empty(res
);
2742 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2743 return isl_basic_map_finalize(res
);
2746 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2747 enum isl_dim_type type
, unsigned n
)
2751 return isl_basic_map_insert(bmap
, type
,
2752 isl_basic_map_dim(bmap
, type
), n
);
2755 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2756 enum isl_dim_type type
, unsigned n
)
2760 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2761 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2763 isl_basic_set_free(bset
);
2767 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2768 enum isl_dim_type type
)
2772 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2775 space
= isl_map_get_space(map
);
2776 space
= isl_space_reset(space
, type
);
2777 map
= isl_map_reset_space(map
, space
);
2781 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2782 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2787 return map_space_reset(map
, type
);
2789 map
= isl_map_cow(map
);
2793 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2797 for (i
= 0; i
< map
->n
; ++i
) {
2798 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2809 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2810 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2812 return isl_map_insert_dims(set
, type
, pos
, n
);
2815 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2816 enum isl_dim_type type
, unsigned n
)
2820 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2823 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2824 enum isl_dim_type type
, unsigned n
)
2828 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2829 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2835 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2836 __isl_take isl_basic_map
*bmap
,
2837 enum isl_dim_type dst_type
, unsigned dst_pos
,
2838 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2840 struct isl_dim_map
*dim_map
;
2841 struct isl_basic_map
*res
;
2842 enum isl_dim_type t
;
2843 unsigned total
, off
;
2850 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2853 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2856 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2858 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2859 pos(bmap
->dim
, src_type
) + src_pos
+
2860 ((src_type
< dst_type
) ? n
: 0)) {
2861 bmap
= isl_basic_map_cow(bmap
);
2865 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2866 src_type
, src_pos
, n
);
2870 bmap
= isl_basic_map_finalize(bmap
);
2875 total
= isl_basic_map_total_dim(bmap
);
2876 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2879 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2880 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2881 if (t
== dst_type
) {
2882 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2885 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2888 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2889 dst_pos
, size
- dst_pos
, off
);
2890 off
+= size
- dst_pos
;
2891 } else if (t
== src_type
) {
2892 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2895 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2896 src_pos
+ n
, size
- src_pos
- n
, off
);
2897 off
+= size
- src_pos
- n
;
2899 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2903 isl_dim_map_div(dim_map
, bmap
, off
);
2905 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2906 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2907 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2909 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2910 src_type
, src_pos
, n
);
2914 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2915 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2916 bmap
= isl_basic_map_finalize(bmap
);
2920 isl_basic_map_free(bmap
);
2924 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2925 enum isl_dim_type dst_type
, unsigned dst_pos
,
2926 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2928 return (isl_basic_set
*)isl_basic_map_move_dims(
2929 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2932 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2933 enum isl_dim_type dst_type
, unsigned dst_pos
,
2934 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2938 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2939 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2940 src_type
, src_pos
, n
);
2946 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2947 enum isl_dim_type dst_type
, unsigned dst_pos
,
2948 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2957 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2960 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2963 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2965 map
= isl_map_cow(map
);
2969 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2973 for (i
= 0; i
< map
->n
; ++i
) {
2974 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2976 src_type
, src_pos
, n
);
2987 /* Move the specified dimensions to the last columns right before
2988 * the divs. Don't change the dimension specification of bmap.
2989 * That's the responsibility of the caller.
2991 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2992 enum isl_dim_type type
, unsigned first
, unsigned n
)
2994 struct isl_dim_map
*dim_map
;
2995 struct isl_basic_map
*res
;
2996 enum isl_dim_type t
;
2997 unsigned total
, off
;
3001 if (pos(bmap
->dim
, type
) + first
+ n
==
3002 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3005 total
= isl_basic_map_total_dim(bmap
);
3006 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3009 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3010 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3012 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3015 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3016 first
, n
, total
- bmap
->n_div
- n
);
3017 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3018 first
+ n
, size
- (first
+ n
), off
);
3019 off
+= size
- (first
+ n
);
3021 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3025 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3027 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3028 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3029 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3033 /* Turn the n dimensions of type type, starting at first
3034 * into existentially quantified variables.
3036 __isl_give isl_basic_map
*isl_basic_map_project_out(
3037 __isl_take isl_basic_map
*bmap
,
3038 enum isl_dim_type type
, unsigned first
, unsigned n
)
3046 return basic_map_space_reset(bmap
, type
);
3051 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3052 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3054 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3057 bmap
= move_last(bmap
, type
, first
, n
);
3058 bmap
= isl_basic_map_cow(bmap
);
3062 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3063 old
= bmap
->block2
.data
;
3064 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3065 (bmap
->extra
+ n
) * (1 + row_size
));
3066 if (!bmap
->block2
.data
)
3068 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3071 for (i
= 0; i
< n
; ++i
) {
3072 new_div
[i
] = bmap
->block2
.data
+
3073 (bmap
->extra
+ i
) * (1 + row_size
);
3074 isl_seq_clr(new_div
[i
], 1 + row_size
);
3076 for (i
= 0; i
< bmap
->extra
; ++i
)
3077 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3079 bmap
->div
= new_div
;
3083 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3086 bmap
= isl_basic_map_simplify(bmap
);
3087 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3088 return isl_basic_map_finalize(bmap
);
3090 isl_basic_map_free(bmap
);
3094 /* Turn the n dimensions of type type, starting at first
3095 * into existentially quantified variables.
3097 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3098 enum isl_dim_type type
, unsigned first
, unsigned n
)
3100 return (isl_basic_set
*)isl_basic_map_project_out(
3101 (isl_basic_map
*)bset
, type
, first
, n
);
3104 /* Turn the n dimensions of type type, starting at first
3105 * into existentially quantified variables.
3107 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3108 enum isl_dim_type type
, unsigned first
, unsigned n
)
3116 return map_space_reset(map
, type
);
3118 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3120 map
= isl_map_cow(map
);
3124 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3128 for (i
= 0; i
< map
->n
; ++i
) {
3129 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3140 /* Turn the n dimensions of type type, starting at first
3141 * into existentially quantified variables.
3143 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3144 enum isl_dim_type type
, unsigned first
, unsigned n
)
3146 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3149 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3153 for (i
= 0; i
< n
; ++i
) {
3154 j
= isl_basic_map_alloc_div(bmap
);
3157 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3161 isl_basic_map_free(bmap
);
3165 struct isl_basic_map
*isl_basic_map_apply_range(
3166 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3168 isl_space
*dim_result
= NULL
;
3169 struct isl_basic_map
*bmap
;
3170 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3171 struct isl_dim_map
*dim_map1
, *dim_map2
;
3173 if (!bmap1
|| !bmap2
)
3176 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3177 isl_space_copy(bmap2
->dim
));
3179 n_in
= isl_basic_map_n_in(bmap1
);
3180 n_out
= isl_basic_map_n_out(bmap2
);
3181 n
= isl_basic_map_n_out(bmap1
);
3182 nparam
= isl_basic_map_n_param(bmap1
);
3184 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3185 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3186 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3187 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3188 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3189 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3190 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3191 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3192 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3193 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3194 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3196 bmap
= isl_basic_map_alloc_space(dim_result
,
3197 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3198 bmap1
->n_eq
+ bmap2
->n_eq
,
3199 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3200 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3201 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3202 bmap
= add_divs(bmap
, n
);
3203 bmap
= isl_basic_map_simplify(bmap
);
3204 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3205 return isl_basic_map_finalize(bmap
);
3207 isl_basic_map_free(bmap1
);
3208 isl_basic_map_free(bmap2
);
3212 struct isl_basic_set
*isl_basic_set_apply(
3213 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3218 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3221 return (struct isl_basic_set
*)
3222 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3224 isl_basic_set_free(bset
);
3225 isl_basic_map_free(bmap
);
3229 struct isl_basic_map
*isl_basic_map_apply_domain(
3230 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3232 if (!bmap1
|| !bmap2
)
3235 isl_assert(bmap1
->ctx
,
3236 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3237 isl_assert(bmap1
->ctx
,
3238 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3241 bmap1
= isl_basic_map_reverse(bmap1
);
3242 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3243 return isl_basic_map_reverse(bmap1
);
3245 isl_basic_map_free(bmap1
);
3246 isl_basic_map_free(bmap2
);
3250 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3251 * A \cap B -> f(A) + f(B)
3253 struct isl_basic_map
*isl_basic_map_sum(
3254 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3256 unsigned n_in
, n_out
, nparam
, total
, pos
;
3257 struct isl_basic_map
*bmap
= NULL
;
3258 struct isl_dim_map
*dim_map1
, *dim_map2
;
3261 if (!bmap1
|| !bmap2
)
3264 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3267 nparam
= isl_basic_map_n_param(bmap1
);
3268 n_in
= isl_basic_map_n_in(bmap1
);
3269 n_out
= isl_basic_map_n_out(bmap1
);
3271 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3272 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3273 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3274 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3275 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3276 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3277 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3278 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3279 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3280 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3281 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3283 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3284 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3285 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3286 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3287 for (i
= 0; i
< n_out
; ++i
) {
3288 int j
= isl_basic_map_alloc_equality(bmap
);
3291 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3292 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3293 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3294 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3296 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3297 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3298 bmap
= add_divs(bmap
, 2 * n_out
);
3300 bmap
= isl_basic_map_simplify(bmap
);
3301 return isl_basic_map_finalize(bmap
);
3303 isl_basic_map_free(bmap
);
3304 isl_basic_map_free(bmap1
);
3305 isl_basic_map_free(bmap2
);
3309 /* Given two maps A -> f(A) and B -> g(B), construct a map
3310 * A \cap B -> f(A) + f(B)
3312 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3314 struct isl_map
*result
;
3320 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3322 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3323 map1
->n
* map2
->n
, 0);
3326 for (i
= 0; i
< map1
->n
; ++i
)
3327 for (j
= 0; j
< map2
->n
; ++j
) {
3328 struct isl_basic_map
*part
;
3329 part
= isl_basic_map_sum(
3330 isl_basic_map_copy(map1
->p
[i
]),
3331 isl_basic_map_copy(map2
->p
[j
]));
3332 if (isl_basic_map_is_empty(part
))
3333 isl_basic_map_free(part
);
3335 result
= isl_map_add_basic_map(result
, part
);
3348 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3349 __isl_take isl_set
*set2
)
3351 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3354 /* Given a basic map A -> f(A), construct A -> -f(A).
3356 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3361 bmap
= isl_basic_map_cow(bmap
);
3365 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3366 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3367 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3368 for (j
= 0; j
< n
; ++j
)
3369 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3370 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3371 for (j
= 0; j
< n
; ++j
)
3372 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3373 for (i
= 0; i
< bmap
->n_div
; ++i
)
3374 for (j
= 0; j
< n
; ++j
)
3375 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3376 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3377 return isl_basic_map_finalize(bmap
);
3380 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3382 return isl_basic_map_neg(bset
);
3385 /* Given a map A -> f(A), construct A -> -f(A).
3387 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3391 map
= isl_map_cow(map
);
3395 for (i
= 0; i
< map
->n
; ++i
) {
3396 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3407 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3409 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3412 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3413 * A -> floor(f(A)/d).
3415 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3418 unsigned n_in
, n_out
, nparam
, total
, pos
;
3419 struct isl_basic_map
*result
= NULL
;
3420 struct isl_dim_map
*dim_map
;
3426 nparam
= isl_basic_map_n_param(bmap
);
3427 n_in
= isl_basic_map_n_in(bmap
);
3428 n_out
= isl_basic_map_n_out(bmap
);
3430 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3431 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3432 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3433 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3434 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3435 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3437 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3438 bmap
->n_div
+ n_out
,
3439 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3440 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3441 result
= add_divs(result
, n_out
);
3442 for (i
= 0; i
< n_out
; ++i
) {
3444 j
= isl_basic_map_alloc_inequality(result
);
3447 isl_seq_clr(result
->ineq
[j
], 1+total
);
3448 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3449 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3450 j
= isl_basic_map_alloc_inequality(result
);
3453 isl_seq_clr(result
->ineq
[j
], 1+total
);
3454 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3455 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3456 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3459 result
= isl_basic_map_simplify(result
);
3460 return isl_basic_map_finalize(result
);
3462 isl_basic_map_free(result
);
3466 /* Given a map A -> f(A) and an integer d, construct a map
3467 * A -> floor(f(A)/d).
3469 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3473 map
= isl_map_cow(map
);
3477 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3478 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3479 for (i
= 0; i
< map
->n
; ++i
) {
3480 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3491 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3497 i
= isl_basic_map_alloc_equality(bmap
);
3500 nparam
= isl_basic_map_n_param(bmap
);
3501 n_in
= isl_basic_map_n_in(bmap
);
3502 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3503 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3504 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3505 return isl_basic_map_finalize(bmap
);
3507 isl_basic_map_free(bmap
);
3511 /* Add a constraints to "bmap" expressing i_pos < o_pos
3513 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3519 i
= isl_basic_map_alloc_inequality(bmap
);
3522 nparam
= isl_basic_map_n_param(bmap
);
3523 n_in
= isl_basic_map_n_in(bmap
);
3524 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3525 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3526 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3527 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3528 return isl_basic_map_finalize(bmap
);
3530 isl_basic_map_free(bmap
);
3534 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3536 static __isl_give isl_basic_map
*var_less_or_equal(
3537 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3543 i
= isl_basic_map_alloc_inequality(bmap
);
3546 nparam
= isl_basic_map_n_param(bmap
);
3547 n_in
= isl_basic_map_n_in(bmap
);
3548 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3549 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3550 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3551 return isl_basic_map_finalize(bmap
);
3553 isl_basic_map_free(bmap
);
3557 /* Add a constraints to "bmap" expressing i_pos > o_pos
3559 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3565 i
= isl_basic_map_alloc_inequality(bmap
);
3568 nparam
= isl_basic_map_n_param(bmap
);
3569 n_in
= isl_basic_map_n_in(bmap
);
3570 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3571 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3572 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3573 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3574 return isl_basic_map_finalize(bmap
);
3576 isl_basic_map_free(bmap
);
3580 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3582 static __isl_give isl_basic_map
*var_more_or_equal(
3583 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3589 i
= isl_basic_map_alloc_inequality(bmap
);
3592 nparam
= isl_basic_map_n_param(bmap
);
3593 n_in
= isl_basic_map_n_in(bmap
);
3594 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3595 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3596 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3597 return isl_basic_map_finalize(bmap
);
3599 isl_basic_map_free(bmap
);
3603 __isl_give isl_basic_map
*isl_basic_map_equal(
3604 __isl_take isl_space
*dim
, unsigned n_equal
)
3607 struct isl_basic_map
*bmap
;
3608 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3611 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3612 bmap
= var_equal(bmap
, i
);
3613 return isl_basic_map_finalize(bmap
);
3616 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3618 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3622 struct isl_basic_map
*bmap
;
3623 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3626 for (i
= 0; i
< pos
&& bmap
; ++i
)
3627 bmap
= var_equal(bmap
, i
);
3629 bmap
= var_less(bmap
, pos
);
3630 return isl_basic_map_finalize(bmap
);
3633 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3635 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3636 __isl_take isl_space
*dim
, unsigned pos
)
3639 isl_basic_map
*bmap
;
3641 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3642 for (i
= 0; i
< pos
; ++i
)
3643 bmap
= var_equal(bmap
, i
);
3644 bmap
= var_less_or_equal(bmap
, pos
);
3645 return isl_basic_map_finalize(bmap
);
3648 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3650 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3654 struct isl_basic_map
*bmap
;
3655 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3658 for (i
= 0; i
< pos
&& bmap
; ++i
)
3659 bmap
= var_equal(bmap
, i
);
3661 bmap
= var_more(bmap
, pos
);
3662 return isl_basic_map_finalize(bmap
);
3665 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3667 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3668 __isl_take isl_space
*dim
, unsigned pos
)
3671 isl_basic_map
*bmap
;
3673 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3674 for (i
= 0; i
< pos
; ++i
)
3675 bmap
= var_equal(bmap
, i
);
3676 bmap
= var_more_or_equal(bmap
, pos
);
3677 return isl_basic_map_finalize(bmap
);
3680 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3681 unsigned n
, int equal
)
3683 struct isl_map
*map
;
3686 if (n
== 0 && equal
)
3687 return isl_map_universe(dims
);
3689 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3691 for (i
= 0; i
+ 1 < n
; ++i
)
3692 map
= isl_map_add_basic_map(map
,
3693 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3696 map
= isl_map_add_basic_map(map
,
3697 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3699 map
= isl_map_add_basic_map(map
,
3700 isl_basic_map_less_at(dims
, n
- 1));
3702 isl_space_free(dims
);
3707 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3711 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3714 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3716 return map_lex_lte_first(dim
, n
, 0);
3719 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3721 return map_lex_lte_first(dim
, n
, 1);
3724 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3726 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3729 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3731 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3734 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3735 unsigned n
, int equal
)
3737 struct isl_map
*map
;
3740 if (n
== 0 && equal
)
3741 return isl_map_universe(dims
);
3743 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3745 for (i
= 0; i
+ 1 < n
; ++i
)
3746 map
= isl_map_add_basic_map(map
,
3747 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3750 map
= isl_map_add_basic_map(map
,
3751 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3753 map
= isl_map_add_basic_map(map
,
3754 isl_basic_map_more_at(dims
, n
- 1));
3756 isl_space_free(dims
);
3761 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3765 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3768 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3770 return map_lex_gte_first(dim
, n
, 0);
3773 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3775 return map_lex_gte_first(dim
, n
, 1);
3778 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3780 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3783 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3785 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3788 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3789 __isl_take isl_set
*set2
)
3792 map
= isl_map_lex_le(isl_set_get_space(set1
));
3793 map
= isl_map_intersect_domain(map
, set1
);
3794 map
= isl_map_intersect_range(map
, set2
);
3798 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3799 __isl_take isl_set
*set2
)
3802 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3803 map
= isl_map_intersect_domain(map
, set1
);
3804 map
= isl_map_intersect_range(map
, set2
);
3808 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3809 __isl_take isl_set
*set2
)
3812 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3813 map
= isl_map_intersect_domain(map
, set1
);
3814 map
= isl_map_intersect_range(map
, set2
);
3818 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3819 __isl_take isl_set
*set2
)
3822 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3823 map
= isl_map_intersect_domain(map
, set1
);
3824 map
= isl_map_intersect_range(map
, set2
);
3828 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3829 __isl_take isl_map
*map2
)
3832 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3833 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3834 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3838 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3839 __isl_take isl_map
*map2
)
3842 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3843 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3844 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3848 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3849 __isl_take isl_map
*map2
)
3852 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3853 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3854 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3858 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3859 __isl_take isl_map
*map2
)
3862 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3863 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3864 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3868 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3869 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3871 struct isl_basic_map
*bmap
;
3873 bset
= isl_basic_set_cow(bset
);
3877 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3878 isl_space_free(bset
->dim
);
3879 bmap
= (struct isl_basic_map
*) bset
;
3881 return isl_basic_map_finalize(bmap
);
3883 isl_basic_set_free(bset
);
3884 isl_space_free(dim
);
3888 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3892 if (bmap
->dim
->n_in
== 0)
3893 return (struct isl_basic_set
*)bmap
;
3894 bmap
= isl_basic_map_cow(bmap
);
3897 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3900 bmap
= isl_basic_map_finalize(bmap
);
3901 return (struct isl_basic_set
*)bmap
;
3903 isl_basic_map_free(bmap
);
3907 /* For a div d = floor(f/m), add the constraints
3910 * -(f-(n-1)) + m d >= 0
3912 * Note that the second constraint is the negation of
3916 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3917 unsigned pos
, isl_int
*div
)
3920 unsigned total
= isl_basic_map_total_dim(bmap
);
3922 i
= isl_basic_map_alloc_inequality(bmap
);
3925 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3926 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3928 j
= isl_basic_map_alloc_inequality(bmap
);
3931 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3932 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3933 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3937 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3938 unsigned pos
, isl_int
*div
)
3940 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3944 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3946 unsigned total
= isl_basic_map_total_dim(bmap
);
3947 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3949 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3953 struct isl_basic_set
*isl_basic_map_underlying_set(
3954 struct isl_basic_map
*bmap
)
3958 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3960 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3961 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3962 return (struct isl_basic_set
*)bmap
;
3963 bmap
= isl_basic_map_cow(bmap
);
3966 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3969 bmap
->extra
-= bmap
->n_div
;
3971 bmap
= isl_basic_map_finalize(bmap
);
3972 return (struct isl_basic_set
*)bmap
;
3977 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3978 __isl_take isl_basic_set
*bset
)
3980 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3983 struct isl_basic_map
*isl_basic_map_overlying_set(
3984 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3986 struct isl_basic_map
*bmap
;
3987 struct isl_ctx
*ctx
;
3994 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3995 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3996 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3998 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3999 isl_basic_map_free(like
);
4000 return (struct isl_basic_map
*)bset
;
4002 bset
= isl_basic_set_cow(bset
);
4005 total
= bset
->dim
->n_out
+ bset
->extra
;
4006 bmap
= (struct isl_basic_map
*)bset
;
4007 isl_space_free(bmap
->dim
);
4008 bmap
->dim
= isl_space_copy(like
->dim
);
4011 bmap
->n_div
= like
->n_div
;
4012 bmap
->extra
+= like
->n_div
;
4016 ltotal
= total
- bmap
->extra
+ like
->extra
;
4019 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4020 bmap
->extra
* (1 + 1 + total
));
4021 if (isl_blk_is_error(bmap
->block2
))
4023 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4027 for (i
= 0; i
< bmap
->extra
; ++i
)
4028 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4029 for (i
= 0; i
< like
->n_div
; ++i
) {
4030 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4031 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4033 bmap
= isl_basic_map_extend_constraints(bmap
,
4034 0, 2 * like
->n_div
);
4035 for (i
= 0; i
< like
->n_div
; ++i
) {
4036 if (isl_int_is_zero(bmap
->div
[i
][0]))
4038 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4042 isl_basic_map_free(like
);
4043 bmap
= isl_basic_map_simplify(bmap
);
4044 bmap
= isl_basic_map_finalize(bmap
);
4047 isl_basic_map_free(like
);
4048 isl_basic_set_free(bset
);
4052 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4053 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4055 return (struct isl_basic_set
*)
4056 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4059 struct isl_set
*isl_set_from_underlying_set(
4060 struct isl_set
*set
, struct isl_basic_set
*like
)
4066 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4068 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4069 isl_basic_set_free(like
);
4072 set
= isl_set_cow(set
);
4075 for (i
= 0; i
< set
->n
; ++i
) {
4076 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4077 isl_basic_set_copy(like
));
4081 isl_space_free(set
->dim
);
4082 set
->dim
= isl_space_copy(like
->dim
);
4085 isl_basic_set_free(like
);
4088 isl_basic_set_free(like
);
4093 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4097 map
= isl_map_cow(map
);
4100 map
->dim
= isl_space_cow(map
->dim
);
4104 for (i
= 1; i
< map
->n
; ++i
)
4105 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4107 for (i
= 0; i
< map
->n
; ++i
) {
4108 map
->p
[i
] = (struct isl_basic_map
*)
4109 isl_basic_map_underlying_set(map
->p
[i
]);
4114 map
->dim
= isl_space_underlying(map
->dim
, 0);
4116 isl_space_free(map
->dim
);
4117 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4121 return (struct isl_set
*)map
;
4127 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4129 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4132 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4133 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4135 bmap
= isl_basic_map_cow(bmap
);
4139 isl_space_free(bmap
->dim
);
4142 bmap
= isl_basic_map_finalize(bmap
);
4146 isl_basic_map_free(bmap
);
4147 isl_space_free(dim
);
4151 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4152 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4154 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4158 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4159 __isl_take isl_space
*dim
)
4163 map
= isl_map_cow(map
);
4167 for (i
= 0; i
< map
->n
; ++i
) {
4168 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4169 isl_space_copy(dim
));
4173 isl_space_free(map
->dim
);
4179 isl_space_free(dim
);
4183 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4184 __isl_take isl_space
*dim
)
4186 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4189 /* Compute the parameter domain of the given basic set.
4191 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4196 if (isl_basic_set_is_params(bset
))
4199 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4200 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4201 space
= isl_basic_set_get_space(bset
);
4202 space
= isl_space_params(space
);
4203 bset
= isl_basic_set_reset_space(bset
, space
);
4207 /* Compute the parameter domain of the given set.
4209 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4214 if (isl_set_is_params(set
))
4217 n
= isl_set_dim(set
, isl_dim_set
);
4218 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4219 space
= isl_set_get_space(set
);
4220 space
= isl_space_params(space
);
4221 set
= isl_set_reset_space(set
, space
);
4225 /* Construct a zero-dimensional set with the given parameter domain.
4227 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4230 space
= isl_set_get_space(set
);
4231 space
= isl_space_set_from_params(space
);
4232 set
= isl_set_reset_space(set
, space
);
4236 /* Compute the parameter domain of the given map.
4238 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4243 n
= isl_map_dim(map
, isl_dim_in
);
4244 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4245 n
= isl_map_dim(map
, isl_dim_out
);
4246 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4247 space
= isl_map_get_space(map
);
4248 space
= isl_space_params(space
);
4249 map
= isl_map_reset_space(map
, space
);
4253 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4256 struct isl_basic_set
*domain
;
4262 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4264 n_in
= isl_basic_map_n_in(bmap
);
4265 n_out
= isl_basic_map_n_out(bmap
);
4266 domain
= isl_basic_set_from_basic_map(bmap
);
4267 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4269 domain
= isl_basic_set_reset_space(domain
, dim
);
4274 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4278 return isl_space_may_be_set(bmap
->dim
);
4281 /* Is this basic map actually a set?
4282 * Users should never call this function. Outside of isl,
4283 * the type should indicate whether something is a set or a map.
4285 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4289 return isl_space_is_set(bmap
->dim
);
4292 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4296 if (isl_basic_map_is_set(bmap
))
4298 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4301 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4302 __isl_take isl_basic_map
*bmap
)
4306 isl_basic_map
*domain
;
4307 int nparam
, n_in
, n_out
;
4310 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4311 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4312 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4314 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4315 domain
= isl_basic_map_universe(dim
);
4317 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4318 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4319 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4321 total
= isl_basic_map_total_dim(bmap
);
4323 for (i
= 0; i
< n_in
; ++i
) {
4324 k
= isl_basic_map_alloc_equality(bmap
);
4327 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4328 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4329 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4332 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4333 return isl_basic_map_finalize(bmap
);
4335 isl_basic_map_free(bmap
);
4339 __isl_give isl_basic_map
*isl_basic_map_range_map(
4340 __isl_take isl_basic_map
*bmap
)
4344 isl_basic_map
*range
;
4345 int nparam
, n_in
, n_out
;
4348 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4349 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4350 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4352 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4353 range
= isl_basic_map_universe(dim
);
4355 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4356 bmap
= isl_basic_map_apply_range(bmap
, range
);
4357 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4359 total
= isl_basic_map_total_dim(bmap
);
4361 for (i
= 0; i
< n_out
; ++i
) {
4362 k
= isl_basic_map_alloc_equality(bmap
);
4365 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4366 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4367 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4370 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4371 return isl_basic_map_finalize(bmap
);
4373 isl_basic_map_free(bmap
);
4377 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4381 return isl_space_may_be_set(map
->dim
);
4384 /* Is this map actually a set?
4385 * Users should never call this function. Outside of isl,
4386 * the type should indicate whether something is a set or a map.
4388 int isl_map_is_set(__isl_keep isl_map
*map
)
4392 return isl_space_is_set(map
->dim
);
4395 struct isl_set
*isl_map_range(struct isl_map
*map
)
4398 struct isl_set
*set
;
4402 if (isl_map_is_set(map
))
4403 return (isl_set
*)map
;
4405 map
= isl_map_cow(map
);
4409 set
= (struct isl_set
*) map
;
4410 set
->dim
= isl_space_range(set
->dim
);
4413 for (i
= 0; i
< map
->n
; ++i
) {
4414 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4418 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4419 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4426 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4429 isl_space
*domain_dim
;
4431 map
= isl_map_cow(map
);
4435 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4436 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4437 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4440 for (i
= 0; i
< map
->n
; ++i
) {
4441 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4445 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4446 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4453 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4456 isl_space
*range_dim
;
4458 map
= isl_map_cow(map
);
4462 range_dim
= isl_space_range(isl_map_get_space(map
));
4463 range_dim
= isl_space_from_range(range_dim
);
4464 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4465 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4468 for (i
= 0; i
< map
->n
; ++i
) {
4469 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4473 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4474 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4481 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4482 __isl_take isl_space
*dim
)
4485 struct isl_map
*map
= NULL
;
4487 set
= isl_set_cow(set
);
4490 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4491 map
= (struct isl_map
*)set
;
4492 for (i
= 0; i
< set
->n
; ++i
) {
4493 map
->p
[i
] = isl_basic_map_from_basic_set(
4494 set
->p
[i
], isl_space_copy(dim
));
4498 isl_space_free(map
->dim
);
4502 isl_space_free(dim
);
4507 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4508 __isl_take isl_basic_set
*bset
)
4510 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4513 __isl_give isl_basic_map
*isl_basic_map_from_range(
4514 __isl_take isl_basic_set
*bset
)
4517 space
= isl_basic_set_get_space(bset
);
4518 space
= isl_space_from_range(space
);
4519 bset
= isl_basic_set_reset_space(bset
, space
);
4520 return (isl_basic_map
*)bset
;
4523 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4526 space
= isl_set_get_space(set
);
4527 space
= isl_space_from_range(space
);
4528 set
= isl_set_reset_space(set
, space
);
4529 return (struct isl_map
*)set
;
4532 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4534 return isl_map_reverse(isl_map_from_range(set
));
4537 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4538 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4540 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4543 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4544 __isl_take isl_set
*range
)
4546 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4549 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4552 struct isl_set
*set
= NULL
;
4556 map
= isl_map_cow(map
);
4559 map
->dim
= isl_space_as_set_space(map
->dim
);
4562 set
= (struct isl_set
*)map
;
4563 for (i
= 0; i
< map
->n
; ++i
) {
4564 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4574 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4577 struct isl_map
*map
;
4582 isl_die(dim
->ctx
, isl_error_internal
,
4583 "negative number of basic maps", goto error
);
4584 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4585 sizeof(struct isl_map
) +
4586 (n
- 1) * sizeof(struct isl_basic_map
*));
4590 map
->ctx
= dim
->ctx
;
4591 isl_ctx_ref(map
->ctx
);
4599 isl_space_free(dim
);
4603 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4604 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4607 struct isl_map
*map
;
4610 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4614 map
= isl_map_alloc_space(dims
, n
, flags
);
4618 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4620 struct isl_basic_map
*bmap
;
4621 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4622 bmap
= isl_basic_map_set_to_empty(bmap
);
4626 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4628 struct isl_basic_set
*bset
;
4629 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4630 bset
= isl_basic_set_set_to_empty(bset
);
4634 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4636 struct isl_basic_map
*bmap
;
4639 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4640 bmap
= isl_basic_map_set_to_empty(bmap
);
4644 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4646 struct isl_basic_map
*bmap
;
4649 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4650 bmap
= isl_basic_map_set_to_empty(bmap
);
4654 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4656 struct isl_basic_set
*bset
;
4659 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4660 bset
= isl_basic_set_set_to_empty(bset
);
4664 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4666 struct isl_basic_map
*bmap
;
4667 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4668 bmap
= isl_basic_map_finalize(bmap
);
4672 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4674 struct isl_basic_set
*bset
;
4675 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4676 bset
= isl_basic_set_finalize(bset
);
4680 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4683 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4684 isl_basic_map
*bmap
;
4686 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4687 for (i
= 0; i
< total
; ++i
) {
4688 int k
= isl_basic_map_alloc_inequality(bmap
);
4691 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4692 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4696 isl_basic_map_free(bmap
);
4700 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4702 return isl_basic_map_nat_universe(dim
);
4705 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4707 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4710 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4712 return isl_map_nat_universe(dim
);
4715 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4716 __isl_keep isl_basic_map
*model
)
4720 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4723 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4727 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4730 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4731 __isl_keep isl_set
*model
)
4735 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4738 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4740 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4743 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4747 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4750 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4754 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4757 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4759 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4762 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4766 return isl_set_empty(isl_space_copy(model
->dim
));
4769 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4771 struct isl_map
*map
;
4774 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4775 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4779 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4781 struct isl_set
*set
;
4784 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4785 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4789 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4793 return isl_set_universe(isl_space_copy(model
->dim
));
4796 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4799 struct isl_map
*dup
;
4803 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4804 for (i
= 0; i
< map
->n
; ++i
)
4805 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4809 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4810 __isl_take isl_basic_map
*bmap
)
4814 if (isl_basic_map_plain_is_empty(bmap
)) {
4815 isl_basic_map_free(bmap
);
4818 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4819 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4820 map
->p
[map
->n
] = bmap
;
4822 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4828 isl_basic_map_free(bmap
);
4832 void isl_map_free(struct isl_map
*map
)
4842 isl_ctx_deref(map
->ctx
);
4843 for (i
= 0; i
< map
->n
; ++i
)
4844 isl_basic_map_free(map
->p
[i
]);
4845 isl_space_free(map
->dim
);
4849 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4850 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4854 base
= isl_map_cow(base
);
4858 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4861 for (i
= 0; i
< base
->n
; ++i
) {
4862 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4863 isl_space_copy(base
->dim
), 0, 0, 0);
4873 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4874 unsigned nparam
, unsigned dim
)
4876 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4880 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4881 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4885 bmap
= isl_basic_map_cow(bmap
);
4886 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4887 j
= isl_basic_map_alloc_equality(bmap
);
4890 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4891 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4892 isl_int_set_si(bmap
->eq
[j
][0], value
);
4893 bmap
= isl_basic_map_simplify(bmap
);
4894 return isl_basic_map_finalize(bmap
);
4896 isl_basic_map_free(bmap
);
4900 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4901 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4905 bmap
= isl_basic_map_cow(bmap
);
4906 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4907 j
= isl_basic_map_alloc_equality(bmap
);
4910 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4911 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4912 isl_int_set(bmap
->eq
[j
][0], value
);
4913 bmap
= isl_basic_map_simplify(bmap
);
4914 return isl_basic_map_finalize(bmap
);
4916 isl_basic_map_free(bmap
);
4920 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4921 enum isl_dim_type type
, unsigned pos
, int value
)
4925 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4926 return isl_basic_map_fix_pos_si(bmap
,
4927 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4929 isl_basic_map_free(bmap
);
4933 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4934 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4938 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4939 return isl_basic_map_fix_pos(bmap
,
4940 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4942 isl_basic_map_free(bmap
);
4946 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4947 enum isl_dim_type type
, unsigned pos
, int value
)
4949 return (struct isl_basic_set
*)
4950 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4954 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4955 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4957 return (struct isl_basic_set
*)
4958 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4962 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4963 unsigned input
, int value
)
4965 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4968 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4969 unsigned dim
, int value
)
4971 return (struct isl_basic_set
*)
4972 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4973 isl_dim_set
, dim
, value
);
4976 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
4978 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
4985 isl_basic_map_free(map
->p
[i
]);
4986 if (i
!= map
->n
- 1) {
4987 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4988 map
->p
[i
] = map
->p
[map
->n
- 1];
4995 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4996 enum isl_dim_type type
, unsigned pos
, int value
)
5000 map
= isl_map_cow(map
);
5004 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5005 for (i
= map
->n
- 1; i
>= 0; --i
) {
5006 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5007 if (remove_if_empty(map
, i
) < 0)
5010 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5017 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5018 enum isl_dim_type type
, unsigned pos
, int value
)
5020 return (struct isl_set
*)
5021 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5024 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5025 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5029 map
= isl_map_cow(map
);
5033 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5034 for (i
= 0; i
< map
->n
; ++i
) {
5035 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5039 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5046 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5047 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5049 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5052 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5053 unsigned input
, int value
)
5055 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5058 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5060 return (struct isl_set
*)
5061 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5064 static __isl_give isl_basic_map
*basic_map_bound_si(
5065 __isl_take isl_basic_map
*bmap
,
5066 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5072 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5073 pos
+= isl_basic_map_offset(bmap
, type
);
5074 bmap
= isl_basic_map_cow(bmap
);
5075 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5076 j
= isl_basic_map_alloc_inequality(bmap
);
5079 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5081 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5082 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5084 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5085 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5087 bmap
= isl_basic_map_simplify(bmap
);
5088 return isl_basic_map_finalize(bmap
);
5090 isl_basic_map_free(bmap
);
5094 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5095 __isl_take isl_basic_map
*bmap
,
5096 enum isl_dim_type type
, unsigned pos
, int value
)
5098 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5101 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5102 unsigned dim
, isl_int value
)
5106 bset
= isl_basic_set_cow(bset
);
5107 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5108 j
= isl_basic_set_alloc_inequality(bset
);
5111 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5112 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5113 isl_int_neg(bset
->ineq
[j
][0], value
);
5114 bset
= isl_basic_set_simplify(bset
);
5115 return isl_basic_set_finalize(bset
);
5117 isl_basic_set_free(bset
);
5121 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5122 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5126 map
= isl_map_cow(map
);
5130 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5131 for (i
= 0; i
< map
->n
; ++i
) {
5132 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5133 type
, pos
, value
, upper
);
5137 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5144 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5145 enum isl_dim_type type
, unsigned pos
, int value
)
5147 return map_bound_si(map
, type
, pos
, value
, 0);
5150 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5151 enum isl_dim_type type
, unsigned pos
, int value
)
5153 return map_bound_si(map
, type
, pos
, value
, 1);
5156 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5157 enum isl_dim_type type
, unsigned pos
, int value
)
5159 return (struct isl_set
*)
5160 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5163 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5164 enum isl_dim_type type
, unsigned pos
, int value
)
5166 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5169 /* Bound the given variable of "bmap" from below (or above is "upper"
5170 * is set) to "value".
5172 static __isl_give isl_basic_map
*basic_map_bound(
5173 __isl_take isl_basic_map
*bmap
,
5174 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5180 if (pos
>= isl_basic_map_dim(bmap
, type
))
5181 isl_die(bmap
->ctx
, isl_error_invalid
,
5182 "index out of bounds", goto error
);
5183 pos
+= isl_basic_map_offset(bmap
, type
);
5184 bmap
= isl_basic_map_cow(bmap
);
5185 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5186 j
= isl_basic_map_alloc_inequality(bmap
);
5189 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5191 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5192 isl_int_set(bmap
->ineq
[j
][0], value
);
5194 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5195 isl_int_neg(bmap
->ineq
[j
][0], value
);
5197 bmap
= isl_basic_map_simplify(bmap
);
5198 return isl_basic_map_finalize(bmap
);
5200 isl_basic_map_free(bmap
);
5204 /* Bound the given variable of "map" from below (or above is "upper"
5205 * is set) to "value".
5207 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5208 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5212 map
= isl_map_cow(map
);
5216 if (pos
>= isl_map_dim(map
, type
))
5217 isl_die(map
->ctx
, isl_error_invalid
,
5218 "index out of bounds", goto error
);
5219 for (i
= map
->n
- 1; i
>= 0; --i
) {
5220 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5221 if (remove_if_empty(map
, i
) < 0)
5224 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5231 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5232 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5234 return map_bound(map
, type
, pos
, value
, 0);
5237 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5238 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5240 return map_bound(map
, type
, pos
, value
, 1);
5243 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5244 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5246 return isl_map_lower_bound(set
, type
, pos
, value
);
5249 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5250 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5252 return isl_map_upper_bound(set
, type
, pos
, value
);
5255 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5260 set
= isl_set_cow(set
);
5264 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5265 for (i
= 0; i
< set
->n
; ++i
) {
5266 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5276 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5280 map
= isl_map_cow(map
);
5284 map
->dim
= isl_space_reverse(map
->dim
);
5287 for (i
= 0; i
< map
->n
; ++i
) {
5288 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5292 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5299 static struct isl_map
*isl_basic_map_partial_lexopt(
5300 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5301 struct isl_set
**empty
, int max
)
5305 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5306 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5308 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5310 isl_basic_map_free(bmap
);
5311 isl_basic_set_free(dom
);
5317 struct isl_map
*isl_basic_map_partial_lexmax(
5318 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5319 struct isl_set
**empty
)
5321 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5324 struct isl_map
*isl_basic_map_partial_lexmin(
5325 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5326 struct isl_set
**empty
)
5328 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5331 struct isl_set
*isl_basic_set_partial_lexmin(
5332 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5333 struct isl_set
**empty
)
5335 return (struct isl_set
*)
5336 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5340 struct isl_set
*isl_basic_set_partial_lexmax(
5341 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5342 struct isl_set
**empty
)
5344 return (struct isl_set
*)
5345 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5349 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5350 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5351 __isl_give isl_set
**empty
)
5353 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5356 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5357 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5358 __isl_give isl_set
**empty
)
5360 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5363 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5364 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5365 __isl_give isl_set
**empty
)
5367 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5370 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5371 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5372 __isl_give isl_set
**empty
)
5374 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5377 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5378 __isl_take isl_basic_map
*bmap
, int max
)
5380 isl_basic_set
*dom
= NULL
;
5381 isl_space
*dom_space
;
5385 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5386 dom
= isl_basic_set_universe(dom_space
);
5387 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5389 isl_basic_map_free(bmap
);
5393 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5394 __isl_take isl_basic_map
*bmap
)
5396 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5399 /* Given a basic map "bmap", compute the lexicographically minimal
5400 * (or maximal) image element for each domain element in dom.
5401 * Set *empty to those elements in dom that do not have an image element.
5403 * We first make sure the basic sets in dom are disjoint and then
5404 * simply collect the results over each of the basic sets separately.
5405 * We could probably improve the efficiency a bit by moving the union
5406 * domain down into the parametric integer programming.
5408 static __isl_give isl_map
*basic_map_partial_lexopt(
5409 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5410 __isl_give isl_set
**empty
, int max
)
5413 struct isl_map
*res
;
5415 dom
= isl_set_make_disjoint(dom
);
5419 if (isl_set_plain_is_empty(dom
)) {
5420 res
= isl_map_empty_like_basic_map(bmap
);
5421 *empty
= isl_set_empty_like(dom
);
5423 isl_basic_map_free(bmap
);
5427 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5428 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5430 for (i
= 1; i
< dom
->n
; ++i
) {
5431 struct isl_map
*res_i
;
5432 struct isl_set
*empty_i
;
5434 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5435 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5437 res
= isl_map_union_disjoint(res
, res_i
);
5438 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5442 isl_basic_map_free(bmap
);
5447 isl_basic_map_free(bmap
);
5451 /* Given a map "map", compute the lexicographically minimal
5452 * (or maximal) image element for each domain element in dom.
5453 * Set *empty to those elements in dom that do not have an image element.
5455 * We first compute the lexicographically minimal or maximal element
5456 * in the first basic map. This results in a partial solution "res"
5457 * and a subset "todo" of dom that still need to be handled.
5458 * We then consider each of the remaining maps in "map" and successively
5459 * improve both "res" and "todo".
5461 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5462 * Assume we are computing the lexicographical maximum.
5463 * We first compute the lexicographically maximal element in basic map i.
5464 * This results in a partial solution res_i and a subset todo_i.
5465 * Then we combine these results with those obtain for the first k basic maps
5466 * to obtain a result that is valid for the first k+1 basic maps.
5467 * In particular, the set where there is no solution is the set where
5468 * there is no solution for the first k basic maps and also no solution
5469 * for the ith basic map, i.e.,
5471 * todo^i = todo^k * todo_i
5473 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5474 * solutions, arbitrarily breaking ties in favor of res^k.
5475 * That is, when res^k(a) >= res_i(a), we pick res^k and
5476 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5477 * the lexicographic order.)
5478 * In practice, we compute
5480 * res^k * (res_i . "<=")
5484 * res_i * (res^k . "<")
5486 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5487 * where only one of res^k and res_i provides a solution and we simply pick
5494 * Note that we only compute these intersections when dom(res^k) intersects
5495 * dom(res_i). Otherwise, the only effect of these intersections is to
5496 * potentially break up res^k and res_i into smaller pieces.
5497 * We want to avoid such splintering as much as possible.
5498 * In fact, an earlier implementation of this function would look for
5499 * better results in the domain of res^k and for extra results in todo^k,
5500 * but this would always result in a splintering according to todo^k,
5501 * even when the domain of basic map i is disjoint from the domains of
5502 * the previous basic maps.
5504 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5505 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5506 __isl_give isl_set
**empty
, int max
)
5509 struct isl_map
*res
;
5510 struct isl_set
*todo
;
5515 if (isl_map_plain_is_empty(map
)) {
5523 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5524 isl_set_copy(dom
), &todo
, max
);
5526 for (i
= 1; i
< map
->n
; ++i
) {
5530 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5532 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5533 isl_set_copy(dom
), &todo_i
, max
);
5536 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5537 le
= isl_map_lex_le(dim
);
5539 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5540 le
= isl_map_lex_ge(dim
);
5542 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5543 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5544 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5545 le
= isl_map_intersect(le
, isl_map_copy(res
));
5547 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5548 res
= isl_map_intersect_domain(res
,
5549 isl_set_copy(todo_i
));
5550 res_i
= isl_map_intersect_domain(res_i
,
5551 isl_set_copy(todo
));
5554 res
= isl_map_union_disjoint(res
, res_i
);
5555 res
= isl_map_union_disjoint(res
, lt
);
5556 res
= isl_map_union_disjoint(res
, le
);
5558 todo
= isl_set_intersect(todo
, todo_i
);
5578 /* Given a map "map", compute the lexicographically minimal
5579 * (or maximal) image element for each domain element in dom.
5580 * Set *empty to those elements in dom that do not have an image element.
5582 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5584 static __isl_give isl_map
*isl_map_partial_lexopt(
5585 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5586 __isl_give isl_set
**empty
, int max
)
5590 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5591 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5592 if (!isl_space_has_named_params(map
->dim
) ||
5593 !isl_space_has_named_params(dom
->dim
))
5594 isl_die(map
->ctx
, isl_error_invalid
,
5595 "unaligned unnamed parameters", goto error
);
5596 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5597 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5598 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5607 __isl_give isl_map
*isl_map_partial_lexmax(
5608 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5609 __isl_give isl_set
**empty
)
5611 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5614 __isl_give isl_map
*isl_map_partial_lexmin(
5615 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5616 __isl_give isl_set
**empty
)
5618 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5621 __isl_give isl_set
*isl_set_partial_lexmin(
5622 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5623 __isl_give isl_set
**empty
)
5625 return (struct isl_set
*)
5626 isl_map_partial_lexmin((struct isl_map
*)set
,
5630 __isl_give isl_set
*isl_set_partial_lexmax(
5631 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5632 __isl_give isl_set
**empty
)
5634 return (struct isl_set
*)
5635 isl_map_partial_lexmax((struct isl_map
*)set
,
5639 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5641 struct isl_basic_set
*dom
= NULL
;
5646 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5647 dom
= isl_basic_set_universe(dom_dim
);
5648 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5650 isl_basic_map_free(bmap
);
5654 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5656 return isl_basic_map_lexopt(bmap
, 0);
5659 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5661 return isl_basic_map_lexopt(bmap
, 1);
5664 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5666 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5669 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5671 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5674 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5676 struct isl_set
*dom
= NULL
;
5681 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5682 dom
= isl_set_universe(dom_dim
);
5683 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5689 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5691 return isl_map_lexopt(map
, 0);
5694 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5696 return isl_map_lexopt(map
, 1);
5699 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5701 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5704 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5706 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5709 /* Extract the first and only affine expression from list
5710 * and then add it to *pwaff with the given dom.
5711 * This domain is known to be disjoint from other domains
5712 * because of the way isl_basic_map_foreach_lexmax works.
5714 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5715 __isl_take isl_aff_list
*list
, void *user
)
5717 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5719 isl_pw_aff
**pwaff
= user
;
5720 isl_pw_aff
*pwaff_i
;
5722 if (isl_aff_list_n_aff(list
) != 1)
5723 isl_die(ctx
, isl_error_internal
,
5724 "expecting single element list", goto error
);
5726 aff
= isl_aff_list_get_aff(list
, 0);
5727 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5729 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5731 isl_aff_list_free(list
);
5735 isl_basic_set_free(dom
);
5736 isl_aff_list_free(list
);
5740 /* Given a basic map with one output dimension, compute the minimum or
5741 * maximum of that dimension as an isl_pw_aff.
5743 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5744 * call update_dim_opt on each leaf of the result.
5746 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5749 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5753 dim
= isl_space_from_domain(isl_space_domain(dim
));
5754 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5755 pwaff
= isl_pw_aff_empty(dim
);
5757 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5759 return isl_pw_aff_free(pwaff
);
5764 /* Compute the minimum or maximum of the given output dimension
5765 * as a function of the parameters and the input dimensions,
5766 * but independently of the other output dimensions.
5768 * We first project out the other output dimension and then compute
5769 * the "lexicographic" maximum in each basic map, combining the results
5770 * using isl_pw_aff_union_max.
5772 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5779 n_out
= isl_map_dim(map
, isl_dim_out
);
5780 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5781 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5786 isl_space
*dim
= isl_map_get_space(map
);
5787 dim
= isl_space_domain(isl_space_from_range(dim
));
5789 return isl_pw_aff_empty(dim
);
5792 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5793 for (i
= 1; i
< map
->n
; ++i
) {
5794 isl_pw_aff
*pwaff_i
;
5796 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5797 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5805 /* Compute the maximum of the given output dimension as a function of the
5806 * parameters and input dimensions, but independently of
5807 * the other output dimensions.
5809 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5811 return map_dim_opt(map
, pos
, 1);
5814 /* Compute the minimum or maximum of the given set dimension
5815 * as a function of the parameters,
5816 * but independently of the other set dimensions.
5818 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5821 return map_dim_opt(set
, pos
, max
);
5824 /* Compute the maximum of the given set dimension as a function of the
5825 * parameters, but independently of the other set dimensions.
5827 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5829 return set_dim_opt(set
, pos
, 1);
5832 /* Compute the minimum of the given set dimension as a function of the
5833 * parameters, but independently of the other set dimensions.
5835 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5837 return set_dim_opt(set
, pos
, 0);
5840 /* Apply a preimage specified by "mat" on the parameters of "bset".
5841 * bset is assumed to have only parameters and divs.
5843 static struct isl_basic_set
*basic_set_parameter_preimage(
5844 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5851 bset
->dim
= isl_space_cow(bset
->dim
);
5855 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5857 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5859 bset
->dim
->nparam
= 0;
5860 bset
->dim
->n_out
= nparam
;
5861 bset
= isl_basic_set_preimage(bset
, mat
);
5863 bset
->dim
->nparam
= bset
->dim
->n_out
;
5864 bset
->dim
->n_out
= 0;
5869 isl_basic_set_free(bset
);
5873 /* Apply a preimage specified by "mat" on the parameters of "set".
5874 * set is assumed to have only parameters and divs.
5876 static struct isl_set
*set_parameter_preimage(
5877 struct isl_set
*set
, struct isl_mat
*mat
)
5879 isl_space
*dim
= NULL
;
5885 dim
= isl_space_copy(set
->dim
);
5886 dim
= isl_space_cow(dim
);
5890 nparam
= isl_set_dim(set
, isl_dim_param
);
5892 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5895 dim
->n_out
= nparam
;
5896 isl_set_reset_space(set
, dim
);
5897 set
= isl_set_preimage(set
, mat
);
5900 dim
= isl_space_copy(set
->dim
);
5901 dim
= isl_space_cow(dim
);
5904 dim
->nparam
= dim
->n_out
;
5906 isl_set_reset_space(set
, dim
);
5909 isl_space_free(dim
);
5916 /* Intersect the basic set "bset" with the affine space specified by the
5917 * equalities in "eq".
5919 static struct isl_basic_set
*basic_set_append_equalities(
5920 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5928 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5933 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5934 for (i
= 0; i
< eq
->n_row
; ++i
) {
5935 k
= isl_basic_set_alloc_equality(bset
);
5938 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5939 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5943 bset
= isl_basic_set_gauss(bset
, NULL
);
5944 bset
= isl_basic_set_finalize(bset
);
5949 isl_basic_set_free(bset
);
5953 /* Intersect the set "set" with the affine space specified by the
5954 * equalities in "eq".
5956 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5964 for (i
= 0; i
< set
->n
; ++i
) {
5965 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5978 /* Project the given basic set onto its parameter domain, possibly introducing
5979 * new, explicit, existential variables in the constraints.
5980 * The input has parameters and (possibly implicit) existential variables.
5981 * The output has the same parameters, but only
5982 * explicit existentially quantified variables.
5984 * The actual projection is performed by pip, but pip doesn't seem
5985 * to like equalities very much, so we first remove the equalities
5986 * among the parameters by performing a variable compression on
5987 * the parameters. Afterward, an inverse transformation is performed
5988 * and the equalities among the parameters are inserted back in.
5990 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5994 struct isl_mat
*T
, *T2
;
5995 struct isl_set
*set
;
5996 unsigned nparam
, n_div
;
5998 bset
= isl_basic_set_cow(bset
);
6002 if (bset
->n_eq
== 0)
6003 return isl_basic_set_lexmin(bset
);
6005 isl_basic_set_gauss(bset
, NULL
);
6007 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6008 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6010 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6011 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6014 if (i
== bset
->n_eq
)
6015 return isl_basic_set_lexmin(bset
);
6017 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6019 eq
= isl_mat_cow(eq
);
6020 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6021 if (T
&& T
->n_col
== 0) {
6025 bset
= isl_basic_set_set_to_empty(bset
);
6026 return isl_set_from_basic_set(bset
);
6028 bset
= basic_set_parameter_preimage(bset
, T
);
6030 set
= isl_basic_set_lexmin(bset
);
6031 set
= set_parameter_preimage(set
, T2
);
6032 set
= set_append_equalities(set
, eq
);
6036 /* Compute an explicit representation for all the existentially
6037 * quantified variables.
6038 * The input and output dimensions are first turned into parameters.
6039 * compute_divs then returns a map with the same parameters and
6040 * no input or output dimensions and the dimension specification
6041 * is reset to that of the input.
6043 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6045 struct isl_basic_set
*bset
;
6046 struct isl_set
*set
;
6047 struct isl_map
*map
;
6048 isl_space
*dim
, *orig_dim
= NULL
;
6053 bmap
= isl_basic_map_cow(bmap
);
6057 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6058 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6059 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6060 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
6064 orig_dim
= bmap
->dim
;
6066 bset
= (struct isl_basic_set
*)bmap
;
6068 set
= parameter_compute_divs(bset
);
6069 map
= (struct isl_map
*)set
;
6070 map
= isl_map_reset_space(map
, orig_dim
);
6074 isl_basic_map_free(bmap
);
6078 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6086 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6087 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6088 if (isl_int_is_zero(bmap
->div
[i
][0]))
6090 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6096 static int map_divs_known(__isl_keep isl_map
*map
)
6103 for (i
= 0; i
< map
->n
; ++i
) {
6104 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6112 /* If bmap contains any unknown divs, then compute explicit
6113 * expressions for them. However, this computation may be
6114 * quite expensive, so first try to remove divs that aren't
6117 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6120 struct isl_map
*map
;
6122 known
= isl_basic_map_divs_known(bmap
);
6126 return isl_map_from_basic_map(bmap
);
6128 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6130 known
= isl_basic_map_divs_known(bmap
);
6134 return isl_map_from_basic_map(bmap
);
6136 map
= compute_divs(bmap
);
6139 isl_basic_map_free(bmap
);
6143 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6147 struct isl_map
*res
;
6154 known
= map_divs_known(map
);
6162 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6163 for (i
= 1 ; i
< map
->n
; ++i
) {
6165 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6166 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6167 res
= isl_map_union_disjoint(res
, r2
);
6169 res
= isl_map_union(res
, r2
);
6176 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6178 return (struct isl_set
*)
6179 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6182 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6184 return (struct isl_set
*)
6185 isl_map_compute_divs((struct isl_map
*)set
);
6188 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6191 struct isl_set
*set
;
6196 map
= isl_map_cow(map
);
6200 set
= (struct isl_set
*)map
;
6201 set
->dim
= isl_space_domain(set
->dim
);
6204 for (i
= 0; i
< map
->n
; ++i
) {
6205 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6209 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6210 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6217 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6218 __isl_take isl_map
*map2
)
6222 struct isl_map
*map
= NULL
;
6236 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6238 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6239 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6240 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6242 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6243 map1
->n
+ map2
->n
, flags
);
6246 for (i
= 0; i
< map1
->n
; ++i
) {
6247 map
= isl_map_add_basic_map(map
,
6248 isl_basic_map_copy(map1
->p
[i
]));
6252 for (i
= 0; i
< map2
->n
; ++i
) {
6253 map
= isl_map_add_basic_map(map
,
6254 isl_basic_map_copy(map2
->p
[i
]));
6268 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6269 __isl_take isl_map
*map2
)
6271 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6274 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6276 map1
= isl_map_union_disjoint(map1
, map2
);
6280 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6284 struct isl_set
*isl_set_union_disjoint(
6285 struct isl_set
*set1
, struct isl_set
*set2
)
6287 return (struct isl_set
*)
6288 isl_map_union_disjoint(
6289 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6292 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6294 return (struct isl_set
*)
6295 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6298 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6299 __isl_take isl_set
*set
)
6302 struct isl_map
*result
;
6308 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6309 isl_die(set
->ctx
, isl_error_invalid
,
6310 "parameters don't match", goto error
);
6312 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6313 !isl_map_compatible_range(map
, set
))
6314 isl_die(set
->ctx
, isl_error_invalid
,
6315 "incompatible spaces", goto error
);
6317 if (isl_set_plain_is_universe(set
)) {
6322 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6323 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6324 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6326 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6327 map
->n
* set
->n
, flags
);
6330 for (i
= 0; i
< map
->n
; ++i
)
6331 for (j
= 0; j
< set
->n
; ++j
) {
6332 result
= isl_map_add_basic_map(result
,
6333 isl_basic_map_intersect_range(
6334 isl_basic_map_copy(map
->p
[i
]),
6335 isl_basic_set_copy(set
->p
[j
])));
6348 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6349 __isl_take isl_set
*set
)
6351 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6354 struct isl_map
*isl_map_intersect_domain(
6355 struct isl_map
*map
, struct isl_set
*set
)
6357 return isl_map_reverse(
6358 isl_map_intersect_range(isl_map_reverse(map
), set
));
6361 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6362 __isl_take isl_map
*map2
)
6366 map1
= isl_map_reverse(map1
);
6367 map1
= isl_map_apply_range(map1
, map2
);
6368 return isl_map_reverse(map1
);
6375 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6376 __isl_take isl_map
*map2
)
6378 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6381 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6382 __isl_take isl_map
*map2
)
6384 isl_space
*dim_result
;
6385 struct isl_map
*result
;
6391 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6392 isl_space_copy(map2
->dim
));
6394 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6397 for (i
= 0; i
< map1
->n
; ++i
)
6398 for (j
= 0; j
< map2
->n
; ++j
) {
6399 result
= isl_map_add_basic_map(result
,
6400 isl_basic_map_apply_range(
6401 isl_basic_map_copy(map1
->p
[i
]),
6402 isl_basic_map_copy(map2
->p
[j
])));
6408 if (result
&& result
->n
<= 1)
6409 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6417 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6418 __isl_take isl_map
*map2
)
6420 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6424 * returns range - domain
6426 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6428 isl_space
*dims
, *target_dim
;
6429 struct isl_basic_set
*bset
;
6436 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6437 bmap
->dim
, isl_dim_out
),
6439 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6440 dim
= isl_basic_map_n_in(bmap
);
6441 nparam
= isl_basic_map_n_param(bmap
);
6442 bset
= isl_basic_set_from_basic_map(bmap
);
6443 bset
= isl_basic_set_cow(bset
);
6444 dims
= isl_basic_set_get_space(bset
);
6445 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6446 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6447 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6448 for (i
= 0; i
< dim
; ++i
) {
6449 int j
= isl_basic_map_alloc_equality(
6450 (struct isl_basic_map
*)bset
);
6453 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6454 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6455 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6456 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6458 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6459 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6462 isl_basic_map_free(bmap
);
6467 * returns range - domain
6469 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
6473 struct isl_set
*result
;
6478 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6479 map
->dim
, isl_dim_out
),
6481 dim
= isl_map_get_space(map
);
6482 dim
= isl_space_domain(dim
);
6483 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6486 for (i
= 0; i
< map
->n
; ++i
)
6487 result
= isl_set_add_basic_set(result
,
6488 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6497 * returns [domain -> range] -> range - domain
6499 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6500 __isl_take isl_basic_map
*bmap
)
6504 isl_basic_map
*domain
;
6508 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6509 isl_die(bmap
->ctx
, isl_error_invalid
,
6510 "domain and range don't match", goto error
);
6512 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6513 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6515 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6516 domain
= isl_basic_map_universe(dim
);
6518 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6519 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6520 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6522 total
= isl_basic_map_total_dim(bmap
);
6524 for (i
= 0; i
< n
; ++i
) {
6525 k
= isl_basic_map_alloc_equality(bmap
);
6528 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6529 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6530 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6531 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6534 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6535 return isl_basic_map_finalize(bmap
);
6537 isl_basic_map_free(bmap
);
6542 * returns [domain -> range] -> range - domain
6544 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6547 isl_space
*domain_dim
;
6552 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6553 isl_die(map
->ctx
, isl_error_invalid
,
6554 "domain and range don't match", goto error
);
6556 map
= isl_map_cow(map
);
6560 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6561 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6562 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6565 for (i
= 0; i
< map
->n
; ++i
) {
6566 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6570 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6577 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6579 struct isl_basic_map
*bmap
;
6587 nparam
= dims
->nparam
;
6589 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6593 for (i
= 0; i
< dim
; ++i
) {
6594 int j
= isl_basic_map_alloc_equality(bmap
);
6597 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6598 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6599 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6601 return isl_basic_map_finalize(bmap
);
6603 isl_basic_map_free(bmap
);
6607 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6611 if (dim
->n_in
!= dim
->n_out
)
6612 isl_die(dim
->ctx
, isl_error_invalid
,
6613 "number of input and output dimensions needs to be "
6614 "the same", goto error
);
6615 return basic_map_identity(dim
);
6617 isl_space_free(dim
);
6621 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6623 if (!model
|| !model
->dim
)
6625 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6628 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6630 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6633 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6635 if (!model
|| !model
->dim
)
6637 return isl_map_identity(isl_space_copy(model
->dim
));
6640 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6642 if (!model
|| !model
->dim
)
6644 return isl_map_identity(isl_space_copy(model
->dim
));
6647 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6649 isl_space
*dim
= isl_set_get_space(set
);
6651 id
= isl_map_identity(isl_space_map_from_set(dim
));
6652 return isl_map_intersect_range(id
, set
);
6655 /* Construct a basic set with all set dimensions having only non-negative
6658 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6659 __isl_take isl_space
*space
)
6664 struct isl_basic_set
*bset
;
6668 nparam
= space
->nparam
;
6670 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6673 for (i
= 0; i
< dim
; ++i
) {
6674 int k
= isl_basic_set_alloc_inequality(bset
);
6677 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6678 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6682 isl_basic_set_free(bset
);
6686 /* Construct the half-space x_pos >= 0.
6688 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6692 isl_basic_set
*nonneg
;
6694 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6695 k
= isl_basic_set_alloc_inequality(nonneg
);
6698 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6699 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6701 return isl_basic_set_finalize(nonneg
);
6703 isl_basic_set_free(nonneg
);
6707 /* Construct the half-space x_pos <= -1.
6709 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6714 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6715 k
= isl_basic_set_alloc_inequality(neg
);
6718 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6719 isl_int_set_si(neg
->ineq
[k
][0], -1);
6720 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6722 return isl_basic_set_finalize(neg
);
6724 isl_basic_set_free(neg
);
6728 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6729 enum isl_dim_type type
, unsigned first
, unsigned n
)
6732 isl_basic_set
*nonneg
;
6740 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6742 for (i
= 0; i
< n
; ++i
) {
6743 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6744 pos(set
->dim
, type
) + first
+ i
);
6745 neg
= neg_halfspace(isl_set_get_space(set
),
6746 pos(set
->dim
, type
) + first
+ i
);
6748 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6757 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6758 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6765 if (isl_set_plain_is_empty(set
)) {
6770 return fn(set
, signs
, user
);
6773 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6775 half
= isl_set_intersect(half
, isl_set_copy(set
));
6776 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6780 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6782 half
= isl_set_intersect(half
, set
);
6783 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6789 /* Call "fn" on the intersections of "set" with each of the orthants
6790 * (except for obviously empty intersections). The orthant is identified
6791 * by the signs array, with each entry having value 1 or -1 according
6792 * to the sign of the corresponding variable.
6794 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6795 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6805 if (isl_set_plain_is_empty(set
))
6808 nparam
= isl_set_dim(set
, isl_dim_param
);
6809 nvar
= isl_set_dim(set
, isl_dim_set
);
6811 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6813 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6821 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6823 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6826 int isl_basic_map_is_subset(
6827 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6830 struct isl_map
*map1
;
6831 struct isl_map
*map2
;
6833 if (!bmap1
|| !bmap2
)
6836 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6837 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6839 is_subset
= isl_map_is_subset(map1
, map2
);
6847 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
6848 __isl_keep isl_basic_set
*bset2
)
6850 return isl_basic_map_is_subset(bset1
, bset2
);
6853 int isl_basic_map_is_equal(
6854 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6858 if (!bmap1
|| !bmap2
)
6860 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6863 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6867 int isl_basic_set_is_equal(
6868 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6870 return isl_basic_map_is_equal(
6871 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6874 int isl_map_is_empty(struct isl_map
*map
)
6881 for (i
= 0; i
< map
->n
; ++i
) {
6882 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6891 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6893 return map
? map
->n
== 0 : -1;
6896 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6898 return isl_map_plain_is_empty(map
);
6901 int isl_set_plain_is_empty(struct isl_set
*set
)
6903 return set
? set
->n
== 0 : -1;
6906 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6908 return isl_set_plain_is_empty(set
);
6911 int isl_set_is_empty(struct isl_set
*set
)
6913 return isl_map_is_empty((struct isl_map
*)set
);
6916 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6921 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6924 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6929 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6932 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6938 is_subset
= isl_map_is_subset(map1
, map2
);
6941 is_subset
= isl_map_is_subset(map2
, map1
);
6945 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6947 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6950 int isl_basic_map_is_strict_subset(
6951 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6955 if (!bmap1
|| !bmap2
)
6957 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6960 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6961 if (is_subset
== -1)
6966 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6972 is_subset
= isl_map_is_subset(map1
, map2
);
6975 is_subset
= isl_map_is_subset(map2
, map1
);
6976 if (is_subset
== -1)
6981 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6983 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6986 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6990 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6993 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6997 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7000 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7007 for (i
= 0; i
< map
->n
; ++i
) {
7008 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7016 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7018 return isl_map_plain_is_universe((isl_map
*) set
);
7021 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7023 return isl_set_plain_is_universe(set
);
7026 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7028 struct isl_basic_set
*bset
= NULL
;
7029 struct isl_vec
*sample
= NULL
;
7036 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7039 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7040 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7041 copy
= isl_basic_map_remove_redundancies(copy
);
7042 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
7043 isl_basic_map_free(copy
);
7047 total
= 1 + isl_basic_map_total_dim(bmap
);
7048 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7049 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7055 isl_vec_free(bmap
->sample
);
7056 bmap
->sample
= NULL
;
7057 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7060 sample
= isl_basic_set_sample_vec(bset
);
7063 empty
= sample
->size
== 0;
7064 isl_vec_free(bmap
->sample
);
7065 bmap
->sample
= sample
;
7067 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7072 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7076 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7079 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7081 return isl_basic_map_plain_is_empty(bmap
);
7084 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7088 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7091 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7093 return isl_basic_set_plain_is_empty(bset
);
7096 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7098 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7101 struct isl_map
*isl_basic_map_union(
7102 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7104 struct isl_map
*map
;
7105 if (!bmap1
|| !bmap2
)
7108 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7110 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7113 map
= isl_map_add_basic_map(map
, bmap1
);
7114 map
= isl_map_add_basic_map(map
, bmap2
);
7117 isl_basic_map_free(bmap1
);
7118 isl_basic_map_free(bmap2
);
7122 struct isl_set
*isl_basic_set_union(
7123 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7125 return (struct isl_set
*)isl_basic_map_union(
7126 (struct isl_basic_map
*)bset1
,
7127 (struct isl_basic_map
*)bset2
);
7130 /* Order divs such that any div only depends on previous divs */
7131 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7139 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7141 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7143 if (isl_int_is_zero(bmap
->div
[i
][0]))
7145 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7149 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7155 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7157 return (struct isl_basic_set
*)
7158 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7161 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7168 for (i
= 0; i
< map
->n
; ++i
) {
7169 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7180 /* Apply the expansion computed by isl_merge_divs.
7181 * The expansion itself is given by "exp" while the resulting
7182 * list of divs is given by "div".
7184 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7185 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7190 bset
= isl_basic_set_cow(bset
);
7194 if (div
->n_row
< bset
->n_div
)
7195 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7196 "not an expansion", goto error
);
7198 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7199 div
->n_row
- bset
->n_div
, 0,
7200 2 * (div
->n_row
- bset
->n_div
));
7202 n_div
= bset
->n_div
;
7203 for (i
= n_div
; i
< div
->n_row
; ++i
)
7204 if (isl_basic_set_alloc_div(bset
) < 0)
7208 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7209 if (j
>= 0 && exp
[j
] == i
) {
7211 isl_basic_map_swap_div(bset
, i
, j
);
7214 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7215 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7223 isl_basic_set_free(bset
);
7228 /* Look for a div in dst that corresponds to the div "div" in src.
7229 * The divs before "div" in src and dst are assumed to be the same.
7231 * Returns -1 if no corresponding div was found and the position
7232 * of the corresponding div in dst otherwise.
7234 static int find_div(struct isl_basic_map
*dst
,
7235 struct isl_basic_map
*src
, unsigned div
)
7239 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7241 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7242 for (i
= div
; i
< dst
->n_div
; ++i
)
7243 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7244 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7245 dst
->n_div
- div
) == -1)
7250 struct isl_basic_map
*isl_basic_map_align_divs(
7251 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7254 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7259 if (src
->n_div
== 0)
7262 for (i
= 0; i
< src
->n_div
; ++i
)
7263 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7265 src
= isl_basic_map_order_divs(src
);
7266 dst
= isl_basic_map_cow(dst
);
7267 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7268 src
->n_div
, 0, 2 * src
->n_div
);
7271 for (i
= 0; i
< src
->n_div
; ++i
) {
7272 int j
= find_div(dst
, src
, i
);
7274 j
= isl_basic_map_alloc_div(dst
);
7277 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7278 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7279 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7283 isl_basic_map_swap_div(dst
, i
, j
);
7287 isl_basic_map_free(dst
);
7291 struct isl_basic_set
*isl_basic_set_align_divs(
7292 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7294 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7295 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7298 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7306 map
= isl_map_compute_divs(map
);
7307 map
= isl_map_cow(map
);
7311 for (i
= 1; i
< map
->n
; ++i
)
7312 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7313 for (i
= 1; i
< map
->n
; ++i
)
7314 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7316 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7320 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7322 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7325 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7326 __isl_take isl_map
*map
)
7330 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7331 map
= isl_map_intersect_domain(map
, set
);
7332 set
= isl_map_range(map
);
7340 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7341 __isl_take isl_map
*map
)
7343 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7346 /* There is no need to cow as removing empty parts doesn't change
7347 * the meaning of the set.
7349 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7356 for (i
= map
->n
- 1; i
>= 0; --i
)
7357 remove_if_empty(map
, i
);
7362 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7364 return (struct isl_set
*)
7365 isl_map_remove_empty_parts((struct isl_map
*)set
);
7368 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7370 struct isl_basic_map
*bmap
;
7371 if (!map
|| map
->n
== 0)
7373 bmap
= map
->p
[map
->n
-1];
7374 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7375 return isl_basic_map_copy(bmap
);
7378 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7380 return (struct isl_basic_set
*)
7381 isl_map_copy_basic_map((struct isl_map
*)set
);
7384 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7385 __isl_keep isl_basic_map
*bmap
)
7391 for (i
= map
->n
-1; i
>= 0; --i
) {
7392 if (map
->p
[i
] != bmap
)
7394 map
= isl_map_cow(map
);
7397 isl_basic_map_free(map
->p
[i
]);
7398 if (i
!= map
->n
-1) {
7399 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7400 map
->p
[i
] = map
->p
[map
->n
-1];
7411 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7412 struct isl_basic_set
*bset
)
7414 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7415 (struct isl_basic_map
*)bset
);
7418 /* Given two basic sets bset1 and bset2, compute the maximal difference
7419 * between the values of dimension pos in bset1 and those in bset2
7420 * for any common value of the parameters and dimensions preceding pos.
7422 static enum isl_lp_result
basic_set_maximal_difference_at(
7423 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7424 int pos
, isl_int
*opt
)
7427 struct isl_basic_map
*bmap1
= NULL
;
7428 struct isl_basic_map
*bmap2
= NULL
;
7429 struct isl_ctx
*ctx
;
7430 struct isl_vec
*obj
;
7433 unsigned dim1
, dim2
;
7434 enum isl_lp_result res
;
7436 if (!bset1
|| !bset2
)
7437 return isl_lp_error
;
7439 nparam
= isl_basic_set_n_param(bset1
);
7440 dim1
= isl_basic_set_n_dim(bset1
);
7441 dim2
= isl_basic_set_n_dim(bset2
);
7442 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7443 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7444 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7445 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7446 if (!bmap1
|| !bmap2
)
7448 bmap1
= isl_basic_map_cow(bmap1
);
7449 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7450 pos
, (dim1
- pos
) + (dim2
- pos
),
7451 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7452 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7455 total
= isl_basic_map_total_dim(bmap1
);
7457 obj
= isl_vec_alloc(ctx
, 1 + total
);
7458 isl_seq_clr(obj
->block
.data
, 1 + total
);
7459 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7460 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7463 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7465 isl_basic_map_free(bmap1
);
7469 isl_basic_map_free(bmap1
);
7470 isl_basic_map_free(bmap2
);
7471 return isl_lp_error
;
7474 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7475 * for any common value of the parameters and dimensions preceding pos
7476 * in both basic sets, the values of dimension pos in bset1 are
7477 * smaller or larger than those in bset2.
7480 * 1 if bset1 follows bset2
7481 * -1 if bset1 precedes bset2
7482 * 0 if bset1 and bset2 are incomparable
7483 * -2 if some error occurred.
7485 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7486 struct isl_basic_set
*bset2
, int pos
)
7489 enum isl_lp_result res
;
7494 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7496 if (res
== isl_lp_empty
)
7498 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7499 res
== isl_lp_unbounded
)
7501 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7510 /* Given two basic sets bset1 and bset2, check whether
7511 * for any common value of the parameters and dimensions preceding pos
7512 * there is a value of dimension pos in bset1 that is larger
7513 * than a value of the same dimension in bset2.
7516 * 1 if there exists such a pair
7517 * 0 if there is no such pair, but there is a pair of equal values
7519 * -2 if some error occurred.
7521 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7522 __isl_keep isl_basic_set
*bset2
, int pos
)
7525 enum isl_lp_result res
;
7530 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7532 if (res
== isl_lp_empty
)
7534 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7535 res
== isl_lp_unbounded
)
7537 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7539 else if (res
== isl_lp_ok
)
7548 /* Given two sets set1 and set2, check whether
7549 * for any common value of the parameters and dimensions preceding pos
7550 * there is a value of dimension pos in set1 that is larger
7551 * than a value of the same dimension in set2.
7554 * 1 if there exists such a pair
7555 * 0 if there is no such pair, but there is a pair of equal values
7557 * -2 if some error occurred.
7559 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7560 __isl_keep isl_set
*set2
, int pos
)
7568 for (i
= 0; i
< set1
->n
; ++i
)
7569 for (j
= 0; j
< set2
->n
; ++j
) {
7571 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7572 if (f
== 1 || f
== -2)
7581 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7582 unsigned pos
, isl_int
*val
)
7590 total
= isl_basic_map_total_dim(bmap
);
7591 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7592 for (; d
+1 > pos
; --d
)
7593 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7597 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7599 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7601 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7604 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7610 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7611 unsigned pos
, isl_int
*val
)
7623 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7626 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7627 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7628 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7629 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7633 isl_int_set(*val
, v
);
7639 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7640 unsigned pos
, isl_int
*val
)
7642 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7646 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7649 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7652 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7653 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7655 if (pos
>= isl_basic_map_dim(bmap
, type
))
7657 return isl_basic_map_plain_has_fixed_var(bmap
,
7658 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7661 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7662 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7664 if (pos
>= isl_map_dim(map
, type
))
7666 return isl_map_plain_has_fixed_var(map
,
7667 map_offset(map
, type
) - 1 + pos
, val
);
7670 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7671 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7673 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7676 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7677 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7679 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7682 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7683 * then return this fixed value in *val.
7685 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7686 unsigned dim
, isl_int
*val
)
7688 return isl_basic_set_plain_has_fixed_var(bset
,
7689 isl_basic_set_n_param(bset
) + dim
, val
);
7692 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7693 * then return this fixed value in *val.
7695 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7696 unsigned dim
, isl_int
*val
)
7698 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7701 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7702 unsigned dim
, isl_int
*val
)
7704 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7707 /* Check if input variable in has fixed value and if so and if val is not NULL,
7708 * then return this fixed value in *val.
7710 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7711 unsigned in
, isl_int
*val
)
7713 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7716 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7717 * and if val is not NULL, then return this lower bound in *val.
7719 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7720 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7722 int i
, i_eq
= -1, i_ineq
= -1;
7729 total
= isl_basic_set_total_dim(bset
);
7730 nparam
= isl_basic_set_n_param(bset
);
7731 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7732 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7738 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7739 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7741 if (i_eq
!= -1 || i_ineq
!= -1)
7745 if (i_eq
== -1 && i_ineq
== -1)
7747 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7748 /* The coefficient should always be one due to normalization. */
7749 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7751 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7753 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7754 total
- nparam
- dim
- 1) != -1)
7757 isl_int_neg(*val
, c
[0]);
7761 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7762 unsigned dim
, isl_int
*val
)
7774 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7778 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7780 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7781 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7783 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7787 isl_int_set(*val
, v
);
7798 /* uset_gist depends on constraints without existentially quantified
7799 * variables sorting first.
7801 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7803 const struct constraint
*c1
= (const struct constraint
*)p1
;
7804 const struct constraint
*c2
= (const struct constraint
*)p2
;
7806 unsigned size
= isl_min(c1
->size
, c2
->size
);
7808 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7809 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7814 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7817 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7818 struct isl_basic_map
*bmap
)
7821 struct constraint
*c
;
7826 total
= isl_basic_map_total_dim(bmap
);
7827 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7830 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7832 c
[i
].c
= bmap
->ineq
[i
];
7834 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7835 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7836 bmap
->ineq
[i
] = c
[i
].c
;
7840 isl_basic_map_free(bmap
);
7844 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7845 __isl_take isl_basic_set
*bset
)
7847 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7848 (struct isl_basic_map
*)bset
);
7851 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7855 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7857 bmap
= isl_basic_map_remove_redundancies(bmap
);
7858 bmap
= isl_basic_map_sort_constraints(bmap
);
7859 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7863 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7865 return (struct isl_basic_set
*)isl_basic_map_normalize(
7866 (struct isl_basic_map
*)bset
);
7869 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7870 const __isl_keep isl_basic_map
*bmap2
)
7877 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7878 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7879 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7880 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7881 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7882 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7883 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7884 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7885 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7886 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7887 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7889 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7891 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7893 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7894 return bmap1
->n_eq
- bmap2
->n_eq
;
7895 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7896 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7897 if (bmap1
->n_div
!= bmap2
->n_div
)
7898 return bmap1
->n_div
- bmap2
->n_div
;
7899 total
= isl_basic_map_total_dim(bmap1
);
7900 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7901 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7905 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7906 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7910 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7911 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7918 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7919 const __isl_keep isl_basic_set
*bset2
)
7921 return isl_basic_map_plain_cmp(bset1
, bset2
);
7924 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7925 const __isl_keep isl_set
*set2
)
7931 if (set1
->n
!= set2
->n
)
7932 return set1
->n
- set2
->n
;
7934 for (i
= 0; i
< set1
->n
; ++i
) {
7935 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7943 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7944 __isl_keep isl_basic_map
*bmap2
)
7946 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7949 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7950 __isl_keep isl_basic_set
*bset2
)
7952 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7953 (isl_basic_map
*)bset2
);
7956 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7958 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7959 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7961 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7964 /* We normalize in place, but if anything goes wrong we need
7965 * to return NULL, so we need to make sure we don't change the
7966 * meaning of any possible other copies of map.
7968 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7971 struct isl_basic_map
*bmap
;
7975 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7977 for (i
= 0; i
< map
->n
; ++i
) {
7978 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7981 isl_basic_map_free(map
->p
[i
]);
7984 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7985 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7986 map
= isl_map_remove_empty_parts(map
);
7989 for (i
= map
->n
- 1; i
>= 1; --i
) {
7990 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7992 isl_basic_map_free(map
->p
[i
-1]);
7993 for (j
= i
; j
< map
->n
; ++j
)
7994 map
->p
[j
-1] = map
->p
[j
];
8004 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8006 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8009 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8019 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8022 map1
= isl_map_copy(map1
);
8023 map2
= isl_map_copy(map2
);
8024 map1
= isl_map_normalize(map1
);
8025 map2
= isl_map_normalize(map2
);
8028 equal
= map1
->n
== map2
->n
;
8029 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8030 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8043 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8045 return isl_map_plain_is_equal(map1
, map2
);
8048 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8050 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8051 (struct isl_map
*)set2
);
8054 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8056 return isl_set_plain_is_equal(set1
, set2
);
8059 /* Return an interval that ranges from min to max (inclusive)
8061 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8062 isl_int min
, isl_int max
)
8065 struct isl_basic_set
*bset
= NULL
;
8067 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8071 k
= isl_basic_set_alloc_inequality(bset
);
8074 isl_int_set_si(bset
->ineq
[k
][1], 1);
8075 isl_int_neg(bset
->ineq
[k
][0], min
);
8077 k
= isl_basic_set_alloc_inequality(bset
);
8080 isl_int_set_si(bset
->ineq
[k
][1], -1);
8081 isl_int_set(bset
->ineq
[k
][0], max
);
8085 isl_basic_set_free(bset
);
8089 /* Return the Cartesian product of the basic sets in list (in the given order).
8091 __isl_give isl_basic_set
*isl_basic_set_list_product(
8092 __isl_take
struct isl_basic_set_list
*list
)
8100 struct isl_basic_set
*product
= NULL
;
8104 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8105 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8106 nparam
= isl_basic_set_n_param(list
->p
[0]);
8107 dim
= isl_basic_set_n_dim(list
->p
[0]);
8108 extra
= list
->p
[0]->n_div
;
8109 n_eq
= list
->p
[0]->n_eq
;
8110 n_ineq
= list
->p
[0]->n_ineq
;
8111 for (i
= 1; i
< list
->n
; ++i
) {
8112 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8113 isl_assert(list
->ctx
,
8114 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8115 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8116 extra
+= list
->p
[i
]->n_div
;
8117 n_eq
+= list
->p
[i
]->n_eq
;
8118 n_ineq
+= list
->p
[i
]->n_ineq
;
8120 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
8125 for (i
= 0; i
< list
->n
; ++i
) {
8126 isl_basic_set_add_constraints(product
,
8127 isl_basic_set_copy(list
->p
[i
]), dim
);
8128 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8130 isl_basic_set_list_free(list
);
8133 isl_basic_set_free(product
);
8134 isl_basic_set_list_free(list
);
8138 struct isl_basic_map
*isl_basic_map_product(
8139 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8141 isl_space
*dim_result
= NULL
;
8142 struct isl_basic_map
*bmap
;
8143 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8144 struct isl_dim_map
*dim_map1
, *dim_map2
;
8146 if (!bmap1
|| !bmap2
)
8149 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8150 bmap2
->dim
, isl_dim_param
), goto error
);
8151 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8152 isl_space_copy(bmap2
->dim
));
8154 in1
= isl_basic_map_n_in(bmap1
);
8155 in2
= isl_basic_map_n_in(bmap2
);
8156 out1
= isl_basic_map_n_out(bmap1
);
8157 out2
= isl_basic_map_n_out(bmap2
);
8158 nparam
= isl_basic_map_n_param(bmap1
);
8160 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8161 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8162 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8163 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8164 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8165 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8166 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8167 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8168 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8169 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8170 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8172 bmap
= isl_basic_map_alloc_space(dim_result
,
8173 bmap1
->n_div
+ bmap2
->n_div
,
8174 bmap1
->n_eq
+ bmap2
->n_eq
,
8175 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8176 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8177 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8178 bmap
= isl_basic_map_simplify(bmap
);
8179 return isl_basic_map_finalize(bmap
);
8181 isl_basic_map_free(bmap1
);
8182 isl_basic_map_free(bmap2
);
8186 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8187 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8189 isl_basic_map
*prod
;
8191 prod
= isl_basic_map_product(bmap1
, bmap2
);
8192 prod
= isl_basic_map_flatten(prod
);
8196 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8197 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8199 return isl_basic_map_flat_range_product(bset1
, bset2
);
8202 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8203 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8205 isl_space
*space_result
= NULL
;
8206 isl_basic_map
*bmap
;
8207 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8208 struct isl_dim_map
*dim_map1
, *dim_map2
;
8210 if (!bmap1
|| !bmap2
)
8213 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8214 isl_space_copy(bmap2
->dim
));
8216 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8217 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8218 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8219 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8221 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8222 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8223 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8224 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8225 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8226 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8227 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8228 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8229 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8230 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8231 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8233 bmap
= isl_basic_map_alloc_space(space_result
,
8234 bmap1
->n_div
+ bmap2
->n_div
,
8235 bmap1
->n_eq
+ bmap2
->n_eq
,
8236 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8237 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8238 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8239 bmap
= isl_basic_map_simplify(bmap
);
8240 return isl_basic_map_finalize(bmap
);
8242 isl_basic_map_free(bmap1
);
8243 isl_basic_map_free(bmap2
);
8247 __isl_give isl_basic_map
*isl_basic_map_range_product(
8248 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8250 isl_space
*dim_result
= NULL
;
8251 isl_basic_map
*bmap
;
8252 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8253 struct isl_dim_map
*dim_map1
, *dim_map2
;
8255 if (!bmap1
|| !bmap2
)
8258 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8259 isl_space_copy(bmap2
->dim
));
8261 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8262 out1
= isl_basic_map_n_out(bmap1
);
8263 out2
= isl_basic_map_n_out(bmap2
);
8264 nparam
= isl_basic_map_n_param(bmap1
);
8266 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8267 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8268 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8269 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8270 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8271 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8272 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8273 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8274 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8275 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8276 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8278 bmap
= isl_basic_map_alloc_space(dim_result
,
8279 bmap1
->n_div
+ bmap2
->n_div
,
8280 bmap1
->n_eq
+ bmap2
->n_eq
,
8281 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8282 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8283 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8284 bmap
= isl_basic_map_simplify(bmap
);
8285 return isl_basic_map_finalize(bmap
);
8287 isl_basic_map_free(bmap1
);
8288 isl_basic_map_free(bmap2
);
8292 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8293 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8295 isl_basic_map
*prod
;
8297 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8298 prod
= isl_basic_map_flatten_range(prod
);
8302 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8303 __isl_take isl_map
*map2
,
8304 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8305 __isl_take isl_space
*right
),
8306 __isl_give isl_basic_map
*(*basic_map_product
)(
8307 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8310 struct isl_map
*result
;
8316 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8317 map2
->dim
, isl_dim_param
), goto error
);
8319 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8320 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8321 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8323 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8324 isl_space_copy(map2
->dim
)),
8325 map1
->n
* map2
->n
, flags
);
8328 for (i
= 0; i
< map1
->n
; ++i
)
8329 for (j
= 0; j
< map2
->n
; ++j
) {
8330 struct isl_basic_map
*part
;
8331 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8332 isl_basic_map_copy(map2
->p
[j
]));
8333 if (isl_basic_map_is_empty(part
))
8334 isl_basic_map_free(part
);
8336 result
= isl_map_add_basic_map(result
, part
);
8349 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8351 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8352 __isl_take isl_map
*map2
)
8354 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8357 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8358 __isl_take isl_map
*map2
)
8360 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8363 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8365 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8366 __isl_take isl_map
*map2
)
8370 prod
= isl_map_product(map1
, map2
);
8371 prod
= isl_map_flatten(prod
);
8375 /* Given two set A and B, construct its Cartesian product A x B.
8377 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8379 return isl_map_range_product(set1
, set2
);
8382 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8383 __isl_take isl_set
*set2
)
8385 return isl_map_flat_range_product(set1
, set2
);
8388 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8390 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8391 __isl_take isl_map
*map2
)
8393 return map_product(map1
, map2
, &isl_space_domain_product
,
8394 &isl_basic_map_domain_product
);
8397 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8399 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8400 __isl_take isl_map
*map2
)
8402 return map_product(map1
, map2
, &isl_space_range_product
,
8403 &isl_basic_map_range_product
);
8406 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8407 __isl_take isl_map
*map2
)
8409 return isl_map_align_params_map_map_and(map1
, map2
,
8410 &map_domain_product_aligned
);
8413 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8414 __isl_take isl_map
*map2
)
8416 return isl_map_align_params_map_map_and(map1
, map2
,
8417 &map_range_product_aligned
);
8420 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8422 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8423 __isl_take isl_map
*map2
)
8427 prod
= isl_map_domain_product(map1
, map2
);
8428 prod
= isl_map_flatten_domain(prod
);
8432 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8434 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8435 __isl_take isl_map
*map2
)
8439 prod
= isl_map_range_product(map1
, map2
);
8440 prod
= isl_map_flatten_range(prod
);
8444 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8447 uint32_t hash
= isl_hash_init();
8452 bmap
= isl_basic_map_copy(bmap
);
8453 bmap
= isl_basic_map_normalize(bmap
);
8456 total
= isl_basic_map_total_dim(bmap
);
8457 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8458 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8460 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8461 isl_hash_hash(hash
, c_hash
);
8463 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8464 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8466 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8467 isl_hash_hash(hash
, c_hash
);
8469 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8470 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8472 if (isl_int_is_zero(bmap
->div
[i
][0]))
8474 isl_hash_byte(hash
, i
& 0xFF);
8475 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8476 isl_hash_hash(hash
, c_hash
);
8478 isl_basic_map_free(bmap
);
8482 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8484 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8487 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8494 map
= isl_map_copy(map
);
8495 map
= isl_map_normalize(map
);
8499 hash
= isl_hash_init();
8500 for (i
= 0; i
< map
->n
; ++i
) {
8502 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8503 isl_hash_hash(hash
, bmap_hash
);
8511 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8513 return isl_map_get_hash((isl_map
*)set
);
8516 /* Check if the value for dimension dim is completely determined
8517 * by the values of the other parameters and variables.
8518 * That is, check if dimension dim is involved in an equality.
8520 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8527 nparam
= isl_basic_set_n_param(bset
);
8528 for (i
= 0; i
< bset
->n_eq
; ++i
)
8529 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8534 /* Check if the value for dimension dim is completely determined
8535 * by the values of the other parameters and variables.
8536 * That is, check if dimension dim is involved in an equality
8537 * for each of the subsets.
8539 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8545 for (i
= 0; i
< set
->n
; ++i
) {
8547 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8554 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8556 return set
? set
->n
: 0;
8559 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8560 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8567 for (i
= 0; i
< map
->n
; ++i
)
8568 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8574 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8575 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8582 for (i
= 0; i
< set
->n
; ++i
)
8583 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8589 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8596 bset
= isl_basic_set_cow(bset
);
8600 dim
= isl_basic_set_get_space(bset
);
8601 dim
= isl_space_lift(dim
, bset
->n_div
);
8604 isl_space_free(bset
->dim
);
8606 bset
->extra
-= bset
->n_div
;
8609 bset
= isl_basic_set_finalize(bset
);
8613 isl_basic_set_free(bset
);
8617 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8623 set
= isl_set_align_divs(set
);
8628 set
= isl_set_cow(set
);
8632 n_div
= set
->p
[0]->n_div
;
8633 dim
= isl_set_get_space(set
);
8634 dim
= isl_space_lift(dim
, n_div
);
8637 isl_space_free(set
->dim
);
8640 for (i
= 0; i
< set
->n
; ++i
) {
8641 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8652 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8655 struct isl_basic_map
*bmap
;
8662 set
= isl_set_align_divs(set
);
8667 dim
= isl_set_get_space(set
);
8668 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8670 return isl_map_identity(isl_space_map_from_set(dim
));
8673 n_div
= set
->p
[0]->n_div
;
8674 dim
= isl_space_map_from_set(dim
);
8675 n_param
= isl_space_dim(dim
, isl_dim_param
);
8676 n_set
= isl_space_dim(dim
, isl_dim_in
);
8677 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8678 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8679 for (i
= 0; i
< n_set
; ++i
)
8680 bmap
= var_equal(bmap
, i
);
8682 total
= n_param
+ n_set
+ n_set
+ n_div
;
8683 for (i
= 0; i
< n_div
; ++i
) {
8684 k
= isl_basic_map_alloc_inequality(bmap
);
8687 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8688 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8689 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8690 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8691 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8692 set
->p
[0]->div
[i
][0]);
8694 l
= isl_basic_map_alloc_inequality(bmap
);
8697 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8698 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8699 set
->p
[0]->div
[i
][0]);
8700 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8704 bmap
= isl_basic_map_simplify(bmap
);
8705 bmap
= isl_basic_map_finalize(bmap
);
8706 return isl_map_from_basic_map(bmap
);
8709 isl_basic_map_free(bmap
);
8713 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8721 dim
= isl_basic_set_total_dim(bset
);
8722 size
+= bset
->n_eq
* (1 + dim
);
8723 size
+= bset
->n_ineq
* (1 + dim
);
8724 size
+= bset
->n_div
* (2 + dim
);
8729 int isl_set_size(__isl_keep isl_set
*set
)
8737 for (i
= 0; i
< set
->n
; ++i
)
8738 size
+= isl_basic_set_size(set
->p
[i
]);
8743 /* Check if there is any lower bound (if lower == 0) and/or upper
8744 * bound (if upper == 0) on the specified dim.
8746 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8747 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8754 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8756 pos
+= isl_basic_map_offset(bmap
, type
);
8758 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8759 if (isl_int_is_zero(bmap
->div
[i
][0]))
8761 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8765 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8766 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8769 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8770 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8777 return lower
&& upper
;
8780 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8781 enum isl_dim_type type
, unsigned pos
)
8783 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8786 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8787 enum isl_dim_type type
, unsigned pos
)
8789 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8792 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8793 enum isl_dim_type type
, unsigned pos
)
8795 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8798 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8799 enum isl_dim_type type
, unsigned pos
)
8806 for (i
= 0; i
< map
->n
; ++i
) {
8808 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8809 if (bounded
< 0 || !bounded
)
8816 /* Return 1 if the specified dim is involved in both an upper bound
8817 * and a lower bound.
8819 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8820 enum isl_dim_type type
, unsigned pos
)
8822 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8825 static int has_bound(__isl_keep isl_map
*map
,
8826 enum isl_dim_type type
, unsigned pos
,
8827 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8828 enum isl_dim_type type
, unsigned pos
))
8835 for (i
= 0; i
< map
->n
; ++i
) {
8837 bounded
= fn(map
->p
[i
], type
, pos
);
8838 if (bounded
< 0 || bounded
)
8845 /* Return 1 if the specified dim is involved in any lower bound.
8847 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8848 enum isl_dim_type type
, unsigned pos
)
8850 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8853 /* Return 1 if the specified dim is involved in any upper bound.
8855 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8856 enum isl_dim_type type
, unsigned pos
)
8858 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8861 /* For each of the "n" variables starting at "first", determine
8862 * the sign of the variable and put the results in the first "n"
8863 * elements of the array "signs".
8865 * 1 means that the variable is non-negative
8866 * -1 means that the variable is non-positive
8867 * 0 means the variable attains both positive and negative values.
8869 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8870 unsigned first
, unsigned n
, int *signs
)
8872 isl_vec
*bound
= NULL
;
8873 struct isl_tab
*tab
= NULL
;
8874 struct isl_tab_undo
*snap
;
8877 if (!bset
|| !signs
)
8880 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8881 tab
= isl_tab_from_basic_set(bset
, 0);
8885 isl_seq_clr(bound
->el
, bound
->size
);
8886 isl_int_set_si(bound
->el
[0], -1);
8888 snap
= isl_tab_snap(tab
);
8889 for (i
= 0; i
< n
; ++i
) {
8892 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8893 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8896 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8897 if (isl_tab_rollback(tab
, snap
) < 0)
8905 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8906 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8909 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8910 if (isl_tab_rollback(tab
, snap
) < 0)
8913 signs
[i
] = empty
? -1 : 0;
8917 isl_vec_free(bound
);
8921 isl_vec_free(bound
);
8925 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8926 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8928 if (!bset
|| !signs
)
8930 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8933 first
+= pos(bset
->dim
, type
) - 1;
8934 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8937 /* Check if the given basic map is obviously single-valued.
8938 * In particular, for each output dimension, check that there is
8939 * an equality that defines the output dimension in terms of
8940 * earlier dimensions.
8942 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8952 total
= 1 + isl_basic_map_total_dim(bmap
);
8953 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8954 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8956 for (i
= 0; i
< n_out
; ++i
) {
8957 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8958 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8960 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8961 total
- (o_out
+ i
+ 1)) == -1)
8964 if (j
>= bmap
->n_eq
)
8971 /* Check if the given basic map is single-valued.
8976 * and check if the result is a subset of the identity mapping.
8978 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8981 isl_basic_map
*test
;
8985 sv
= isl_basic_map_plain_is_single_valued(bmap
);
8989 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
8990 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
8992 space
= isl_basic_map_get_space(bmap
);
8993 space
= isl_space_map_from_set(isl_space_range(space
));
8994 id
= isl_basic_map_identity(space
);
8996 sv
= isl_basic_map_is_subset(test
, id
);
8998 isl_basic_map_free(test
);
8999 isl_basic_map_free(id
);
9004 /* Check if the given map is obviously single-valued.
9006 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9015 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9018 /* Check if the given map is single-valued.
9023 * and check if the result is a subset of the identity mapping.
9025 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9032 sv
= isl_map_plain_is_single_valued(map
);
9036 test
= isl_map_reverse(isl_map_copy(map
));
9037 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9039 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9040 id
= isl_map_identity(dim
);
9042 sv
= isl_map_is_subset(test
, id
);
9050 int isl_map_is_injective(__isl_keep isl_map
*map
)
9054 map
= isl_map_copy(map
);
9055 map
= isl_map_reverse(map
);
9056 in
= isl_map_is_single_valued(map
);
9062 /* Check if the given map is obviously injective.
9064 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9068 map
= isl_map_copy(map
);
9069 map
= isl_map_reverse(map
);
9070 in
= isl_map_plain_is_single_valued(map
);
9076 int isl_map_is_bijective(__isl_keep isl_map
*map
)
9080 sv
= isl_map_is_single_valued(map
);
9084 return isl_map_is_injective(map
);
9087 int isl_set_is_singleton(__isl_keep isl_set
*set
)
9089 return isl_map_is_single_valued((isl_map
*)set
);
9092 int isl_map_is_translation(__isl_keep isl_map
*map
)
9097 delta
= isl_map_deltas(isl_map_copy(map
));
9098 ok
= isl_set_is_singleton(delta
);
9099 isl_set_free(delta
);
9104 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
9106 if (isl_seq_first_non_zero(p
, pos
) != -1)
9108 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
9113 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
9122 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
9125 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
9126 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
9127 for (j
= 0; j
< nvar
; ++j
) {
9128 int lower
= 0, upper
= 0;
9129 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9130 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
9132 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
9138 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9139 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
9141 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
9143 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
9148 if (!lower
|| !upper
)
9155 int isl_set_is_box(__isl_keep isl_set
*set
)
9162 return isl_basic_set_is_box(set
->p
[0]);
9165 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9170 return isl_space_is_wrapping(bset
->dim
);
9173 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9178 return isl_space_is_wrapping(set
->dim
);
9181 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9183 bmap
= isl_basic_map_cow(bmap
);
9187 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9191 bmap
= isl_basic_map_finalize(bmap
);
9193 return (isl_basic_set
*)bmap
;
9195 isl_basic_map_free(bmap
);
9199 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9203 map
= isl_map_cow(map
);
9207 for (i
= 0; i
< map
->n
; ++i
) {
9208 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9212 map
->dim
= isl_space_wrap(map
->dim
);
9216 return (isl_set
*)map
;
9222 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9224 bset
= isl_basic_set_cow(bset
);
9228 bset
->dim
= isl_space_unwrap(bset
->dim
);
9232 bset
= isl_basic_set_finalize(bset
);
9234 return (isl_basic_map
*)bset
;
9236 isl_basic_set_free(bset
);
9240 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9247 if (!isl_set_is_wrapping(set
))
9248 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9251 set
= isl_set_cow(set
);
9255 for (i
= 0; i
< set
->n
; ++i
) {
9256 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9261 set
->dim
= isl_space_unwrap(set
->dim
);
9265 return (isl_map
*)set
;
9271 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9272 enum isl_dim_type type
)
9277 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9280 bmap
= isl_basic_map_cow(bmap
);
9284 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9288 bmap
= isl_basic_map_finalize(bmap
);
9292 isl_basic_map_free(bmap
);
9296 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9297 enum isl_dim_type type
)
9304 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9307 map
= isl_map_cow(map
);
9311 for (i
= 0; i
< map
->n
; ++i
) {
9312 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9316 map
->dim
= isl_space_reset(map
->dim
, type
);
9326 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9331 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9334 bmap
= isl_basic_map_cow(bmap
);
9338 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9342 bmap
= isl_basic_map_finalize(bmap
);
9346 isl_basic_map_free(bmap
);
9350 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9352 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9355 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9356 __isl_take isl_basic_map
*bmap
)
9361 if (!bmap
->dim
->nested
[0])
9364 bmap
= isl_basic_map_cow(bmap
);
9368 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9372 bmap
= isl_basic_map_finalize(bmap
);
9376 isl_basic_map_free(bmap
);
9380 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9381 __isl_take isl_basic_map
*bmap
)
9386 if (!bmap
->dim
->nested
[1])
9389 bmap
= isl_basic_map_cow(bmap
);
9393 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9397 bmap
= isl_basic_map_finalize(bmap
);
9401 isl_basic_map_free(bmap
);
9405 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9412 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9415 map
= isl_map_cow(map
);
9419 for (i
= 0; i
< map
->n
; ++i
) {
9420 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9424 map
->dim
= isl_space_flatten(map
->dim
);
9434 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9436 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9439 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9441 isl_space
*dim
, *flat_dim
;
9444 dim
= isl_set_get_space(set
);
9445 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9446 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9447 map
= isl_map_intersect_domain(map
, set
);
9452 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9459 if (!map
->dim
->nested
[0])
9462 map
= isl_map_cow(map
);
9466 for (i
= 0; i
< map
->n
; ++i
) {
9467 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9471 map
->dim
= isl_space_flatten_domain(map
->dim
);
9481 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9488 if (!map
->dim
->nested
[1])
9491 map
= isl_map_cow(map
);
9495 for (i
= 0; i
< map
->n
; ++i
) {
9496 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9500 map
->dim
= isl_space_flatten_range(map
->dim
);
9510 /* Reorder the dimensions of "bmap" according to the given dim_map
9511 * and set the dimension specification to "dim".
9513 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9514 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9519 bmap
= isl_basic_map_cow(bmap
);
9520 if (!bmap
|| !dim
|| !dim_map
)
9523 flags
= bmap
->flags
;
9524 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
9525 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
9526 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
9527 res
= isl_basic_map_alloc_space(dim
,
9528 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9529 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9532 res
= isl_basic_map_finalize(res
);
9536 isl_basic_map_free(bmap
);
9537 isl_space_free(dim
);
9541 /* Reorder the dimensions of "map" according to given reordering.
9543 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9544 __isl_take isl_reordering
*r
)
9547 struct isl_dim_map
*dim_map
;
9549 map
= isl_map_cow(map
);
9550 dim_map
= isl_dim_map_from_reordering(r
);
9551 if (!map
|| !r
|| !dim_map
)
9554 for (i
= 0; i
< map
->n
; ++i
) {
9555 struct isl_dim_map
*dim_map_i
;
9557 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9559 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9560 isl_space_copy(r
->dim
), dim_map_i
);
9566 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9568 isl_reordering_free(r
);
9574 isl_reordering_free(r
);
9578 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9579 __isl_take isl_reordering
*r
)
9581 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9584 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9585 __isl_take isl_space
*model
)
9592 ctx
= isl_space_get_ctx(model
);
9593 if (!isl_space_has_named_params(model
))
9594 isl_die(ctx
, isl_error_invalid
,
9595 "model has unnamed parameters", goto error
);
9596 if (!isl_space_has_named_params(map
->dim
))
9597 isl_die(ctx
, isl_error_invalid
,
9598 "relation has unnamed parameters", goto error
);
9599 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9600 isl_reordering
*exp
;
9602 model
= isl_space_drop_dims(model
, isl_dim_in
,
9603 0, isl_space_dim(model
, isl_dim_in
));
9604 model
= isl_space_drop_dims(model
, isl_dim_out
,
9605 0, isl_space_dim(model
, isl_dim_out
));
9606 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9607 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9608 map
= isl_map_realign(map
, exp
);
9611 isl_space_free(model
);
9614 isl_space_free(model
);
9619 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9620 __isl_take isl_space
*model
)
9622 return isl_map_align_params(set
, model
);
9625 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9626 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9627 enum isl_dim_type c2
, enum isl_dim_type c3
,
9628 enum isl_dim_type c4
, enum isl_dim_type c5
)
9630 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9631 struct isl_mat
*mat
;
9637 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9638 isl_basic_map_total_dim(bmap
) + 1);
9641 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9642 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9643 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9644 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9645 isl_int_set(mat
->row
[i
][pos
],
9646 bmap
->eq
[i
][off
+ k
]);
9654 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9655 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9656 enum isl_dim_type c2
, enum isl_dim_type c3
,
9657 enum isl_dim_type c4
, enum isl_dim_type c5
)
9659 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9660 struct isl_mat
*mat
;
9666 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9667 isl_basic_map_total_dim(bmap
) + 1);
9670 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9671 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9672 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9673 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9674 isl_int_set(mat
->row
[i
][pos
],
9675 bmap
->ineq
[i
][off
+ k
]);
9683 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9684 __isl_take isl_space
*dim
,
9685 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9686 enum isl_dim_type c2
, enum isl_dim_type c3
,
9687 enum isl_dim_type c4
, enum isl_dim_type c5
)
9689 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9690 isl_basic_map
*bmap
;
9696 if (!dim
|| !eq
|| !ineq
)
9699 if (eq
->n_col
!= ineq
->n_col
)
9700 isl_die(dim
->ctx
, isl_error_invalid
,
9701 "equalities and inequalities matrices should have "
9702 "same number of columns", goto error
);
9704 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9706 if (eq
->n_col
< total
)
9707 isl_die(dim
->ctx
, isl_error_invalid
,
9708 "number of columns too small", goto error
);
9710 extra
= eq
->n_col
- total
;
9712 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9713 eq
->n_row
, ineq
->n_row
);
9716 for (i
= 0; i
< extra
; ++i
) {
9717 k
= isl_basic_map_alloc_div(bmap
);
9720 isl_int_set_si(bmap
->div
[k
][0], 0);
9722 for (i
= 0; i
< eq
->n_row
; ++i
) {
9723 l
= isl_basic_map_alloc_equality(bmap
);
9726 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9727 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9728 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9729 isl_int_set(bmap
->eq
[l
][off
+ k
],
9735 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9736 l
= isl_basic_map_alloc_inequality(bmap
);
9739 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9740 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9741 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9742 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9749 isl_space_free(dim
);
9755 isl_space_free(dim
);
9761 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9762 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9763 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9765 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9766 c1
, c2
, c3
, c4
, isl_dim_in
);
9769 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9770 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9771 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9773 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9774 c1
, c2
, c3
, c4
, isl_dim_in
);
9777 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9778 __isl_take isl_space
*dim
,
9779 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9780 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9782 return (isl_basic_set
*)
9783 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9784 c1
, c2
, c3
, c4
, isl_dim_in
);
9787 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9792 return isl_space_can_zip(bmap
->dim
);
9795 int isl_map_can_zip(__isl_keep isl_map
*map
)
9800 return isl_space_can_zip(map
->dim
);
9803 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9804 * (A -> C) -> (B -> D).
9806 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9815 if (!isl_basic_map_can_zip(bmap
))
9816 isl_die(bmap
->ctx
, isl_error_invalid
,
9817 "basic map cannot be zipped", goto error
);
9818 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9819 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9820 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9821 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9822 bmap
= isl_basic_map_cow(bmap
);
9823 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9826 bmap
->dim
= isl_space_zip(bmap
->dim
);
9831 isl_basic_map_free(bmap
);
9835 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9836 * (A -> C) -> (B -> D).
9838 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9845 if (!isl_map_can_zip(map
))
9846 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9849 map
= isl_map_cow(map
);
9853 for (i
= 0; i
< map
->n
; ++i
) {
9854 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9859 map
->dim
= isl_space_zip(map
->dim
);
9869 /* Can we apply isl_basic_map_curry to "bmap"?
9870 * That is, does it have a nested relation in its domain?
9872 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
9877 return isl_space_can_curry(bmap
->dim
);
9880 /* Can we apply isl_map_curry to "map"?
9881 * That is, does it have a nested relation in its domain?
9883 int isl_map_can_curry(__isl_keep isl_map
*map
)
9888 return isl_space_can_curry(map
->dim
);
9891 /* Given a basic map (A -> B) -> C, return the corresponding basic map
9894 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
9900 if (!isl_basic_map_can_curry(bmap
))
9901 isl_die(bmap
->ctx
, isl_error_invalid
,
9902 "basic map cannot be curried", goto error
);
9903 bmap
->dim
= isl_space_curry(bmap
->dim
);
9908 isl_basic_map_free(bmap
);
9912 /* Given a map (A -> B) -> C, return the corresponding map
9915 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
9922 if (!isl_map_can_curry(map
))
9923 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
9926 map
= isl_map_cow(map
);
9930 for (i
= 0; i
< map
->n
; ++i
) {
9931 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
9936 map
->dim
= isl_space_curry(map
->dim
);
9946 /* Construct a basic map mapping the domain of the affine expression
9947 * to a one-dimensional range prescribed by the affine expression.
9949 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9953 isl_local_space
*ls
;
9954 isl_basic_map
*bmap
;
9959 ls
= isl_aff_get_local_space(aff
);
9960 bmap
= isl_basic_map_from_local_space(ls
);
9961 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9962 k
= isl_basic_map_alloc_equality(bmap
);
9966 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9967 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9968 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9969 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9970 aff
->v
->size
- (pos
+ 1));
9973 bmap
= isl_basic_map_finalize(bmap
);
9977 isl_basic_map_free(bmap
);
9981 /* Construct a map mapping the domain of the affine expression
9982 * to a one-dimensional range prescribed by the affine expression.
9984 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
9986 isl_basic_map
*bmap
;
9988 bmap
= isl_basic_map_from_aff(aff
);
9989 return isl_map_from_basic_map(bmap
);
9992 /* Construct a basic map mapping the domain the multi-affine expression
9993 * to its range, with each dimension in the range equated to the
9994 * corresponding affine expression.
9996 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9997 __isl_take isl_multi_aff
*maff
)
10001 isl_basic_map
*bmap
;
10006 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
10007 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
10008 "invalid space", return isl_multi_aff_free(maff
));
10010 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
10011 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
10013 for (i
= 0; i
< maff
->n
; ++i
) {
10015 isl_basic_map
*bmap_i
;
10017 aff
= isl_aff_copy(maff
->p
[i
]);
10018 bmap_i
= isl_basic_map_from_aff(aff
);
10020 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10023 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
10025 isl_multi_aff_free(maff
);
10029 /* Construct a map mapping the domain the multi-affine expression
10030 * to its range, with each dimension in the range equated to the
10031 * corresponding affine expression.
10033 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
10035 isl_basic_map
*bmap
;
10037 bmap
= isl_basic_map_from_multi_aff(maff
);
10038 return isl_map_from_basic_map(bmap
);
10041 /* Construct a basic map mapping a domain in the given space to
10042 * to an n-dimensional range, with n the number of elements in the list,
10043 * where each coordinate in the range is prescribed by the
10044 * corresponding affine expression.
10045 * The domains of all affine expressions in the list are assumed to match
10048 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
10049 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
10053 isl_basic_map
*bmap
;
10058 dim
= isl_space_from_domain(domain_dim
);
10059 bmap
= isl_basic_map_universe(dim
);
10061 for (i
= 0; i
< list
->n
; ++i
) {
10063 isl_basic_map
*bmap_i
;
10065 aff
= isl_aff_copy(list
->p
[i
]);
10066 bmap_i
= isl_basic_map_from_aff(aff
);
10068 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10071 isl_aff_list_free(list
);
10075 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
10076 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10078 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
10081 /* Construct a basic map where the given dimensions are equal to each other.
10083 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
10084 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10086 isl_basic_map
*bmap
= NULL
;
10092 if (pos1
>= isl_space_dim(space
, type1
))
10093 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10094 "index out of bounds", goto error
);
10095 if (pos2
>= isl_space_dim(space
, type2
))
10096 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10097 "index out of bounds", goto error
);
10099 if (type1
== type2
&& pos1
== pos2
)
10100 return isl_basic_map_universe(space
);
10102 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
10103 i
= isl_basic_map_alloc_equality(bmap
);
10106 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10107 pos1
+= isl_basic_map_offset(bmap
, type1
);
10108 pos2
+= isl_basic_map_offset(bmap
, type2
);
10109 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
10110 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10111 bmap
= isl_basic_map_finalize(bmap
);
10112 isl_space_free(space
);
10115 isl_space_free(space
);
10116 isl_basic_map_free(bmap
);
10120 /* Add a constraint imposing that the given two dimensions are equal.
10122 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
10123 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10127 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
10129 bmap
= isl_basic_map_intersect(bmap
, eq
);
10134 /* Add a constraint imposing that the given two dimensions are equal.
10136 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
10137 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10139 isl_basic_map
*bmap
;
10141 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
10143 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10148 /* Add a constraint imposing that the given two dimensions have opposite values.
10150 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
10151 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10153 isl_basic_map
*bmap
= NULL
;
10159 if (pos1
>= isl_map_dim(map
, type1
))
10160 isl_die(map
->ctx
, isl_error_invalid
,
10161 "index out of bounds", goto error
);
10162 if (pos2
>= isl_map_dim(map
, type2
))
10163 isl_die(map
->ctx
, isl_error_invalid
,
10164 "index out of bounds", goto error
);
10166 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
10167 i
= isl_basic_map_alloc_equality(bmap
);
10170 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10171 pos1
+= isl_basic_map_offset(bmap
, type1
);
10172 pos2
+= isl_basic_map_offset(bmap
, type2
);
10173 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
10174 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10175 bmap
= isl_basic_map_finalize(bmap
);
10177 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10181 isl_basic_map_free(bmap
);
10186 /* Add a constraint imposing that the value of the first dimension is
10187 * greater than that of the second.
10189 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
10190 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10192 isl_basic_map
*bmap
= NULL
;
10198 if (pos1
>= isl_map_dim(map
, type1
))
10199 isl_die(map
->ctx
, isl_error_invalid
,
10200 "index out of bounds", goto error
);
10201 if (pos2
>= isl_map_dim(map
, type2
))
10202 isl_die(map
->ctx
, isl_error_invalid
,
10203 "index out of bounds", goto error
);
10205 if (type1
== type2
&& pos1
== pos2
) {
10206 isl_space
*space
= isl_map_get_space(map
);
10208 return isl_map_empty(space
);
10211 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 0, 1);
10212 i
= isl_basic_map_alloc_inequality(bmap
);
10215 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10216 pos1
+= isl_basic_map_offset(bmap
, type1
);
10217 pos2
+= isl_basic_map_offset(bmap
, type2
);
10218 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
10219 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
10220 isl_int_set_si(bmap
->ineq
[i
][0], -1);
10221 bmap
= isl_basic_map_finalize(bmap
);
10223 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10227 isl_basic_map_free(bmap
);
10232 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
10236 isl_local_space
*ls
;
10241 if (!isl_basic_map_divs_known(bmap
))
10242 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
10243 "some divs are unknown", return NULL
);
10245 ls
= isl_basic_map_get_local_space(bmap
);
10246 div
= isl_local_space_get_div(ls
, pos
);
10247 isl_local_space_free(ls
);
10252 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
10255 return isl_basic_map_get_div(bset
, pos
);
10258 /* Plug in "subs" for dimension "type", "pos" of "bset".
10260 * Let i be the dimension to replace and let "subs" be of the form
10264 * Any integer division with a non-zero coefficient for i,
10266 * floor((a i + g)/m)
10270 * floor((a f + d g)/(m d))
10272 * Constraints of the form
10280 __isl_give isl_basic_set
*isl_basic_set_substitute(
10281 __isl_take isl_basic_set
*bset
,
10282 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10288 if (bset
&& isl_basic_set_plain_is_empty(bset
))
10291 bset
= isl_basic_set_cow(bset
);
10292 if (!bset
|| !subs
)
10295 ctx
= isl_basic_set_get_ctx(bset
);
10296 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
10297 isl_die(ctx
, isl_error_invalid
,
10298 "spaces don't match", goto error
);
10299 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
10300 isl_die(ctx
, isl_error_unsupported
,
10301 "cannot handle divs yet", goto error
);
10303 pos
+= isl_basic_set_offset(bset
, type
);
10307 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10308 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
10310 isl_int_set(v
, bset
->eq
[i
][pos
]);
10311 isl_int_set_si(bset
->eq
[i
][pos
], 0);
10312 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
10313 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10316 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10317 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
10319 isl_int_set(v
, bset
->ineq
[i
][pos
]);
10320 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
10321 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
10322 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10325 for (i
= 0; i
< bset
->n_div
; ++i
) {
10326 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
10328 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
10329 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
10330 isl_seq_combine(bset
->div
[i
] + 1,
10331 subs
->v
->el
[0], bset
->div
[i
] + 1,
10332 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10333 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
10338 bset
= isl_basic_set_simplify(bset
);
10339 return isl_basic_set_finalize(bset
);
10341 isl_basic_set_free(bset
);
10345 /* Plug in "subs" for dimension "type", "pos" of "set".
10347 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
10348 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10352 if (set
&& isl_set_plain_is_empty(set
))
10355 set
= isl_set_cow(set
);
10359 for (i
= set
->n
- 1; i
>= 0; --i
) {
10360 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
10361 if (remove_if_empty(set
, i
) < 0)