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
);
2402 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2403 old
= bmap
->block2
.data
;
2404 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2405 (bmap
->extra
+ n
) * (1 + row_size
));
2406 if (!bmap
->block2
.data
)
2408 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2411 for (i
= 0; i
< n
; ++i
) {
2412 new_div
[i
] = bmap
->block2
.data
+
2413 (bmap
->extra
+ i
) * (1 + row_size
);
2414 isl_seq_clr(new_div
[i
], 1 + row_size
);
2416 for (i
= 0; i
< bmap
->extra
; ++i
)
2417 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2419 bmap
->div
= new_div
;
2423 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2426 bmap
= isl_basic_map_simplify(bmap
);
2427 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2428 return isl_basic_map_finalize(bmap
);
2430 isl_basic_map_free(bmap
);
2434 /* Turn the n dimensions of type type, starting at first
2435 * into existentially quantified variables.
2437 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2438 enum isl_dim_type type
, unsigned first
, unsigned n
)
2440 return (isl_basic_set
*)isl_basic_map_project_out(
2441 (isl_basic_map
*)bset
, type
, first
, n
);
2444 /* Turn the n dimensions of type type, starting at first
2445 * into existentially quantified variables.
2447 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2448 enum isl_dim_type type
, unsigned first
, unsigned n
)
2458 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2460 map
= isl_map_cow(map
);
2464 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2468 for (i
= 0; i
< map
->n
; ++i
) {
2469 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2480 /* Turn the n dimensions of type type, starting at first
2481 * into existentially quantified variables.
2483 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2484 enum isl_dim_type type
, unsigned first
, unsigned n
)
2486 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2489 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2493 for (i
= 0; i
< n
; ++i
) {
2494 j
= isl_basic_map_alloc_div(bmap
);
2497 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2501 isl_basic_map_free(bmap
);
2505 struct isl_basic_map
*isl_basic_map_apply_range(
2506 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2508 struct isl_dim
*dim_result
= NULL
;
2509 struct isl_basic_map
*bmap
;
2510 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2511 struct isl_dim_map
*dim_map1
, *dim_map2
;
2513 if (!bmap1
|| !bmap2
)
2516 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2517 isl_dim_copy(bmap2
->dim
));
2519 n_in
= isl_basic_map_n_in(bmap1
);
2520 n_out
= isl_basic_map_n_out(bmap2
);
2521 n
= isl_basic_map_n_out(bmap1
);
2522 nparam
= isl_basic_map_n_param(bmap1
);
2524 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2525 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2526 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2527 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2528 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2529 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2530 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2531 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2532 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2533 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2534 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2536 bmap
= isl_basic_map_alloc_dim(dim_result
,
2537 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2538 bmap1
->n_eq
+ bmap2
->n_eq
,
2539 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2540 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2541 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2542 bmap
= add_divs(bmap
, n
);
2543 bmap
= isl_basic_map_simplify(bmap
);
2544 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2545 return isl_basic_map_finalize(bmap
);
2547 isl_basic_map_free(bmap1
);
2548 isl_basic_map_free(bmap2
);
2552 struct isl_basic_set
*isl_basic_set_apply(
2553 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2558 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2561 return (struct isl_basic_set
*)
2562 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2564 isl_basic_set_free(bset
);
2565 isl_basic_map_free(bmap
);
2569 struct isl_basic_map
*isl_basic_map_apply_domain(
2570 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2572 if (!bmap1
|| !bmap2
)
2575 isl_assert(bmap1
->ctx
,
2576 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2577 isl_assert(bmap1
->ctx
,
2578 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2581 bmap1
= isl_basic_map_reverse(bmap1
);
2582 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2583 return isl_basic_map_reverse(bmap1
);
2585 isl_basic_map_free(bmap1
);
2586 isl_basic_map_free(bmap2
);
2590 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2591 * A \cap B -> f(A) + f(B)
2593 struct isl_basic_map
*isl_basic_map_sum(
2594 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2596 unsigned n_in
, n_out
, nparam
, total
, pos
;
2597 struct isl_basic_map
*bmap
= NULL
;
2598 struct isl_dim_map
*dim_map1
, *dim_map2
;
2601 if (!bmap1
|| !bmap2
)
2604 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2607 nparam
= isl_basic_map_n_param(bmap1
);
2608 n_in
= isl_basic_map_n_in(bmap1
);
2609 n_out
= isl_basic_map_n_out(bmap1
);
2611 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2612 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2613 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2614 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2615 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2616 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2617 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2618 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2619 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2620 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2621 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2623 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2624 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2625 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2626 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2627 for (i
= 0; i
< n_out
; ++i
) {
2628 int j
= isl_basic_map_alloc_equality(bmap
);
2631 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2632 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2633 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2634 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2636 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2637 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2638 bmap
= add_divs(bmap
, 2 * n_out
);
2640 bmap
= isl_basic_map_simplify(bmap
);
2641 return isl_basic_map_finalize(bmap
);
2643 isl_basic_map_free(bmap
);
2644 isl_basic_map_free(bmap1
);
2645 isl_basic_map_free(bmap2
);
2649 /* Given two maps A -> f(A) and B -> g(B), construct a map
2650 * A \cap B -> f(A) + f(B)
2652 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2654 struct isl_map
*result
;
2660 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2662 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2663 map1
->n
* map2
->n
, 0);
2666 for (i
= 0; i
< map1
->n
; ++i
)
2667 for (j
= 0; j
< map2
->n
; ++j
) {
2668 struct isl_basic_map
*part
;
2669 part
= isl_basic_map_sum(
2670 isl_basic_map_copy(map1
->p
[i
]),
2671 isl_basic_map_copy(map2
->p
[j
]));
2672 if (isl_basic_map_is_empty(part
))
2673 isl_basic_map_free(part
);
2675 result
= isl_map_add_basic_map(result
, part
);
2688 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2689 __isl_take isl_set
*set2
)
2691 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2694 /* Given a basic map A -> f(A), construct A -> -f(A).
2696 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2701 bmap
= isl_basic_map_cow(bmap
);
2705 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2706 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2707 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2708 for (j
= 0; j
< n
; ++j
)
2709 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2710 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2711 for (j
= 0; j
< n
; ++j
)
2712 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2713 for (i
= 0; i
< bmap
->n_div
; ++i
)
2714 for (j
= 0; j
< n
; ++j
)
2715 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2716 return isl_basic_map_finalize(bmap
);
2719 /* Given a map A -> f(A), construct A -> -f(A).
2721 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2725 map
= isl_map_cow(map
);
2729 for (i
= 0; i
< map
->n
; ++i
) {
2730 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2741 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2743 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2746 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2747 * A -> floor(f(A)/d).
2749 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2752 unsigned n_in
, n_out
, nparam
, total
, pos
;
2753 struct isl_basic_map
*result
= NULL
;
2754 struct isl_dim_map
*dim_map
;
2760 nparam
= isl_basic_map_n_param(bmap
);
2761 n_in
= isl_basic_map_n_in(bmap
);
2762 n_out
= isl_basic_map_n_out(bmap
);
2764 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2765 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2766 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2767 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2768 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2769 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2771 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2772 bmap
->n_div
+ n_out
,
2773 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2774 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2775 result
= add_divs(result
, n_out
);
2776 for (i
= 0; i
< n_out
; ++i
) {
2778 j
= isl_basic_map_alloc_inequality(result
);
2781 isl_seq_clr(result
->ineq
[j
], 1+total
);
2782 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2783 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2784 j
= isl_basic_map_alloc_inequality(result
);
2787 isl_seq_clr(result
->ineq
[j
], 1+total
);
2788 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2789 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2790 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2793 result
= isl_basic_map_simplify(result
);
2794 return isl_basic_map_finalize(result
);
2796 isl_basic_map_free(result
);
2800 /* Given a map A -> f(A) and an integer d, construct a map
2801 * A -> floor(f(A)/d).
2803 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2807 map
= isl_map_cow(map
);
2811 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2812 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2813 for (i
= 0; i
< map
->n
; ++i
) {
2814 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2825 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2831 i
= isl_basic_map_alloc_equality(bmap
);
2834 nparam
= isl_basic_map_n_param(bmap
);
2835 n_in
= isl_basic_map_n_in(bmap
);
2836 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2837 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2838 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2839 return isl_basic_map_finalize(bmap
);
2841 isl_basic_map_free(bmap
);
2845 /* Add a constraints to "bmap" expressing i_pos < o_pos
2847 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2853 i
= isl_basic_map_alloc_inequality(bmap
);
2856 nparam
= isl_basic_map_n_param(bmap
);
2857 n_in
= isl_basic_map_n_in(bmap
);
2858 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2859 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2860 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2861 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2862 return isl_basic_map_finalize(bmap
);
2864 isl_basic_map_free(bmap
);
2868 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2870 static __isl_give isl_basic_map
*var_less_or_equal(
2871 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2877 i
= isl_basic_map_alloc_inequality(bmap
);
2880 nparam
= isl_basic_map_n_param(bmap
);
2881 n_in
= isl_basic_map_n_in(bmap
);
2882 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2883 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2884 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2885 return isl_basic_map_finalize(bmap
);
2887 isl_basic_map_free(bmap
);
2891 /* Add a constraints to "bmap" expressing i_pos > o_pos
2893 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2899 i
= isl_basic_map_alloc_inequality(bmap
);
2902 nparam
= isl_basic_map_n_param(bmap
);
2903 n_in
= isl_basic_map_n_in(bmap
);
2904 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2905 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2906 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2907 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2908 return isl_basic_map_finalize(bmap
);
2910 isl_basic_map_free(bmap
);
2914 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2916 static __isl_give isl_basic_map
*var_more_or_equal(
2917 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2923 i
= isl_basic_map_alloc_inequality(bmap
);
2926 nparam
= isl_basic_map_n_param(bmap
);
2927 n_in
= isl_basic_map_n_in(bmap
);
2928 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2929 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2930 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2931 return isl_basic_map_finalize(bmap
);
2933 isl_basic_map_free(bmap
);
2937 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2940 struct isl_basic_map
*bmap
;
2941 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2944 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2945 bmap
= var_equal(bmap
, i
);
2946 return isl_basic_map_finalize(bmap
);
2949 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2951 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2954 struct isl_basic_map
*bmap
;
2955 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2958 for (i
= 0; i
< pos
&& bmap
; ++i
)
2959 bmap
= var_equal(bmap
, i
);
2961 bmap
= var_less(bmap
, pos
);
2962 return isl_basic_map_finalize(bmap
);
2965 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2967 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
2968 __isl_take isl_dim
*dim
, unsigned pos
)
2971 isl_basic_map
*bmap
;
2973 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2974 for (i
= 0; i
< pos
; ++i
)
2975 bmap
= var_equal(bmap
, i
);
2976 bmap
= var_less_or_equal(bmap
, pos
);
2977 return isl_basic_map_finalize(bmap
);
2980 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2982 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2985 struct isl_basic_map
*bmap
;
2986 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2989 for (i
= 0; i
< pos
&& bmap
; ++i
)
2990 bmap
= var_equal(bmap
, i
);
2992 bmap
= var_more(bmap
, pos
);
2993 return isl_basic_map_finalize(bmap
);
2996 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
2998 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
2999 __isl_take isl_dim
*dim
, unsigned pos
)
3002 isl_basic_map
*bmap
;
3004 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3005 for (i
= 0; i
< pos
; ++i
)
3006 bmap
= var_equal(bmap
, i
);
3007 bmap
= var_more_or_equal(bmap
, pos
);
3008 return isl_basic_map_finalize(bmap
);
3011 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3012 unsigned n
, int equal
)
3014 struct isl_map
*map
;
3017 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3019 for (i
= 0; i
+ 1 < n
; ++i
)
3020 map
= isl_map_add_basic_map(map
,
3021 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3024 map
= isl_map_add_basic_map(map
,
3025 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3027 map
= isl_map_add_basic_map(map
,
3028 isl_basic_map_less_at(dims
, n
- 1));
3035 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3039 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3042 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3044 return map_lex_lte_first(dim
, n
, 0);
3047 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3049 return map_lex_lte_first(dim
, n
, 1);
3052 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3054 return map_lex_lte(isl_dim_map(set_dim
), 0);
3057 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3059 return map_lex_lte(isl_dim_map(set_dim
), 1);
3062 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3063 unsigned n
, int equal
)
3065 struct isl_map
*map
;
3068 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3070 for (i
= 0; i
+ 1 < n
; ++i
)
3071 map
= isl_map_add_basic_map(map
,
3072 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3075 map
= isl_map_add_basic_map(map
,
3076 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3078 map
= isl_map_add_basic_map(map
,
3079 isl_basic_map_more_at(dims
, n
- 1));
3086 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3090 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3093 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3095 return map_lex_gte_first(dim
, n
, 0);
3098 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3100 return map_lex_gte_first(dim
, n
, 1);
3103 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3105 return map_lex_gte(isl_dim_map(set_dim
), 0);
3108 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3110 return map_lex_gte(isl_dim_map(set_dim
), 1);
3113 struct isl_basic_map
*isl_basic_map_from_basic_set(
3114 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3116 struct isl_basic_map
*bmap
;
3118 bset
= isl_basic_set_cow(bset
);
3122 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3123 isl_dim_free(bset
->dim
);
3124 bmap
= (struct isl_basic_map
*) bset
;
3126 return isl_basic_map_finalize(bmap
);
3128 isl_basic_set_free(bset
);
3133 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3137 if (bmap
->dim
->n_in
== 0)
3138 return (struct isl_basic_set
*)bmap
;
3139 bmap
= isl_basic_map_cow(bmap
);
3142 bmap
->dim
= isl_dim_cow(bmap
->dim
);
3145 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
3146 bmap
->dim
->n_in
= 0;
3147 bmap
= isl_basic_map_finalize(bmap
);
3148 return (struct isl_basic_set
*)bmap
;
3150 isl_basic_map_free(bmap
);
3154 /* For a div d = floor(f/m), add the constraints
3157 * -(f-(n-1)) + m d >= 0
3159 * Note that the second constraint is the negation of
3163 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3164 unsigned pos
, isl_int
*div
)
3167 unsigned total
= isl_basic_map_total_dim(bmap
);
3169 i
= isl_basic_map_alloc_inequality(bmap
);
3172 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3173 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3175 j
= isl_basic_map_alloc_inequality(bmap
);
3178 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3179 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3180 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3184 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3185 unsigned pos
, isl_int
*div
)
3187 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3191 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3193 unsigned total
= isl_basic_map_total_dim(bmap
);
3194 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3196 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3200 struct isl_basic_set
*isl_basic_map_underlying_set(
3201 struct isl_basic_map
*bmap
)
3205 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
3206 return (struct isl_basic_set
*)bmap
;
3207 bmap
= isl_basic_map_cow(bmap
);
3210 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3213 bmap
->extra
-= bmap
->n_div
;
3215 bmap
= isl_basic_map_finalize(bmap
);
3216 return (struct isl_basic_set
*)bmap
;
3221 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3222 __isl_take isl_basic_set
*bset
)
3224 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3227 struct isl_basic_map
*isl_basic_map_overlying_set(
3228 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3230 struct isl_basic_map
*bmap
;
3231 struct isl_ctx
*ctx
;
3238 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3239 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3240 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3242 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3243 isl_basic_map_free(like
);
3244 return (struct isl_basic_map
*)bset
;
3246 bset
= isl_basic_set_cow(bset
);
3249 total
= bset
->dim
->n_out
+ bset
->extra
;
3250 bmap
= (struct isl_basic_map
*)bset
;
3251 isl_dim_free(bmap
->dim
);
3252 bmap
->dim
= isl_dim_copy(like
->dim
);
3255 bmap
->n_div
= like
->n_div
;
3256 bmap
->extra
+= like
->n_div
;
3259 ltotal
= total
- bmap
->extra
+ like
->extra
;
3262 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3263 bmap
->extra
* (1 + 1 + total
));
3264 if (isl_blk_is_error(bmap
->block2
))
3266 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3270 for (i
= 0; i
< bmap
->extra
; ++i
)
3271 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3272 for (i
= 0; i
< like
->n_div
; ++i
) {
3273 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3274 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3276 bmap
= isl_basic_map_extend_constraints(bmap
,
3277 0, 2 * like
->n_div
);
3278 for (i
= 0; i
< like
->n_div
; ++i
) {
3279 if (isl_int_is_zero(bmap
->div
[i
][0]))
3281 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3285 isl_basic_map_free(like
);
3286 bmap
= isl_basic_map_simplify(bmap
);
3287 bmap
= isl_basic_map_finalize(bmap
);
3290 isl_basic_map_free(like
);
3291 isl_basic_set_free(bset
);
3295 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3296 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3298 return (struct isl_basic_set
*)
3299 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3302 struct isl_set
*isl_set_from_underlying_set(
3303 struct isl_set
*set
, struct isl_basic_set
*like
)
3309 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3311 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3312 isl_basic_set_free(like
);
3315 set
= isl_set_cow(set
);
3318 for (i
= 0; i
< set
->n
; ++i
) {
3319 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3320 isl_basic_set_copy(like
));
3324 isl_dim_free(set
->dim
);
3325 set
->dim
= isl_dim_copy(like
->dim
);
3328 isl_basic_set_free(like
);
3331 isl_basic_set_free(like
);
3336 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3340 map
= isl_map_cow(map
);
3343 map
->dim
= isl_dim_cow(map
->dim
);
3347 for (i
= 1; i
< map
->n
; ++i
)
3348 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3350 for (i
= 0; i
< map
->n
; ++i
) {
3351 map
->p
[i
] = (struct isl_basic_map
*)
3352 isl_basic_map_underlying_set(map
->p
[i
]);
3357 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3359 isl_dim_free(map
->dim
);
3360 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3364 return (struct isl_set
*)map
;
3370 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3372 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3375 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3377 struct isl_basic_set
*domain
;
3382 n_in
= isl_basic_map_n_in(bmap
);
3383 n_out
= isl_basic_map_n_out(bmap
);
3384 domain
= isl_basic_set_from_basic_map(bmap
);
3385 return isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3388 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3390 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3393 struct isl_set
*isl_map_range(struct isl_map
*map
)
3396 struct isl_set
*set
;
3400 if (isl_map_dim(map
, isl_dim_in
) == 0)
3401 return (isl_set
*)map
;
3403 map
= isl_map_cow(map
);
3407 set
= (struct isl_set
*) map
;
3408 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3411 for (i
= 0; i
< map
->n
; ++i
) {
3412 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3416 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3417 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3424 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3427 struct isl_map
*map
= NULL
;
3429 set
= isl_set_cow(set
);
3432 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3433 map
= (struct isl_map
*)set
;
3434 for (i
= 0; i
< set
->n
; ++i
) {
3435 map
->p
[i
] = isl_basic_map_from_basic_set(
3436 set
->p
[i
], isl_dim_copy(dim
));
3440 isl_dim_free(map
->dim
);
3449 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3451 return (struct isl_map
*)set
;
3454 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3456 return isl_map_reverse(isl_map_from_range(set
));;
3459 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3460 __isl_take isl_set
*range
)
3462 return isl_map_product(isl_map_from_domain(domain
),
3463 isl_map_from_range(range
));
3466 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3469 struct isl_set
*set
= NULL
;
3473 map
= isl_map_cow(map
);
3476 map
->dim
= isl_dim_cow(map
->dim
);
3479 map
->dim
->n_out
+= map
->dim
->n_in
;
3481 set
= (struct isl_set
*)map
;
3482 for (i
= 0; i
< map
->n
; ++i
) {
3483 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3493 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3495 struct isl_map
*map
;
3499 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3500 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3501 sizeof(struct isl_map
) +
3502 (n
- 1) * sizeof(struct isl_basic_map
*));
3506 map
->ctx
= dim
->ctx
;
3507 isl_ctx_ref(map
->ctx
);
3519 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
3520 unsigned nparam
, unsigned in
, unsigned out
, int n
,
3523 struct isl_map
*map
;
3524 struct isl_dim
*dims
;
3526 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
3530 map
= isl_map_alloc_dim(dims
, n
, flags
);
3534 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
3536 struct isl_basic_map
*bmap
;
3537 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
3538 bmap
= isl_basic_map_set_to_empty(bmap
);
3542 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
3544 struct isl_basic_set
*bset
;
3545 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
3546 bset
= isl_basic_set_set_to_empty(bset
);
3550 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
3552 struct isl_basic_map
*bmap
;
3555 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3556 bmap
= isl_basic_map_set_to_empty(bmap
);
3560 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
3562 struct isl_basic_map
*bmap
;
3565 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3566 bmap
= isl_basic_map_set_to_empty(bmap
);
3570 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
3572 struct isl_basic_set
*bset
;
3575 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3576 bset
= isl_basic_set_set_to_empty(bset
);
3580 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
3582 struct isl_basic_map
*bmap
;
3583 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
3587 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
3589 struct isl_basic_set
*bset
;
3590 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
3594 __isl_give isl_basic_map
*isl_basic_map_universe_like(
3595 __isl_keep isl_basic_map
*model
)
3599 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3602 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
3606 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3609 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
3610 __isl_keep isl_set
*model
)
3614 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3617 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
3619 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3622 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
3626 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3629 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
3633 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3636 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
3638 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3641 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
3645 return isl_set_empty(isl_dim_copy(model
->dim
));
3648 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
3650 struct isl_map
*map
;
3653 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3654 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
3658 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
3660 struct isl_set
*set
;
3663 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3664 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
3668 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3672 return isl_set_universe(isl_dim_copy(model
->dim
));
3675 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3678 struct isl_map
*dup
;
3682 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3683 for (i
= 0; i
< map
->n
; ++i
)
3684 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
3688 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
3689 __isl_take isl_basic_map
*bmap
)
3693 if (isl_basic_map_fast_is_empty(bmap
)) {
3694 isl_basic_map_free(bmap
);
3697 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3698 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3699 map
->p
[map
->n
] = bmap
;
3701 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3707 isl_basic_map_free(bmap
);
3711 void isl_map_free(struct isl_map
*map
)
3721 isl_ctx_deref(map
->ctx
);
3722 for (i
= 0; i
< map
->n
; ++i
)
3723 isl_basic_map_free(map
->p
[i
]);
3724 isl_dim_free(map
->dim
);
3728 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3729 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3733 base
= isl_map_cow(base
);
3737 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3740 for (i
= 0; i
< base
->n
; ++i
) {
3741 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3742 isl_dim_copy(base
->dim
), 0, 0, 0);
3752 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3753 unsigned nparam
, unsigned dim
)
3755 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3759 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3760 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3764 bmap
= isl_basic_map_cow(bmap
);
3765 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3766 j
= isl_basic_map_alloc_equality(bmap
);
3769 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3770 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3771 isl_int_set_si(bmap
->eq
[j
][0], value
);
3772 bmap
= isl_basic_map_simplify(bmap
);
3773 return isl_basic_map_finalize(bmap
);
3775 isl_basic_map_free(bmap
);
3779 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3780 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3784 bmap
= isl_basic_map_cow(bmap
);
3785 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3786 j
= isl_basic_map_alloc_equality(bmap
);
3789 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3790 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3791 isl_int_set(bmap
->eq
[j
][0], value
);
3792 bmap
= isl_basic_map_simplify(bmap
);
3793 return isl_basic_map_finalize(bmap
);
3795 isl_basic_map_free(bmap
);
3799 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3800 enum isl_dim_type type
, unsigned pos
, int value
)
3804 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3805 return isl_basic_map_fix_pos_si(bmap
,
3806 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3808 isl_basic_map_free(bmap
);
3812 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3813 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3817 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3818 return isl_basic_map_fix_pos(bmap
,
3819 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3821 isl_basic_map_free(bmap
);
3825 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3826 enum isl_dim_type type
, unsigned pos
, int value
)
3828 return (struct isl_basic_set
*)
3829 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3833 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3834 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3836 return (struct isl_basic_set
*)
3837 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3841 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3842 unsigned input
, int value
)
3844 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3847 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3848 unsigned dim
, int value
)
3850 return (struct isl_basic_set
*)
3851 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3852 isl_dim_set
, dim
, value
);
3855 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3856 enum isl_dim_type type
, unsigned pos
, int value
)
3860 map
= isl_map_cow(map
);
3864 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3865 for (i
= 0; i
< map
->n
; ++i
) {
3866 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
3870 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3877 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
3878 enum isl_dim_type type
, unsigned pos
, int value
)
3880 return (struct isl_set
*)
3881 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
3884 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
3885 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3889 map
= isl_map_cow(map
);
3893 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3894 for (i
= 0; i
< map
->n
; ++i
) {
3895 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
3899 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3906 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
3907 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3909 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
3912 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
3913 unsigned input
, int value
)
3915 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
3918 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
3920 return (struct isl_set
*)
3921 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
3924 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
3925 __isl_take isl_basic_map
*bmap
,
3926 enum isl_dim_type type
, unsigned pos
, int value
)
3932 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3933 pos
+= isl_basic_map_offset(bmap
, type
);
3934 bmap
= isl_basic_map_cow(bmap
);
3935 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
3936 j
= isl_basic_map_alloc_inequality(bmap
);
3939 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3940 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
3941 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
3942 bmap
= isl_basic_map_simplify(bmap
);
3943 return isl_basic_map_finalize(bmap
);
3945 isl_basic_map_free(bmap
);
3949 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
3950 unsigned dim
, isl_int value
)
3954 bset
= isl_basic_set_cow(bset
);
3955 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
3956 j
= isl_basic_set_alloc_inequality(bset
);
3959 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3960 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3961 isl_int_neg(bset
->ineq
[j
][0], value
);
3962 bset
= isl_basic_set_simplify(bset
);
3963 return isl_basic_set_finalize(bset
);
3965 isl_basic_set_free(bset
);
3969 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
3970 enum isl_dim_type type
, unsigned pos
, int value
)
3974 map
= isl_map_cow(map
);
3978 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3979 for (i
= 0; i
< map
->n
; ++i
) {
3980 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
3985 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3992 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
3993 enum isl_dim_type type
, unsigned pos
, int value
)
3995 return (struct isl_set
*)
3996 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
3999 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4004 set
= isl_set_cow(set
);
4008 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4009 for (i
= 0; i
< set
->n
; ++i
) {
4010 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4020 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4024 map
= isl_map_cow(map
);
4028 map
->dim
= isl_dim_reverse(map
->dim
);
4031 for (i
= 0; i
< map
->n
; ++i
) {
4032 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4036 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4043 static struct isl_map
*isl_basic_map_partial_lexopt(
4044 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4045 struct isl_set
**empty
, int max
)
4049 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4050 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4052 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4054 isl_basic_map_free(bmap
);
4055 isl_basic_set_free(dom
);
4061 struct isl_map
*isl_basic_map_partial_lexmax(
4062 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4063 struct isl_set
**empty
)
4065 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4068 struct isl_map
*isl_basic_map_partial_lexmin(
4069 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4070 struct isl_set
**empty
)
4072 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4075 struct isl_set
*isl_basic_set_partial_lexmin(
4076 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4077 struct isl_set
**empty
)
4079 return (struct isl_set
*)
4080 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4084 struct isl_set
*isl_basic_set_partial_lexmax(
4085 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4086 struct isl_set
**empty
)
4088 return (struct isl_set
*)
4089 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4093 /* Given a basic map "bmap", compute the lexicograhically minimal
4094 * (or maximal) image element for each domain element in dom.
4095 * Set *empty to those elements in dom that do not have an image element.
4097 * We first make sure the basic sets in dom are disjoint and then
4098 * simply collect the results over each of the basic sets separately.
4099 * We could probably improve the efficiency a bit by moving the union
4100 * domain down into the parametric integer programming.
4102 static __isl_give isl_map
*basic_map_partial_lexopt(
4103 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4104 __isl_give isl_set
**empty
, int max
)
4107 struct isl_map
*res
;
4109 dom
= isl_set_make_disjoint(dom
);
4113 if (isl_set_fast_is_empty(dom
)) {
4114 res
= isl_map_empty_like_basic_map(bmap
);
4115 *empty
= isl_set_empty_like(dom
);
4117 isl_basic_map_free(bmap
);
4121 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4122 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4124 for (i
= 1; i
< dom
->n
; ++i
) {
4125 struct isl_map
*res_i
;
4126 struct isl_set
*empty_i
;
4128 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4129 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4131 res
= isl_map_union_disjoint(res
, res_i
);
4132 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4136 isl_basic_map_free(bmap
);
4141 isl_basic_map_free(bmap
);
4145 /* Given a map "map", compute the lexicograhically minimal
4146 * (or maximal) image element for each domain element in dom.
4147 * Set *empty to those elements in dom that do not have an image element.
4149 * We first compute the lexicographically minimal or maximal element
4150 * in the first basic map. This results in a partial solution "res"
4151 * and a subset "todo" of dom that still need to be handled.
4152 * We then consider each of the remaining maps in "map" and successively
4153 * improve both "res" and "todo".
4155 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4156 * Assume we are computing the lexicographical maximum.
4157 * We first intersect basic map i with a relation that maps elements
4158 * to elements that are lexicographically larger than the image elements
4159 * in res^k and the compute the maximum image element of this intersection.
4160 * The result ("better") corresponds to those image elements in basic map i
4161 * that are better than what we had before. The remainder ("keep") are the
4162 * domain elements for which the image element in res_k was better.
4163 * We also compute the lexicographical maximum of basic map i in todo^k.
4164 * res^i is the result of the operation + better + those elements in
4165 * res^k that we should keep
4166 * todo^i is the remainder of the maximum operation on todo^k.
4168 static __isl_give isl_map
*isl_map_partial_lexopt(
4169 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4170 __isl_give isl_set
**empty
, int max
)
4173 struct isl_map
*res
;
4174 struct isl_set
*todo
;
4179 if (isl_map_fast_is_empty(map
)) {
4187 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4188 isl_set_copy(dom
), &todo
, max
);
4190 for (i
= 1; i
< map
->n
; ++i
) {
4192 struct isl_map
*better
;
4193 struct isl_set
*keep
;
4194 struct isl_map
*res_i
;
4195 struct isl_set
*todo_i
;
4196 struct isl_dim
*dim
= isl_map_get_dim(res
);
4198 dim
= isl_dim_range(dim
);
4200 lt
= isl_map_lex_lt(dim
);
4202 lt
= isl_map_lex_gt(dim
);
4203 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4204 lt
= isl_map_intersect(lt
,
4205 isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
])));
4206 better
= isl_map_partial_lexopt(lt
,
4207 isl_map_domain(isl_map_copy(res
)),
4210 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4211 todo
, &todo_i
, max
);
4213 res
= isl_map_intersect_domain(res
, keep
);
4214 res
= isl_map_union_disjoint(res
, res_i
);
4215 res
= isl_map_union_disjoint(res
, better
);
4236 __isl_give isl_map
*isl_map_partial_lexmax(
4237 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4238 __isl_give isl_set
**empty
)
4240 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4243 __isl_give isl_map
*isl_map_partial_lexmin(
4244 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4245 __isl_give isl_set
**empty
)
4247 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4250 __isl_give isl_set
*isl_set_partial_lexmin(
4251 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4252 __isl_give isl_set
**empty
)
4254 return (struct isl_set
*)
4255 isl_map_partial_lexmin((struct isl_map
*)set
,
4259 __isl_give isl_set
*isl_set_partial_lexmax(
4260 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4261 __isl_give isl_set
**empty
)
4263 return (struct isl_set
*)
4264 isl_map_partial_lexmax((struct isl_map
*)set
,
4268 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4270 struct isl_basic_set
*dom
= NULL
;
4271 struct isl_dim
*dom_dim
;
4275 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4276 dom
= isl_basic_set_universe(dom_dim
);
4277 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4279 isl_basic_map_free(bmap
);
4283 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4285 return isl_basic_map_lexopt(bmap
, 0);
4288 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4290 return isl_basic_map_lexopt(bmap
, 1);
4293 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4295 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4298 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4300 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4303 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4305 struct isl_set
*dom
= NULL
;
4306 struct isl_dim
*dom_dim
;
4310 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4311 dom
= isl_set_universe(dom_dim
);
4312 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4318 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4320 return isl_map_lexopt(map
, 0);
4323 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4325 return isl_map_lexopt(map
, 1);
4328 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4330 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4333 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4335 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4338 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
4339 struct isl_dim
*dim
)
4346 for (i
= 0; i
< map
->n
; ++i
) {
4347 isl_dim_free(map
->p
[i
]->dim
);
4348 map
->p
[i
]->dim
= isl_dim_copy(dim
);
4350 isl_dim_free(map
->dim
);
4360 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
4361 struct isl_dim
*dim
)
4363 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
4366 /* Apply a preimage specified by "mat" on the parameters of "bset".
4367 * bset is assumed to have only parameters and divs.
4369 static struct isl_basic_set
*basic_set_parameter_preimage(
4370 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4377 bset
->dim
= isl_dim_cow(bset
->dim
);
4381 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4383 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4385 bset
->dim
->nparam
= 0;
4386 bset
->dim
->n_out
= nparam
;
4387 bset
= isl_basic_set_preimage(bset
, mat
);
4389 bset
->dim
->nparam
= bset
->dim
->n_out
;
4390 bset
->dim
->n_out
= 0;
4395 isl_basic_set_free(bset
);
4399 /* Apply a preimage specified by "mat" on the parameters of "set".
4400 * set is assumed to have only parameters and divs.
4402 static struct isl_set
*set_parameter_preimage(
4403 struct isl_set
*set
, struct isl_mat
*mat
)
4405 struct isl_dim
*dim
= NULL
;
4411 dim
= isl_dim_copy(set
->dim
);
4412 dim
= isl_dim_cow(dim
);
4416 nparam
= isl_set_dim(set
, isl_dim_param
);
4418 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4421 dim
->n_out
= nparam
;
4422 isl_set_reset_dim(set
, dim
);
4423 set
= isl_set_preimage(set
, mat
);
4426 dim
= isl_dim_copy(set
->dim
);
4427 dim
= isl_dim_cow(dim
);
4430 dim
->nparam
= dim
->n_out
;
4432 isl_set_reset_dim(set
, dim
);
4442 /* Intersect the basic set "bset" with the affine space specified by the
4443 * equalities in "eq".
4445 static struct isl_basic_set
*basic_set_append_equalities(
4446 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4454 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4459 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4460 for (i
= 0; i
< eq
->n_row
; ++i
) {
4461 k
= isl_basic_set_alloc_equality(bset
);
4464 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4465 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
4469 bset
= isl_basic_set_gauss(bset
, NULL
);
4470 bset
= isl_basic_set_finalize(bset
);
4475 isl_basic_set_free(bset
);
4479 /* Intersect the set "set" with the affine space specified by the
4480 * equalities in "eq".
4482 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
4490 for (i
= 0; i
< set
->n
; ++i
) {
4491 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
4504 /* Project the given basic set onto its parameter domain, possibly introducing
4505 * new, explicit, existential variables in the constraints.
4506 * The input has parameters and (possibly implicit) existential variables.
4507 * The output has the same parameters, but only
4508 * explicit existentially quantified variables.
4510 * The actual projection is performed by pip, but pip doesn't seem
4511 * to like equalities very much, so we first remove the equalities
4512 * among the parameters by performing a variable compression on
4513 * the parameters. Afterward, an inverse transformation is performed
4514 * and the equalities among the parameters are inserted back in.
4516 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
4520 struct isl_mat
*T
, *T2
;
4521 struct isl_set
*set
;
4522 unsigned nparam
, n_div
;
4524 bset
= isl_basic_set_cow(bset
);
4528 if (bset
->n_eq
== 0)
4529 return isl_basic_set_lexmin(bset
);
4531 isl_basic_set_gauss(bset
, NULL
);
4533 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4534 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
4536 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
4537 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
4540 if (i
== bset
->n_eq
)
4541 return isl_basic_set_lexmin(bset
);
4543 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
4545 eq
= isl_mat_cow(eq
);
4546 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
4547 if (T
&& T
->n_col
== 0) {
4551 bset
= isl_basic_set_set_to_empty(bset
);
4552 return isl_set_from_basic_set(bset
);
4554 bset
= basic_set_parameter_preimage(bset
, T
);
4556 set
= isl_basic_set_lexmin(bset
);
4557 set
= set_parameter_preimage(set
, T2
);
4558 set
= set_append_equalities(set
, eq
);
4562 /* Compute an explicit representation for all the existentially
4563 * quantified variables.
4564 * The input and output dimensions are first turned into parameters.
4565 * compute_divs then returns a map with the same parameters and
4566 * no input or output dimensions and the dimension specification
4567 * is reset to that of the input.
4569 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
4571 struct isl_basic_set
*bset
;
4572 struct isl_set
*set
;
4573 struct isl_map
*map
;
4574 struct isl_dim
*dim
, *orig_dim
= NULL
;
4579 bmap
= isl_basic_map_cow(bmap
);
4583 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4584 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4585 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4586 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
4590 orig_dim
= bmap
->dim
;
4592 bset
= (struct isl_basic_set
*)bmap
;
4594 set
= parameter_compute_divs(bset
);
4595 map
= (struct isl_map
*)set
;
4596 map
= isl_map_reset_dim(map
, orig_dim
);
4600 isl_basic_map_free(bmap
);
4604 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
4612 off
= isl_dim_total(bmap
->dim
);
4613 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4614 if (isl_int_is_zero(bmap
->div
[i
][0]))
4616 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
4622 static int map_divs_known(__isl_keep isl_map
*map
)
4629 for (i
= 0; i
< map
->n
; ++i
) {
4630 int known
= basic_map_divs_known(map
->p
[i
]);
4638 /* If bmap contains any unknown divs, then compute explicit
4639 * expressions for them. However, this computation may be
4640 * quite expensive, so first try to remove divs that aren't
4643 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
4647 struct isl_map
*map
;
4649 known
= basic_map_divs_known(bmap
);
4653 return isl_map_from_basic_map(bmap
);
4655 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4657 known
= basic_map_divs_known(bmap
);
4661 return isl_map_from_basic_map(bmap
);
4663 map
= compute_divs(bmap
);
4666 isl_basic_map_free(bmap
);
4670 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
4674 struct isl_map
*res
;
4681 known
= map_divs_known(map
);
4689 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
4690 for (i
= 1 ; i
< map
->n
; ++i
) {
4692 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
4693 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4694 res
= isl_map_union_disjoint(res
, r2
);
4696 res
= isl_map_union(res
, r2
);
4703 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
4705 return (struct isl_set
*)
4706 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
4709 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
4711 return (struct isl_set
*)
4712 isl_map_compute_divs((struct isl_map
*)set
);
4715 struct isl_set
*isl_map_domain(struct isl_map
*map
)
4718 struct isl_set
*set
;
4723 map
= isl_map_cow(map
);
4727 set
= (struct isl_set
*)map
;
4728 set
->dim
= isl_dim_domain(set
->dim
);
4731 for (i
= 0; i
< map
->n
; ++i
) {
4732 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
4736 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4737 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4744 struct isl_map
*isl_map_union_disjoint(
4745 struct isl_map
*map1
, struct isl_map
*map2
)
4749 struct isl_map
*map
= NULL
;
4763 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4765 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4766 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4767 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4769 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
4770 map1
->n
+ map2
->n
, flags
);
4773 for (i
= 0; i
< map1
->n
; ++i
) {
4774 map
= isl_map_add_basic_map(map
,
4775 isl_basic_map_copy(map1
->p
[i
]));
4779 for (i
= 0; i
< map2
->n
; ++i
) {
4780 map
= isl_map_add_basic_map(map
,
4781 isl_basic_map_copy(map2
->p
[i
]));
4795 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
4797 map1
= isl_map_union_disjoint(map1
, map2
);
4801 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
4805 struct isl_set
*isl_set_union_disjoint(
4806 struct isl_set
*set1
, struct isl_set
*set2
)
4808 return (struct isl_set
*)
4809 isl_map_union_disjoint(
4810 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4813 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
4815 return (struct isl_set
*)
4816 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4819 struct isl_map
*isl_map_intersect_range(
4820 struct isl_map
*map
, struct isl_set
*set
)
4823 struct isl_map
*result
;
4829 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
4830 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
4831 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4833 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
4834 map
->n
* set
->n
, flags
);
4837 for (i
= 0; i
< map
->n
; ++i
)
4838 for (j
= 0; j
< set
->n
; ++j
) {
4839 result
= isl_map_add_basic_map(result
,
4840 isl_basic_map_intersect_range(
4841 isl_basic_map_copy(map
->p
[i
]),
4842 isl_basic_set_copy(set
->p
[j
])));
4855 struct isl_map
*isl_map_intersect_domain(
4856 struct isl_map
*map
, struct isl_set
*set
)
4858 return isl_map_reverse(
4859 isl_map_intersect_range(isl_map_reverse(map
), set
));
4862 struct isl_map
*isl_map_apply_domain(
4863 struct isl_map
*map1
, struct isl_map
*map2
)
4867 map1
= isl_map_reverse(map1
);
4868 map1
= isl_map_apply_range(map1
, map2
);
4869 return isl_map_reverse(map1
);
4876 struct isl_map
*isl_map_apply_range(
4877 struct isl_map
*map1
, struct isl_map
*map2
)
4879 struct isl_dim
*dim_result
;
4880 struct isl_map
*result
;
4886 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
4887 isl_dim_copy(map2
->dim
));
4889 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
4892 for (i
= 0; i
< map1
->n
; ++i
)
4893 for (j
= 0; j
< map2
->n
; ++j
) {
4894 result
= isl_map_add_basic_map(result
,
4895 isl_basic_map_apply_range(
4896 isl_basic_map_copy(map1
->p
[i
]),
4897 isl_basic_map_copy(map2
->p
[j
])));
4903 if (result
&& result
->n
<= 1)
4904 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
4913 * returns range - domain
4915 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
4918 struct isl_basic_set
*bset
;
4925 dim
= isl_basic_map_n_in(bmap
);
4926 nparam
= isl_basic_map_n_param(bmap
);
4927 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
4928 bset
= isl_basic_set_from_basic_map(bmap
);
4929 bset
= isl_basic_set_cow(bset
);
4930 dims
= isl_basic_set_get_dim(bset
);
4931 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
4932 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
4933 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
4934 for (i
= 0; i
< dim
; ++i
) {
4935 int j
= isl_basic_map_alloc_equality(
4936 (struct isl_basic_map
*)bset
);
4939 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
4940 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
4941 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
4942 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
4944 return isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
4946 isl_basic_map_free(bmap
);
4951 * returns range - domain
4953 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
4957 struct isl_set
*result
;
4962 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
4963 dim
= isl_map_get_dim(map
);
4964 dim
= isl_dim_domain(dim
);
4965 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
4968 for (i
= 0; i
< map
->n
; ++i
)
4969 result
= isl_set_add_basic_set(result
,
4970 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
4978 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
4980 struct isl_basic_map
*bmap
;
4988 nparam
= dims
->nparam
;
4990 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
4994 for (i
= 0; i
< dim
; ++i
) {
4995 int j
= isl_basic_map_alloc_equality(bmap
);
4998 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4999 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5000 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5002 return isl_basic_map_finalize(bmap
);
5004 isl_basic_map_free(bmap
);
5008 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5010 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5013 return basic_map_identity(dim
);
5016 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5018 if (!model
|| !model
->dim
)
5020 isl_assert(model
->ctx
,
5021 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5022 return basic_map_identity(isl_dim_copy(model
->dim
));
5025 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5027 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5028 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5031 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5033 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5036 return map_identity(dim
);
5039 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5041 if (!model
|| !model
->dim
)
5043 isl_assert(model
->ctx
,
5044 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5045 return map_identity(isl_dim_copy(model
->dim
));
5048 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5050 if (!model
|| !model
->dim
)
5052 isl_assert(model
->ctx
,
5053 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5054 return map_identity(isl_dim_copy(model
->dim
));
5057 /* Construct a basic set with all set dimensions having only non-negative
5060 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5065 struct isl_basic_set
*bset
;
5069 nparam
= dims
->nparam
;
5071 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5074 for (i
= 0; i
< dim
; ++i
) {
5075 int k
= isl_basic_set_alloc_inequality(bset
);
5078 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5079 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5083 isl_basic_set_free(bset
);
5087 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5088 enum isl_dim_type type
, unsigned first
, unsigned n
)
5091 isl_basic_set
*nonneg
= NULL
;
5092 isl_basic_set
*neg
= NULL
;
5099 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5101 for (i
= 0; i
< n
; ++i
) {
5105 nonneg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5106 k
= isl_basic_set_alloc_inequality(nonneg
);
5109 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5110 isl_int_set_si(nonneg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], 1);
5112 neg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5113 k
= isl_basic_set_alloc_inequality(neg
);
5116 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5117 isl_int_set_si(neg
->ineq
[k
][0], -1);
5118 isl_int_set_si(neg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], -1);
5120 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5125 isl_basic_set_free(nonneg
);
5126 isl_basic_set_free(neg
);
5131 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5133 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5136 int isl_basic_map_is_subset(
5137 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5140 struct isl_map
*map1
;
5141 struct isl_map
*map2
;
5143 if (!bmap1
|| !bmap2
)
5146 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5147 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5149 is_subset
= isl_map_is_subset(map1
, map2
);
5157 int isl_basic_map_is_equal(
5158 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5162 if (!bmap1
|| !bmap2
)
5164 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5167 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5171 int isl_basic_set_is_equal(
5172 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5174 return isl_basic_map_is_equal(
5175 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5178 int isl_map_is_empty(struct isl_map
*map
)
5185 for (i
= 0; i
< map
->n
; ++i
) {
5186 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5195 int isl_map_fast_is_empty(struct isl_map
*map
)
5197 return map
? map
->n
== 0 : -1;
5200 int isl_set_fast_is_empty(struct isl_set
*set
)
5202 return set
? set
->n
== 0 : -1;
5205 int isl_set_is_empty(struct isl_set
*set
)
5207 return isl_map_is_empty((struct isl_map
*)set
);
5210 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5216 is_subset
= isl_map_is_subset(map1
, map2
);
5219 is_subset
= isl_map_is_subset(map2
, map1
);
5223 int isl_basic_map_is_strict_subset(
5224 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5228 if (!bmap1
|| !bmap2
)
5230 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5233 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5234 if (is_subset
== -1)
5239 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5245 is_subset
= isl_map_is_subset(map1
, map2
);
5248 is_subset
= isl_map_is_subset(map2
, map1
);
5249 if (is_subset
== -1)
5254 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5256 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5259 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5263 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5266 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5270 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5273 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5278 return map
->n
== 1 && isl_basic_map_is_universe(map
->p
[0]);
5281 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5283 return isl_map_fast_is_universe((isl_map
*) set
);
5286 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
5288 struct isl_basic_set
*bset
= NULL
;
5289 struct isl_vec
*sample
= NULL
;
5296 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
5299 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
5300 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
5301 copy
= isl_basic_map_convex_hull(copy
);
5302 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
5303 isl_basic_map_free(copy
);
5307 total
= 1 + isl_basic_map_total_dim(bmap
);
5308 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
5309 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
5315 isl_vec_free(bmap
->sample
);
5316 bmap
->sample
= NULL
;
5317 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
5320 sample
= isl_basic_set_sample_vec(bset
);
5323 empty
= sample
->size
== 0;
5324 isl_vec_free(bmap
->sample
);
5325 bmap
->sample
= sample
;
5327 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
5332 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
5336 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
5339 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
5343 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
5346 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
5348 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
5351 struct isl_map
*isl_basic_map_union(
5352 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5354 struct isl_map
*map
;
5355 if (!bmap1
|| !bmap2
)
5358 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
5360 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
5363 map
= isl_map_add_basic_map(map
, bmap1
);
5364 map
= isl_map_add_basic_map(map
, bmap2
);
5367 isl_basic_map_free(bmap1
);
5368 isl_basic_map_free(bmap2
);
5372 struct isl_set
*isl_basic_set_union(
5373 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5375 return (struct isl_set
*)isl_basic_map_union(
5376 (struct isl_basic_map
*)bset1
,
5377 (struct isl_basic_map
*)bset2
);
5380 /* Order divs such that any div only depends on previous divs */
5381 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
5389 off
= isl_dim_total(bmap
->dim
);
5391 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5393 if (isl_int_is_zero(bmap
->div
[i
][0]))
5395 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
5399 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
5405 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
5407 return (struct isl_basic_set
*)
5408 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
5411 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
5418 for (i
= 0; i
< map
->n
; ++i
) {
5419 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
5430 /* Look for a div in dst that corresponds to the div "div" in src.
5431 * The divs before "div" in src and dst are assumed to be the same.
5433 * Returns -1 if no corresponding div was found and the position
5434 * of the corresponding div in dst otherwise.
5436 static int find_div(struct isl_basic_map
*dst
,
5437 struct isl_basic_map
*src
, unsigned div
)
5441 unsigned total
= isl_dim_total(src
->dim
);
5443 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
5444 for (i
= div
; i
< dst
->n_div
; ++i
)
5445 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
5446 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
5447 dst
->n_div
- div
) == -1)
5452 struct isl_basic_map
*isl_basic_map_align_divs(
5453 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
5456 unsigned total
= isl_dim_total(src
->dim
);
5461 if (src
->n_div
== 0)
5464 for (i
= 0; i
< src
->n_div
; ++i
)
5465 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
5467 src
= isl_basic_map_order_divs(src
);
5468 dst
= isl_basic_map_cow(dst
);
5469 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
5470 src
->n_div
, 0, 2 * src
->n_div
);
5473 for (i
= 0; i
< src
->n_div
; ++i
) {
5474 int j
= find_div(dst
, src
, i
);
5476 j
= isl_basic_map_alloc_div(dst
);
5479 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
5480 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
5481 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
5485 isl_basic_map_swap_div(dst
, i
, j
);
5489 isl_basic_map_free(dst
);
5493 struct isl_basic_set
*isl_basic_set_align_divs(
5494 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
5496 return (struct isl_basic_set
*)isl_basic_map_align_divs(
5497 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
5500 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
5508 map
= isl_map_compute_divs(map
);
5509 map
= isl_map_cow(map
);
5513 for (i
= 1; i
< map
->n
; ++i
)
5514 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
5515 for (i
= 1; i
< map
->n
; ++i
)
5516 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
5518 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5522 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
5524 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
5527 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
5531 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
5532 map
= isl_map_intersect_domain(map
, set
);
5533 set
= isl_map_range(map
);
5541 /* There is no need to cow as removing empty parts doesn't change
5542 * the meaning of the set.
5544 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
5551 for (i
= map
->n
-1; i
>= 0; --i
) {
5552 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
5554 isl_basic_map_free(map
->p
[i
]);
5555 if (i
!= map
->n
-1) {
5556 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5557 map
->p
[i
] = map
->p
[map
->n
-1];
5565 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
5567 return (struct isl_set
*)
5568 isl_map_remove_empty_parts((struct isl_map
*)set
);
5571 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
5573 struct isl_basic_map
*bmap
;
5574 if (!map
|| map
->n
== 0)
5576 bmap
= map
->p
[map
->n
-1];
5577 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
5578 return isl_basic_map_copy(bmap
);
5581 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
5583 return (struct isl_basic_set
*)
5584 isl_map_copy_basic_map((struct isl_map
*)set
);
5587 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
5588 __isl_keep isl_basic_map
*bmap
)
5594 for (i
= map
->n
-1; i
>= 0; --i
) {
5595 if (map
->p
[i
] != bmap
)
5597 map
= isl_map_cow(map
);
5600 isl_basic_map_free(map
->p
[i
]);
5601 if (i
!= map
->n
-1) {
5602 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
5603 map
->p
[i
] = map
->p
[map
->n
-1];
5614 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
5615 struct isl_basic_set
*bset
)
5617 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
5618 (struct isl_basic_map
*)bset
);
5621 /* Given two basic sets bset1 and bset2, compute the maximal difference
5622 * between the values of dimension pos in bset1 and those in bset2
5623 * for any common value of the parameters and dimensions preceding pos.
5625 static enum isl_lp_result
basic_set_maximal_difference_at(
5626 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
5627 int pos
, isl_int
*opt
)
5629 struct isl_dim
*dims
;
5630 struct isl_basic_map
*bmap1
= NULL
;
5631 struct isl_basic_map
*bmap2
= NULL
;
5632 struct isl_ctx
*ctx
;
5633 struct isl_vec
*obj
;
5636 unsigned dim1
, dim2
;
5637 enum isl_lp_result res
;
5639 if (!bset1
|| !bset2
)
5640 return isl_lp_error
;
5642 nparam
= isl_basic_set_n_param(bset1
);
5643 dim1
= isl_basic_set_n_dim(bset1
);
5644 dim2
= isl_basic_set_n_dim(bset2
);
5645 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
5646 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
5647 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
5648 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
5649 if (!bmap1
|| !bmap2
)
5651 bmap1
= isl_basic_map_cow(bmap1
);
5652 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
5653 pos
, (dim1
- pos
) + (dim2
- pos
),
5654 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
5655 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
5658 total
= isl_basic_map_total_dim(bmap1
);
5660 obj
= isl_vec_alloc(ctx
, 1 + total
);
5661 isl_seq_clr(obj
->block
.data
, 1 + total
);
5662 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
5663 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
5666 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
5668 isl_basic_map_free(bmap1
);
5672 isl_basic_map_free(bmap1
);
5673 isl_basic_map_free(bmap2
);
5674 return isl_lp_error
;
5677 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5678 * for any common value of the parameters and dimensions preceding pos
5679 * in both basic sets, the values of dimension pos in bset1 are
5680 * smaller or larger than those in bset2.
5683 * 1 if bset1 follows bset2
5684 * -1 if bset1 precedes bset2
5685 * 0 if bset1 and bset2 are incomparable
5686 * -2 if some error occurred.
5688 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
5689 struct isl_basic_set
*bset2
, int pos
)
5692 enum isl_lp_result res
;
5697 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5699 if (res
== isl_lp_empty
)
5701 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5702 res
== isl_lp_unbounded
)
5704 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5713 /* Given two basic sets bset1 and bset2, check whether
5714 * for any common value of the parameters and dimensions preceding pos
5715 * there is a value of dimension pos in bset1 that is larger
5716 * than a value of the same dimension in bset2.
5719 * 1 if there exists such a pair
5720 * 0 if there is no such pair, but there is a pair of equal values
5722 * -2 if some error occurred.
5724 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
5725 __isl_keep isl_basic_set
*bset2
, int pos
)
5728 enum isl_lp_result res
;
5733 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5735 if (res
== isl_lp_empty
)
5737 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5738 res
== isl_lp_unbounded
)
5740 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5742 else if (res
== isl_lp_ok
)
5751 /* Given two sets set1 and set2, check whether
5752 * for any common value of the parameters and dimensions preceding pos
5753 * there is a value of dimension pos in set1 that is larger
5754 * than a value of the same dimension in set2.
5757 * 1 if there exists such a pair
5758 * 0 if there is no such pair, but there is a pair of equal values
5760 * -2 if some error occurred.
5762 int isl_set_follows_at(__isl_keep isl_set
*set1
,
5763 __isl_keep isl_set
*set2
, int pos
)
5771 for (i
= 0; i
< set1
->n
; ++i
)
5772 for (j
= 0; j
< set2
->n
; ++j
) {
5774 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
5775 if (f
== 1 || f
== -2)
5784 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5785 unsigned pos
, isl_int
*val
)
5793 total
= isl_basic_map_total_dim(bmap
);
5794 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5795 for (; d
+1 > pos
; --d
)
5796 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5800 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5802 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5804 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5807 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5813 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5814 unsigned pos
, isl_int
*val
)
5826 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5829 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5830 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5831 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5832 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5836 isl_int_set(*val
, v
);
5842 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5843 unsigned pos
, isl_int
*val
)
5845 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
5849 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
5852 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
5855 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
5856 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5858 if (pos
>= isl_basic_map_dim(bmap
, type
))
5860 return isl_basic_map_fast_has_fixed_var(bmap
,
5861 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
5864 int isl_map_fast_is_fixed(struct isl_map
*map
,
5865 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5867 if (pos
>= isl_map_dim(map
, type
))
5869 return isl_map_fast_has_fixed_var(map
,
5870 map_offset(map
, type
) - 1 + pos
, val
);
5873 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5874 * then return this fixed value in *val.
5876 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
5879 return isl_basic_set_fast_has_fixed_var(bset
,
5880 isl_basic_set_n_param(bset
) + dim
, val
);
5883 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5884 * then return this fixed value in *val.
5886 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
5888 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
5891 /* Check if input variable in has fixed value and if so and if val is not NULL,
5892 * then return this fixed value in *val.
5894 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
5896 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
5899 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5900 * and if val is not NULL, then return this lower bound in *val.
5902 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
5903 unsigned dim
, isl_int
*val
)
5905 int i
, i_eq
= -1, i_ineq
= -1;
5912 total
= isl_basic_set_total_dim(bset
);
5913 nparam
= isl_basic_set_n_param(bset
);
5914 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5915 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
5921 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5922 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
5924 if (i_eq
!= -1 || i_ineq
!= -1)
5928 if (i_eq
== -1 && i_ineq
== -1)
5930 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
5931 /* The coefficient should always be one due to normalization. */
5932 if (!isl_int_is_one(c
[1+nparam
+dim
]))
5934 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
5936 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
5937 total
- nparam
- dim
- 1) != -1)
5940 isl_int_neg(*val
, c
[0]);
5944 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
5945 unsigned dim
, isl_int
*val
)
5957 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5961 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5963 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
5964 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
5966 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5970 isl_int_set(*val
, v
);
5981 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
5983 const struct constraint
*c1
= (const struct constraint
*)p1
;
5984 const struct constraint
*c2
= (const struct constraint
*)p2
;
5985 unsigned size
= isl_min(c1
->size
, c2
->size
);
5986 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
5989 static struct isl_basic_map
*isl_basic_map_sort_constraints(
5990 struct isl_basic_map
*bmap
)
5993 struct constraint
*c
;
5998 total
= isl_basic_map_total_dim(bmap
);
5999 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6002 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6004 c
[i
].c
= bmap
->ineq
[i
];
6006 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6007 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6008 bmap
->ineq
[i
] = c
[i
].c
;
6012 isl_basic_map_free(bmap
);
6016 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6017 __isl_take isl_basic_set
*bset
)
6019 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6020 (struct isl_basic_map
*)bset
);
6023 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6027 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6029 bmap
= isl_basic_map_convex_hull(bmap
);
6030 bmap
= isl_basic_map_sort_constraints(bmap
);
6031 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6035 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6037 return (struct isl_basic_set
*)isl_basic_map_normalize(
6038 (struct isl_basic_map
*)bset
);
6041 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
6042 const struct isl_basic_map
*bmap2
)
6049 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6050 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6051 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6052 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6053 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6054 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6055 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6056 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6058 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6060 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6062 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6063 return bmap1
->n_eq
- bmap2
->n_eq
;
6064 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6065 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6066 if (bmap1
->n_div
!= bmap2
->n_div
)
6067 return bmap1
->n_div
- bmap2
->n_div
;
6068 total
= isl_basic_map_total_dim(bmap1
);
6069 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6070 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6074 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6075 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6079 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6080 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6087 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
6088 struct isl_basic_map
*bmap2
)
6090 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6093 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6094 __isl_keep isl_basic_set
*bset2
)
6096 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6097 (isl_basic_map
*)bset2
);
6100 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6102 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6103 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6105 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6108 /* We normalize in place, but if anything goes wrong we need
6109 * to return NULL, so we need to make sure we don't change the
6110 * meaning of any possible other copies of map.
6112 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6115 struct isl_basic_map
*bmap
;
6119 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6121 for (i
= 0; i
< map
->n
; ++i
) {
6122 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6125 isl_basic_map_free(map
->p
[i
]);
6128 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6129 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6130 map
= isl_map_remove_empty_parts(map
);
6133 for (i
= map
->n
- 1; i
>= 1; --i
) {
6134 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6136 isl_basic_map_free(map
->p
[i
-1]);
6137 for (j
= i
; j
< map
->n
; ++j
)
6138 map
->p
[j
-1] = map
->p
[j
];
6148 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6150 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6153 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6163 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6166 map1
= isl_map_copy(map1
);
6167 map2
= isl_map_copy(map2
);
6168 map1
= isl_map_normalize(map1
);
6169 map2
= isl_map_normalize(map2
);
6172 equal
= map1
->n
== map2
->n
;
6173 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6174 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6187 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6189 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6190 (struct isl_map
*)set2
);
6193 /* Return an interval that ranges from min to max (inclusive)
6195 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6196 isl_int min
, isl_int max
)
6199 struct isl_basic_set
*bset
= NULL
;
6201 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6205 k
= isl_basic_set_alloc_inequality(bset
);
6208 isl_int_set_si(bset
->ineq
[k
][1], 1);
6209 isl_int_neg(bset
->ineq
[k
][0], min
);
6211 k
= isl_basic_set_alloc_inequality(bset
);
6214 isl_int_set_si(bset
->ineq
[k
][1], -1);
6215 isl_int_set(bset
->ineq
[k
][0], max
);
6219 isl_basic_set_free(bset
);
6223 /* Return the Cartesian product of the basic sets in list (in the given order).
6225 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6233 struct isl_basic_set
*product
= NULL
;
6237 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6238 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6239 nparam
= isl_basic_set_n_param(list
->p
[0]);
6240 dim
= isl_basic_set_n_dim(list
->p
[0]);
6241 extra
= list
->p
[0]->n_div
;
6242 n_eq
= list
->p
[0]->n_eq
;
6243 n_ineq
= list
->p
[0]->n_ineq
;
6244 for (i
= 1; i
< list
->n
; ++i
) {
6245 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6246 isl_assert(list
->ctx
,
6247 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6248 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6249 extra
+= list
->p
[i
]->n_div
;
6250 n_eq
+= list
->p
[i
]->n_eq
;
6251 n_ineq
+= list
->p
[i
]->n_ineq
;
6253 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6258 for (i
= 0; i
< list
->n
; ++i
) {
6259 isl_basic_set_add_constraints(product
,
6260 isl_basic_set_copy(list
->p
[i
]), dim
);
6261 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6263 isl_basic_set_list_free(list
);
6266 isl_basic_set_free(product
);
6267 isl_basic_set_list_free(list
);
6271 struct isl_basic_map
*isl_basic_map_product(
6272 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6274 struct isl_dim
*dim_result
= NULL
;
6275 struct isl_basic_map
*bmap
;
6276 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6277 struct isl_dim_map
*dim_map1
, *dim_map2
;
6279 if (!bmap1
|| !bmap2
)
6282 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6283 bmap2
->dim
, isl_dim_param
), goto error
);
6284 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6285 isl_dim_copy(bmap2
->dim
));
6287 in1
= isl_basic_map_n_in(bmap1
);
6288 in2
= isl_basic_map_n_in(bmap2
);
6289 out1
= isl_basic_map_n_out(bmap1
);
6290 out2
= isl_basic_map_n_out(bmap2
);
6291 nparam
= isl_basic_map_n_param(bmap1
);
6293 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
6294 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6295 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6296 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
6297 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
6298 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
6299 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
6300 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
6301 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
6302 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
6303 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
6305 bmap
= isl_basic_map_alloc_dim(dim_result
,
6306 bmap1
->n_div
+ bmap2
->n_div
,
6307 bmap1
->n_eq
+ bmap2
->n_eq
,
6308 bmap1
->n_ineq
+ bmap2
->n_ineq
);
6309 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
6310 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
6311 bmap
= isl_basic_map_simplify(bmap
);
6312 return isl_basic_map_finalize(bmap
);
6314 isl_basic_map_free(bmap1
);
6315 isl_basic_map_free(bmap2
);
6319 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6321 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
6324 struct isl_map
*result
;
6330 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
6331 map2
->dim
, isl_dim_param
), goto error
);
6333 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6334 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6335 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6337 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
6338 isl_dim_copy(map2
->dim
)),
6339 map1
->n
* map2
->n
, flags
);
6342 for (i
= 0; i
< map1
->n
; ++i
)
6343 for (j
= 0; j
< map2
->n
; ++j
) {
6344 struct isl_basic_map
*part
;
6345 part
= isl_basic_map_product(
6346 isl_basic_map_copy(map1
->p
[i
]),
6347 isl_basic_map_copy(map2
->p
[j
]));
6348 if (isl_basic_map_is_empty(part
))
6349 isl_basic_map_free(part
);
6351 result
= isl_map_add_basic_map(result
, part
);
6364 /* Given two set A and B, construct its Cartesian product A x B.
6366 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
6368 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
6369 (struct isl_map
*)set2
);
6372 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
6375 uint32_t hash
= isl_hash_init();
6380 bset
= isl_basic_set_copy(bset
);
6381 bset
= isl_basic_set_normalize(bset
);
6384 total
= isl_basic_set_total_dim(bset
);
6385 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
6386 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6388 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
6389 isl_hash_hash(hash
, c_hash
);
6391 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
6392 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6394 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
6395 isl_hash_hash(hash
, c_hash
);
6397 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
6398 for (i
= 0; i
< bset
->n_div
; ++i
) {
6400 if (isl_int_is_zero(bset
->div
[i
][0]))
6402 isl_hash_byte(hash
, i
& 0xFF);
6403 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
6404 isl_hash_hash(hash
, c_hash
);
6406 isl_basic_set_free(bset
);
6410 uint32_t isl_set_get_hash(struct isl_set
*set
)
6417 set
= isl_set_copy(set
);
6418 set
= isl_set_normalize(set
);
6422 hash
= isl_hash_init();
6423 for (i
= 0; i
< set
->n
; ++i
) {
6425 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
6426 isl_hash_hash(hash
, bset_hash
);
6434 /* Check if the value for dimension dim is completely determined
6435 * by the values of the other parameters and variables.
6436 * That is, check if dimension dim is involved in an equality.
6438 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
6445 nparam
= isl_basic_set_n_param(bset
);
6446 for (i
= 0; i
< bset
->n_eq
; ++i
)
6447 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
6452 /* Check if the value for dimension dim is completely determined
6453 * by the values of the other parameters and variables.
6454 * That is, check if dimension dim is involved in an equality
6455 * for each of the subsets.
6457 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
6463 for (i
= 0; i
< set
->n
; ++i
) {
6465 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
6472 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
6473 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
6480 for (i
= 0; i
< map
->n
; ++i
)
6481 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
6487 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
6488 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
6495 for (i
= 0; i
< set
->n
; ++i
)
6496 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
6502 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
6504 struct isl_dim
*dim
;
6509 if (bset
->n_div
== 0)
6512 bset
= isl_basic_set_cow(bset
);
6516 dim
= isl_basic_set_get_dim(bset
);
6517 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
6520 isl_dim_free(bset
->dim
);
6524 bset
= isl_basic_set_finalize(bset
);
6528 isl_basic_set_free(bset
);
6532 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
6535 struct isl_dim
*dim
;
6538 set
= isl_set_align_divs(set
);
6542 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
6545 set
= isl_set_cow(set
);
6549 n_div
= set
->p
[0]->n_div
;
6550 dim
= isl_set_get_dim(set
);
6551 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
6554 isl_dim_free(set
->dim
);
6557 for (i
= 0; i
< set
->n
; ++i
) {
6558 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
6569 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
6571 struct isl_dim
*dim
;
6572 struct isl_basic_map
*bmap
;
6579 set
= isl_set_align_divs(set
);
6584 dim
= isl_set_get_dim(set
);
6585 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
6587 return isl_map_identity(dim
);
6590 n_div
= set
->p
[0]->n_div
;
6591 dim
= isl_dim_map(dim
);
6592 n_param
= isl_dim_size(dim
, isl_dim_param
);
6593 n_set
= isl_dim_size(dim
, isl_dim_in
);
6594 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
6595 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
6596 for (i
= 0; i
< n_set
; ++i
)
6597 bmap
= var_equal(bmap
, i
);
6599 total
= n_param
+ n_set
+ n_set
+ n_div
;
6600 for (i
= 0; i
< n_div
; ++i
) {
6601 k
= isl_basic_map_alloc_inequality(bmap
);
6604 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
6605 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
6606 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
6607 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
6608 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
6609 set
->p
[0]->div
[i
][0]);
6611 l
= isl_basic_map_alloc_inequality(bmap
);
6614 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
6615 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
6616 set
->p
[0]->div
[i
][0]);
6617 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
6621 return isl_map_from_basic_map(bmap
);
6624 isl_basic_map_free(bmap
);
6628 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
6636 dim
= isl_basic_set_total_dim(bset
);
6637 size
+= bset
->n_eq
* (1 + dim
);
6638 size
+= bset
->n_ineq
* (1 + dim
);
6639 size
+= bset
->n_div
* (2 + dim
);
6644 int isl_set_size(__isl_keep isl_set
*set
)
6652 for (i
= 0; i
< set
->n
; ++i
)
6653 size
+= isl_basic_set_size(set
->p
[i
]);
6658 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
6659 enum isl_dim_type type
, unsigned pos
)
6667 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
6669 pos
+= isl_basic_map_offset(bmap
, type
);
6671 for (i
= 0; i
< bmap
->n_eq
; ++i
)
6672 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
6676 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6677 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
6684 return lower
&& upper
;
6687 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
6688 enum isl_dim_type type
, unsigned pos
)
6695 for (i
= 0; i
< map
->n
; ++i
) {
6697 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
6698 if (bounded
< 0 || !bounded
)
6705 /* Return 1 if the specified dim is involved in both an upper bound
6706 * and a lower bound.
6708 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
6709 enum isl_dim_type type
, unsigned pos
)
6711 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
6714 /* For each of the "n" variables starting at "first", determine
6715 * the sign of the variable and put the results in the first "n"
6716 * elements of the array "signs".
6718 * 1 means that the variable is non-negative
6719 * -1 means that the variable is non-positive
6720 * 0 means the variable attains both positive and negative values.
6722 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
6723 unsigned first
, unsigned n
, int *signs
)
6725 isl_vec
*bound
= NULL
;
6726 struct isl_tab
*tab
= NULL
;
6727 struct isl_tab_undo
*snap
;
6730 if (!bset
|| !signs
)
6733 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
6734 tab
= isl_tab_from_basic_set(bset
);
6738 isl_seq_clr(bound
->el
, bound
->size
);
6739 isl_int_set_si(bound
->el
[0], -1);
6741 snap
= isl_tab_snap(tab
);
6742 for (i
= 0; i
< n
; ++i
) {
6745 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
6746 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6749 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6750 if (isl_tab_rollback(tab
, snap
) < 0)
6758 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
6759 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6762 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6763 if (isl_tab_rollback(tab
, snap
) < 0)
6766 signs
[i
] = empty
? -1 : 0;
6770 isl_vec_free(bound
);
6774 isl_vec_free(bound
);
6778 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
6779 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
6781 if (!bset
|| !signs
)
6783 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
6786 first
+= pos(bset
->dim
, type
) - 1;
6787 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
6790 /* Check if the given map is single-valued.
6795 * and check if the result is a subset of the identity mapping.
6797 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
6803 test
= isl_map_reverse(isl_map_copy(map
));
6804 test
= isl_map_apply_range(test
, isl_map_copy(map
));
6806 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
6808 sv
= isl_map_is_subset(test
, id
);
6816 int isl_set_is_singleton(__isl_keep isl_set
*set
)
6818 return isl_map_is_single_valued((isl_map
*)set
);
6821 int isl_map_is_translation(__isl_keep isl_map
*map
)
6826 delta
= isl_map_deltas(isl_map_copy(map
));
6827 ok
= isl_set_is_singleton(delta
);
6828 isl_set_free(delta
);
6833 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
6835 if (isl_seq_first_non_zero(p
, pos
) != -1)
6837 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
6842 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
6851 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
6854 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
6855 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
6856 for (j
= 0; j
< nvar
; ++j
) {
6857 int lower
= 0, upper
= 0;
6858 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6859 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
6861 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
6867 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6868 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
6870 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
6872 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
6877 if (!lower
|| !upper
)
6884 int isl_set_is_box(__isl_keep isl_set
*set
)
6891 return isl_basic_set_is_box(set
->p
[0]);