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
);
1563 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1564 first
+= pos(map
->dim
, type
) - 1;
1566 for (i
= 0; i
< map
->n
; ++i
) {
1567 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1577 /* Eliminate the specified n dimensions starting at first from the
1578 * constraints using Fourier-Motzkin. The dimensions themselves
1581 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1582 enum isl_dim_type type
, unsigned first
, unsigned n
)
1584 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1587 /* Eliminate the specified n dimensions starting at first from the
1588 * constraints using Fourier-Motzkin. The dimensions themselves
1591 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1592 unsigned first
, unsigned n
)
1594 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1597 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1598 __isl_take isl_basic_map
*bmap
)
1602 bmap
= isl_basic_map_eliminate_vars(bmap
,
1603 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1607 return isl_basic_map_finalize(bmap
);
1610 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1611 __isl_take isl_basic_set
*bset
)
1613 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1614 (struct isl_basic_map
*)bset
);
1617 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1626 map
= isl_map_cow(map
);
1630 for (i
= 0; i
< map
->n
; ++i
) {
1631 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1641 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1643 return isl_map_remove_divs(set
);
1646 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1647 enum isl_dim_type type
, unsigned first
, unsigned n
)
1651 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1653 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1655 bmap
= isl_basic_map_eliminate_vars(bmap
,
1656 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1659 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1661 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1664 isl_basic_map_free(bmap
);
1668 /* Return true if the definition of the given div (recursively) involves
1669 * any of the given variables.
1671 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1672 unsigned first
, unsigned n
)
1675 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1677 if (isl_int_is_zero(bmap
->div
[div
][0]))
1679 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1682 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1683 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1685 if (div_involves_vars(bmap
, i
, first
, n
))
1692 /* Remove all divs (recursively) involving any of the given dimensions
1693 * in their definitions.
1695 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1696 __isl_take isl_basic_map
*bmap
,
1697 enum isl_dim_type type
, unsigned first
, unsigned n
)
1703 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1705 first
+= isl_basic_map_offset(bmap
, type
);
1707 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1708 if (!div_involves_vars(bmap
, i
, first
, n
))
1710 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1718 isl_basic_map_free(bmap
);
1722 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1723 enum isl_dim_type type
, unsigned first
, unsigned n
)
1732 map
= isl_map_cow(map
);
1736 for (i
= 0; i
< map
->n
; ++i
) {
1737 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1748 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1749 enum isl_dim_type type
, unsigned first
, unsigned n
)
1751 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1755 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1756 enum isl_dim_type type
, unsigned first
, unsigned n
)
1763 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1764 isl_die(bmap
->ctx
, isl_error_invalid
,
1765 "index out of bounds", return -1);
1767 first
+= isl_basic_map_offset(bmap
, type
);
1768 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1769 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1771 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1772 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1778 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1779 enum isl_dim_type type
, unsigned first
, unsigned n
)
1786 if (first
+ n
> isl_map_dim(map
, type
))
1787 isl_die(map
->ctx
, isl_error_invalid
,
1788 "index out of bounds", return -1);
1790 for (i
= 0; i
< map
->n
; ++i
) {
1791 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1793 if (involves
< 0 || involves
)
1800 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1801 enum isl_dim_type type
, unsigned first
, unsigned n
)
1803 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1806 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1807 enum isl_dim_type type
, unsigned first
, unsigned n
)
1809 return isl_map_involves_dims(set
, type
, first
, n
);
1812 /* Return true if the definition of the given div is unknown or depends
1815 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1818 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1820 if (isl_int_is_zero(bmap
->div
[div
][0]))
1823 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1824 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1826 if (div_is_unknown(bmap
, i
))
1833 /* Remove all divs that are unknown or defined in terms of unknown divs.
1835 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1836 __isl_take isl_basic_map
*bmap
)
1843 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1844 if (!div_is_unknown(bmap
, i
))
1846 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1852 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1861 map
= isl_map_cow(map
);
1865 for (i
= 0; i
< map
->n
; ++i
) {
1866 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1876 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1878 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1881 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1882 __isl_take isl_basic_set
*bset
,
1883 enum isl_dim_type type
, unsigned first
, unsigned n
)
1885 return (isl_basic_set
*)
1886 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1889 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1890 enum isl_dim_type type
, unsigned first
, unsigned n
)
1897 map
= isl_map_cow(map
);
1900 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1902 for (i
= 0; i
< map
->n
; ++i
) {
1903 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1904 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1908 map
= isl_map_drop(map
, type
, first
, n
);
1915 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1916 enum isl_dim_type type
, unsigned first
, unsigned n
)
1918 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1921 /* Project out n inputs starting at first using Fourier-Motzkin */
1922 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1923 unsigned first
, unsigned n
)
1925 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1928 static void dump_term(struct isl_basic_map
*bmap
,
1929 isl_int c
, int pos
, FILE *out
)
1932 unsigned in
= isl_basic_map_n_in(bmap
);
1933 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1934 unsigned nparam
= isl_basic_map_n_param(bmap
);
1936 isl_int_print(out
, c
, 0);
1938 if (!isl_int_is_one(c
))
1939 isl_int_print(out
, c
, 0);
1940 if (pos
< 1 + nparam
) {
1941 name
= isl_space_get_dim_name(bmap
->dim
,
1942 isl_dim_param
, pos
- 1);
1944 fprintf(out
, "%s", name
);
1946 fprintf(out
, "p%d", pos
- 1);
1947 } else if (pos
< 1 + nparam
+ in
)
1948 fprintf(out
, "i%d", pos
- 1 - nparam
);
1949 else if (pos
< 1 + nparam
+ dim
)
1950 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1952 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1956 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1957 int sign
, FILE *out
)
1961 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1965 for (i
= 0, first
= 1; i
< len
; ++i
) {
1966 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1969 fprintf(out
, " + ");
1971 isl_int_abs(v
, c
[i
]);
1972 dump_term(bmap
, v
, i
, out
);
1979 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1980 const char *op
, FILE *out
, int indent
)
1984 fprintf(out
, "%*s", indent
, "");
1986 dump_constraint_sign(bmap
, c
, 1, out
);
1987 fprintf(out
, " %s ", op
);
1988 dump_constraint_sign(bmap
, c
, -1, out
);
1992 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1993 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
1995 fprintf(out
, "%*s", indent
, "");
1996 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2001 static void dump_constraints(struct isl_basic_map
*bmap
,
2002 isl_int
**c
, unsigned n
,
2003 const char *op
, FILE *out
, int indent
)
2007 for (i
= 0; i
< n
; ++i
)
2008 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2011 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2015 unsigned total
= isl_basic_map_total_dim(bmap
);
2017 for (j
= 0; j
< 1 + total
; ++j
) {
2018 if (isl_int_is_zero(exp
[j
]))
2020 if (!first
&& isl_int_is_pos(exp
[j
]))
2022 dump_term(bmap
, exp
[j
], j
, out
);
2027 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2031 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2032 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2034 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2035 fprintf(out
, "%*s", indent
, "");
2036 fprintf(out
, "e%d = [(", i
);
2037 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2039 isl_int_print(out
, bmap
->div
[i
][0], 0);
2040 fprintf(out
, "]\n");
2044 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2045 FILE *out
, int indent
)
2048 fprintf(out
, "null basic set\n");
2052 fprintf(out
, "%*s", indent
, "");
2053 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2054 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2055 bset
->extra
, bset
->flags
);
2056 dump((struct isl_basic_map
*)bset
, out
, indent
);
2059 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2060 FILE *out
, int indent
)
2063 fprintf(out
, "null basic map\n");
2067 fprintf(out
, "%*s", indent
, "");
2068 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2069 "flags: %x, n_name: %d\n",
2071 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2072 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2073 dump(bmap
, out
, indent
);
2076 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2081 total
= isl_basic_map_total_dim(bmap
);
2082 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2083 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2084 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2085 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2089 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2092 struct isl_set
*set
;
2096 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2097 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2098 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2099 sizeof(struct isl_set
) +
2100 (n
- 1) * sizeof(struct isl_basic_set
*));
2104 set
->ctx
= dim
->ctx
;
2105 isl_ctx_ref(set
->ctx
);
2113 isl_space_free(dim
);
2117 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2118 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2120 struct isl_set
*set
;
2123 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2127 set
= isl_set_alloc_space(dims
, n
, flags
);
2131 /* Make sure "map" has room for at least "n" more basic maps.
2133 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2136 struct isl_map
*grown
= NULL
;
2140 isl_assert(map
->ctx
, n
>= 0, goto error
);
2141 if (map
->n
+ n
<= map
->size
)
2143 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2146 for (i
= 0; i
< map
->n
; ++i
) {
2147 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2155 isl_map_free(grown
);
2160 /* Make sure "set" has room for at least "n" more basic sets.
2162 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2164 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2167 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2170 struct isl_set
*dup
;
2175 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2178 for (i
= 0; i
< set
->n
; ++i
)
2179 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2183 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2185 return isl_map_from_basic_map(bset
);
2188 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2190 struct isl_map
*map
;
2195 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2196 return isl_map_add_basic_map(map
, bmap
);
2199 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2200 __isl_take isl_basic_set
*bset
)
2202 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2203 (struct isl_basic_map
*)bset
);
2206 void isl_set_free(struct isl_set
*set
)
2216 isl_ctx_deref(set
->ctx
);
2217 for (i
= 0; i
< set
->n
; ++i
)
2218 isl_basic_set_free(set
->p
[i
]);
2219 isl_space_free(set
->dim
);
2223 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2228 fprintf(out
, "null set\n");
2232 fprintf(out
, "%*s", indent
, "");
2233 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2234 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2236 for (i
= 0; i
< set
->n
; ++i
) {
2237 fprintf(out
, "%*s", indent
, "");
2238 fprintf(out
, "basic set %d:\n", i
);
2239 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2243 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2248 fprintf(out
, "null map\n");
2252 fprintf(out
, "%*s", indent
, "");
2253 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2254 "flags: %x, n_name: %d\n",
2255 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2256 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2257 for (i
= 0; i
< map
->n
; ++i
) {
2258 fprintf(out
, "%*s", indent
, "");
2259 fprintf(out
, "basic map %d:\n", i
);
2260 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2264 struct isl_basic_map
*isl_basic_map_intersect_domain(
2265 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2267 struct isl_basic_map
*bmap_domain
;
2272 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2273 bset
->dim
, isl_dim_param
), goto error
);
2275 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2276 isl_assert(bset
->ctx
,
2277 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2279 bmap
= isl_basic_map_cow(bmap
);
2282 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2283 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2284 bmap_domain
= isl_basic_map_from_domain(bset
);
2285 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2287 bmap
= isl_basic_map_simplify(bmap
);
2288 return isl_basic_map_finalize(bmap
);
2290 isl_basic_map_free(bmap
);
2291 isl_basic_set_free(bset
);
2295 struct isl_basic_map
*isl_basic_map_intersect_range(
2296 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2298 struct isl_basic_map
*bmap_range
;
2303 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2304 bset
->dim
, isl_dim_param
), goto error
);
2306 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2307 isl_assert(bset
->ctx
,
2308 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2310 if (isl_basic_set_is_universe(bset
)) {
2311 isl_basic_set_free(bset
);
2315 bmap
= isl_basic_map_cow(bmap
);
2318 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2319 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2320 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2321 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2323 bmap
= isl_basic_map_simplify(bmap
);
2324 return isl_basic_map_finalize(bmap
);
2326 isl_basic_map_free(bmap
);
2327 isl_basic_set_free(bset
);
2331 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2337 total
= 1 + isl_basic_map_total_dim(bmap
);
2338 if (total
!= vec
->size
)
2343 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2344 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2345 if (!isl_int_is_zero(s
)) {
2351 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2352 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2353 if (isl_int_is_neg(s
)) {
2364 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2366 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2369 struct isl_basic_map
*isl_basic_map_intersect(
2370 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2372 struct isl_vec
*sample
= NULL
;
2374 if (!bmap1
|| !bmap2
)
2377 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2378 bmap2
->dim
, isl_dim_param
), goto error
);
2379 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2380 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2381 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2382 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2383 return isl_basic_map_intersect(bmap2
, bmap1
);
2385 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2386 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2387 isl_assert(bmap1
->ctx
,
2388 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2390 if (bmap1
->sample
&&
2391 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2392 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2393 sample
= isl_vec_copy(bmap1
->sample
);
2394 else if (bmap2
->sample
&&
2395 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2396 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2397 sample
= isl_vec_copy(bmap2
->sample
);
2399 bmap1
= isl_basic_map_cow(bmap1
);
2402 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2403 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2404 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2407 isl_vec_free(sample
);
2409 isl_vec_free(bmap1
->sample
);
2410 bmap1
->sample
= sample
;
2413 bmap1
= isl_basic_map_simplify(bmap1
);
2414 return isl_basic_map_finalize(bmap1
);
2417 isl_vec_free(sample
);
2418 isl_basic_map_free(bmap1
);
2419 isl_basic_map_free(bmap2
);
2423 struct isl_basic_set
*isl_basic_set_intersect(
2424 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2426 return (struct isl_basic_set
*)
2427 isl_basic_map_intersect(
2428 (struct isl_basic_map
*)bset1
,
2429 (struct isl_basic_map
*)bset2
);
2432 /* Special case of isl_map_intersect, where both map1 and map2
2433 * are convex, without any divs and such that either map1 or map2
2434 * contains a single constraint. This constraint is then simply
2435 * added to the other map.
2437 static __isl_give isl_map
*map_intersect_add_constraint(
2438 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2440 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2441 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2442 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2443 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2445 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2446 return isl_map_intersect(map2
, map1
);
2448 isl_assert(map2
->ctx
,
2449 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2451 map1
= isl_map_cow(map1
);
2454 if (isl_map_plain_is_empty(map1
)) {
2458 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2459 if (map2
->p
[0]->n_eq
== 1)
2460 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2462 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2463 map2
->p
[0]->ineq
[0]);
2465 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2466 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2470 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2471 isl_basic_map_free(map1
->p
[0]);
2484 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2485 __isl_take isl_map
*map2
)
2488 struct isl_map
*result
;
2494 if (isl_map_plain_is_empty(map1
) &&
2495 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2499 if (isl_map_plain_is_empty(map2
) &&
2500 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2505 if (map1
->n
== 1 && map2
->n
== 1 &&
2506 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2507 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2508 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2509 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2510 return map_intersect_add_constraint(map1
, map2
);
2511 if (isl_map_is_params(map1
) && !isl_map_is_params(map2
))
2512 return isl_map_intersect(map2
, map1
);
2513 if (isl_space_dim(map1
->dim
, isl_dim_all
) ==
2514 isl_space_dim(map1
->dim
, isl_dim_param
) &&
2515 isl_space_dim(map2
->dim
, isl_dim_all
) !=
2516 isl_space_dim(map2
->dim
, isl_dim_param
))
2517 return isl_map_intersect(map2
, map1
);
2519 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2520 isl_space_dim(map2
->dim
, isl_dim_param
))
2521 isl_assert(map1
->ctx
,
2522 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2524 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2525 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2526 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2528 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2529 map1
->n
* map2
->n
, flags
);
2532 for (i
= 0; i
< map1
->n
; ++i
)
2533 for (j
= 0; j
< map2
->n
; ++j
) {
2534 struct isl_basic_map
*part
;
2535 part
= isl_basic_map_intersect(
2536 isl_basic_map_copy(map1
->p
[i
]),
2537 isl_basic_map_copy(map2
->p
[j
]));
2538 if (isl_basic_map_is_empty(part
))
2539 isl_basic_map_free(part
);
2541 result
= isl_map_add_basic_map(result
, part
);
2554 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2555 __isl_take isl_map
*map2
)
2557 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2560 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2562 return (struct isl_set
*)
2563 isl_map_intersect((struct isl_map
*)set1
,
2564 (struct isl_map
*)set2
);
2567 /* The current implementation of isl_map_intersect accepts intersections
2568 * with parameter domains, so we can just call that for now.
2570 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2571 __isl_take isl_set
*params
)
2573 return isl_map_intersect(map
, params
);
2576 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2577 __isl_take isl_map
*map2
)
2579 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2582 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2583 __isl_take isl_set
*params
)
2585 return isl_map_intersect_params(set
, params
);
2588 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2591 struct isl_basic_set
*bset
;
2596 bmap
= isl_basic_map_cow(bmap
);
2599 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2600 in
= isl_basic_map_n_in(bmap
);
2601 bset
= isl_basic_set_from_basic_map(bmap
);
2602 bset
= isl_basic_set_swap_vars(bset
, in
);
2603 return isl_basic_map_from_basic_set(bset
, dim
);
2606 static __isl_give isl_basic_map
*basic_map_space_reset(
2607 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2611 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2614 space
= isl_basic_map_get_space(bmap
);
2615 space
= isl_space_reset(space
, type
);
2616 bmap
= isl_basic_map_reset_space(bmap
, space
);
2620 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2621 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2624 struct isl_basic_map
*res
;
2625 struct isl_dim_map
*dim_map
;
2626 unsigned total
, off
;
2627 enum isl_dim_type t
;
2630 return basic_map_space_reset(bmap
, type
);
2635 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2637 total
= isl_basic_map_total_dim(bmap
) + n
;
2638 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2640 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2642 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2644 unsigned size
= isl_basic_map_dim(bmap
, t
);
2645 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2647 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2648 pos
, size
- pos
, off
+ pos
+ n
);
2650 off
+= isl_space_dim(res_dim
, t
);
2652 isl_dim_map_div(dim_map
, bmap
, off
);
2654 res
= isl_basic_map_alloc_space(res_dim
,
2655 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2656 if (isl_basic_map_is_rational(bmap
))
2657 res
= isl_basic_map_set_rational(res
);
2658 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2659 return isl_basic_map_finalize(res
);
2662 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2663 enum isl_dim_type type
, unsigned n
)
2667 return isl_basic_map_insert(bmap
, type
,
2668 isl_basic_map_dim(bmap
, type
), n
);
2671 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2672 enum isl_dim_type type
, unsigned n
)
2676 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2677 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2679 isl_basic_set_free(bset
);
2683 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2684 enum isl_dim_type type
)
2688 if (!isl_space_is_named_or_nested(map
->dim
, type
))
2691 space
= isl_map_get_space(map
);
2692 space
= isl_space_reset(space
, type
);
2693 map
= isl_map_reset_space(map
, space
);
2697 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2698 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2703 return map_space_reset(map
, type
);
2705 map
= isl_map_cow(map
);
2709 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2713 for (i
= 0; i
< map
->n
; ++i
) {
2714 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2725 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2726 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2728 return isl_map_insert_dims(set
, type
, pos
, n
);
2731 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2732 enum isl_dim_type type
, unsigned n
)
2736 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2739 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2740 enum isl_dim_type type
, unsigned n
)
2744 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2745 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2751 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2752 __isl_take isl_basic_map
*bmap
,
2753 enum isl_dim_type dst_type
, unsigned dst_pos
,
2754 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2756 struct isl_dim_map
*dim_map
;
2757 struct isl_basic_map
*res
;
2758 enum isl_dim_type t
;
2759 unsigned total
, off
;
2766 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2769 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2772 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2774 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2775 pos(bmap
->dim
, src_type
) + src_pos
+
2776 ((src_type
< dst_type
) ? n
: 0)) {
2777 bmap
= isl_basic_map_cow(bmap
);
2781 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2782 src_type
, src_pos
, n
);
2786 bmap
= isl_basic_map_finalize(bmap
);
2791 total
= isl_basic_map_total_dim(bmap
);
2792 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2795 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2796 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2797 if (t
== dst_type
) {
2798 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2801 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2804 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2805 dst_pos
, size
- dst_pos
, off
);
2806 off
+= size
- dst_pos
;
2807 } else if (t
== src_type
) {
2808 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2811 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2812 src_pos
+ n
, size
- src_pos
- n
, off
);
2813 off
+= size
- src_pos
- n
;
2815 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2819 isl_dim_map_div(dim_map
, bmap
, off
);
2821 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2822 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2823 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2825 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2826 src_type
, src_pos
, n
);
2830 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2831 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2832 bmap
= isl_basic_map_finalize(bmap
);
2836 isl_basic_map_free(bmap
);
2840 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2841 enum isl_dim_type dst_type
, unsigned dst_pos
,
2842 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2844 return (isl_basic_set
*)isl_basic_map_move_dims(
2845 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2848 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2849 enum isl_dim_type dst_type
, unsigned dst_pos
,
2850 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2854 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2855 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2856 src_type
, src_pos
, n
);
2862 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2863 enum isl_dim_type dst_type
, unsigned dst_pos
,
2864 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2873 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2876 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2879 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2881 map
= isl_map_cow(map
);
2885 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2889 for (i
= 0; i
< map
->n
; ++i
) {
2890 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2892 src_type
, src_pos
, n
);
2903 /* Move the specified dimensions to the last columns right before
2904 * the divs. Don't change the dimension specification of bmap.
2905 * That's the responsibility of the caller.
2907 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2908 enum isl_dim_type type
, unsigned first
, unsigned n
)
2910 struct isl_dim_map
*dim_map
;
2911 struct isl_basic_map
*res
;
2912 enum isl_dim_type t
;
2913 unsigned total
, off
;
2917 if (pos(bmap
->dim
, type
) + first
+ n
==
2918 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2921 total
= isl_basic_map_total_dim(bmap
);
2922 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2925 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2926 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2928 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2931 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2932 first
, n
, total
- bmap
->n_div
- n
);
2933 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2934 first
+ n
, size
- (first
+ n
), off
);
2935 off
+= size
- (first
+ n
);
2937 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2941 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2943 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2944 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2945 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2949 /* Turn the n dimensions of type type, starting at first
2950 * into existentially quantified variables.
2952 __isl_give isl_basic_map
*isl_basic_map_project_out(
2953 __isl_take isl_basic_map
*bmap
,
2954 enum isl_dim_type type
, unsigned first
, unsigned n
)
2967 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2968 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2970 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2973 bmap
= move_last(bmap
, type
, first
, n
);
2974 bmap
= isl_basic_map_cow(bmap
);
2978 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2979 old
= bmap
->block2
.data
;
2980 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2981 (bmap
->extra
+ n
) * (1 + row_size
));
2982 if (!bmap
->block2
.data
)
2984 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2987 for (i
= 0; i
< n
; ++i
) {
2988 new_div
[i
] = bmap
->block2
.data
+
2989 (bmap
->extra
+ i
) * (1 + row_size
);
2990 isl_seq_clr(new_div
[i
], 1 + row_size
);
2992 for (i
= 0; i
< bmap
->extra
; ++i
)
2993 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2995 bmap
->div
= new_div
;
2999 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3002 bmap
= isl_basic_map_simplify(bmap
);
3003 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3004 return isl_basic_map_finalize(bmap
);
3006 isl_basic_map_free(bmap
);
3010 /* Turn the n dimensions of type type, starting at first
3011 * into existentially quantified variables.
3013 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3014 enum isl_dim_type type
, unsigned first
, unsigned n
)
3016 return (isl_basic_set
*)isl_basic_map_project_out(
3017 (isl_basic_map
*)bset
, type
, first
, n
);
3020 /* Turn the n dimensions of type type, starting at first
3021 * into existentially quantified variables.
3023 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3024 enum isl_dim_type type
, unsigned first
, unsigned n
)
3034 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3036 map
= isl_map_cow(map
);
3040 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3044 for (i
= 0; i
< map
->n
; ++i
) {
3045 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3056 /* Turn the n dimensions of type type, starting at first
3057 * into existentially quantified variables.
3059 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3060 enum isl_dim_type type
, unsigned first
, unsigned n
)
3062 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3065 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3069 for (i
= 0; i
< n
; ++i
) {
3070 j
= isl_basic_map_alloc_div(bmap
);
3073 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3077 isl_basic_map_free(bmap
);
3081 struct isl_basic_map
*isl_basic_map_apply_range(
3082 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3084 isl_space
*dim_result
= NULL
;
3085 struct isl_basic_map
*bmap
;
3086 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3087 struct isl_dim_map
*dim_map1
, *dim_map2
;
3089 if (!bmap1
|| !bmap2
)
3092 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3093 isl_space_copy(bmap2
->dim
));
3095 n_in
= isl_basic_map_n_in(bmap1
);
3096 n_out
= isl_basic_map_n_out(bmap2
);
3097 n
= isl_basic_map_n_out(bmap1
);
3098 nparam
= isl_basic_map_n_param(bmap1
);
3100 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3101 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3102 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3103 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3104 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3105 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3106 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3107 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3108 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3109 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3110 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3112 bmap
= isl_basic_map_alloc_space(dim_result
,
3113 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3114 bmap1
->n_eq
+ bmap2
->n_eq
,
3115 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3116 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3117 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3118 bmap
= add_divs(bmap
, n
);
3119 bmap
= isl_basic_map_simplify(bmap
);
3120 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3121 return isl_basic_map_finalize(bmap
);
3123 isl_basic_map_free(bmap1
);
3124 isl_basic_map_free(bmap2
);
3128 struct isl_basic_set
*isl_basic_set_apply(
3129 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3134 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3137 return (struct isl_basic_set
*)
3138 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3140 isl_basic_set_free(bset
);
3141 isl_basic_map_free(bmap
);
3145 struct isl_basic_map
*isl_basic_map_apply_domain(
3146 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3148 if (!bmap1
|| !bmap2
)
3151 isl_assert(bmap1
->ctx
,
3152 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3153 isl_assert(bmap1
->ctx
,
3154 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3157 bmap1
= isl_basic_map_reverse(bmap1
);
3158 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3159 return isl_basic_map_reverse(bmap1
);
3161 isl_basic_map_free(bmap1
);
3162 isl_basic_map_free(bmap2
);
3166 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3167 * A \cap B -> f(A) + f(B)
3169 struct isl_basic_map
*isl_basic_map_sum(
3170 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3172 unsigned n_in
, n_out
, nparam
, total
, pos
;
3173 struct isl_basic_map
*bmap
= NULL
;
3174 struct isl_dim_map
*dim_map1
, *dim_map2
;
3177 if (!bmap1
|| !bmap2
)
3180 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3183 nparam
= isl_basic_map_n_param(bmap1
);
3184 n_in
= isl_basic_map_n_in(bmap1
);
3185 n_out
= isl_basic_map_n_out(bmap1
);
3187 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3188 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3189 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3190 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3191 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3192 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3193 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3194 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3195 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3196 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3197 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3199 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3200 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3201 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3202 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3203 for (i
= 0; i
< n_out
; ++i
) {
3204 int j
= isl_basic_map_alloc_equality(bmap
);
3207 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3208 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3209 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3210 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3212 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3213 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3214 bmap
= add_divs(bmap
, 2 * n_out
);
3216 bmap
= isl_basic_map_simplify(bmap
);
3217 return isl_basic_map_finalize(bmap
);
3219 isl_basic_map_free(bmap
);
3220 isl_basic_map_free(bmap1
);
3221 isl_basic_map_free(bmap2
);
3225 /* Given two maps A -> f(A) and B -> g(B), construct a map
3226 * A \cap B -> f(A) + f(B)
3228 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3230 struct isl_map
*result
;
3236 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3238 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3239 map1
->n
* map2
->n
, 0);
3242 for (i
= 0; i
< map1
->n
; ++i
)
3243 for (j
= 0; j
< map2
->n
; ++j
) {
3244 struct isl_basic_map
*part
;
3245 part
= isl_basic_map_sum(
3246 isl_basic_map_copy(map1
->p
[i
]),
3247 isl_basic_map_copy(map2
->p
[j
]));
3248 if (isl_basic_map_is_empty(part
))
3249 isl_basic_map_free(part
);
3251 result
= isl_map_add_basic_map(result
, part
);
3264 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3265 __isl_take isl_set
*set2
)
3267 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3270 /* Given a basic map A -> f(A), construct A -> -f(A).
3272 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3277 bmap
= isl_basic_map_cow(bmap
);
3281 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3282 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3283 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3284 for (j
= 0; j
< n
; ++j
)
3285 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3286 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3287 for (j
= 0; j
< n
; ++j
)
3288 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3289 for (i
= 0; i
< bmap
->n_div
; ++i
)
3290 for (j
= 0; j
< n
; ++j
)
3291 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3292 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3293 return isl_basic_map_finalize(bmap
);
3296 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3298 return isl_basic_map_neg(bset
);
3301 /* Given a map A -> f(A), construct A -> -f(A).
3303 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3307 map
= isl_map_cow(map
);
3311 for (i
= 0; i
< map
->n
; ++i
) {
3312 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3323 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3325 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3328 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3329 * A -> floor(f(A)/d).
3331 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3334 unsigned n_in
, n_out
, nparam
, total
, pos
;
3335 struct isl_basic_map
*result
= NULL
;
3336 struct isl_dim_map
*dim_map
;
3342 nparam
= isl_basic_map_n_param(bmap
);
3343 n_in
= isl_basic_map_n_in(bmap
);
3344 n_out
= isl_basic_map_n_out(bmap
);
3346 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3347 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3348 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3349 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3350 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3351 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3353 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3354 bmap
->n_div
+ n_out
,
3355 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3356 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3357 result
= add_divs(result
, n_out
);
3358 for (i
= 0; i
< n_out
; ++i
) {
3360 j
= isl_basic_map_alloc_inequality(result
);
3363 isl_seq_clr(result
->ineq
[j
], 1+total
);
3364 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3365 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3366 j
= isl_basic_map_alloc_inequality(result
);
3369 isl_seq_clr(result
->ineq
[j
], 1+total
);
3370 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3371 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3372 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3375 result
= isl_basic_map_simplify(result
);
3376 return isl_basic_map_finalize(result
);
3378 isl_basic_map_free(result
);
3382 /* Given a map A -> f(A) and an integer d, construct a map
3383 * A -> floor(f(A)/d).
3385 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3389 map
= isl_map_cow(map
);
3393 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3394 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3395 for (i
= 0; i
< map
->n
; ++i
) {
3396 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3407 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3413 i
= isl_basic_map_alloc_equality(bmap
);
3416 nparam
= isl_basic_map_n_param(bmap
);
3417 n_in
= isl_basic_map_n_in(bmap
);
3418 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3419 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3420 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3421 return isl_basic_map_finalize(bmap
);
3423 isl_basic_map_free(bmap
);
3427 /* Add a constraints to "bmap" expressing i_pos < o_pos
3429 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3435 i
= isl_basic_map_alloc_inequality(bmap
);
3438 nparam
= isl_basic_map_n_param(bmap
);
3439 n_in
= isl_basic_map_n_in(bmap
);
3440 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3441 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3442 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3443 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3444 return isl_basic_map_finalize(bmap
);
3446 isl_basic_map_free(bmap
);
3450 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3452 static __isl_give isl_basic_map
*var_less_or_equal(
3453 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3459 i
= isl_basic_map_alloc_inequality(bmap
);
3462 nparam
= isl_basic_map_n_param(bmap
);
3463 n_in
= isl_basic_map_n_in(bmap
);
3464 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3465 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3466 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3467 return isl_basic_map_finalize(bmap
);
3469 isl_basic_map_free(bmap
);
3473 /* Add a constraints to "bmap" expressing i_pos > o_pos
3475 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3481 i
= isl_basic_map_alloc_inequality(bmap
);
3484 nparam
= isl_basic_map_n_param(bmap
);
3485 n_in
= isl_basic_map_n_in(bmap
);
3486 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3487 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3488 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3489 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3490 return isl_basic_map_finalize(bmap
);
3492 isl_basic_map_free(bmap
);
3496 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3498 static __isl_give isl_basic_map
*var_more_or_equal(
3499 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3505 i
= isl_basic_map_alloc_inequality(bmap
);
3508 nparam
= isl_basic_map_n_param(bmap
);
3509 n_in
= isl_basic_map_n_in(bmap
);
3510 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3511 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3512 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3513 return isl_basic_map_finalize(bmap
);
3515 isl_basic_map_free(bmap
);
3519 __isl_give isl_basic_map
*isl_basic_map_equal(
3520 __isl_take isl_space
*dim
, unsigned n_equal
)
3523 struct isl_basic_map
*bmap
;
3524 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3527 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3528 bmap
= var_equal(bmap
, i
);
3529 return isl_basic_map_finalize(bmap
);
3532 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3534 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3538 struct isl_basic_map
*bmap
;
3539 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3542 for (i
= 0; i
< pos
&& bmap
; ++i
)
3543 bmap
= var_equal(bmap
, i
);
3545 bmap
= var_less(bmap
, pos
);
3546 return isl_basic_map_finalize(bmap
);
3549 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3551 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3552 __isl_take isl_space
*dim
, unsigned pos
)
3555 isl_basic_map
*bmap
;
3557 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3558 for (i
= 0; i
< pos
; ++i
)
3559 bmap
= var_equal(bmap
, i
);
3560 bmap
= var_less_or_equal(bmap
, pos
);
3561 return isl_basic_map_finalize(bmap
);
3564 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3566 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3570 struct isl_basic_map
*bmap
;
3571 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3574 for (i
= 0; i
< pos
&& bmap
; ++i
)
3575 bmap
= var_equal(bmap
, i
);
3577 bmap
= var_more(bmap
, pos
);
3578 return isl_basic_map_finalize(bmap
);
3581 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3583 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3584 __isl_take isl_space
*dim
, unsigned pos
)
3587 isl_basic_map
*bmap
;
3589 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3590 for (i
= 0; i
< pos
; ++i
)
3591 bmap
= var_equal(bmap
, i
);
3592 bmap
= var_more_or_equal(bmap
, pos
);
3593 return isl_basic_map_finalize(bmap
);
3596 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3597 unsigned n
, int equal
)
3599 struct isl_map
*map
;
3602 if (n
== 0 && equal
)
3603 return isl_map_universe(dims
);
3605 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3607 for (i
= 0; i
+ 1 < n
; ++i
)
3608 map
= isl_map_add_basic_map(map
,
3609 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3612 map
= isl_map_add_basic_map(map
,
3613 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3615 map
= isl_map_add_basic_map(map
,
3616 isl_basic_map_less_at(dims
, n
- 1));
3618 isl_space_free(dims
);
3623 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3627 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3630 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3632 return map_lex_lte_first(dim
, n
, 0);
3635 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3637 return map_lex_lte_first(dim
, n
, 1);
3640 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3642 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3645 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3647 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3650 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3651 unsigned n
, int equal
)
3653 struct isl_map
*map
;
3656 if (n
== 0 && equal
)
3657 return isl_map_universe(dims
);
3659 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3661 for (i
= 0; i
+ 1 < n
; ++i
)
3662 map
= isl_map_add_basic_map(map
,
3663 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3666 map
= isl_map_add_basic_map(map
,
3667 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3669 map
= isl_map_add_basic_map(map
,
3670 isl_basic_map_more_at(dims
, n
- 1));
3672 isl_space_free(dims
);
3677 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3681 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3684 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3686 return map_lex_gte_first(dim
, n
, 0);
3689 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3691 return map_lex_gte_first(dim
, n
, 1);
3694 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3696 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3699 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3701 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3704 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3705 __isl_take isl_set
*set2
)
3708 map
= isl_map_lex_le(isl_set_get_space(set1
));
3709 map
= isl_map_intersect_domain(map
, set1
);
3710 map
= isl_map_intersect_range(map
, set2
);
3714 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3715 __isl_take isl_set
*set2
)
3718 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3719 map
= isl_map_intersect_domain(map
, set1
);
3720 map
= isl_map_intersect_range(map
, set2
);
3724 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3725 __isl_take isl_set
*set2
)
3728 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3729 map
= isl_map_intersect_domain(map
, set1
);
3730 map
= isl_map_intersect_range(map
, set2
);
3734 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3735 __isl_take isl_set
*set2
)
3738 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3739 map
= isl_map_intersect_domain(map
, set1
);
3740 map
= isl_map_intersect_range(map
, set2
);
3744 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3745 __isl_take isl_map
*map2
)
3748 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3749 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3750 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3754 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3755 __isl_take isl_map
*map2
)
3758 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3759 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3760 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3764 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3765 __isl_take isl_map
*map2
)
3768 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3769 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3770 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3774 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3775 __isl_take isl_map
*map2
)
3778 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3779 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3780 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3784 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3785 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3787 struct isl_basic_map
*bmap
;
3789 bset
= isl_basic_set_cow(bset
);
3793 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3794 isl_space_free(bset
->dim
);
3795 bmap
= (struct isl_basic_map
*) bset
;
3797 return isl_basic_map_finalize(bmap
);
3799 isl_basic_set_free(bset
);
3800 isl_space_free(dim
);
3804 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3808 if (bmap
->dim
->n_in
== 0)
3809 return (struct isl_basic_set
*)bmap
;
3810 bmap
= isl_basic_map_cow(bmap
);
3813 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3816 bmap
= isl_basic_map_finalize(bmap
);
3817 return (struct isl_basic_set
*)bmap
;
3819 isl_basic_map_free(bmap
);
3823 /* For a div d = floor(f/m), add the constraints
3826 * -(f-(n-1)) + m d >= 0
3828 * Note that the second constraint is the negation of
3832 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3833 unsigned pos
, isl_int
*div
)
3836 unsigned total
= isl_basic_map_total_dim(bmap
);
3838 i
= isl_basic_map_alloc_inequality(bmap
);
3841 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3842 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3844 j
= isl_basic_map_alloc_inequality(bmap
);
3847 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3848 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3849 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3853 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3854 unsigned pos
, isl_int
*div
)
3856 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3860 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3862 unsigned total
= isl_basic_map_total_dim(bmap
);
3863 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3865 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3869 struct isl_basic_set
*isl_basic_map_underlying_set(
3870 struct isl_basic_map
*bmap
)
3874 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3876 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3877 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3878 return (struct isl_basic_set
*)bmap
;
3879 bmap
= isl_basic_map_cow(bmap
);
3882 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3885 bmap
->extra
-= bmap
->n_div
;
3887 bmap
= isl_basic_map_finalize(bmap
);
3888 return (struct isl_basic_set
*)bmap
;
3893 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3894 __isl_take isl_basic_set
*bset
)
3896 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3899 struct isl_basic_map
*isl_basic_map_overlying_set(
3900 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3902 struct isl_basic_map
*bmap
;
3903 struct isl_ctx
*ctx
;
3910 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3911 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3912 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3914 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3915 isl_basic_map_free(like
);
3916 return (struct isl_basic_map
*)bset
;
3918 bset
= isl_basic_set_cow(bset
);
3921 total
= bset
->dim
->n_out
+ bset
->extra
;
3922 bmap
= (struct isl_basic_map
*)bset
;
3923 isl_space_free(bmap
->dim
);
3924 bmap
->dim
= isl_space_copy(like
->dim
);
3927 bmap
->n_div
= like
->n_div
;
3928 bmap
->extra
+= like
->n_div
;
3932 ltotal
= total
- bmap
->extra
+ like
->extra
;
3935 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3936 bmap
->extra
* (1 + 1 + total
));
3937 if (isl_blk_is_error(bmap
->block2
))
3939 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3943 for (i
= 0; i
< bmap
->extra
; ++i
)
3944 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3945 for (i
= 0; i
< like
->n_div
; ++i
) {
3946 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3947 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3949 bmap
= isl_basic_map_extend_constraints(bmap
,
3950 0, 2 * like
->n_div
);
3951 for (i
= 0; i
< like
->n_div
; ++i
) {
3952 if (isl_int_is_zero(bmap
->div
[i
][0]))
3954 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3958 isl_basic_map_free(like
);
3959 bmap
= isl_basic_map_simplify(bmap
);
3960 bmap
= isl_basic_map_finalize(bmap
);
3963 isl_basic_map_free(like
);
3964 isl_basic_set_free(bset
);
3968 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3969 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3971 return (struct isl_basic_set
*)
3972 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3975 struct isl_set
*isl_set_from_underlying_set(
3976 struct isl_set
*set
, struct isl_basic_set
*like
)
3982 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3984 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3985 isl_basic_set_free(like
);
3988 set
= isl_set_cow(set
);
3991 for (i
= 0; i
< set
->n
; ++i
) {
3992 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3993 isl_basic_set_copy(like
));
3997 isl_space_free(set
->dim
);
3998 set
->dim
= isl_space_copy(like
->dim
);
4001 isl_basic_set_free(like
);
4004 isl_basic_set_free(like
);
4009 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4013 map
= isl_map_cow(map
);
4016 map
->dim
= isl_space_cow(map
->dim
);
4020 for (i
= 1; i
< map
->n
; ++i
)
4021 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4023 for (i
= 0; i
< map
->n
; ++i
) {
4024 map
->p
[i
] = (struct isl_basic_map
*)
4025 isl_basic_map_underlying_set(map
->p
[i
]);
4030 map
->dim
= isl_space_underlying(map
->dim
, 0);
4032 isl_space_free(map
->dim
);
4033 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4037 return (struct isl_set
*)map
;
4043 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4045 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4048 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4049 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4051 bmap
= isl_basic_map_cow(bmap
);
4055 isl_space_free(bmap
->dim
);
4058 bmap
= isl_basic_map_finalize(bmap
);
4062 isl_basic_map_free(bmap
);
4063 isl_space_free(dim
);
4067 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4068 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4070 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4074 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4075 __isl_take isl_space
*dim
)
4079 map
= isl_map_cow(map
);
4083 for (i
= 0; i
< map
->n
; ++i
) {
4084 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4085 isl_space_copy(dim
));
4089 isl_space_free(map
->dim
);
4095 isl_space_free(dim
);
4099 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4100 __isl_take isl_space
*dim
)
4102 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4105 /* Compute the parameter domain of the given basic set.
4107 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4112 if (isl_basic_set_is_params(bset
))
4115 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4116 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4117 space
= isl_basic_set_get_space(bset
);
4118 space
= isl_space_params(space
);
4119 bset
= isl_basic_set_reset_space(bset
, space
);
4123 /* Compute the parameter domain of the given set.
4125 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4130 if (isl_set_is_params(set
))
4133 n
= isl_set_dim(set
, isl_dim_set
);
4134 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4135 space
= isl_set_get_space(set
);
4136 space
= isl_space_params(space
);
4137 set
= isl_set_reset_space(set
, space
);
4141 /* Construct a zero-dimensional set with the given parameter domain.
4143 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4146 space
= isl_set_get_space(set
);
4147 space
= isl_space_set_from_params(space
);
4148 set
= isl_set_reset_space(set
, space
);
4152 /* Compute the parameter domain of the given map.
4154 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4159 n
= isl_map_dim(map
, isl_dim_in
);
4160 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4161 n
= isl_map_dim(map
, isl_dim_out
);
4162 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4163 space
= isl_map_get_space(map
);
4164 space
= isl_space_params(space
);
4165 map
= isl_map_reset_space(map
, space
);
4169 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4172 struct isl_basic_set
*domain
;
4178 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4180 n_in
= isl_basic_map_n_in(bmap
);
4181 n_out
= isl_basic_map_n_out(bmap
);
4182 domain
= isl_basic_set_from_basic_map(bmap
);
4183 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4185 domain
= isl_basic_set_reset_space(domain
, dim
);
4190 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4194 return isl_space_may_be_set(bmap
->dim
);
4197 /* Is this basic map actually a set?
4198 * Users should never call this function. Outside of isl,
4199 * the type should indicate whether something is a set or a map.
4201 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4205 return isl_space_is_set(bmap
->dim
);
4208 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4212 if (isl_basic_map_is_set(bmap
))
4214 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4217 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4218 __isl_take isl_basic_map
*bmap
)
4222 isl_basic_map
*domain
;
4223 int nparam
, n_in
, n_out
;
4226 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4227 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4228 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4230 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4231 domain
= isl_basic_map_universe(dim
);
4233 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4234 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4235 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4237 total
= isl_basic_map_total_dim(bmap
);
4239 for (i
= 0; i
< n_in
; ++i
) {
4240 k
= isl_basic_map_alloc_equality(bmap
);
4243 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4244 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4245 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4248 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4249 return isl_basic_map_finalize(bmap
);
4251 isl_basic_map_free(bmap
);
4255 __isl_give isl_basic_map
*isl_basic_map_range_map(
4256 __isl_take isl_basic_map
*bmap
)
4260 isl_basic_map
*range
;
4261 int nparam
, n_in
, n_out
;
4264 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4265 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4266 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4268 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4269 range
= isl_basic_map_universe(dim
);
4271 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4272 bmap
= isl_basic_map_apply_range(bmap
, range
);
4273 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4275 total
= isl_basic_map_total_dim(bmap
);
4277 for (i
= 0; i
< n_out
; ++i
) {
4278 k
= isl_basic_map_alloc_equality(bmap
);
4281 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4282 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4283 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4286 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4287 return isl_basic_map_finalize(bmap
);
4289 isl_basic_map_free(bmap
);
4293 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4297 return isl_space_may_be_set(map
->dim
);
4300 /* Is this map actually a set?
4301 * Users should never call this function. Outside of isl,
4302 * the type should indicate whether something is a set or a map.
4304 int isl_map_is_set(__isl_keep isl_map
*map
)
4308 return isl_space_is_set(map
->dim
);
4311 struct isl_set
*isl_map_range(struct isl_map
*map
)
4314 struct isl_set
*set
;
4318 if (isl_map_is_set(map
))
4319 return (isl_set
*)map
;
4321 map
= isl_map_cow(map
);
4325 set
= (struct isl_set
*) map
;
4326 set
->dim
= isl_space_range(set
->dim
);
4329 for (i
= 0; i
< map
->n
; ++i
) {
4330 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4334 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4335 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4342 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4345 isl_space
*domain_dim
;
4347 map
= isl_map_cow(map
);
4351 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4352 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4353 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4356 for (i
= 0; i
< map
->n
; ++i
) {
4357 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4361 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4362 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4369 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4372 isl_space
*range_dim
;
4374 map
= isl_map_cow(map
);
4378 range_dim
= isl_space_range(isl_map_get_space(map
));
4379 range_dim
= isl_space_from_range(range_dim
);
4380 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4381 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4384 for (i
= 0; i
< map
->n
; ++i
) {
4385 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4389 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4390 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4397 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4398 __isl_take isl_space
*dim
)
4401 struct isl_map
*map
= NULL
;
4403 set
= isl_set_cow(set
);
4406 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4407 map
= (struct isl_map
*)set
;
4408 for (i
= 0; i
< set
->n
; ++i
) {
4409 map
->p
[i
] = isl_basic_map_from_basic_set(
4410 set
->p
[i
], isl_space_copy(dim
));
4414 isl_space_free(map
->dim
);
4418 isl_space_free(dim
);
4423 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4424 __isl_take isl_basic_set
*bset
)
4426 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4429 __isl_give isl_basic_map
*isl_basic_map_from_range(
4430 __isl_take isl_basic_set
*bset
)
4433 space
= isl_basic_set_get_space(bset
);
4434 space
= isl_space_from_range(space
);
4435 bset
= isl_basic_set_reset_space(bset
, space
);
4436 return (isl_basic_map
*)bset
;
4439 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4442 space
= isl_set_get_space(set
);
4443 space
= isl_space_from_range(space
);
4444 set
= isl_set_reset_space(set
, space
);
4445 return (struct isl_map
*)set
;
4448 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4450 return isl_map_reverse(isl_map_from_range(set
));
4453 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4454 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4456 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4459 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4460 __isl_take isl_set
*range
)
4462 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4465 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4468 struct isl_set
*set
= NULL
;
4472 map
= isl_map_cow(map
);
4475 map
->dim
= isl_space_as_set_space(map
->dim
);
4478 set
= (struct isl_set
*)map
;
4479 for (i
= 0; i
< map
->n
; ++i
) {
4480 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4490 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4493 struct isl_map
*map
;
4497 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4498 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4499 sizeof(struct isl_map
) +
4500 (n
- 1) * sizeof(struct isl_basic_map
*));
4504 map
->ctx
= dim
->ctx
;
4505 isl_ctx_ref(map
->ctx
);
4513 isl_space_free(dim
);
4517 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4518 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4521 struct isl_map
*map
;
4524 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4528 map
= isl_map_alloc_space(dims
, n
, flags
);
4532 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4534 struct isl_basic_map
*bmap
;
4535 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4536 bmap
= isl_basic_map_set_to_empty(bmap
);
4540 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4542 struct isl_basic_set
*bset
;
4543 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4544 bset
= isl_basic_set_set_to_empty(bset
);
4548 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4550 struct isl_basic_map
*bmap
;
4553 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4554 bmap
= isl_basic_map_set_to_empty(bmap
);
4558 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4560 struct isl_basic_map
*bmap
;
4563 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4564 bmap
= isl_basic_map_set_to_empty(bmap
);
4568 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4570 struct isl_basic_set
*bset
;
4573 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4574 bset
= isl_basic_set_set_to_empty(bset
);
4578 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4580 struct isl_basic_map
*bmap
;
4581 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4582 bmap
= isl_basic_map_finalize(bmap
);
4586 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4588 struct isl_basic_set
*bset
;
4589 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4590 bset
= isl_basic_set_finalize(bset
);
4594 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4597 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4598 isl_basic_map
*bmap
;
4600 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4601 for (i
= 0; i
< total
; ++i
) {
4602 int k
= isl_basic_map_alloc_inequality(bmap
);
4605 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4606 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4610 isl_basic_map_free(bmap
);
4614 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4616 return isl_basic_map_nat_universe(dim
);
4619 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4621 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4624 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4626 return isl_map_nat_universe(dim
);
4629 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4630 __isl_keep isl_basic_map
*model
)
4634 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4637 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4641 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4644 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4645 __isl_keep isl_set
*model
)
4649 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4652 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4654 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4657 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4661 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4664 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4668 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4671 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4673 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4676 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4680 return isl_set_empty(isl_space_copy(model
->dim
));
4683 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4685 struct isl_map
*map
;
4688 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4689 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4693 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4695 struct isl_set
*set
;
4698 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4699 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4703 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4707 return isl_set_universe(isl_space_copy(model
->dim
));
4710 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4713 struct isl_map
*dup
;
4717 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4718 for (i
= 0; i
< map
->n
; ++i
)
4719 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4723 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4724 __isl_take isl_basic_map
*bmap
)
4728 if (isl_basic_map_plain_is_empty(bmap
)) {
4729 isl_basic_map_free(bmap
);
4732 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4733 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4734 map
->p
[map
->n
] = bmap
;
4736 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4742 isl_basic_map_free(bmap
);
4746 void isl_map_free(struct isl_map
*map
)
4756 isl_ctx_deref(map
->ctx
);
4757 for (i
= 0; i
< map
->n
; ++i
)
4758 isl_basic_map_free(map
->p
[i
]);
4759 isl_space_free(map
->dim
);
4763 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4764 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4768 base
= isl_map_cow(base
);
4772 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4775 for (i
= 0; i
< base
->n
; ++i
) {
4776 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4777 isl_space_copy(base
->dim
), 0, 0, 0);
4787 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4788 unsigned nparam
, unsigned dim
)
4790 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4794 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4795 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4799 bmap
= isl_basic_map_cow(bmap
);
4800 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4801 j
= isl_basic_map_alloc_equality(bmap
);
4804 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4805 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4806 isl_int_set_si(bmap
->eq
[j
][0], value
);
4807 bmap
= isl_basic_map_simplify(bmap
);
4808 return isl_basic_map_finalize(bmap
);
4810 isl_basic_map_free(bmap
);
4814 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4815 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4819 bmap
= isl_basic_map_cow(bmap
);
4820 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4821 j
= isl_basic_map_alloc_equality(bmap
);
4824 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4825 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4826 isl_int_set(bmap
->eq
[j
][0], value
);
4827 bmap
= isl_basic_map_simplify(bmap
);
4828 return isl_basic_map_finalize(bmap
);
4830 isl_basic_map_free(bmap
);
4834 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4835 enum isl_dim_type type
, unsigned pos
, int value
)
4839 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4840 return isl_basic_map_fix_pos_si(bmap
,
4841 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4843 isl_basic_map_free(bmap
);
4847 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4848 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4852 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4853 return isl_basic_map_fix_pos(bmap
,
4854 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4856 isl_basic_map_free(bmap
);
4860 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4861 enum isl_dim_type type
, unsigned pos
, int value
)
4863 return (struct isl_basic_set
*)
4864 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4868 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4869 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4871 return (struct isl_basic_set
*)
4872 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4876 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4877 unsigned input
, int value
)
4879 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4882 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4883 unsigned dim
, int value
)
4885 return (struct isl_basic_set
*)
4886 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4887 isl_dim_set
, dim
, value
);
4890 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4891 enum isl_dim_type type
, unsigned pos
, int value
)
4895 map
= isl_map_cow(map
);
4899 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4900 for (i
= 0; i
< map
->n
; ++i
) {
4901 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4905 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4912 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4913 enum isl_dim_type type
, unsigned pos
, int value
)
4915 return (struct isl_set
*)
4916 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4919 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4920 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4924 map
= isl_map_cow(map
);
4928 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4929 for (i
= 0; i
< map
->n
; ++i
) {
4930 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4934 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4941 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4942 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4944 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4947 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4948 unsigned input
, int value
)
4950 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4953 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4955 return (struct isl_set
*)
4956 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4959 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4960 __isl_take isl_basic_map
*bmap
,
4961 enum isl_dim_type type
, unsigned pos
, int value
)
4967 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4968 pos
+= isl_basic_map_offset(bmap
, type
);
4969 bmap
= isl_basic_map_cow(bmap
);
4970 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4971 j
= isl_basic_map_alloc_inequality(bmap
);
4974 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4975 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4976 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4977 bmap
= isl_basic_map_simplify(bmap
);
4978 return isl_basic_map_finalize(bmap
);
4980 isl_basic_map_free(bmap
);
4984 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4985 unsigned dim
, isl_int value
)
4989 bset
= isl_basic_set_cow(bset
);
4990 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4991 j
= isl_basic_set_alloc_inequality(bset
);
4994 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4995 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4996 isl_int_neg(bset
->ineq
[j
][0], value
);
4997 bset
= isl_basic_set_simplify(bset
);
4998 return isl_basic_set_finalize(bset
);
5000 isl_basic_set_free(bset
);
5004 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5005 enum isl_dim_type type
, unsigned pos
, int value
)
5009 map
= isl_map_cow(map
);
5013 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5014 for (i
= 0; i
< map
->n
; ++i
) {
5015 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
5020 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5027 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5028 enum isl_dim_type type
, unsigned pos
, int value
)
5030 return (struct isl_set
*)
5031 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5034 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5039 set
= isl_set_cow(set
);
5043 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5044 for (i
= 0; i
< set
->n
; ++i
) {
5045 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5055 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5059 map
= isl_map_cow(map
);
5063 map
->dim
= isl_space_reverse(map
->dim
);
5066 for (i
= 0; i
< map
->n
; ++i
) {
5067 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5071 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5078 static struct isl_map
*isl_basic_map_partial_lexopt(
5079 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5080 struct isl_set
**empty
, int max
)
5084 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5085 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5087 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5089 isl_basic_map_free(bmap
);
5090 isl_basic_set_free(dom
);
5096 struct isl_map
*isl_basic_map_partial_lexmax(
5097 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5098 struct isl_set
**empty
)
5100 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5103 struct isl_map
*isl_basic_map_partial_lexmin(
5104 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5105 struct isl_set
**empty
)
5107 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5110 struct isl_set
*isl_basic_set_partial_lexmin(
5111 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5112 struct isl_set
**empty
)
5114 return (struct isl_set
*)
5115 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5119 struct isl_set
*isl_basic_set_partial_lexmax(
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_lexmax((struct isl_basic_map
*)bset
,
5128 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5129 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5130 __isl_give isl_set
**empty
)
5132 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5135 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5136 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5137 __isl_give isl_set
**empty
)
5139 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5142 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5143 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5144 __isl_give isl_set
**empty
)
5146 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5149 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5150 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5151 __isl_give isl_set
**empty
)
5153 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5156 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5157 __isl_take isl_basic_map
*bmap
, int max
)
5159 isl_basic_set
*dom
= NULL
;
5160 isl_space
*dom_space
;
5164 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5165 dom
= isl_basic_set_universe(dom_space
);
5166 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5168 isl_basic_map_free(bmap
);
5172 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5173 __isl_take isl_basic_map
*bmap
)
5175 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5178 /* Given a basic map "bmap", compute the lexicographically minimal
5179 * (or maximal) image element for each domain element in dom.
5180 * Set *empty to those elements in dom that do not have an image element.
5182 * We first make sure the basic sets in dom are disjoint and then
5183 * simply collect the results over each of the basic sets separately.
5184 * We could probably improve the efficiency a bit by moving the union
5185 * domain down into the parametric integer programming.
5187 static __isl_give isl_map
*basic_map_partial_lexopt(
5188 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5189 __isl_give isl_set
**empty
, int max
)
5192 struct isl_map
*res
;
5194 dom
= isl_set_make_disjoint(dom
);
5198 if (isl_set_plain_is_empty(dom
)) {
5199 res
= isl_map_empty_like_basic_map(bmap
);
5200 *empty
= isl_set_empty_like(dom
);
5202 isl_basic_map_free(bmap
);
5206 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5207 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5209 for (i
= 1; i
< dom
->n
; ++i
) {
5210 struct isl_map
*res_i
;
5211 struct isl_set
*empty_i
;
5213 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5214 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5216 res
= isl_map_union_disjoint(res
, res_i
);
5217 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5221 isl_basic_map_free(bmap
);
5226 isl_basic_map_free(bmap
);
5230 /* Given a map "map", compute the lexicographically minimal
5231 * (or maximal) image element for each domain element in dom.
5232 * Set *empty to those elements in dom that do not have an image element.
5234 * We first compute the lexicographically minimal or maximal element
5235 * in the first basic map. This results in a partial solution "res"
5236 * and a subset "todo" of dom that still need to be handled.
5237 * We then consider each of the remaining maps in "map" and successively
5238 * improve both "res" and "todo".
5240 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5241 * Assume we are computing the lexicographical maximum.
5242 * We first compute the lexicographically maximal element in basic map i.
5243 * This results in a partial solution res_i and a subset todo_i.
5244 * Then we combine these results with those obtain for the first k basic maps
5245 * to obtain a result that is valid for the first k+1 basic maps.
5246 * In particular, the set where there is no solution is the set where
5247 * there is no solution for the first k basic maps and also no solution
5248 * for the ith basic map, i.e.,
5250 * todo^i = todo^k * todo_i
5252 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5253 * solutions, arbitrarily breaking ties in favor of res^k.
5254 * That is, when res^k(a) >= res_i(a), we pick res^k and
5255 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5256 * the lexicographic order.)
5257 * In practice, we compute
5259 * res^k * (res_i . "<=")
5263 * res_i * (res^k . "<")
5265 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5266 * where only one of res^k and res_i provides a solution and we simply pick
5273 * Note that we only compute these intersections when dom(res^k) intersects
5274 * dom(res_i). Otherwise, the only effect of these intersections is to
5275 * potentially break up res^k and res_i into smaller pieces.
5276 * We want to avoid such splintering as much as possible.
5277 * In fact, an earlier implementation of this function would look for
5278 * better results in the domain of res^k and for extra results in todo^k,
5279 * but this would always result in a splintering according to todo^k,
5280 * even when the domain of basic map i is disjoint from the domains of
5281 * the previous basic maps.
5283 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5284 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5285 __isl_give isl_set
**empty
, int max
)
5288 struct isl_map
*res
;
5289 struct isl_set
*todo
;
5294 if (isl_map_plain_is_empty(map
)) {
5302 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5303 isl_set_copy(dom
), &todo
, max
);
5305 for (i
= 1; i
< map
->n
; ++i
) {
5309 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5311 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5312 isl_set_copy(dom
), &todo_i
, max
);
5315 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5316 le
= isl_map_lex_le(dim
);
5318 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5319 le
= isl_map_lex_ge(dim
);
5321 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5322 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5323 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5324 le
= isl_map_intersect(le
, isl_map_copy(res
));
5326 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5327 res
= isl_map_intersect_domain(res
,
5328 isl_set_copy(todo_i
));
5329 res_i
= isl_map_intersect_domain(res_i
,
5330 isl_set_copy(todo
));
5333 res
= isl_map_union_disjoint(res
, res_i
);
5334 res
= isl_map_union_disjoint(res
, lt
);
5335 res
= isl_map_union_disjoint(res
, le
);
5337 todo
= isl_set_intersect(todo
, todo_i
);
5357 /* Given a map "map", compute the lexicographically minimal
5358 * (or maximal) image element for each domain element in dom.
5359 * Set *empty to those elements in dom that do not have an image element.
5361 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5363 static __isl_give isl_map
*isl_map_partial_lexopt(
5364 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5365 __isl_give isl_set
**empty
, int max
)
5369 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5370 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5371 if (!isl_space_has_named_params(map
->dim
) ||
5372 !isl_space_has_named_params(dom
->dim
))
5373 isl_die(map
->ctx
, isl_error_invalid
,
5374 "unaligned unnamed parameters", goto error
);
5375 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5376 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5377 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5386 __isl_give isl_map
*isl_map_partial_lexmax(
5387 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5388 __isl_give isl_set
**empty
)
5390 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5393 __isl_give isl_map
*isl_map_partial_lexmin(
5394 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5395 __isl_give isl_set
**empty
)
5397 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5400 __isl_give isl_set
*isl_set_partial_lexmin(
5401 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5402 __isl_give isl_set
**empty
)
5404 return (struct isl_set
*)
5405 isl_map_partial_lexmin((struct isl_map
*)set
,
5409 __isl_give isl_set
*isl_set_partial_lexmax(
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_lexmax((struct isl_map
*)set
,
5418 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5420 struct isl_basic_set
*dom
= NULL
;
5425 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5426 dom
= isl_basic_set_universe(dom_dim
);
5427 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5429 isl_basic_map_free(bmap
);
5433 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5435 return isl_basic_map_lexopt(bmap
, 0);
5438 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5440 return isl_basic_map_lexopt(bmap
, 1);
5443 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5445 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5448 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5450 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5453 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5455 struct isl_set
*dom
= NULL
;
5460 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5461 dom
= isl_set_universe(dom_dim
);
5462 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5468 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5470 return isl_map_lexopt(map
, 0);
5473 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5475 return isl_map_lexopt(map
, 1);
5478 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5480 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5483 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5485 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5488 /* Extract the first and only affine expression from list
5489 * and then add it to *pwaff with the given dom.
5490 * This domain is known to be disjoint from other domains
5491 * because of the way isl_basic_map_foreach_lexmax works.
5493 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5494 __isl_take isl_aff_list
*list
, void *user
)
5496 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5498 isl_pw_aff
**pwaff
= user
;
5499 isl_pw_aff
*pwaff_i
;
5501 if (isl_aff_list_n_aff(list
) != 1)
5502 isl_die(ctx
, isl_error_internal
,
5503 "expecting single element list", goto error
);
5505 aff
= isl_aff_list_get_aff(list
, 0);
5506 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5508 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5510 isl_aff_list_free(list
);
5514 isl_basic_set_free(dom
);
5515 isl_aff_list_free(list
);
5519 /* Given a basic map with one output dimension, compute the minimum or
5520 * maximum of that dimension as an isl_pw_aff.
5522 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5523 * call update_dim_opt on each leaf of the result.
5525 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5528 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5532 dim
= isl_space_from_domain(isl_space_domain(dim
));
5533 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5534 pwaff
= isl_pw_aff_empty(dim
);
5536 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5538 return isl_pw_aff_free(pwaff
);
5543 /* Compute the minimum or maximum of the given output dimension
5544 * as a function of the parameters and the input dimensions,
5545 * but independently of the other output dimensions.
5547 * We first project out the other output dimension and then compute
5548 * the "lexicographic" maximum in each basic map, combining the results
5549 * using isl_pw_aff_union_max.
5551 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5558 n_out
= isl_map_dim(map
, isl_dim_out
);
5559 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5560 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5565 isl_space
*dim
= isl_map_get_space(map
);
5566 dim
= isl_space_domain(isl_space_from_range(dim
));
5568 return isl_pw_aff_empty(dim
);
5571 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5572 for (i
= 1; i
< map
->n
; ++i
) {
5573 isl_pw_aff
*pwaff_i
;
5575 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5576 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5584 /* Compute the maximum of the given output dimension as a function of the
5585 * parameters and input dimensions, but independently of
5586 * the other output dimensions.
5588 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5590 return map_dim_opt(map
, pos
, 1);
5593 /* Compute the minimum or maximum of the given set dimension
5594 * as a function of the parameters,
5595 * but independently of the other set dimensions.
5597 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5600 return map_dim_opt(set
, pos
, max
);
5603 /* Compute the maximum of the given set dimension as a function of the
5604 * parameters, but independently of the other set dimensions.
5606 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5608 return set_dim_opt(set
, pos
, 1);
5611 /* Compute the minimum of the given set dimension as a function of the
5612 * parameters, but independently of the other set dimensions.
5614 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5616 return set_dim_opt(set
, pos
, 0);
5619 /* Apply a preimage specified by "mat" on the parameters of "bset".
5620 * bset is assumed to have only parameters and divs.
5622 static struct isl_basic_set
*basic_set_parameter_preimage(
5623 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5630 bset
->dim
= isl_space_cow(bset
->dim
);
5634 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5636 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5638 bset
->dim
->nparam
= 0;
5639 bset
->dim
->n_out
= nparam
;
5640 bset
= isl_basic_set_preimage(bset
, mat
);
5642 bset
->dim
->nparam
= bset
->dim
->n_out
;
5643 bset
->dim
->n_out
= 0;
5648 isl_basic_set_free(bset
);
5652 /* Apply a preimage specified by "mat" on the parameters of "set".
5653 * set is assumed to have only parameters and divs.
5655 static struct isl_set
*set_parameter_preimage(
5656 struct isl_set
*set
, struct isl_mat
*mat
)
5658 isl_space
*dim
= NULL
;
5664 dim
= isl_space_copy(set
->dim
);
5665 dim
= isl_space_cow(dim
);
5669 nparam
= isl_set_dim(set
, isl_dim_param
);
5671 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5674 dim
->n_out
= nparam
;
5675 isl_set_reset_space(set
, dim
);
5676 set
= isl_set_preimage(set
, mat
);
5679 dim
= isl_space_copy(set
->dim
);
5680 dim
= isl_space_cow(dim
);
5683 dim
->nparam
= dim
->n_out
;
5685 isl_set_reset_space(set
, dim
);
5688 isl_space_free(dim
);
5695 /* Intersect the basic set "bset" with the affine space specified by the
5696 * equalities in "eq".
5698 static struct isl_basic_set
*basic_set_append_equalities(
5699 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5707 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5712 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5713 for (i
= 0; i
< eq
->n_row
; ++i
) {
5714 k
= isl_basic_set_alloc_equality(bset
);
5717 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5718 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5722 bset
= isl_basic_set_gauss(bset
, NULL
);
5723 bset
= isl_basic_set_finalize(bset
);
5728 isl_basic_set_free(bset
);
5732 /* Intersect the set "set" with the affine space specified by the
5733 * equalities in "eq".
5735 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5743 for (i
= 0; i
< set
->n
; ++i
) {
5744 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5757 /* Project the given basic set onto its parameter domain, possibly introducing
5758 * new, explicit, existential variables in the constraints.
5759 * The input has parameters and (possibly implicit) existential variables.
5760 * The output has the same parameters, but only
5761 * explicit existentially quantified variables.
5763 * The actual projection is performed by pip, but pip doesn't seem
5764 * to like equalities very much, so we first remove the equalities
5765 * among the parameters by performing a variable compression on
5766 * the parameters. Afterward, an inverse transformation is performed
5767 * and the equalities among the parameters are inserted back in.
5769 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5773 struct isl_mat
*T
, *T2
;
5774 struct isl_set
*set
;
5775 unsigned nparam
, n_div
;
5777 bset
= isl_basic_set_cow(bset
);
5781 if (bset
->n_eq
== 0)
5782 return isl_basic_set_lexmin(bset
);
5784 isl_basic_set_gauss(bset
, NULL
);
5786 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5787 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5789 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5790 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5793 if (i
== bset
->n_eq
)
5794 return isl_basic_set_lexmin(bset
);
5796 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5798 eq
= isl_mat_cow(eq
);
5799 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5800 if (T
&& T
->n_col
== 0) {
5804 bset
= isl_basic_set_set_to_empty(bset
);
5805 return isl_set_from_basic_set(bset
);
5807 bset
= basic_set_parameter_preimage(bset
, T
);
5809 set
= isl_basic_set_lexmin(bset
);
5810 set
= set_parameter_preimage(set
, T2
);
5811 set
= set_append_equalities(set
, eq
);
5815 /* Compute an explicit representation for all the existentially
5816 * quantified variables.
5817 * The input and output dimensions are first turned into parameters.
5818 * compute_divs then returns a map with the same parameters and
5819 * no input or output dimensions and the dimension specification
5820 * is reset to that of the input.
5822 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5824 struct isl_basic_set
*bset
;
5825 struct isl_set
*set
;
5826 struct isl_map
*map
;
5827 isl_space
*dim
, *orig_dim
= NULL
;
5832 bmap
= isl_basic_map_cow(bmap
);
5836 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5837 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5838 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5839 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5843 orig_dim
= bmap
->dim
;
5845 bset
= (struct isl_basic_set
*)bmap
;
5847 set
= parameter_compute_divs(bset
);
5848 map
= (struct isl_map
*)set
;
5849 map
= isl_map_reset_space(map
, orig_dim
);
5853 isl_basic_map_free(bmap
);
5857 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5865 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5866 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5867 if (isl_int_is_zero(bmap
->div
[i
][0]))
5869 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5875 static int map_divs_known(__isl_keep isl_map
*map
)
5882 for (i
= 0; i
< map
->n
; ++i
) {
5883 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5891 /* If bmap contains any unknown divs, then compute explicit
5892 * expressions for them. However, this computation may be
5893 * quite expensive, so first try to remove divs that aren't
5896 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5899 struct isl_map
*map
;
5901 known
= isl_basic_map_divs_known(bmap
);
5905 return isl_map_from_basic_map(bmap
);
5907 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5909 known
= isl_basic_map_divs_known(bmap
);
5913 return isl_map_from_basic_map(bmap
);
5915 map
= compute_divs(bmap
);
5918 isl_basic_map_free(bmap
);
5922 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5926 struct isl_map
*res
;
5933 known
= map_divs_known(map
);
5941 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5942 for (i
= 1 ; i
< map
->n
; ++i
) {
5944 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5945 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5946 res
= isl_map_union_disjoint(res
, r2
);
5948 res
= isl_map_union(res
, r2
);
5955 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5957 return (struct isl_set
*)
5958 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5961 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5963 return (struct isl_set
*)
5964 isl_map_compute_divs((struct isl_map
*)set
);
5967 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5970 struct isl_set
*set
;
5975 map
= isl_map_cow(map
);
5979 set
= (struct isl_set
*)map
;
5980 set
->dim
= isl_space_domain(set
->dim
);
5983 for (i
= 0; i
< map
->n
; ++i
) {
5984 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5988 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5989 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5996 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
5997 __isl_take isl_map
*map2
)
6001 struct isl_map
*map
= NULL
;
6015 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6017 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6018 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6019 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6021 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6022 map1
->n
+ map2
->n
, flags
);
6025 for (i
= 0; i
< map1
->n
; ++i
) {
6026 map
= isl_map_add_basic_map(map
,
6027 isl_basic_map_copy(map1
->p
[i
]));
6031 for (i
= 0; i
< map2
->n
; ++i
) {
6032 map
= isl_map_add_basic_map(map
,
6033 isl_basic_map_copy(map2
->p
[i
]));
6047 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6048 __isl_take isl_map
*map2
)
6050 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6053 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6055 map1
= isl_map_union_disjoint(map1
, map2
);
6059 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6063 struct isl_set
*isl_set_union_disjoint(
6064 struct isl_set
*set1
, struct isl_set
*set2
)
6066 return (struct isl_set
*)
6067 isl_map_union_disjoint(
6068 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6071 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6073 return (struct isl_set
*)
6074 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6077 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6078 __isl_take isl_set
*set
)
6081 struct isl_map
*result
;
6087 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6088 isl_die(set
->ctx
, isl_error_invalid
,
6089 "parameters don't match", goto error
);
6091 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6092 !isl_map_compatible_range(map
, set
))
6093 isl_die(set
->ctx
, isl_error_invalid
,
6094 "incompatible spaces", goto error
);
6096 if (isl_set_plain_is_universe(set
)) {
6101 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6102 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6103 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6105 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6106 map
->n
* set
->n
, flags
);
6109 for (i
= 0; i
< map
->n
; ++i
)
6110 for (j
= 0; j
< set
->n
; ++j
) {
6111 result
= isl_map_add_basic_map(result
,
6112 isl_basic_map_intersect_range(
6113 isl_basic_map_copy(map
->p
[i
]),
6114 isl_basic_set_copy(set
->p
[j
])));
6127 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6128 __isl_take isl_set
*set
)
6130 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6133 struct isl_map
*isl_map_intersect_domain(
6134 struct isl_map
*map
, struct isl_set
*set
)
6136 return isl_map_reverse(
6137 isl_map_intersect_range(isl_map_reverse(map
), set
));
6140 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6141 __isl_take isl_map
*map2
)
6145 map1
= isl_map_reverse(map1
);
6146 map1
= isl_map_apply_range(map1
, map2
);
6147 return isl_map_reverse(map1
);
6154 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6155 __isl_take isl_map
*map2
)
6157 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6160 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6161 __isl_take isl_map
*map2
)
6163 isl_space
*dim_result
;
6164 struct isl_map
*result
;
6170 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6171 isl_space_copy(map2
->dim
));
6173 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6176 for (i
= 0; i
< map1
->n
; ++i
)
6177 for (j
= 0; j
< map2
->n
; ++j
) {
6178 result
= isl_map_add_basic_map(result
,
6179 isl_basic_map_apply_range(
6180 isl_basic_map_copy(map1
->p
[i
]),
6181 isl_basic_map_copy(map2
->p
[j
])));
6187 if (result
&& result
->n
<= 1)
6188 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6196 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6197 __isl_take isl_map
*map2
)
6199 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6203 * returns range - domain
6205 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6207 isl_space
*dims
, *target_dim
;
6208 struct isl_basic_set
*bset
;
6215 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6216 bmap
->dim
, isl_dim_out
),
6218 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6219 dim
= isl_basic_map_n_in(bmap
);
6220 nparam
= isl_basic_map_n_param(bmap
);
6221 bset
= isl_basic_set_from_basic_map(bmap
);
6222 bset
= isl_basic_set_cow(bset
);
6223 dims
= isl_basic_set_get_space(bset
);
6224 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6225 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6226 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6227 for (i
= 0; i
< dim
; ++i
) {
6228 int j
= isl_basic_map_alloc_equality(
6229 (struct isl_basic_map
*)bset
);
6232 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6233 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6234 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6235 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6237 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6238 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6241 isl_basic_map_free(bmap
);
6246 * returns range - domain
6248 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6252 struct isl_set
*result
;
6257 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6258 map
->dim
, isl_dim_out
),
6260 dim
= isl_map_get_space(map
);
6261 dim
= isl_space_domain(dim
);
6262 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6265 for (i
= 0; i
< map
->n
; ++i
)
6266 result
= isl_set_add_basic_set(result
,
6267 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6276 * returns [domain -> range] -> range - domain
6278 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6279 __isl_take isl_basic_map
*bmap
)
6283 isl_basic_map
*domain
;
6287 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6288 isl_die(bmap
->ctx
, isl_error_invalid
,
6289 "domain and range don't match", goto error
);
6291 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6292 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6294 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6295 domain
= isl_basic_map_universe(dim
);
6297 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6298 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6299 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6301 total
= isl_basic_map_total_dim(bmap
);
6303 for (i
= 0; i
< n
; ++i
) {
6304 k
= isl_basic_map_alloc_equality(bmap
);
6307 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6308 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6309 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6310 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6313 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6314 return isl_basic_map_finalize(bmap
);
6316 isl_basic_map_free(bmap
);
6321 * returns [domain -> range] -> range - domain
6323 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6326 isl_space
*domain_dim
;
6331 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6332 isl_die(map
->ctx
, isl_error_invalid
,
6333 "domain and range don't match", goto error
);
6335 map
= isl_map_cow(map
);
6339 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6340 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6341 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6344 for (i
= 0; i
< map
->n
; ++i
) {
6345 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6349 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6356 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6358 struct isl_basic_map
*bmap
;
6366 nparam
= dims
->nparam
;
6368 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6372 for (i
= 0; i
< dim
; ++i
) {
6373 int j
= isl_basic_map_alloc_equality(bmap
);
6376 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6377 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6378 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6380 return isl_basic_map_finalize(bmap
);
6382 isl_basic_map_free(bmap
);
6386 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6390 if (dim
->n_in
!= dim
->n_out
)
6391 isl_die(dim
->ctx
, isl_error_invalid
,
6392 "number of input and output dimensions needs to be "
6393 "the same", goto error
);
6394 return basic_map_identity(dim
);
6396 isl_space_free(dim
);
6400 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6402 if (!model
|| !model
->dim
)
6404 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6407 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6409 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6412 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6414 if (!model
|| !model
->dim
)
6416 return isl_map_identity(isl_space_copy(model
->dim
));
6419 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6421 if (!model
|| !model
->dim
)
6423 return isl_map_identity(isl_space_copy(model
->dim
));
6426 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6428 isl_space
*dim
= isl_set_get_space(set
);
6430 id
= isl_map_identity(isl_space_map_from_set(dim
));
6431 return isl_map_intersect_range(id
, set
);
6434 /* Construct a basic set with all set dimensions having only non-negative
6437 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6438 __isl_take isl_space
*space
)
6443 struct isl_basic_set
*bset
;
6447 nparam
= space
->nparam
;
6449 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6452 for (i
= 0; i
< dim
; ++i
) {
6453 int k
= isl_basic_set_alloc_inequality(bset
);
6456 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6457 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6461 isl_basic_set_free(bset
);
6465 /* Construct the half-space x_pos >= 0.
6467 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6471 isl_basic_set
*nonneg
;
6473 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6474 k
= isl_basic_set_alloc_inequality(nonneg
);
6477 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6478 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6480 return isl_basic_set_finalize(nonneg
);
6482 isl_basic_set_free(nonneg
);
6486 /* Construct the half-space x_pos <= -1.
6488 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6493 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6494 k
= isl_basic_set_alloc_inequality(neg
);
6497 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6498 isl_int_set_si(neg
->ineq
[k
][0], -1);
6499 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6501 return isl_basic_set_finalize(neg
);
6503 isl_basic_set_free(neg
);
6507 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6508 enum isl_dim_type type
, unsigned first
, unsigned n
)
6511 isl_basic_set
*nonneg
;
6519 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6521 for (i
= 0; i
< n
; ++i
) {
6522 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6523 pos(set
->dim
, type
) + first
+ i
);
6524 neg
= neg_halfspace(isl_set_get_space(set
),
6525 pos(set
->dim
, type
) + first
+ i
);
6527 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6536 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6537 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6544 if (isl_set_plain_is_empty(set
)) {
6549 return fn(set
, signs
, user
);
6552 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6554 half
= isl_set_intersect(half
, isl_set_copy(set
));
6555 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6559 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6561 half
= isl_set_intersect(half
, set
);
6562 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6568 /* Call "fn" on the intersections of "set" with each of the orthants
6569 * (except for obviously empty intersections). The orthant is identified
6570 * by the signs array, with each entry having value 1 or -1 according
6571 * to the sign of the corresponding variable.
6573 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6574 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6584 if (isl_set_plain_is_empty(set
))
6587 nparam
= isl_set_dim(set
, isl_dim_param
);
6588 nvar
= isl_set_dim(set
, isl_dim_set
);
6590 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6592 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6600 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6602 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6605 int isl_basic_map_is_subset(
6606 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6609 struct isl_map
*map1
;
6610 struct isl_map
*map2
;
6612 if (!bmap1
|| !bmap2
)
6615 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6616 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6618 is_subset
= isl_map_is_subset(map1
, map2
);
6626 int isl_basic_map_is_equal(
6627 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6631 if (!bmap1
|| !bmap2
)
6633 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6636 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6640 int isl_basic_set_is_equal(
6641 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6643 return isl_basic_map_is_equal(
6644 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6647 int isl_map_is_empty(struct isl_map
*map
)
6654 for (i
= 0; i
< map
->n
; ++i
) {
6655 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6664 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6666 return map
? map
->n
== 0 : -1;
6669 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6671 return isl_map_plain_is_empty(map
);
6674 int isl_set_plain_is_empty(struct isl_set
*set
)
6676 return set
? set
->n
== 0 : -1;
6679 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6681 return isl_set_plain_is_empty(set
);
6684 int isl_set_is_empty(struct isl_set
*set
)
6686 return isl_map_is_empty((struct isl_map
*)set
);
6689 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6694 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6697 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6702 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6705 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6711 is_subset
= isl_map_is_subset(map1
, map2
);
6714 is_subset
= isl_map_is_subset(map2
, map1
);
6718 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6720 return align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6723 int isl_basic_map_is_strict_subset(
6724 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6728 if (!bmap1
|| !bmap2
)
6730 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6733 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6734 if (is_subset
== -1)
6739 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6745 is_subset
= isl_map_is_subset(map1
, map2
);
6748 is_subset
= isl_map_is_subset(map2
, map1
);
6749 if (is_subset
== -1)
6754 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6756 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6759 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6763 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6766 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6770 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6773 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6780 for (i
= 0; i
< map
->n
; ++i
) {
6781 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6789 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6791 return isl_map_plain_is_universe((isl_map
*) set
);
6794 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6796 return isl_set_plain_is_universe(set
);
6799 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6801 struct isl_basic_set
*bset
= NULL
;
6802 struct isl_vec
*sample
= NULL
;
6809 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6812 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6813 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6814 copy
= isl_basic_map_remove_redundancies(copy
);
6815 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6816 isl_basic_map_free(copy
);
6820 total
= 1 + isl_basic_map_total_dim(bmap
);
6821 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6822 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6828 isl_vec_free(bmap
->sample
);
6829 bmap
->sample
= NULL
;
6830 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6833 sample
= isl_basic_set_sample_vec(bset
);
6836 empty
= sample
->size
== 0;
6837 isl_vec_free(bmap
->sample
);
6838 bmap
->sample
= sample
;
6840 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6845 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6849 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6852 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6854 return isl_basic_map_plain_is_empty(bmap
);
6857 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6861 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6864 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6866 return isl_basic_set_plain_is_empty(bset
);
6869 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6871 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6874 struct isl_map
*isl_basic_map_union(
6875 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6877 struct isl_map
*map
;
6878 if (!bmap1
|| !bmap2
)
6881 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6883 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6886 map
= isl_map_add_basic_map(map
, bmap1
);
6887 map
= isl_map_add_basic_map(map
, bmap2
);
6890 isl_basic_map_free(bmap1
);
6891 isl_basic_map_free(bmap2
);
6895 struct isl_set
*isl_basic_set_union(
6896 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6898 return (struct isl_set
*)isl_basic_map_union(
6899 (struct isl_basic_map
*)bset1
,
6900 (struct isl_basic_map
*)bset2
);
6903 /* Order divs such that any div only depends on previous divs */
6904 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6912 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6914 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6916 if (isl_int_is_zero(bmap
->div
[i
][0]))
6918 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6922 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6928 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6930 return (struct isl_basic_set
*)
6931 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6934 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6941 for (i
= 0; i
< map
->n
; ++i
) {
6942 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6953 /* Apply the expansion computed by isl_merge_divs.
6954 * The expansion itself is given by "exp" while the resulting
6955 * list of divs is given by "div".
6957 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6958 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6963 bset
= isl_basic_set_cow(bset
);
6967 if (div
->n_row
< bset
->n_div
)
6968 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6969 "not an expansion", goto error
);
6971 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6972 div
->n_row
- bset
->n_div
, 0,
6973 2 * (div
->n_row
- bset
->n_div
));
6975 n_div
= bset
->n_div
;
6976 for (i
= n_div
; i
< div
->n_row
; ++i
)
6977 if (isl_basic_set_alloc_div(bset
) < 0)
6981 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6982 if (j
>= 0 && exp
[j
] == i
) {
6984 isl_basic_map_swap_div(bset
, i
, j
);
6987 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6988 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6996 isl_basic_set_free(bset
);
7001 /* Look for a div in dst that corresponds to the div "div" in src.
7002 * The divs before "div" in src and dst are assumed to be the same.
7004 * Returns -1 if no corresponding div was found and the position
7005 * of the corresponding div in dst otherwise.
7007 static int find_div(struct isl_basic_map
*dst
,
7008 struct isl_basic_map
*src
, unsigned div
)
7012 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7014 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7015 for (i
= div
; i
< dst
->n_div
; ++i
)
7016 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7017 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7018 dst
->n_div
- div
) == -1)
7023 struct isl_basic_map
*isl_basic_map_align_divs(
7024 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7027 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7032 if (src
->n_div
== 0)
7035 for (i
= 0; i
< src
->n_div
; ++i
)
7036 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7038 src
= isl_basic_map_order_divs(src
);
7039 dst
= isl_basic_map_cow(dst
);
7040 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7041 src
->n_div
, 0, 2 * src
->n_div
);
7044 for (i
= 0; i
< src
->n_div
; ++i
) {
7045 int j
= find_div(dst
, src
, i
);
7047 j
= isl_basic_map_alloc_div(dst
);
7050 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7051 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7052 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7056 isl_basic_map_swap_div(dst
, i
, j
);
7060 isl_basic_map_free(dst
);
7064 struct isl_basic_set
*isl_basic_set_align_divs(
7065 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7067 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7068 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7071 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7079 map
= isl_map_compute_divs(map
);
7080 map
= isl_map_cow(map
);
7084 for (i
= 1; i
< map
->n
; ++i
)
7085 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7086 for (i
= 1; i
< map
->n
; ++i
)
7087 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7089 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7093 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7095 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7098 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7099 __isl_take isl_map
*map
)
7103 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7104 map
= isl_map_intersect_domain(map
, set
);
7105 set
= isl_map_range(map
);
7113 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7114 __isl_take isl_map
*map
)
7116 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7119 /* There is no need to cow as removing empty parts doesn't change
7120 * the meaning of the set.
7122 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7129 for (i
= map
->n
-1; i
>= 0; --i
) {
7130 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7132 isl_basic_map_free(map
->p
[i
]);
7133 if (i
!= map
->n
-1) {
7134 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7135 map
->p
[i
] = map
->p
[map
->n
-1];
7143 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7145 return (struct isl_set
*)
7146 isl_map_remove_empty_parts((struct isl_map
*)set
);
7149 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7151 struct isl_basic_map
*bmap
;
7152 if (!map
|| map
->n
== 0)
7154 bmap
= map
->p
[map
->n
-1];
7155 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7156 return isl_basic_map_copy(bmap
);
7159 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7161 return (struct isl_basic_set
*)
7162 isl_map_copy_basic_map((struct isl_map
*)set
);
7165 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7166 __isl_keep isl_basic_map
*bmap
)
7172 for (i
= map
->n
-1; i
>= 0; --i
) {
7173 if (map
->p
[i
] != bmap
)
7175 map
= isl_map_cow(map
);
7178 isl_basic_map_free(map
->p
[i
]);
7179 if (i
!= map
->n
-1) {
7180 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7181 map
->p
[i
] = map
->p
[map
->n
-1];
7192 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7193 struct isl_basic_set
*bset
)
7195 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7196 (struct isl_basic_map
*)bset
);
7199 /* Given two basic sets bset1 and bset2, compute the maximal difference
7200 * between the values of dimension pos in bset1 and those in bset2
7201 * for any common value of the parameters and dimensions preceding pos.
7203 static enum isl_lp_result
basic_set_maximal_difference_at(
7204 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7205 int pos
, isl_int
*opt
)
7208 struct isl_basic_map
*bmap1
= NULL
;
7209 struct isl_basic_map
*bmap2
= NULL
;
7210 struct isl_ctx
*ctx
;
7211 struct isl_vec
*obj
;
7214 unsigned dim1
, dim2
;
7215 enum isl_lp_result res
;
7217 if (!bset1
|| !bset2
)
7218 return isl_lp_error
;
7220 nparam
= isl_basic_set_n_param(bset1
);
7221 dim1
= isl_basic_set_n_dim(bset1
);
7222 dim2
= isl_basic_set_n_dim(bset2
);
7223 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7224 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7225 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7226 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7227 if (!bmap1
|| !bmap2
)
7229 bmap1
= isl_basic_map_cow(bmap1
);
7230 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7231 pos
, (dim1
- pos
) + (dim2
- pos
),
7232 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7233 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7236 total
= isl_basic_map_total_dim(bmap1
);
7238 obj
= isl_vec_alloc(ctx
, 1 + total
);
7239 isl_seq_clr(obj
->block
.data
, 1 + total
);
7240 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7241 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7244 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7246 isl_basic_map_free(bmap1
);
7250 isl_basic_map_free(bmap1
);
7251 isl_basic_map_free(bmap2
);
7252 return isl_lp_error
;
7255 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7256 * for any common value of the parameters and dimensions preceding pos
7257 * in both basic sets, the values of dimension pos in bset1 are
7258 * smaller or larger than those in bset2.
7261 * 1 if bset1 follows bset2
7262 * -1 if bset1 precedes bset2
7263 * 0 if bset1 and bset2 are incomparable
7264 * -2 if some error occurred.
7266 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7267 struct isl_basic_set
*bset2
, int pos
)
7270 enum isl_lp_result res
;
7275 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7277 if (res
== isl_lp_empty
)
7279 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7280 res
== isl_lp_unbounded
)
7282 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7291 /* Given two basic sets bset1 and bset2, check whether
7292 * for any common value of the parameters and dimensions preceding pos
7293 * there is a value of dimension pos in bset1 that is larger
7294 * than a value of the same dimension in bset2.
7297 * 1 if there exists such a pair
7298 * 0 if there is no such pair, but there is a pair of equal values
7300 * -2 if some error occurred.
7302 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7303 __isl_keep isl_basic_set
*bset2
, int pos
)
7306 enum isl_lp_result res
;
7311 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7313 if (res
== isl_lp_empty
)
7315 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7316 res
== isl_lp_unbounded
)
7318 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7320 else if (res
== isl_lp_ok
)
7329 /* Given two sets set1 and set2, check whether
7330 * for any common value of the parameters and dimensions preceding pos
7331 * there is a value of dimension pos in set1 that is larger
7332 * than a value of the same dimension in set2.
7335 * 1 if there exists such a pair
7336 * 0 if there is no such pair, but there is a pair of equal values
7338 * -2 if some error occurred.
7340 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7341 __isl_keep isl_set
*set2
, int pos
)
7349 for (i
= 0; i
< set1
->n
; ++i
)
7350 for (j
= 0; j
< set2
->n
; ++j
) {
7352 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7353 if (f
== 1 || f
== -2)
7362 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7363 unsigned pos
, isl_int
*val
)
7371 total
= isl_basic_map_total_dim(bmap
);
7372 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7373 for (; d
+1 > pos
; --d
)
7374 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7378 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7380 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7382 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7385 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7391 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7392 unsigned pos
, isl_int
*val
)
7404 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7407 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7408 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7409 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7410 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7414 isl_int_set(*val
, v
);
7420 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7421 unsigned pos
, isl_int
*val
)
7423 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7427 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7430 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7433 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7434 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7436 if (pos
>= isl_basic_map_dim(bmap
, type
))
7438 return isl_basic_map_plain_has_fixed_var(bmap
,
7439 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7442 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7443 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7445 if (pos
>= isl_map_dim(map
, type
))
7447 return isl_map_plain_has_fixed_var(map
,
7448 map_offset(map
, type
) - 1 + pos
, val
);
7451 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7452 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7454 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7457 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7458 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7460 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7463 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7464 * then return this fixed value in *val.
7466 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7467 unsigned dim
, isl_int
*val
)
7469 return isl_basic_set_plain_has_fixed_var(bset
,
7470 isl_basic_set_n_param(bset
) + dim
, val
);
7473 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7474 * then return this fixed value in *val.
7476 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7477 unsigned dim
, isl_int
*val
)
7479 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7482 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7483 unsigned dim
, isl_int
*val
)
7485 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7488 /* Check if input variable in has fixed value and if so and if val is not NULL,
7489 * then return this fixed value in *val.
7491 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7492 unsigned in
, isl_int
*val
)
7494 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7497 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7498 * and if val is not NULL, then return this lower bound in *val.
7500 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7501 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7503 int i
, i_eq
= -1, i_ineq
= -1;
7510 total
= isl_basic_set_total_dim(bset
);
7511 nparam
= isl_basic_set_n_param(bset
);
7512 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7513 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7519 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7520 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7522 if (i_eq
!= -1 || i_ineq
!= -1)
7526 if (i_eq
== -1 && i_ineq
== -1)
7528 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7529 /* The coefficient should always be one due to normalization. */
7530 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7532 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7534 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7535 total
- nparam
- dim
- 1) != -1)
7538 isl_int_neg(*val
, c
[0]);
7542 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7543 unsigned dim
, isl_int
*val
)
7555 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7559 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7561 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7562 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7564 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7568 isl_int_set(*val
, v
);
7579 /* uset_gist depends on constraints without existentially quantified
7580 * variables sorting first.
7582 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7584 const struct constraint
*c1
= (const struct constraint
*)p1
;
7585 const struct constraint
*c2
= (const struct constraint
*)p2
;
7587 unsigned size
= isl_min(c1
->size
, c2
->size
);
7589 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7590 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7595 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7598 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7599 struct isl_basic_map
*bmap
)
7602 struct constraint
*c
;
7607 total
= isl_basic_map_total_dim(bmap
);
7608 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7611 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7613 c
[i
].c
= bmap
->ineq
[i
];
7615 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7616 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7617 bmap
->ineq
[i
] = c
[i
].c
;
7621 isl_basic_map_free(bmap
);
7625 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7626 __isl_take isl_basic_set
*bset
)
7628 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7629 (struct isl_basic_map
*)bset
);
7632 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7636 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7638 bmap
= isl_basic_map_remove_redundancies(bmap
);
7639 bmap
= isl_basic_map_sort_constraints(bmap
);
7640 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7644 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7646 return (struct isl_basic_set
*)isl_basic_map_normalize(
7647 (struct isl_basic_map
*)bset
);
7650 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7651 const __isl_keep isl_basic_map
*bmap2
)
7658 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7659 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7660 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7661 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7662 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7663 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7664 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7665 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7666 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7667 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7668 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7670 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7672 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7674 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7675 return bmap1
->n_eq
- bmap2
->n_eq
;
7676 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7677 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7678 if (bmap1
->n_div
!= bmap2
->n_div
)
7679 return bmap1
->n_div
- bmap2
->n_div
;
7680 total
= isl_basic_map_total_dim(bmap1
);
7681 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7682 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7686 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7687 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7691 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7692 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7699 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7700 const __isl_keep isl_basic_set
*bset2
)
7702 return isl_basic_map_plain_cmp(bset1
, bset2
);
7705 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7706 const __isl_keep isl_set
*set2
)
7712 if (set1
->n
!= set2
->n
)
7713 return set1
->n
- set2
->n
;
7715 for (i
= 0; i
< set1
->n
; ++i
) {
7716 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7724 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7725 __isl_keep isl_basic_map
*bmap2
)
7727 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7730 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7731 __isl_keep isl_basic_set
*bset2
)
7733 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7734 (isl_basic_map
*)bset2
);
7737 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7739 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7740 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7742 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7745 /* We normalize in place, but if anything goes wrong we need
7746 * to return NULL, so we need to make sure we don't change the
7747 * meaning of any possible other copies of map.
7749 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7752 struct isl_basic_map
*bmap
;
7756 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7758 for (i
= 0; i
< map
->n
; ++i
) {
7759 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7762 isl_basic_map_free(map
->p
[i
]);
7765 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7766 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7767 map
= isl_map_remove_empty_parts(map
);
7770 for (i
= map
->n
- 1; i
>= 1; --i
) {
7771 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7773 isl_basic_map_free(map
->p
[i
-1]);
7774 for (j
= i
; j
< map
->n
; ++j
)
7775 map
->p
[j
-1] = map
->p
[j
];
7785 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7787 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7790 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7800 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7803 map1
= isl_map_copy(map1
);
7804 map2
= isl_map_copy(map2
);
7805 map1
= isl_map_normalize(map1
);
7806 map2
= isl_map_normalize(map2
);
7809 equal
= map1
->n
== map2
->n
;
7810 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7811 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7824 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7826 return isl_map_plain_is_equal(map1
, map2
);
7829 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7831 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7832 (struct isl_map
*)set2
);
7835 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7837 return isl_set_plain_is_equal(set1
, set2
);
7840 /* Return an interval that ranges from min to max (inclusive)
7842 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7843 isl_int min
, isl_int max
)
7846 struct isl_basic_set
*bset
= NULL
;
7848 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7852 k
= isl_basic_set_alloc_inequality(bset
);
7855 isl_int_set_si(bset
->ineq
[k
][1], 1);
7856 isl_int_neg(bset
->ineq
[k
][0], min
);
7858 k
= isl_basic_set_alloc_inequality(bset
);
7861 isl_int_set_si(bset
->ineq
[k
][1], -1);
7862 isl_int_set(bset
->ineq
[k
][0], max
);
7866 isl_basic_set_free(bset
);
7870 /* Return the Cartesian product of the basic sets in list (in the given order).
7872 __isl_give isl_basic_set
*isl_basic_set_list_product(
7873 __isl_take
struct isl_basic_set_list
*list
)
7881 struct isl_basic_set
*product
= NULL
;
7885 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7886 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7887 nparam
= isl_basic_set_n_param(list
->p
[0]);
7888 dim
= isl_basic_set_n_dim(list
->p
[0]);
7889 extra
= list
->p
[0]->n_div
;
7890 n_eq
= list
->p
[0]->n_eq
;
7891 n_ineq
= list
->p
[0]->n_ineq
;
7892 for (i
= 1; i
< list
->n
; ++i
) {
7893 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7894 isl_assert(list
->ctx
,
7895 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7896 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7897 extra
+= list
->p
[i
]->n_div
;
7898 n_eq
+= list
->p
[i
]->n_eq
;
7899 n_ineq
+= list
->p
[i
]->n_ineq
;
7901 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7906 for (i
= 0; i
< list
->n
; ++i
) {
7907 isl_basic_set_add_constraints(product
,
7908 isl_basic_set_copy(list
->p
[i
]), dim
);
7909 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7911 isl_basic_set_list_free(list
);
7914 isl_basic_set_free(product
);
7915 isl_basic_set_list_free(list
);
7919 struct isl_basic_map
*isl_basic_map_product(
7920 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7922 isl_space
*dim_result
= NULL
;
7923 struct isl_basic_map
*bmap
;
7924 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7925 struct isl_dim_map
*dim_map1
, *dim_map2
;
7927 if (!bmap1
|| !bmap2
)
7930 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7931 bmap2
->dim
, isl_dim_param
), goto error
);
7932 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7933 isl_space_copy(bmap2
->dim
));
7935 in1
= isl_basic_map_n_in(bmap1
);
7936 in2
= isl_basic_map_n_in(bmap2
);
7937 out1
= isl_basic_map_n_out(bmap1
);
7938 out2
= isl_basic_map_n_out(bmap2
);
7939 nparam
= isl_basic_map_n_param(bmap1
);
7941 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7942 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7943 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7944 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7945 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7946 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7947 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7948 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7949 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7950 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7951 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7953 bmap
= isl_basic_map_alloc_space(dim_result
,
7954 bmap1
->n_div
+ bmap2
->n_div
,
7955 bmap1
->n_eq
+ bmap2
->n_eq
,
7956 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7957 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7958 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7959 bmap
= isl_basic_map_simplify(bmap
);
7960 return isl_basic_map_finalize(bmap
);
7962 isl_basic_map_free(bmap1
);
7963 isl_basic_map_free(bmap2
);
7967 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7968 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7970 isl_basic_map
*prod
;
7972 prod
= isl_basic_map_product(bmap1
, bmap2
);
7973 prod
= isl_basic_map_flatten(prod
);
7977 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7978 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7980 return isl_basic_map_flat_range_product(bset1
, bset2
);
7983 __isl_give isl_basic_map
*isl_basic_map_domain_product(
7984 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7986 isl_space
*space_result
= NULL
;
7987 isl_basic_map
*bmap
;
7988 unsigned in1
, in2
, out
, nparam
, total
, pos
;
7989 struct isl_dim_map
*dim_map1
, *dim_map2
;
7991 if (!bmap1
|| !bmap2
)
7994 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
7995 isl_space_copy(bmap2
->dim
));
7997 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7998 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
7999 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8000 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8002 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8003 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8004 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8005 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8006 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8007 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8008 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8009 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8010 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8011 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8012 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8014 bmap
= isl_basic_map_alloc_space(space_result
,
8015 bmap1
->n_div
+ bmap2
->n_div
,
8016 bmap1
->n_eq
+ bmap2
->n_eq
,
8017 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8018 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8019 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8020 bmap
= isl_basic_map_simplify(bmap
);
8021 return isl_basic_map_finalize(bmap
);
8023 isl_basic_map_free(bmap1
);
8024 isl_basic_map_free(bmap2
);
8028 __isl_give isl_basic_map
*isl_basic_map_range_product(
8029 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8031 isl_space
*dim_result
= NULL
;
8032 isl_basic_map
*bmap
;
8033 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8034 struct isl_dim_map
*dim_map1
, *dim_map2
;
8036 if (!bmap1
|| !bmap2
)
8039 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8040 isl_space_copy(bmap2
->dim
));
8042 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8043 out1
= isl_basic_map_n_out(bmap1
);
8044 out2
= isl_basic_map_n_out(bmap2
);
8045 nparam
= isl_basic_map_n_param(bmap1
);
8047 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8048 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8049 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8050 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8051 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8052 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8053 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8054 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8055 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8056 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8057 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8059 bmap
= isl_basic_map_alloc_space(dim_result
,
8060 bmap1
->n_div
+ bmap2
->n_div
,
8061 bmap1
->n_eq
+ bmap2
->n_eq
,
8062 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8063 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8064 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8065 bmap
= isl_basic_map_simplify(bmap
);
8066 return isl_basic_map_finalize(bmap
);
8068 isl_basic_map_free(bmap1
);
8069 isl_basic_map_free(bmap2
);
8073 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8074 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8076 isl_basic_map
*prod
;
8078 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8079 prod
= isl_basic_map_flatten_range(prod
);
8083 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8084 __isl_take isl_map
*map2
,
8085 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8086 __isl_take isl_space
*right
),
8087 __isl_give isl_basic_map
*(*basic_map_product
)(
8088 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8091 struct isl_map
*result
;
8097 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8098 map2
->dim
, isl_dim_param
), goto error
);
8100 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8101 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8102 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8104 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8105 isl_space_copy(map2
->dim
)),
8106 map1
->n
* map2
->n
, flags
);
8109 for (i
= 0; i
< map1
->n
; ++i
)
8110 for (j
= 0; j
< map2
->n
; ++j
) {
8111 struct isl_basic_map
*part
;
8112 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8113 isl_basic_map_copy(map2
->p
[j
]));
8114 if (isl_basic_map_is_empty(part
))
8115 isl_basic_map_free(part
);
8117 result
= isl_map_add_basic_map(result
, part
);
8130 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8132 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8133 __isl_take isl_map
*map2
)
8135 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8138 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8139 __isl_take isl_map
*map2
)
8141 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8144 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8146 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8147 __isl_take isl_map
*map2
)
8151 prod
= isl_map_product(map1
, map2
);
8152 prod
= isl_map_flatten(prod
);
8156 /* Given two set A and B, construct its Cartesian product A x B.
8158 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8160 return isl_map_range_product(set1
, set2
);
8163 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8164 __isl_take isl_set
*set2
)
8166 return isl_map_flat_range_product(set1
, set2
);
8169 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8171 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8172 __isl_take isl_map
*map2
)
8174 return map_product(map1
, map2
, &isl_space_domain_product
,
8175 &isl_basic_map_domain_product
);
8178 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8180 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8181 __isl_take isl_map
*map2
)
8183 return map_product(map1
, map2
, &isl_space_range_product
,
8184 &isl_basic_map_range_product
);
8187 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8188 __isl_take isl_map
*map2
)
8190 return isl_map_align_params_map_map_and(map1
, map2
,
8191 &map_domain_product_aligned
);
8194 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8195 __isl_take isl_map
*map2
)
8197 return isl_map_align_params_map_map_and(map1
, map2
,
8198 &map_range_product_aligned
);
8201 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8203 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8204 __isl_take isl_map
*map2
)
8208 prod
= isl_map_domain_product(map1
, map2
);
8209 prod
= isl_map_flatten_domain(prod
);
8213 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8215 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8216 __isl_take isl_map
*map2
)
8220 prod
= isl_map_range_product(map1
, map2
);
8221 prod
= isl_map_flatten_range(prod
);
8225 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8228 uint32_t hash
= isl_hash_init();
8233 bmap
= isl_basic_map_copy(bmap
);
8234 bmap
= isl_basic_map_normalize(bmap
);
8237 total
= isl_basic_map_total_dim(bmap
);
8238 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8239 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8241 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8242 isl_hash_hash(hash
, c_hash
);
8244 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8245 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8247 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8248 isl_hash_hash(hash
, c_hash
);
8250 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8251 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8253 if (isl_int_is_zero(bmap
->div
[i
][0]))
8255 isl_hash_byte(hash
, i
& 0xFF);
8256 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8257 isl_hash_hash(hash
, c_hash
);
8259 isl_basic_map_free(bmap
);
8263 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8265 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8268 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8275 map
= isl_map_copy(map
);
8276 map
= isl_map_normalize(map
);
8280 hash
= isl_hash_init();
8281 for (i
= 0; i
< map
->n
; ++i
) {
8283 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8284 isl_hash_hash(hash
, bmap_hash
);
8292 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8294 return isl_map_get_hash((isl_map
*)set
);
8297 /* Check if the value for dimension dim is completely determined
8298 * by the values of the other parameters and variables.
8299 * That is, check if dimension dim is involved in an equality.
8301 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8308 nparam
= isl_basic_set_n_param(bset
);
8309 for (i
= 0; i
< bset
->n_eq
; ++i
)
8310 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8315 /* Check if the value for dimension dim is completely determined
8316 * by the values of the other parameters and variables.
8317 * That is, check if dimension dim is involved in an equality
8318 * for each of the subsets.
8320 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8326 for (i
= 0; i
< set
->n
; ++i
) {
8328 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8335 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8337 return set
? set
->n
: 0;
8340 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8341 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8348 for (i
= 0; i
< map
->n
; ++i
)
8349 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8355 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8356 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8363 for (i
= 0; i
< set
->n
; ++i
)
8364 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8370 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8377 bset
= isl_basic_set_cow(bset
);
8381 dim
= isl_basic_set_get_space(bset
);
8382 dim
= isl_space_lift(dim
, bset
->n_div
);
8385 isl_space_free(bset
->dim
);
8387 bset
->extra
-= bset
->n_div
;
8390 bset
= isl_basic_set_finalize(bset
);
8394 isl_basic_set_free(bset
);
8398 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8404 set
= isl_set_align_divs(set
);
8409 set
= isl_set_cow(set
);
8413 n_div
= set
->p
[0]->n_div
;
8414 dim
= isl_set_get_space(set
);
8415 dim
= isl_space_lift(dim
, n_div
);
8418 isl_space_free(set
->dim
);
8421 for (i
= 0; i
< set
->n
; ++i
) {
8422 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8433 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8436 struct isl_basic_map
*bmap
;
8443 set
= isl_set_align_divs(set
);
8448 dim
= isl_set_get_space(set
);
8449 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8451 return isl_map_identity(isl_space_map_from_set(dim
));
8454 n_div
= set
->p
[0]->n_div
;
8455 dim
= isl_space_map_from_set(dim
);
8456 n_param
= isl_space_dim(dim
, isl_dim_param
);
8457 n_set
= isl_space_dim(dim
, isl_dim_in
);
8458 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8459 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8460 for (i
= 0; i
< n_set
; ++i
)
8461 bmap
= var_equal(bmap
, i
);
8463 total
= n_param
+ n_set
+ n_set
+ n_div
;
8464 for (i
= 0; i
< n_div
; ++i
) {
8465 k
= isl_basic_map_alloc_inequality(bmap
);
8468 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8469 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8470 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8471 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8472 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8473 set
->p
[0]->div
[i
][0]);
8475 l
= isl_basic_map_alloc_inequality(bmap
);
8478 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8479 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8480 set
->p
[0]->div
[i
][0]);
8481 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8485 bmap
= isl_basic_map_simplify(bmap
);
8486 bmap
= isl_basic_map_finalize(bmap
);
8487 return isl_map_from_basic_map(bmap
);
8490 isl_basic_map_free(bmap
);
8494 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8502 dim
= isl_basic_set_total_dim(bset
);
8503 size
+= bset
->n_eq
* (1 + dim
);
8504 size
+= bset
->n_ineq
* (1 + dim
);
8505 size
+= bset
->n_div
* (2 + dim
);
8510 int isl_set_size(__isl_keep isl_set
*set
)
8518 for (i
= 0; i
< set
->n
; ++i
)
8519 size
+= isl_basic_set_size(set
->p
[i
]);
8524 /* Check if there is any lower bound (if lower == 0) and/or upper
8525 * bound (if upper == 0) on the specified dim.
8527 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8528 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8535 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8537 pos
+= isl_basic_map_offset(bmap
, type
);
8539 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8540 if (isl_int_is_zero(bmap
->div
[i
][0]))
8542 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8546 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8547 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8550 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8551 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8558 return lower
&& upper
;
8561 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8562 enum isl_dim_type type
, unsigned pos
)
8564 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8567 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8568 enum isl_dim_type type
, unsigned pos
)
8570 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8573 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8574 enum isl_dim_type type
, unsigned pos
)
8576 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8579 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8580 enum isl_dim_type type
, unsigned pos
)
8587 for (i
= 0; i
< map
->n
; ++i
) {
8589 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8590 if (bounded
< 0 || !bounded
)
8597 /* Return 1 if the specified dim is involved in both an upper bound
8598 * and a lower bound.
8600 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8601 enum isl_dim_type type
, unsigned pos
)
8603 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8606 static int has_bound(__isl_keep isl_map
*map
,
8607 enum isl_dim_type type
, unsigned pos
,
8608 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8609 enum isl_dim_type type
, unsigned pos
))
8616 for (i
= 0; i
< map
->n
; ++i
) {
8618 bounded
= fn(map
->p
[i
], type
, pos
);
8619 if (bounded
< 0 || bounded
)
8626 /* Return 1 if the specified dim is involved in any lower bound.
8628 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8629 enum isl_dim_type type
, unsigned pos
)
8631 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8634 /* Return 1 if the specified dim is involved in any upper bound.
8636 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8637 enum isl_dim_type type
, unsigned pos
)
8639 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8642 /* For each of the "n" variables starting at "first", determine
8643 * the sign of the variable and put the results in the first "n"
8644 * elements of the array "signs".
8646 * 1 means that the variable is non-negative
8647 * -1 means that the variable is non-positive
8648 * 0 means the variable attains both positive and negative values.
8650 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8651 unsigned first
, unsigned n
, int *signs
)
8653 isl_vec
*bound
= NULL
;
8654 struct isl_tab
*tab
= NULL
;
8655 struct isl_tab_undo
*snap
;
8658 if (!bset
|| !signs
)
8661 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8662 tab
= isl_tab_from_basic_set(bset
);
8666 isl_seq_clr(bound
->el
, bound
->size
);
8667 isl_int_set_si(bound
->el
[0], -1);
8669 snap
= isl_tab_snap(tab
);
8670 for (i
= 0; i
< n
; ++i
) {
8673 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8674 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8677 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8678 if (isl_tab_rollback(tab
, snap
) < 0)
8686 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8687 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8690 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8691 if (isl_tab_rollback(tab
, snap
) < 0)
8694 signs
[i
] = empty
? -1 : 0;
8698 isl_vec_free(bound
);
8702 isl_vec_free(bound
);
8706 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8707 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8709 if (!bset
|| !signs
)
8711 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8714 first
+= pos(bset
->dim
, type
) - 1;
8715 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8718 /* Check if the given basic map is obviously single-valued.
8719 * In particular, for each output dimension, check that there is
8720 * an equality that defines the output dimension in terms of
8721 * earlier dimensions.
8723 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8733 total
= 1 + isl_basic_map_total_dim(bmap
);
8734 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8735 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8737 for (i
= 0; i
< n_out
; ++i
) {
8738 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8739 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8741 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8742 total
- (o_out
+ i
+ 1)) == -1)
8745 if (j
>= bmap
->n_eq
)
8752 /* Check if the given map is obviously single-valued.
8754 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8763 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8766 /* Check if the given map is single-valued.
8771 * and check if the result is a subset of the identity mapping.
8773 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8780 sv
= isl_map_plain_is_single_valued(map
);
8784 test
= isl_map_reverse(isl_map_copy(map
));
8785 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8787 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8788 id
= isl_map_identity(dim
);
8790 sv
= isl_map_is_subset(test
, id
);
8798 int isl_map_is_injective(__isl_keep isl_map
*map
)
8802 map
= isl_map_copy(map
);
8803 map
= isl_map_reverse(map
);
8804 in
= isl_map_is_single_valued(map
);
8810 /* Check if the given map is obviously injective.
8812 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8816 map
= isl_map_copy(map
);
8817 map
= isl_map_reverse(map
);
8818 in
= isl_map_plain_is_single_valued(map
);
8824 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8828 sv
= isl_map_is_single_valued(map
);
8832 return isl_map_is_injective(map
);
8835 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8837 return isl_map_is_single_valued((isl_map
*)set
);
8840 int isl_map_is_translation(__isl_keep isl_map
*map
)
8845 delta
= isl_map_deltas(isl_map_copy(map
));
8846 ok
= isl_set_is_singleton(delta
);
8847 isl_set_free(delta
);
8852 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8854 if (isl_seq_first_non_zero(p
, pos
) != -1)
8856 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8861 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8870 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8873 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8874 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8875 for (j
= 0; j
< nvar
; ++j
) {
8876 int lower
= 0, upper
= 0;
8877 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8878 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8880 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8886 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8887 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8889 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8891 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8896 if (!lower
|| !upper
)
8903 int isl_set_is_box(__isl_keep isl_set
*set
)
8910 return isl_basic_set_is_box(set
->p
[0]);
8913 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8918 return isl_space_is_wrapping(bset
->dim
);
8921 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8926 return isl_space_is_wrapping(set
->dim
);
8929 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8931 bmap
= isl_basic_map_cow(bmap
);
8935 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8939 bmap
= isl_basic_map_finalize(bmap
);
8941 return (isl_basic_set
*)bmap
;
8943 isl_basic_map_free(bmap
);
8947 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8951 map
= isl_map_cow(map
);
8955 for (i
= 0; i
< map
->n
; ++i
) {
8956 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8960 map
->dim
= isl_space_wrap(map
->dim
);
8964 return (isl_set
*)map
;
8970 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8972 bset
= isl_basic_set_cow(bset
);
8976 bset
->dim
= isl_space_unwrap(bset
->dim
);
8980 bset
= isl_basic_set_finalize(bset
);
8982 return (isl_basic_map
*)bset
;
8984 isl_basic_set_free(bset
);
8988 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8995 if (!isl_set_is_wrapping(set
))
8996 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8999 set
= isl_set_cow(set
);
9003 for (i
= 0; i
< set
->n
; ++i
) {
9004 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9009 set
->dim
= isl_space_unwrap(set
->dim
);
9013 return (isl_map
*)set
;
9019 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9020 enum isl_dim_type type
)
9025 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9028 bmap
= isl_basic_map_cow(bmap
);
9032 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9036 bmap
= isl_basic_map_finalize(bmap
);
9040 isl_basic_map_free(bmap
);
9044 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9045 enum isl_dim_type type
)
9052 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9055 map
= isl_map_cow(map
);
9059 for (i
= 0; i
< map
->n
; ++i
) {
9060 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9064 map
->dim
= isl_space_reset(map
->dim
, type
);
9074 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9079 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9082 bmap
= isl_basic_map_cow(bmap
);
9086 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9090 bmap
= isl_basic_map_finalize(bmap
);
9094 isl_basic_map_free(bmap
);
9098 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9100 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9103 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9104 __isl_take isl_basic_map
*bmap
)
9109 if (!bmap
->dim
->nested
[0])
9112 bmap
= isl_basic_map_cow(bmap
);
9116 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9120 bmap
= isl_basic_map_finalize(bmap
);
9124 isl_basic_map_free(bmap
);
9128 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9129 __isl_take isl_basic_map
*bmap
)
9134 if (!bmap
->dim
->nested
[1])
9137 bmap
= isl_basic_map_cow(bmap
);
9141 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9145 bmap
= isl_basic_map_finalize(bmap
);
9149 isl_basic_map_free(bmap
);
9153 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9160 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9163 map
= isl_map_cow(map
);
9167 for (i
= 0; i
< map
->n
; ++i
) {
9168 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9172 map
->dim
= isl_space_flatten(map
->dim
);
9182 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9184 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9187 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9189 isl_space
*dim
, *flat_dim
;
9192 dim
= isl_set_get_space(set
);
9193 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9194 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9195 map
= isl_map_intersect_domain(map
, set
);
9200 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9207 if (!map
->dim
->nested
[0])
9210 map
= isl_map_cow(map
);
9214 for (i
= 0; i
< map
->n
; ++i
) {
9215 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9219 map
->dim
= isl_space_flatten_domain(map
->dim
);
9229 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9236 if (!map
->dim
->nested
[1])
9239 map
= isl_map_cow(map
);
9243 for (i
= 0; i
< map
->n
; ++i
) {
9244 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9248 map
->dim
= isl_space_flatten_range(map
->dim
);
9258 /* Reorder the dimensions of "bmap" according to the given dim_map
9259 * and set the dimension specification to "dim".
9261 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9262 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9266 bmap
= isl_basic_map_cow(bmap
);
9267 if (!bmap
|| !dim
|| !dim_map
)
9270 res
= isl_basic_map_alloc_space(dim
,
9271 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9272 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9273 res
= isl_basic_map_finalize(res
);
9277 isl_basic_map_free(bmap
);
9278 isl_space_free(dim
);
9282 /* Reorder the dimensions of "map" according to given reordering.
9284 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9285 __isl_take isl_reordering
*r
)
9288 struct isl_dim_map
*dim_map
;
9290 map
= isl_map_cow(map
);
9291 dim_map
= isl_dim_map_from_reordering(r
);
9292 if (!map
|| !r
|| !dim_map
)
9295 for (i
= 0; i
< map
->n
; ++i
) {
9296 struct isl_dim_map
*dim_map_i
;
9298 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9300 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9301 isl_space_copy(r
->dim
), dim_map_i
);
9307 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9309 isl_reordering_free(r
);
9315 isl_reordering_free(r
);
9319 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9320 __isl_take isl_reordering
*r
)
9322 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9325 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9326 __isl_take isl_space
*model
)
9333 ctx
= isl_space_get_ctx(model
);
9334 if (!isl_space_has_named_params(model
))
9335 isl_die(ctx
, isl_error_invalid
,
9336 "model has unnamed parameters", goto error
);
9337 if (!isl_space_has_named_params(map
->dim
))
9338 isl_die(ctx
, isl_error_invalid
,
9339 "relation has unnamed parameters", goto error
);
9340 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9341 isl_reordering
*exp
;
9343 model
= isl_space_drop_dims(model
, isl_dim_in
,
9344 0, isl_space_dim(model
, isl_dim_in
));
9345 model
= isl_space_drop_dims(model
, isl_dim_out
,
9346 0, isl_space_dim(model
, isl_dim_out
));
9347 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9348 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9349 map
= isl_map_realign(map
, exp
);
9352 isl_space_free(model
);
9355 isl_space_free(model
);
9360 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9361 __isl_take isl_space
*model
)
9363 return isl_map_align_params(set
, model
);
9366 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9367 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9368 enum isl_dim_type c2
, enum isl_dim_type c3
,
9369 enum isl_dim_type c4
, enum isl_dim_type c5
)
9371 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9372 struct isl_mat
*mat
;
9378 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9379 isl_basic_map_total_dim(bmap
) + 1);
9382 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9383 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9384 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9385 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9386 isl_int_set(mat
->row
[i
][pos
],
9387 bmap
->eq
[i
][off
+ k
]);
9395 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9396 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9397 enum isl_dim_type c2
, enum isl_dim_type c3
,
9398 enum isl_dim_type c4
, enum isl_dim_type c5
)
9400 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9401 struct isl_mat
*mat
;
9407 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9408 isl_basic_map_total_dim(bmap
) + 1);
9411 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9412 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9413 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9414 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9415 isl_int_set(mat
->row
[i
][pos
],
9416 bmap
->ineq
[i
][off
+ k
]);
9424 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9425 __isl_take isl_space
*dim
,
9426 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9427 enum isl_dim_type c2
, enum isl_dim_type c3
,
9428 enum isl_dim_type c4
, enum isl_dim_type c5
)
9430 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9431 isl_basic_map
*bmap
;
9437 if (!dim
|| !eq
|| !ineq
)
9440 if (eq
->n_col
!= ineq
->n_col
)
9441 isl_die(dim
->ctx
, isl_error_invalid
,
9442 "equalities and inequalities matrices should have "
9443 "same number of columns", goto error
);
9445 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9447 if (eq
->n_col
< total
)
9448 isl_die(dim
->ctx
, isl_error_invalid
,
9449 "number of columns too small", goto error
);
9451 extra
= eq
->n_col
- total
;
9453 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9454 eq
->n_row
, ineq
->n_row
);
9457 for (i
= 0; i
< extra
; ++i
) {
9458 k
= isl_basic_map_alloc_div(bmap
);
9461 isl_int_set_si(bmap
->div
[k
][0], 0);
9463 for (i
= 0; i
< eq
->n_row
; ++i
) {
9464 l
= isl_basic_map_alloc_equality(bmap
);
9467 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9468 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9469 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9470 isl_int_set(bmap
->eq
[l
][off
+ k
],
9476 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9477 l
= isl_basic_map_alloc_inequality(bmap
);
9480 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9481 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9482 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9483 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9490 isl_space_free(dim
);
9496 isl_space_free(dim
);
9502 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9503 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9504 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9506 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9507 c1
, c2
, c3
, c4
, isl_dim_in
);
9510 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9511 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9512 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9514 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9515 c1
, c2
, c3
, c4
, isl_dim_in
);
9518 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9519 __isl_take isl_space
*dim
,
9520 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, 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_set
*)
9524 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9525 c1
, c2
, c3
, c4
, isl_dim_in
);
9528 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9533 return isl_space_can_zip(bmap
->dim
);
9536 int isl_map_can_zip(__isl_keep isl_map
*map
)
9541 return isl_space_can_zip(map
->dim
);
9544 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9545 * (A -> C) -> (B -> D).
9547 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9556 if (!isl_basic_map_can_zip(bmap
))
9557 isl_die(bmap
->ctx
, isl_error_invalid
,
9558 "basic map cannot be zipped", goto error
);
9559 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9560 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9561 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9562 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9563 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9566 bmap
->dim
= isl_space_zip(bmap
->dim
);
9571 isl_basic_map_free(bmap
);
9575 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9576 * (A -> C) -> (B -> D).
9578 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9585 if (!isl_map_can_zip(map
))
9586 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9589 map
= isl_map_cow(map
);
9593 for (i
= 0; i
< map
->n
; ++i
) {
9594 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9599 map
->dim
= isl_space_zip(map
->dim
);
9609 /* Construct a basic map mapping the domain of the affine expression
9610 * to a one-dimensional range prescribed by the affine expression.
9612 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9616 isl_local_space
*ls
;
9617 isl_basic_map
*bmap
;
9622 ls
= isl_aff_get_local_space(aff
);
9623 bmap
= isl_basic_map_from_local_space(ls
);
9624 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9625 k
= isl_basic_map_alloc_equality(bmap
);
9629 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9630 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9631 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9632 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9633 aff
->v
->size
- (pos
+ 1));
9636 bmap
= isl_basic_map_finalize(bmap
);
9640 isl_basic_map_free(bmap
);
9644 /* Construct a basic map mapping the domain the multi-affine expression
9645 * to its range, with each dimension in the range equated to the
9646 * corresponding affine expression.
9648 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9649 __isl_take isl_multi_aff
*maff
)
9653 isl_basic_map
*bmap
;
9658 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9659 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9660 "invalid space", return isl_multi_aff_free(maff
));
9662 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9663 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9665 for (i
= 0; i
< maff
->n
; ++i
) {
9667 isl_basic_map
*bmap_i
;
9669 aff
= isl_aff_copy(maff
->p
[i
]);
9670 bmap_i
= isl_basic_map_from_aff(aff
);
9672 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9675 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9677 isl_multi_aff_free(maff
);
9681 /* Construct a basic map mapping a domain in the given space to
9682 * to an n-dimensional range, with n the number of elements in the list,
9683 * where each coordinate in the range is prescribed by the
9684 * corresponding affine expression.
9685 * The domains of all affine expressions in the list are assumed to match
9688 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9689 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9693 isl_basic_map
*bmap
;
9698 dim
= isl_space_from_domain(domain_dim
);
9699 bmap
= isl_basic_map_universe(dim
);
9701 for (i
= 0; i
< list
->n
; ++i
) {
9703 isl_basic_map
*bmap_i
;
9705 aff
= isl_aff_copy(list
->p
[i
]);
9706 bmap_i
= isl_basic_map_from_aff(aff
);
9708 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9711 isl_aff_list_free(list
);
9715 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9716 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9718 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9721 /* Add a constraint imposing that the given two dimensions are equal.
9723 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9724 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9726 isl_basic_map
*bmap
= NULL
;
9732 if (pos1
>= isl_map_dim(map
, type1
))
9733 isl_die(map
->ctx
, isl_error_invalid
,
9734 "index out of bounds", goto error
);
9735 if (pos2
>= isl_map_dim(map
, type2
))
9736 isl_die(map
->ctx
, isl_error_invalid
,
9737 "index out of bounds", goto error
);
9739 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9740 i
= isl_basic_map_alloc_equality(bmap
);
9743 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9744 pos1
+= isl_basic_map_offset(bmap
, type1
);
9745 pos2
+= isl_basic_map_offset(bmap
, type2
);
9746 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9747 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9748 bmap
= isl_basic_map_finalize(bmap
);
9750 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9754 isl_basic_map_free(bmap
);
9759 /* Add a constraint imposing that the given two dimensions have opposite values.
9761 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9762 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9764 isl_basic_map
*bmap
= NULL
;
9770 if (pos1
>= isl_map_dim(map
, type1
))
9771 isl_die(map
->ctx
, isl_error_invalid
,
9772 "index out of bounds", goto error
);
9773 if (pos2
>= isl_map_dim(map
, type2
))
9774 isl_die(map
->ctx
, isl_error_invalid
,
9775 "index out of bounds", goto error
);
9777 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9778 i
= isl_basic_map_alloc_equality(bmap
);
9781 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9782 pos1
+= isl_basic_map_offset(bmap
, type1
);
9783 pos2
+= isl_basic_map_offset(bmap
, type2
);
9784 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9785 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9786 bmap
= isl_basic_map_finalize(bmap
);
9788 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9792 isl_basic_map_free(bmap
);
9797 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9801 isl_local_space
*ls
;
9806 if (!isl_basic_map_divs_known(bmap
))
9807 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9808 "some divs are unknown", return NULL
);
9810 ls
= isl_basic_map_get_local_space(bmap
);
9811 div
= isl_local_space_get_div(ls
, pos
);
9812 isl_local_space_free(ls
);
9817 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9820 return isl_basic_map_get_div(bset
, pos
);
9823 /* Plug in "subs" for dimension "type", "pos" of "bset".
9825 * Let i be the dimension to replace and let "subs" be of the form
9829 * Any integer division with a non-zero coefficient for i,
9831 * floor((a i + g)/m)
9835 * floor((a f + d g)/(m d))
9837 * Constraints of the form
9845 __isl_give isl_basic_set
*isl_basic_set_substitute(
9846 __isl_take isl_basic_set
*bset
,
9847 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9853 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9856 bset
= isl_basic_set_cow(bset
);
9860 ctx
= isl_basic_set_get_ctx(bset
);
9861 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9862 isl_die(ctx
, isl_error_invalid
,
9863 "spaces don't match", goto error
);
9864 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9865 isl_die(ctx
, isl_error_unsupported
,
9866 "cannot handle divs yet", goto error
);
9868 pos
+= isl_basic_set_offset(bset
, type
);
9872 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9873 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9875 isl_int_set(v
, bset
->eq
[i
][pos
]);
9876 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9877 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9878 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9881 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9882 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9884 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9885 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9886 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9887 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9890 for (i
= 0; i
< bset
->n_div
; ++i
) {
9891 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9893 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9894 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9895 isl_seq_combine(bset
->div
[i
] + 1,
9896 subs
->v
->el
[0], bset
->div
[i
] + 1,
9897 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9898 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9903 bset
= isl_basic_set_simplify(bset
);
9904 return isl_basic_set_finalize(bset
);
9906 isl_basic_set_free(bset
);
9910 /* Plug in "subs" for dimension "type", "pos" of "set".
9912 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9913 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9917 if (set
&& isl_set_plain_is_empty(set
))
9920 set
= isl_set_cow(set
);
9924 for (i
= set
->n
- 1; i
>= 0; --i
) {
9925 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9928 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
9929 isl_basic_set_free(set
->p
[i
]);
9930 if (i
!= set
->n
- 1)
9931 set
->p
[i
] = set
->p
[set
->n
- 1];