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>
34 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
37 case isl_dim_param
: return dim
->nparam
;
38 case isl_dim_in
: return dim
->n_in
;
39 case isl_dim_out
: return dim
->n_out
;
40 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
45 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return 1;
49 case isl_dim_in
: return 1 + dim
->nparam
;
50 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
56 enum isl_dim_type type
)
61 case isl_dim_cst
: return 1;
64 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
65 case isl_dim_div
: return bmap
->n_div
;
66 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
71 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
73 return map
? n(map
->dim
, type
) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
78 return set
? n(set
->dim
, type
) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
84 isl_space
*dim
= bmap
->dim
;
86 case isl_dim_cst
: return 0;
87 case isl_dim_param
: return 1;
88 case isl_dim_in
: return 1 + dim
->nparam
;
89 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
90 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
95 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
96 enum isl_dim_type type
)
98 return isl_basic_map_offset(bset
, type
);
101 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
103 return pos(map
->dim
, type
);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_dim(bset
, type
);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
114 return isl_basic_set_dim(bset
, isl_dim_set
);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
119 return isl_basic_set_dim(bset
, isl_dim_param
);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
124 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
127 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
129 return isl_set_dim(set
, isl_dim_set
);
132 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_param
);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
139 return bmap
? bmap
->dim
->n_in
: 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_out
: 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->nparam
: 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->n_div
: 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
159 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
162 unsigned isl_map_n_in(const struct isl_map
*map
)
164 return map
? map
->dim
->n_in
: 0;
167 unsigned isl_map_n_out(const struct isl_map
*map
)
169 return map
? map
->dim
->n_out
: 0;
172 unsigned isl_map_n_param(const struct isl_map
*map
)
174 return map
? map
->dim
->nparam
: 0;
177 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
182 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
185 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
188 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
189 struct isl_basic_set
*bset
)
194 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
197 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
200 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
205 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
208 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
211 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
212 struct isl_basic_set
*bset
)
217 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
220 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
223 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
225 return bmap
? bmap
->ctx
: NULL
;
228 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
230 return bset
? bset
->ctx
: NULL
;
233 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
235 return map
? map
->ctx
: NULL
;
238 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
240 return set
? set
->ctx
: NULL
;
243 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
247 return isl_space_copy(bmap
->dim
);
250 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
254 return isl_space_copy(bset
->dim
);
257 __isl_give isl_local_space
*isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map
*bmap
)
267 total
= isl_basic_map_total_dim(bmap
);
268 ls
= isl_local_space_alloc(isl_space_copy(bmap
->dim
), bmap
->n_div
);
272 for (i
= 0; i
< bmap
->n_div
; ++i
)
273 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
278 __isl_give isl_local_space
*isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set
*bset
)
281 return isl_basic_map_get_local_space(bset
);
284 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
285 __isl_take isl_local_space
*ls
)
294 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
295 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
296 n_div
, 0, 2 * n_div
);
298 for (i
= 0; i
< n_div
; ++i
)
299 if (isl_basic_map_alloc_div(bmap
) < 0)
302 for (i
= 0; i
< n_div
; ++i
) {
303 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
304 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
308 isl_local_space_free(ls
);
311 isl_local_space_free(ls
);
312 isl_basic_map_free(bmap
);
316 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
317 __isl_take isl_local_space
*ls
)
319 return isl_basic_map_from_local_space(ls
);
322 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
326 return isl_space_copy(map
->dim
);
329 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
333 return isl_space_copy(set
->dim
);
336 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
339 bmap
= isl_basic_map_cow(bmap
);
342 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
345 bmap
= isl_basic_map_finalize(bmap
);
348 isl_basic_map_free(bmap
);
352 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set
*bset
, const char *s
)
355 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
359 enum isl_dim_type type
)
361 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
364 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
365 enum isl_dim_type type
, const char *s
)
369 map
= isl_map_cow(map
);
373 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
377 for (i
= 0; i
< map
->n
; ++i
) {
378 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
389 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
390 enum isl_dim_type type
)
392 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
395 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
398 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
401 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
402 enum isl_dim_type type
, __isl_take isl_id
*id
)
404 map
= isl_map_cow(map
);
406 return isl_id_free(id
);
408 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
410 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
413 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
414 __isl_take isl_id
*id
)
416 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
419 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
420 enum isl_dim_type type
)
422 map
= isl_map_cow(map
);
426 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
428 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
431 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
433 return isl_map_reset_tuple_id(set
, isl_dim_set
);
436 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
438 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
441 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
442 enum isl_dim_type type
)
444 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
447 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
449 return isl_map_has_tuple_id(set
, isl_dim_set
);
452 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
454 return isl_map_get_tuple_id(set
, isl_dim_set
);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
459 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
464 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
468 enum isl_dim_type type
, unsigned pos
)
470 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
474 enum isl_dim_type type
, unsigned pos
)
476 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
479 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
480 enum isl_dim_type type
, unsigned pos
)
482 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
485 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
486 enum isl_dim_type type
, unsigned pos
)
488 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
491 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map
*bmap
,
493 enum isl_dim_type type
, unsigned pos
, const char *s
)
497 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
502 isl_basic_map_free(bmap
);
506 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
507 enum isl_dim_type type
, unsigned pos
, const char *s
)
514 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
518 for (i
= 0; i
< map
->n
; ++i
) {
519 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
530 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set
*bset
,
532 enum isl_dim_type type
, unsigned pos
, const char *s
)
534 return (isl_basic_set
*)isl_basic_map_set_dim_name(
535 (isl_basic_map
*)bset
, type
, pos
, s
);
538 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
539 enum isl_dim_type type
, unsigned pos
, const char *s
)
541 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
544 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
545 enum isl_dim_type type
, unsigned pos
)
547 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
550 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
551 enum isl_dim_type type
, unsigned pos
)
553 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
556 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
557 enum isl_dim_type type
, unsigned pos
)
559 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
562 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
563 enum isl_dim_type type
, unsigned pos
)
565 return isl_map_has_dim_id(set
, type
, pos
);
568 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
569 enum isl_dim_type type
, unsigned pos
)
571 return isl_map_get_dim_id(set
, type
, pos
);
574 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
575 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
577 map
= isl_map_cow(map
);
579 return isl_id_free(id
);
581 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
583 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
586 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
587 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
589 return isl_map_set_dim_id(set
, type
, pos
, id
);
592 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
593 __isl_keep isl_id
*id
)
597 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
600 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
601 __isl_keep isl_id
*id
)
603 return isl_map_find_dim_by_id(set
, type
, id
);
606 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
611 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
614 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
618 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
621 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
623 return isl_basic_map_is_rational(bset
);
626 /* Is this basic set a parameter domain?
628 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
632 return isl_space_is_params(bset
->dim
);
635 /* Is this set a parameter domain?
637 int isl_set_is_params(__isl_keep isl_set
*set
)
641 return isl_space_is_params(set
->dim
);
644 /* Is this map actually a parameter domain?
645 * Users should never call this function. Outside of isl,
646 * a map can never be a parameter domain.
648 int isl_map_is_params(__isl_keep isl_map
*map
)
652 return isl_space_is_params(map
->dim
);
655 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
656 struct isl_basic_map
*bmap
, unsigned extra
,
657 unsigned n_eq
, unsigned n_ineq
)
660 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
665 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
666 if (isl_blk_is_error(bmap
->block
))
669 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
674 bmap
->block2
= isl_blk_empty();
677 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
678 if (isl_blk_is_error(bmap
->block2
))
681 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
686 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
687 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
689 for (i
= 0; i
< extra
; ++i
)
690 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
694 bmap
->c_size
= n_eq
+ n_ineq
;
695 bmap
->eq
= bmap
->ineq
+ n_ineq
;
704 isl_basic_map_free(bmap
);
708 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
709 unsigned nparam
, unsigned dim
, unsigned extra
,
710 unsigned n_eq
, unsigned n_ineq
)
712 struct isl_basic_map
*bmap
;
715 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
719 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
720 return (struct isl_basic_set
*)bmap
;
723 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
724 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
726 struct isl_basic_map
*bmap
;
729 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
730 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
731 return (struct isl_basic_set
*)bmap
;
737 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
738 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
740 struct isl_basic_map
*bmap
;
744 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
749 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
755 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
756 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
757 unsigned n_eq
, unsigned n_ineq
)
759 struct isl_basic_map
*bmap
;
762 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
766 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
770 static void dup_constraints(
771 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
774 unsigned total
= isl_basic_map_total_dim(src
);
776 for (i
= 0; i
< src
->n_eq
; ++i
) {
777 int j
= isl_basic_map_alloc_equality(dst
);
778 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
781 for (i
= 0; i
< src
->n_ineq
; ++i
) {
782 int j
= isl_basic_map_alloc_inequality(dst
);
783 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
786 for (i
= 0; i
< src
->n_div
; ++i
) {
787 int j
= isl_basic_map_alloc_div(dst
);
788 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
790 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
793 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
795 struct isl_basic_map
*dup
;
799 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
800 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
803 dup_constraints(dup
, bmap
);
804 dup
->flags
= bmap
->flags
;
805 dup
->sample
= isl_vec_copy(bmap
->sample
);
809 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
811 struct isl_basic_map
*dup
;
813 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
814 return (struct isl_basic_set
*)dup
;
817 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
822 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
826 return isl_basic_set_dup(bset
);
829 struct isl_set
*isl_set_copy(struct isl_set
*set
)
838 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
843 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
847 bmap
= isl_basic_map_dup(bmap
);
849 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
853 struct isl_map
*isl_map_copy(struct isl_map
*map
)
862 void isl_basic_map_free(struct isl_basic_map
*bmap
)
870 isl_ctx_deref(bmap
->ctx
);
872 isl_blk_free(bmap
->ctx
, bmap
->block2
);
874 isl_blk_free(bmap
->ctx
, bmap
->block
);
875 isl_vec_free(bmap
->sample
);
876 isl_space_free(bmap
->dim
);
880 void isl_basic_set_free(struct isl_basic_set
*bset
)
882 isl_basic_map_free((struct isl_basic_map
*)bset
);
885 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
887 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
890 __isl_give isl_map
*isl_map_align_params_map_map_and(
891 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
892 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
893 __isl_take isl_map
*map2
))
897 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
898 return fn(map1
, map2
);
899 if (!isl_space_has_named_params(map1
->dim
) ||
900 !isl_space_has_named_params(map2
->dim
))
901 isl_die(map1
->ctx
, isl_error_invalid
,
902 "unaligned unnamed parameters", goto error
);
903 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
904 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
905 return fn(map1
, map2
);
912 static int align_params_map_map_and_test(__isl_keep isl_map
*map1
,
913 __isl_keep isl_map
*map2
,
914 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
920 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
921 return fn(map1
, map2
);
922 if (!isl_space_has_named_params(map1
->dim
) ||
923 !isl_space_has_named_params(map2
->dim
))
924 isl_die(map1
->ctx
, isl_error_invalid
,
925 "unaligned unnamed parameters", return -1);
926 map1
= isl_map_copy(map1
);
927 map2
= isl_map_copy(map2
);
928 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
929 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
936 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
942 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
943 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
945 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
946 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
947 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
948 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
949 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
950 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
952 int j
= isl_basic_map_alloc_inequality(bmap
);
956 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
957 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
964 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
965 bmap
->extra
- bmap
->n_div
);
969 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
971 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
974 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
978 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
983 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
985 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
988 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
993 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
995 if (pos
!= bmap
->n_eq
- 1) {
997 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
998 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1004 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1006 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1009 void isl_basic_map_inequality_to_equality(
1010 struct isl_basic_map
*bmap
, unsigned pos
)
1014 t
= bmap
->ineq
[pos
];
1015 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1016 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1021 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1022 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1023 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1024 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1027 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1029 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1032 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1034 struct isl_ctx
*ctx
;
1038 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1039 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1040 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1041 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1042 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1043 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1044 1 + isl_basic_map_total_dim(bmap
),
1045 bmap
->extra
- bmap
->n_div
);
1046 return bmap
->n_ineq
++;
1049 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1051 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1054 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1058 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1063 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1065 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1068 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1073 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1075 if (pos
!= bmap
->n_ineq
- 1) {
1076 t
= bmap
->ineq
[pos
];
1077 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1078 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1079 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1085 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1087 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1090 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1095 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1098 k
= isl_basic_map_alloc_equality(bmap
);
1101 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1104 isl_basic_map_free(bmap
);
1108 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1111 return (isl_basic_set
*)
1112 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1115 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1120 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1123 k
= isl_basic_map_alloc_inequality(bmap
);
1126 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1129 isl_basic_map_free(bmap
);
1133 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1136 return (isl_basic_set
*)
1137 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1140 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1144 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1145 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1146 1 + 1 + isl_basic_map_total_dim(bmap
),
1147 bmap
->extra
- bmap
->n_div
);
1148 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1149 return bmap
->n_div
++;
1152 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1154 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1157 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1161 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1166 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1168 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1171 /* Copy constraint from src to dst, putting the vars of src at offset
1172 * dim_off in dst and the divs of src at offset div_off in dst.
1173 * If both sets are actually map, then dim_off applies to the input
1176 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1177 struct isl_basic_map
*src_map
, isl_int
*src
,
1178 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1180 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1181 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1182 unsigned src_in
= isl_basic_map_n_in(src_map
);
1183 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1184 unsigned src_out
= isl_basic_map_n_out(src_map
);
1185 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1186 isl_int_set(dst
[0], src
[0]);
1187 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1188 if (dst_nparam
> src_nparam
)
1189 isl_seq_clr(dst
+1+src_nparam
,
1190 dst_nparam
- src_nparam
);
1191 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1192 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1194 isl_min(dst_in
-in_off
, src_in
));
1195 if (dst_in
-in_off
> src_in
)
1196 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1197 dst_in
- in_off
- src_in
);
1198 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1199 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1200 src
+1+src_nparam
+src_in
,
1201 isl_min(dst_out
-out_off
, src_out
));
1202 if (dst_out
-out_off
> src_out
)
1203 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1204 dst_out
- out_off
- src_out
);
1205 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1206 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1207 src
+1+src_nparam
+src_in
+src_out
,
1208 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1209 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1210 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1211 div_off
+src_map
->n_div
,
1212 dst_map
->n_div
- div_off
- src_map
->n_div
);
1215 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1216 struct isl_basic_map
*src_map
, isl_int
*src
,
1217 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1219 isl_int_set(dst
[0], src
[0]);
1220 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1223 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1224 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1229 if (!bmap1
|| !bmap2
)
1232 div_off
= bmap1
->n_div
;
1234 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1235 int i1
= isl_basic_map_alloc_equality(bmap1
);
1238 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1239 i_pos
, o_pos
, div_off
);
1242 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1243 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1246 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1247 i_pos
, o_pos
, div_off
);
1250 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1251 int i1
= isl_basic_map_alloc_div(bmap1
);
1254 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1255 i_pos
, o_pos
, div_off
);
1258 isl_basic_map_free(bmap2
);
1263 isl_basic_map_free(bmap1
);
1264 isl_basic_map_free(bmap2
);
1268 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1269 struct isl_basic_set
*bset2
, unsigned pos
)
1271 return (struct isl_basic_set
*)
1272 add_constraints((struct isl_basic_map
*)bset1
,
1273 (struct isl_basic_map
*)bset2
, 0, pos
);
1276 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1277 __isl_take isl_space
*dim
, unsigned extra
,
1278 unsigned n_eq
, unsigned n_ineq
)
1280 struct isl_basic_map
*ext
;
1290 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1291 base
->extra
>= base
->n_div
+ extra
;
1293 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1294 room_for_ineq(base
, n_ineq
)) {
1295 isl_space_free(dim
);
1299 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1300 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1301 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1302 extra
+= base
->extra
;
1304 n_ineq
+= base
->n_ineq
;
1306 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1312 ext
->sample
= isl_vec_copy(base
->sample
);
1313 flags
= base
->flags
;
1314 ext
= add_constraints(ext
, base
, 0, 0);
1317 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1323 isl_space_free(dim
);
1324 isl_basic_map_free(base
);
1328 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1329 __isl_take isl_space
*dim
, unsigned extra
,
1330 unsigned n_eq
, unsigned n_ineq
)
1332 return (struct isl_basic_set
*)
1333 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1334 extra
, n_eq
, n_ineq
);
1337 struct isl_basic_map
*isl_basic_map_extend_constraints(
1338 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1342 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1346 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1347 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1348 unsigned n_eq
, unsigned n_ineq
)
1350 struct isl_basic_map
*bmap
;
1355 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1359 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1362 isl_basic_map_free(base
);
1366 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1367 unsigned nparam
, unsigned dim
, unsigned extra
,
1368 unsigned n_eq
, unsigned n_ineq
)
1370 return (struct isl_basic_set
*)
1371 isl_basic_map_extend((struct isl_basic_map
*)base
,
1372 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1375 struct isl_basic_set
*isl_basic_set_extend_constraints(
1376 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1378 return (struct isl_basic_set
*)
1379 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1383 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1385 return (struct isl_basic_set
*)
1386 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1389 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1394 if (bmap
->ref
> 1) {
1396 bmap
= isl_basic_map_dup(bmap
);
1399 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1403 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1411 return isl_set_dup(set
);
1414 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1422 return isl_map_dup(map
);
1425 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1426 unsigned a_len
, unsigned b_len
)
1428 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1429 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1430 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1433 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1434 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1442 isl_assert(bmap
->ctx
,
1443 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1445 if (n1
== 0 || n2
== 0)
1448 bmap
= isl_basic_map_cow(bmap
);
1452 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1453 if (isl_blk_is_error(blk
))
1456 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1458 bmap
->eq
[i
] + pos
, n1
, n2
);
1460 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1462 bmap
->ineq
[i
] + pos
, n1
, n2
);
1464 for (i
= 0; i
< bmap
->n_div
; ++i
)
1466 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1468 isl_blk_free(bmap
->ctx
, blk
);
1470 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1471 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1472 return isl_basic_map_finalize(bmap
);
1474 isl_basic_map_free(bmap
);
1478 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1479 __isl_take isl_basic_set
*bset
, unsigned n
)
1484 nparam
= isl_basic_set_n_param(bset
);
1485 dim
= isl_basic_set_n_dim(bset
);
1486 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1488 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1490 isl_basic_set_free(bset
);
1494 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1500 total
= isl_basic_map_total_dim(bmap
);
1501 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1502 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1504 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1506 i
= isl_basic_map_alloc_equality(bmap
);
1510 isl_int_set_si(bmap
->eq
[i
][0], 1);
1511 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1512 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1513 isl_vec_free(bmap
->sample
);
1514 bmap
->sample
= NULL
;
1515 return isl_basic_map_finalize(bmap
);
1517 isl_basic_map_free(bmap
);
1521 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1523 return (struct isl_basic_set
*)
1524 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1527 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1530 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1531 isl_int
*t
= bmap
->div
[a
];
1532 bmap
->div
[a
] = bmap
->div
[b
];
1535 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1536 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1538 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1539 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1541 for (i
= 0; i
< bmap
->n_div
; ++i
)
1542 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1543 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1546 /* Eliminate the specified n dimensions starting at first from the
1547 * constraints using Fourier-Motzkin. The dimensions themselves
1550 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1551 enum isl_dim_type type
, unsigned first
, unsigned n
)
1560 map
= isl_map_cow(map
);
1564 for (i
= 0; i
< map
->n
; ++i
) {
1565 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1575 /* Eliminate the specified n dimensions starting at first from the
1576 * constraints using Fourier-Motzkin. The dimensions themselves
1579 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1580 enum isl_dim_type type
, unsigned first
, unsigned n
)
1582 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1585 /* Eliminate the specified n dimensions starting at first from the
1586 * constraints using Fourier-Motzkin. The dimensions themselves
1589 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1590 unsigned first
, unsigned n
)
1592 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1595 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1596 __isl_take isl_basic_map
*bmap
)
1600 bmap
= isl_basic_map_eliminate_vars(bmap
,
1601 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1605 return isl_basic_map_finalize(bmap
);
1608 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1609 __isl_take isl_basic_set
*bset
)
1611 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1612 (struct isl_basic_map
*)bset
);
1615 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1624 map
= isl_map_cow(map
);
1628 for (i
= 0; i
< map
->n
; ++i
) {
1629 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1639 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1641 return isl_map_remove_divs(set
);
1644 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1645 enum isl_dim_type type
, unsigned first
, unsigned n
)
1649 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1651 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1653 bmap
= isl_basic_map_eliminate_vars(bmap
,
1654 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1657 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1659 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1662 isl_basic_map_free(bmap
);
1666 /* Return true if the definition of the given div (recursively) involves
1667 * any of the given variables.
1669 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1670 unsigned first
, unsigned n
)
1673 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1675 if (isl_int_is_zero(bmap
->div
[div
][0]))
1677 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1680 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1681 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1683 if (div_involves_vars(bmap
, i
, first
, n
))
1690 /* Remove all divs (recursively) involving any of the given dimensions
1691 * in their definitions.
1693 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1694 __isl_take isl_basic_map
*bmap
,
1695 enum isl_dim_type type
, unsigned first
, unsigned n
)
1701 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1703 first
+= isl_basic_map_offset(bmap
, type
);
1705 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1706 if (!div_involves_vars(bmap
, i
, first
, n
))
1708 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1716 isl_basic_map_free(bmap
);
1720 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1721 enum isl_dim_type type
, unsigned first
, unsigned n
)
1730 map
= isl_map_cow(map
);
1734 for (i
= 0; i
< map
->n
; ++i
) {
1735 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1746 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1747 enum isl_dim_type type
, unsigned first
, unsigned n
)
1749 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1753 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1754 enum isl_dim_type type
, unsigned first
, unsigned n
)
1761 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1762 isl_die(bmap
->ctx
, isl_error_invalid
,
1763 "index out of bounds", return -1);
1765 first
+= isl_basic_map_offset(bmap
, type
);
1766 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1767 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1769 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1770 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1776 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1777 enum isl_dim_type type
, unsigned first
, unsigned n
)
1784 if (first
+ n
> isl_map_dim(map
, type
))
1785 isl_die(map
->ctx
, isl_error_invalid
,
1786 "index out of bounds", return -1);
1788 for (i
= 0; i
< map
->n
; ++i
) {
1789 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1791 if (involves
< 0 || involves
)
1798 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1799 enum isl_dim_type type
, unsigned first
, unsigned n
)
1801 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1804 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1805 enum isl_dim_type type
, unsigned first
, unsigned n
)
1807 return isl_map_involves_dims(set
, type
, first
, n
);
1810 /* Return true if the definition of the given div is unknown or depends
1813 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1816 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1818 if (isl_int_is_zero(bmap
->div
[div
][0]))
1821 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1822 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1824 if (div_is_unknown(bmap
, i
))
1831 /* Remove all divs that are unknown or defined in terms of unknown divs.
1833 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1834 __isl_take isl_basic_map
*bmap
)
1841 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1842 if (!div_is_unknown(bmap
, i
))
1844 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1850 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1859 map
= isl_map_cow(map
);
1863 for (i
= 0; i
< map
->n
; ++i
) {
1864 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1874 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1876 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1879 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1880 __isl_take isl_basic_set
*bset
,
1881 enum isl_dim_type type
, unsigned first
, unsigned n
)
1883 return (isl_basic_set
*)
1884 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1887 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1888 enum isl_dim_type type
, unsigned first
, unsigned n
)
1895 map
= isl_map_cow(map
);
1898 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1900 for (i
= 0; i
< map
->n
; ++i
) {
1901 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1902 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1906 map
= isl_map_drop(map
, type
, first
, n
);
1913 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1914 enum isl_dim_type type
, unsigned first
, unsigned n
)
1916 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1919 /* Project out n inputs starting at first using Fourier-Motzkin */
1920 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1921 unsigned first
, unsigned n
)
1923 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1926 static void dump_term(struct isl_basic_map
*bmap
,
1927 isl_int c
, int pos
, FILE *out
)
1930 unsigned in
= isl_basic_map_n_in(bmap
);
1931 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1932 unsigned nparam
= isl_basic_map_n_param(bmap
);
1934 isl_int_print(out
, c
, 0);
1936 if (!isl_int_is_one(c
))
1937 isl_int_print(out
, c
, 0);
1938 if (pos
< 1 + nparam
) {
1939 name
= isl_space_get_dim_name(bmap
->dim
,
1940 isl_dim_param
, pos
- 1);
1942 fprintf(out
, "%s", name
);
1944 fprintf(out
, "p%d", pos
- 1);
1945 } else if (pos
< 1 + nparam
+ in
)
1946 fprintf(out
, "i%d", pos
- 1 - nparam
);
1947 else if (pos
< 1 + nparam
+ dim
)
1948 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1950 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1954 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1955 int sign
, FILE *out
)
1959 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1963 for (i
= 0, first
= 1; i
< len
; ++i
) {
1964 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1967 fprintf(out
, " + ");
1969 isl_int_abs(v
, c
[i
]);
1970 dump_term(bmap
, v
, i
, out
);
1977 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1978 const char *op
, FILE *out
, int indent
)
1982 fprintf(out
, "%*s", indent
, "");
1984 dump_constraint_sign(bmap
, c
, 1, out
);
1985 fprintf(out
, " %s ", op
);
1986 dump_constraint_sign(bmap
, c
, -1, out
);
1990 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1991 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
1993 fprintf(out
, "%*s", indent
, "");
1994 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1999 static void dump_constraints(struct isl_basic_map
*bmap
,
2000 isl_int
**c
, unsigned n
,
2001 const char *op
, FILE *out
, int indent
)
2005 for (i
= 0; i
< n
; ++i
)
2006 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2009 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2013 unsigned total
= isl_basic_map_total_dim(bmap
);
2015 for (j
= 0; j
< 1 + total
; ++j
) {
2016 if (isl_int_is_zero(exp
[j
]))
2018 if (!first
&& isl_int_is_pos(exp
[j
]))
2020 dump_term(bmap
, exp
[j
], j
, out
);
2025 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2029 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2030 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2032 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2033 fprintf(out
, "%*s", indent
, "");
2034 fprintf(out
, "e%d = [(", i
);
2035 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2037 isl_int_print(out
, bmap
->div
[i
][0], 0);
2038 fprintf(out
, "]\n");
2042 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2043 FILE *out
, int indent
)
2046 fprintf(out
, "null basic set\n");
2050 fprintf(out
, "%*s", indent
, "");
2051 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2052 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2053 bset
->extra
, bset
->flags
);
2054 dump((struct isl_basic_map
*)bset
, out
, indent
);
2057 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2058 FILE *out
, int indent
)
2061 fprintf(out
, "null basic map\n");
2065 fprintf(out
, "%*s", indent
, "");
2066 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2067 "flags: %x, n_name: %d\n",
2069 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2070 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2071 dump(bmap
, out
, indent
);
2074 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2079 total
= isl_basic_map_total_dim(bmap
);
2080 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2081 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2082 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2083 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2087 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2090 struct isl_set
*set
;
2094 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2095 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2096 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2097 sizeof(struct isl_set
) +
2098 (n
- 1) * sizeof(struct isl_basic_set
*));
2102 set
->ctx
= dim
->ctx
;
2103 isl_ctx_ref(set
->ctx
);
2111 isl_space_free(dim
);
2115 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2116 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2118 struct isl_set
*set
;
2121 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2125 set
= isl_set_alloc_space(dims
, n
, flags
);
2129 /* Make sure "map" has room for at least "n" more basic maps.
2131 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2134 struct isl_map
*grown
= NULL
;
2138 isl_assert(map
->ctx
, n
>= 0, goto error
);
2139 if (map
->n
+ n
<= map
->size
)
2141 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2144 for (i
= 0; i
< map
->n
; ++i
) {
2145 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2153 isl_map_free(grown
);
2158 /* Make sure "set" has room for at least "n" more basic sets.
2160 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2162 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2165 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2168 struct isl_set
*dup
;
2173 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2176 for (i
= 0; i
< set
->n
; ++i
)
2177 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2181 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2183 return isl_map_from_basic_map(bset
);
2186 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2188 struct isl_map
*map
;
2193 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2194 return isl_map_add_basic_map(map
, bmap
);
2197 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2198 __isl_take isl_basic_set
*bset
)
2200 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2201 (struct isl_basic_map
*)bset
);
2204 void isl_set_free(struct isl_set
*set
)
2214 isl_ctx_deref(set
->ctx
);
2215 for (i
= 0; i
< set
->n
; ++i
)
2216 isl_basic_set_free(set
->p
[i
]);
2217 isl_space_free(set
->dim
);
2221 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2226 fprintf(out
, "null set\n");
2230 fprintf(out
, "%*s", indent
, "");
2231 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2232 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2234 for (i
= 0; i
< set
->n
; ++i
) {
2235 fprintf(out
, "%*s", indent
, "");
2236 fprintf(out
, "basic set %d:\n", i
);
2237 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2241 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2246 fprintf(out
, "null map\n");
2250 fprintf(out
, "%*s", indent
, "");
2251 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2252 "flags: %x, n_name: %d\n",
2253 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2254 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2255 for (i
= 0; i
< map
->n
; ++i
) {
2256 fprintf(out
, "%*s", indent
, "");
2257 fprintf(out
, "basic map %d:\n", i
);
2258 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2262 struct isl_basic_map
*isl_basic_map_intersect_domain(
2263 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2265 struct isl_basic_map
*bmap_domain
;
2270 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2271 bset
->dim
, isl_dim_param
), goto error
);
2273 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2274 isl_assert(bset
->ctx
,
2275 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2277 bmap
= isl_basic_map_cow(bmap
);
2280 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2281 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2282 bmap_domain
= isl_basic_map_from_domain(bset
);
2283 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2285 bmap
= isl_basic_map_simplify(bmap
);
2286 return isl_basic_map_finalize(bmap
);
2288 isl_basic_map_free(bmap
);
2289 isl_basic_set_free(bset
);
2293 struct isl_basic_map
*isl_basic_map_intersect_range(
2294 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2296 struct isl_basic_map
*bmap_range
;
2301 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2302 bset
->dim
, isl_dim_param
), goto error
);
2304 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2305 isl_assert(bset
->ctx
,
2306 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2308 if (isl_basic_set_is_universe(bset
)) {
2309 isl_basic_set_free(bset
);
2313 bmap
= isl_basic_map_cow(bmap
);
2316 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2317 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2318 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2319 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2321 bmap
= isl_basic_map_simplify(bmap
);
2322 return isl_basic_map_finalize(bmap
);
2324 isl_basic_map_free(bmap
);
2325 isl_basic_set_free(bset
);
2329 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2335 total
= 1 + isl_basic_map_total_dim(bmap
);
2336 if (total
!= vec
->size
)
2341 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2342 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2343 if (!isl_int_is_zero(s
)) {
2349 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2350 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2351 if (isl_int_is_neg(s
)) {
2362 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2364 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2367 struct isl_basic_map
*isl_basic_map_intersect(
2368 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2370 struct isl_vec
*sample
= NULL
;
2372 if (!bmap1
|| !bmap2
)
2375 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2376 bmap2
->dim
, isl_dim_param
), goto error
);
2377 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2378 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2379 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2380 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2381 return isl_basic_map_intersect(bmap2
, bmap1
);
2383 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2384 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2385 isl_assert(bmap1
->ctx
,
2386 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2388 if (bmap1
->sample
&&
2389 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2390 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2391 sample
= isl_vec_copy(bmap1
->sample
);
2392 else if (bmap2
->sample
&&
2393 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2394 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2395 sample
= isl_vec_copy(bmap2
->sample
);
2397 bmap1
= isl_basic_map_cow(bmap1
);
2400 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2401 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2402 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2405 isl_vec_free(sample
);
2407 isl_vec_free(bmap1
->sample
);
2408 bmap1
->sample
= sample
;
2411 bmap1
= isl_basic_map_simplify(bmap1
);
2412 return isl_basic_map_finalize(bmap1
);
2415 isl_vec_free(sample
);
2416 isl_basic_map_free(bmap1
);
2417 isl_basic_map_free(bmap2
);
2421 struct isl_basic_set
*isl_basic_set_intersect(
2422 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2424 return (struct isl_basic_set
*)
2425 isl_basic_map_intersect(
2426 (struct isl_basic_map
*)bset1
,
2427 (struct isl_basic_map
*)bset2
);
2430 /* Special case of isl_map_intersect, where both map1 and map2
2431 * are convex, without any divs and such that either map1 or map2
2432 * contains a single constraint. This constraint is then simply
2433 * added to the other map.
2435 static __isl_give isl_map
*map_intersect_add_constraint(
2436 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2438 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2439 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2440 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2441 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2443 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2444 return isl_map_intersect(map2
, map1
);
2446 isl_assert(map2
->ctx
,
2447 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2449 map1
= isl_map_cow(map1
);
2452 if (isl_map_plain_is_empty(map1
)) {
2456 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2457 if (map2
->p
[0]->n_eq
== 1)
2458 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2460 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2461 map2
->p
[0]->ineq
[0]);
2463 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2464 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2468 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2469 isl_basic_map_free(map1
->p
[0]);
2482 /* map2 may be either a parameter domain or a map living in the same
2485 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2486 __isl_take isl_map
*map2
)
2489 struct isl_map
*result
;
2495 if (isl_map_plain_is_empty(map1
) &&
2496 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2500 if (isl_map_plain_is_empty(map2
) &&
2501 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2506 if (map1
->n
== 1 && map2
->n
== 1 &&
2507 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2508 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2509 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2510 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2511 return map_intersect_add_constraint(map1
, map2
);
2513 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2514 isl_space_dim(map2
->dim
, isl_dim_param
))
2515 isl_assert(map1
->ctx
,
2516 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2518 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2519 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2520 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2522 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2523 map1
->n
* map2
->n
, flags
);
2526 for (i
= 0; i
< map1
->n
; ++i
)
2527 for (j
= 0; j
< map2
->n
; ++j
) {
2528 struct isl_basic_map
*part
;
2529 part
= isl_basic_map_intersect(
2530 isl_basic_map_copy(map1
->p
[i
]),
2531 isl_basic_map_copy(map2
->p
[j
]));
2532 if (isl_basic_map_is_empty(part
))
2533 isl_basic_map_free(part
);
2535 result
= isl_map_add_basic_map(result
, part
);
2548 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2549 __isl_take isl_map
*map2
)
2553 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2554 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2555 "spaces don't match", goto error
);
2556 return map_intersect_internal(map1
, map2
);
2563 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2564 __isl_take isl_map
*map2
)
2566 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2569 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2571 return (struct isl_set
*)
2572 isl_map_intersect((struct isl_map
*)set1
,
2573 (struct isl_map
*)set2
);
2576 /* map_intersect_internal accepts intersections
2577 * with parameter domains, so we can just call that function.
2579 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2580 __isl_take isl_set
*params
)
2582 return map_intersect_internal(map
, params
);
2585 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2586 __isl_take isl_map
*map2
)
2588 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2591 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2592 __isl_take isl_set
*params
)
2594 return isl_map_intersect_params(set
, params
);
2597 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2600 struct isl_basic_set
*bset
;
2605 bmap
= isl_basic_map_cow(bmap
);
2608 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2609 in
= isl_basic_map_n_in(bmap
);
2610 bset
= isl_basic_set_from_basic_map(bmap
);
2611 bset
= isl_basic_set_swap_vars(bset
, in
);
2612 return isl_basic_map_from_basic_set(bset
, dim
);
2615 static __isl_give isl_basic_map
*basic_map_space_reset(
2616 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2620 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2623 space
= isl_basic_map_get_space(bmap
);
2624 space
= isl_space_reset(space
, type
);
2625 bmap
= isl_basic_map_reset_space(bmap
, space
);
2629 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2630 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2633 struct isl_basic_map
*res
;
2634 struct isl_dim_map
*dim_map
;
2635 unsigned total
, off
;
2636 enum isl_dim_type t
;
2639 return basic_map_space_reset(bmap
, type
);
2644 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2646 total
= isl_basic_map_total_dim(bmap
) + n
;
2647 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2649 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2651 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2653 unsigned size
= isl_basic_map_dim(bmap
, t
);
2654 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2656 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2657 pos
, size
- pos
, off
+ pos
+ n
);
2659 off
+= isl_space_dim(res_dim
, t
);
2661 isl_dim_map_div(dim_map
, bmap
, off
);
2663 res
= isl_basic_map_alloc_space(res_dim
,
2664 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2665 if (isl_basic_map_is_rational(bmap
))
2666 res
= isl_basic_map_set_rational(res
);
2667 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2668 return isl_basic_map_finalize(res
);
2671 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2672 enum isl_dim_type type
, unsigned n
)
2676 return isl_basic_map_insert(bmap
, type
,
2677 isl_basic_map_dim(bmap
, type
), n
);
2680 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2681 enum isl_dim_type type
, unsigned n
)
2685 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2686 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2688 isl_basic_set_free(bset
);
2692 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2693 enum isl_dim_type type
)
2697 if (!isl_space_is_named_or_nested(map
->dim
, type
))
2700 space
= isl_map_get_space(map
);
2701 space
= isl_space_reset(space
, type
);
2702 map
= isl_map_reset_space(map
, space
);
2706 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2707 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2712 return map_space_reset(map
, type
);
2714 map
= isl_map_cow(map
);
2718 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2722 for (i
= 0; i
< map
->n
; ++i
) {
2723 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2734 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2735 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2737 return isl_map_insert_dims(set
, type
, pos
, n
);
2740 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2741 enum isl_dim_type type
, unsigned n
)
2745 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2748 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2749 enum isl_dim_type type
, unsigned n
)
2753 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2754 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2760 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2761 __isl_take isl_basic_map
*bmap
,
2762 enum isl_dim_type dst_type
, unsigned dst_pos
,
2763 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2765 struct isl_dim_map
*dim_map
;
2766 struct isl_basic_map
*res
;
2767 enum isl_dim_type t
;
2768 unsigned total
, off
;
2775 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2778 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2781 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2783 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2784 pos(bmap
->dim
, src_type
) + src_pos
+
2785 ((src_type
< dst_type
) ? n
: 0)) {
2786 bmap
= isl_basic_map_cow(bmap
);
2790 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2791 src_type
, src_pos
, n
);
2795 bmap
= isl_basic_map_finalize(bmap
);
2800 total
= isl_basic_map_total_dim(bmap
);
2801 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2804 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2805 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2806 if (t
== dst_type
) {
2807 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2810 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2813 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2814 dst_pos
, size
- dst_pos
, off
);
2815 off
+= size
- dst_pos
;
2816 } else if (t
== src_type
) {
2817 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2820 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2821 src_pos
+ n
, size
- src_pos
- n
, off
);
2822 off
+= size
- src_pos
- n
;
2824 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2828 isl_dim_map_div(dim_map
, bmap
, off
);
2830 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2831 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2832 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2834 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2835 src_type
, src_pos
, n
);
2839 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2840 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2841 bmap
= isl_basic_map_finalize(bmap
);
2845 isl_basic_map_free(bmap
);
2849 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2850 enum isl_dim_type dst_type
, unsigned dst_pos
,
2851 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2853 return (isl_basic_set
*)isl_basic_map_move_dims(
2854 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2857 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2858 enum isl_dim_type dst_type
, unsigned dst_pos
,
2859 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2863 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2864 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2865 src_type
, src_pos
, n
);
2871 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2872 enum isl_dim_type dst_type
, unsigned dst_pos
,
2873 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2882 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2885 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2888 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2890 map
= isl_map_cow(map
);
2894 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2898 for (i
= 0; i
< map
->n
; ++i
) {
2899 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2901 src_type
, src_pos
, n
);
2912 /* Move the specified dimensions to the last columns right before
2913 * the divs. Don't change the dimension specification of bmap.
2914 * That's the responsibility of the caller.
2916 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2917 enum isl_dim_type type
, unsigned first
, unsigned n
)
2919 struct isl_dim_map
*dim_map
;
2920 struct isl_basic_map
*res
;
2921 enum isl_dim_type t
;
2922 unsigned total
, off
;
2926 if (pos(bmap
->dim
, type
) + first
+ n
==
2927 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2930 total
= isl_basic_map_total_dim(bmap
);
2931 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2934 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2935 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2937 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2940 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2941 first
, n
, total
- bmap
->n_div
- n
);
2942 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2943 first
+ n
, size
- (first
+ n
), off
);
2944 off
+= size
- (first
+ n
);
2946 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2950 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2952 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2953 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2954 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2958 /* Turn the n dimensions of type type, starting at first
2959 * into existentially quantified variables.
2961 __isl_give isl_basic_map
*isl_basic_map_project_out(
2962 __isl_take isl_basic_map
*bmap
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
)
2976 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2977 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2979 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2982 bmap
= move_last(bmap
, type
, first
, n
);
2983 bmap
= isl_basic_map_cow(bmap
);
2987 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2988 old
= bmap
->block2
.data
;
2989 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2990 (bmap
->extra
+ n
) * (1 + row_size
));
2991 if (!bmap
->block2
.data
)
2993 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2996 for (i
= 0; i
< n
; ++i
) {
2997 new_div
[i
] = bmap
->block2
.data
+
2998 (bmap
->extra
+ i
) * (1 + row_size
);
2999 isl_seq_clr(new_div
[i
], 1 + row_size
);
3001 for (i
= 0; i
< bmap
->extra
; ++i
)
3002 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3004 bmap
->div
= new_div
;
3008 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3011 bmap
= isl_basic_map_simplify(bmap
);
3012 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3013 return isl_basic_map_finalize(bmap
);
3015 isl_basic_map_free(bmap
);
3019 /* Turn the n dimensions of type type, starting at first
3020 * into existentially quantified variables.
3022 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3023 enum isl_dim_type type
, unsigned first
, unsigned n
)
3025 return (isl_basic_set
*)isl_basic_map_project_out(
3026 (isl_basic_map
*)bset
, type
, first
, n
);
3029 /* Turn the n dimensions of type type, starting at first
3030 * into existentially quantified variables.
3032 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3033 enum isl_dim_type type
, unsigned first
, unsigned n
)
3043 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3045 map
= isl_map_cow(map
);
3049 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3053 for (i
= 0; i
< map
->n
; ++i
) {
3054 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3065 /* Turn the n dimensions of type type, starting at first
3066 * into existentially quantified variables.
3068 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3069 enum isl_dim_type type
, unsigned first
, unsigned n
)
3071 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3074 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3078 for (i
= 0; i
< n
; ++i
) {
3079 j
= isl_basic_map_alloc_div(bmap
);
3082 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3086 isl_basic_map_free(bmap
);
3090 struct isl_basic_map
*isl_basic_map_apply_range(
3091 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3093 isl_space
*dim_result
= NULL
;
3094 struct isl_basic_map
*bmap
;
3095 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3096 struct isl_dim_map
*dim_map1
, *dim_map2
;
3098 if (!bmap1
|| !bmap2
)
3101 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3102 isl_space_copy(bmap2
->dim
));
3104 n_in
= isl_basic_map_n_in(bmap1
);
3105 n_out
= isl_basic_map_n_out(bmap2
);
3106 n
= isl_basic_map_n_out(bmap1
);
3107 nparam
= isl_basic_map_n_param(bmap1
);
3109 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3110 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3111 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3112 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3113 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3114 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3115 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3116 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3117 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3118 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3119 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3121 bmap
= isl_basic_map_alloc_space(dim_result
,
3122 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3123 bmap1
->n_eq
+ bmap2
->n_eq
,
3124 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3125 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3126 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3127 bmap
= add_divs(bmap
, n
);
3128 bmap
= isl_basic_map_simplify(bmap
);
3129 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3130 return isl_basic_map_finalize(bmap
);
3132 isl_basic_map_free(bmap1
);
3133 isl_basic_map_free(bmap2
);
3137 struct isl_basic_set
*isl_basic_set_apply(
3138 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3143 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3146 return (struct isl_basic_set
*)
3147 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3149 isl_basic_set_free(bset
);
3150 isl_basic_map_free(bmap
);
3154 struct isl_basic_map
*isl_basic_map_apply_domain(
3155 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3157 if (!bmap1
|| !bmap2
)
3160 isl_assert(bmap1
->ctx
,
3161 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3162 isl_assert(bmap1
->ctx
,
3163 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3166 bmap1
= isl_basic_map_reverse(bmap1
);
3167 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3168 return isl_basic_map_reverse(bmap1
);
3170 isl_basic_map_free(bmap1
);
3171 isl_basic_map_free(bmap2
);
3175 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3176 * A \cap B -> f(A) + f(B)
3178 struct isl_basic_map
*isl_basic_map_sum(
3179 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3181 unsigned n_in
, n_out
, nparam
, total
, pos
;
3182 struct isl_basic_map
*bmap
= NULL
;
3183 struct isl_dim_map
*dim_map1
, *dim_map2
;
3186 if (!bmap1
|| !bmap2
)
3189 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3192 nparam
= isl_basic_map_n_param(bmap1
);
3193 n_in
= isl_basic_map_n_in(bmap1
);
3194 n_out
= isl_basic_map_n_out(bmap1
);
3196 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3197 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3198 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3199 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3200 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3201 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3202 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3203 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3204 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3205 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3206 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3208 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3209 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3210 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3211 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3212 for (i
= 0; i
< n_out
; ++i
) {
3213 int j
= isl_basic_map_alloc_equality(bmap
);
3216 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3217 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3218 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3219 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3221 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3222 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3223 bmap
= add_divs(bmap
, 2 * n_out
);
3225 bmap
= isl_basic_map_simplify(bmap
);
3226 return isl_basic_map_finalize(bmap
);
3228 isl_basic_map_free(bmap
);
3229 isl_basic_map_free(bmap1
);
3230 isl_basic_map_free(bmap2
);
3234 /* Given two maps A -> f(A) and B -> g(B), construct a map
3235 * A \cap B -> f(A) + f(B)
3237 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3239 struct isl_map
*result
;
3245 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3247 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3248 map1
->n
* map2
->n
, 0);
3251 for (i
= 0; i
< map1
->n
; ++i
)
3252 for (j
= 0; j
< map2
->n
; ++j
) {
3253 struct isl_basic_map
*part
;
3254 part
= isl_basic_map_sum(
3255 isl_basic_map_copy(map1
->p
[i
]),
3256 isl_basic_map_copy(map2
->p
[j
]));
3257 if (isl_basic_map_is_empty(part
))
3258 isl_basic_map_free(part
);
3260 result
= isl_map_add_basic_map(result
, part
);
3273 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3274 __isl_take isl_set
*set2
)
3276 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3279 /* Given a basic map A -> f(A), construct A -> -f(A).
3281 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3286 bmap
= isl_basic_map_cow(bmap
);
3290 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3291 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3292 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3293 for (j
= 0; j
< n
; ++j
)
3294 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3295 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3296 for (j
= 0; j
< n
; ++j
)
3297 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3298 for (i
= 0; i
< bmap
->n_div
; ++i
)
3299 for (j
= 0; j
< n
; ++j
)
3300 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3301 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3302 return isl_basic_map_finalize(bmap
);
3305 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3307 return isl_basic_map_neg(bset
);
3310 /* Given a map A -> f(A), construct A -> -f(A).
3312 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3316 map
= isl_map_cow(map
);
3320 for (i
= 0; i
< map
->n
; ++i
) {
3321 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3332 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3334 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3337 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3338 * A -> floor(f(A)/d).
3340 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3343 unsigned n_in
, n_out
, nparam
, total
, pos
;
3344 struct isl_basic_map
*result
= NULL
;
3345 struct isl_dim_map
*dim_map
;
3351 nparam
= isl_basic_map_n_param(bmap
);
3352 n_in
= isl_basic_map_n_in(bmap
);
3353 n_out
= isl_basic_map_n_out(bmap
);
3355 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3356 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3357 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3358 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3359 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3360 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3362 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3363 bmap
->n_div
+ n_out
,
3364 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3365 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3366 result
= add_divs(result
, n_out
);
3367 for (i
= 0; i
< n_out
; ++i
) {
3369 j
= isl_basic_map_alloc_inequality(result
);
3372 isl_seq_clr(result
->ineq
[j
], 1+total
);
3373 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3374 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3375 j
= isl_basic_map_alloc_inequality(result
);
3378 isl_seq_clr(result
->ineq
[j
], 1+total
);
3379 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3380 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3381 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3384 result
= isl_basic_map_simplify(result
);
3385 return isl_basic_map_finalize(result
);
3387 isl_basic_map_free(result
);
3391 /* Given a map A -> f(A) and an integer d, construct a map
3392 * A -> floor(f(A)/d).
3394 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3398 map
= isl_map_cow(map
);
3402 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3403 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3404 for (i
= 0; i
< map
->n
; ++i
) {
3405 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3416 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3422 i
= isl_basic_map_alloc_equality(bmap
);
3425 nparam
= isl_basic_map_n_param(bmap
);
3426 n_in
= isl_basic_map_n_in(bmap
);
3427 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3428 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3429 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3430 return isl_basic_map_finalize(bmap
);
3432 isl_basic_map_free(bmap
);
3436 /* Add a constraints to "bmap" expressing i_pos < o_pos
3438 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3444 i
= isl_basic_map_alloc_inequality(bmap
);
3447 nparam
= isl_basic_map_n_param(bmap
);
3448 n_in
= isl_basic_map_n_in(bmap
);
3449 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3450 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3451 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3452 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3453 return isl_basic_map_finalize(bmap
);
3455 isl_basic_map_free(bmap
);
3459 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3461 static __isl_give isl_basic_map
*var_less_or_equal(
3462 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3468 i
= isl_basic_map_alloc_inequality(bmap
);
3471 nparam
= isl_basic_map_n_param(bmap
);
3472 n_in
= isl_basic_map_n_in(bmap
);
3473 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3474 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3475 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3476 return isl_basic_map_finalize(bmap
);
3478 isl_basic_map_free(bmap
);
3482 /* Add a constraints to "bmap" expressing i_pos > o_pos
3484 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3490 i
= isl_basic_map_alloc_inequality(bmap
);
3493 nparam
= isl_basic_map_n_param(bmap
);
3494 n_in
= isl_basic_map_n_in(bmap
);
3495 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3496 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3497 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3498 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3499 return isl_basic_map_finalize(bmap
);
3501 isl_basic_map_free(bmap
);
3505 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3507 static __isl_give isl_basic_map
*var_more_or_equal(
3508 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3514 i
= isl_basic_map_alloc_inequality(bmap
);
3517 nparam
= isl_basic_map_n_param(bmap
);
3518 n_in
= isl_basic_map_n_in(bmap
);
3519 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3520 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3521 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3522 return isl_basic_map_finalize(bmap
);
3524 isl_basic_map_free(bmap
);
3528 __isl_give isl_basic_map
*isl_basic_map_equal(
3529 __isl_take isl_space
*dim
, unsigned n_equal
)
3532 struct isl_basic_map
*bmap
;
3533 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3536 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3537 bmap
= var_equal(bmap
, i
);
3538 return isl_basic_map_finalize(bmap
);
3541 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3543 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3547 struct isl_basic_map
*bmap
;
3548 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3551 for (i
= 0; i
< pos
&& bmap
; ++i
)
3552 bmap
= var_equal(bmap
, i
);
3554 bmap
= var_less(bmap
, pos
);
3555 return isl_basic_map_finalize(bmap
);
3558 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3560 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3561 __isl_take isl_space
*dim
, unsigned pos
)
3564 isl_basic_map
*bmap
;
3566 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3567 for (i
= 0; i
< pos
; ++i
)
3568 bmap
= var_equal(bmap
, i
);
3569 bmap
= var_less_or_equal(bmap
, pos
);
3570 return isl_basic_map_finalize(bmap
);
3573 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3575 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3579 struct isl_basic_map
*bmap
;
3580 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3583 for (i
= 0; i
< pos
&& bmap
; ++i
)
3584 bmap
= var_equal(bmap
, i
);
3586 bmap
= var_more(bmap
, pos
);
3587 return isl_basic_map_finalize(bmap
);
3590 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3592 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3593 __isl_take isl_space
*dim
, unsigned pos
)
3596 isl_basic_map
*bmap
;
3598 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3599 for (i
= 0; i
< pos
; ++i
)
3600 bmap
= var_equal(bmap
, i
);
3601 bmap
= var_more_or_equal(bmap
, pos
);
3602 return isl_basic_map_finalize(bmap
);
3605 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3606 unsigned n
, int equal
)
3608 struct isl_map
*map
;
3611 if (n
== 0 && equal
)
3612 return isl_map_universe(dims
);
3614 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3616 for (i
= 0; i
+ 1 < n
; ++i
)
3617 map
= isl_map_add_basic_map(map
,
3618 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3621 map
= isl_map_add_basic_map(map
,
3622 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3624 map
= isl_map_add_basic_map(map
,
3625 isl_basic_map_less_at(dims
, n
- 1));
3627 isl_space_free(dims
);
3632 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3636 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3639 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3641 return map_lex_lte_first(dim
, n
, 0);
3644 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3646 return map_lex_lte_first(dim
, n
, 1);
3649 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3651 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3654 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3656 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3659 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3660 unsigned n
, int equal
)
3662 struct isl_map
*map
;
3665 if (n
== 0 && equal
)
3666 return isl_map_universe(dims
);
3668 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3670 for (i
= 0; i
+ 1 < n
; ++i
)
3671 map
= isl_map_add_basic_map(map
,
3672 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3675 map
= isl_map_add_basic_map(map
,
3676 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3678 map
= isl_map_add_basic_map(map
,
3679 isl_basic_map_more_at(dims
, n
- 1));
3681 isl_space_free(dims
);
3686 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3690 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3693 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3695 return map_lex_gte_first(dim
, n
, 0);
3698 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3700 return map_lex_gte_first(dim
, n
, 1);
3703 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3705 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3708 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3710 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3713 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3714 __isl_take isl_set
*set2
)
3717 map
= isl_map_lex_le(isl_set_get_space(set1
));
3718 map
= isl_map_intersect_domain(map
, set1
);
3719 map
= isl_map_intersect_range(map
, set2
);
3723 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3724 __isl_take isl_set
*set2
)
3727 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3728 map
= isl_map_intersect_domain(map
, set1
);
3729 map
= isl_map_intersect_range(map
, set2
);
3733 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3734 __isl_take isl_set
*set2
)
3737 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3738 map
= isl_map_intersect_domain(map
, set1
);
3739 map
= isl_map_intersect_range(map
, set2
);
3743 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3744 __isl_take isl_set
*set2
)
3747 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3748 map
= isl_map_intersect_domain(map
, set1
);
3749 map
= isl_map_intersect_range(map
, set2
);
3753 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3754 __isl_take isl_map
*map2
)
3757 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3758 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3759 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3763 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3764 __isl_take isl_map
*map2
)
3767 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3768 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3769 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3773 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3774 __isl_take isl_map
*map2
)
3777 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3778 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3779 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3783 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3784 __isl_take isl_map
*map2
)
3787 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3788 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3789 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3793 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3794 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3796 struct isl_basic_map
*bmap
;
3798 bset
= isl_basic_set_cow(bset
);
3802 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3803 isl_space_free(bset
->dim
);
3804 bmap
= (struct isl_basic_map
*) bset
;
3806 return isl_basic_map_finalize(bmap
);
3808 isl_basic_set_free(bset
);
3809 isl_space_free(dim
);
3813 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3817 if (bmap
->dim
->n_in
== 0)
3818 return (struct isl_basic_set
*)bmap
;
3819 bmap
= isl_basic_map_cow(bmap
);
3822 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3825 bmap
= isl_basic_map_finalize(bmap
);
3826 return (struct isl_basic_set
*)bmap
;
3828 isl_basic_map_free(bmap
);
3832 /* For a div d = floor(f/m), add the constraints
3835 * -(f-(n-1)) + m d >= 0
3837 * Note that the second constraint is the negation of
3841 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3842 unsigned pos
, isl_int
*div
)
3845 unsigned total
= isl_basic_map_total_dim(bmap
);
3847 i
= isl_basic_map_alloc_inequality(bmap
);
3850 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3851 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3853 j
= isl_basic_map_alloc_inequality(bmap
);
3856 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3857 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3858 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3862 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3863 unsigned pos
, isl_int
*div
)
3865 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3869 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3871 unsigned total
= isl_basic_map_total_dim(bmap
);
3872 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3874 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3878 struct isl_basic_set
*isl_basic_map_underlying_set(
3879 struct isl_basic_map
*bmap
)
3883 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3885 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3886 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3887 return (struct isl_basic_set
*)bmap
;
3888 bmap
= isl_basic_map_cow(bmap
);
3891 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3894 bmap
->extra
-= bmap
->n_div
;
3896 bmap
= isl_basic_map_finalize(bmap
);
3897 return (struct isl_basic_set
*)bmap
;
3902 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3903 __isl_take isl_basic_set
*bset
)
3905 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3908 struct isl_basic_map
*isl_basic_map_overlying_set(
3909 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3911 struct isl_basic_map
*bmap
;
3912 struct isl_ctx
*ctx
;
3919 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3920 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3921 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3923 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3924 isl_basic_map_free(like
);
3925 return (struct isl_basic_map
*)bset
;
3927 bset
= isl_basic_set_cow(bset
);
3930 total
= bset
->dim
->n_out
+ bset
->extra
;
3931 bmap
= (struct isl_basic_map
*)bset
;
3932 isl_space_free(bmap
->dim
);
3933 bmap
->dim
= isl_space_copy(like
->dim
);
3936 bmap
->n_div
= like
->n_div
;
3937 bmap
->extra
+= like
->n_div
;
3941 ltotal
= total
- bmap
->extra
+ like
->extra
;
3944 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3945 bmap
->extra
* (1 + 1 + total
));
3946 if (isl_blk_is_error(bmap
->block2
))
3948 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3952 for (i
= 0; i
< bmap
->extra
; ++i
)
3953 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3954 for (i
= 0; i
< like
->n_div
; ++i
) {
3955 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3956 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3958 bmap
= isl_basic_map_extend_constraints(bmap
,
3959 0, 2 * like
->n_div
);
3960 for (i
= 0; i
< like
->n_div
; ++i
) {
3961 if (isl_int_is_zero(bmap
->div
[i
][0]))
3963 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3967 isl_basic_map_free(like
);
3968 bmap
= isl_basic_map_simplify(bmap
);
3969 bmap
= isl_basic_map_finalize(bmap
);
3972 isl_basic_map_free(like
);
3973 isl_basic_set_free(bset
);
3977 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3978 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3980 return (struct isl_basic_set
*)
3981 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3984 struct isl_set
*isl_set_from_underlying_set(
3985 struct isl_set
*set
, struct isl_basic_set
*like
)
3991 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3993 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3994 isl_basic_set_free(like
);
3997 set
= isl_set_cow(set
);
4000 for (i
= 0; i
< set
->n
; ++i
) {
4001 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4002 isl_basic_set_copy(like
));
4006 isl_space_free(set
->dim
);
4007 set
->dim
= isl_space_copy(like
->dim
);
4010 isl_basic_set_free(like
);
4013 isl_basic_set_free(like
);
4018 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4022 map
= isl_map_cow(map
);
4025 map
->dim
= isl_space_cow(map
->dim
);
4029 for (i
= 1; i
< map
->n
; ++i
)
4030 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4032 for (i
= 0; i
< map
->n
; ++i
) {
4033 map
->p
[i
] = (struct isl_basic_map
*)
4034 isl_basic_map_underlying_set(map
->p
[i
]);
4039 map
->dim
= isl_space_underlying(map
->dim
, 0);
4041 isl_space_free(map
->dim
);
4042 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4046 return (struct isl_set
*)map
;
4052 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4054 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4057 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4058 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4060 bmap
= isl_basic_map_cow(bmap
);
4064 isl_space_free(bmap
->dim
);
4067 bmap
= isl_basic_map_finalize(bmap
);
4071 isl_basic_map_free(bmap
);
4072 isl_space_free(dim
);
4076 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4077 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4079 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4083 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4084 __isl_take isl_space
*dim
)
4088 map
= isl_map_cow(map
);
4092 for (i
= 0; i
< map
->n
; ++i
) {
4093 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4094 isl_space_copy(dim
));
4098 isl_space_free(map
->dim
);
4104 isl_space_free(dim
);
4108 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4109 __isl_take isl_space
*dim
)
4111 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4114 /* Compute the parameter domain of the given basic set.
4116 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4121 if (isl_basic_set_is_params(bset
))
4124 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4125 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4126 space
= isl_basic_set_get_space(bset
);
4127 space
= isl_space_params(space
);
4128 bset
= isl_basic_set_reset_space(bset
, space
);
4132 /* Compute the parameter domain of the given set.
4134 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4139 if (isl_set_is_params(set
))
4142 n
= isl_set_dim(set
, isl_dim_set
);
4143 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4144 space
= isl_set_get_space(set
);
4145 space
= isl_space_params(space
);
4146 set
= isl_set_reset_space(set
, space
);
4150 /* Construct a zero-dimensional set with the given parameter domain.
4152 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4155 space
= isl_set_get_space(set
);
4156 space
= isl_space_set_from_params(space
);
4157 set
= isl_set_reset_space(set
, space
);
4161 /* Compute the parameter domain of the given map.
4163 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4168 n
= isl_map_dim(map
, isl_dim_in
);
4169 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4170 n
= isl_map_dim(map
, isl_dim_out
);
4171 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4172 space
= isl_map_get_space(map
);
4173 space
= isl_space_params(space
);
4174 map
= isl_map_reset_space(map
, space
);
4178 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4181 struct isl_basic_set
*domain
;
4187 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4189 n_in
= isl_basic_map_n_in(bmap
);
4190 n_out
= isl_basic_map_n_out(bmap
);
4191 domain
= isl_basic_set_from_basic_map(bmap
);
4192 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4194 domain
= isl_basic_set_reset_space(domain
, dim
);
4199 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4203 return isl_space_may_be_set(bmap
->dim
);
4206 /* Is this basic map actually a set?
4207 * Users should never call this function. Outside of isl,
4208 * the type should indicate whether something is a set or a map.
4210 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4214 return isl_space_is_set(bmap
->dim
);
4217 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4221 if (isl_basic_map_is_set(bmap
))
4223 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4226 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4227 __isl_take isl_basic_map
*bmap
)
4231 isl_basic_map
*domain
;
4232 int nparam
, n_in
, n_out
;
4235 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4236 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4237 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4239 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4240 domain
= isl_basic_map_universe(dim
);
4242 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4243 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4244 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4246 total
= isl_basic_map_total_dim(bmap
);
4248 for (i
= 0; i
< n_in
; ++i
) {
4249 k
= isl_basic_map_alloc_equality(bmap
);
4252 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4253 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4254 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4257 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4258 return isl_basic_map_finalize(bmap
);
4260 isl_basic_map_free(bmap
);
4264 __isl_give isl_basic_map
*isl_basic_map_range_map(
4265 __isl_take isl_basic_map
*bmap
)
4269 isl_basic_map
*range
;
4270 int nparam
, n_in
, n_out
;
4273 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4274 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4275 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4277 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4278 range
= isl_basic_map_universe(dim
);
4280 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4281 bmap
= isl_basic_map_apply_range(bmap
, range
);
4282 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4284 total
= isl_basic_map_total_dim(bmap
);
4286 for (i
= 0; i
< n_out
; ++i
) {
4287 k
= isl_basic_map_alloc_equality(bmap
);
4290 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4291 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4292 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4295 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4296 return isl_basic_map_finalize(bmap
);
4298 isl_basic_map_free(bmap
);
4302 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4306 return isl_space_may_be_set(map
->dim
);
4309 /* Is this map actually a set?
4310 * Users should never call this function. Outside of isl,
4311 * the type should indicate whether something is a set or a map.
4313 int isl_map_is_set(__isl_keep isl_map
*map
)
4317 return isl_space_is_set(map
->dim
);
4320 struct isl_set
*isl_map_range(struct isl_map
*map
)
4323 struct isl_set
*set
;
4327 if (isl_map_is_set(map
))
4328 return (isl_set
*)map
;
4330 map
= isl_map_cow(map
);
4334 set
= (struct isl_set
*) map
;
4335 set
->dim
= isl_space_range(set
->dim
);
4338 for (i
= 0; i
< map
->n
; ++i
) {
4339 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4343 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4344 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4351 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4354 isl_space
*domain_dim
;
4356 map
= isl_map_cow(map
);
4360 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4361 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4362 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4365 for (i
= 0; i
< map
->n
; ++i
) {
4366 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4370 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4371 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4378 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4381 isl_space
*range_dim
;
4383 map
= isl_map_cow(map
);
4387 range_dim
= isl_space_range(isl_map_get_space(map
));
4388 range_dim
= isl_space_from_range(range_dim
);
4389 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4390 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4393 for (i
= 0; i
< map
->n
; ++i
) {
4394 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4398 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4399 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4406 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4407 __isl_take isl_space
*dim
)
4410 struct isl_map
*map
= NULL
;
4412 set
= isl_set_cow(set
);
4415 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4416 map
= (struct isl_map
*)set
;
4417 for (i
= 0; i
< set
->n
; ++i
) {
4418 map
->p
[i
] = isl_basic_map_from_basic_set(
4419 set
->p
[i
], isl_space_copy(dim
));
4423 isl_space_free(map
->dim
);
4427 isl_space_free(dim
);
4432 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4433 __isl_take isl_basic_set
*bset
)
4435 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4438 __isl_give isl_basic_map
*isl_basic_map_from_range(
4439 __isl_take isl_basic_set
*bset
)
4442 space
= isl_basic_set_get_space(bset
);
4443 space
= isl_space_from_range(space
);
4444 bset
= isl_basic_set_reset_space(bset
, space
);
4445 return (isl_basic_map
*)bset
;
4448 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4451 space
= isl_set_get_space(set
);
4452 space
= isl_space_from_range(space
);
4453 set
= isl_set_reset_space(set
, space
);
4454 return (struct isl_map
*)set
;
4457 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4459 return isl_map_reverse(isl_map_from_range(set
));
4462 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4463 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4465 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4468 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4469 __isl_take isl_set
*range
)
4471 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4474 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4477 struct isl_set
*set
= NULL
;
4481 map
= isl_map_cow(map
);
4484 map
->dim
= isl_space_as_set_space(map
->dim
);
4487 set
= (struct isl_set
*)map
;
4488 for (i
= 0; i
< map
->n
; ++i
) {
4489 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4499 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4502 struct isl_map
*map
;
4506 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4507 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4508 sizeof(struct isl_map
) +
4509 (n
- 1) * sizeof(struct isl_basic_map
*));
4513 map
->ctx
= dim
->ctx
;
4514 isl_ctx_ref(map
->ctx
);
4522 isl_space_free(dim
);
4526 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4527 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4530 struct isl_map
*map
;
4533 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4537 map
= isl_map_alloc_space(dims
, n
, flags
);
4541 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4543 struct isl_basic_map
*bmap
;
4544 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4545 bmap
= isl_basic_map_set_to_empty(bmap
);
4549 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4551 struct isl_basic_set
*bset
;
4552 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4553 bset
= isl_basic_set_set_to_empty(bset
);
4557 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4559 struct isl_basic_map
*bmap
;
4562 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4563 bmap
= isl_basic_map_set_to_empty(bmap
);
4567 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4569 struct isl_basic_map
*bmap
;
4572 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4573 bmap
= isl_basic_map_set_to_empty(bmap
);
4577 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4579 struct isl_basic_set
*bset
;
4582 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4583 bset
= isl_basic_set_set_to_empty(bset
);
4587 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4589 struct isl_basic_map
*bmap
;
4590 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4591 bmap
= isl_basic_map_finalize(bmap
);
4595 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4597 struct isl_basic_set
*bset
;
4598 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4599 bset
= isl_basic_set_finalize(bset
);
4603 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4606 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4607 isl_basic_map
*bmap
;
4609 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4610 for (i
= 0; i
< total
; ++i
) {
4611 int k
= isl_basic_map_alloc_inequality(bmap
);
4614 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4615 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4619 isl_basic_map_free(bmap
);
4623 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4625 return isl_basic_map_nat_universe(dim
);
4628 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4630 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4633 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4635 return isl_map_nat_universe(dim
);
4638 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4639 __isl_keep isl_basic_map
*model
)
4643 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4646 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4650 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4653 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4654 __isl_keep isl_set
*model
)
4658 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4661 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4663 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4666 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4670 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4673 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4677 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4680 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4682 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4685 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4689 return isl_set_empty(isl_space_copy(model
->dim
));
4692 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4694 struct isl_map
*map
;
4697 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4698 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4702 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4704 struct isl_set
*set
;
4707 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4708 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4712 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4716 return isl_set_universe(isl_space_copy(model
->dim
));
4719 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4722 struct isl_map
*dup
;
4726 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4727 for (i
= 0; i
< map
->n
; ++i
)
4728 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4732 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4733 __isl_take isl_basic_map
*bmap
)
4737 if (isl_basic_map_plain_is_empty(bmap
)) {
4738 isl_basic_map_free(bmap
);
4741 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4742 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4743 map
->p
[map
->n
] = bmap
;
4745 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4751 isl_basic_map_free(bmap
);
4755 void isl_map_free(struct isl_map
*map
)
4765 isl_ctx_deref(map
->ctx
);
4766 for (i
= 0; i
< map
->n
; ++i
)
4767 isl_basic_map_free(map
->p
[i
]);
4768 isl_space_free(map
->dim
);
4772 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4773 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4777 base
= isl_map_cow(base
);
4781 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4784 for (i
= 0; i
< base
->n
; ++i
) {
4785 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4786 isl_space_copy(base
->dim
), 0, 0, 0);
4796 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4797 unsigned nparam
, unsigned dim
)
4799 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4803 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4804 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4808 bmap
= isl_basic_map_cow(bmap
);
4809 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4810 j
= isl_basic_map_alloc_equality(bmap
);
4813 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4814 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4815 isl_int_set_si(bmap
->eq
[j
][0], value
);
4816 bmap
= isl_basic_map_simplify(bmap
);
4817 return isl_basic_map_finalize(bmap
);
4819 isl_basic_map_free(bmap
);
4823 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4824 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4828 bmap
= isl_basic_map_cow(bmap
);
4829 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4830 j
= isl_basic_map_alloc_equality(bmap
);
4833 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4834 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4835 isl_int_set(bmap
->eq
[j
][0], value
);
4836 bmap
= isl_basic_map_simplify(bmap
);
4837 return isl_basic_map_finalize(bmap
);
4839 isl_basic_map_free(bmap
);
4843 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4844 enum isl_dim_type type
, unsigned pos
, int value
)
4848 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4849 return isl_basic_map_fix_pos_si(bmap
,
4850 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4852 isl_basic_map_free(bmap
);
4856 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4857 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4861 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4862 return isl_basic_map_fix_pos(bmap
,
4863 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4865 isl_basic_map_free(bmap
);
4869 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4870 enum isl_dim_type type
, unsigned pos
, int value
)
4872 return (struct isl_basic_set
*)
4873 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4877 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4878 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4880 return (struct isl_basic_set
*)
4881 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4885 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4886 unsigned input
, int value
)
4888 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4891 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4892 unsigned dim
, int value
)
4894 return (struct isl_basic_set
*)
4895 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4896 isl_dim_set
, dim
, value
);
4899 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4900 enum isl_dim_type type
, unsigned pos
, int value
)
4904 map
= isl_map_cow(map
);
4908 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4909 for (i
= 0; i
< map
->n
; ++i
) {
4910 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4914 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4921 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4922 enum isl_dim_type type
, unsigned pos
, int value
)
4924 return (struct isl_set
*)
4925 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4928 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4929 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4933 map
= isl_map_cow(map
);
4937 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4938 for (i
= 0; i
< map
->n
; ++i
) {
4939 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4943 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4950 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4951 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4953 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4956 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4957 unsigned input
, int value
)
4959 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4962 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4964 return (struct isl_set
*)
4965 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4968 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4969 __isl_take isl_basic_map
*bmap
,
4970 enum isl_dim_type type
, unsigned pos
, int value
)
4976 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4977 pos
+= isl_basic_map_offset(bmap
, type
);
4978 bmap
= isl_basic_map_cow(bmap
);
4979 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4980 j
= isl_basic_map_alloc_inequality(bmap
);
4983 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4984 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4985 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4986 bmap
= isl_basic_map_simplify(bmap
);
4987 return isl_basic_map_finalize(bmap
);
4989 isl_basic_map_free(bmap
);
4993 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4994 unsigned dim
, isl_int value
)
4998 bset
= isl_basic_set_cow(bset
);
4999 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5000 j
= isl_basic_set_alloc_inequality(bset
);
5003 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5004 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5005 isl_int_neg(bset
->ineq
[j
][0], value
);
5006 bset
= isl_basic_set_simplify(bset
);
5007 return isl_basic_set_finalize(bset
);
5009 isl_basic_set_free(bset
);
5013 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5014 enum isl_dim_type type
, unsigned pos
, int value
)
5018 map
= isl_map_cow(map
);
5022 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5023 for (i
= 0; i
< map
->n
; ++i
) {
5024 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
5029 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5036 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5037 enum isl_dim_type type
, unsigned pos
, int value
)
5039 return (struct isl_set
*)
5040 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5043 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5048 set
= isl_set_cow(set
);
5052 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5053 for (i
= 0; i
< set
->n
; ++i
) {
5054 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5064 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5068 map
= isl_map_cow(map
);
5072 map
->dim
= isl_space_reverse(map
->dim
);
5075 for (i
= 0; i
< map
->n
; ++i
) {
5076 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5080 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5087 static struct isl_map
*isl_basic_map_partial_lexopt(
5088 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5089 struct isl_set
**empty
, int max
)
5093 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5094 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5096 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5098 isl_basic_map_free(bmap
);
5099 isl_basic_set_free(dom
);
5105 struct isl_map
*isl_basic_map_partial_lexmax(
5106 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5107 struct isl_set
**empty
)
5109 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5112 struct isl_map
*isl_basic_map_partial_lexmin(
5113 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5114 struct isl_set
**empty
)
5116 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5119 struct isl_set
*isl_basic_set_partial_lexmin(
5120 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5121 struct isl_set
**empty
)
5123 return (struct isl_set
*)
5124 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5128 struct isl_set
*isl_basic_set_partial_lexmax(
5129 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5130 struct isl_set
**empty
)
5132 return (struct isl_set
*)
5133 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5137 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5138 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5139 __isl_give isl_set
**empty
)
5141 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5144 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5145 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5146 __isl_give isl_set
**empty
)
5148 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5151 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5152 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5153 __isl_give isl_set
**empty
)
5155 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5158 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5159 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5160 __isl_give isl_set
**empty
)
5162 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5165 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5166 __isl_take isl_basic_map
*bmap
, int max
)
5168 isl_basic_set
*dom
= NULL
;
5169 isl_space
*dom_space
;
5173 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5174 dom
= isl_basic_set_universe(dom_space
);
5175 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5177 isl_basic_map_free(bmap
);
5181 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5182 __isl_take isl_basic_map
*bmap
)
5184 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5187 /* Given a basic map "bmap", compute the lexicographically minimal
5188 * (or maximal) image element for each domain element in dom.
5189 * Set *empty to those elements in dom that do not have an image element.
5191 * We first make sure the basic sets in dom are disjoint and then
5192 * simply collect the results over each of the basic sets separately.
5193 * We could probably improve the efficiency a bit by moving the union
5194 * domain down into the parametric integer programming.
5196 static __isl_give isl_map
*basic_map_partial_lexopt(
5197 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5198 __isl_give isl_set
**empty
, int max
)
5201 struct isl_map
*res
;
5203 dom
= isl_set_make_disjoint(dom
);
5207 if (isl_set_plain_is_empty(dom
)) {
5208 res
= isl_map_empty_like_basic_map(bmap
);
5209 *empty
= isl_set_empty_like(dom
);
5211 isl_basic_map_free(bmap
);
5215 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5216 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5218 for (i
= 1; i
< dom
->n
; ++i
) {
5219 struct isl_map
*res_i
;
5220 struct isl_set
*empty_i
;
5222 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5223 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5225 res
= isl_map_union_disjoint(res
, res_i
);
5226 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5230 isl_basic_map_free(bmap
);
5235 isl_basic_map_free(bmap
);
5239 /* Given a map "map", compute the lexicographically minimal
5240 * (or maximal) image element for each domain element in dom.
5241 * Set *empty to those elements in dom that do not have an image element.
5243 * We first compute the lexicographically minimal or maximal element
5244 * in the first basic map. This results in a partial solution "res"
5245 * and a subset "todo" of dom that still need to be handled.
5246 * We then consider each of the remaining maps in "map" and successively
5247 * improve both "res" and "todo".
5249 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5250 * Assume we are computing the lexicographical maximum.
5251 * We first compute the lexicographically maximal element in basic map i.
5252 * This results in a partial solution res_i and a subset todo_i.
5253 * Then we combine these results with those obtain for the first k basic maps
5254 * to obtain a result that is valid for the first k+1 basic maps.
5255 * In particular, the set where there is no solution is the set where
5256 * there is no solution for the first k basic maps and also no solution
5257 * for the ith basic map, i.e.,
5259 * todo^i = todo^k * todo_i
5261 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5262 * solutions, arbitrarily breaking ties in favor of res^k.
5263 * That is, when res^k(a) >= res_i(a), we pick res^k and
5264 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5265 * the lexicographic order.)
5266 * In practice, we compute
5268 * res^k * (res_i . "<=")
5272 * res_i * (res^k . "<")
5274 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5275 * where only one of res^k and res_i provides a solution and we simply pick
5282 * Note that we only compute these intersections when dom(res^k) intersects
5283 * dom(res_i). Otherwise, the only effect of these intersections is to
5284 * potentially break up res^k and res_i into smaller pieces.
5285 * We want to avoid such splintering as much as possible.
5286 * In fact, an earlier implementation of this function would look for
5287 * better results in the domain of res^k and for extra results in todo^k,
5288 * but this would always result in a splintering according to todo^k,
5289 * even when the domain of basic map i is disjoint from the domains of
5290 * the previous basic maps.
5292 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5293 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5294 __isl_give isl_set
**empty
, int max
)
5297 struct isl_map
*res
;
5298 struct isl_set
*todo
;
5303 if (isl_map_plain_is_empty(map
)) {
5311 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5312 isl_set_copy(dom
), &todo
, max
);
5314 for (i
= 1; i
< map
->n
; ++i
) {
5318 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5320 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5321 isl_set_copy(dom
), &todo_i
, max
);
5324 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5325 le
= isl_map_lex_le(dim
);
5327 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5328 le
= isl_map_lex_ge(dim
);
5330 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5331 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5332 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5333 le
= isl_map_intersect(le
, isl_map_copy(res
));
5335 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5336 res
= isl_map_intersect_domain(res
,
5337 isl_set_copy(todo_i
));
5338 res_i
= isl_map_intersect_domain(res_i
,
5339 isl_set_copy(todo
));
5342 res
= isl_map_union_disjoint(res
, res_i
);
5343 res
= isl_map_union_disjoint(res
, lt
);
5344 res
= isl_map_union_disjoint(res
, le
);
5346 todo
= isl_set_intersect(todo
, todo_i
);
5366 /* Given a map "map", compute the lexicographically minimal
5367 * (or maximal) image element for each domain element in dom.
5368 * Set *empty to those elements in dom that do not have an image element.
5370 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5372 static __isl_give isl_map
*isl_map_partial_lexopt(
5373 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5374 __isl_give isl_set
**empty
, int max
)
5378 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5379 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5380 if (!isl_space_has_named_params(map
->dim
) ||
5381 !isl_space_has_named_params(dom
->dim
))
5382 isl_die(map
->ctx
, isl_error_invalid
,
5383 "unaligned unnamed parameters", goto error
);
5384 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5385 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5386 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5395 __isl_give isl_map
*isl_map_partial_lexmax(
5396 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5397 __isl_give isl_set
**empty
)
5399 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5402 __isl_give isl_map
*isl_map_partial_lexmin(
5403 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5404 __isl_give isl_set
**empty
)
5406 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5409 __isl_give isl_set
*isl_set_partial_lexmin(
5410 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5411 __isl_give isl_set
**empty
)
5413 return (struct isl_set
*)
5414 isl_map_partial_lexmin((struct isl_map
*)set
,
5418 __isl_give isl_set
*isl_set_partial_lexmax(
5419 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5420 __isl_give isl_set
**empty
)
5422 return (struct isl_set
*)
5423 isl_map_partial_lexmax((struct isl_map
*)set
,
5427 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5429 struct isl_basic_set
*dom
= NULL
;
5434 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5435 dom
= isl_basic_set_universe(dom_dim
);
5436 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5438 isl_basic_map_free(bmap
);
5442 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5444 return isl_basic_map_lexopt(bmap
, 0);
5447 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5449 return isl_basic_map_lexopt(bmap
, 1);
5452 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5454 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5457 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5459 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5462 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5464 struct isl_set
*dom
= NULL
;
5469 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5470 dom
= isl_set_universe(dom_dim
);
5471 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5477 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5479 return isl_map_lexopt(map
, 0);
5482 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5484 return isl_map_lexopt(map
, 1);
5487 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5489 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5492 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5494 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5497 /* Extract the first and only affine expression from list
5498 * and then add it to *pwaff with the given dom.
5499 * This domain is known to be disjoint from other domains
5500 * because of the way isl_basic_map_foreach_lexmax works.
5502 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5503 __isl_take isl_aff_list
*list
, void *user
)
5505 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5507 isl_pw_aff
**pwaff
= user
;
5508 isl_pw_aff
*pwaff_i
;
5510 if (isl_aff_list_n_aff(list
) != 1)
5511 isl_die(ctx
, isl_error_internal
,
5512 "expecting single element list", goto error
);
5514 aff
= isl_aff_list_get_aff(list
, 0);
5515 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5517 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5519 isl_aff_list_free(list
);
5523 isl_basic_set_free(dom
);
5524 isl_aff_list_free(list
);
5528 /* Given a basic map with one output dimension, compute the minimum or
5529 * maximum of that dimension as an isl_pw_aff.
5531 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5532 * call update_dim_opt on each leaf of the result.
5534 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5537 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5541 dim
= isl_space_from_domain(isl_space_domain(dim
));
5542 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5543 pwaff
= isl_pw_aff_empty(dim
);
5545 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5547 return isl_pw_aff_free(pwaff
);
5552 /* Compute the minimum or maximum of the given output dimension
5553 * as a function of the parameters and the input dimensions,
5554 * but independently of the other output dimensions.
5556 * We first project out the other output dimension and then compute
5557 * the "lexicographic" maximum in each basic map, combining the results
5558 * using isl_pw_aff_union_max.
5560 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5567 n_out
= isl_map_dim(map
, isl_dim_out
);
5568 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5569 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5574 isl_space
*dim
= isl_map_get_space(map
);
5575 dim
= isl_space_domain(isl_space_from_range(dim
));
5577 return isl_pw_aff_empty(dim
);
5580 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5581 for (i
= 1; i
< map
->n
; ++i
) {
5582 isl_pw_aff
*pwaff_i
;
5584 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5585 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5593 /* Compute the maximum of the given output dimension as a function of the
5594 * parameters and input dimensions, but independently of
5595 * the other output dimensions.
5597 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5599 return map_dim_opt(map
, pos
, 1);
5602 /* Compute the minimum or maximum of the given set dimension
5603 * as a function of the parameters,
5604 * but independently of the other set dimensions.
5606 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5609 return map_dim_opt(set
, pos
, max
);
5612 /* Compute the maximum of the given set dimension as a function of the
5613 * parameters, but independently of the other set dimensions.
5615 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5617 return set_dim_opt(set
, pos
, 1);
5620 /* Compute the minimum of the given set dimension as a function of the
5621 * parameters, but independently of the other set dimensions.
5623 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5625 return set_dim_opt(set
, pos
, 0);
5628 /* Apply a preimage specified by "mat" on the parameters of "bset".
5629 * bset is assumed to have only parameters and divs.
5631 static struct isl_basic_set
*basic_set_parameter_preimage(
5632 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5639 bset
->dim
= isl_space_cow(bset
->dim
);
5643 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5645 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5647 bset
->dim
->nparam
= 0;
5648 bset
->dim
->n_out
= nparam
;
5649 bset
= isl_basic_set_preimage(bset
, mat
);
5651 bset
->dim
->nparam
= bset
->dim
->n_out
;
5652 bset
->dim
->n_out
= 0;
5657 isl_basic_set_free(bset
);
5661 /* Apply a preimage specified by "mat" on the parameters of "set".
5662 * set is assumed to have only parameters and divs.
5664 static struct isl_set
*set_parameter_preimage(
5665 struct isl_set
*set
, struct isl_mat
*mat
)
5667 isl_space
*dim
= NULL
;
5673 dim
= isl_space_copy(set
->dim
);
5674 dim
= isl_space_cow(dim
);
5678 nparam
= isl_set_dim(set
, isl_dim_param
);
5680 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5683 dim
->n_out
= nparam
;
5684 isl_set_reset_space(set
, dim
);
5685 set
= isl_set_preimage(set
, mat
);
5688 dim
= isl_space_copy(set
->dim
);
5689 dim
= isl_space_cow(dim
);
5692 dim
->nparam
= dim
->n_out
;
5694 isl_set_reset_space(set
, dim
);
5697 isl_space_free(dim
);
5704 /* Intersect the basic set "bset" with the affine space specified by the
5705 * equalities in "eq".
5707 static struct isl_basic_set
*basic_set_append_equalities(
5708 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5716 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5721 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5722 for (i
= 0; i
< eq
->n_row
; ++i
) {
5723 k
= isl_basic_set_alloc_equality(bset
);
5726 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5727 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5731 bset
= isl_basic_set_gauss(bset
, NULL
);
5732 bset
= isl_basic_set_finalize(bset
);
5737 isl_basic_set_free(bset
);
5741 /* Intersect the set "set" with the affine space specified by the
5742 * equalities in "eq".
5744 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5752 for (i
= 0; i
< set
->n
; ++i
) {
5753 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5766 /* Project the given basic set onto its parameter domain, possibly introducing
5767 * new, explicit, existential variables in the constraints.
5768 * The input has parameters and (possibly implicit) existential variables.
5769 * The output has the same parameters, but only
5770 * explicit existentially quantified variables.
5772 * The actual projection is performed by pip, but pip doesn't seem
5773 * to like equalities very much, so we first remove the equalities
5774 * among the parameters by performing a variable compression on
5775 * the parameters. Afterward, an inverse transformation is performed
5776 * and the equalities among the parameters are inserted back in.
5778 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5782 struct isl_mat
*T
, *T2
;
5783 struct isl_set
*set
;
5784 unsigned nparam
, n_div
;
5786 bset
= isl_basic_set_cow(bset
);
5790 if (bset
->n_eq
== 0)
5791 return isl_basic_set_lexmin(bset
);
5793 isl_basic_set_gauss(bset
, NULL
);
5795 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5796 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5798 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5799 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5802 if (i
== bset
->n_eq
)
5803 return isl_basic_set_lexmin(bset
);
5805 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5807 eq
= isl_mat_cow(eq
);
5808 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5809 if (T
&& T
->n_col
== 0) {
5813 bset
= isl_basic_set_set_to_empty(bset
);
5814 return isl_set_from_basic_set(bset
);
5816 bset
= basic_set_parameter_preimage(bset
, T
);
5818 set
= isl_basic_set_lexmin(bset
);
5819 set
= set_parameter_preimage(set
, T2
);
5820 set
= set_append_equalities(set
, eq
);
5824 /* Compute an explicit representation for all the existentially
5825 * quantified variables.
5826 * The input and output dimensions are first turned into parameters.
5827 * compute_divs then returns a map with the same parameters and
5828 * no input or output dimensions and the dimension specification
5829 * is reset to that of the input.
5831 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5833 struct isl_basic_set
*bset
;
5834 struct isl_set
*set
;
5835 struct isl_map
*map
;
5836 isl_space
*dim
, *orig_dim
= NULL
;
5841 bmap
= isl_basic_map_cow(bmap
);
5845 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5846 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5847 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5848 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5852 orig_dim
= bmap
->dim
;
5854 bset
= (struct isl_basic_set
*)bmap
;
5856 set
= parameter_compute_divs(bset
);
5857 map
= (struct isl_map
*)set
;
5858 map
= isl_map_reset_space(map
, orig_dim
);
5862 isl_basic_map_free(bmap
);
5866 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5874 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5875 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5876 if (isl_int_is_zero(bmap
->div
[i
][0]))
5878 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5884 static int map_divs_known(__isl_keep isl_map
*map
)
5891 for (i
= 0; i
< map
->n
; ++i
) {
5892 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5900 /* If bmap contains any unknown divs, then compute explicit
5901 * expressions for them. However, this computation may be
5902 * quite expensive, so first try to remove divs that aren't
5905 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5908 struct isl_map
*map
;
5910 known
= isl_basic_map_divs_known(bmap
);
5914 return isl_map_from_basic_map(bmap
);
5916 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5918 known
= isl_basic_map_divs_known(bmap
);
5922 return isl_map_from_basic_map(bmap
);
5924 map
= compute_divs(bmap
);
5927 isl_basic_map_free(bmap
);
5931 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5935 struct isl_map
*res
;
5942 known
= map_divs_known(map
);
5950 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5951 for (i
= 1 ; i
< map
->n
; ++i
) {
5953 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5954 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5955 res
= isl_map_union_disjoint(res
, r2
);
5957 res
= isl_map_union(res
, r2
);
5964 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5966 return (struct isl_set
*)
5967 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5970 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5972 return (struct isl_set
*)
5973 isl_map_compute_divs((struct isl_map
*)set
);
5976 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5979 struct isl_set
*set
;
5984 map
= isl_map_cow(map
);
5988 set
= (struct isl_set
*)map
;
5989 set
->dim
= isl_space_domain(set
->dim
);
5992 for (i
= 0; i
< map
->n
; ++i
) {
5993 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5997 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5998 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6005 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6006 __isl_take isl_map
*map2
)
6010 struct isl_map
*map
= NULL
;
6024 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6026 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6027 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6028 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6030 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6031 map1
->n
+ map2
->n
, flags
);
6034 for (i
= 0; i
< map1
->n
; ++i
) {
6035 map
= isl_map_add_basic_map(map
,
6036 isl_basic_map_copy(map1
->p
[i
]));
6040 for (i
= 0; i
< map2
->n
; ++i
) {
6041 map
= isl_map_add_basic_map(map
,
6042 isl_basic_map_copy(map2
->p
[i
]));
6056 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6057 __isl_take isl_map
*map2
)
6059 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6062 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6064 map1
= isl_map_union_disjoint(map1
, map2
);
6068 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6072 struct isl_set
*isl_set_union_disjoint(
6073 struct isl_set
*set1
, struct isl_set
*set2
)
6075 return (struct isl_set
*)
6076 isl_map_union_disjoint(
6077 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6080 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6082 return (struct isl_set
*)
6083 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6086 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6087 __isl_take isl_set
*set
)
6090 struct isl_map
*result
;
6096 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6097 isl_die(set
->ctx
, isl_error_invalid
,
6098 "parameters don't match", goto error
);
6100 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6101 !isl_map_compatible_range(map
, set
))
6102 isl_die(set
->ctx
, isl_error_invalid
,
6103 "incompatible spaces", goto error
);
6105 if (isl_set_plain_is_universe(set
)) {
6110 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6111 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6112 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6114 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6115 map
->n
* set
->n
, flags
);
6118 for (i
= 0; i
< map
->n
; ++i
)
6119 for (j
= 0; j
< set
->n
; ++j
) {
6120 result
= isl_map_add_basic_map(result
,
6121 isl_basic_map_intersect_range(
6122 isl_basic_map_copy(map
->p
[i
]),
6123 isl_basic_set_copy(set
->p
[j
])));
6136 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6137 __isl_take isl_set
*set
)
6139 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6142 struct isl_map
*isl_map_intersect_domain(
6143 struct isl_map
*map
, struct isl_set
*set
)
6145 return isl_map_reverse(
6146 isl_map_intersect_range(isl_map_reverse(map
), set
));
6149 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6150 __isl_take isl_map
*map2
)
6154 map1
= isl_map_reverse(map1
);
6155 map1
= isl_map_apply_range(map1
, map2
);
6156 return isl_map_reverse(map1
);
6163 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6164 __isl_take isl_map
*map2
)
6166 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6169 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6170 __isl_take isl_map
*map2
)
6172 isl_space
*dim_result
;
6173 struct isl_map
*result
;
6179 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6180 isl_space_copy(map2
->dim
));
6182 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6185 for (i
= 0; i
< map1
->n
; ++i
)
6186 for (j
= 0; j
< map2
->n
; ++j
) {
6187 result
= isl_map_add_basic_map(result
,
6188 isl_basic_map_apply_range(
6189 isl_basic_map_copy(map1
->p
[i
]),
6190 isl_basic_map_copy(map2
->p
[j
])));
6196 if (result
&& result
->n
<= 1)
6197 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6205 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6206 __isl_take isl_map
*map2
)
6208 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6212 * returns range - domain
6214 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6216 isl_space
*dims
, *target_dim
;
6217 struct isl_basic_set
*bset
;
6224 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6225 bmap
->dim
, isl_dim_out
),
6227 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6228 dim
= isl_basic_map_n_in(bmap
);
6229 nparam
= isl_basic_map_n_param(bmap
);
6230 bset
= isl_basic_set_from_basic_map(bmap
);
6231 bset
= isl_basic_set_cow(bset
);
6232 dims
= isl_basic_set_get_space(bset
);
6233 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6234 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6235 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6236 for (i
= 0; i
< dim
; ++i
) {
6237 int j
= isl_basic_map_alloc_equality(
6238 (struct isl_basic_map
*)bset
);
6241 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6242 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6243 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6244 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6246 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6247 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6250 isl_basic_map_free(bmap
);
6255 * returns range - domain
6257 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6261 struct isl_set
*result
;
6266 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6267 map
->dim
, isl_dim_out
),
6269 dim
= isl_map_get_space(map
);
6270 dim
= isl_space_domain(dim
);
6271 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6274 for (i
= 0; i
< map
->n
; ++i
)
6275 result
= isl_set_add_basic_set(result
,
6276 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6285 * returns [domain -> range] -> range - domain
6287 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6288 __isl_take isl_basic_map
*bmap
)
6292 isl_basic_map
*domain
;
6296 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6297 isl_die(bmap
->ctx
, isl_error_invalid
,
6298 "domain and range don't match", goto error
);
6300 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6301 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6303 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6304 domain
= isl_basic_map_universe(dim
);
6306 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6307 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6308 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6310 total
= isl_basic_map_total_dim(bmap
);
6312 for (i
= 0; i
< n
; ++i
) {
6313 k
= isl_basic_map_alloc_equality(bmap
);
6316 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6317 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6318 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6319 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6322 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6323 return isl_basic_map_finalize(bmap
);
6325 isl_basic_map_free(bmap
);
6330 * returns [domain -> range] -> range - domain
6332 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6335 isl_space
*domain_dim
;
6340 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6341 isl_die(map
->ctx
, isl_error_invalid
,
6342 "domain and range don't match", goto error
);
6344 map
= isl_map_cow(map
);
6348 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6349 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6350 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6353 for (i
= 0; i
< map
->n
; ++i
) {
6354 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6358 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6365 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6367 struct isl_basic_map
*bmap
;
6375 nparam
= dims
->nparam
;
6377 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6381 for (i
= 0; i
< dim
; ++i
) {
6382 int j
= isl_basic_map_alloc_equality(bmap
);
6385 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6386 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6387 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6389 return isl_basic_map_finalize(bmap
);
6391 isl_basic_map_free(bmap
);
6395 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6399 if (dim
->n_in
!= dim
->n_out
)
6400 isl_die(dim
->ctx
, isl_error_invalid
,
6401 "number of input and output dimensions needs to be "
6402 "the same", goto error
);
6403 return basic_map_identity(dim
);
6405 isl_space_free(dim
);
6409 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6411 if (!model
|| !model
->dim
)
6413 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6416 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6418 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6421 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6423 if (!model
|| !model
->dim
)
6425 return isl_map_identity(isl_space_copy(model
->dim
));
6428 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6430 if (!model
|| !model
->dim
)
6432 return isl_map_identity(isl_space_copy(model
->dim
));
6435 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6437 isl_space
*dim
= isl_set_get_space(set
);
6439 id
= isl_map_identity(isl_space_map_from_set(dim
));
6440 return isl_map_intersect_range(id
, set
);
6443 /* Construct a basic set with all set dimensions having only non-negative
6446 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6447 __isl_take isl_space
*space
)
6452 struct isl_basic_set
*bset
;
6456 nparam
= space
->nparam
;
6458 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6461 for (i
= 0; i
< dim
; ++i
) {
6462 int k
= isl_basic_set_alloc_inequality(bset
);
6465 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6466 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6470 isl_basic_set_free(bset
);
6474 /* Construct the half-space x_pos >= 0.
6476 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6480 isl_basic_set
*nonneg
;
6482 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6483 k
= isl_basic_set_alloc_inequality(nonneg
);
6486 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6487 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6489 return isl_basic_set_finalize(nonneg
);
6491 isl_basic_set_free(nonneg
);
6495 /* Construct the half-space x_pos <= -1.
6497 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6502 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6503 k
= isl_basic_set_alloc_inequality(neg
);
6506 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6507 isl_int_set_si(neg
->ineq
[k
][0], -1);
6508 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6510 return isl_basic_set_finalize(neg
);
6512 isl_basic_set_free(neg
);
6516 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6517 enum isl_dim_type type
, unsigned first
, unsigned n
)
6520 isl_basic_set
*nonneg
;
6528 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6530 for (i
= 0; i
< n
; ++i
) {
6531 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6532 pos(set
->dim
, type
) + first
+ i
);
6533 neg
= neg_halfspace(isl_set_get_space(set
),
6534 pos(set
->dim
, type
) + first
+ i
);
6536 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6545 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6546 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6553 if (isl_set_plain_is_empty(set
)) {
6558 return fn(set
, signs
, user
);
6561 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6563 half
= isl_set_intersect(half
, isl_set_copy(set
));
6564 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6568 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6570 half
= isl_set_intersect(half
, set
);
6571 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6577 /* Call "fn" on the intersections of "set" with each of the orthants
6578 * (except for obviously empty intersections). The orthant is identified
6579 * by the signs array, with each entry having value 1 or -1 according
6580 * to the sign of the corresponding variable.
6582 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6583 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6593 if (isl_set_plain_is_empty(set
))
6596 nparam
= isl_set_dim(set
, isl_dim_param
);
6597 nvar
= isl_set_dim(set
, isl_dim_set
);
6599 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6601 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6609 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6611 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6614 int isl_basic_map_is_subset(
6615 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6618 struct isl_map
*map1
;
6619 struct isl_map
*map2
;
6621 if (!bmap1
|| !bmap2
)
6624 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6625 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6627 is_subset
= isl_map_is_subset(map1
, map2
);
6635 int isl_basic_map_is_equal(
6636 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6640 if (!bmap1
|| !bmap2
)
6642 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6645 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6649 int isl_basic_set_is_equal(
6650 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6652 return isl_basic_map_is_equal(
6653 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6656 int isl_map_is_empty(struct isl_map
*map
)
6663 for (i
= 0; i
< map
->n
; ++i
) {
6664 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6673 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6675 return map
? map
->n
== 0 : -1;
6678 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6680 return isl_map_plain_is_empty(map
);
6683 int isl_set_plain_is_empty(struct isl_set
*set
)
6685 return set
? set
->n
== 0 : -1;
6688 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6690 return isl_set_plain_is_empty(set
);
6693 int isl_set_is_empty(struct isl_set
*set
)
6695 return isl_map_is_empty((struct isl_map
*)set
);
6698 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6703 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6706 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6711 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6714 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6720 is_subset
= isl_map_is_subset(map1
, map2
);
6723 is_subset
= isl_map_is_subset(map2
, map1
);
6727 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6729 return align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6732 int isl_basic_map_is_strict_subset(
6733 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6737 if (!bmap1
|| !bmap2
)
6739 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6742 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6743 if (is_subset
== -1)
6748 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6754 is_subset
= isl_map_is_subset(map1
, map2
);
6757 is_subset
= isl_map_is_subset(map2
, map1
);
6758 if (is_subset
== -1)
6763 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6765 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6768 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6772 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6775 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6779 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6782 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6789 for (i
= 0; i
< map
->n
; ++i
) {
6790 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6798 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6800 return isl_map_plain_is_universe((isl_map
*) set
);
6803 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6805 return isl_set_plain_is_universe(set
);
6808 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6810 struct isl_basic_set
*bset
= NULL
;
6811 struct isl_vec
*sample
= NULL
;
6818 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6821 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6822 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6823 copy
= isl_basic_map_remove_redundancies(copy
);
6824 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6825 isl_basic_map_free(copy
);
6829 total
= 1 + isl_basic_map_total_dim(bmap
);
6830 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6831 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6837 isl_vec_free(bmap
->sample
);
6838 bmap
->sample
= NULL
;
6839 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6842 sample
= isl_basic_set_sample_vec(bset
);
6845 empty
= sample
->size
== 0;
6846 isl_vec_free(bmap
->sample
);
6847 bmap
->sample
= sample
;
6849 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6854 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6858 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6861 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6863 return isl_basic_map_plain_is_empty(bmap
);
6866 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6870 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6873 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6875 return isl_basic_set_plain_is_empty(bset
);
6878 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6880 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6883 struct isl_map
*isl_basic_map_union(
6884 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6886 struct isl_map
*map
;
6887 if (!bmap1
|| !bmap2
)
6890 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6892 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6895 map
= isl_map_add_basic_map(map
, bmap1
);
6896 map
= isl_map_add_basic_map(map
, bmap2
);
6899 isl_basic_map_free(bmap1
);
6900 isl_basic_map_free(bmap2
);
6904 struct isl_set
*isl_basic_set_union(
6905 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6907 return (struct isl_set
*)isl_basic_map_union(
6908 (struct isl_basic_map
*)bset1
,
6909 (struct isl_basic_map
*)bset2
);
6912 /* Order divs such that any div only depends on previous divs */
6913 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6921 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6923 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6925 if (isl_int_is_zero(bmap
->div
[i
][0]))
6927 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6931 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6937 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6939 return (struct isl_basic_set
*)
6940 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6943 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6950 for (i
= 0; i
< map
->n
; ++i
) {
6951 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6962 /* Apply the expansion computed by isl_merge_divs.
6963 * The expansion itself is given by "exp" while the resulting
6964 * list of divs is given by "div".
6966 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6967 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6972 bset
= isl_basic_set_cow(bset
);
6976 if (div
->n_row
< bset
->n_div
)
6977 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6978 "not an expansion", goto error
);
6980 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6981 div
->n_row
- bset
->n_div
, 0,
6982 2 * (div
->n_row
- bset
->n_div
));
6984 n_div
= bset
->n_div
;
6985 for (i
= n_div
; i
< div
->n_row
; ++i
)
6986 if (isl_basic_set_alloc_div(bset
) < 0)
6990 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6991 if (j
>= 0 && exp
[j
] == i
) {
6993 isl_basic_map_swap_div(bset
, i
, j
);
6996 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6997 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7005 isl_basic_set_free(bset
);
7010 /* Look for a div in dst that corresponds to the div "div" in src.
7011 * The divs before "div" in src and dst are assumed to be the same.
7013 * Returns -1 if no corresponding div was found and the position
7014 * of the corresponding div in dst otherwise.
7016 static int find_div(struct isl_basic_map
*dst
,
7017 struct isl_basic_map
*src
, unsigned div
)
7021 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7023 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7024 for (i
= div
; i
< dst
->n_div
; ++i
)
7025 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7026 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7027 dst
->n_div
- div
) == -1)
7032 struct isl_basic_map
*isl_basic_map_align_divs(
7033 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7036 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7041 if (src
->n_div
== 0)
7044 for (i
= 0; i
< src
->n_div
; ++i
)
7045 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7047 src
= isl_basic_map_order_divs(src
);
7048 dst
= isl_basic_map_cow(dst
);
7049 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7050 src
->n_div
, 0, 2 * src
->n_div
);
7053 for (i
= 0; i
< src
->n_div
; ++i
) {
7054 int j
= find_div(dst
, src
, i
);
7056 j
= isl_basic_map_alloc_div(dst
);
7059 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7060 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7061 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7065 isl_basic_map_swap_div(dst
, i
, j
);
7069 isl_basic_map_free(dst
);
7073 struct isl_basic_set
*isl_basic_set_align_divs(
7074 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7076 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7077 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7080 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7088 map
= isl_map_compute_divs(map
);
7089 map
= isl_map_cow(map
);
7093 for (i
= 1; i
< map
->n
; ++i
)
7094 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7095 for (i
= 1; i
< map
->n
; ++i
)
7096 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7098 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7102 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7104 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7107 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7108 __isl_take isl_map
*map
)
7112 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7113 map
= isl_map_intersect_domain(map
, set
);
7114 set
= isl_map_range(map
);
7122 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7123 __isl_take isl_map
*map
)
7125 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7128 /* There is no need to cow as removing empty parts doesn't change
7129 * the meaning of the set.
7131 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7138 for (i
= map
->n
-1; i
>= 0; --i
) {
7139 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7141 isl_basic_map_free(map
->p
[i
]);
7142 if (i
!= map
->n
-1) {
7143 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7144 map
->p
[i
] = map
->p
[map
->n
-1];
7152 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7154 return (struct isl_set
*)
7155 isl_map_remove_empty_parts((struct isl_map
*)set
);
7158 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7160 struct isl_basic_map
*bmap
;
7161 if (!map
|| map
->n
== 0)
7163 bmap
= map
->p
[map
->n
-1];
7164 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7165 return isl_basic_map_copy(bmap
);
7168 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7170 return (struct isl_basic_set
*)
7171 isl_map_copy_basic_map((struct isl_map
*)set
);
7174 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7175 __isl_keep isl_basic_map
*bmap
)
7181 for (i
= map
->n
-1; i
>= 0; --i
) {
7182 if (map
->p
[i
] != bmap
)
7184 map
= isl_map_cow(map
);
7187 isl_basic_map_free(map
->p
[i
]);
7188 if (i
!= map
->n
-1) {
7189 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7190 map
->p
[i
] = map
->p
[map
->n
-1];
7201 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7202 struct isl_basic_set
*bset
)
7204 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7205 (struct isl_basic_map
*)bset
);
7208 /* Given two basic sets bset1 and bset2, compute the maximal difference
7209 * between the values of dimension pos in bset1 and those in bset2
7210 * for any common value of the parameters and dimensions preceding pos.
7212 static enum isl_lp_result
basic_set_maximal_difference_at(
7213 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7214 int pos
, isl_int
*opt
)
7217 struct isl_basic_map
*bmap1
= NULL
;
7218 struct isl_basic_map
*bmap2
= NULL
;
7219 struct isl_ctx
*ctx
;
7220 struct isl_vec
*obj
;
7223 unsigned dim1
, dim2
;
7224 enum isl_lp_result res
;
7226 if (!bset1
|| !bset2
)
7227 return isl_lp_error
;
7229 nparam
= isl_basic_set_n_param(bset1
);
7230 dim1
= isl_basic_set_n_dim(bset1
);
7231 dim2
= isl_basic_set_n_dim(bset2
);
7232 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7233 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7234 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7235 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7236 if (!bmap1
|| !bmap2
)
7238 bmap1
= isl_basic_map_cow(bmap1
);
7239 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7240 pos
, (dim1
- pos
) + (dim2
- pos
),
7241 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7242 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7245 total
= isl_basic_map_total_dim(bmap1
);
7247 obj
= isl_vec_alloc(ctx
, 1 + total
);
7248 isl_seq_clr(obj
->block
.data
, 1 + total
);
7249 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7250 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7253 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7255 isl_basic_map_free(bmap1
);
7259 isl_basic_map_free(bmap1
);
7260 isl_basic_map_free(bmap2
);
7261 return isl_lp_error
;
7264 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7265 * for any common value of the parameters and dimensions preceding pos
7266 * in both basic sets, the values of dimension pos in bset1 are
7267 * smaller or larger than those in bset2.
7270 * 1 if bset1 follows bset2
7271 * -1 if bset1 precedes bset2
7272 * 0 if bset1 and bset2 are incomparable
7273 * -2 if some error occurred.
7275 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7276 struct isl_basic_set
*bset2
, int pos
)
7279 enum isl_lp_result res
;
7284 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7286 if (res
== isl_lp_empty
)
7288 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7289 res
== isl_lp_unbounded
)
7291 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7300 /* Given two basic sets bset1 and bset2, check whether
7301 * for any common value of the parameters and dimensions preceding pos
7302 * there is a value of dimension pos in bset1 that is larger
7303 * than a value of the same dimension in bset2.
7306 * 1 if there exists such a pair
7307 * 0 if there is no such pair, but there is a pair of equal values
7309 * -2 if some error occurred.
7311 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7312 __isl_keep isl_basic_set
*bset2
, int pos
)
7315 enum isl_lp_result res
;
7320 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7322 if (res
== isl_lp_empty
)
7324 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7325 res
== isl_lp_unbounded
)
7327 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7329 else if (res
== isl_lp_ok
)
7338 /* Given two sets set1 and set2, check whether
7339 * for any common value of the parameters and dimensions preceding pos
7340 * there is a value of dimension pos in set1 that is larger
7341 * than a value of the same dimension in set2.
7344 * 1 if there exists such a pair
7345 * 0 if there is no such pair, but there is a pair of equal values
7347 * -2 if some error occurred.
7349 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7350 __isl_keep isl_set
*set2
, int pos
)
7358 for (i
= 0; i
< set1
->n
; ++i
)
7359 for (j
= 0; j
< set2
->n
; ++j
) {
7361 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7362 if (f
== 1 || f
== -2)
7371 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7372 unsigned pos
, isl_int
*val
)
7380 total
= isl_basic_map_total_dim(bmap
);
7381 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7382 for (; d
+1 > pos
; --d
)
7383 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7387 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7389 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7391 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7394 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7400 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7401 unsigned pos
, isl_int
*val
)
7413 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7416 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7417 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7418 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7419 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7423 isl_int_set(*val
, v
);
7429 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7430 unsigned pos
, isl_int
*val
)
7432 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7436 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7439 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7442 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7443 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7445 if (pos
>= isl_basic_map_dim(bmap
, type
))
7447 return isl_basic_map_plain_has_fixed_var(bmap
,
7448 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7451 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7452 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7454 if (pos
>= isl_map_dim(map
, type
))
7456 return isl_map_plain_has_fixed_var(map
,
7457 map_offset(map
, type
) - 1 + pos
, val
);
7460 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7461 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7463 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7466 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7467 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7469 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7472 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7473 * then return this fixed value in *val.
7475 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7476 unsigned dim
, isl_int
*val
)
7478 return isl_basic_set_plain_has_fixed_var(bset
,
7479 isl_basic_set_n_param(bset
) + dim
, val
);
7482 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7483 * then return this fixed value in *val.
7485 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7486 unsigned dim
, isl_int
*val
)
7488 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7491 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7492 unsigned dim
, isl_int
*val
)
7494 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7497 /* Check if input variable in has fixed value and if so and if val is not NULL,
7498 * then return this fixed value in *val.
7500 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7501 unsigned in
, isl_int
*val
)
7503 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7506 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7507 * and if val is not NULL, then return this lower bound in *val.
7509 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7510 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7512 int i
, i_eq
= -1, i_ineq
= -1;
7519 total
= isl_basic_set_total_dim(bset
);
7520 nparam
= isl_basic_set_n_param(bset
);
7521 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7522 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7528 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7529 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7531 if (i_eq
!= -1 || i_ineq
!= -1)
7535 if (i_eq
== -1 && i_ineq
== -1)
7537 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7538 /* The coefficient should always be one due to normalization. */
7539 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7541 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7543 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7544 total
- nparam
- dim
- 1) != -1)
7547 isl_int_neg(*val
, c
[0]);
7551 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7552 unsigned dim
, isl_int
*val
)
7564 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7568 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7570 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7571 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7573 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7577 isl_int_set(*val
, v
);
7588 /* uset_gist depends on constraints without existentially quantified
7589 * variables sorting first.
7591 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7593 const struct constraint
*c1
= (const struct constraint
*)p1
;
7594 const struct constraint
*c2
= (const struct constraint
*)p2
;
7596 unsigned size
= isl_min(c1
->size
, c2
->size
);
7598 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7599 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7604 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7607 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7608 struct isl_basic_map
*bmap
)
7611 struct constraint
*c
;
7616 total
= isl_basic_map_total_dim(bmap
);
7617 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7620 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7622 c
[i
].c
= bmap
->ineq
[i
];
7624 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7625 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7626 bmap
->ineq
[i
] = c
[i
].c
;
7630 isl_basic_map_free(bmap
);
7634 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7635 __isl_take isl_basic_set
*bset
)
7637 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7638 (struct isl_basic_map
*)bset
);
7641 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7645 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7647 bmap
= isl_basic_map_remove_redundancies(bmap
);
7648 bmap
= isl_basic_map_sort_constraints(bmap
);
7649 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7653 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7655 return (struct isl_basic_set
*)isl_basic_map_normalize(
7656 (struct isl_basic_map
*)bset
);
7659 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7660 const __isl_keep isl_basic_map
*bmap2
)
7667 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7668 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7669 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7670 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7671 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7672 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7673 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7674 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7675 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7676 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7677 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7679 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7681 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7683 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7684 return bmap1
->n_eq
- bmap2
->n_eq
;
7685 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7686 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7687 if (bmap1
->n_div
!= bmap2
->n_div
)
7688 return bmap1
->n_div
- bmap2
->n_div
;
7689 total
= isl_basic_map_total_dim(bmap1
);
7690 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7691 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7695 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7696 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7700 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7701 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7708 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7709 const __isl_keep isl_basic_set
*bset2
)
7711 return isl_basic_map_plain_cmp(bset1
, bset2
);
7714 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7715 const __isl_keep isl_set
*set2
)
7721 if (set1
->n
!= set2
->n
)
7722 return set1
->n
- set2
->n
;
7724 for (i
= 0; i
< set1
->n
; ++i
) {
7725 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7733 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7734 __isl_keep isl_basic_map
*bmap2
)
7736 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7739 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7740 __isl_keep isl_basic_set
*bset2
)
7742 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7743 (isl_basic_map
*)bset2
);
7746 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7748 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7749 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7751 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7754 /* We normalize in place, but if anything goes wrong we need
7755 * to return NULL, so we need to make sure we don't change the
7756 * meaning of any possible other copies of map.
7758 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7761 struct isl_basic_map
*bmap
;
7765 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7767 for (i
= 0; i
< map
->n
; ++i
) {
7768 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7771 isl_basic_map_free(map
->p
[i
]);
7774 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7775 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7776 map
= isl_map_remove_empty_parts(map
);
7779 for (i
= map
->n
- 1; i
>= 1; --i
) {
7780 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7782 isl_basic_map_free(map
->p
[i
-1]);
7783 for (j
= i
; j
< map
->n
; ++j
)
7784 map
->p
[j
-1] = map
->p
[j
];
7794 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7796 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7799 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7809 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7812 map1
= isl_map_copy(map1
);
7813 map2
= isl_map_copy(map2
);
7814 map1
= isl_map_normalize(map1
);
7815 map2
= isl_map_normalize(map2
);
7818 equal
= map1
->n
== map2
->n
;
7819 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7820 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7833 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7835 return isl_map_plain_is_equal(map1
, map2
);
7838 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7840 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7841 (struct isl_map
*)set2
);
7844 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7846 return isl_set_plain_is_equal(set1
, set2
);
7849 /* Return an interval that ranges from min to max (inclusive)
7851 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7852 isl_int min
, isl_int max
)
7855 struct isl_basic_set
*bset
= NULL
;
7857 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7861 k
= isl_basic_set_alloc_inequality(bset
);
7864 isl_int_set_si(bset
->ineq
[k
][1], 1);
7865 isl_int_neg(bset
->ineq
[k
][0], min
);
7867 k
= isl_basic_set_alloc_inequality(bset
);
7870 isl_int_set_si(bset
->ineq
[k
][1], -1);
7871 isl_int_set(bset
->ineq
[k
][0], max
);
7875 isl_basic_set_free(bset
);
7879 /* Return the Cartesian product of the basic sets in list (in the given order).
7881 __isl_give isl_basic_set
*isl_basic_set_list_product(
7882 __isl_take
struct isl_basic_set_list
*list
)
7890 struct isl_basic_set
*product
= NULL
;
7894 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7895 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7896 nparam
= isl_basic_set_n_param(list
->p
[0]);
7897 dim
= isl_basic_set_n_dim(list
->p
[0]);
7898 extra
= list
->p
[0]->n_div
;
7899 n_eq
= list
->p
[0]->n_eq
;
7900 n_ineq
= list
->p
[0]->n_ineq
;
7901 for (i
= 1; i
< list
->n
; ++i
) {
7902 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7903 isl_assert(list
->ctx
,
7904 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7905 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7906 extra
+= list
->p
[i
]->n_div
;
7907 n_eq
+= list
->p
[i
]->n_eq
;
7908 n_ineq
+= list
->p
[i
]->n_ineq
;
7910 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7915 for (i
= 0; i
< list
->n
; ++i
) {
7916 isl_basic_set_add_constraints(product
,
7917 isl_basic_set_copy(list
->p
[i
]), dim
);
7918 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7920 isl_basic_set_list_free(list
);
7923 isl_basic_set_free(product
);
7924 isl_basic_set_list_free(list
);
7928 struct isl_basic_map
*isl_basic_map_product(
7929 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7931 isl_space
*dim_result
= NULL
;
7932 struct isl_basic_map
*bmap
;
7933 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7934 struct isl_dim_map
*dim_map1
, *dim_map2
;
7936 if (!bmap1
|| !bmap2
)
7939 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7940 bmap2
->dim
, isl_dim_param
), goto error
);
7941 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7942 isl_space_copy(bmap2
->dim
));
7944 in1
= isl_basic_map_n_in(bmap1
);
7945 in2
= isl_basic_map_n_in(bmap2
);
7946 out1
= isl_basic_map_n_out(bmap1
);
7947 out2
= isl_basic_map_n_out(bmap2
);
7948 nparam
= isl_basic_map_n_param(bmap1
);
7950 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7951 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7952 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7953 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7954 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7955 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7956 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7957 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7958 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7959 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7960 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7962 bmap
= isl_basic_map_alloc_space(dim_result
,
7963 bmap1
->n_div
+ bmap2
->n_div
,
7964 bmap1
->n_eq
+ bmap2
->n_eq
,
7965 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7966 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7967 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7968 bmap
= isl_basic_map_simplify(bmap
);
7969 return isl_basic_map_finalize(bmap
);
7971 isl_basic_map_free(bmap1
);
7972 isl_basic_map_free(bmap2
);
7976 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7977 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7979 isl_basic_map
*prod
;
7981 prod
= isl_basic_map_product(bmap1
, bmap2
);
7982 prod
= isl_basic_map_flatten(prod
);
7986 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7987 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7989 return isl_basic_map_flat_range_product(bset1
, bset2
);
7992 __isl_give isl_basic_map
*isl_basic_map_domain_product(
7993 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7995 isl_space
*space_result
= NULL
;
7996 isl_basic_map
*bmap
;
7997 unsigned in1
, in2
, out
, nparam
, total
, pos
;
7998 struct isl_dim_map
*dim_map1
, *dim_map2
;
8000 if (!bmap1
|| !bmap2
)
8003 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8004 isl_space_copy(bmap2
->dim
));
8006 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8007 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8008 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8009 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8011 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8012 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8013 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8014 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8015 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8016 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8017 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8018 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8019 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8020 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8021 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8023 bmap
= isl_basic_map_alloc_space(space_result
,
8024 bmap1
->n_div
+ bmap2
->n_div
,
8025 bmap1
->n_eq
+ bmap2
->n_eq
,
8026 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8027 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8028 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8029 bmap
= isl_basic_map_simplify(bmap
);
8030 return isl_basic_map_finalize(bmap
);
8032 isl_basic_map_free(bmap1
);
8033 isl_basic_map_free(bmap2
);
8037 __isl_give isl_basic_map
*isl_basic_map_range_product(
8038 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8040 isl_space
*dim_result
= NULL
;
8041 isl_basic_map
*bmap
;
8042 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8043 struct isl_dim_map
*dim_map1
, *dim_map2
;
8045 if (!bmap1
|| !bmap2
)
8048 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8049 isl_space_copy(bmap2
->dim
));
8051 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8052 out1
= isl_basic_map_n_out(bmap1
);
8053 out2
= isl_basic_map_n_out(bmap2
);
8054 nparam
= isl_basic_map_n_param(bmap1
);
8056 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8057 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8058 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8059 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8060 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8061 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8062 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8063 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8064 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8065 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8066 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8068 bmap
= isl_basic_map_alloc_space(dim_result
,
8069 bmap1
->n_div
+ bmap2
->n_div
,
8070 bmap1
->n_eq
+ bmap2
->n_eq
,
8071 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8072 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8073 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8074 bmap
= isl_basic_map_simplify(bmap
);
8075 return isl_basic_map_finalize(bmap
);
8077 isl_basic_map_free(bmap1
);
8078 isl_basic_map_free(bmap2
);
8082 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8083 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8085 isl_basic_map
*prod
;
8087 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8088 prod
= isl_basic_map_flatten_range(prod
);
8092 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8093 __isl_take isl_map
*map2
,
8094 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8095 __isl_take isl_space
*right
),
8096 __isl_give isl_basic_map
*(*basic_map_product
)(
8097 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8100 struct isl_map
*result
;
8106 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8107 map2
->dim
, isl_dim_param
), goto error
);
8109 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8110 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8111 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8113 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8114 isl_space_copy(map2
->dim
)),
8115 map1
->n
* map2
->n
, flags
);
8118 for (i
= 0; i
< map1
->n
; ++i
)
8119 for (j
= 0; j
< map2
->n
; ++j
) {
8120 struct isl_basic_map
*part
;
8121 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8122 isl_basic_map_copy(map2
->p
[j
]));
8123 if (isl_basic_map_is_empty(part
))
8124 isl_basic_map_free(part
);
8126 result
= isl_map_add_basic_map(result
, part
);
8139 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8141 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8142 __isl_take isl_map
*map2
)
8144 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8147 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8148 __isl_take isl_map
*map2
)
8150 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8153 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8155 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8156 __isl_take isl_map
*map2
)
8160 prod
= isl_map_product(map1
, map2
);
8161 prod
= isl_map_flatten(prod
);
8165 /* Given two set A and B, construct its Cartesian product A x B.
8167 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8169 return isl_map_range_product(set1
, set2
);
8172 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8173 __isl_take isl_set
*set2
)
8175 return isl_map_flat_range_product(set1
, set2
);
8178 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8180 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8181 __isl_take isl_map
*map2
)
8183 return map_product(map1
, map2
, &isl_space_domain_product
,
8184 &isl_basic_map_domain_product
);
8187 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8189 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8190 __isl_take isl_map
*map2
)
8192 return map_product(map1
, map2
, &isl_space_range_product
,
8193 &isl_basic_map_range_product
);
8196 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8197 __isl_take isl_map
*map2
)
8199 return isl_map_align_params_map_map_and(map1
, map2
,
8200 &map_domain_product_aligned
);
8203 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8204 __isl_take isl_map
*map2
)
8206 return isl_map_align_params_map_map_and(map1
, map2
,
8207 &map_range_product_aligned
);
8210 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8212 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8213 __isl_take isl_map
*map2
)
8217 prod
= isl_map_domain_product(map1
, map2
);
8218 prod
= isl_map_flatten_domain(prod
);
8222 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8224 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8225 __isl_take isl_map
*map2
)
8229 prod
= isl_map_range_product(map1
, map2
);
8230 prod
= isl_map_flatten_range(prod
);
8234 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8237 uint32_t hash
= isl_hash_init();
8242 bmap
= isl_basic_map_copy(bmap
);
8243 bmap
= isl_basic_map_normalize(bmap
);
8246 total
= isl_basic_map_total_dim(bmap
);
8247 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8248 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8250 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8251 isl_hash_hash(hash
, c_hash
);
8253 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8254 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8256 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8257 isl_hash_hash(hash
, c_hash
);
8259 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8260 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8262 if (isl_int_is_zero(bmap
->div
[i
][0]))
8264 isl_hash_byte(hash
, i
& 0xFF);
8265 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8266 isl_hash_hash(hash
, c_hash
);
8268 isl_basic_map_free(bmap
);
8272 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8274 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8277 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8284 map
= isl_map_copy(map
);
8285 map
= isl_map_normalize(map
);
8289 hash
= isl_hash_init();
8290 for (i
= 0; i
< map
->n
; ++i
) {
8292 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8293 isl_hash_hash(hash
, bmap_hash
);
8301 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8303 return isl_map_get_hash((isl_map
*)set
);
8306 /* Check if the value for dimension dim is completely determined
8307 * by the values of the other parameters and variables.
8308 * That is, check if dimension dim is involved in an equality.
8310 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8317 nparam
= isl_basic_set_n_param(bset
);
8318 for (i
= 0; i
< bset
->n_eq
; ++i
)
8319 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8324 /* Check if the value for dimension dim is completely determined
8325 * by the values of the other parameters and variables.
8326 * That is, check if dimension dim is involved in an equality
8327 * for each of the subsets.
8329 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8335 for (i
= 0; i
< set
->n
; ++i
) {
8337 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8344 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8346 return set
? set
->n
: 0;
8349 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8350 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8357 for (i
= 0; i
< map
->n
; ++i
)
8358 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8364 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8365 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8372 for (i
= 0; i
< set
->n
; ++i
)
8373 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8379 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8386 bset
= isl_basic_set_cow(bset
);
8390 dim
= isl_basic_set_get_space(bset
);
8391 dim
= isl_space_lift(dim
, bset
->n_div
);
8394 isl_space_free(bset
->dim
);
8396 bset
->extra
-= bset
->n_div
;
8399 bset
= isl_basic_set_finalize(bset
);
8403 isl_basic_set_free(bset
);
8407 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8413 set
= isl_set_align_divs(set
);
8418 set
= isl_set_cow(set
);
8422 n_div
= set
->p
[0]->n_div
;
8423 dim
= isl_set_get_space(set
);
8424 dim
= isl_space_lift(dim
, n_div
);
8427 isl_space_free(set
->dim
);
8430 for (i
= 0; i
< set
->n
; ++i
) {
8431 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8442 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8445 struct isl_basic_map
*bmap
;
8452 set
= isl_set_align_divs(set
);
8457 dim
= isl_set_get_space(set
);
8458 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8460 return isl_map_identity(isl_space_map_from_set(dim
));
8463 n_div
= set
->p
[0]->n_div
;
8464 dim
= isl_space_map_from_set(dim
);
8465 n_param
= isl_space_dim(dim
, isl_dim_param
);
8466 n_set
= isl_space_dim(dim
, isl_dim_in
);
8467 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8468 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8469 for (i
= 0; i
< n_set
; ++i
)
8470 bmap
= var_equal(bmap
, i
);
8472 total
= n_param
+ n_set
+ n_set
+ n_div
;
8473 for (i
= 0; i
< n_div
; ++i
) {
8474 k
= isl_basic_map_alloc_inequality(bmap
);
8477 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8478 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8479 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8480 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8481 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8482 set
->p
[0]->div
[i
][0]);
8484 l
= isl_basic_map_alloc_inequality(bmap
);
8487 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8488 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8489 set
->p
[0]->div
[i
][0]);
8490 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8494 bmap
= isl_basic_map_simplify(bmap
);
8495 bmap
= isl_basic_map_finalize(bmap
);
8496 return isl_map_from_basic_map(bmap
);
8499 isl_basic_map_free(bmap
);
8503 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8511 dim
= isl_basic_set_total_dim(bset
);
8512 size
+= bset
->n_eq
* (1 + dim
);
8513 size
+= bset
->n_ineq
* (1 + dim
);
8514 size
+= bset
->n_div
* (2 + dim
);
8519 int isl_set_size(__isl_keep isl_set
*set
)
8527 for (i
= 0; i
< set
->n
; ++i
)
8528 size
+= isl_basic_set_size(set
->p
[i
]);
8533 /* Check if there is any lower bound (if lower == 0) and/or upper
8534 * bound (if upper == 0) on the specified dim.
8536 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8537 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8544 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8546 pos
+= isl_basic_map_offset(bmap
, type
);
8548 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8549 if (isl_int_is_zero(bmap
->div
[i
][0]))
8551 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8555 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8556 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8559 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8560 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8567 return lower
&& upper
;
8570 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8571 enum isl_dim_type type
, unsigned pos
)
8573 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8576 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8577 enum isl_dim_type type
, unsigned pos
)
8579 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8582 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8583 enum isl_dim_type type
, unsigned pos
)
8585 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8588 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8589 enum isl_dim_type type
, unsigned pos
)
8596 for (i
= 0; i
< map
->n
; ++i
) {
8598 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8599 if (bounded
< 0 || !bounded
)
8606 /* Return 1 if the specified dim is involved in both an upper bound
8607 * and a lower bound.
8609 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8610 enum isl_dim_type type
, unsigned pos
)
8612 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8615 static int has_bound(__isl_keep isl_map
*map
,
8616 enum isl_dim_type type
, unsigned pos
,
8617 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8618 enum isl_dim_type type
, unsigned pos
))
8625 for (i
= 0; i
< map
->n
; ++i
) {
8627 bounded
= fn(map
->p
[i
], type
, pos
);
8628 if (bounded
< 0 || bounded
)
8635 /* Return 1 if the specified dim is involved in any lower bound.
8637 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8638 enum isl_dim_type type
, unsigned pos
)
8640 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8643 /* Return 1 if the specified dim is involved in any upper bound.
8645 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8646 enum isl_dim_type type
, unsigned pos
)
8648 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8651 /* For each of the "n" variables starting at "first", determine
8652 * the sign of the variable and put the results in the first "n"
8653 * elements of the array "signs".
8655 * 1 means that the variable is non-negative
8656 * -1 means that the variable is non-positive
8657 * 0 means the variable attains both positive and negative values.
8659 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8660 unsigned first
, unsigned n
, int *signs
)
8662 isl_vec
*bound
= NULL
;
8663 struct isl_tab
*tab
= NULL
;
8664 struct isl_tab_undo
*snap
;
8667 if (!bset
|| !signs
)
8670 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8671 tab
= isl_tab_from_basic_set(bset
);
8675 isl_seq_clr(bound
->el
, bound
->size
);
8676 isl_int_set_si(bound
->el
[0], -1);
8678 snap
= isl_tab_snap(tab
);
8679 for (i
= 0; i
< n
; ++i
) {
8682 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8683 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8686 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8687 if (isl_tab_rollback(tab
, snap
) < 0)
8695 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8696 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8699 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8700 if (isl_tab_rollback(tab
, snap
) < 0)
8703 signs
[i
] = empty
? -1 : 0;
8707 isl_vec_free(bound
);
8711 isl_vec_free(bound
);
8715 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8716 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8718 if (!bset
|| !signs
)
8720 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8723 first
+= pos(bset
->dim
, type
) - 1;
8724 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8727 /* Check if the given basic map is obviously single-valued.
8728 * In particular, for each output dimension, check that there is
8729 * an equality that defines the output dimension in terms of
8730 * earlier dimensions.
8732 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8742 total
= 1 + isl_basic_map_total_dim(bmap
);
8743 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8744 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8746 for (i
= 0; i
< n_out
; ++i
) {
8747 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8748 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8750 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8751 total
- (o_out
+ i
+ 1)) == -1)
8754 if (j
>= bmap
->n_eq
)
8761 /* Check if the given map is obviously single-valued.
8763 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8772 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8775 /* Check if the given map is single-valued.
8780 * and check if the result is a subset of the identity mapping.
8782 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8789 sv
= isl_map_plain_is_single_valued(map
);
8793 test
= isl_map_reverse(isl_map_copy(map
));
8794 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8796 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8797 id
= isl_map_identity(dim
);
8799 sv
= isl_map_is_subset(test
, id
);
8807 int isl_map_is_injective(__isl_keep isl_map
*map
)
8811 map
= isl_map_copy(map
);
8812 map
= isl_map_reverse(map
);
8813 in
= isl_map_is_single_valued(map
);
8819 /* Check if the given map is obviously injective.
8821 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8825 map
= isl_map_copy(map
);
8826 map
= isl_map_reverse(map
);
8827 in
= isl_map_plain_is_single_valued(map
);
8833 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8837 sv
= isl_map_is_single_valued(map
);
8841 return isl_map_is_injective(map
);
8844 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8846 return isl_map_is_single_valued((isl_map
*)set
);
8849 int isl_map_is_translation(__isl_keep isl_map
*map
)
8854 delta
= isl_map_deltas(isl_map_copy(map
));
8855 ok
= isl_set_is_singleton(delta
);
8856 isl_set_free(delta
);
8861 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8863 if (isl_seq_first_non_zero(p
, pos
) != -1)
8865 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8870 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8879 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8882 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8883 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8884 for (j
= 0; j
< nvar
; ++j
) {
8885 int lower
= 0, upper
= 0;
8886 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8887 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8889 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8895 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8896 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8898 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8900 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8905 if (!lower
|| !upper
)
8912 int isl_set_is_box(__isl_keep isl_set
*set
)
8919 return isl_basic_set_is_box(set
->p
[0]);
8922 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8927 return isl_space_is_wrapping(bset
->dim
);
8930 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8935 return isl_space_is_wrapping(set
->dim
);
8938 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8940 bmap
= isl_basic_map_cow(bmap
);
8944 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8948 bmap
= isl_basic_map_finalize(bmap
);
8950 return (isl_basic_set
*)bmap
;
8952 isl_basic_map_free(bmap
);
8956 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8960 map
= isl_map_cow(map
);
8964 for (i
= 0; i
< map
->n
; ++i
) {
8965 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8969 map
->dim
= isl_space_wrap(map
->dim
);
8973 return (isl_set
*)map
;
8979 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8981 bset
= isl_basic_set_cow(bset
);
8985 bset
->dim
= isl_space_unwrap(bset
->dim
);
8989 bset
= isl_basic_set_finalize(bset
);
8991 return (isl_basic_map
*)bset
;
8993 isl_basic_set_free(bset
);
8997 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9004 if (!isl_set_is_wrapping(set
))
9005 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9008 set
= isl_set_cow(set
);
9012 for (i
= 0; i
< set
->n
; ++i
) {
9013 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9018 set
->dim
= isl_space_unwrap(set
->dim
);
9022 return (isl_map
*)set
;
9028 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9029 enum isl_dim_type type
)
9034 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9037 bmap
= isl_basic_map_cow(bmap
);
9041 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9045 bmap
= isl_basic_map_finalize(bmap
);
9049 isl_basic_map_free(bmap
);
9053 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9054 enum isl_dim_type type
)
9061 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9064 map
= isl_map_cow(map
);
9068 for (i
= 0; i
< map
->n
; ++i
) {
9069 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9073 map
->dim
= isl_space_reset(map
->dim
, type
);
9083 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9088 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9091 bmap
= isl_basic_map_cow(bmap
);
9095 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9099 bmap
= isl_basic_map_finalize(bmap
);
9103 isl_basic_map_free(bmap
);
9107 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9109 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9112 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9113 __isl_take isl_basic_map
*bmap
)
9118 if (!bmap
->dim
->nested
[0])
9121 bmap
= isl_basic_map_cow(bmap
);
9125 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9129 bmap
= isl_basic_map_finalize(bmap
);
9133 isl_basic_map_free(bmap
);
9137 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9138 __isl_take isl_basic_map
*bmap
)
9143 if (!bmap
->dim
->nested
[1])
9146 bmap
= isl_basic_map_cow(bmap
);
9150 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9154 bmap
= isl_basic_map_finalize(bmap
);
9158 isl_basic_map_free(bmap
);
9162 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9169 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9172 map
= isl_map_cow(map
);
9176 for (i
= 0; i
< map
->n
; ++i
) {
9177 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9181 map
->dim
= isl_space_flatten(map
->dim
);
9191 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9193 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9196 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9198 isl_space
*dim
, *flat_dim
;
9201 dim
= isl_set_get_space(set
);
9202 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9203 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9204 map
= isl_map_intersect_domain(map
, set
);
9209 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9216 if (!map
->dim
->nested
[0])
9219 map
= isl_map_cow(map
);
9223 for (i
= 0; i
< map
->n
; ++i
) {
9224 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9228 map
->dim
= isl_space_flatten_domain(map
->dim
);
9238 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9245 if (!map
->dim
->nested
[1])
9248 map
= isl_map_cow(map
);
9252 for (i
= 0; i
< map
->n
; ++i
) {
9253 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9257 map
->dim
= isl_space_flatten_range(map
->dim
);
9267 /* Reorder the dimensions of "bmap" according to the given dim_map
9268 * and set the dimension specification to "dim".
9270 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9271 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9275 bmap
= isl_basic_map_cow(bmap
);
9276 if (!bmap
|| !dim
|| !dim_map
)
9279 res
= isl_basic_map_alloc_space(dim
,
9280 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9281 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9282 res
= isl_basic_map_finalize(res
);
9286 isl_basic_map_free(bmap
);
9287 isl_space_free(dim
);
9291 /* Reorder the dimensions of "map" according to given reordering.
9293 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9294 __isl_take isl_reordering
*r
)
9297 struct isl_dim_map
*dim_map
;
9299 map
= isl_map_cow(map
);
9300 dim_map
= isl_dim_map_from_reordering(r
);
9301 if (!map
|| !r
|| !dim_map
)
9304 for (i
= 0; i
< map
->n
; ++i
) {
9305 struct isl_dim_map
*dim_map_i
;
9307 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9309 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9310 isl_space_copy(r
->dim
), dim_map_i
);
9316 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9318 isl_reordering_free(r
);
9324 isl_reordering_free(r
);
9328 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9329 __isl_take isl_reordering
*r
)
9331 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9334 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9335 __isl_take isl_space
*model
)
9342 ctx
= isl_space_get_ctx(model
);
9343 if (!isl_space_has_named_params(model
))
9344 isl_die(ctx
, isl_error_invalid
,
9345 "model has unnamed parameters", goto error
);
9346 if (!isl_space_has_named_params(map
->dim
))
9347 isl_die(ctx
, isl_error_invalid
,
9348 "relation has unnamed parameters", goto error
);
9349 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9350 isl_reordering
*exp
;
9352 model
= isl_space_drop_dims(model
, isl_dim_in
,
9353 0, isl_space_dim(model
, isl_dim_in
));
9354 model
= isl_space_drop_dims(model
, isl_dim_out
,
9355 0, isl_space_dim(model
, isl_dim_out
));
9356 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9357 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9358 map
= isl_map_realign(map
, exp
);
9361 isl_space_free(model
);
9364 isl_space_free(model
);
9369 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9370 __isl_take isl_space
*model
)
9372 return isl_map_align_params(set
, model
);
9375 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9376 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9377 enum isl_dim_type c2
, enum isl_dim_type c3
,
9378 enum isl_dim_type c4
, enum isl_dim_type c5
)
9380 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9381 struct isl_mat
*mat
;
9387 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9388 isl_basic_map_total_dim(bmap
) + 1);
9391 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9392 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9393 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9394 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9395 isl_int_set(mat
->row
[i
][pos
],
9396 bmap
->eq
[i
][off
+ k
]);
9404 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9405 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9406 enum isl_dim_type c2
, enum isl_dim_type c3
,
9407 enum isl_dim_type c4
, enum isl_dim_type c5
)
9409 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9410 struct isl_mat
*mat
;
9416 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9417 isl_basic_map_total_dim(bmap
) + 1);
9420 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9421 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9422 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9423 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9424 isl_int_set(mat
->row
[i
][pos
],
9425 bmap
->ineq
[i
][off
+ k
]);
9433 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9434 __isl_take isl_space
*dim
,
9435 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9436 enum isl_dim_type c2
, enum isl_dim_type c3
,
9437 enum isl_dim_type c4
, enum isl_dim_type c5
)
9439 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9440 isl_basic_map
*bmap
;
9446 if (!dim
|| !eq
|| !ineq
)
9449 if (eq
->n_col
!= ineq
->n_col
)
9450 isl_die(dim
->ctx
, isl_error_invalid
,
9451 "equalities and inequalities matrices should have "
9452 "same number of columns", goto error
);
9454 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9456 if (eq
->n_col
< total
)
9457 isl_die(dim
->ctx
, isl_error_invalid
,
9458 "number of columns too small", goto error
);
9460 extra
= eq
->n_col
- total
;
9462 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9463 eq
->n_row
, ineq
->n_row
);
9466 for (i
= 0; i
< extra
; ++i
) {
9467 k
= isl_basic_map_alloc_div(bmap
);
9470 isl_int_set_si(bmap
->div
[k
][0], 0);
9472 for (i
= 0; i
< eq
->n_row
; ++i
) {
9473 l
= isl_basic_map_alloc_equality(bmap
);
9476 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9477 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9478 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9479 isl_int_set(bmap
->eq
[l
][off
+ k
],
9485 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9486 l
= isl_basic_map_alloc_inequality(bmap
);
9489 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9490 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9491 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9492 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9499 isl_space_free(dim
);
9505 isl_space_free(dim
);
9511 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9512 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9513 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9515 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9516 c1
, c2
, c3
, c4
, isl_dim_in
);
9519 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9520 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9521 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9523 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9524 c1
, c2
, c3
, c4
, isl_dim_in
);
9527 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9528 __isl_take isl_space
*dim
,
9529 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9530 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9532 return (isl_basic_set
*)
9533 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9534 c1
, c2
, c3
, c4
, isl_dim_in
);
9537 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9542 return isl_space_can_zip(bmap
->dim
);
9545 int isl_map_can_zip(__isl_keep isl_map
*map
)
9550 return isl_space_can_zip(map
->dim
);
9553 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9554 * (A -> C) -> (B -> D).
9556 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9565 if (!isl_basic_map_can_zip(bmap
))
9566 isl_die(bmap
->ctx
, isl_error_invalid
,
9567 "basic map cannot be zipped", goto error
);
9568 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9569 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9570 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9571 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9572 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9575 bmap
->dim
= isl_space_zip(bmap
->dim
);
9580 isl_basic_map_free(bmap
);
9584 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9585 * (A -> C) -> (B -> D).
9587 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9594 if (!isl_map_can_zip(map
))
9595 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9598 map
= isl_map_cow(map
);
9602 for (i
= 0; i
< map
->n
; ++i
) {
9603 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9608 map
->dim
= isl_space_zip(map
->dim
);
9618 /* Construct a basic map mapping the domain of the affine expression
9619 * to a one-dimensional range prescribed by the affine expression.
9621 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9625 isl_local_space
*ls
;
9626 isl_basic_map
*bmap
;
9631 ls
= isl_aff_get_local_space(aff
);
9632 bmap
= isl_basic_map_from_local_space(ls
);
9633 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9634 k
= isl_basic_map_alloc_equality(bmap
);
9638 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9639 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9640 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9641 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9642 aff
->v
->size
- (pos
+ 1));
9645 bmap
= isl_basic_map_finalize(bmap
);
9649 isl_basic_map_free(bmap
);
9653 /* Construct a basic map mapping the domain the multi-affine expression
9654 * to its range, with each dimension in the range equated to the
9655 * corresponding affine expression.
9657 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9658 __isl_take isl_multi_aff
*maff
)
9662 isl_basic_map
*bmap
;
9667 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9668 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9669 "invalid space", return isl_multi_aff_free(maff
));
9671 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9672 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9674 for (i
= 0; i
< maff
->n
; ++i
) {
9676 isl_basic_map
*bmap_i
;
9678 aff
= isl_aff_copy(maff
->p
[i
]);
9679 bmap_i
= isl_basic_map_from_aff(aff
);
9681 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9684 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9686 isl_multi_aff_free(maff
);
9690 /* Construct a basic map mapping a domain in the given space to
9691 * to an n-dimensional range, with n the number of elements in the list,
9692 * where each coordinate in the range is prescribed by the
9693 * corresponding affine expression.
9694 * The domains of all affine expressions in the list are assumed to match
9697 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9698 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9702 isl_basic_map
*bmap
;
9707 dim
= isl_space_from_domain(domain_dim
);
9708 bmap
= isl_basic_map_universe(dim
);
9710 for (i
= 0; i
< list
->n
; ++i
) {
9712 isl_basic_map
*bmap_i
;
9714 aff
= isl_aff_copy(list
->p
[i
]);
9715 bmap_i
= isl_basic_map_from_aff(aff
);
9717 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9720 isl_aff_list_free(list
);
9724 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9725 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9727 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9730 /* Add a constraint imposing that the given two dimensions are equal.
9732 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9733 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9735 isl_basic_map
*bmap
= NULL
;
9741 if (pos1
>= isl_map_dim(map
, type1
))
9742 isl_die(map
->ctx
, isl_error_invalid
,
9743 "index out of bounds", goto error
);
9744 if (pos2
>= isl_map_dim(map
, type2
))
9745 isl_die(map
->ctx
, isl_error_invalid
,
9746 "index out of bounds", goto error
);
9748 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9749 i
= isl_basic_map_alloc_equality(bmap
);
9752 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9753 pos1
+= isl_basic_map_offset(bmap
, type1
);
9754 pos2
+= isl_basic_map_offset(bmap
, type2
);
9755 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9756 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9757 bmap
= isl_basic_map_finalize(bmap
);
9759 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9763 isl_basic_map_free(bmap
);
9768 /* Add a constraint imposing that the given two dimensions have opposite values.
9770 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9771 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9773 isl_basic_map
*bmap
= NULL
;
9779 if (pos1
>= isl_map_dim(map
, type1
))
9780 isl_die(map
->ctx
, isl_error_invalid
,
9781 "index out of bounds", goto error
);
9782 if (pos2
>= isl_map_dim(map
, type2
))
9783 isl_die(map
->ctx
, isl_error_invalid
,
9784 "index out of bounds", goto error
);
9786 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9787 i
= isl_basic_map_alloc_equality(bmap
);
9790 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9791 pos1
+= isl_basic_map_offset(bmap
, type1
);
9792 pos2
+= isl_basic_map_offset(bmap
, type2
);
9793 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9794 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9795 bmap
= isl_basic_map_finalize(bmap
);
9797 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9801 isl_basic_map_free(bmap
);
9806 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9810 isl_local_space
*ls
;
9815 if (!isl_basic_map_divs_known(bmap
))
9816 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9817 "some divs are unknown", return NULL
);
9819 ls
= isl_basic_map_get_local_space(bmap
);
9820 div
= isl_local_space_get_div(ls
, pos
);
9821 isl_local_space_free(ls
);
9826 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9829 return isl_basic_map_get_div(bset
, pos
);
9832 /* Plug in "subs" for dimension "type", "pos" of "bset".
9834 * Let i be the dimension to replace and let "subs" be of the form
9838 * Any integer division with a non-zero coefficient for i,
9840 * floor((a i + g)/m)
9844 * floor((a f + d g)/(m d))
9846 * Constraints of the form
9854 __isl_give isl_basic_set
*isl_basic_set_substitute(
9855 __isl_take isl_basic_set
*bset
,
9856 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9862 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9865 bset
= isl_basic_set_cow(bset
);
9869 ctx
= isl_basic_set_get_ctx(bset
);
9870 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9871 isl_die(ctx
, isl_error_invalid
,
9872 "spaces don't match", goto error
);
9873 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9874 isl_die(ctx
, isl_error_unsupported
,
9875 "cannot handle divs yet", goto error
);
9877 pos
+= isl_basic_set_offset(bset
, type
);
9881 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9882 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9884 isl_int_set(v
, bset
->eq
[i
][pos
]);
9885 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9886 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9887 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9890 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9891 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9893 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9894 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9895 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9896 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9899 for (i
= 0; i
< bset
->n_div
; ++i
) {
9900 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9902 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9903 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9904 isl_seq_combine(bset
->div
[i
] + 1,
9905 subs
->v
->el
[0], bset
->div
[i
] + 1,
9906 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9907 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9912 bset
= isl_basic_set_simplify(bset
);
9913 return isl_basic_set_finalize(bset
);
9915 isl_basic_set_free(bset
);
9919 /* Plug in "subs" for dimension "type", "pos" of "set".
9921 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9922 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9926 if (set
&& isl_set_plain_is_empty(set
))
9929 set
= isl_set_cow(set
);
9933 for (i
= set
->n
- 1; i
>= 0; --i
) {
9934 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9937 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
9938 isl_basic_set_free(set
->p
[i
]);
9939 if (i
!= set
->n
- 1)
9940 set
->p
[i
] = set
->p
[set
->n
- 1];