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
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
30 /* Maps dst positions to src positions */
36 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
39 struct isl_dim_map
*dim_map
;
40 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
41 sizeof(struct isl_dim_map
) + len
* sizeof(int));
44 dim_map
->len
= 1 + len
;
46 for (i
= 0; i
< len
; ++i
)
47 dim_map
->pos
[1 + i
] = -1;
51 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
54 case isl_dim_param
: return dim
->nparam
;
55 case isl_dim_in
: return dim
->n_in
;
56 case isl_dim_out
: return dim
->n_out
;
57 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
62 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
65 case isl_dim_param
: return 1;
66 case isl_dim_in
: return 1 + dim
->nparam
;
67 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
72 static void isl_dim_map_dim_range(struct isl_dim_map
*dim_map
,
73 struct isl_dim
*dim
, enum isl_dim_type type
,
74 unsigned first
, unsigned n
, unsigned dst_pos
)
82 src_pos
= pos(dim
, type
);
83 for (i
= 0; i
< n
; ++i
)
84 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
87 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
88 enum isl_dim_type type
, unsigned dst_pos
)
90 isl_dim_map_dim_range(dim_map
, dim
, type
, 0, n(dim
, type
), dst_pos
);
93 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
94 struct isl_basic_map
*bmap
, unsigned dst_pos
)
99 if (!dim_map
|| !bmap
)
102 src_pos
= 1 + isl_dim_total(bmap
->dim
);
103 for (i
= 0; i
< bmap
->n_div
; ++i
)
104 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
107 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
111 for (i
= 0; i
< dim_map
->len
; ++i
)
112 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
113 fprintf(stderr
, "\n");
116 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
117 enum isl_dim_type type
)
122 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
123 case isl_dim_div
: return bmap
->n_div
;
124 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
129 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
131 return map
? n(map
->dim
, type
) : 0;
134 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
136 return set
? n(set
->dim
, type
) : 0;
139 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
140 enum isl_dim_type type
)
142 struct isl_dim
*dim
= bmap
->dim
;
144 case isl_dim_param
: return 1;
145 case isl_dim_in
: return 1 + dim
->nparam
;
146 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
147 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
152 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
154 return pos(map
->dim
, type
);
157 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
158 enum isl_dim_type type
)
160 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
163 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
165 return bset
->dim
->n_out
;
168 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
170 return bset
->dim
->nparam
;
173 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
175 return isl_dim_total(bset
->dim
) + bset
->n_div
;
178 unsigned isl_set_n_dim(const struct isl_set
*set
)
180 return set
->dim
->n_out
;
183 unsigned isl_set_n_param(const struct isl_set
*set
)
185 return set
->dim
->nparam
;
188 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
190 return bmap
->dim
->n_in
;
193 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
195 return bmap
->dim
->n_out
;
198 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
200 return bmap
->dim
->nparam
;
203 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
208 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
210 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
213 unsigned isl_map_n_in(const struct isl_map
*map
)
215 return map
->dim
->n_in
;
218 unsigned isl_map_n_out(const struct isl_map
*map
)
220 return map
->dim
->n_out
;
223 unsigned isl_map_n_param(const struct isl_map
*map
)
225 return map
->dim
->nparam
;
228 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
230 return map
->dim
->n_in
== set
->dim
->n_out
&&
231 map
->dim
->nparam
== set
->dim
->nparam
;
234 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
235 struct isl_basic_set
*bset
)
237 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
238 bmap
->dim
->nparam
== bset
->dim
->nparam
;
241 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
242 struct isl_basic_set
*bset
)
244 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
245 bmap
->dim
->nparam
== bset
->dim
->nparam
;
248 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
252 return isl_dim_copy(bmap
->dim
);
255 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
259 return isl_dim_copy(bset
->dim
);
262 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
266 return isl_dim_copy(map
->dim
);
269 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
273 return isl_dim_copy(set
->dim
);
276 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
277 __isl_take isl_basic_map
*bmap
,
278 enum isl_dim_type type
, unsigned pos
, const char *s
)
282 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
287 isl_basic_map_free(bmap
);
291 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
292 enum isl_dim_type type
, unsigned pos
, const char *s
)
299 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
303 for (i
= 0; i
< map
->n
; ++i
) {
304 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
315 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
316 __isl_take isl_basic_set
*bset
,
317 enum isl_dim_type type
, unsigned pos
, const char *s
)
319 return (isl_basic_set
*)isl_basic_map_set_dim_name(
320 (isl_basic_map
*)bset
, type
, pos
, s
);
323 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
324 enum isl_dim_type type
, unsigned pos
, const char *s
)
326 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
329 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
333 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
336 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
337 struct isl_basic_map
*bmap
, unsigned extra
,
338 unsigned n_eq
, unsigned n_ineq
)
341 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
346 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
347 if (isl_blk_is_error(bmap
->block
))
350 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
355 bmap
->block2
= isl_blk_empty();
358 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
359 if (isl_blk_is_error(bmap
->block2
))
362 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
367 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
368 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
370 for (i
= 0; i
< extra
; ++i
)
371 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
375 bmap
->c_size
= n_eq
+ n_ineq
;
376 bmap
->eq
= bmap
->ineq
+ n_ineq
;
385 isl_basic_map_free(bmap
);
389 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
390 unsigned nparam
, unsigned dim
, unsigned extra
,
391 unsigned n_eq
, unsigned n_ineq
)
393 struct isl_basic_map
*bmap
;
394 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
395 return (struct isl_basic_set
*)bmap
;
398 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
399 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
401 struct isl_basic_map
*bmap
;
404 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
405 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
406 return (struct isl_basic_set
*)bmap
;
409 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
410 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
412 struct isl_basic_map
*bmap
;
416 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
421 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
427 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
428 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
429 unsigned n_eq
, unsigned n_ineq
)
431 struct isl_basic_map
*bmap
;
434 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
438 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
442 static void dup_constraints(
443 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
446 unsigned total
= isl_basic_map_total_dim(src
);
448 for (i
= 0; i
< src
->n_eq
; ++i
) {
449 int j
= isl_basic_map_alloc_equality(dst
);
450 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
453 for (i
= 0; i
< src
->n_ineq
; ++i
) {
454 int j
= isl_basic_map_alloc_inequality(dst
);
455 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
458 for (i
= 0; i
< src
->n_div
; ++i
) {
459 int j
= isl_basic_map_alloc_div(dst
);
460 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
462 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
465 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
467 struct isl_basic_map
*dup
;
471 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
472 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
475 dup_constraints(dup
, bmap
);
476 dup
->flags
= bmap
->flags
;
477 dup
->sample
= isl_vec_copy(bmap
->sample
);
481 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
483 struct isl_basic_map
*dup
;
485 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
486 return (struct isl_basic_set
*)dup
;
489 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
494 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
498 return isl_basic_set_dup(bset
);
501 struct isl_set
*isl_set_copy(struct isl_set
*set
)
510 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
515 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
519 bmap
= isl_basic_map_dup(bmap
);
521 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
525 struct isl_map
*isl_map_copy(struct isl_map
*map
)
534 void isl_basic_map_free(struct isl_basic_map
*bmap
)
542 isl_ctx_deref(bmap
->ctx
);
544 isl_blk_free(bmap
->ctx
, bmap
->block2
);
546 isl_blk_free(bmap
->ctx
, bmap
->block
);
547 isl_vec_free(bmap
->sample
);
548 isl_dim_free(bmap
->dim
);
552 void isl_basic_set_free(struct isl_basic_set
*bset
)
554 isl_basic_map_free((struct isl_basic_map
*)bset
);
557 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
559 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
562 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
568 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
569 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
571 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
572 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
574 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
576 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
578 int j
= isl_basic_map_alloc_inequality(bmap
);
582 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
583 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
590 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
591 bmap
->extra
- bmap
->n_div
);
595 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
597 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
600 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
604 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
609 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
611 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
614 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
619 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
621 if (pos
!= bmap
->n_eq
- 1) {
623 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
624 bmap
->eq
[bmap
->n_eq
- 1] = t
;
630 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
632 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
635 void isl_basic_map_inequality_to_equality(
636 struct isl_basic_map
*bmap
, unsigned pos
)
641 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
642 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
647 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
648 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
649 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
650 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
653 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
655 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
658 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
664 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
665 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
666 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
667 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
668 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
669 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
670 1 + isl_basic_map_total_dim(bmap
),
671 bmap
->extra
- bmap
->n_div
);
672 return bmap
->n_ineq
++;
675 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
677 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
680 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
684 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
689 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
691 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
694 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
699 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
701 if (pos
!= bmap
->n_ineq
- 1) {
703 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
704 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
705 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
711 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
713 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
716 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
721 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
724 k
= isl_basic_map_alloc_equality(bmap
);
727 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
730 isl_basic_map_free(bmap
);
734 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
737 return (isl_basic_set
*)
738 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
741 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
746 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
749 k
= isl_basic_map_alloc_inequality(bmap
);
752 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
755 isl_basic_map_free(bmap
);
759 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
762 return (isl_basic_set
*)
763 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
766 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
770 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
771 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
772 1 + 1 + isl_basic_map_total_dim(bmap
),
773 bmap
->extra
- bmap
->n_div
);
774 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
775 return bmap
->n_div
++;
778 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
780 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
783 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
787 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
792 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
794 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
797 /* Copy constraint from src to dst, putting the vars of src at offset
798 * dim_off in dst and the divs of src at offset div_off in dst.
799 * If both sets are actually map, then dim_off applies to the input
802 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
803 struct isl_basic_map
*src_map
, isl_int
*src
,
804 unsigned in_off
, unsigned out_off
, unsigned div_off
)
806 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
807 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
808 unsigned src_in
= isl_basic_map_n_in(src_map
);
809 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
810 unsigned src_out
= isl_basic_map_n_out(src_map
);
811 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
812 isl_int_set(dst
[0], src
[0]);
813 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
814 if (dst_nparam
> src_nparam
)
815 isl_seq_clr(dst
+1+src_nparam
,
816 dst_nparam
- src_nparam
);
817 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
818 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
820 isl_min(dst_in
-in_off
, src_in
));
821 if (dst_in
-in_off
> src_in
)
822 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
823 dst_in
- in_off
- src_in
);
824 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
825 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
826 src
+1+src_nparam
+src_in
,
827 isl_min(dst_out
-out_off
, src_out
));
828 if (dst_out
-out_off
> src_out
)
829 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
830 dst_out
- out_off
- src_out
);
831 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
832 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
833 src
+1+src_nparam
+src_in
+src_out
,
834 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
835 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
836 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
837 div_off
+src_map
->n_div
,
838 dst_map
->n_div
- div_off
- src_map
->n_div
);
841 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
842 struct isl_basic_map
*src_map
, isl_int
*src
,
843 unsigned in_off
, unsigned out_off
, unsigned div_off
)
845 isl_int_set(dst
[0], src
[0]);
846 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
849 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
850 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
855 if (!bmap1
|| !bmap2
)
858 div_off
= bmap1
->n_div
;
860 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
861 int i1
= isl_basic_map_alloc_equality(bmap1
);
864 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
865 i_pos
, o_pos
, div_off
);
868 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
869 int i1
= isl_basic_map_alloc_inequality(bmap1
);
872 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
873 i_pos
, o_pos
, div_off
);
876 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
877 int i1
= isl_basic_map_alloc_div(bmap1
);
880 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
881 i_pos
, o_pos
, div_off
);
884 isl_basic_map_free(bmap2
);
889 isl_basic_map_free(bmap1
);
890 isl_basic_map_free(bmap2
);
894 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
895 struct isl_dim_map
*dim_map
)
899 for (i
= 0; i
< dim_map
->len
; ++i
) {
900 if (dim_map
->pos
[i
] < 0)
901 isl_int_set_si(dst
[i
], 0);
903 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
907 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
908 struct isl_dim_map
*dim_map
)
910 isl_int_set(dst
[0], src
[0]);
911 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
914 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
915 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
919 if (!src
|| !dst
|| !dim_map
)
922 for (i
= 0; i
< src
->n_eq
; ++i
) {
923 int i1
= isl_basic_map_alloc_equality(dst
);
926 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
929 for (i
= 0; i
< src
->n_ineq
; ++i
) {
930 int i1
= isl_basic_map_alloc_inequality(dst
);
933 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
936 for (i
= 0; i
< src
->n_div
; ++i
) {
937 int i1
= isl_basic_map_alloc_div(dst
);
940 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
944 isl_basic_map_free(src
);
949 isl_basic_map_free(src
);
950 isl_basic_map_free(dst
);
954 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
955 struct isl_basic_set
*bset2
, unsigned pos
)
957 return (struct isl_basic_set
*)
958 add_constraints((struct isl_basic_map
*)bset1
,
959 (struct isl_basic_map
*)bset2
, 0, pos
);
962 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
963 struct isl_dim
*dim
, unsigned extra
,
964 unsigned n_eq
, unsigned n_ineq
)
966 struct isl_basic_map
*ext
;
976 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
977 base
->extra
>= base
->n_div
+ extra
;
979 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
980 room_for_ineq(base
, n_ineq
)) {
985 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
986 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
987 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
988 extra
+= base
->extra
;
990 n_ineq
+= base
->n_ineq
;
992 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
998 ext
->sample
= isl_vec_copy(base
->sample
);
1000 ext
= add_constraints(ext
, base
, 0, 0);
1003 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1010 isl_basic_map_free(base
);
1014 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1015 struct isl_dim
*dim
, unsigned extra
,
1016 unsigned n_eq
, unsigned n_ineq
)
1018 return (struct isl_basic_set
*)
1019 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1020 extra
, n_eq
, n_ineq
);
1023 struct isl_basic_map
*isl_basic_map_extend_constraints(
1024 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1028 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1032 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1033 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1034 unsigned n_eq
, unsigned n_ineq
)
1036 struct isl_basic_map
*bmap
;
1037 struct isl_dim
*dim
;
1041 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1045 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1049 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1050 unsigned nparam
, unsigned dim
, unsigned extra
,
1051 unsigned n_eq
, unsigned n_ineq
)
1053 return (struct isl_basic_set
*)
1054 isl_basic_map_extend((struct isl_basic_map
*)base
,
1055 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1058 struct isl_basic_set
*isl_basic_set_extend_constraints(
1059 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1061 return (struct isl_basic_set
*)
1062 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1066 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1068 return (struct isl_basic_set
*)
1069 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1072 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1077 if (bmap
->ref
> 1) {
1079 bmap
= isl_basic_map_dup(bmap
);
1082 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1086 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1094 return isl_set_dup(set
);
1097 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1105 return isl_map_dup(map
);
1108 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1109 unsigned a_len
, unsigned b_len
)
1111 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1112 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1113 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1116 struct isl_basic_set
*isl_basic_set_swap_vars(
1117 struct isl_basic_set
*bset
, unsigned n
)
1127 nparam
= isl_basic_set_n_param(bset
);
1128 dim
= isl_basic_set_n_dim(bset
);
1129 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1134 bset
= isl_basic_set_cow(bset
);
1138 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1139 if (isl_blk_is_error(blk
))
1142 for (i
= 0; i
< bset
->n_eq
; ++i
)
1144 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1146 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1148 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1150 for (i
= 0; i
< bset
->n_div
; ++i
)
1152 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1154 isl_blk_free(bset
->ctx
, blk
);
1156 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1157 return isl_basic_set_gauss(bset
, NULL
);
1159 isl_basic_set_free(bset
);
1163 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1166 set
= isl_set_cow(set
);
1170 for (i
= 0; i
< set
->n
; ++i
) {
1171 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1177 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1181 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1187 total
= isl_basic_map_total_dim(bmap
);
1188 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1189 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1191 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1193 i
= isl_basic_map_alloc_equality(bmap
);
1197 isl_int_set_si(bmap
->eq
[i
][0], 1);
1198 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1199 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1200 isl_vec_free(bmap
->sample
);
1201 bmap
->sample
= NULL
;
1202 return isl_basic_map_finalize(bmap
);
1204 isl_basic_map_free(bmap
);
1208 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1210 return (struct isl_basic_set
*)
1211 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1214 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1217 unsigned off
= isl_dim_total(bmap
->dim
);
1218 isl_int
*t
= bmap
->div
[a
];
1219 bmap
->div
[a
] = bmap
->div
[b
];
1222 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1223 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1225 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1226 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1228 for (i
= 0; i
< bmap
->n_div
; ++i
)
1229 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1230 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1233 /* Eliminate the specified n dimensions starting at first from the
1234 * constraints using Fourier-Motzkin. The dimensions themselves
1237 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1238 enum isl_dim_type type
, unsigned first
, unsigned n
)
1247 map
= isl_map_cow(map
);
1250 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1251 first
+= pos(map
->dim
, type
) - 1;
1253 for (i
= 0; i
< map
->n
; ++i
) {
1254 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1264 /* Eliminate the specified n dimensions starting at first from the
1265 * constraints using Fourier-Motzkin. The dimensions themselves
1268 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1269 enum isl_dim_type type
, unsigned first
, unsigned n
)
1271 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1274 /* Eliminate the specified n dimensions starting at first from the
1275 * constraints using Fourier-Motzkin. The dimensions themselves
1278 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1279 unsigned first
, unsigned n
)
1281 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1284 /* Project out n dimensions starting at first using Fourier-Motzkin */
1285 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1286 unsigned first
, unsigned n
)
1288 set
= isl_set_eliminate_dims(set
, first
, n
);
1289 set
= isl_set_drop_dims(set
, first
, n
);
1293 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1295 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1303 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1312 set
= isl_set_cow(set
);
1316 for (i
= 0; i
< set
->n
; ++i
) {
1317 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1327 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1328 enum isl_dim_type type
, unsigned first
, unsigned n
)
1332 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1336 bmap
= isl_basic_map_eliminate_vars(bmap
,
1337 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1340 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1342 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1345 isl_basic_map_free(bmap
);
1349 __isl_give isl_basic_set
*isl_basic_set_remove(__isl_take isl_basic_set
*bset
,
1350 enum isl_dim_type type
, unsigned first
, unsigned n
)
1352 return (isl_basic_set
*)
1353 isl_basic_map_remove((isl_basic_map
*)bset
, type
, first
, n
);
1356 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1357 enum isl_dim_type type
, unsigned first
, unsigned n
)
1364 map
= isl_map_cow(map
);
1367 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1369 for (i
= 0; i
< map
->n
; ++i
) {
1370 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1371 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1375 map
= isl_map_drop(map
, type
, first
, n
);
1382 __isl_give isl_set
*isl_set_remove(__isl_take isl_set
*bset
,
1383 enum isl_dim_type type
, unsigned first
, unsigned n
)
1385 return (isl_set
*)isl_map_remove((isl_map
*)bset
, type
, first
, n
);
1388 /* Project out n inputs starting at first using Fourier-Motzkin */
1389 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1390 unsigned first
, unsigned n
)
1392 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1395 /* Project out n dimensions starting at first using Fourier-Motzkin */
1396 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1397 unsigned first
, unsigned n
)
1399 unsigned nparam
= isl_basic_set_n_param(bset
);
1400 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1401 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1405 static void dump_term(struct isl_basic_map
*bmap
,
1406 isl_int c
, int pos
, FILE *out
)
1409 unsigned in
= isl_basic_map_n_in(bmap
);
1410 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1411 unsigned nparam
= isl_basic_map_n_param(bmap
);
1413 isl_int_print(out
, c
, 0);
1415 if (!isl_int_is_one(c
))
1416 isl_int_print(out
, c
, 0);
1417 if (pos
< 1 + nparam
) {
1418 name
= isl_dim_get_name(bmap
->dim
,
1419 isl_dim_param
, pos
- 1);
1421 fprintf(out
, "%s", name
);
1423 fprintf(out
, "p%d", pos
- 1);
1424 } else if (pos
< 1 + nparam
+ in
)
1425 fprintf(out
, "i%d", pos
- 1 - nparam
);
1426 else if (pos
< 1 + nparam
+ dim
)
1427 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1429 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1433 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1434 int sign
, FILE *out
)
1438 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1442 for (i
= 0, first
= 1; i
< len
; ++i
) {
1443 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1446 fprintf(out
, " + ");
1448 isl_int_abs(v
, c
[i
]);
1449 dump_term(bmap
, v
, i
, out
);
1456 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1457 const char *op
, FILE *out
, int indent
)
1461 fprintf(out
, "%*s", indent
, "");
1463 dump_constraint_sign(bmap
, c
, 1, out
);
1464 fprintf(out
, " %s ", op
);
1465 dump_constraint_sign(bmap
, c
, -1, out
);
1469 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1470 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1472 fprintf(out
, "%*s", indent
, "");
1473 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1478 static void dump_constraints(struct isl_basic_map
*bmap
,
1479 isl_int
**c
, unsigned n
,
1480 const char *op
, FILE *out
, int indent
)
1484 for (i
= 0; i
< n
; ++i
)
1485 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1488 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1492 unsigned total
= isl_basic_map_total_dim(bmap
);
1494 for (j
= 0; j
< 1 + total
; ++j
) {
1495 if (isl_int_is_zero(exp
[j
]))
1497 if (!first
&& isl_int_is_pos(exp
[j
]))
1499 dump_term(bmap
, exp
[j
], j
, out
);
1504 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1508 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1509 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1511 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1512 fprintf(out
, "%*s", indent
, "");
1513 fprintf(out
, "e%d = [(", i
);
1514 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1516 isl_int_print(out
, bmap
->div
[i
][0], 0);
1517 fprintf(out
, "]\n");
1521 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1524 fprintf(out
, "null basic set\n");
1528 fprintf(out
, "%*s", indent
, "");
1529 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1530 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1531 bset
->extra
, bset
->flags
);
1532 dump((struct isl_basic_map
*)bset
, out
, indent
);
1535 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1538 fprintf(out
, "null basic map\n");
1542 fprintf(out
, "%*s", indent
, "");
1543 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1544 "flags: %x, n_name: %d\n",
1546 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1547 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1548 dump(bmap
, out
, indent
);
1551 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1556 total
= isl_basic_map_total_dim(bmap
);
1557 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1558 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1559 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1560 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1564 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1566 struct isl_set
*set
;
1570 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1571 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1572 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1573 sizeof(struct isl_set
) +
1574 (n
- 1) * sizeof(struct isl_basic_set
*));
1578 set
->ctx
= dim
->ctx
;
1579 isl_ctx_ref(set
->ctx
);
1591 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1592 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1594 struct isl_set
*set
;
1595 struct isl_dim
*dims
;
1597 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1601 set
= isl_set_alloc_dim(dims
, n
, flags
);
1605 /* Make sure "map" has room for at least "n" more basic maps.
1607 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1610 struct isl_map
*grown
= NULL
;
1614 isl_assert(map
->ctx
, n
>= 0, goto error
);
1615 if (map
->n
+ n
<= map
->size
)
1617 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1620 for (i
= 0; i
< map
->n
; ++i
) {
1621 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1629 isl_map_free(grown
);
1634 /* Make sure "set" has room for at least "n" more basic sets.
1636 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1638 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1641 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1644 struct isl_set
*dup
;
1649 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1652 for (i
= 0; i
< set
->n
; ++i
)
1653 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1657 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1659 struct isl_set
*set
;
1664 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1666 isl_basic_set_free(bset
);
1669 return isl_set_add_basic_set(set
, bset
);
1672 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1674 struct isl_map
*map
;
1679 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1681 isl_basic_map_free(bmap
);
1684 return isl_map_add_basic_map(map
, bmap
);
1687 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1688 __isl_take isl_basic_set
*bset
)
1690 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1691 (struct isl_basic_map
*)bset
);
1694 void isl_set_free(struct isl_set
*set
)
1704 isl_ctx_deref(set
->ctx
);
1705 for (i
= 0; i
< set
->n
; ++i
)
1706 isl_basic_set_free(set
->p
[i
]);
1707 isl_dim_free(set
->dim
);
1711 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1716 fprintf(out
, "null set\n");
1720 fprintf(out
, "%*s", indent
, "");
1721 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1722 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1724 for (i
= 0; i
< set
->n
; ++i
) {
1725 fprintf(out
, "%*s", indent
, "");
1726 fprintf(out
, "basic set %d:\n", i
);
1727 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1731 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1736 fprintf(out
, "null map\n");
1740 fprintf(out
, "%*s", indent
, "");
1741 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1742 "flags: %x, n_name: %d\n",
1743 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1744 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1745 for (i
= 0; i
< map
->n
; ++i
) {
1746 fprintf(out
, "%*s", indent
, "");
1747 fprintf(out
, "basic map %d:\n", i
);
1748 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1752 struct isl_basic_map
*isl_basic_map_intersect_domain(
1753 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1755 struct isl_basic_map
*bmap_domain
;
1756 struct isl_dim
*dim
;
1761 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1762 bset
->dim
, isl_dim_param
), goto error
);
1764 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1765 isl_assert(bset
->ctx
,
1766 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1768 bmap
= isl_basic_map_cow(bmap
);
1771 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1772 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1773 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1774 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1775 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1777 bmap
= isl_basic_map_simplify(bmap
);
1778 return isl_basic_map_finalize(bmap
);
1780 isl_basic_map_free(bmap
);
1781 isl_basic_set_free(bset
);
1785 struct isl_basic_map
*isl_basic_map_intersect_range(
1786 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1788 struct isl_basic_map
*bmap_range
;
1793 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1794 bset
->dim
, isl_dim_param
), goto error
);
1796 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1797 isl_assert(bset
->ctx
,
1798 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1800 bmap
= isl_basic_map_cow(bmap
);
1803 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1804 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1805 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1806 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1808 bmap
= isl_basic_map_simplify(bmap
);
1809 return isl_basic_map_finalize(bmap
);
1811 isl_basic_map_free(bmap
);
1812 isl_basic_set_free(bset
);
1816 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1822 total
= 1 + isl_basic_map_total_dim(bmap
);
1823 if (total
!= vec
->size
)
1828 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1829 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1830 if (!isl_int_is_zero(s
)) {
1836 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1837 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1838 if (isl_int_is_neg(s
)) {
1849 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1851 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1854 struct isl_basic_map
*isl_basic_map_intersect(
1855 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1857 struct isl_vec
*sample
= NULL
;
1859 if (!bmap1
|| !bmap2
)
1862 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1863 bmap2
->dim
, isl_dim_param
), goto error
);
1864 if (isl_dim_total(bmap1
->dim
) ==
1865 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1866 isl_dim_total(bmap2
->dim
) !=
1867 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1868 return isl_basic_map_intersect(bmap2
, bmap1
);
1870 if (isl_dim_total(bmap2
->dim
) !=
1871 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1872 isl_assert(bmap1
->ctx
,
1873 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1875 if (bmap1
->sample
&&
1876 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1877 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1878 sample
= isl_vec_copy(bmap1
->sample
);
1879 else if (bmap2
->sample
&&
1880 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1881 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1882 sample
= isl_vec_copy(bmap2
->sample
);
1884 bmap1
= isl_basic_map_cow(bmap1
);
1887 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1888 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1889 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1892 isl_vec_free(sample
);
1894 isl_vec_free(bmap1
->sample
);
1895 bmap1
->sample
= sample
;
1898 bmap1
= isl_basic_map_simplify(bmap1
);
1899 return isl_basic_map_finalize(bmap1
);
1902 isl_vec_free(sample
);
1903 isl_basic_map_free(bmap1
);
1904 isl_basic_map_free(bmap2
);
1908 struct isl_basic_set
*isl_basic_set_intersect(
1909 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1911 return (struct isl_basic_set
*)
1912 isl_basic_map_intersect(
1913 (struct isl_basic_map
*)bset1
,
1914 (struct isl_basic_map
*)bset2
);
1917 /* Special case of isl_map_intersect, where both map1 and map2
1918 * are convex, without any divs and such that either map1 or map2
1919 * contains a single constraint. This constraint is then simply
1920 * added to the other map.
1922 static __isl_give isl_map
*map_intersect_add_constraint(
1923 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
1925 struct isl_basic_map
*bmap1
;
1926 struct isl_basic_map
*bmap2
;
1928 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
1929 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
1930 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
1931 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
1933 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
1934 return isl_map_intersect(map2
, map1
);
1936 isl_assert(map2
->ctx
,
1937 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
1939 map1
= isl_map_cow(map1
);
1942 if (isl_map_fast_is_empty(map1
)) {
1946 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
1947 if (map2
->p
[0]->n_eq
== 1)
1948 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
1950 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
1951 map2
->p
[0]->ineq
[0]);
1953 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
1954 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
1958 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
1959 isl_basic_map_free(map1
->p
[0]);
1972 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1975 struct isl_map
*result
;
1981 if (isl_map_fast_is_empty(map1
)) {
1985 if (isl_map_fast_is_empty(map2
)) {
1990 if (map1
->n
== 1 && map2
->n
== 1 &&
1991 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
1992 isl_dim_equal(map1
->dim
, map2
->dim
) &&
1993 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
1994 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
1995 return map_intersect_add_constraint(map1
, map2
);
1996 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1997 map2
->dim
, isl_dim_param
), goto error
);
1998 if (isl_dim_total(map1
->dim
) ==
1999 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2000 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2001 return isl_map_intersect(map2
, map1
);
2003 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2004 isl_assert(map1
->ctx
,
2005 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2007 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2008 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2009 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2011 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2012 map1
->n
* map2
->n
, flags
);
2015 for (i
= 0; i
< map1
->n
; ++i
)
2016 for (j
= 0; j
< map2
->n
; ++j
) {
2017 struct isl_basic_map
*part
;
2018 part
= isl_basic_map_intersect(
2019 isl_basic_map_copy(map1
->p
[i
]),
2020 isl_basic_map_copy(map2
->p
[j
]));
2021 if (isl_basic_map_is_empty(part
))
2022 isl_basic_map_free(part
);
2024 result
= isl_map_add_basic_map(result
, part
);
2037 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2039 return (struct isl_set
*)
2040 isl_map_intersect((struct isl_map
*)set1
,
2041 (struct isl_map
*)set2
);
2044 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2046 struct isl_dim
*dim
;
2047 struct isl_basic_set
*bset
;
2052 bmap
= isl_basic_map_cow(bmap
);
2055 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2056 in
= isl_basic_map_n_in(bmap
);
2057 bset
= isl_basic_set_from_basic_map(bmap
);
2058 bset
= isl_basic_set_swap_vars(bset
, in
);
2059 return isl_basic_map_from_basic_set(bset
, dim
);
2062 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2063 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2065 struct isl_dim
*res_dim
;
2066 struct isl_basic_map
*res
;
2067 struct isl_dim_map
*dim_map
;
2068 unsigned total
, off
;
2069 enum isl_dim_type t
;
2077 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2079 total
= isl_basic_map_total_dim(bmap
) + n
;
2080 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2082 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2084 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2086 unsigned size
= isl_basic_map_dim(bmap
, t
);
2087 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2089 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2090 pos
, size
- pos
, off
+ pos
+ n
);
2092 off
+= isl_dim_size(res_dim
, t
);
2094 isl_dim_map_div(dim_map
, bmap
, off
);
2096 res
= isl_basic_map_alloc_dim(res_dim
,
2097 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2098 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2099 res
= isl_basic_map_simplify(res
);
2100 return isl_basic_map_finalize(res
);
2103 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2104 enum isl_dim_type type
, unsigned n
)
2108 return isl_basic_map_insert(bmap
, type
,
2109 isl_basic_map_dim(bmap
, type
), n
);
2112 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2113 enum isl_dim_type type
, unsigned n
)
2117 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2118 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2120 isl_basic_set_free(bset
);
2124 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2125 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2132 map
= isl_map_cow(map
);
2136 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2140 for (i
= 0; i
< map
->n
; ++i
) {
2141 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2152 __isl_give isl_map
*isl_map_add(__isl_take isl_map
*map
,
2153 enum isl_dim_type type
, unsigned n
)
2157 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2160 __isl_give isl_set
*isl_set_add(__isl_take isl_set
*set
,
2161 enum isl_dim_type type
, unsigned n
)
2165 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2166 return (isl_set
*)isl_map_add((isl_map
*)set
, type
, n
);
2172 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2173 __isl_take isl_basic_map
*bmap
,
2174 enum isl_dim_type dst_type
, unsigned dst_pos
,
2175 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2178 struct isl_dim_map
*dim_map
;
2179 struct isl_basic_map
*res
;
2180 enum isl_dim_type t
;
2181 unsigned total
, off
;
2188 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2191 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2194 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2196 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2197 pos(bmap
->dim
, src_type
) + src_pos
+
2198 ((src_type
< dst_type
) ? n
: 0)) {
2199 bmap
= isl_basic_map_cow(bmap
);
2203 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2204 src_type
, src_pos
, n
);
2208 bmap
= isl_basic_map_finalize(bmap
);
2213 total
= isl_basic_map_total_dim(bmap
);
2214 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2217 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2218 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2219 if (t
== dst_type
) {
2220 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2223 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2226 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2227 dst_pos
, size
- dst_pos
, off
);
2228 off
+= size
- dst_pos
;
2229 } else if (t
== src_type
) {
2230 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2233 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2234 src_pos
+ n
, size
- src_pos
- n
, off
);
2235 off
+= size
- src_pos
- n
;
2237 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2241 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2243 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2244 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2245 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2247 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2248 src_type
, src_pos
, n
);
2252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2253 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2254 bmap
= isl_basic_map_finalize(bmap
);
2258 isl_basic_map_free(bmap
);
2262 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2263 enum isl_dim_type dst_type
, unsigned dst_pos
,
2264 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2266 return (isl_basic_set
*)isl_basic_map_move_dims(
2267 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2270 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2271 enum isl_dim_type dst_type
, unsigned dst_pos
,
2272 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2276 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2277 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2278 src_type
, src_pos
, n
);
2284 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2285 enum isl_dim_type dst_type
, unsigned dst_pos
,
2286 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2295 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2298 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2301 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2303 map
= isl_map_cow(map
);
2307 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2311 for (i
= 0; i
< map
->n
; ++i
) {
2312 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2314 src_type
, src_pos
, n
);
2325 /* Move the specified dimensions to the last columns right before
2326 * the divs. Don't change the dimension specification of bmap.
2327 * That's the responsibility of the caller.
2329 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2330 enum isl_dim_type type
, unsigned first
, unsigned n
)
2332 struct isl_dim_map
*dim_map
;
2333 struct isl_basic_map
*res
;
2334 enum isl_dim_type t
;
2335 unsigned total
, off
;
2339 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2342 total
= isl_basic_map_total_dim(bmap
);
2343 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2346 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2347 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2349 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2352 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2353 first
, n
, total
- bmap
->n_div
- n
);
2354 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2355 first
+ n
, size
- (first
+ n
), off
);
2356 off
+= size
- (first
+ n
);
2358 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2362 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2364 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2365 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2366 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2370 /* Turn the n dimensions of type type, starting at first
2371 * into existentially quantified variables.
2373 __isl_give isl_basic_map
*isl_basic_map_project_out(
2374 __isl_take isl_basic_map
*bmap
,
2375 enum isl_dim_type type
, unsigned first
, unsigned n
)
2388 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2389 return isl_basic_map_remove(bmap
, type
, first
, n
);
2391 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2394 bmap
= move_last(bmap
, type
, first
, n
);
2395 bmap
= isl_basic_map_cow(bmap
);
2397 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2398 old
= bmap
->block2
.data
;
2399 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2400 (bmap
->extra
+ n
) * (1 + row_size
));
2401 if (!bmap
->block2
.data
)
2403 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2406 for (i
= 0; i
< n
; ++i
) {
2407 new_div
[i
] = bmap
->block2
.data
+
2408 (bmap
->extra
+ i
) * (1 + row_size
);
2409 isl_seq_clr(new_div
[i
], 1 + row_size
);
2411 for (i
= 0; i
< bmap
->extra
; ++i
)
2412 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2414 bmap
->div
= new_div
;
2418 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2421 bmap
= isl_basic_map_simplify(bmap
);
2422 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2423 return isl_basic_map_finalize(bmap
);
2425 isl_basic_map_free(bmap
);
2429 /* Turn the n dimensions of type type, starting at first
2430 * into existentially quantified variables.
2432 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2433 enum isl_dim_type type
, unsigned first
, unsigned n
)
2435 return (isl_basic_set
*)isl_basic_map_project_out(
2436 (isl_basic_map
*)bset
, type
, first
, n
);
2439 /* Turn the n dimensions of type type, starting at first
2440 * into existentially quantified variables.
2442 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2443 enum isl_dim_type type
, unsigned first
, unsigned n
)
2453 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2455 map
= isl_map_cow(map
);
2459 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2463 for (i
= 0; i
< map
->n
; ++i
) {
2464 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2475 /* Turn the n dimensions of type type, starting at first
2476 * into existentially quantified variables.
2478 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2479 enum isl_dim_type type
, unsigned first
, unsigned n
)
2481 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2484 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2488 for (i
= 0; i
< n
; ++i
) {
2489 j
= isl_basic_map_alloc_div(bmap
);
2492 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2496 isl_basic_map_free(bmap
);
2500 struct isl_basic_map
*isl_basic_map_apply_range(
2501 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2503 struct isl_dim
*dim_result
= NULL
;
2504 struct isl_basic_map
*bmap
;
2505 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2506 struct isl_dim_map
*dim_map1
, *dim_map2
;
2508 if (!bmap1
|| !bmap2
)
2511 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2512 isl_dim_copy(bmap2
->dim
));
2514 n_in
= isl_basic_map_n_in(bmap1
);
2515 n_out
= isl_basic_map_n_out(bmap2
);
2516 n
= isl_basic_map_n_out(bmap1
);
2517 nparam
= isl_basic_map_n_param(bmap1
);
2519 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2520 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2521 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2522 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2523 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2524 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2525 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2526 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2527 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2528 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2529 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2531 bmap
= isl_basic_map_alloc_dim(dim_result
,
2532 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2533 bmap1
->n_eq
+ bmap2
->n_eq
,
2534 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2535 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2536 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2537 bmap
= add_divs(bmap
, n
);
2538 bmap
= isl_basic_map_simplify(bmap
);
2539 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2540 return isl_basic_map_finalize(bmap
);
2542 isl_basic_map_free(bmap1
);
2543 isl_basic_map_free(bmap2
);
2547 struct isl_basic_set
*isl_basic_set_apply(
2548 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2553 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2556 return (struct isl_basic_set
*)
2557 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2559 isl_basic_set_free(bset
);
2560 isl_basic_map_free(bmap
);
2564 struct isl_basic_map
*isl_basic_map_apply_domain(
2565 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2567 if (!bmap1
|| !bmap2
)
2570 isl_assert(bmap1
->ctx
,
2571 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2572 isl_assert(bmap1
->ctx
,
2573 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2576 bmap1
= isl_basic_map_reverse(bmap1
);
2577 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2578 return isl_basic_map_reverse(bmap1
);
2580 isl_basic_map_free(bmap1
);
2581 isl_basic_map_free(bmap2
);
2585 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2586 * A \cap B -> f(A) + f(B)
2588 struct isl_basic_map
*isl_basic_map_sum(
2589 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2591 unsigned n_in
, n_out
, nparam
, total
, pos
;
2592 struct isl_basic_map
*bmap
= NULL
;
2593 struct isl_dim_map
*dim_map1
, *dim_map2
;
2596 if (!bmap1
|| !bmap2
)
2599 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2602 nparam
= isl_basic_map_n_param(bmap1
);
2603 n_in
= isl_basic_map_n_in(bmap1
);
2604 n_out
= isl_basic_map_n_out(bmap1
);
2606 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2607 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2608 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2609 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2610 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2611 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2612 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2613 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2614 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2615 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2616 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2618 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2619 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2620 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2621 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2622 for (i
= 0; i
< n_out
; ++i
) {
2623 int j
= isl_basic_map_alloc_equality(bmap
);
2626 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2627 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2628 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2629 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2631 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2632 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2633 bmap
= add_divs(bmap
, 2 * n_out
);
2635 bmap
= isl_basic_map_simplify(bmap
);
2636 return isl_basic_map_finalize(bmap
);
2638 isl_basic_map_free(bmap
);
2639 isl_basic_map_free(bmap1
);
2640 isl_basic_map_free(bmap2
);
2644 /* Given two maps A -> f(A) and B -> g(B), construct a map
2645 * A \cap B -> f(A) + f(B)
2647 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2649 struct isl_map
*result
;
2655 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2657 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2658 map1
->n
* map2
->n
, 0);
2661 for (i
= 0; i
< map1
->n
; ++i
)
2662 for (j
= 0; j
< map2
->n
; ++j
) {
2663 struct isl_basic_map
*part
;
2664 part
= isl_basic_map_sum(
2665 isl_basic_map_copy(map1
->p
[i
]),
2666 isl_basic_map_copy(map2
->p
[j
]));
2667 if (isl_basic_map_is_empty(part
))
2668 isl_basic_map_free(part
);
2670 result
= isl_map_add_basic_map(result
, part
);
2683 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2684 __isl_take isl_set
*set2
)
2686 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2689 /* Given a basic map A -> f(A), construct A -> -f(A).
2691 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2696 bmap
= isl_basic_map_cow(bmap
);
2700 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2701 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2702 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2703 for (j
= 0; j
< n
; ++j
)
2704 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2705 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2706 for (j
= 0; j
< n
; ++j
)
2707 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2708 for (i
= 0; i
< bmap
->n_div
; ++i
)
2709 for (j
= 0; j
< n
; ++j
)
2710 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2711 return isl_basic_map_finalize(bmap
);
2714 /* Given a map A -> f(A), construct A -> -f(A).
2716 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2720 map
= isl_map_cow(map
);
2724 for (i
= 0; i
< map
->n
; ++i
) {
2725 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2736 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2738 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2741 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2742 * A -> floor(f(A)/d).
2744 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2747 unsigned n_in
, n_out
, nparam
, total
, pos
;
2748 struct isl_basic_map
*result
= NULL
;
2749 struct isl_dim_map
*dim_map
;
2755 nparam
= isl_basic_map_n_param(bmap
);
2756 n_in
= isl_basic_map_n_in(bmap
);
2757 n_out
= isl_basic_map_n_out(bmap
);
2759 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2760 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2761 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2762 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2763 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2764 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2766 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2767 bmap
->n_div
+ n_out
,
2768 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2769 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2770 result
= add_divs(result
, n_out
);
2771 for (i
= 0; i
< n_out
; ++i
) {
2773 j
= isl_basic_map_alloc_inequality(result
);
2776 isl_seq_clr(result
->ineq
[j
], 1+total
);
2777 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2778 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2779 j
= isl_basic_map_alloc_inequality(result
);
2782 isl_seq_clr(result
->ineq
[j
], 1+total
);
2783 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2784 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2785 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2788 result
= isl_basic_map_simplify(result
);
2789 return isl_basic_map_finalize(result
);
2791 isl_basic_map_free(result
);
2795 /* Given a map A -> f(A) and an integer d, construct a map
2796 * A -> floor(f(A)/d).
2798 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2802 map
= isl_map_cow(map
);
2806 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2807 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2808 for (i
= 0; i
< map
->n
; ++i
) {
2809 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2820 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2826 i
= isl_basic_map_alloc_equality(bmap
);
2829 nparam
= isl_basic_map_n_param(bmap
);
2830 n_in
= isl_basic_map_n_in(bmap
);
2831 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2832 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2833 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2834 return isl_basic_map_finalize(bmap
);
2836 isl_basic_map_free(bmap
);
2840 /* Add a constraints to "bmap" expressing i_pos < o_pos
2842 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2848 i
= isl_basic_map_alloc_inequality(bmap
);
2851 nparam
= isl_basic_map_n_param(bmap
);
2852 n_in
= isl_basic_map_n_in(bmap
);
2853 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2854 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2855 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2856 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2857 return isl_basic_map_finalize(bmap
);
2859 isl_basic_map_free(bmap
);
2863 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2865 static __isl_give isl_basic_map
*var_less_or_equal(
2866 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2872 i
= isl_basic_map_alloc_inequality(bmap
);
2875 nparam
= isl_basic_map_n_param(bmap
);
2876 n_in
= isl_basic_map_n_in(bmap
);
2877 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2878 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2879 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2880 return isl_basic_map_finalize(bmap
);
2882 isl_basic_map_free(bmap
);
2886 /* Add a constraints to "bmap" expressing i_pos > o_pos
2888 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2894 i
= isl_basic_map_alloc_inequality(bmap
);
2897 nparam
= isl_basic_map_n_param(bmap
);
2898 n_in
= isl_basic_map_n_in(bmap
);
2899 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2900 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2901 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2902 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2903 return isl_basic_map_finalize(bmap
);
2905 isl_basic_map_free(bmap
);
2909 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2911 static __isl_give isl_basic_map
*var_more_or_equal(
2912 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2918 i
= isl_basic_map_alloc_inequality(bmap
);
2921 nparam
= isl_basic_map_n_param(bmap
);
2922 n_in
= isl_basic_map_n_in(bmap
);
2923 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2924 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2925 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2926 return isl_basic_map_finalize(bmap
);
2928 isl_basic_map_free(bmap
);
2932 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2935 struct isl_basic_map
*bmap
;
2936 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2939 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2940 bmap
= var_equal(bmap
, i
);
2941 return isl_basic_map_finalize(bmap
);
2944 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2946 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2949 struct isl_basic_map
*bmap
;
2950 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2953 for (i
= 0; i
< pos
&& bmap
; ++i
)
2954 bmap
= var_equal(bmap
, i
);
2956 bmap
= var_less(bmap
, pos
);
2957 return isl_basic_map_finalize(bmap
);
2960 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2962 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
2963 __isl_take isl_dim
*dim
, unsigned pos
)
2966 isl_basic_map
*bmap
;
2968 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2969 for (i
= 0; i
< pos
; ++i
)
2970 bmap
= var_equal(bmap
, i
);
2971 bmap
= var_less_or_equal(bmap
, pos
);
2972 return isl_basic_map_finalize(bmap
);
2975 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2977 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2980 struct isl_basic_map
*bmap
;
2981 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2984 for (i
= 0; i
< pos
&& bmap
; ++i
)
2985 bmap
= var_equal(bmap
, i
);
2987 bmap
= var_more(bmap
, pos
);
2988 return isl_basic_map_finalize(bmap
);
2991 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
2993 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
2994 __isl_take isl_dim
*dim
, unsigned pos
)
2997 isl_basic_map
*bmap
;
2999 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3000 for (i
= 0; i
< pos
; ++i
)
3001 bmap
= var_equal(bmap
, i
);
3002 bmap
= var_more_or_equal(bmap
, pos
);
3003 return isl_basic_map_finalize(bmap
);
3006 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3007 unsigned n
, int equal
)
3009 struct isl_map
*map
;
3012 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3014 for (i
= 0; i
+ 1 < n
; ++i
)
3015 map
= isl_map_add_basic_map(map
,
3016 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3019 map
= isl_map_add_basic_map(map
,
3020 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3022 map
= isl_map_add_basic_map(map
,
3023 isl_basic_map_less_at(dims
, n
- 1));
3030 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3034 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3037 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3039 return map_lex_lte_first(dim
, n
, 0);
3042 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3044 return map_lex_lte_first(dim
, n
, 1);
3047 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3049 return map_lex_lte(isl_dim_map(set_dim
), 0);
3052 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3054 return map_lex_lte(isl_dim_map(set_dim
), 1);
3057 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3058 unsigned n
, int equal
)
3060 struct isl_map
*map
;
3063 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3065 for (i
= 0; i
+ 1 < n
; ++i
)
3066 map
= isl_map_add_basic_map(map
,
3067 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3070 map
= isl_map_add_basic_map(map
,
3071 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3073 map
= isl_map_add_basic_map(map
,
3074 isl_basic_map_more_at(dims
, n
- 1));
3081 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3085 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3088 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3090 return map_lex_gte_first(dim
, n
, 0);
3093 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3095 return map_lex_gte_first(dim
, n
, 1);
3098 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3100 return map_lex_gte(isl_dim_map(set_dim
), 0);
3103 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3105 return map_lex_gte(isl_dim_map(set_dim
), 1);
3108 struct isl_basic_map
*isl_basic_map_from_basic_set(
3109 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3111 struct isl_basic_map
*bmap
;
3113 bset
= isl_basic_set_cow(bset
);
3117 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3118 isl_dim_free(bset
->dim
);
3119 bmap
= (struct isl_basic_map
*) bset
;
3121 return isl_basic_map_finalize(bmap
);
3123 isl_basic_set_free(bset
);
3128 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3132 if (bmap
->dim
->n_in
== 0)
3133 return (struct isl_basic_set
*)bmap
;
3134 bmap
= isl_basic_map_cow(bmap
);
3137 bmap
->dim
= isl_dim_cow(bmap
->dim
);
3140 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
3141 bmap
->dim
->n_in
= 0;
3142 bmap
= isl_basic_map_finalize(bmap
);
3143 return (struct isl_basic_set
*)bmap
;
3145 isl_basic_map_free(bmap
);
3149 /* For a div d = floor(f/m), add the constraints
3152 * -(f-(n-1)) + m d >= 0
3154 * Note that the second constraint is the negation of
3158 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3159 unsigned pos
, isl_int
*div
)
3162 unsigned total
= isl_basic_map_total_dim(bmap
);
3164 i
= isl_basic_map_alloc_inequality(bmap
);
3167 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3168 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3170 j
= isl_basic_map_alloc_inequality(bmap
);
3173 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3174 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3175 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3179 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3180 unsigned pos
, isl_int
*div
)
3182 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3186 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3188 unsigned total
= isl_basic_map_total_dim(bmap
);
3189 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3191 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3195 struct isl_basic_set
*isl_basic_map_underlying_set(
3196 struct isl_basic_map
*bmap
)
3200 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
3201 return (struct isl_basic_set
*)bmap
;
3202 bmap
= isl_basic_map_cow(bmap
);
3205 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3208 bmap
->extra
-= bmap
->n_div
;
3210 bmap
= isl_basic_map_finalize(bmap
);
3211 return (struct isl_basic_set
*)bmap
;
3216 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3217 __isl_take isl_basic_set
*bset
)
3219 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3222 struct isl_basic_map
*isl_basic_map_overlying_set(
3223 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3225 struct isl_basic_map
*bmap
;
3226 struct isl_ctx
*ctx
;
3233 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3234 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3235 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3237 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3238 isl_basic_map_free(like
);
3239 return (struct isl_basic_map
*)bset
;
3241 bset
= isl_basic_set_cow(bset
);
3244 total
= bset
->dim
->n_out
+ bset
->extra
;
3245 bmap
= (struct isl_basic_map
*)bset
;
3246 isl_dim_free(bmap
->dim
);
3247 bmap
->dim
= isl_dim_copy(like
->dim
);
3250 bmap
->n_div
= like
->n_div
;
3251 bmap
->extra
+= like
->n_div
;
3254 ltotal
= total
- bmap
->extra
+ like
->extra
;
3257 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3258 bmap
->extra
* (1 + 1 + total
));
3259 if (isl_blk_is_error(bmap
->block2
))
3261 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3265 for (i
= 0; i
< bmap
->extra
; ++i
)
3266 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3267 for (i
= 0; i
< like
->n_div
; ++i
) {
3268 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3269 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3271 bmap
= isl_basic_map_extend_constraints(bmap
,
3272 0, 2 * like
->n_div
);
3273 for (i
= 0; i
< like
->n_div
; ++i
) {
3274 if (isl_int_is_zero(bmap
->div
[i
][0]))
3276 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3280 isl_basic_map_free(like
);
3281 bmap
= isl_basic_map_simplify(bmap
);
3282 bmap
= isl_basic_map_finalize(bmap
);
3285 isl_basic_map_free(like
);
3286 isl_basic_set_free(bset
);
3290 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3291 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3293 return (struct isl_basic_set
*)
3294 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3297 struct isl_set
*isl_set_from_underlying_set(
3298 struct isl_set
*set
, struct isl_basic_set
*like
)
3304 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3306 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3307 isl_basic_set_free(like
);
3310 set
= isl_set_cow(set
);
3313 for (i
= 0; i
< set
->n
; ++i
) {
3314 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3315 isl_basic_set_copy(like
));
3319 isl_dim_free(set
->dim
);
3320 set
->dim
= isl_dim_copy(like
->dim
);
3323 isl_basic_set_free(like
);
3326 isl_basic_set_free(like
);
3331 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3335 map
= isl_map_cow(map
);
3338 map
->dim
= isl_dim_cow(map
->dim
);
3342 for (i
= 1; i
< map
->n
; ++i
)
3343 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3345 for (i
= 0; i
< map
->n
; ++i
) {
3346 map
->p
[i
] = (struct isl_basic_map
*)
3347 isl_basic_map_underlying_set(map
->p
[i
]);
3352 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3354 isl_dim_free(map
->dim
);
3355 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3359 return (struct isl_set
*)map
;
3365 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3367 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3370 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3372 struct isl_basic_set
*domain
;
3377 n_in
= isl_basic_map_n_in(bmap
);
3378 n_out
= isl_basic_map_n_out(bmap
);
3379 domain
= isl_basic_set_from_basic_map(bmap
);
3380 return isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3383 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3385 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3388 struct isl_set
*isl_map_range(struct isl_map
*map
)
3391 struct isl_set
*set
;
3395 if (isl_map_dim(map
, isl_dim_in
) == 0)
3396 return (isl_set
*)map
;
3398 map
= isl_map_cow(map
);
3402 set
= (struct isl_set
*) map
;
3403 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3406 for (i
= 0; i
< map
->n
; ++i
) {
3407 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3411 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3412 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3419 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3422 struct isl_map
*map
= NULL
;
3424 set
= isl_set_cow(set
);
3427 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3428 map
= (struct isl_map
*)set
;
3429 for (i
= 0; i
< set
->n
; ++i
) {
3430 map
->p
[i
] = isl_basic_map_from_basic_set(
3431 set
->p
[i
], isl_dim_copy(dim
));
3435 isl_dim_free(map
->dim
);
3444 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3446 return (struct isl_map
*)set
;
3449 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3451 return isl_map_reverse(isl_map_from_range(set
));;
3454 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3455 __isl_take isl_set
*range
)
3457 return isl_map_product(isl_map_from_domain(domain
),
3458 isl_map_from_range(range
));
3461 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3464 struct isl_set
*set
= NULL
;
3468 map
= isl_map_cow(map
);
3471 map
->dim
= isl_dim_cow(map
->dim
);
3474 map
->dim
->n_out
+= map
->dim
->n_in
;
3476 set
= (struct isl_set
*)map
;
3477 for (i
= 0; i
< map
->n
; ++i
) {
3478 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3488 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3490 struct isl_map
*map
;
3494 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3495 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3496 sizeof(struct isl_map
) +
3497 (n
- 1) * sizeof(struct isl_basic_map
*));
3501 map
->ctx
= dim
->ctx
;
3502 isl_ctx_ref(map
->ctx
);
3514 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
3515 unsigned nparam
, unsigned in
, unsigned out
, int n
,
3518 struct isl_map
*map
;
3519 struct isl_dim
*dims
;
3521 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
3525 map
= isl_map_alloc_dim(dims
, n
, flags
);
3529 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
3531 struct isl_basic_map
*bmap
;
3532 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
3533 bmap
= isl_basic_map_set_to_empty(bmap
);
3537 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
3539 struct isl_basic_set
*bset
;
3540 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
3541 bset
= isl_basic_set_set_to_empty(bset
);
3545 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
3547 struct isl_basic_map
*bmap
;
3550 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3551 bmap
= isl_basic_map_set_to_empty(bmap
);
3555 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
3557 struct isl_basic_map
*bmap
;
3560 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3561 bmap
= isl_basic_map_set_to_empty(bmap
);
3565 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
3567 struct isl_basic_set
*bset
;
3570 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3571 bset
= isl_basic_set_set_to_empty(bset
);
3575 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
3577 struct isl_basic_map
*bmap
;
3578 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
3582 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
3584 struct isl_basic_set
*bset
;
3585 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
3589 __isl_give isl_basic_map
*isl_basic_map_universe_like(
3590 __isl_keep isl_basic_map
*model
)
3594 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3597 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
3601 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3604 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
3605 __isl_keep isl_set
*model
)
3609 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3612 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
3614 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3617 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
3621 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3624 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
3628 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3631 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
3633 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3636 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
3640 return isl_set_empty(isl_dim_copy(model
->dim
));
3643 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
3645 struct isl_map
*map
;
3648 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3649 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
3653 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
3655 struct isl_set
*set
;
3658 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3659 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
3663 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3667 return isl_set_universe(isl_dim_copy(model
->dim
));
3670 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3673 struct isl_map
*dup
;
3677 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3678 for (i
= 0; i
< map
->n
; ++i
)
3679 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
3683 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
3684 __isl_take isl_basic_map
*bmap
)
3688 if (isl_basic_map_fast_is_empty(bmap
)) {
3689 isl_basic_map_free(bmap
);
3692 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3693 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3694 map
->p
[map
->n
] = bmap
;
3696 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3702 isl_basic_map_free(bmap
);
3706 void isl_map_free(struct isl_map
*map
)
3716 isl_ctx_deref(map
->ctx
);
3717 for (i
= 0; i
< map
->n
; ++i
)
3718 isl_basic_map_free(map
->p
[i
]);
3719 isl_dim_free(map
->dim
);
3723 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3724 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3728 base
= isl_map_cow(base
);
3732 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3735 for (i
= 0; i
< base
->n
; ++i
) {
3736 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3737 isl_dim_copy(base
->dim
), 0, 0, 0);
3747 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3748 unsigned nparam
, unsigned dim
)
3750 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3754 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3755 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3759 bmap
= isl_basic_map_cow(bmap
);
3760 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3761 j
= isl_basic_map_alloc_equality(bmap
);
3764 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3765 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3766 isl_int_set_si(bmap
->eq
[j
][0], value
);
3767 bmap
= isl_basic_map_simplify(bmap
);
3768 return isl_basic_map_finalize(bmap
);
3770 isl_basic_map_free(bmap
);
3774 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3775 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3779 bmap
= isl_basic_map_cow(bmap
);
3780 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3781 j
= isl_basic_map_alloc_equality(bmap
);
3784 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3785 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3786 isl_int_set(bmap
->eq
[j
][0], value
);
3787 bmap
= isl_basic_map_simplify(bmap
);
3788 return isl_basic_map_finalize(bmap
);
3790 isl_basic_map_free(bmap
);
3794 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3795 enum isl_dim_type type
, unsigned pos
, int value
)
3799 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3800 return isl_basic_map_fix_pos_si(bmap
,
3801 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3803 isl_basic_map_free(bmap
);
3807 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3808 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3812 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3813 return isl_basic_map_fix_pos(bmap
,
3814 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3816 isl_basic_map_free(bmap
);
3820 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3821 enum isl_dim_type type
, unsigned pos
, int value
)
3823 return (struct isl_basic_set
*)
3824 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3828 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3829 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3831 return (struct isl_basic_set
*)
3832 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3836 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3837 unsigned input
, int value
)
3839 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3842 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3843 unsigned dim
, int value
)
3845 return (struct isl_basic_set
*)
3846 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3847 isl_dim_set
, dim
, value
);
3850 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3851 enum isl_dim_type type
, unsigned pos
, int value
)
3855 map
= isl_map_cow(map
);
3859 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3860 for (i
= 0; i
< map
->n
; ++i
) {
3861 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
3865 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3872 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
3873 enum isl_dim_type type
, unsigned pos
, int value
)
3875 return (struct isl_set
*)
3876 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
3879 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
3880 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3884 map
= isl_map_cow(map
);
3888 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3889 for (i
= 0; i
< map
->n
; ++i
) {
3890 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
3894 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3901 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
3902 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3904 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
3907 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
3908 unsigned input
, int value
)
3910 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
3913 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
3915 return (struct isl_set
*)
3916 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
3919 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
3920 __isl_take isl_basic_map
*bmap
,
3921 enum isl_dim_type type
, unsigned pos
, int value
)
3927 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3928 pos
+= isl_basic_map_offset(bmap
, type
);
3929 bmap
= isl_basic_map_cow(bmap
);
3930 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
3931 j
= isl_basic_map_alloc_inequality(bmap
);
3934 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3935 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
3936 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
3937 bmap
= isl_basic_map_simplify(bmap
);
3938 return isl_basic_map_finalize(bmap
);
3940 isl_basic_map_free(bmap
);
3944 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
3945 unsigned dim
, isl_int value
)
3949 bset
= isl_basic_set_cow(bset
);
3950 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
3951 j
= isl_basic_set_alloc_inequality(bset
);
3954 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3955 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3956 isl_int_neg(bset
->ineq
[j
][0], value
);
3957 bset
= isl_basic_set_simplify(bset
);
3958 return isl_basic_set_finalize(bset
);
3960 isl_basic_set_free(bset
);
3964 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
3965 enum isl_dim_type type
, unsigned pos
, int value
)
3969 map
= isl_map_cow(map
);
3973 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3974 for (i
= 0; i
< map
->n
; ++i
) {
3975 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
3980 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3987 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
3988 enum isl_dim_type type
, unsigned pos
, int value
)
3990 return (struct isl_set
*)
3991 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
3994 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
3999 set
= isl_set_cow(set
);
4003 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4004 for (i
= 0; i
< set
->n
; ++i
) {
4005 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4015 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4019 map
= isl_map_cow(map
);
4023 map
->dim
= isl_dim_reverse(map
->dim
);
4026 for (i
= 0; i
< map
->n
; ++i
) {
4027 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4031 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4038 static struct isl_map
*isl_basic_map_partial_lexopt(
4039 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4040 struct isl_set
**empty
, int max
)
4044 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4045 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4047 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4049 isl_basic_map_free(bmap
);
4050 isl_basic_set_free(dom
);
4056 struct isl_map
*isl_basic_map_partial_lexmax(
4057 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4058 struct isl_set
**empty
)
4060 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4063 struct isl_map
*isl_basic_map_partial_lexmin(
4064 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4065 struct isl_set
**empty
)
4067 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4070 struct isl_set
*isl_basic_set_partial_lexmin(
4071 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4072 struct isl_set
**empty
)
4074 return (struct isl_set
*)
4075 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4079 struct isl_set
*isl_basic_set_partial_lexmax(
4080 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4081 struct isl_set
**empty
)
4083 return (struct isl_set
*)
4084 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4088 /* Given a basic map "bmap", compute the lexicograhically minimal
4089 * (or maximal) image element for each domain element in dom.
4090 * Set *empty to those elements in dom that do not have an image element.
4092 * We first make sure the basic sets in dom are disjoint and then
4093 * simply collect the results over each of the basic sets separately.
4094 * We could probably improve the efficiency a bit by moving the union
4095 * domain down into the parametric integer programming.
4097 static __isl_give isl_map
*basic_map_partial_lexopt(
4098 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4099 __isl_give isl_set
**empty
, int max
)
4102 struct isl_map
*res
;
4104 dom
= isl_set_make_disjoint(dom
);
4108 if (isl_set_fast_is_empty(dom
)) {
4109 res
= isl_map_empty_like_basic_map(bmap
);
4110 *empty
= isl_set_empty_like(dom
);
4112 isl_basic_map_free(bmap
);
4116 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4117 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4119 for (i
= 1; i
< dom
->n
; ++i
) {
4120 struct isl_map
*res_i
;
4121 struct isl_set
*empty_i
;
4123 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4124 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4126 res
= isl_map_union_disjoint(res
, res_i
);
4127 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4131 isl_basic_map_free(bmap
);
4136 isl_basic_map_free(bmap
);
4140 /* Given a map "map", compute the lexicograhically minimal
4141 * (or maximal) image element for each domain element in dom.
4142 * Set *empty to those elements in dom that do not have an image element.
4144 * We first compute the lexicographically minimal or maximal element
4145 * in the first basic map. This results in a partial solution "res"
4146 * and a subset "todo" of dom that still need to be handled.
4147 * We then consider each of the remaining maps in "map" and successively
4148 * improve both "res" and "todo".
4150 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4151 * Assume we are computing the lexicographical maximum.
4152 * We first intersect basic map i with a relation that maps elements
4153 * to elements that are lexicographically larger than the image elements
4154 * in res^k and the compute the maximum image element of this intersection.
4155 * The result ("better") corresponds to those image elements in basic map i
4156 * that are better than what we had before. The remainder ("keep") are the
4157 * domain elements for which the image element in res_k was better.
4158 * We also compute the lexicographical maximum of basic map i in todo^k.
4159 * res^i is the result of the operation + better + those elements in
4160 * res^k that we should keep
4161 * todo^i is the remainder of the maximum operation on todo^k.
4163 static __isl_give isl_map
*isl_map_partial_lexopt(
4164 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4165 __isl_give isl_set
**empty
, int max
)
4168 struct isl_map
*res
;
4169 struct isl_set
*todo
;
4174 if (isl_map_fast_is_empty(map
)) {
4182 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4183 isl_set_copy(dom
), &todo
, max
);
4185 for (i
= 1; i
< map
->n
; ++i
) {
4187 struct isl_map
*better
;
4188 struct isl_set
*keep
;
4189 struct isl_map
*res_i
;
4190 struct isl_set
*todo_i
;
4191 struct isl_dim
*dim
= isl_map_get_dim(res
);
4193 dim
= isl_dim_range(dim
);
4195 lt
= isl_map_lex_lt(dim
);
4197 lt
= isl_map_lex_gt(dim
);
4198 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4199 lt
= isl_map_intersect(lt
,
4200 isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
])));
4201 better
= isl_map_partial_lexopt(lt
,
4202 isl_map_domain(isl_map_copy(res
)),
4205 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4206 todo
, &todo_i
, max
);
4208 res
= isl_map_intersect_domain(res
, keep
);
4209 res
= isl_map_union_disjoint(res
, res_i
);
4210 res
= isl_map_union_disjoint(res
, better
);
4231 __isl_give isl_map
*isl_map_partial_lexmax(
4232 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4233 __isl_give isl_set
**empty
)
4235 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4238 __isl_give isl_map
*isl_map_partial_lexmin(
4239 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4240 __isl_give isl_set
**empty
)
4242 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4245 __isl_give isl_set
*isl_set_partial_lexmin(
4246 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4247 __isl_give isl_set
**empty
)
4249 return (struct isl_set
*)
4250 isl_map_partial_lexmin((struct isl_map
*)set
,
4254 __isl_give isl_set
*isl_set_partial_lexmax(
4255 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4256 __isl_give isl_set
**empty
)
4258 return (struct isl_set
*)
4259 isl_map_partial_lexmax((struct isl_map
*)set
,
4263 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4265 struct isl_basic_set
*dom
= NULL
;
4266 struct isl_dim
*dom_dim
;
4270 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4271 dom
= isl_basic_set_universe(dom_dim
);
4272 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4274 isl_basic_map_free(bmap
);
4278 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4280 return isl_basic_map_lexopt(bmap
, 0);
4283 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4285 return isl_basic_map_lexopt(bmap
, 1);
4288 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4290 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4293 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4295 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4298 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4300 struct isl_set
*dom
= NULL
;
4301 struct isl_dim
*dom_dim
;
4305 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4306 dom
= isl_set_universe(dom_dim
);
4307 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4313 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4315 return isl_map_lexopt(map
, 0);
4318 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4320 return isl_map_lexopt(map
, 1);
4323 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4325 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4328 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4330 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4333 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
4334 struct isl_dim
*dim
)
4341 for (i
= 0; i
< map
->n
; ++i
) {
4342 isl_dim_free(map
->p
[i
]->dim
);
4343 map
->p
[i
]->dim
= isl_dim_copy(dim
);
4345 isl_dim_free(map
->dim
);
4355 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
4356 struct isl_dim
*dim
)
4358 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
4361 /* Apply a preimage specified by "mat" on the parameters of "bset".
4362 * bset is assumed to have only parameters and divs.
4364 static struct isl_basic_set
*basic_set_parameter_preimage(
4365 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4372 bset
->dim
= isl_dim_cow(bset
->dim
);
4376 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4378 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4380 bset
->dim
->nparam
= 0;
4381 bset
->dim
->n_out
= nparam
;
4382 bset
= isl_basic_set_preimage(bset
, mat
);
4384 bset
->dim
->nparam
= bset
->dim
->n_out
;
4385 bset
->dim
->n_out
= 0;
4390 isl_basic_set_free(bset
);
4394 /* Apply a preimage specified by "mat" on the parameters of "set".
4395 * set is assumed to have only parameters and divs.
4397 static struct isl_set
*set_parameter_preimage(
4398 struct isl_set
*set
, struct isl_mat
*mat
)
4400 struct isl_dim
*dim
= NULL
;
4406 dim
= isl_dim_copy(set
->dim
);
4407 dim
= isl_dim_cow(dim
);
4411 nparam
= isl_set_dim(set
, isl_dim_param
);
4413 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4416 dim
->n_out
= nparam
;
4417 isl_set_reset_dim(set
, dim
);
4418 set
= isl_set_preimage(set
, mat
);
4421 dim
= isl_dim_copy(set
->dim
);
4422 dim
= isl_dim_cow(dim
);
4425 dim
->nparam
= dim
->n_out
;
4427 isl_set_reset_dim(set
, dim
);
4437 /* Intersect the basic set "bset" with the affine space specified by the
4438 * equalities in "eq".
4440 static struct isl_basic_set
*basic_set_append_equalities(
4441 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4449 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4454 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4455 for (i
= 0; i
< eq
->n_row
; ++i
) {
4456 k
= isl_basic_set_alloc_equality(bset
);
4459 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4460 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
4464 bset
= isl_basic_set_gauss(bset
, NULL
);
4465 bset
= isl_basic_set_finalize(bset
);
4470 isl_basic_set_free(bset
);
4474 /* Intersect the set "set" with the affine space specified by the
4475 * equalities in "eq".
4477 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
4485 for (i
= 0; i
< set
->n
; ++i
) {
4486 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
4499 /* Project the given basic set onto its parameter domain, possibly introducing
4500 * new, explicit, existential variables in the constraints.
4501 * The input has parameters and (possibly implicit) existential variables.
4502 * The output has the same parameters, but only
4503 * explicit existentially quantified variables.
4505 * The actual projection is performed by pip, but pip doesn't seem
4506 * to like equalities very much, so we first remove the equalities
4507 * among the parameters by performing a variable compression on
4508 * the parameters. Afterward, an inverse transformation is performed
4509 * and the equalities among the parameters are inserted back in.
4511 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
4515 struct isl_mat
*T
, *T2
;
4516 struct isl_set
*set
;
4517 unsigned nparam
, n_div
;
4519 bset
= isl_basic_set_cow(bset
);
4523 if (bset
->n_eq
== 0)
4524 return isl_basic_set_lexmin(bset
);
4526 isl_basic_set_gauss(bset
, NULL
);
4528 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4529 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
4531 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
4532 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
4535 if (i
== bset
->n_eq
)
4536 return isl_basic_set_lexmin(bset
);
4538 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
4540 eq
= isl_mat_cow(eq
);
4541 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
4542 if (T
&& T
->n_col
== 0) {
4546 bset
= isl_basic_set_set_to_empty(bset
);
4547 return isl_set_from_basic_set(bset
);
4549 bset
= basic_set_parameter_preimage(bset
, T
);
4551 set
= isl_basic_set_lexmin(bset
);
4552 set
= set_parameter_preimage(set
, T2
);
4553 set
= set_append_equalities(set
, eq
);
4557 /* Compute an explicit representation for all the existentially
4558 * quantified variables.
4559 * The input and output dimensions are first turned into parameters.
4560 * compute_divs then returns a map with the same parameters and
4561 * no input or output dimensions and the dimension specification
4562 * is reset to that of the input.
4564 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
4566 struct isl_basic_set
*bset
;
4567 struct isl_set
*set
;
4568 struct isl_map
*map
;
4569 struct isl_dim
*dim
, *orig_dim
= NULL
;
4574 bmap
= isl_basic_map_cow(bmap
);
4578 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4579 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4580 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4581 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
4585 orig_dim
= bmap
->dim
;
4587 bset
= (struct isl_basic_set
*)bmap
;
4589 set
= parameter_compute_divs(bset
);
4590 map
= (struct isl_map
*)set
;
4591 map
= isl_map_reset_dim(map
, orig_dim
);
4595 isl_basic_map_free(bmap
);
4599 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
4607 off
= isl_dim_total(bmap
->dim
);
4608 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4609 if (isl_int_is_zero(bmap
->div
[i
][0]))
4611 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
4617 static int map_divs_known(__isl_keep isl_map
*map
)
4624 for (i
= 0; i
< map
->n
; ++i
) {
4625 int known
= basic_map_divs_known(map
->p
[i
]);
4633 /* If bmap contains any unknown divs, then compute explicit
4634 * expressions for them. However, this computation may be
4635 * quite expensive, so first try to remove divs that aren't
4638 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
4642 struct isl_map
*map
;
4644 known
= basic_map_divs_known(bmap
);
4648 return isl_map_from_basic_map(bmap
);
4650 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4652 known
= basic_map_divs_known(bmap
);
4656 return isl_map_from_basic_map(bmap
);
4658 map
= compute_divs(bmap
);
4661 isl_basic_map_free(bmap
);
4665 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
4669 struct isl_map
*res
;
4676 known
= map_divs_known(map
);
4684 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
4685 for (i
= 1 ; i
< map
->n
; ++i
) {
4687 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
4688 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4689 res
= isl_map_union_disjoint(res
, r2
);
4691 res
= isl_map_union(res
, r2
);
4698 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
4700 return (struct isl_set
*)
4701 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
4704 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
4706 return (struct isl_set
*)
4707 isl_map_compute_divs((struct isl_map
*)set
);
4710 struct isl_set
*isl_map_domain(struct isl_map
*map
)
4713 struct isl_set
*set
;
4718 map
= isl_map_cow(map
);
4722 set
= (struct isl_set
*)map
;
4723 set
->dim
= isl_dim_domain(set
->dim
);
4726 for (i
= 0; i
< map
->n
; ++i
) {
4727 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
4731 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4732 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4739 struct isl_map
*isl_map_union_disjoint(
4740 struct isl_map
*map1
, struct isl_map
*map2
)
4744 struct isl_map
*map
= NULL
;
4758 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4760 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4761 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4762 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4764 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
4765 map1
->n
+ map2
->n
, flags
);
4768 for (i
= 0; i
< map1
->n
; ++i
) {
4769 map
= isl_map_add_basic_map(map
,
4770 isl_basic_map_copy(map1
->p
[i
]));
4774 for (i
= 0; i
< map2
->n
; ++i
) {
4775 map
= isl_map_add_basic_map(map
,
4776 isl_basic_map_copy(map2
->p
[i
]));
4790 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
4792 map1
= isl_map_union_disjoint(map1
, map2
);
4796 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
4800 struct isl_set
*isl_set_union_disjoint(
4801 struct isl_set
*set1
, struct isl_set
*set2
)
4803 return (struct isl_set
*)
4804 isl_map_union_disjoint(
4805 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4808 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
4810 return (struct isl_set
*)
4811 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4814 struct isl_map
*isl_map_intersect_range(
4815 struct isl_map
*map
, struct isl_set
*set
)
4818 struct isl_map
*result
;
4824 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
4825 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
4826 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4828 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
4829 map
->n
* set
->n
, flags
);
4832 for (i
= 0; i
< map
->n
; ++i
)
4833 for (j
= 0; j
< set
->n
; ++j
) {
4834 result
= isl_map_add_basic_map(result
,
4835 isl_basic_map_intersect_range(
4836 isl_basic_map_copy(map
->p
[i
]),
4837 isl_basic_set_copy(set
->p
[j
])));
4850 struct isl_map
*isl_map_intersect_domain(
4851 struct isl_map
*map
, struct isl_set
*set
)
4853 return isl_map_reverse(
4854 isl_map_intersect_range(isl_map_reverse(map
), set
));
4857 struct isl_map
*isl_map_apply_domain(
4858 struct isl_map
*map1
, struct isl_map
*map2
)
4862 map1
= isl_map_reverse(map1
);
4863 map1
= isl_map_apply_range(map1
, map2
);
4864 return isl_map_reverse(map1
);
4871 struct isl_map
*isl_map_apply_range(
4872 struct isl_map
*map1
, struct isl_map
*map2
)
4874 struct isl_dim
*dim_result
;
4875 struct isl_map
*result
;
4881 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
4882 isl_dim_copy(map2
->dim
));
4884 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
4887 for (i
= 0; i
< map1
->n
; ++i
)
4888 for (j
= 0; j
< map2
->n
; ++j
) {
4889 result
= isl_map_add_basic_map(result
,
4890 isl_basic_map_apply_range(
4891 isl_basic_map_copy(map1
->p
[i
]),
4892 isl_basic_map_copy(map2
->p
[j
])));
4898 if (result
&& result
->n
<= 1)
4899 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
4908 * returns range - domain
4910 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
4913 struct isl_basic_set
*bset
;
4920 dim
= isl_basic_map_n_in(bmap
);
4921 nparam
= isl_basic_map_n_param(bmap
);
4922 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
4923 bset
= isl_basic_set_from_basic_map(bmap
);
4924 bset
= isl_basic_set_cow(bset
);
4925 dims
= isl_basic_set_get_dim(bset
);
4926 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
4927 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
4928 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
4929 for (i
= 0; i
< dim
; ++i
) {
4930 int j
= isl_basic_map_alloc_equality(
4931 (struct isl_basic_map
*)bset
);
4934 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
4935 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
4936 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
4937 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
4939 return isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
4941 isl_basic_map_free(bmap
);
4946 * returns range - domain
4948 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
4952 struct isl_set
*result
;
4957 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
4958 dim
= isl_map_get_dim(map
);
4959 dim
= isl_dim_domain(dim
);
4960 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
4963 for (i
= 0; i
< map
->n
; ++i
)
4964 result
= isl_set_add_basic_set(result
,
4965 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
4973 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
4975 struct isl_basic_map
*bmap
;
4983 nparam
= dims
->nparam
;
4985 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
4989 for (i
= 0; i
< dim
; ++i
) {
4990 int j
= isl_basic_map_alloc_equality(bmap
);
4993 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4994 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
4995 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
4997 return isl_basic_map_finalize(bmap
);
4999 isl_basic_map_free(bmap
);
5003 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5005 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5008 return basic_map_identity(dim
);
5011 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5013 if (!model
|| !model
->dim
)
5015 isl_assert(model
->ctx
,
5016 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5017 return basic_map_identity(isl_dim_copy(model
->dim
));
5020 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5022 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5023 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5026 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5028 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5031 return map_identity(dim
);
5034 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5036 if (!model
|| !model
->dim
)
5038 isl_assert(model
->ctx
,
5039 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5040 return map_identity(isl_dim_copy(model
->dim
));
5043 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5045 if (!model
|| !model
->dim
)
5047 isl_assert(model
->ctx
,
5048 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5049 return map_identity(isl_dim_copy(model
->dim
));
5052 /* Construct a basic set with all set dimensions having only non-negative
5055 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5060 struct isl_basic_set
*bset
;
5064 nparam
= dims
->nparam
;
5066 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5069 for (i
= 0; i
< dim
; ++i
) {
5070 int k
= isl_basic_set_alloc_inequality(bset
);
5073 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5074 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5078 isl_basic_set_free(bset
);
5082 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5083 enum isl_dim_type type
, unsigned first
, unsigned n
)
5086 isl_basic_set
*nonneg
= NULL
;
5087 isl_basic_set
*neg
= NULL
;
5094 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5096 for (i
= 0; i
< n
; ++i
) {
5100 nonneg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5101 k
= isl_basic_set_alloc_inequality(nonneg
);
5104 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5105 isl_int_set_si(nonneg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], 1);
5107 neg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5108 k
= isl_basic_set_alloc_inequality(neg
);
5111 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5112 isl_int_set_si(neg
->ineq
[k
][0], -1);
5113 isl_int_set_si(neg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], -1);
5115 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5120 isl_basic_set_free(nonneg
);
5121 isl_basic_set_free(neg
);
5126 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5128 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5131 int isl_basic_map_is_subset(
5132 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5135 struct isl_map
*map1
;
5136 struct isl_map
*map2
;
5138 if (!bmap1
|| !bmap2
)
5141 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5142 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5144 is_subset
= isl_map_is_subset(map1
, map2
);
5152 int isl_basic_map_is_equal(
5153 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5157 if (!bmap1
|| !bmap2
)
5159 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5162 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5166 int isl_basic_set_is_equal(
5167 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5169 return isl_basic_map_is_equal(
5170 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5173 int isl_map_is_empty(struct isl_map
*map
)
5180 for (i
= 0; i
< map
->n
; ++i
) {
5181 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5190 int isl_map_fast_is_empty(struct isl_map
*map
)
5192 return map
? map
->n
== 0 : -1;
5195 int isl_set_fast_is_empty(struct isl_set
*set
)
5197 return set
? set
->n
== 0 : -1;
5200 int isl_set_is_empty(struct isl_set
*set
)
5202 return isl_map_is_empty((struct isl_map
*)set
);
5205 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5211 is_subset
= isl_map_is_subset(map1
, map2
);
5214 is_subset
= isl_map_is_subset(map2
, map1
);
5218 int isl_basic_map_is_strict_subset(
5219 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5223 if (!bmap1
|| !bmap2
)
5225 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5228 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5229 if (is_subset
== -1)
5234 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5240 is_subset
= isl_map_is_subset(map1
, map2
);
5243 is_subset
= isl_map_is_subset(map2
, map1
);
5244 if (is_subset
== -1)
5249 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5251 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5254 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5258 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5261 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5265 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5268 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5273 return map
->n
== 1 && isl_basic_map_is_universe(map
->p
[0]);
5276 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5278 return isl_map_fast_is_universe((isl_map
*) set
);
5281 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
5283 struct isl_basic_set
*bset
= NULL
;
5284 struct isl_vec
*sample
= NULL
;
5291 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
5294 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
5295 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
5296 copy
= isl_basic_map_convex_hull(copy
);
5297 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
5298 isl_basic_map_free(copy
);
5302 total
= 1 + isl_basic_map_total_dim(bmap
);
5303 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
5304 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
5310 isl_vec_free(bmap
->sample
);
5311 bmap
->sample
= NULL
;
5312 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
5315 sample
= isl_basic_set_sample_vec(bset
);
5318 empty
= sample
->size
== 0;
5319 isl_vec_free(bmap
->sample
);
5320 bmap
->sample
= sample
;
5322 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
5327 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
5331 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
5334 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
5338 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
5341 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
5343 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
5346 struct isl_map
*isl_basic_map_union(
5347 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5349 struct isl_map
*map
;
5350 if (!bmap1
|| !bmap2
)
5353 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
5355 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
5358 map
= isl_map_add_basic_map(map
, bmap1
);
5359 map
= isl_map_add_basic_map(map
, bmap2
);
5362 isl_basic_map_free(bmap1
);
5363 isl_basic_map_free(bmap2
);
5367 struct isl_set
*isl_basic_set_union(
5368 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5370 return (struct isl_set
*)isl_basic_map_union(
5371 (struct isl_basic_map
*)bset1
,
5372 (struct isl_basic_map
*)bset2
);
5375 /* Order divs such that any div only depends on previous divs */
5376 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
5384 off
= isl_dim_total(bmap
->dim
);
5386 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5388 if (isl_int_is_zero(bmap
->div
[i
][0]))
5390 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
5394 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
5400 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
5402 return (struct isl_basic_set
*)
5403 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
5406 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
5413 for (i
= 0; i
< map
->n
; ++i
) {
5414 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
5425 /* Look for a div in dst that corresponds to the div "div" in src.
5426 * The divs before "div" in src and dst are assumed to be the same.
5428 * Returns -1 if no corresponding div was found and the position
5429 * of the corresponding div in dst otherwise.
5431 static int find_div(struct isl_basic_map
*dst
,
5432 struct isl_basic_map
*src
, unsigned div
)
5436 unsigned total
= isl_dim_total(src
->dim
);
5438 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
5439 for (i
= div
; i
< dst
->n_div
; ++i
)
5440 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
5441 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
5442 dst
->n_div
- div
) == -1)
5447 struct isl_basic_map
*isl_basic_map_align_divs(
5448 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
5451 unsigned total
= isl_dim_total(src
->dim
);
5456 if (src
->n_div
== 0)
5459 for (i
= 0; i
< src
->n_div
; ++i
)
5460 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
5462 src
= isl_basic_map_order_divs(src
);
5463 dst
= isl_basic_map_cow(dst
);
5464 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
5465 src
->n_div
, 0, 2 * src
->n_div
);
5468 for (i
= 0; i
< src
->n_div
; ++i
) {
5469 int j
= find_div(dst
, src
, i
);
5471 j
= isl_basic_map_alloc_div(dst
);
5474 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
5475 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
5476 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
5480 isl_basic_map_swap_div(dst
, i
, j
);
5484 isl_basic_map_free(dst
);
5488 struct isl_basic_set
*isl_basic_set_align_divs(
5489 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
5491 return (struct isl_basic_set
*)isl_basic_map_align_divs(
5492 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
5495 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
5503 map
= isl_map_compute_divs(map
);
5504 map
= isl_map_cow(map
);
5508 for (i
= 1; i
< map
->n
; ++i
)
5509 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
5510 for (i
= 1; i
< map
->n
; ++i
)
5511 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
5513 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5517 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
5519 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
5522 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
5526 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
5527 map
= isl_map_intersect_domain(map
, set
);
5528 set
= isl_map_range(map
);
5536 /* There is no need to cow as removing empty parts doesn't change
5537 * the meaning of the set.
5539 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
5546 for (i
= map
->n
-1; i
>= 0; --i
) {
5547 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
5549 isl_basic_map_free(map
->p
[i
]);
5550 if (i
!= map
->n
-1) {
5551 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5552 map
->p
[i
] = map
->p
[map
->n
-1];
5560 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
5562 return (struct isl_set
*)
5563 isl_map_remove_empty_parts((struct isl_map
*)set
);
5566 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
5568 struct isl_basic_map
*bmap
;
5569 if (!map
|| map
->n
== 0)
5571 bmap
= map
->p
[map
->n
-1];
5572 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
5573 return isl_basic_map_copy(bmap
);
5576 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
5578 return (struct isl_basic_set
*)
5579 isl_map_copy_basic_map((struct isl_map
*)set
);
5582 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
5583 __isl_keep isl_basic_map
*bmap
)
5589 for (i
= map
->n
-1; i
>= 0; --i
) {
5590 if (map
->p
[i
] != bmap
)
5592 map
= isl_map_cow(map
);
5595 isl_basic_map_free(map
->p
[i
]);
5596 if (i
!= map
->n
-1) {
5597 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
5598 map
->p
[i
] = map
->p
[map
->n
-1];
5609 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
5610 struct isl_basic_set
*bset
)
5612 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
5613 (struct isl_basic_map
*)bset
);
5616 /* Given two basic sets bset1 and bset2, compute the maximal difference
5617 * between the values of dimension pos in bset1 and those in bset2
5618 * for any common value of the parameters and dimensions preceding pos.
5620 static enum isl_lp_result
basic_set_maximal_difference_at(
5621 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
5622 int pos
, isl_int
*opt
)
5624 struct isl_dim
*dims
;
5625 struct isl_basic_map
*bmap1
= NULL
;
5626 struct isl_basic_map
*bmap2
= NULL
;
5627 struct isl_ctx
*ctx
;
5628 struct isl_vec
*obj
;
5631 unsigned dim1
, dim2
;
5632 enum isl_lp_result res
;
5634 if (!bset1
|| !bset2
)
5635 return isl_lp_error
;
5637 nparam
= isl_basic_set_n_param(bset1
);
5638 dim1
= isl_basic_set_n_dim(bset1
);
5639 dim2
= isl_basic_set_n_dim(bset2
);
5640 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
5641 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
5642 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
5643 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
5644 if (!bmap1
|| !bmap2
)
5646 bmap1
= isl_basic_map_cow(bmap1
);
5647 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
5648 pos
, (dim1
- pos
) + (dim2
- pos
),
5649 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
5650 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
5653 total
= isl_basic_map_total_dim(bmap1
);
5655 obj
= isl_vec_alloc(ctx
, 1 + total
);
5656 isl_seq_clr(obj
->block
.data
, 1 + total
);
5657 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
5658 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
5661 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
5663 isl_basic_map_free(bmap1
);
5667 isl_basic_map_free(bmap1
);
5668 isl_basic_map_free(bmap2
);
5669 return isl_lp_error
;
5672 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5673 * for any common value of the parameters and dimensions preceding pos
5674 * in both basic sets, the values of dimension pos in bset1 are
5675 * smaller or larger than those in bset2.
5678 * 1 if bset1 follows bset2
5679 * -1 if bset1 precedes bset2
5680 * 0 if bset1 and bset2 are incomparable
5681 * -2 if some error occurred.
5683 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
5684 struct isl_basic_set
*bset2
, int pos
)
5687 enum isl_lp_result res
;
5692 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5694 if (res
== isl_lp_empty
)
5696 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5697 res
== isl_lp_unbounded
)
5699 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5708 /* Given two basic sets bset1 and bset2, check whether
5709 * for any common value of the parameters and dimensions preceding pos
5710 * there is a value of dimension pos in bset1 that is larger
5711 * than a value of the same dimension in bset2.
5714 * 1 if there exists such a pair
5715 * 0 if there is no such pair, but there is a pair of equal values
5717 * -2 if some error occurred.
5719 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
5720 __isl_keep isl_basic_set
*bset2
, int pos
)
5723 enum isl_lp_result res
;
5728 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5730 if (res
== isl_lp_empty
)
5732 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5733 res
== isl_lp_unbounded
)
5735 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5737 else if (res
== isl_lp_ok
)
5746 /* Given two sets set1 and set2, check whether
5747 * for any common value of the parameters and dimensions preceding pos
5748 * there is a value of dimension pos in set1 that is larger
5749 * than a value of the same dimension in set2.
5752 * 1 if there exists such a pair
5753 * 0 if there is no such pair, but there is a pair of equal values
5755 * -2 if some error occurred.
5757 int isl_set_follows_at(__isl_keep isl_set
*set1
,
5758 __isl_keep isl_set
*set2
, int pos
)
5766 for (i
= 0; i
< set1
->n
; ++i
)
5767 for (j
= 0; j
< set2
->n
; ++j
) {
5769 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
5770 if (f
== 1 || f
== -2)
5779 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5780 unsigned pos
, isl_int
*val
)
5788 total
= isl_basic_map_total_dim(bmap
);
5789 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5790 for (; d
+1 > pos
; --d
)
5791 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5795 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5797 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5799 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5802 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5808 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5809 unsigned pos
, isl_int
*val
)
5821 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5824 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5825 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5826 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5827 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5831 isl_int_set(*val
, v
);
5837 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5838 unsigned pos
, isl_int
*val
)
5840 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
5844 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
5847 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
5850 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
5851 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5853 if (pos
>= isl_basic_map_dim(bmap
, type
))
5855 return isl_basic_map_fast_has_fixed_var(bmap
,
5856 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
5859 int isl_map_fast_is_fixed(struct isl_map
*map
,
5860 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5862 if (pos
>= isl_map_dim(map
, type
))
5864 return isl_map_fast_has_fixed_var(map
,
5865 map_offset(map
, type
) - 1 + pos
, val
);
5868 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5869 * then return this fixed value in *val.
5871 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
5874 return isl_basic_set_fast_has_fixed_var(bset
,
5875 isl_basic_set_n_param(bset
) + dim
, val
);
5878 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5879 * then return this fixed value in *val.
5881 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
5883 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
5886 /* Check if input variable in has fixed value and if so and if val is not NULL,
5887 * then return this fixed value in *val.
5889 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
5891 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
5894 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5895 * and if val is not NULL, then return this lower bound in *val.
5897 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
5898 unsigned dim
, isl_int
*val
)
5900 int i
, i_eq
= -1, i_ineq
= -1;
5907 total
= isl_basic_set_total_dim(bset
);
5908 nparam
= isl_basic_set_n_param(bset
);
5909 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5910 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
5916 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5917 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
5919 if (i_eq
!= -1 || i_ineq
!= -1)
5923 if (i_eq
== -1 && i_ineq
== -1)
5925 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
5926 /* The coefficient should always be one due to normalization. */
5927 if (!isl_int_is_one(c
[1+nparam
+dim
]))
5929 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
5931 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
5932 total
- nparam
- dim
- 1) != -1)
5935 isl_int_neg(*val
, c
[0]);
5939 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
5940 unsigned dim
, isl_int
*val
)
5952 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5956 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5958 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
5959 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
5961 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5965 isl_int_set(*val
, v
);
5976 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
5978 const struct constraint
*c1
= (const struct constraint
*)p1
;
5979 const struct constraint
*c2
= (const struct constraint
*)p2
;
5980 unsigned size
= isl_min(c1
->size
, c2
->size
);
5981 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
5984 static struct isl_basic_map
*isl_basic_map_sort_constraints(
5985 struct isl_basic_map
*bmap
)
5988 struct constraint
*c
;
5993 total
= isl_basic_map_total_dim(bmap
);
5994 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
5997 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5999 c
[i
].c
= bmap
->ineq
[i
];
6001 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6002 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6003 bmap
->ineq
[i
] = c
[i
].c
;
6007 isl_basic_map_free(bmap
);
6011 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6012 __isl_take isl_basic_set
*bset
)
6014 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6015 (struct isl_basic_map
*)bset
);
6018 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6022 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6024 bmap
= isl_basic_map_convex_hull(bmap
);
6025 bmap
= isl_basic_map_sort_constraints(bmap
);
6026 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6030 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6032 return (struct isl_basic_set
*)isl_basic_map_normalize(
6033 (struct isl_basic_map
*)bset
);
6036 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
6037 const struct isl_basic_map
*bmap2
)
6044 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6045 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6046 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6047 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6048 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6049 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6050 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6051 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6053 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6055 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6057 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6058 return bmap1
->n_eq
- bmap2
->n_eq
;
6059 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6060 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6061 if (bmap1
->n_div
!= bmap2
->n_div
)
6062 return bmap1
->n_div
- bmap2
->n_div
;
6063 total
= isl_basic_map_total_dim(bmap1
);
6064 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6065 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6069 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6070 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6074 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6075 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6082 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
6083 struct isl_basic_map
*bmap2
)
6085 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6088 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6089 __isl_keep isl_basic_set
*bset2
)
6091 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6092 (isl_basic_map
*)bset2
);
6095 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6097 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6098 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6100 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6103 /* We normalize in place, but if anything goes wrong we need
6104 * to return NULL, so we need to make sure we don't change the
6105 * meaning of any possible other copies of map.
6107 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6110 struct isl_basic_map
*bmap
;
6114 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6116 for (i
= 0; i
< map
->n
; ++i
) {
6117 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6120 isl_basic_map_free(map
->p
[i
]);
6123 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6124 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6125 map
= isl_map_remove_empty_parts(map
);
6128 for (i
= map
->n
- 1; i
>= 1; --i
) {
6129 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6131 isl_basic_map_free(map
->p
[i
-1]);
6132 for (j
= i
; j
< map
->n
; ++j
)
6133 map
->p
[j
-1] = map
->p
[j
];
6143 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6145 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6148 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6158 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6161 map1
= isl_map_copy(map1
);
6162 map2
= isl_map_copy(map2
);
6163 map1
= isl_map_normalize(map1
);
6164 map2
= isl_map_normalize(map2
);
6167 equal
= map1
->n
== map2
->n
;
6168 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6169 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6182 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6184 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6185 (struct isl_map
*)set2
);
6188 /* Return an interval that ranges from min to max (inclusive)
6190 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6191 isl_int min
, isl_int max
)
6194 struct isl_basic_set
*bset
= NULL
;
6196 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6200 k
= isl_basic_set_alloc_inequality(bset
);
6203 isl_int_set_si(bset
->ineq
[k
][1], 1);
6204 isl_int_neg(bset
->ineq
[k
][0], min
);
6206 k
= isl_basic_set_alloc_inequality(bset
);
6209 isl_int_set_si(bset
->ineq
[k
][1], -1);
6210 isl_int_set(bset
->ineq
[k
][0], max
);
6214 isl_basic_set_free(bset
);
6218 /* Return the Cartesian product of the basic sets in list (in the given order).
6220 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6228 struct isl_basic_set
*product
= NULL
;
6232 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6233 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6234 nparam
= isl_basic_set_n_param(list
->p
[0]);
6235 dim
= isl_basic_set_n_dim(list
->p
[0]);
6236 extra
= list
->p
[0]->n_div
;
6237 n_eq
= list
->p
[0]->n_eq
;
6238 n_ineq
= list
->p
[0]->n_ineq
;
6239 for (i
= 1; i
< list
->n
; ++i
) {
6240 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6241 isl_assert(list
->ctx
,
6242 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6243 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6244 extra
+= list
->p
[i
]->n_div
;
6245 n_eq
+= list
->p
[i
]->n_eq
;
6246 n_ineq
+= list
->p
[i
]->n_ineq
;
6248 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6253 for (i
= 0; i
< list
->n
; ++i
) {
6254 isl_basic_set_add_constraints(product
,
6255 isl_basic_set_copy(list
->p
[i
]), dim
);
6256 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6258 isl_basic_set_list_free(list
);
6261 isl_basic_set_free(product
);
6262 isl_basic_set_list_free(list
);
6266 struct isl_basic_map
*isl_basic_map_product(
6267 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6269 struct isl_dim
*dim_result
= NULL
;
6270 struct isl_basic_map
*bmap
;
6271 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6272 struct isl_dim_map
*dim_map1
, *dim_map2
;
6274 if (!bmap1
|| !bmap2
)
6277 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6278 bmap2
->dim
, isl_dim_param
), goto error
);
6279 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6280 isl_dim_copy(bmap2
->dim
));
6282 in1
= isl_basic_map_n_in(bmap1
);
6283 in2
= isl_basic_map_n_in(bmap2
);
6284 out1
= isl_basic_map_n_out(bmap1
);
6285 out2
= isl_basic_map_n_out(bmap2
);
6286 nparam
= isl_basic_map_n_param(bmap1
);
6288 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
6289 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6290 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6291 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
6292 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
6293 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
6294 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
6295 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
6296 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
6297 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
6298 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
6300 bmap
= isl_basic_map_alloc_dim(dim_result
,
6301 bmap1
->n_div
+ bmap2
->n_div
,
6302 bmap1
->n_eq
+ bmap2
->n_eq
,
6303 bmap1
->n_ineq
+ bmap2
->n_ineq
);
6304 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
6305 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
6306 bmap
= isl_basic_map_simplify(bmap
);
6307 return isl_basic_map_finalize(bmap
);
6309 isl_basic_map_free(bmap1
);
6310 isl_basic_map_free(bmap2
);
6314 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6316 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
6319 struct isl_map
*result
;
6325 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
6326 map2
->dim
, isl_dim_param
), goto error
);
6328 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6329 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6330 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6332 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
6333 isl_dim_copy(map2
->dim
)),
6334 map1
->n
* map2
->n
, flags
);
6337 for (i
= 0; i
< map1
->n
; ++i
)
6338 for (j
= 0; j
< map2
->n
; ++j
) {
6339 struct isl_basic_map
*part
;
6340 part
= isl_basic_map_product(
6341 isl_basic_map_copy(map1
->p
[i
]),
6342 isl_basic_map_copy(map2
->p
[j
]));
6343 if (isl_basic_map_is_empty(part
))
6344 isl_basic_map_free(part
);
6346 result
= isl_map_add_basic_map(result
, part
);
6359 /* Given two set A and B, construct its Cartesian product A x B.
6361 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
6363 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
6364 (struct isl_map
*)set2
);
6367 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
6370 uint32_t hash
= isl_hash_init();
6375 bset
= isl_basic_set_copy(bset
);
6376 bset
= isl_basic_set_normalize(bset
);
6379 total
= isl_basic_set_total_dim(bset
);
6380 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
6381 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6383 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
6384 isl_hash_hash(hash
, c_hash
);
6386 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
6387 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6389 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
6390 isl_hash_hash(hash
, c_hash
);
6392 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
6393 for (i
= 0; i
< bset
->n_div
; ++i
) {
6395 if (isl_int_is_zero(bset
->div
[i
][0]))
6397 isl_hash_byte(hash
, i
& 0xFF);
6398 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
6399 isl_hash_hash(hash
, c_hash
);
6401 isl_basic_set_free(bset
);
6405 uint32_t isl_set_get_hash(struct isl_set
*set
)
6412 set
= isl_set_copy(set
);
6413 set
= isl_set_normalize(set
);
6417 hash
= isl_hash_init();
6418 for (i
= 0; i
< set
->n
; ++i
) {
6420 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
6421 isl_hash_hash(hash
, bset_hash
);
6429 /* Check if the value for dimension dim is completely determined
6430 * by the values of the other parameters and variables.
6431 * That is, check if dimension dim is involved in an equality.
6433 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
6440 nparam
= isl_basic_set_n_param(bset
);
6441 for (i
= 0; i
< bset
->n_eq
; ++i
)
6442 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
6447 /* Check if the value for dimension dim is completely determined
6448 * by the values of the other parameters and variables.
6449 * That is, check if dimension dim is involved in an equality
6450 * for each of the subsets.
6452 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
6458 for (i
= 0; i
< set
->n
; ++i
) {
6460 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
6467 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
6468 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
6475 for (i
= 0; i
< map
->n
; ++i
)
6476 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
6482 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
6483 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
6490 for (i
= 0; i
< set
->n
; ++i
)
6491 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
6497 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
6499 struct isl_dim
*dim
;
6504 if (bset
->n_div
== 0)
6507 bset
= isl_basic_set_cow(bset
);
6511 dim
= isl_basic_set_get_dim(bset
);
6512 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
6515 isl_dim_free(bset
->dim
);
6519 bset
= isl_basic_set_finalize(bset
);
6523 isl_basic_set_free(bset
);
6527 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
6530 struct isl_dim
*dim
;
6533 set
= isl_set_align_divs(set
);
6537 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
6540 set
= isl_set_cow(set
);
6544 n_div
= set
->p
[0]->n_div
;
6545 dim
= isl_set_get_dim(set
);
6546 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
6549 isl_dim_free(set
->dim
);
6552 for (i
= 0; i
< set
->n
; ++i
) {
6553 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
6564 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
6566 struct isl_dim
*dim
;
6567 struct isl_basic_map
*bmap
;
6574 set
= isl_set_align_divs(set
);
6579 dim
= isl_set_get_dim(set
);
6580 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
6582 return isl_map_identity(dim
);
6585 n_div
= set
->p
[0]->n_div
;
6586 dim
= isl_dim_map(dim
);
6587 n_param
= isl_dim_size(dim
, isl_dim_param
);
6588 n_set
= isl_dim_size(dim
, isl_dim_in
);
6589 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
6590 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
6591 for (i
= 0; i
< n_set
; ++i
)
6592 bmap
= var_equal(bmap
, i
);
6594 total
= n_param
+ n_set
+ n_set
+ n_div
;
6595 for (i
= 0; i
< n_div
; ++i
) {
6596 k
= isl_basic_map_alloc_inequality(bmap
);
6599 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
6600 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
6601 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
6602 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
6603 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
6604 set
->p
[0]->div
[i
][0]);
6606 l
= isl_basic_map_alloc_inequality(bmap
);
6609 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
6610 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
6611 set
->p
[0]->div
[i
][0]);
6612 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
6616 return isl_map_from_basic_map(bmap
);
6619 isl_basic_map_free(bmap
);
6623 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
6631 dim
= isl_basic_set_total_dim(bset
);
6632 size
+= bset
->n_eq
* (1 + dim
);
6633 size
+= bset
->n_ineq
* (1 + dim
);
6634 size
+= bset
->n_div
* (2 + dim
);
6639 int isl_set_size(__isl_keep isl_set
*set
)
6647 for (i
= 0; i
< set
->n
; ++i
)
6648 size
+= isl_basic_set_size(set
->p
[i
]);
6653 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
6654 enum isl_dim_type type
, unsigned pos
)
6662 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
6664 pos
+= isl_basic_map_offset(bmap
, type
);
6666 for (i
= 0; i
< bmap
->n_eq
; ++i
)
6667 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
6671 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6672 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
6679 return lower
&& upper
;
6682 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
6683 enum isl_dim_type type
, unsigned pos
)
6690 for (i
= 0; i
< map
->n
; ++i
) {
6692 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
6693 if (bounded
< 0 || !bounded
)
6700 /* Return 1 if the specified dim is involved in both an upper bound
6701 * and a lower bound.
6703 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
6704 enum isl_dim_type type
, unsigned pos
)
6706 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
6709 /* For each of the "n" variables starting at "first", determine
6710 * the sign of the variable and put the results in the first "n"
6711 * elements of the array "signs".
6713 * 1 means that the variable is non-negative
6714 * -1 means that the variable is non-positive
6715 * 0 means the variable attains both positive and negative values.
6717 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
6718 unsigned first
, unsigned n
, int *signs
)
6720 isl_vec
*bound
= NULL
;
6721 struct isl_tab
*tab
= NULL
;
6722 struct isl_tab_undo
*snap
;
6725 if (!bset
|| !signs
)
6728 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
6729 tab
= isl_tab_from_basic_set(bset
);
6733 isl_seq_clr(bound
->el
, bound
->size
);
6734 isl_int_set_si(bound
->el
[0], -1);
6736 snap
= isl_tab_snap(tab
);
6737 for (i
= 0; i
< n
; ++i
) {
6740 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
6741 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6744 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6745 if (isl_tab_rollback(tab
, snap
) < 0)
6753 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
6754 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6757 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6758 if (isl_tab_rollback(tab
, snap
) < 0)
6761 signs
[i
] = empty
? -1 : 0;
6765 isl_vec_free(bound
);
6769 isl_vec_free(bound
);
6773 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
6774 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
6776 if (!bset
|| !signs
)
6778 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
6781 first
+= pos(bset
->dim
, type
) - 1;
6782 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
6785 /* Check if the given map is single-valued.
6790 * and check if the result is a subset of the identity mapping.
6792 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
6798 test
= isl_map_reverse(isl_map_copy(map
));
6799 test
= isl_map_apply_range(test
, isl_map_copy(map
));
6801 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
6803 sv
= isl_map_is_subset(test
, id
);
6811 int isl_set_is_singleton(__isl_keep isl_set
*set
)
6813 return isl_map_is_single_valued((isl_map
*)set
);
6816 int isl_map_is_translation(__isl_keep isl_map
*map
)
6821 delta
= isl_map_deltas(isl_map_copy(map
));
6822 ok
= isl_set_is_singleton(delta
);
6823 isl_set_free(delta
);
6828 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
6830 if (isl_seq_first_non_zero(p
, pos
) != -1)
6832 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
6837 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
6846 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
6849 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
6850 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
6851 for (j
= 0; j
< nvar
; ++j
) {
6852 int lower
= 0, upper
= 0;
6853 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6854 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
6856 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
6862 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6863 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
6865 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
6867 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
6872 if (!lower
|| !upper
)
6879 int isl_set_is_box(__isl_keep isl_set
*set
)
6886 return isl_basic_set_is_box(set
->p
[0]);