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 isl_set_dim(set
, isl_dim_set
);
183 unsigned isl_set_n_param(const struct isl_set
*set
)
185 return isl_set_dim(set
, isl_dim_param
);
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
);
1048 isl_basic_map_free(base
);
1052 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1053 unsigned nparam
, unsigned dim
, unsigned extra
,
1054 unsigned n_eq
, unsigned n_ineq
)
1056 return (struct isl_basic_set
*)
1057 isl_basic_map_extend((struct isl_basic_map
*)base
,
1058 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1061 struct isl_basic_set
*isl_basic_set_extend_constraints(
1062 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1064 return (struct isl_basic_set
*)
1065 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1069 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1071 return (struct isl_basic_set
*)
1072 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1075 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1080 if (bmap
->ref
> 1) {
1082 bmap
= isl_basic_map_dup(bmap
);
1085 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1089 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1097 return isl_set_dup(set
);
1100 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1108 return isl_map_dup(map
);
1111 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1112 unsigned a_len
, unsigned b_len
)
1114 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1115 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1116 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1119 struct isl_basic_set
*isl_basic_set_swap_vars(
1120 struct isl_basic_set
*bset
, unsigned n
)
1130 nparam
= isl_basic_set_n_param(bset
);
1131 dim
= isl_basic_set_n_dim(bset
);
1132 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1137 bset
= isl_basic_set_cow(bset
);
1141 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1142 if (isl_blk_is_error(blk
))
1145 for (i
= 0; i
< bset
->n_eq
; ++i
)
1147 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1149 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1151 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1153 for (i
= 0; i
< bset
->n_div
; ++i
)
1155 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1157 isl_blk_free(bset
->ctx
, blk
);
1159 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1160 return isl_basic_set_gauss(bset
, NULL
);
1162 isl_basic_set_free(bset
);
1166 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1169 set
= isl_set_cow(set
);
1173 for (i
= 0; i
< set
->n
; ++i
) {
1174 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1180 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1184 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1190 total
= isl_basic_map_total_dim(bmap
);
1191 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1192 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1194 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1196 i
= isl_basic_map_alloc_equality(bmap
);
1200 isl_int_set_si(bmap
->eq
[i
][0], 1);
1201 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1202 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1203 isl_vec_free(bmap
->sample
);
1204 bmap
->sample
= NULL
;
1205 return isl_basic_map_finalize(bmap
);
1207 isl_basic_map_free(bmap
);
1211 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1213 return (struct isl_basic_set
*)
1214 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1217 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1220 unsigned off
= isl_dim_total(bmap
->dim
);
1221 isl_int
*t
= bmap
->div
[a
];
1222 bmap
->div
[a
] = bmap
->div
[b
];
1225 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1226 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1228 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1229 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1231 for (i
= 0; i
< bmap
->n_div
; ++i
)
1232 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1233 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1236 /* Eliminate the specified n dimensions starting at first from the
1237 * constraints using Fourier-Motzkin. The dimensions themselves
1240 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1241 enum isl_dim_type type
, unsigned first
, unsigned n
)
1250 map
= isl_map_cow(map
);
1253 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1254 first
+= pos(map
->dim
, type
) - 1;
1256 for (i
= 0; i
< map
->n
; ++i
) {
1257 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1267 /* Eliminate the specified n dimensions starting at first from the
1268 * constraints using Fourier-Motzkin. The dimensions themselves
1271 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1272 enum isl_dim_type type
, unsigned first
, unsigned n
)
1274 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1277 /* Eliminate the specified n dimensions starting at first from the
1278 * constraints using Fourier-Motzkin. The dimensions themselves
1281 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1282 unsigned first
, unsigned n
)
1284 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1287 /* Project out n dimensions starting at first using Fourier-Motzkin */
1288 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1289 unsigned first
, unsigned n
)
1291 set
= isl_set_eliminate_dims(set
, first
, n
);
1292 set
= isl_set_drop_dims(set
, first
, n
);
1296 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1298 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1306 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1315 set
= isl_set_cow(set
);
1319 for (i
= 0; i
< set
->n
; ++i
) {
1320 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1330 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1331 enum isl_dim_type type
, unsigned first
, unsigned n
)
1335 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1339 bmap
= isl_basic_map_eliminate_vars(bmap
,
1340 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1343 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1345 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1348 isl_basic_map_free(bmap
);
1352 __isl_give isl_basic_set
*isl_basic_set_remove(__isl_take isl_basic_set
*bset
,
1353 enum isl_dim_type type
, unsigned first
, unsigned n
)
1355 return (isl_basic_set
*)
1356 isl_basic_map_remove((isl_basic_map
*)bset
, type
, first
, n
);
1359 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1360 enum isl_dim_type type
, unsigned first
, unsigned n
)
1367 map
= isl_map_cow(map
);
1370 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1372 for (i
= 0; i
< map
->n
; ++i
) {
1373 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1374 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1378 map
= isl_map_drop(map
, type
, first
, n
);
1385 __isl_give isl_set
*isl_set_remove(__isl_take isl_set
*bset
,
1386 enum isl_dim_type type
, unsigned first
, unsigned n
)
1388 return (isl_set
*)isl_map_remove((isl_map
*)bset
, type
, first
, n
);
1391 /* Project out n inputs starting at first using Fourier-Motzkin */
1392 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1393 unsigned first
, unsigned n
)
1395 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1398 /* Project out n dimensions starting at first using Fourier-Motzkin */
1399 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1400 unsigned first
, unsigned n
)
1402 unsigned nparam
= isl_basic_set_n_param(bset
);
1403 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1404 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1408 static void dump_term(struct isl_basic_map
*bmap
,
1409 isl_int c
, int pos
, FILE *out
)
1412 unsigned in
= isl_basic_map_n_in(bmap
);
1413 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1414 unsigned nparam
= isl_basic_map_n_param(bmap
);
1416 isl_int_print(out
, c
, 0);
1418 if (!isl_int_is_one(c
))
1419 isl_int_print(out
, c
, 0);
1420 if (pos
< 1 + nparam
) {
1421 name
= isl_dim_get_name(bmap
->dim
,
1422 isl_dim_param
, pos
- 1);
1424 fprintf(out
, "%s", name
);
1426 fprintf(out
, "p%d", pos
- 1);
1427 } else if (pos
< 1 + nparam
+ in
)
1428 fprintf(out
, "i%d", pos
- 1 - nparam
);
1429 else if (pos
< 1 + nparam
+ dim
)
1430 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1432 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1436 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1437 int sign
, FILE *out
)
1441 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1445 for (i
= 0, first
= 1; i
< len
; ++i
) {
1446 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1449 fprintf(out
, " + ");
1451 isl_int_abs(v
, c
[i
]);
1452 dump_term(bmap
, v
, i
, out
);
1459 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1460 const char *op
, FILE *out
, int indent
)
1464 fprintf(out
, "%*s", indent
, "");
1466 dump_constraint_sign(bmap
, c
, 1, out
);
1467 fprintf(out
, " %s ", op
);
1468 dump_constraint_sign(bmap
, c
, -1, out
);
1472 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1473 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1475 fprintf(out
, "%*s", indent
, "");
1476 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1481 static void dump_constraints(struct isl_basic_map
*bmap
,
1482 isl_int
**c
, unsigned n
,
1483 const char *op
, FILE *out
, int indent
)
1487 for (i
= 0; i
< n
; ++i
)
1488 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1491 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1495 unsigned total
= isl_basic_map_total_dim(bmap
);
1497 for (j
= 0; j
< 1 + total
; ++j
) {
1498 if (isl_int_is_zero(exp
[j
]))
1500 if (!first
&& isl_int_is_pos(exp
[j
]))
1502 dump_term(bmap
, exp
[j
], j
, out
);
1507 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1511 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1512 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1514 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1515 fprintf(out
, "%*s", indent
, "");
1516 fprintf(out
, "e%d = [(", i
);
1517 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1519 isl_int_print(out
, bmap
->div
[i
][0], 0);
1520 fprintf(out
, "]\n");
1524 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1527 fprintf(out
, "null basic set\n");
1531 fprintf(out
, "%*s", indent
, "");
1532 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1533 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1534 bset
->extra
, bset
->flags
);
1535 dump((struct isl_basic_map
*)bset
, out
, indent
);
1538 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1541 fprintf(out
, "null basic map\n");
1545 fprintf(out
, "%*s", indent
, "");
1546 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1547 "flags: %x, n_name: %d\n",
1549 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1550 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1551 dump(bmap
, out
, indent
);
1554 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1559 total
= isl_basic_map_total_dim(bmap
);
1560 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1561 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1562 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1563 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1567 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1569 struct isl_set
*set
;
1573 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1574 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1575 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1576 sizeof(struct isl_set
) +
1577 (n
- 1) * sizeof(struct isl_basic_set
*));
1581 set
->ctx
= dim
->ctx
;
1582 isl_ctx_ref(set
->ctx
);
1594 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1595 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1597 struct isl_set
*set
;
1598 struct isl_dim
*dims
;
1600 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1604 set
= isl_set_alloc_dim(dims
, n
, flags
);
1608 /* Make sure "map" has room for at least "n" more basic maps.
1610 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1613 struct isl_map
*grown
= NULL
;
1617 isl_assert(map
->ctx
, n
>= 0, goto error
);
1618 if (map
->n
+ n
<= map
->size
)
1620 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1623 for (i
= 0; i
< map
->n
; ++i
) {
1624 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1632 isl_map_free(grown
);
1637 /* Make sure "set" has room for at least "n" more basic sets.
1639 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1641 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1644 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1647 struct isl_set
*dup
;
1652 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1655 for (i
= 0; i
< set
->n
; ++i
)
1656 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1660 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1662 struct isl_set
*set
;
1667 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1669 isl_basic_set_free(bset
);
1672 return isl_set_add_basic_set(set
, bset
);
1675 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1677 struct isl_map
*map
;
1682 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1684 isl_basic_map_free(bmap
);
1687 return isl_map_add_basic_map(map
, bmap
);
1690 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1691 __isl_take isl_basic_set
*bset
)
1693 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1694 (struct isl_basic_map
*)bset
);
1697 void isl_set_free(struct isl_set
*set
)
1707 isl_ctx_deref(set
->ctx
);
1708 for (i
= 0; i
< set
->n
; ++i
)
1709 isl_basic_set_free(set
->p
[i
]);
1710 isl_dim_free(set
->dim
);
1714 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1719 fprintf(out
, "null set\n");
1723 fprintf(out
, "%*s", indent
, "");
1724 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1725 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1727 for (i
= 0; i
< set
->n
; ++i
) {
1728 fprintf(out
, "%*s", indent
, "");
1729 fprintf(out
, "basic set %d:\n", i
);
1730 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1734 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1739 fprintf(out
, "null map\n");
1743 fprintf(out
, "%*s", indent
, "");
1744 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1745 "flags: %x, n_name: %d\n",
1746 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1747 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1748 for (i
= 0; i
< map
->n
; ++i
) {
1749 fprintf(out
, "%*s", indent
, "");
1750 fprintf(out
, "basic map %d:\n", i
);
1751 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1755 struct isl_basic_map
*isl_basic_map_intersect_domain(
1756 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1758 struct isl_basic_map
*bmap_domain
;
1759 struct isl_dim
*dim
;
1764 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1765 bset
->dim
, isl_dim_param
), goto error
);
1767 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1768 isl_assert(bset
->ctx
,
1769 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1771 bmap
= isl_basic_map_cow(bmap
);
1774 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1775 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1776 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1777 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1778 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1780 bmap
= isl_basic_map_simplify(bmap
);
1781 return isl_basic_map_finalize(bmap
);
1783 isl_basic_map_free(bmap
);
1784 isl_basic_set_free(bset
);
1788 struct isl_basic_map
*isl_basic_map_intersect_range(
1789 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1791 struct isl_basic_map
*bmap_range
;
1796 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1797 bset
->dim
, isl_dim_param
), goto error
);
1799 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1800 isl_assert(bset
->ctx
,
1801 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1803 bmap
= isl_basic_map_cow(bmap
);
1806 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1807 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1808 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1809 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1811 bmap
= isl_basic_map_simplify(bmap
);
1812 return isl_basic_map_finalize(bmap
);
1814 isl_basic_map_free(bmap
);
1815 isl_basic_set_free(bset
);
1819 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1825 total
= 1 + isl_basic_map_total_dim(bmap
);
1826 if (total
!= vec
->size
)
1831 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1832 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1833 if (!isl_int_is_zero(s
)) {
1839 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1840 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1841 if (isl_int_is_neg(s
)) {
1852 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1854 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1857 struct isl_basic_map
*isl_basic_map_intersect(
1858 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1860 struct isl_vec
*sample
= NULL
;
1862 if (!bmap1
|| !bmap2
)
1865 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1866 bmap2
->dim
, isl_dim_param
), goto error
);
1867 if (isl_dim_total(bmap1
->dim
) ==
1868 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1869 isl_dim_total(bmap2
->dim
) !=
1870 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1871 return isl_basic_map_intersect(bmap2
, bmap1
);
1873 if (isl_dim_total(bmap2
->dim
) !=
1874 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1875 isl_assert(bmap1
->ctx
,
1876 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1878 if (bmap1
->sample
&&
1879 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1880 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1881 sample
= isl_vec_copy(bmap1
->sample
);
1882 else if (bmap2
->sample
&&
1883 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1884 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1885 sample
= isl_vec_copy(bmap2
->sample
);
1887 bmap1
= isl_basic_map_cow(bmap1
);
1890 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1891 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1892 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1895 isl_vec_free(sample
);
1897 isl_vec_free(bmap1
->sample
);
1898 bmap1
->sample
= sample
;
1901 bmap1
= isl_basic_map_simplify(bmap1
);
1902 return isl_basic_map_finalize(bmap1
);
1905 isl_vec_free(sample
);
1906 isl_basic_map_free(bmap1
);
1907 isl_basic_map_free(bmap2
);
1911 struct isl_basic_set
*isl_basic_set_intersect(
1912 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1914 return (struct isl_basic_set
*)
1915 isl_basic_map_intersect(
1916 (struct isl_basic_map
*)bset1
,
1917 (struct isl_basic_map
*)bset2
);
1920 /* Special case of isl_map_intersect, where both map1 and map2
1921 * are convex, without any divs and such that either map1 or map2
1922 * contains a single constraint. This constraint is then simply
1923 * added to the other map.
1925 static __isl_give isl_map
*map_intersect_add_constraint(
1926 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
1928 struct isl_basic_map
*bmap1
;
1929 struct isl_basic_map
*bmap2
;
1931 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
1932 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
1933 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
1934 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
1936 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
1937 return isl_map_intersect(map2
, map1
);
1939 isl_assert(map2
->ctx
,
1940 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
1942 map1
= isl_map_cow(map1
);
1945 if (isl_map_fast_is_empty(map1
)) {
1949 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
1950 if (map2
->p
[0]->n_eq
== 1)
1951 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
1953 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
1954 map2
->p
[0]->ineq
[0]);
1956 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
1957 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
1961 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
1962 isl_basic_map_free(map1
->p
[0]);
1975 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1978 struct isl_map
*result
;
1984 if (isl_map_fast_is_empty(map1
)) {
1988 if (isl_map_fast_is_empty(map2
)) {
1993 if (map1
->n
== 1 && map2
->n
== 1 &&
1994 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
1995 isl_dim_equal(map1
->dim
, map2
->dim
) &&
1996 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
1997 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
1998 return map_intersect_add_constraint(map1
, map2
);
1999 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2000 map2
->dim
, isl_dim_param
), goto error
);
2001 if (isl_dim_total(map1
->dim
) ==
2002 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2003 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2004 return isl_map_intersect(map2
, map1
);
2006 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2007 isl_assert(map1
->ctx
,
2008 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2010 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2011 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2012 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2014 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2015 map1
->n
* map2
->n
, flags
);
2018 for (i
= 0; i
< map1
->n
; ++i
)
2019 for (j
= 0; j
< map2
->n
; ++j
) {
2020 struct isl_basic_map
*part
;
2021 part
= isl_basic_map_intersect(
2022 isl_basic_map_copy(map1
->p
[i
]),
2023 isl_basic_map_copy(map2
->p
[j
]));
2024 if (isl_basic_map_is_empty(part
))
2025 isl_basic_map_free(part
);
2027 result
= isl_map_add_basic_map(result
, part
);
2040 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2042 return (struct isl_set
*)
2043 isl_map_intersect((struct isl_map
*)set1
,
2044 (struct isl_map
*)set2
);
2047 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2049 struct isl_dim
*dim
;
2050 struct isl_basic_set
*bset
;
2055 bmap
= isl_basic_map_cow(bmap
);
2058 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2059 in
= isl_basic_map_n_in(bmap
);
2060 bset
= isl_basic_set_from_basic_map(bmap
);
2061 bset
= isl_basic_set_swap_vars(bset
, in
);
2062 return isl_basic_map_from_basic_set(bset
, dim
);
2065 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2066 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2068 struct isl_dim
*res_dim
;
2069 struct isl_basic_map
*res
;
2070 struct isl_dim_map
*dim_map
;
2071 unsigned total
, off
;
2072 enum isl_dim_type t
;
2080 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2082 total
= isl_basic_map_total_dim(bmap
) + n
;
2083 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2085 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2087 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2089 unsigned size
= isl_basic_map_dim(bmap
, t
);
2090 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2092 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2093 pos
, size
- pos
, off
+ pos
+ n
);
2095 off
+= isl_dim_size(res_dim
, t
);
2097 isl_dim_map_div(dim_map
, bmap
, off
);
2099 res
= isl_basic_map_alloc_dim(res_dim
,
2100 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2101 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2102 res
= isl_basic_map_simplify(res
);
2103 return isl_basic_map_finalize(res
);
2106 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2107 enum isl_dim_type type
, unsigned n
)
2111 return isl_basic_map_insert(bmap
, type
,
2112 isl_basic_map_dim(bmap
, type
), n
);
2115 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2116 enum isl_dim_type type
, unsigned n
)
2120 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2121 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2123 isl_basic_set_free(bset
);
2127 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2128 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2135 map
= isl_map_cow(map
);
2139 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2143 for (i
= 0; i
< map
->n
; ++i
) {
2144 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2155 __isl_give isl_map
*isl_map_add(__isl_take isl_map
*map
,
2156 enum isl_dim_type type
, unsigned n
)
2160 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2163 __isl_give isl_set
*isl_set_add(__isl_take isl_set
*set
,
2164 enum isl_dim_type type
, unsigned n
)
2168 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2169 return (isl_set
*)isl_map_add((isl_map
*)set
, type
, n
);
2175 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2176 __isl_take isl_basic_map
*bmap
,
2177 enum isl_dim_type dst_type
, unsigned dst_pos
,
2178 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2181 struct isl_dim_map
*dim_map
;
2182 struct isl_basic_map
*res
;
2183 enum isl_dim_type t
;
2184 unsigned total
, off
;
2191 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2194 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2197 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2199 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2200 pos(bmap
->dim
, src_type
) + src_pos
+
2201 ((src_type
< dst_type
) ? n
: 0)) {
2202 bmap
= isl_basic_map_cow(bmap
);
2206 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2207 src_type
, src_pos
, n
);
2211 bmap
= isl_basic_map_finalize(bmap
);
2216 total
= isl_basic_map_total_dim(bmap
);
2217 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2220 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2221 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2222 if (t
== dst_type
) {
2223 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2226 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2229 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2230 dst_pos
, size
- dst_pos
, off
);
2231 off
+= size
- dst_pos
;
2232 } else if (t
== src_type
) {
2233 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2236 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2237 src_pos
+ n
, size
- src_pos
- n
, off
);
2238 off
+= size
- src_pos
- n
;
2240 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2244 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2246 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2247 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2248 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2250 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2251 src_type
, src_pos
, n
);
2255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2256 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2257 bmap
= isl_basic_map_finalize(bmap
);
2261 isl_basic_map_free(bmap
);
2265 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2266 enum isl_dim_type dst_type
, unsigned dst_pos
,
2267 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2269 return (isl_basic_set
*)isl_basic_map_move_dims(
2270 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2273 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2274 enum isl_dim_type dst_type
, unsigned dst_pos
,
2275 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2279 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2280 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2281 src_type
, src_pos
, n
);
2287 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2288 enum isl_dim_type dst_type
, unsigned dst_pos
,
2289 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2298 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2301 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2304 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2306 map
= isl_map_cow(map
);
2310 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2314 for (i
= 0; i
< map
->n
; ++i
) {
2315 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2317 src_type
, src_pos
, n
);
2328 /* Move the specified dimensions to the last columns right before
2329 * the divs. Don't change the dimension specification of bmap.
2330 * That's the responsibility of the caller.
2332 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2333 enum isl_dim_type type
, unsigned first
, unsigned n
)
2335 struct isl_dim_map
*dim_map
;
2336 struct isl_basic_map
*res
;
2337 enum isl_dim_type t
;
2338 unsigned total
, off
;
2342 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2345 total
= isl_basic_map_total_dim(bmap
);
2346 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2349 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2350 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2352 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2355 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2356 first
, n
, total
- bmap
->n_div
- n
);
2357 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2358 first
+ n
, size
- (first
+ n
), off
);
2359 off
+= size
- (first
+ n
);
2361 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2365 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2367 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2368 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2369 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2373 /* Turn the n dimensions of type type, starting at first
2374 * into existentially quantified variables.
2376 __isl_give isl_basic_map
*isl_basic_map_project_out(
2377 __isl_take isl_basic_map
*bmap
,
2378 enum isl_dim_type type
, unsigned first
, unsigned n
)
2391 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2392 return isl_basic_map_remove(bmap
, type
, first
, n
);
2394 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2397 bmap
= move_last(bmap
, type
, first
, n
);
2398 bmap
= isl_basic_map_cow(bmap
);
2400 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2401 old
= bmap
->block2
.data
;
2402 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2403 (bmap
->extra
+ n
) * (1 + row_size
));
2404 if (!bmap
->block2
.data
)
2406 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2409 for (i
= 0; i
< n
; ++i
) {
2410 new_div
[i
] = bmap
->block2
.data
+
2411 (bmap
->extra
+ i
) * (1 + row_size
);
2412 isl_seq_clr(new_div
[i
], 1 + row_size
);
2414 for (i
= 0; i
< bmap
->extra
; ++i
)
2415 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2417 bmap
->div
= new_div
;
2421 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2424 bmap
= isl_basic_map_simplify(bmap
);
2425 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2426 return isl_basic_map_finalize(bmap
);
2428 isl_basic_map_free(bmap
);
2432 /* Turn the n dimensions of type type, starting at first
2433 * into existentially quantified variables.
2435 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2436 enum isl_dim_type type
, unsigned first
, unsigned n
)
2438 return (isl_basic_set
*)isl_basic_map_project_out(
2439 (isl_basic_map
*)bset
, type
, first
, n
);
2442 /* Turn the n dimensions of type type, starting at first
2443 * into existentially quantified variables.
2445 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2446 enum isl_dim_type type
, unsigned first
, unsigned n
)
2456 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2458 map
= isl_map_cow(map
);
2462 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2466 for (i
= 0; i
< map
->n
; ++i
) {
2467 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2478 /* Turn the n dimensions of type type, starting at first
2479 * into existentially quantified variables.
2481 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2482 enum isl_dim_type type
, unsigned first
, unsigned n
)
2484 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2487 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2491 for (i
= 0; i
< n
; ++i
) {
2492 j
= isl_basic_map_alloc_div(bmap
);
2495 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2499 isl_basic_map_free(bmap
);
2503 struct isl_basic_map
*isl_basic_map_apply_range(
2504 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2506 struct isl_dim
*dim_result
= NULL
;
2507 struct isl_basic_map
*bmap
;
2508 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2509 struct isl_dim_map
*dim_map1
, *dim_map2
;
2511 if (!bmap1
|| !bmap2
)
2514 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2515 isl_dim_copy(bmap2
->dim
));
2517 n_in
= isl_basic_map_n_in(bmap1
);
2518 n_out
= isl_basic_map_n_out(bmap2
);
2519 n
= isl_basic_map_n_out(bmap1
);
2520 nparam
= isl_basic_map_n_param(bmap1
);
2522 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2523 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2524 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2525 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2526 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2527 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2528 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2529 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2530 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2531 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2532 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2534 bmap
= isl_basic_map_alloc_dim(dim_result
,
2535 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2536 bmap1
->n_eq
+ bmap2
->n_eq
,
2537 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2538 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2539 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2540 bmap
= add_divs(bmap
, n
);
2541 bmap
= isl_basic_map_simplify(bmap
);
2542 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2543 return isl_basic_map_finalize(bmap
);
2545 isl_basic_map_free(bmap1
);
2546 isl_basic_map_free(bmap2
);
2550 struct isl_basic_set
*isl_basic_set_apply(
2551 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2556 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2559 return (struct isl_basic_set
*)
2560 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2562 isl_basic_set_free(bset
);
2563 isl_basic_map_free(bmap
);
2567 struct isl_basic_map
*isl_basic_map_apply_domain(
2568 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2570 if (!bmap1
|| !bmap2
)
2573 isl_assert(bmap1
->ctx
,
2574 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2575 isl_assert(bmap1
->ctx
,
2576 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2579 bmap1
= isl_basic_map_reverse(bmap1
);
2580 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2581 return isl_basic_map_reverse(bmap1
);
2583 isl_basic_map_free(bmap1
);
2584 isl_basic_map_free(bmap2
);
2588 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2589 * A \cap B -> f(A) + f(B)
2591 struct isl_basic_map
*isl_basic_map_sum(
2592 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2594 unsigned n_in
, n_out
, nparam
, total
, pos
;
2595 struct isl_basic_map
*bmap
= NULL
;
2596 struct isl_dim_map
*dim_map1
, *dim_map2
;
2599 if (!bmap1
|| !bmap2
)
2602 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2605 nparam
= isl_basic_map_n_param(bmap1
);
2606 n_in
= isl_basic_map_n_in(bmap1
);
2607 n_out
= isl_basic_map_n_out(bmap1
);
2609 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2610 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2611 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2612 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2613 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2614 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2615 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2616 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2617 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2618 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2619 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2621 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2622 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2623 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2624 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2625 for (i
= 0; i
< n_out
; ++i
) {
2626 int j
= isl_basic_map_alloc_equality(bmap
);
2629 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2630 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2631 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2632 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2634 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2635 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2636 bmap
= add_divs(bmap
, 2 * n_out
);
2638 bmap
= isl_basic_map_simplify(bmap
);
2639 return isl_basic_map_finalize(bmap
);
2641 isl_basic_map_free(bmap
);
2642 isl_basic_map_free(bmap1
);
2643 isl_basic_map_free(bmap2
);
2647 /* Given two maps A -> f(A) and B -> g(B), construct a map
2648 * A \cap B -> f(A) + f(B)
2650 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2652 struct isl_map
*result
;
2658 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2660 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2661 map1
->n
* map2
->n
, 0);
2664 for (i
= 0; i
< map1
->n
; ++i
)
2665 for (j
= 0; j
< map2
->n
; ++j
) {
2666 struct isl_basic_map
*part
;
2667 part
= isl_basic_map_sum(
2668 isl_basic_map_copy(map1
->p
[i
]),
2669 isl_basic_map_copy(map2
->p
[j
]));
2670 if (isl_basic_map_is_empty(part
))
2671 isl_basic_map_free(part
);
2673 result
= isl_map_add_basic_map(result
, part
);
2686 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2687 __isl_take isl_set
*set2
)
2689 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2692 /* Given a basic map A -> f(A), construct A -> -f(A).
2694 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2699 bmap
= isl_basic_map_cow(bmap
);
2703 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2704 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2705 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2706 for (j
= 0; j
< n
; ++j
)
2707 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2708 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2709 for (j
= 0; j
< n
; ++j
)
2710 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2711 for (i
= 0; i
< bmap
->n_div
; ++i
)
2712 for (j
= 0; j
< n
; ++j
)
2713 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2714 return isl_basic_map_finalize(bmap
);
2717 /* Given a map A -> f(A), construct A -> -f(A).
2719 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2723 map
= isl_map_cow(map
);
2727 for (i
= 0; i
< map
->n
; ++i
) {
2728 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2739 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2741 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2744 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2745 * A -> floor(f(A)/d).
2747 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2750 unsigned n_in
, n_out
, nparam
, total
, pos
;
2751 struct isl_basic_map
*result
= NULL
;
2752 struct isl_dim_map
*dim_map
;
2758 nparam
= isl_basic_map_n_param(bmap
);
2759 n_in
= isl_basic_map_n_in(bmap
);
2760 n_out
= isl_basic_map_n_out(bmap
);
2762 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2763 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2764 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2765 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2766 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2767 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2769 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2770 bmap
->n_div
+ n_out
,
2771 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2772 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2773 result
= add_divs(result
, n_out
);
2774 for (i
= 0; i
< n_out
; ++i
) {
2776 j
= isl_basic_map_alloc_inequality(result
);
2779 isl_seq_clr(result
->ineq
[j
], 1+total
);
2780 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2781 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2782 j
= isl_basic_map_alloc_inequality(result
);
2785 isl_seq_clr(result
->ineq
[j
], 1+total
);
2786 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2787 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2788 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2791 result
= isl_basic_map_simplify(result
);
2792 return isl_basic_map_finalize(result
);
2794 isl_basic_map_free(result
);
2798 /* Given a map A -> f(A) and an integer d, construct a map
2799 * A -> floor(f(A)/d).
2801 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2805 map
= isl_map_cow(map
);
2809 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2810 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2811 for (i
= 0; i
< map
->n
; ++i
) {
2812 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2823 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2829 i
= isl_basic_map_alloc_equality(bmap
);
2832 nparam
= isl_basic_map_n_param(bmap
);
2833 n_in
= isl_basic_map_n_in(bmap
);
2834 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2835 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2836 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2837 return isl_basic_map_finalize(bmap
);
2839 isl_basic_map_free(bmap
);
2843 /* Add a constraints to "bmap" expressing i_pos < o_pos
2845 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2851 i
= isl_basic_map_alloc_inequality(bmap
);
2854 nparam
= isl_basic_map_n_param(bmap
);
2855 n_in
= isl_basic_map_n_in(bmap
);
2856 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2857 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2858 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2859 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2860 return isl_basic_map_finalize(bmap
);
2862 isl_basic_map_free(bmap
);
2866 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2868 static __isl_give isl_basic_map
*var_less_or_equal(
2869 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2875 i
= isl_basic_map_alloc_inequality(bmap
);
2878 nparam
= isl_basic_map_n_param(bmap
);
2879 n_in
= isl_basic_map_n_in(bmap
);
2880 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2881 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2882 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2883 return isl_basic_map_finalize(bmap
);
2885 isl_basic_map_free(bmap
);
2889 /* Add a constraints to "bmap" expressing i_pos > o_pos
2891 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2897 i
= isl_basic_map_alloc_inequality(bmap
);
2900 nparam
= isl_basic_map_n_param(bmap
);
2901 n_in
= isl_basic_map_n_in(bmap
);
2902 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2903 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2904 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2905 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2906 return isl_basic_map_finalize(bmap
);
2908 isl_basic_map_free(bmap
);
2912 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2914 static __isl_give isl_basic_map
*var_more_or_equal(
2915 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2921 i
= isl_basic_map_alloc_inequality(bmap
);
2924 nparam
= isl_basic_map_n_param(bmap
);
2925 n_in
= isl_basic_map_n_in(bmap
);
2926 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2927 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2928 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2929 return isl_basic_map_finalize(bmap
);
2931 isl_basic_map_free(bmap
);
2935 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2938 struct isl_basic_map
*bmap
;
2939 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2942 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2943 bmap
= var_equal(bmap
, i
);
2944 return isl_basic_map_finalize(bmap
);
2947 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2949 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2952 struct isl_basic_map
*bmap
;
2953 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2956 for (i
= 0; i
< pos
&& bmap
; ++i
)
2957 bmap
= var_equal(bmap
, i
);
2959 bmap
= var_less(bmap
, pos
);
2960 return isl_basic_map_finalize(bmap
);
2963 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2965 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
2966 __isl_take isl_dim
*dim
, unsigned pos
)
2969 isl_basic_map
*bmap
;
2971 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2972 for (i
= 0; i
< pos
; ++i
)
2973 bmap
= var_equal(bmap
, i
);
2974 bmap
= var_less_or_equal(bmap
, pos
);
2975 return isl_basic_map_finalize(bmap
);
2978 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2980 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2983 struct isl_basic_map
*bmap
;
2984 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2987 for (i
= 0; i
< pos
&& bmap
; ++i
)
2988 bmap
= var_equal(bmap
, i
);
2990 bmap
= var_more(bmap
, pos
);
2991 return isl_basic_map_finalize(bmap
);
2994 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
2996 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
2997 __isl_take isl_dim
*dim
, unsigned pos
)
3000 isl_basic_map
*bmap
;
3002 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3003 for (i
= 0; i
< pos
; ++i
)
3004 bmap
= var_equal(bmap
, i
);
3005 bmap
= var_more_or_equal(bmap
, pos
);
3006 return isl_basic_map_finalize(bmap
);
3009 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3010 unsigned n
, int equal
)
3012 struct isl_map
*map
;
3015 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3017 for (i
= 0; i
+ 1 < n
; ++i
)
3018 map
= isl_map_add_basic_map(map
,
3019 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3022 map
= isl_map_add_basic_map(map
,
3023 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3025 map
= isl_map_add_basic_map(map
,
3026 isl_basic_map_less_at(dims
, n
- 1));
3033 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3037 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3040 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3042 return map_lex_lte_first(dim
, n
, 0);
3045 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3047 return map_lex_lte_first(dim
, n
, 1);
3050 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3052 return map_lex_lte(isl_dim_map(set_dim
), 0);
3055 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3057 return map_lex_lte(isl_dim_map(set_dim
), 1);
3060 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3061 unsigned n
, int equal
)
3063 struct isl_map
*map
;
3066 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3068 for (i
= 0; i
+ 1 < n
; ++i
)
3069 map
= isl_map_add_basic_map(map
,
3070 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3073 map
= isl_map_add_basic_map(map
,
3074 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3076 map
= isl_map_add_basic_map(map
,
3077 isl_basic_map_more_at(dims
, n
- 1));
3084 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3088 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3091 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3093 return map_lex_gte_first(dim
, n
, 0);
3096 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3098 return map_lex_gte_first(dim
, n
, 1);
3101 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3103 return map_lex_gte(isl_dim_map(set_dim
), 0);
3106 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3108 return map_lex_gte(isl_dim_map(set_dim
), 1);
3111 struct isl_basic_map
*isl_basic_map_from_basic_set(
3112 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3114 struct isl_basic_map
*bmap
;
3116 bset
= isl_basic_set_cow(bset
);
3120 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3121 isl_dim_free(bset
->dim
);
3122 bmap
= (struct isl_basic_map
*) bset
;
3124 return isl_basic_map_finalize(bmap
);
3126 isl_basic_set_free(bset
);
3131 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3135 if (bmap
->dim
->n_in
== 0)
3136 return (struct isl_basic_set
*)bmap
;
3137 bmap
= isl_basic_map_cow(bmap
);
3140 bmap
->dim
= isl_dim_cow(bmap
->dim
);
3143 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
3144 bmap
->dim
->n_in
= 0;
3145 bmap
= isl_basic_map_finalize(bmap
);
3146 return (struct isl_basic_set
*)bmap
;
3148 isl_basic_map_free(bmap
);
3152 /* For a div d = floor(f/m), add the constraints
3155 * -(f-(n-1)) + m d >= 0
3157 * Note that the second constraint is the negation of
3161 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3162 unsigned pos
, isl_int
*div
)
3165 unsigned total
= isl_basic_map_total_dim(bmap
);
3167 i
= isl_basic_map_alloc_inequality(bmap
);
3170 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3171 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3173 j
= isl_basic_map_alloc_inequality(bmap
);
3176 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3177 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3178 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3182 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3183 unsigned pos
, isl_int
*div
)
3185 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3189 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3191 unsigned total
= isl_basic_map_total_dim(bmap
);
3192 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3194 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3198 struct isl_basic_set
*isl_basic_map_underlying_set(
3199 struct isl_basic_map
*bmap
)
3203 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
3204 return (struct isl_basic_set
*)bmap
;
3205 bmap
= isl_basic_map_cow(bmap
);
3208 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3211 bmap
->extra
-= bmap
->n_div
;
3213 bmap
= isl_basic_map_finalize(bmap
);
3214 return (struct isl_basic_set
*)bmap
;
3219 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3220 __isl_take isl_basic_set
*bset
)
3222 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3225 struct isl_basic_map
*isl_basic_map_overlying_set(
3226 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3228 struct isl_basic_map
*bmap
;
3229 struct isl_ctx
*ctx
;
3236 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3237 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3238 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3240 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3241 isl_basic_map_free(like
);
3242 return (struct isl_basic_map
*)bset
;
3244 bset
= isl_basic_set_cow(bset
);
3247 total
= bset
->dim
->n_out
+ bset
->extra
;
3248 bmap
= (struct isl_basic_map
*)bset
;
3249 isl_dim_free(bmap
->dim
);
3250 bmap
->dim
= isl_dim_copy(like
->dim
);
3253 bmap
->n_div
= like
->n_div
;
3254 bmap
->extra
+= like
->n_div
;
3257 ltotal
= total
- bmap
->extra
+ like
->extra
;
3260 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3261 bmap
->extra
* (1 + 1 + total
));
3262 if (isl_blk_is_error(bmap
->block2
))
3264 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3268 for (i
= 0; i
< bmap
->extra
; ++i
)
3269 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3270 for (i
= 0; i
< like
->n_div
; ++i
) {
3271 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3272 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3274 bmap
= isl_basic_map_extend_constraints(bmap
,
3275 0, 2 * like
->n_div
);
3276 for (i
= 0; i
< like
->n_div
; ++i
) {
3277 if (isl_int_is_zero(bmap
->div
[i
][0]))
3279 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3283 isl_basic_map_free(like
);
3284 bmap
= isl_basic_map_simplify(bmap
);
3285 bmap
= isl_basic_map_finalize(bmap
);
3288 isl_basic_map_free(like
);
3289 isl_basic_set_free(bset
);
3293 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3294 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3296 return (struct isl_basic_set
*)
3297 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3300 struct isl_set
*isl_set_from_underlying_set(
3301 struct isl_set
*set
, struct isl_basic_set
*like
)
3307 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3309 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3310 isl_basic_set_free(like
);
3313 set
= isl_set_cow(set
);
3316 for (i
= 0; i
< set
->n
; ++i
) {
3317 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3318 isl_basic_set_copy(like
));
3322 isl_dim_free(set
->dim
);
3323 set
->dim
= isl_dim_copy(like
->dim
);
3326 isl_basic_set_free(like
);
3329 isl_basic_set_free(like
);
3334 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3338 map
= isl_map_cow(map
);
3341 map
->dim
= isl_dim_cow(map
->dim
);
3345 for (i
= 1; i
< map
->n
; ++i
)
3346 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3348 for (i
= 0; i
< map
->n
; ++i
) {
3349 map
->p
[i
] = (struct isl_basic_map
*)
3350 isl_basic_map_underlying_set(map
->p
[i
]);
3355 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3357 isl_dim_free(map
->dim
);
3358 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3362 return (struct isl_set
*)map
;
3368 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3370 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3373 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3375 struct isl_basic_set
*domain
;
3380 n_in
= isl_basic_map_n_in(bmap
);
3381 n_out
= isl_basic_map_n_out(bmap
);
3382 domain
= isl_basic_set_from_basic_map(bmap
);
3383 return isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3386 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3388 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3391 struct isl_set
*isl_map_range(struct isl_map
*map
)
3394 struct isl_set
*set
;
3398 if (isl_map_dim(map
, isl_dim_in
) == 0)
3399 return (isl_set
*)map
;
3401 map
= isl_map_cow(map
);
3405 set
= (struct isl_set
*) map
;
3406 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3409 for (i
= 0; i
< map
->n
; ++i
) {
3410 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3414 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3415 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3422 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3425 struct isl_map
*map
= NULL
;
3427 set
= isl_set_cow(set
);
3430 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3431 map
= (struct isl_map
*)set
;
3432 for (i
= 0; i
< set
->n
; ++i
) {
3433 map
->p
[i
] = isl_basic_map_from_basic_set(
3434 set
->p
[i
], isl_dim_copy(dim
));
3438 isl_dim_free(map
->dim
);
3447 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3449 return (struct isl_map
*)set
;
3452 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3454 return isl_map_reverse(isl_map_from_range(set
));;
3457 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3458 __isl_take isl_set
*range
)
3460 return isl_map_product(isl_map_from_domain(domain
),
3461 isl_map_from_range(range
));
3464 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3467 struct isl_set
*set
= NULL
;
3471 map
= isl_map_cow(map
);
3474 map
->dim
= isl_dim_cow(map
->dim
);
3477 map
->dim
->n_out
+= map
->dim
->n_in
;
3479 set
= (struct isl_set
*)map
;
3480 for (i
= 0; i
< map
->n
; ++i
) {
3481 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3491 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3493 struct isl_map
*map
;
3497 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3498 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3499 sizeof(struct isl_map
) +
3500 (n
- 1) * sizeof(struct isl_basic_map
*));
3504 map
->ctx
= dim
->ctx
;
3505 isl_ctx_ref(map
->ctx
);
3517 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
3518 unsigned nparam
, unsigned in
, unsigned out
, int n
,
3521 struct isl_map
*map
;
3522 struct isl_dim
*dims
;
3524 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
3528 map
= isl_map_alloc_dim(dims
, n
, flags
);
3532 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
3534 struct isl_basic_map
*bmap
;
3535 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
3536 bmap
= isl_basic_map_set_to_empty(bmap
);
3540 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
3542 struct isl_basic_set
*bset
;
3543 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
3544 bset
= isl_basic_set_set_to_empty(bset
);
3548 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
3550 struct isl_basic_map
*bmap
;
3553 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3554 bmap
= isl_basic_map_set_to_empty(bmap
);
3558 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
3560 struct isl_basic_map
*bmap
;
3563 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3564 bmap
= isl_basic_map_set_to_empty(bmap
);
3568 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
3570 struct isl_basic_set
*bset
;
3573 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3574 bset
= isl_basic_set_set_to_empty(bset
);
3578 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
3580 struct isl_basic_map
*bmap
;
3581 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
3585 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
3587 struct isl_basic_set
*bset
;
3588 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
3592 __isl_give isl_basic_map
*isl_basic_map_universe_like(
3593 __isl_keep isl_basic_map
*model
)
3597 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3600 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
3604 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3607 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
3608 __isl_keep isl_set
*model
)
3612 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3615 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
3617 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3620 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
3624 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3627 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
3631 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3634 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
3636 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3639 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
3643 return isl_set_empty(isl_dim_copy(model
->dim
));
3646 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
3648 struct isl_map
*map
;
3651 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3652 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
3656 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
3658 struct isl_set
*set
;
3661 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3662 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
3666 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3670 return isl_set_universe(isl_dim_copy(model
->dim
));
3673 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3676 struct isl_map
*dup
;
3680 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3681 for (i
= 0; i
< map
->n
; ++i
)
3682 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
3686 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
3687 __isl_take isl_basic_map
*bmap
)
3691 if (isl_basic_map_fast_is_empty(bmap
)) {
3692 isl_basic_map_free(bmap
);
3695 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3696 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3697 map
->p
[map
->n
] = bmap
;
3699 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3705 isl_basic_map_free(bmap
);
3709 void isl_map_free(struct isl_map
*map
)
3719 isl_ctx_deref(map
->ctx
);
3720 for (i
= 0; i
< map
->n
; ++i
)
3721 isl_basic_map_free(map
->p
[i
]);
3722 isl_dim_free(map
->dim
);
3726 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3727 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3731 base
= isl_map_cow(base
);
3735 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3738 for (i
= 0; i
< base
->n
; ++i
) {
3739 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3740 isl_dim_copy(base
->dim
), 0, 0, 0);
3750 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3751 unsigned nparam
, unsigned dim
)
3753 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3757 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3758 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3762 bmap
= isl_basic_map_cow(bmap
);
3763 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3764 j
= isl_basic_map_alloc_equality(bmap
);
3767 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3768 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3769 isl_int_set_si(bmap
->eq
[j
][0], value
);
3770 bmap
= isl_basic_map_simplify(bmap
);
3771 return isl_basic_map_finalize(bmap
);
3773 isl_basic_map_free(bmap
);
3777 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3778 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3782 bmap
= isl_basic_map_cow(bmap
);
3783 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3784 j
= isl_basic_map_alloc_equality(bmap
);
3787 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3788 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3789 isl_int_set(bmap
->eq
[j
][0], value
);
3790 bmap
= isl_basic_map_simplify(bmap
);
3791 return isl_basic_map_finalize(bmap
);
3793 isl_basic_map_free(bmap
);
3797 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3798 enum isl_dim_type type
, unsigned pos
, int value
)
3802 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3803 return isl_basic_map_fix_pos_si(bmap
,
3804 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3806 isl_basic_map_free(bmap
);
3810 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3811 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3815 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3816 return isl_basic_map_fix_pos(bmap
,
3817 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3819 isl_basic_map_free(bmap
);
3823 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3824 enum isl_dim_type type
, unsigned pos
, int value
)
3826 return (struct isl_basic_set
*)
3827 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3831 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3832 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3834 return (struct isl_basic_set
*)
3835 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3839 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3840 unsigned input
, int value
)
3842 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3845 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3846 unsigned dim
, int value
)
3848 return (struct isl_basic_set
*)
3849 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3850 isl_dim_set
, dim
, value
);
3853 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3854 enum isl_dim_type type
, unsigned pos
, int value
)
3858 map
= isl_map_cow(map
);
3862 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3863 for (i
= 0; i
< map
->n
; ++i
) {
3864 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
3868 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3875 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
3876 enum isl_dim_type type
, unsigned pos
, int value
)
3878 return (struct isl_set
*)
3879 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
3882 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
3883 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3887 map
= isl_map_cow(map
);
3891 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3892 for (i
= 0; i
< map
->n
; ++i
) {
3893 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
3897 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3904 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
3905 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3907 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
3910 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
3911 unsigned input
, int value
)
3913 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
3916 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
3918 return (struct isl_set
*)
3919 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
3922 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
3923 __isl_take isl_basic_map
*bmap
,
3924 enum isl_dim_type type
, unsigned pos
, int value
)
3930 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3931 pos
+= isl_basic_map_offset(bmap
, type
);
3932 bmap
= isl_basic_map_cow(bmap
);
3933 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
3934 j
= isl_basic_map_alloc_inequality(bmap
);
3937 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3938 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
3939 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
3940 bmap
= isl_basic_map_simplify(bmap
);
3941 return isl_basic_map_finalize(bmap
);
3943 isl_basic_map_free(bmap
);
3947 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
3948 unsigned dim
, isl_int value
)
3952 bset
= isl_basic_set_cow(bset
);
3953 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
3954 j
= isl_basic_set_alloc_inequality(bset
);
3957 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3958 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3959 isl_int_neg(bset
->ineq
[j
][0], value
);
3960 bset
= isl_basic_set_simplify(bset
);
3961 return isl_basic_set_finalize(bset
);
3963 isl_basic_set_free(bset
);
3967 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
3968 enum isl_dim_type type
, unsigned pos
, int value
)
3972 map
= isl_map_cow(map
);
3976 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3977 for (i
= 0; i
< map
->n
; ++i
) {
3978 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
3983 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3990 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
3991 enum isl_dim_type type
, unsigned pos
, int value
)
3993 return (struct isl_set
*)
3994 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
3997 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4002 set
= isl_set_cow(set
);
4006 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4007 for (i
= 0; i
< set
->n
; ++i
) {
4008 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4018 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4022 map
= isl_map_cow(map
);
4026 map
->dim
= isl_dim_reverse(map
->dim
);
4029 for (i
= 0; i
< map
->n
; ++i
) {
4030 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4034 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4041 static struct isl_map
*isl_basic_map_partial_lexopt(
4042 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4043 struct isl_set
**empty
, int max
)
4047 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4048 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4050 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4052 isl_basic_map_free(bmap
);
4053 isl_basic_set_free(dom
);
4059 struct isl_map
*isl_basic_map_partial_lexmax(
4060 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4061 struct isl_set
**empty
)
4063 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4066 struct isl_map
*isl_basic_map_partial_lexmin(
4067 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4068 struct isl_set
**empty
)
4070 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4073 struct isl_set
*isl_basic_set_partial_lexmin(
4074 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4075 struct isl_set
**empty
)
4077 return (struct isl_set
*)
4078 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4082 struct isl_set
*isl_basic_set_partial_lexmax(
4083 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4084 struct isl_set
**empty
)
4086 return (struct isl_set
*)
4087 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4091 /* Given a basic map "bmap", compute the lexicograhically minimal
4092 * (or maximal) image element for each domain element in dom.
4093 * Set *empty to those elements in dom that do not have an image element.
4095 * We first make sure the basic sets in dom are disjoint and then
4096 * simply collect the results over each of the basic sets separately.
4097 * We could probably improve the efficiency a bit by moving the union
4098 * domain down into the parametric integer programming.
4100 static __isl_give isl_map
*basic_map_partial_lexopt(
4101 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4102 __isl_give isl_set
**empty
, int max
)
4105 struct isl_map
*res
;
4107 dom
= isl_set_make_disjoint(dom
);
4111 if (isl_set_fast_is_empty(dom
)) {
4112 res
= isl_map_empty_like_basic_map(bmap
);
4113 *empty
= isl_set_empty_like(dom
);
4115 isl_basic_map_free(bmap
);
4119 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4120 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4122 for (i
= 1; i
< dom
->n
; ++i
) {
4123 struct isl_map
*res_i
;
4124 struct isl_set
*empty_i
;
4126 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4127 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4129 res
= isl_map_union_disjoint(res
, res_i
);
4130 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4134 isl_basic_map_free(bmap
);
4139 isl_basic_map_free(bmap
);
4143 /* Given a map "map", compute the lexicograhically minimal
4144 * (or maximal) image element for each domain element in dom.
4145 * Set *empty to those elements in dom that do not have an image element.
4147 * We first compute the lexicographically minimal or maximal element
4148 * in the first basic map. This results in a partial solution "res"
4149 * and a subset "todo" of dom that still need to be handled.
4150 * We then consider each of the remaining maps in "map" and successively
4151 * improve both "res" and "todo".
4153 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4154 * Assume we are computing the lexicographical maximum.
4155 * We first intersect basic map i with a relation that maps elements
4156 * to elements that are lexicographically larger than the image elements
4157 * in res^k and the compute the maximum image element of this intersection.
4158 * The result ("better") corresponds to those image elements in basic map i
4159 * that are better than what we had before. The remainder ("keep") are the
4160 * domain elements for which the image element in res_k was better.
4161 * We also compute the lexicographical maximum of basic map i in todo^k.
4162 * res^i is the result of the operation + better + those elements in
4163 * res^k that we should keep
4164 * todo^i is the remainder of the maximum operation on todo^k.
4166 static __isl_give isl_map
*isl_map_partial_lexopt(
4167 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4168 __isl_give isl_set
**empty
, int max
)
4171 struct isl_map
*res
;
4172 struct isl_set
*todo
;
4177 if (isl_map_fast_is_empty(map
)) {
4185 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4186 isl_set_copy(dom
), &todo
, max
);
4188 for (i
= 1; i
< map
->n
; ++i
) {
4190 struct isl_map
*better
;
4191 struct isl_set
*keep
;
4192 struct isl_map
*res_i
;
4193 struct isl_set
*todo_i
;
4194 struct isl_dim
*dim
= isl_map_get_dim(res
);
4196 dim
= isl_dim_range(dim
);
4198 lt
= isl_map_lex_lt(dim
);
4200 lt
= isl_map_lex_gt(dim
);
4201 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4202 lt
= isl_map_intersect(lt
,
4203 isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
])));
4204 better
= isl_map_partial_lexopt(lt
,
4205 isl_map_domain(isl_map_copy(res
)),
4208 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4209 todo
, &todo_i
, max
);
4211 res
= isl_map_intersect_domain(res
, keep
);
4212 res
= isl_map_union_disjoint(res
, res_i
);
4213 res
= isl_map_union_disjoint(res
, better
);
4234 __isl_give isl_map
*isl_map_partial_lexmax(
4235 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4236 __isl_give isl_set
**empty
)
4238 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4241 __isl_give isl_map
*isl_map_partial_lexmin(
4242 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4243 __isl_give isl_set
**empty
)
4245 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4248 __isl_give isl_set
*isl_set_partial_lexmin(
4249 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4250 __isl_give isl_set
**empty
)
4252 return (struct isl_set
*)
4253 isl_map_partial_lexmin((struct isl_map
*)set
,
4257 __isl_give isl_set
*isl_set_partial_lexmax(
4258 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4259 __isl_give isl_set
**empty
)
4261 return (struct isl_set
*)
4262 isl_map_partial_lexmax((struct isl_map
*)set
,
4266 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4268 struct isl_basic_set
*dom
= NULL
;
4269 struct isl_dim
*dom_dim
;
4273 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4274 dom
= isl_basic_set_universe(dom_dim
);
4275 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4277 isl_basic_map_free(bmap
);
4281 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4283 return isl_basic_map_lexopt(bmap
, 0);
4286 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4288 return isl_basic_map_lexopt(bmap
, 1);
4291 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4293 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4296 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4298 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4301 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4303 struct isl_set
*dom
= NULL
;
4304 struct isl_dim
*dom_dim
;
4308 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4309 dom
= isl_set_universe(dom_dim
);
4310 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4316 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4318 return isl_map_lexopt(map
, 0);
4321 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4323 return isl_map_lexopt(map
, 1);
4326 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4328 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4331 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4333 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4336 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
4337 struct isl_dim
*dim
)
4344 for (i
= 0; i
< map
->n
; ++i
) {
4345 isl_dim_free(map
->p
[i
]->dim
);
4346 map
->p
[i
]->dim
= isl_dim_copy(dim
);
4348 isl_dim_free(map
->dim
);
4358 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
4359 struct isl_dim
*dim
)
4361 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
4364 /* Apply a preimage specified by "mat" on the parameters of "bset".
4365 * bset is assumed to have only parameters and divs.
4367 static struct isl_basic_set
*basic_set_parameter_preimage(
4368 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4375 bset
->dim
= isl_dim_cow(bset
->dim
);
4379 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4381 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4383 bset
->dim
->nparam
= 0;
4384 bset
->dim
->n_out
= nparam
;
4385 bset
= isl_basic_set_preimage(bset
, mat
);
4387 bset
->dim
->nparam
= bset
->dim
->n_out
;
4388 bset
->dim
->n_out
= 0;
4393 isl_basic_set_free(bset
);
4397 /* Apply a preimage specified by "mat" on the parameters of "set".
4398 * set is assumed to have only parameters and divs.
4400 static struct isl_set
*set_parameter_preimage(
4401 struct isl_set
*set
, struct isl_mat
*mat
)
4403 struct isl_dim
*dim
= NULL
;
4409 dim
= isl_dim_copy(set
->dim
);
4410 dim
= isl_dim_cow(dim
);
4414 nparam
= isl_set_dim(set
, isl_dim_param
);
4416 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4419 dim
->n_out
= nparam
;
4420 isl_set_reset_dim(set
, dim
);
4421 set
= isl_set_preimage(set
, mat
);
4424 dim
= isl_dim_copy(set
->dim
);
4425 dim
= isl_dim_cow(dim
);
4428 dim
->nparam
= dim
->n_out
;
4430 isl_set_reset_dim(set
, dim
);
4440 /* Intersect the basic set "bset" with the affine space specified by the
4441 * equalities in "eq".
4443 static struct isl_basic_set
*basic_set_append_equalities(
4444 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4452 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4457 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4458 for (i
= 0; i
< eq
->n_row
; ++i
) {
4459 k
= isl_basic_set_alloc_equality(bset
);
4462 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4463 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
4467 bset
= isl_basic_set_gauss(bset
, NULL
);
4468 bset
= isl_basic_set_finalize(bset
);
4473 isl_basic_set_free(bset
);
4477 /* Intersect the set "set" with the affine space specified by the
4478 * equalities in "eq".
4480 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
4488 for (i
= 0; i
< set
->n
; ++i
) {
4489 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
4502 /* Project the given basic set onto its parameter domain, possibly introducing
4503 * new, explicit, existential variables in the constraints.
4504 * The input has parameters and (possibly implicit) existential variables.
4505 * The output has the same parameters, but only
4506 * explicit existentially quantified variables.
4508 * The actual projection is performed by pip, but pip doesn't seem
4509 * to like equalities very much, so we first remove the equalities
4510 * among the parameters by performing a variable compression on
4511 * the parameters. Afterward, an inverse transformation is performed
4512 * and the equalities among the parameters are inserted back in.
4514 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
4518 struct isl_mat
*T
, *T2
;
4519 struct isl_set
*set
;
4520 unsigned nparam
, n_div
;
4522 bset
= isl_basic_set_cow(bset
);
4526 if (bset
->n_eq
== 0)
4527 return isl_basic_set_lexmin(bset
);
4529 isl_basic_set_gauss(bset
, NULL
);
4531 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4532 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
4534 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
4535 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
4538 if (i
== bset
->n_eq
)
4539 return isl_basic_set_lexmin(bset
);
4541 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
4543 eq
= isl_mat_cow(eq
);
4544 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
4545 if (T
&& T
->n_col
== 0) {
4549 bset
= isl_basic_set_set_to_empty(bset
);
4550 return isl_set_from_basic_set(bset
);
4552 bset
= basic_set_parameter_preimage(bset
, T
);
4554 set
= isl_basic_set_lexmin(bset
);
4555 set
= set_parameter_preimage(set
, T2
);
4556 set
= set_append_equalities(set
, eq
);
4560 /* Compute an explicit representation for all the existentially
4561 * quantified variables.
4562 * The input and output dimensions are first turned into parameters.
4563 * compute_divs then returns a map with the same parameters and
4564 * no input or output dimensions and the dimension specification
4565 * is reset to that of the input.
4567 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
4569 struct isl_basic_set
*bset
;
4570 struct isl_set
*set
;
4571 struct isl_map
*map
;
4572 struct isl_dim
*dim
, *orig_dim
= NULL
;
4577 bmap
= isl_basic_map_cow(bmap
);
4581 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4582 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4583 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4584 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
4588 orig_dim
= bmap
->dim
;
4590 bset
= (struct isl_basic_set
*)bmap
;
4592 set
= parameter_compute_divs(bset
);
4593 map
= (struct isl_map
*)set
;
4594 map
= isl_map_reset_dim(map
, orig_dim
);
4598 isl_basic_map_free(bmap
);
4602 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
4610 off
= isl_dim_total(bmap
->dim
);
4611 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4612 if (isl_int_is_zero(bmap
->div
[i
][0]))
4614 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
4620 static int map_divs_known(__isl_keep isl_map
*map
)
4627 for (i
= 0; i
< map
->n
; ++i
) {
4628 int known
= basic_map_divs_known(map
->p
[i
]);
4636 /* If bmap contains any unknown divs, then compute explicit
4637 * expressions for them. However, this computation may be
4638 * quite expensive, so first try to remove divs that aren't
4641 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
4645 struct isl_map
*map
;
4647 known
= basic_map_divs_known(bmap
);
4651 return isl_map_from_basic_map(bmap
);
4653 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4655 known
= basic_map_divs_known(bmap
);
4659 return isl_map_from_basic_map(bmap
);
4661 map
= compute_divs(bmap
);
4664 isl_basic_map_free(bmap
);
4668 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
4672 struct isl_map
*res
;
4679 known
= map_divs_known(map
);
4687 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
4688 for (i
= 1 ; i
< map
->n
; ++i
) {
4690 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
4691 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4692 res
= isl_map_union_disjoint(res
, r2
);
4694 res
= isl_map_union(res
, r2
);
4701 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
4703 return (struct isl_set
*)
4704 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
4707 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
4709 return (struct isl_set
*)
4710 isl_map_compute_divs((struct isl_map
*)set
);
4713 struct isl_set
*isl_map_domain(struct isl_map
*map
)
4716 struct isl_set
*set
;
4721 map
= isl_map_cow(map
);
4725 set
= (struct isl_set
*)map
;
4726 set
->dim
= isl_dim_domain(set
->dim
);
4729 for (i
= 0; i
< map
->n
; ++i
) {
4730 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
4734 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4735 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4742 struct isl_map
*isl_map_union_disjoint(
4743 struct isl_map
*map1
, struct isl_map
*map2
)
4747 struct isl_map
*map
= NULL
;
4761 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4763 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4764 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4765 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4767 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
4768 map1
->n
+ map2
->n
, flags
);
4771 for (i
= 0; i
< map1
->n
; ++i
) {
4772 map
= isl_map_add_basic_map(map
,
4773 isl_basic_map_copy(map1
->p
[i
]));
4777 for (i
= 0; i
< map2
->n
; ++i
) {
4778 map
= isl_map_add_basic_map(map
,
4779 isl_basic_map_copy(map2
->p
[i
]));
4793 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
4795 map1
= isl_map_union_disjoint(map1
, map2
);
4799 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
4803 struct isl_set
*isl_set_union_disjoint(
4804 struct isl_set
*set1
, struct isl_set
*set2
)
4806 return (struct isl_set
*)
4807 isl_map_union_disjoint(
4808 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4811 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
4813 return (struct isl_set
*)
4814 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4817 struct isl_map
*isl_map_intersect_range(
4818 struct isl_map
*map
, struct isl_set
*set
)
4821 struct isl_map
*result
;
4827 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
4828 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
4829 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4831 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
4832 map
->n
* set
->n
, flags
);
4835 for (i
= 0; i
< map
->n
; ++i
)
4836 for (j
= 0; j
< set
->n
; ++j
) {
4837 result
= isl_map_add_basic_map(result
,
4838 isl_basic_map_intersect_range(
4839 isl_basic_map_copy(map
->p
[i
]),
4840 isl_basic_set_copy(set
->p
[j
])));
4853 struct isl_map
*isl_map_intersect_domain(
4854 struct isl_map
*map
, struct isl_set
*set
)
4856 return isl_map_reverse(
4857 isl_map_intersect_range(isl_map_reverse(map
), set
));
4860 struct isl_map
*isl_map_apply_domain(
4861 struct isl_map
*map1
, struct isl_map
*map2
)
4865 map1
= isl_map_reverse(map1
);
4866 map1
= isl_map_apply_range(map1
, map2
);
4867 return isl_map_reverse(map1
);
4874 struct isl_map
*isl_map_apply_range(
4875 struct isl_map
*map1
, struct isl_map
*map2
)
4877 struct isl_dim
*dim_result
;
4878 struct isl_map
*result
;
4884 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
4885 isl_dim_copy(map2
->dim
));
4887 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
4890 for (i
= 0; i
< map1
->n
; ++i
)
4891 for (j
= 0; j
< map2
->n
; ++j
) {
4892 result
= isl_map_add_basic_map(result
,
4893 isl_basic_map_apply_range(
4894 isl_basic_map_copy(map1
->p
[i
]),
4895 isl_basic_map_copy(map2
->p
[j
])));
4901 if (result
&& result
->n
<= 1)
4902 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
4911 * returns range - domain
4913 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
4916 struct isl_basic_set
*bset
;
4923 dim
= isl_basic_map_n_in(bmap
);
4924 nparam
= isl_basic_map_n_param(bmap
);
4925 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
4926 bset
= isl_basic_set_from_basic_map(bmap
);
4927 bset
= isl_basic_set_cow(bset
);
4928 dims
= isl_basic_set_get_dim(bset
);
4929 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
4930 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
4931 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
4932 for (i
= 0; i
< dim
; ++i
) {
4933 int j
= isl_basic_map_alloc_equality(
4934 (struct isl_basic_map
*)bset
);
4937 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
4938 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
4939 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
4940 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
4942 return isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
4944 isl_basic_map_free(bmap
);
4949 * returns range - domain
4951 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
4955 struct isl_set
*result
;
4960 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
4961 dim
= isl_map_get_dim(map
);
4962 dim
= isl_dim_domain(dim
);
4963 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
4966 for (i
= 0; i
< map
->n
; ++i
)
4967 result
= isl_set_add_basic_set(result
,
4968 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
4976 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
4978 struct isl_basic_map
*bmap
;
4986 nparam
= dims
->nparam
;
4988 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
4992 for (i
= 0; i
< dim
; ++i
) {
4993 int j
= isl_basic_map_alloc_equality(bmap
);
4996 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4997 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
4998 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5000 return isl_basic_map_finalize(bmap
);
5002 isl_basic_map_free(bmap
);
5006 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5008 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5011 return basic_map_identity(dim
);
5014 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5016 if (!model
|| !model
->dim
)
5018 isl_assert(model
->ctx
,
5019 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5020 return basic_map_identity(isl_dim_copy(model
->dim
));
5023 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5025 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5026 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5029 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5031 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5034 return map_identity(dim
);
5037 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5039 if (!model
|| !model
->dim
)
5041 isl_assert(model
->ctx
,
5042 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5043 return map_identity(isl_dim_copy(model
->dim
));
5046 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5048 if (!model
|| !model
->dim
)
5050 isl_assert(model
->ctx
,
5051 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5052 return map_identity(isl_dim_copy(model
->dim
));
5055 /* Construct a basic set with all set dimensions having only non-negative
5058 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5063 struct isl_basic_set
*bset
;
5067 nparam
= dims
->nparam
;
5069 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5072 for (i
= 0; i
< dim
; ++i
) {
5073 int k
= isl_basic_set_alloc_inequality(bset
);
5076 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5077 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5081 isl_basic_set_free(bset
);
5085 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5086 enum isl_dim_type type
, unsigned first
, unsigned n
)
5089 isl_basic_set
*nonneg
= NULL
;
5090 isl_basic_set
*neg
= NULL
;
5097 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5099 for (i
= 0; i
< n
; ++i
) {
5103 nonneg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5104 k
= isl_basic_set_alloc_inequality(nonneg
);
5107 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5108 isl_int_set_si(nonneg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], 1);
5110 neg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5111 k
= isl_basic_set_alloc_inequality(neg
);
5114 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5115 isl_int_set_si(neg
->ineq
[k
][0], -1);
5116 isl_int_set_si(neg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], -1);
5118 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5123 isl_basic_set_free(nonneg
);
5124 isl_basic_set_free(neg
);
5129 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5131 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5134 int isl_basic_map_is_subset(
5135 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5138 struct isl_map
*map1
;
5139 struct isl_map
*map2
;
5141 if (!bmap1
|| !bmap2
)
5144 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5145 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5147 is_subset
= isl_map_is_subset(map1
, map2
);
5155 int isl_basic_map_is_equal(
5156 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5160 if (!bmap1
|| !bmap2
)
5162 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5165 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5169 int isl_basic_set_is_equal(
5170 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5172 return isl_basic_map_is_equal(
5173 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5176 int isl_map_is_empty(struct isl_map
*map
)
5183 for (i
= 0; i
< map
->n
; ++i
) {
5184 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5193 int isl_map_fast_is_empty(struct isl_map
*map
)
5195 return map
? map
->n
== 0 : -1;
5198 int isl_set_fast_is_empty(struct isl_set
*set
)
5200 return set
? set
->n
== 0 : -1;
5203 int isl_set_is_empty(struct isl_set
*set
)
5205 return isl_map_is_empty((struct isl_map
*)set
);
5208 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5214 is_subset
= isl_map_is_subset(map1
, map2
);
5217 is_subset
= isl_map_is_subset(map2
, map1
);
5221 int isl_basic_map_is_strict_subset(
5222 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5226 if (!bmap1
|| !bmap2
)
5228 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5231 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5232 if (is_subset
== -1)
5237 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5243 is_subset
= isl_map_is_subset(map1
, map2
);
5246 is_subset
= isl_map_is_subset(map2
, map1
);
5247 if (is_subset
== -1)
5252 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5254 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5257 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5261 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5264 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5268 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5271 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5276 return map
->n
== 1 && isl_basic_map_is_universe(map
->p
[0]);
5279 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5281 return isl_map_fast_is_universe((isl_map
*) set
);
5284 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
5286 struct isl_basic_set
*bset
= NULL
;
5287 struct isl_vec
*sample
= NULL
;
5294 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
5297 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
5298 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
5299 copy
= isl_basic_map_convex_hull(copy
);
5300 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
5301 isl_basic_map_free(copy
);
5305 total
= 1 + isl_basic_map_total_dim(bmap
);
5306 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
5307 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
5313 isl_vec_free(bmap
->sample
);
5314 bmap
->sample
= NULL
;
5315 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
5318 sample
= isl_basic_set_sample_vec(bset
);
5321 empty
= sample
->size
== 0;
5322 isl_vec_free(bmap
->sample
);
5323 bmap
->sample
= sample
;
5325 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
5330 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
5334 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
5337 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
5341 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
5344 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
5346 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
5349 struct isl_map
*isl_basic_map_union(
5350 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5352 struct isl_map
*map
;
5353 if (!bmap1
|| !bmap2
)
5356 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
5358 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
5361 map
= isl_map_add_basic_map(map
, bmap1
);
5362 map
= isl_map_add_basic_map(map
, bmap2
);
5365 isl_basic_map_free(bmap1
);
5366 isl_basic_map_free(bmap2
);
5370 struct isl_set
*isl_basic_set_union(
5371 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5373 return (struct isl_set
*)isl_basic_map_union(
5374 (struct isl_basic_map
*)bset1
,
5375 (struct isl_basic_map
*)bset2
);
5378 /* Order divs such that any div only depends on previous divs */
5379 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
5387 off
= isl_dim_total(bmap
->dim
);
5389 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5391 if (isl_int_is_zero(bmap
->div
[i
][0]))
5393 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
5397 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
5403 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
5405 return (struct isl_basic_set
*)
5406 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
5409 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
5416 for (i
= 0; i
< map
->n
; ++i
) {
5417 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
5428 /* Look for a div in dst that corresponds to the div "div" in src.
5429 * The divs before "div" in src and dst are assumed to be the same.
5431 * Returns -1 if no corresponding div was found and the position
5432 * of the corresponding div in dst otherwise.
5434 static int find_div(struct isl_basic_map
*dst
,
5435 struct isl_basic_map
*src
, unsigned div
)
5439 unsigned total
= isl_dim_total(src
->dim
);
5441 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
5442 for (i
= div
; i
< dst
->n_div
; ++i
)
5443 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
5444 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
5445 dst
->n_div
- div
) == -1)
5450 struct isl_basic_map
*isl_basic_map_align_divs(
5451 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
5454 unsigned total
= isl_dim_total(src
->dim
);
5459 if (src
->n_div
== 0)
5462 for (i
= 0; i
< src
->n_div
; ++i
)
5463 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
5465 src
= isl_basic_map_order_divs(src
);
5466 dst
= isl_basic_map_cow(dst
);
5467 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
5468 src
->n_div
, 0, 2 * src
->n_div
);
5471 for (i
= 0; i
< src
->n_div
; ++i
) {
5472 int j
= find_div(dst
, src
, i
);
5474 j
= isl_basic_map_alloc_div(dst
);
5477 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
5478 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
5479 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
5483 isl_basic_map_swap_div(dst
, i
, j
);
5487 isl_basic_map_free(dst
);
5491 struct isl_basic_set
*isl_basic_set_align_divs(
5492 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
5494 return (struct isl_basic_set
*)isl_basic_map_align_divs(
5495 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
5498 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
5506 map
= isl_map_compute_divs(map
);
5507 map
= isl_map_cow(map
);
5511 for (i
= 1; i
< map
->n
; ++i
)
5512 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
5513 for (i
= 1; i
< map
->n
; ++i
)
5514 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
5516 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5520 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
5522 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
5525 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
5529 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
5530 map
= isl_map_intersect_domain(map
, set
);
5531 set
= isl_map_range(map
);
5539 /* There is no need to cow as removing empty parts doesn't change
5540 * the meaning of the set.
5542 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
5549 for (i
= map
->n
-1; i
>= 0; --i
) {
5550 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
5552 isl_basic_map_free(map
->p
[i
]);
5553 if (i
!= map
->n
-1) {
5554 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5555 map
->p
[i
] = map
->p
[map
->n
-1];
5563 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
5565 return (struct isl_set
*)
5566 isl_map_remove_empty_parts((struct isl_map
*)set
);
5569 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
5571 struct isl_basic_map
*bmap
;
5572 if (!map
|| map
->n
== 0)
5574 bmap
= map
->p
[map
->n
-1];
5575 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
5576 return isl_basic_map_copy(bmap
);
5579 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
5581 return (struct isl_basic_set
*)
5582 isl_map_copy_basic_map((struct isl_map
*)set
);
5585 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
5586 __isl_keep isl_basic_map
*bmap
)
5592 for (i
= map
->n
-1; i
>= 0; --i
) {
5593 if (map
->p
[i
] != bmap
)
5595 map
= isl_map_cow(map
);
5598 isl_basic_map_free(map
->p
[i
]);
5599 if (i
!= map
->n
-1) {
5600 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
5601 map
->p
[i
] = map
->p
[map
->n
-1];
5612 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
5613 struct isl_basic_set
*bset
)
5615 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
5616 (struct isl_basic_map
*)bset
);
5619 /* Given two basic sets bset1 and bset2, compute the maximal difference
5620 * between the values of dimension pos in bset1 and those in bset2
5621 * for any common value of the parameters and dimensions preceding pos.
5623 static enum isl_lp_result
basic_set_maximal_difference_at(
5624 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
5625 int pos
, isl_int
*opt
)
5627 struct isl_dim
*dims
;
5628 struct isl_basic_map
*bmap1
= NULL
;
5629 struct isl_basic_map
*bmap2
= NULL
;
5630 struct isl_ctx
*ctx
;
5631 struct isl_vec
*obj
;
5634 unsigned dim1
, dim2
;
5635 enum isl_lp_result res
;
5637 if (!bset1
|| !bset2
)
5638 return isl_lp_error
;
5640 nparam
= isl_basic_set_n_param(bset1
);
5641 dim1
= isl_basic_set_n_dim(bset1
);
5642 dim2
= isl_basic_set_n_dim(bset2
);
5643 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
5644 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
5645 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
5646 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
5647 if (!bmap1
|| !bmap2
)
5649 bmap1
= isl_basic_map_cow(bmap1
);
5650 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
5651 pos
, (dim1
- pos
) + (dim2
- pos
),
5652 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
5653 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
5656 total
= isl_basic_map_total_dim(bmap1
);
5658 obj
= isl_vec_alloc(ctx
, 1 + total
);
5659 isl_seq_clr(obj
->block
.data
, 1 + total
);
5660 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
5661 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
5664 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
5666 isl_basic_map_free(bmap1
);
5670 isl_basic_map_free(bmap1
);
5671 isl_basic_map_free(bmap2
);
5672 return isl_lp_error
;
5675 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5676 * for any common value of the parameters and dimensions preceding pos
5677 * in both basic sets, the values of dimension pos in bset1 are
5678 * smaller or larger than those in bset2.
5681 * 1 if bset1 follows bset2
5682 * -1 if bset1 precedes bset2
5683 * 0 if bset1 and bset2 are incomparable
5684 * -2 if some error occurred.
5686 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
5687 struct isl_basic_set
*bset2
, int pos
)
5690 enum isl_lp_result res
;
5695 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5697 if (res
== isl_lp_empty
)
5699 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5700 res
== isl_lp_unbounded
)
5702 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5711 /* Given two basic sets bset1 and bset2, check whether
5712 * for any common value of the parameters and dimensions preceding pos
5713 * there is a value of dimension pos in bset1 that is larger
5714 * than a value of the same dimension in bset2.
5717 * 1 if there exists such a pair
5718 * 0 if there is no such pair, but there is a pair of equal values
5720 * -2 if some error occurred.
5722 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
5723 __isl_keep isl_basic_set
*bset2
, int pos
)
5726 enum isl_lp_result res
;
5731 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5733 if (res
== isl_lp_empty
)
5735 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5736 res
== isl_lp_unbounded
)
5738 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5740 else if (res
== isl_lp_ok
)
5749 /* Given two sets set1 and set2, check whether
5750 * for any common value of the parameters and dimensions preceding pos
5751 * there is a value of dimension pos in set1 that is larger
5752 * than a value of the same dimension in set2.
5755 * 1 if there exists such a pair
5756 * 0 if there is no such pair, but there is a pair of equal values
5758 * -2 if some error occurred.
5760 int isl_set_follows_at(__isl_keep isl_set
*set1
,
5761 __isl_keep isl_set
*set2
, int pos
)
5769 for (i
= 0; i
< set1
->n
; ++i
)
5770 for (j
= 0; j
< set2
->n
; ++j
) {
5772 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
5773 if (f
== 1 || f
== -2)
5782 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5783 unsigned pos
, isl_int
*val
)
5791 total
= isl_basic_map_total_dim(bmap
);
5792 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5793 for (; d
+1 > pos
; --d
)
5794 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5798 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5800 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5802 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5805 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5811 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5812 unsigned pos
, isl_int
*val
)
5824 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5827 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5828 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5829 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5830 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5834 isl_int_set(*val
, v
);
5840 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5841 unsigned pos
, isl_int
*val
)
5843 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
5847 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
5850 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
5853 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
5854 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5856 if (pos
>= isl_basic_map_dim(bmap
, type
))
5858 return isl_basic_map_fast_has_fixed_var(bmap
,
5859 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
5862 int isl_map_fast_is_fixed(struct isl_map
*map
,
5863 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5865 if (pos
>= isl_map_dim(map
, type
))
5867 return isl_map_fast_has_fixed_var(map
,
5868 map_offset(map
, type
) - 1 + pos
, val
);
5871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5872 * then return this fixed value in *val.
5874 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
5877 return isl_basic_set_fast_has_fixed_var(bset
,
5878 isl_basic_set_n_param(bset
) + dim
, val
);
5881 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5882 * then return this fixed value in *val.
5884 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
5886 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
5889 /* Check if input variable in has fixed value and if so and if val is not NULL,
5890 * then return this fixed value in *val.
5892 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
5894 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
5897 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5898 * and if val is not NULL, then return this lower bound in *val.
5900 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
5901 unsigned dim
, isl_int
*val
)
5903 int i
, i_eq
= -1, i_ineq
= -1;
5910 total
= isl_basic_set_total_dim(bset
);
5911 nparam
= isl_basic_set_n_param(bset
);
5912 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5913 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
5919 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5920 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
5922 if (i_eq
!= -1 || i_ineq
!= -1)
5926 if (i_eq
== -1 && i_ineq
== -1)
5928 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
5929 /* The coefficient should always be one due to normalization. */
5930 if (!isl_int_is_one(c
[1+nparam
+dim
]))
5932 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
5934 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
5935 total
- nparam
- dim
- 1) != -1)
5938 isl_int_neg(*val
, c
[0]);
5942 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
5943 unsigned dim
, isl_int
*val
)
5955 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5959 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5961 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
5962 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
5964 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5968 isl_int_set(*val
, v
);
5979 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
5981 const struct constraint
*c1
= (const struct constraint
*)p1
;
5982 const struct constraint
*c2
= (const struct constraint
*)p2
;
5983 unsigned size
= isl_min(c1
->size
, c2
->size
);
5984 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
5987 static struct isl_basic_map
*isl_basic_map_sort_constraints(
5988 struct isl_basic_map
*bmap
)
5991 struct constraint
*c
;
5996 total
= isl_basic_map_total_dim(bmap
);
5997 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6000 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6002 c
[i
].c
= bmap
->ineq
[i
];
6004 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6005 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6006 bmap
->ineq
[i
] = c
[i
].c
;
6010 isl_basic_map_free(bmap
);
6014 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6015 __isl_take isl_basic_set
*bset
)
6017 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6018 (struct isl_basic_map
*)bset
);
6021 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6025 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6027 bmap
= isl_basic_map_convex_hull(bmap
);
6028 bmap
= isl_basic_map_sort_constraints(bmap
);
6029 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6033 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6035 return (struct isl_basic_set
*)isl_basic_map_normalize(
6036 (struct isl_basic_map
*)bset
);
6039 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
6040 const struct isl_basic_map
*bmap2
)
6047 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6048 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6049 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6050 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6051 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6052 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6053 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6054 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6056 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6058 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6060 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6061 return bmap1
->n_eq
- bmap2
->n_eq
;
6062 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6063 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6064 if (bmap1
->n_div
!= bmap2
->n_div
)
6065 return bmap1
->n_div
- bmap2
->n_div
;
6066 total
= isl_basic_map_total_dim(bmap1
);
6067 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6068 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6072 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6073 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6077 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6078 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6085 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
6086 struct isl_basic_map
*bmap2
)
6088 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6091 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6092 __isl_keep isl_basic_set
*bset2
)
6094 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6095 (isl_basic_map
*)bset2
);
6098 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6100 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6101 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6103 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6106 /* We normalize in place, but if anything goes wrong we need
6107 * to return NULL, so we need to make sure we don't change the
6108 * meaning of any possible other copies of map.
6110 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6113 struct isl_basic_map
*bmap
;
6117 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6119 for (i
= 0; i
< map
->n
; ++i
) {
6120 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6123 isl_basic_map_free(map
->p
[i
]);
6126 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6127 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6128 map
= isl_map_remove_empty_parts(map
);
6131 for (i
= map
->n
- 1; i
>= 1; --i
) {
6132 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6134 isl_basic_map_free(map
->p
[i
-1]);
6135 for (j
= i
; j
< map
->n
; ++j
)
6136 map
->p
[j
-1] = map
->p
[j
];
6146 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6148 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6151 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6161 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6164 map1
= isl_map_copy(map1
);
6165 map2
= isl_map_copy(map2
);
6166 map1
= isl_map_normalize(map1
);
6167 map2
= isl_map_normalize(map2
);
6170 equal
= map1
->n
== map2
->n
;
6171 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6172 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6185 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6187 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6188 (struct isl_map
*)set2
);
6191 /* Return an interval that ranges from min to max (inclusive)
6193 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6194 isl_int min
, isl_int max
)
6197 struct isl_basic_set
*bset
= NULL
;
6199 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6203 k
= isl_basic_set_alloc_inequality(bset
);
6206 isl_int_set_si(bset
->ineq
[k
][1], 1);
6207 isl_int_neg(bset
->ineq
[k
][0], min
);
6209 k
= isl_basic_set_alloc_inequality(bset
);
6212 isl_int_set_si(bset
->ineq
[k
][1], -1);
6213 isl_int_set(bset
->ineq
[k
][0], max
);
6217 isl_basic_set_free(bset
);
6221 /* Return the Cartesian product of the basic sets in list (in the given order).
6223 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6231 struct isl_basic_set
*product
= NULL
;
6235 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6236 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6237 nparam
= isl_basic_set_n_param(list
->p
[0]);
6238 dim
= isl_basic_set_n_dim(list
->p
[0]);
6239 extra
= list
->p
[0]->n_div
;
6240 n_eq
= list
->p
[0]->n_eq
;
6241 n_ineq
= list
->p
[0]->n_ineq
;
6242 for (i
= 1; i
< list
->n
; ++i
) {
6243 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6244 isl_assert(list
->ctx
,
6245 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6246 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6247 extra
+= list
->p
[i
]->n_div
;
6248 n_eq
+= list
->p
[i
]->n_eq
;
6249 n_ineq
+= list
->p
[i
]->n_ineq
;
6251 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6256 for (i
= 0; i
< list
->n
; ++i
) {
6257 isl_basic_set_add_constraints(product
,
6258 isl_basic_set_copy(list
->p
[i
]), dim
);
6259 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6261 isl_basic_set_list_free(list
);
6264 isl_basic_set_free(product
);
6265 isl_basic_set_list_free(list
);
6269 struct isl_basic_map
*isl_basic_map_product(
6270 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6272 struct isl_dim
*dim_result
= NULL
;
6273 struct isl_basic_map
*bmap
;
6274 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6275 struct isl_dim_map
*dim_map1
, *dim_map2
;
6277 if (!bmap1
|| !bmap2
)
6280 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6281 bmap2
->dim
, isl_dim_param
), goto error
);
6282 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6283 isl_dim_copy(bmap2
->dim
));
6285 in1
= isl_basic_map_n_in(bmap1
);
6286 in2
= isl_basic_map_n_in(bmap2
);
6287 out1
= isl_basic_map_n_out(bmap1
);
6288 out2
= isl_basic_map_n_out(bmap2
);
6289 nparam
= isl_basic_map_n_param(bmap1
);
6291 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
6292 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6293 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6294 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
6295 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
6296 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
6297 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
6298 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
6299 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
6300 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
6301 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
6303 bmap
= isl_basic_map_alloc_dim(dim_result
,
6304 bmap1
->n_div
+ bmap2
->n_div
,
6305 bmap1
->n_eq
+ bmap2
->n_eq
,
6306 bmap1
->n_ineq
+ bmap2
->n_ineq
);
6307 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
6308 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
6309 bmap
= isl_basic_map_simplify(bmap
);
6310 return isl_basic_map_finalize(bmap
);
6312 isl_basic_map_free(bmap1
);
6313 isl_basic_map_free(bmap2
);
6317 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6319 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
6322 struct isl_map
*result
;
6328 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
6329 map2
->dim
, isl_dim_param
), goto error
);
6331 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6332 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6333 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6335 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
6336 isl_dim_copy(map2
->dim
)),
6337 map1
->n
* map2
->n
, flags
);
6340 for (i
= 0; i
< map1
->n
; ++i
)
6341 for (j
= 0; j
< map2
->n
; ++j
) {
6342 struct isl_basic_map
*part
;
6343 part
= isl_basic_map_product(
6344 isl_basic_map_copy(map1
->p
[i
]),
6345 isl_basic_map_copy(map2
->p
[j
]));
6346 if (isl_basic_map_is_empty(part
))
6347 isl_basic_map_free(part
);
6349 result
= isl_map_add_basic_map(result
, part
);
6362 /* Given two set A and B, construct its Cartesian product A x B.
6364 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
6366 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
6367 (struct isl_map
*)set2
);
6370 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
6373 uint32_t hash
= isl_hash_init();
6378 bset
= isl_basic_set_copy(bset
);
6379 bset
= isl_basic_set_normalize(bset
);
6382 total
= isl_basic_set_total_dim(bset
);
6383 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
6384 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6386 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
6387 isl_hash_hash(hash
, c_hash
);
6389 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
6390 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6392 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
6393 isl_hash_hash(hash
, c_hash
);
6395 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
6396 for (i
= 0; i
< bset
->n_div
; ++i
) {
6398 if (isl_int_is_zero(bset
->div
[i
][0]))
6400 isl_hash_byte(hash
, i
& 0xFF);
6401 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
6402 isl_hash_hash(hash
, c_hash
);
6404 isl_basic_set_free(bset
);
6408 uint32_t isl_set_get_hash(struct isl_set
*set
)
6415 set
= isl_set_copy(set
);
6416 set
= isl_set_normalize(set
);
6420 hash
= isl_hash_init();
6421 for (i
= 0; i
< set
->n
; ++i
) {
6423 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
6424 isl_hash_hash(hash
, bset_hash
);
6432 /* Check if the value for dimension dim is completely determined
6433 * by the values of the other parameters and variables.
6434 * That is, check if dimension dim is involved in an equality.
6436 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
6443 nparam
= isl_basic_set_n_param(bset
);
6444 for (i
= 0; i
< bset
->n_eq
; ++i
)
6445 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
6450 /* Check if the value for dimension dim is completely determined
6451 * by the values of the other parameters and variables.
6452 * That is, check if dimension dim is involved in an equality
6453 * for each of the subsets.
6455 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
6461 for (i
= 0; i
< set
->n
; ++i
) {
6463 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
6470 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
6471 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
6478 for (i
= 0; i
< map
->n
; ++i
)
6479 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
6485 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
6486 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
6493 for (i
= 0; i
< set
->n
; ++i
)
6494 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
6500 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
6502 struct isl_dim
*dim
;
6507 if (bset
->n_div
== 0)
6510 bset
= isl_basic_set_cow(bset
);
6514 dim
= isl_basic_set_get_dim(bset
);
6515 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
6518 isl_dim_free(bset
->dim
);
6522 bset
= isl_basic_set_finalize(bset
);
6526 isl_basic_set_free(bset
);
6530 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
6533 struct isl_dim
*dim
;
6536 set
= isl_set_align_divs(set
);
6540 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
6543 set
= isl_set_cow(set
);
6547 n_div
= set
->p
[0]->n_div
;
6548 dim
= isl_set_get_dim(set
);
6549 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
6552 isl_dim_free(set
->dim
);
6555 for (i
= 0; i
< set
->n
; ++i
) {
6556 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
6567 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
6569 struct isl_dim
*dim
;
6570 struct isl_basic_map
*bmap
;
6577 set
= isl_set_align_divs(set
);
6582 dim
= isl_set_get_dim(set
);
6583 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
6585 return isl_map_identity(dim
);
6588 n_div
= set
->p
[0]->n_div
;
6589 dim
= isl_dim_map(dim
);
6590 n_param
= isl_dim_size(dim
, isl_dim_param
);
6591 n_set
= isl_dim_size(dim
, isl_dim_in
);
6592 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
6593 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
6594 for (i
= 0; i
< n_set
; ++i
)
6595 bmap
= var_equal(bmap
, i
);
6597 total
= n_param
+ n_set
+ n_set
+ n_div
;
6598 for (i
= 0; i
< n_div
; ++i
) {
6599 k
= isl_basic_map_alloc_inequality(bmap
);
6602 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
6603 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
6604 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
6605 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
6606 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
6607 set
->p
[0]->div
[i
][0]);
6609 l
= isl_basic_map_alloc_inequality(bmap
);
6612 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
6613 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
6614 set
->p
[0]->div
[i
][0]);
6615 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
6619 return isl_map_from_basic_map(bmap
);
6622 isl_basic_map_free(bmap
);
6626 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
6634 dim
= isl_basic_set_total_dim(bset
);
6635 size
+= bset
->n_eq
* (1 + dim
);
6636 size
+= bset
->n_ineq
* (1 + dim
);
6637 size
+= bset
->n_div
* (2 + dim
);
6642 int isl_set_size(__isl_keep isl_set
*set
)
6650 for (i
= 0; i
< set
->n
; ++i
)
6651 size
+= isl_basic_set_size(set
->p
[i
]);
6656 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
6657 enum isl_dim_type type
, unsigned pos
)
6665 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
6667 pos
+= isl_basic_map_offset(bmap
, type
);
6669 for (i
= 0; i
< bmap
->n_eq
; ++i
)
6670 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
6674 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6675 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
6682 return lower
&& upper
;
6685 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
6686 enum isl_dim_type type
, unsigned pos
)
6693 for (i
= 0; i
< map
->n
; ++i
) {
6695 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
6696 if (bounded
< 0 || !bounded
)
6703 /* Return 1 if the specified dim is involved in both an upper bound
6704 * and a lower bound.
6706 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
6707 enum isl_dim_type type
, unsigned pos
)
6709 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
6712 /* For each of the "n" variables starting at "first", determine
6713 * the sign of the variable and put the results in the first "n"
6714 * elements of the array "signs".
6716 * 1 means that the variable is non-negative
6717 * -1 means that the variable is non-positive
6718 * 0 means the variable attains both positive and negative values.
6720 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
6721 unsigned first
, unsigned n
, int *signs
)
6723 isl_vec
*bound
= NULL
;
6724 struct isl_tab
*tab
= NULL
;
6725 struct isl_tab_undo
*snap
;
6728 if (!bset
|| !signs
)
6731 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
6732 tab
= isl_tab_from_basic_set(bset
);
6736 isl_seq_clr(bound
->el
, bound
->size
);
6737 isl_int_set_si(bound
->el
[0], -1);
6739 snap
= isl_tab_snap(tab
);
6740 for (i
= 0; i
< n
; ++i
) {
6743 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
6744 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6747 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6748 if (isl_tab_rollback(tab
, snap
) < 0)
6756 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
6757 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6760 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6761 if (isl_tab_rollback(tab
, snap
) < 0)
6764 signs
[i
] = empty
? -1 : 0;
6768 isl_vec_free(bound
);
6772 isl_vec_free(bound
);
6776 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
6777 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
6779 if (!bset
|| !signs
)
6781 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
6784 first
+= pos(bset
->dim
, type
) - 1;
6785 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
6788 /* Check if the given map is single-valued.
6793 * and check if the result is a subset of the identity mapping.
6795 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
6801 test
= isl_map_reverse(isl_map_copy(map
));
6802 test
= isl_map_apply_range(test
, isl_map_copy(map
));
6804 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
6806 sv
= isl_map_is_subset(test
, id
);
6814 int isl_set_is_singleton(__isl_keep isl_set
*set
)
6816 return isl_map_is_single_valued((isl_map
*)set
);
6819 int isl_map_is_translation(__isl_keep isl_map
*map
)
6824 delta
= isl_map_deltas(isl_map_copy(map
));
6825 ok
= isl_set_is_singleton(delta
);
6826 isl_set_free(delta
);
6831 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
6833 if (isl_seq_first_non_zero(p
, pos
) != -1)
6835 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
6840 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
6849 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
6852 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
6853 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
6854 for (j
= 0; j
< nvar
; ++j
) {
6855 int lower
= 0, upper
= 0;
6856 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6857 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
6859 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
6865 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6866 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
6868 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
6870 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
6875 if (!lower
|| !upper
)
6882 int isl_set_is_box(__isl_keep isl_set
*set
)
6889 return isl_basic_set_is_box(set
->p
[0]);