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
15 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
38 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
41 struct isl_dim_map
*dim_map
;
42 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
43 sizeof(struct isl_dim_map
) + len
* sizeof(int));
46 dim_map
->len
= 1 + len
;
48 for (i
= 0; i
< len
; ++i
)
49 dim_map
->pos
[1 + i
] = -1;
53 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
56 case isl_dim_param
: return dim
->nparam
;
57 case isl_dim_in
: return dim
->n_in
;
58 case isl_dim_out
: return dim
->n_out
;
59 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
64 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return 1;
68 case isl_dim_in
: return 1 + dim
->nparam
;
69 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
74 static void isl_dim_map_dim_range(struct isl_dim_map
*dim_map
,
75 struct isl_dim
*dim
, enum isl_dim_type type
,
76 unsigned first
, unsigned n
, unsigned dst_pos
)
84 src_pos
= pos(dim
, type
);
85 for (i
= 0; i
< n
; ++i
)
86 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
89 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
90 enum isl_dim_type type
, unsigned dst_pos
)
92 isl_dim_map_dim_range(dim_map
, dim
, type
, 0, n(dim
, type
), dst_pos
);
95 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
96 struct isl_basic_map
*bmap
, unsigned dst_pos
)
101 if (!dim_map
|| !bmap
)
104 src_pos
= 1 + isl_dim_total(bmap
->dim
);
105 for (i
= 0; i
< bmap
->n_div
; ++i
)
106 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
109 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
113 for (i
= 0; i
< dim_map
->len
; ++i
)
114 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
115 fprintf(stderr
, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
119 enum isl_dim_type type
)
124 case isl_dim_cst
: return 1;
127 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
128 case isl_dim_div
: return bmap
->n_div
;
129 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
134 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
136 return map
? n(map
->dim
, type
) : 0;
139 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
141 return set
? n(set
->dim
, type
) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
145 enum isl_dim_type type
)
147 struct isl_dim
*dim
= bmap
->dim
;
149 case isl_dim_cst
: return 0;
150 case isl_dim_param
: return 1;
151 case isl_dim_in
: return 1 + dim
->nparam
;
152 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
153 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
158 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
160 return pos(map
->dim
, type
);
163 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
164 enum isl_dim_type type
)
166 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
171 return isl_basic_set_dim(bset
, isl_dim_set
);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
176 return isl_basic_set_dim(bset
, isl_dim_param
);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
181 return isl_dim_total(bset
->dim
) + bset
->n_div
;
184 unsigned isl_set_n_dim(const struct isl_set
*set
)
186 return isl_set_dim(set
, isl_dim_set
);
189 unsigned isl_set_n_param(const struct isl_set
*set
)
191 return isl_set_dim(set
, isl_dim_param
);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
196 return bmap
? bmap
->dim
->n_in
: 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
201 return bmap
? bmap
->dim
->n_out
: 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
206 return bmap
? bmap
->dim
->nparam
: 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
211 return bmap
? bmap
->n_div
: 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
216 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
219 unsigned isl_map_n_in(const struct isl_map
*map
)
221 return map
->dim
->n_in
;
224 unsigned isl_map_n_out(const struct isl_map
*map
)
226 return map
->dim
->n_out
;
229 unsigned isl_map_n_param(const struct isl_map
*map
)
231 return map
->dim
->nparam
;
234 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
239 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
242 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
245 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
246 struct isl_basic_set
*bset
)
251 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
254 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
257 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
258 struct isl_basic_set
*bset
)
263 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
266 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
269 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
271 return bmap
? bmap
->ctx
: NULL
;
274 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
276 return bset
? bset
->ctx
: NULL
;
279 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
281 return map
? map
->ctx
: NULL
;
284 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
286 return set
? set
->ctx
: NULL
;
289 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
293 return isl_dim_copy(bmap
->dim
);
296 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
300 return isl_dim_copy(bset
->dim
);
303 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
307 return isl_dim_copy(map
->dim
);
310 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
314 return isl_dim_copy(set
->dim
);
317 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
320 bmap
= isl_basic_map_cow(bmap
);
323 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
326 bmap
= isl_basic_map_finalize(bmap
);
329 isl_basic_map_free(bmap
);
333 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
334 enum isl_dim_type type
)
336 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
339 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
340 enum isl_dim_type type
, const char *s
)
344 map
= isl_map_cow(map
);
348 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
352 for (i
= 0; i
< map
->n
; ++i
) {
353 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
364 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
365 enum isl_dim_type type
)
367 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
370 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
373 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
376 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
378 return bset
? isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
381 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
383 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
386 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
387 enum isl_dim_type type
, unsigned pos
)
389 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
392 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
393 enum isl_dim_type type
, unsigned pos
)
395 return bset
? isl_dim_get_name(bset
->dim
, type
, pos
) : NULL
;
398 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
399 enum isl_dim_type type
, unsigned pos
)
401 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
404 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
405 enum isl_dim_type type
, unsigned pos
)
407 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
410 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
411 __isl_take isl_basic_map
*bmap
,
412 enum isl_dim_type type
, unsigned pos
, const char *s
)
416 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
421 isl_basic_map_free(bmap
);
425 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
426 enum isl_dim_type type
, unsigned pos
, const char *s
)
433 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
437 for (i
= 0; i
< map
->n
; ++i
) {
438 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
449 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
450 __isl_take isl_basic_set
*bset
,
451 enum isl_dim_type type
, unsigned pos
, const char *s
)
453 return (isl_basic_set
*)isl_basic_map_set_dim_name(
454 (isl_basic_map
*)bset
, type
, pos
, s
);
457 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
458 enum isl_dim_type type
, unsigned pos
, const char *s
)
460 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
463 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
467 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
470 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
472 return isl_basic_map_is_rational(bset
);
475 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
476 struct isl_basic_map
*bmap
, unsigned extra
,
477 unsigned n_eq
, unsigned n_ineq
)
480 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
485 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
486 if (isl_blk_is_error(bmap
->block
))
489 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
494 bmap
->block2
= isl_blk_empty();
497 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
498 if (isl_blk_is_error(bmap
->block2
))
501 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
506 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
507 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
509 for (i
= 0; i
< extra
; ++i
)
510 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
514 bmap
->c_size
= n_eq
+ n_ineq
;
515 bmap
->eq
= bmap
->ineq
+ n_ineq
;
524 isl_basic_map_free(bmap
);
528 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
529 unsigned nparam
, unsigned dim
, unsigned extra
,
530 unsigned n_eq
, unsigned n_ineq
)
532 struct isl_basic_map
*bmap
;
533 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
534 return (struct isl_basic_set
*)bmap
;
537 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
538 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
540 struct isl_basic_map
*bmap
;
543 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
544 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
545 return (struct isl_basic_set
*)bmap
;
551 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
552 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
554 struct isl_basic_map
*bmap
;
558 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
563 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
569 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
570 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
571 unsigned n_eq
, unsigned n_ineq
)
573 struct isl_basic_map
*bmap
;
576 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
580 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
584 static void dup_constraints(
585 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
588 unsigned total
= isl_basic_map_total_dim(src
);
590 for (i
= 0; i
< src
->n_eq
; ++i
) {
591 int j
= isl_basic_map_alloc_equality(dst
);
592 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
595 for (i
= 0; i
< src
->n_ineq
; ++i
) {
596 int j
= isl_basic_map_alloc_inequality(dst
);
597 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
600 for (i
= 0; i
< src
->n_div
; ++i
) {
601 int j
= isl_basic_map_alloc_div(dst
);
602 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
604 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
607 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
609 struct isl_basic_map
*dup
;
613 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
614 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
617 dup_constraints(dup
, bmap
);
618 dup
->flags
= bmap
->flags
;
619 dup
->sample
= isl_vec_copy(bmap
->sample
);
623 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
625 struct isl_basic_map
*dup
;
627 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
628 return (struct isl_basic_set
*)dup
;
631 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
636 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
640 return isl_basic_set_dup(bset
);
643 struct isl_set
*isl_set_copy(struct isl_set
*set
)
652 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
657 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
661 bmap
= isl_basic_map_dup(bmap
);
663 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
667 struct isl_map
*isl_map_copy(struct isl_map
*map
)
676 void isl_basic_map_free(struct isl_basic_map
*bmap
)
684 isl_ctx_deref(bmap
->ctx
);
686 isl_blk_free(bmap
->ctx
, bmap
->block2
);
688 isl_blk_free(bmap
->ctx
, bmap
->block
);
689 isl_vec_free(bmap
->sample
);
690 isl_dim_free(bmap
->dim
);
694 void isl_basic_set_free(struct isl_basic_set
*bset
)
696 isl_basic_map_free((struct isl_basic_map
*)bset
);
699 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
701 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
704 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
710 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
711 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
713 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
714 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
715 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
716 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
717 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
718 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
720 int j
= isl_basic_map_alloc_inequality(bmap
);
724 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
725 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
732 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
733 bmap
->extra
- bmap
->n_div
);
737 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
739 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
742 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
746 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
751 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
753 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
756 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
761 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
763 if (pos
!= bmap
->n_eq
- 1) {
765 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
766 bmap
->eq
[bmap
->n_eq
- 1] = t
;
772 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
774 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
777 void isl_basic_map_inequality_to_equality(
778 struct isl_basic_map
*bmap
, unsigned pos
)
783 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
784 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
789 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
790 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
791 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
792 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
795 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
797 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
800 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
806 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
807 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
808 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
809 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
810 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
811 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
812 1 + isl_basic_map_total_dim(bmap
),
813 bmap
->extra
- bmap
->n_div
);
814 return bmap
->n_ineq
++;
817 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
819 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
822 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
826 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
831 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
833 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
836 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
841 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
843 if (pos
!= bmap
->n_ineq
- 1) {
845 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
846 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
847 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
853 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
855 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
858 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
863 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
866 k
= isl_basic_map_alloc_equality(bmap
);
869 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
872 isl_basic_map_free(bmap
);
876 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
879 return (isl_basic_set
*)
880 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
883 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
888 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
891 k
= isl_basic_map_alloc_inequality(bmap
);
894 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
897 isl_basic_map_free(bmap
);
901 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
904 return (isl_basic_set
*)
905 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
908 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
912 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
913 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
914 1 + 1 + isl_basic_map_total_dim(bmap
),
915 bmap
->extra
- bmap
->n_div
);
916 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
917 return bmap
->n_div
++;
920 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
922 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
925 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
929 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
934 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
936 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
939 /* Copy constraint from src to dst, putting the vars of src at offset
940 * dim_off in dst and the divs of src at offset div_off in dst.
941 * If both sets are actually map, then dim_off applies to the input
944 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
945 struct isl_basic_map
*src_map
, isl_int
*src
,
946 unsigned in_off
, unsigned out_off
, unsigned div_off
)
948 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
949 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
950 unsigned src_in
= isl_basic_map_n_in(src_map
);
951 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
952 unsigned src_out
= isl_basic_map_n_out(src_map
);
953 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
954 isl_int_set(dst
[0], src
[0]);
955 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
956 if (dst_nparam
> src_nparam
)
957 isl_seq_clr(dst
+1+src_nparam
,
958 dst_nparam
- src_nparam
);
959 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
960 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
962 isl_min(dst_in
-in_off
, src_in
));
963 if (dst_in
-in_off
> src_in
)
964 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
965 dst_in
- in_off
- src_in
);
966 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
967 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
968 src
+1+src_nparam
+src_in
,
969 isl_min(dst_out
-out_off
, src_out
));
970 if (dst_out
-out_off
> src_out
)
971 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
972 dst_out
- out_off
- src_out
);
973 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
974 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
975 src
+1+src_nparam
+src_in
+src_out
,
976 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
977 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
978 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
979 div_off
+src_map
->n_div
,
980 dst_map
->n_div
- div_off
- src_map
->n_div
);
983 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
984 struct isl_basic_map
*src_map
, isl_int
*src
,
985 unsigned in_off
, unsigned out_off
, unsigned div_off
)
987 isl_int_set(dst
[0], src
[0]);
988 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
991 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
992 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
997 if (!bmap1
|| !bmap2
)
1000 div_off
= bmap1
->n_div
;
1002 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1003 int i1
= isl_basic_map_alloc_equality(bmap1
);
1006 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1007 i_pos
, o_pos
, div_off
);
1010 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1011 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1014 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1015 i_pos
, o_pos
, div_off
);
1018 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1019 int i1
= isl_basic_map_alloc_div(bmap1
);
1022 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1023 i_pos
, o_pos
, div_off
);
1026 isl_basic_map_free(bmap2
);
1031 isl_basic_map_free(bmap1
);
1032 isl_basic_map_free(bmap2
);
1036 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
1037 struct isl_dim_map
*dim_map
)
1041 for (i
= 0; i
< dim_map
->len
; ++i
) {
1042 if (dim_map
->pos
[i
] < 0)
1043 isl_int_set_si(dst
[i
], 0);
1045 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
1049 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
1050 struct isl_dim_map
*dim_map
)
1052 isl_int_set(dst
[0], src
[0]);
1053 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
1056 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
1057 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
1061 if (!src
|| !dst
|| !dim_map
)
1064 for (i
= 0; i
< src
->n_eq
; ++i
) {
1065 int i1
= isl_basic_map_alloc_equality(dst
);
1068 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
1071 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1072 int i1
= isl_basic_map_alloc_inequality(dst
);
1075 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1078 for (i
= 0; i
< src
->n_div
; ++i
) {
1079 int i1
= isl_basic_map_alloc_div(dst
);
1082 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1086 isl_basic_map_free(src
);
1091 isl_basic_map_free(src
);
1092 isl_basic_map_free(dst
);
1096 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1097 struct isl_basic_set
*bset2
, unsigned pos
)
1099 return (struct isl_basic_set
*)
1100 add_constraints((struct isl_basic_map
*)bset1
,
1101 (struct isl_basic_map
*)bset2
, 0, pos
);
1104 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1105 struct isl_dim
*dim
, unsigned extra
,
1106 unsigned n_eq
, unsigned n_ineq
)
1108 struct isl_basic_map
*ext
;
1118 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1119 base
->extra
>= base
->n_div
+ extra
;
1121 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1122 room_for_ineq(base
, n_ineq
)) {
1127 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1128 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1129 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1130 extra
+= base
->extra
;
1132 n_ineq
+= base
->n_ineq
;
1134 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1140 ext
->sample
= isl_vec_copy(base
->sample
);
1141 flags
= base
->flags
;
1142 ext
= add_constraints(ext
, base
, 0, 0);
1145 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1152 isl_basic_map_free(base
);
1156 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1157 struct isl_dim
*dim
, unsigned extra
,
1158 unsigned n_eq
, unsigned n_ineq
)
1160 return (struct isl_basic_set
*)
1161 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1162 extra
, n_eq
, n_ineq
);
1165 struct isl_basic_map
*isl_basic_map_extend_constraints(
1166 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1170 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1174 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1175 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1176 unsigned n_eq
, unsigned n_ineq
)
1178 struct isl_basic_map
*bmap
;
1179 struct isl_dim
*dim
;
1183 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1187 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1190 isl_basic_map_free(base
);
1194 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1195 unsigned nparam
, unsigned dim
, unsigned extra
,
1196 unsigned n_eq
, unsigned n_ineq
)
1198 return (struct isl_basic_set
*)
1199 isl_basic_map_extend((struct isl_basic_map
*)base
,
1200 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1203 struct isl_basic_set
*isl_basic_set_extend_constraints(
1204 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1206 return (struct isl_basic_set
*)
1207 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1211 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1213 return (struct isl_basic_set
*)
1214 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1217 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1222 if (bmap
->ref
> 1) {
1224 bmap
= isl_basic_map_dup(bmap
);
1227 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1231 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1239 return isl_set_dup(set
);
1242 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1250 return isl_map_dup(map
);
1253 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1254 unsigned a_len
, unsigned b_len
)
1256 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1257 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1258 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1261 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1262 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1270 isl_assert(bmap
->ctx
,
1271 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1273 if (n1
== 0 || n2
== 0)
1276 bmap
= isl_basic_map_cow(bmap
);
1280 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1281 if (isl_blk_is_error(blk
))
1284 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1286 bmap
->eq
[i
] + pos
, n1
, n2
);
1288 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1290 bmap
->ineq
[i
] + pos
, n1
, n2
);
1292 for (i
= 0; i
< bmap
->n_div
; ++i
)
1294 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1296 isl_blk_free(bmap
->ctx
, blk
);
1298 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1299 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1300 return isl_basic_map_finalize(bmap
);
1302 isl_basic_map_free(bmap
);
1306 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1307 __isl_take isl_basic_set
*bset
, unsigned n
)
1312 nparam
= isl_basic_set_n_param(bset
);
1313 dim
= isl_basic_set_n_dim(bset
);
1314 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1316 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1318 isl_basic_set_free(bset
);
1322 static __isl_give isl_set
*isl_set_swap_vars(__isl_take isl_set
*set
,
1326 set
= isl_set_cow(set
);
1330 for (i
= 0; i
< set
->n
; ++i
) {
1331 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1337 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1341 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1347 total
= isl_basic_map_total_dim(bmap
);
1348 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1349 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1351 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1353 i
= isl_basic_map_alloc_equality(bmap
);
1357 isl_int_set_si(bmap
->eq
[i
][0], 1);
1358 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1359 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1360 isl_vec_free(bmap
->sample
);
1361 bmap
->sample
= NULL
;
1362 return isl_basic_map_finalize(bmap
);
1364 isl_basic_map_free(bmap
);
1368 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1370 return (struct isl_basic_set
*)
1371 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1374 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1377 unsigned off
= isl_dim_total(bmap
->dim
);
1378 isl_int
*t
= bmap
->div
[a
];
1379 bmap
->div
[a
] = bmap
->div
[b
];
1382 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1383 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1385 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1386 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1388 for (i
= 0; i
< bmap
->n_div
; ++i
)
1389 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1390 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1397 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1398 enum isl_dim_type type
, unsigned first
, unsigned n
)
1407 map
= isl_map_cow(map
);
1410 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1411 first
+= pos(map
->dim
, type
) - 1;
1413 for (i
= 0; i
< map
->n
; ++i
) {
1414 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1424 /* Eliminate the specified n dimensions starting at first from the
1425 * constraints using Fourier-Motzkin. The dimensions themselves
1428 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1429 enum isl_dim_type type
, unsigned first
, unsigned n
)
1431 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1434 /* Eliminate the specified n dimensions starting at first from the
1435 * constraints using Fourier-Motzkin. The dimensions themselves
1438 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1439 unsigned first
, unsigned n
)
1441 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1444 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1445 __isl_take isl_basic_map
*bmap
)
1447 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1452 return isl_basic_map_finalize(bmap
);
1455 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1456 __isl_take isl_basic_set
*bset
)
1458 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1459 (struct isl_basic_map
*)bset
);
1462 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1471 set
= isl_set_cow(set
);
1475 for (i
= 0; i
< set
->n
; ++i
) {
1476 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1486 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1487 enum isl_dim_type type
, unsigned first
, unsigned n
)
1491 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1495 bmap
= isl_basic_map_eliminate_vars(bmap
,
1496 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1499 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1501 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1504 isl_basic_map_free(bmap
);
1508 /* Return true if the definition of the given div (recursively) involves
1509 * any of the given variables.
1511 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1512 unsigned first
, unsigned n
)
1515 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1517 if (isl_int_is_zero(bmap
->div
[div
][0]))
1519 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1522 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1523 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1525 if (div_involves_vars(bmap
, i
, first
, n
))
1532 /* Remove all divs (recursively) involving any of the given dimensions
1533 * in their definitions.
1535 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1536 __isl_take isl_basic_map
*bmap
,
1537 enum isl_dim_type type
, unsigned first
, unsigned n
)
1543 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1545 first
+= isl_basic_map_offset(bmap
, type
);
1547 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1548 if (!div_involves_vars(bmap
, i
, first
, n
))
1550 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1555 isl_basic_map_free(bmap
);
1559 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1560 enum isl_dim_type type
, unsigned first
, unsigned n
)
1569 map
= isl_map_cow(map
);
1573 for (i
= 0; i
< map
->n
; ++i
) {
1574 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1585 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1586 enum isl_dim_type type
, unsigned first
, unsigned n
)
1588 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1592 /* Return true if the definition of the given div is unknown or depends
1595 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1598 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1600 if (isl_int_is_zero(bmap
->div
[div
][0]))
1603 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1604 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1606 if (div_is_unknown(bmap
, i
))
1613 /* Remove all divs that are unknown or defined in terms of unknown divs.
1615 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1616 __isl_take isl_basic_map
*bmap
)
1623 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1624 if (!div_is_unknown(bmap
, i
))
1626 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1631 isl_basic_map_free(bmap
);
1635 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1644 map
= isl_map_cow(map
);
1648 for (i
= 0; i
< map
->n
; ++i
) {
1649 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1659 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1661 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1664 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1665 __isl_take isl_basic_set
*bset
,
1666 enum isl_dim_type type
, unsigned first
, unsigned n
)
1668 return (isl_basic_set
*)
1669 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1672 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1673 enum isl_dim_type type
, unsigned first
, unsigned n
)
1680 map
= isl_map_cow(map
);
1683 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1685 for (i
= 0; i
< map
->n
; ++i
) {
1686 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1687 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1691 map
= isl_map_drop(map
, type
, first
, n
);
1698 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1699 enum isl_dim_type type
, unsigned first
, unsigned n
)
1701 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1704 /* Project out n inputs starting at first using Fourier-Motzkin */
1705 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1706 unsigned first
, unsigned n
)
1708 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1711 static void dump_term(struct isl_basic_map
*bmap
,
1712 isl_int c
, int pos
, FILE *out
)
1715 unsigned in
= isl_basic_map_n_in(bmap
);
1716 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1717 unsigned nparam
= isl_basic_map_n_param(bmap
);
1719 isl_int_print(out
, c
, 0);
1721 if (!isl_int_is_one(c
))
1722 isl_int_print(out
, c
, 0);
1723 if (pos
< 1 + nparam
) {
1724 name
= isl_dim_get_name(bmap
->dim
,
1725 isl_dim_param
, pos
- 1);
1727 fprintf(out
, "%s", name
);
1729 fprintf(out
, "p%d", pos
- 1);
1730 } else if (pos
< 1 + nparam
+ in
)
1731 fprintf(out
, "i%d", pos
- 1 - nparam
);
1732 else if (pos
< 1 + nparam
+ dim
)
1733 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1735 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1739 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1740 int sign
, FILE *out
)
1744 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1748 for (i
= 0, first
= 1; i
< len
; ++i
) {
1749 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1752 fprintf(out
, " + ");
1754 isl_int_abs(v
, c
[i
]);
1755 dump_term(bmap
, v
, i
, out
);
1762 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1763 const char *op
, FILE *out
, int indent
)
1767 fprintf(out
, "%*s", indent
, "");
1769 dump_constraint_sign(bmap
, c
, 1, out
);
1770 fprintf(out
, " %s ", op
);
1771 dump_constraint_sign(bmap
, c
, -1, out
);
1775 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1776 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1778 fprintf(out
, "%*s", indent
, "");
1779 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1784 static void dump_constraints(struct isl_basic_map
*bmap
,
1785 isl_int
**c
, unsigned n
,
1786 const char *op
, FILE *out
, int indent
)
1790 for (i
= 0; i
< n
; ++i
)
1791 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1794 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1798 unsigned total
= isl_basic_map_total_dim(bmap
);
1800 for (j
= 0; j
< 1 + total
; ++j
) {
1801 if (isl_int_is_zero(exp
[j
]))
1803 if (!first
&& isl_int_is_pos(exp
[j
]))
1805 dump_term(bmap
, exp
[j
], j
, out
);
1810 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1814 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1815 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1817 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1818 fprintf(out
, "%*s", indent
, "");
1819 fprintf(out
, "e%d = [(", i
);
1820 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1822 isl_int_print(out
, bmap
->div
[i
][0], 0);
1823 fprintf(out
, "]\n");
1827 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1828 FILE *out
, int indent
)
1831 fprintf(out
, "null basic set\n");
1835 fprintf(out
, "%*s", indent
, "");
1836 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1837 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1838 bset
->extra
, bset
->flags
);
1839 dump((struct isl_basic_map
*)bset
, out
, indent
);
1842 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1843 FILE *out
, int indent
)
1846 fprintf(out
, "null basic map\n");
1850 fprintf(out
, "%*s", indent
, "");
1851 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1852 "flags: %x, n_name: %d\n",
1854 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1855 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1856 dump(bmap
, out
, indent
);
1859 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1864 total
= isl_basic_map_total_dim(bmap
);
1865 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1866 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1867 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1868 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1872 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1874 struct isl_set
*set
;
1878 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1879 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1880 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1881 sizeof(struct isl_set
) +
1882 (n
- 1) * sizeof(struct isl_basic_set
*));
1886 set
->ctx
= dim
->ctx
;
1887 isl_ctx_ref(set
->ctx
);
1899 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1900 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1902 struct isl_set
*set
;
1903 struct isl_dim
*dims
;
1905 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1909 set
= isl_set_alloc_dim(dims
, n
, flags
);
1913 /* Make sure "map" has room for at least "n" more basic maps.
1915 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1918 struct isl_map
*grown
= NULL
;
1922 isl_assert(map
->ctx
, n
>= 0, goto error
);
1923 if (map
->n
+ n
<= map
->size
)
1925 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1928 for (i
= 0; i
< map
->n
; ++i
) {
1929 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1937 isl_map_free(grown
);
1942 /* Make sure "set" has room for at least "n" more basic sets.
1944 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1946 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1949 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1952 struct isl_set
*dup
;
1957 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1960 for (i
= 0; i
< set
->n
; ++i
)
1961 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1965 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1967 return isl_map_from_basic_map(bset
);
1970 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1972 struct isl_map
*map
;
1977 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1978 return isl_map_add_basic_map(map
, bmap
);
1981 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1982 __isl_take isl_basic_set
*bset
)
1984 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1985 (struct isl_basic_map
*)bset
);
1988 void isl_set_free(struct isl_set
*set
)
1998 isl_ctx_deref(set
->ctx
);
1999 for (i
= 0; i
< set
->n
; ++i
)
2000 isl_basic_set_free(set
->p
[i
]);
2001 isl_dim_free(set
->dim
);
2005 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2010 fprintf(out
, "null set\n");
2014 fprintf(out
, "%*s", indent
, "");
2015 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2016 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2018 for (i
= 0; i
< set
->n
; ++i
) {
2019 fprintf(out
, "%*s", indent
, "");
2020 fprintf(out
, "basic set %d:\n", i
);
2021 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2025 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2030 fprintf(out
, "null map\n");
2034 fprintf(out
, "%*s", indent
, "");
2035 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2036 "flags: %x, n_name: %d\n",
2037 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2038 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2039 for (i
= 0; i
< map
->n
; ++i
) {
2040 fprintf(out
, "%*s", indent
, "");
2041 fprintf(out
, "basic map %d:\n", i
);
2042 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2046 struct isl_basic_map
*isl_basic_map_intersect_domain(
2047 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2049 struct isl_basic_map
*bmap_domain
;
2054 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2055 bset
->dim
, isl_dim_param
), goto error
);
2057 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2058 isl_assert(bset
->ctx
,
2059 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2061 bmap
= isl_basic_map_cow(bmap
);
2064 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2065 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2066 bmap_domain
= isl_basic_map_from_domain(bset
);
2067 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2069 bmap
= isl_basic_map_simplify(bmap
);
2070 return isl_basic_map_finalize(bmap
);
2072 isl_basic_map_free(bmap
);
2073 isl_basic_set_free(bset
);
2077 struct isl_basic_map
*isl_basic_map_intersect_range(
2078 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2080 struct isl_basic_map
*bmap_range
;
2085 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2086 bset
->dim
, isl_dim_param
), goto error
);
2088 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2089 isl_assert(bset
->ctx
,
2090 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2092 bmap
= isl_basic_map_cow(bmap
);
2095 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2096 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2097 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2098 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2100 bmap
= isl_basic_map_simplify(bmap
);
2101 return isl_basic_map_finalize(bmap
);
2103 isl_basic_map_free(bmap
);
2104 isl_basic_set_free(bset
);
2108 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2114 total
= 1 + isl_basic_map_total_dim(bmap
);
2115 if (total
!= vec
->size
)
2120 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2121 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2122 if (!isl_int_is_zero(s
)) {
2128 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2129 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2130 if (isl_int_is_neg(s
)) {
2141 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2143 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2146 struct isl_basic_map
*isl_basic_map_intersect(
2147 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2149 struct isl_vec
*sample
= NULL
;
2151 if (!bmap1
|| !bmap2
)
2154 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2155 bmap2
->dim
, isl_dim_param
), goto error
);
2156 if (isl_dim_total(bmap1
->dim
) ==
2157 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2158 isl_dim_total(bmap2
->dim
) !=
2159 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2160 return isl_basic_map_intersect(bmap2
, bmap1
);
2162 if (isl_dim_total(bmap2
->dim
) !=
2163 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2164 isl_assert(bmap1
->ctx
,
2165 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2167 if (bmap1
->sample
&&
2168 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2169 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2170 sample
= isl_vec_copy(bmap1
->sample
);
2171 else if (bmap2
->sample
&&
2172 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2173 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2174 sample
= isl_vec_copy(bmap2
->sample
);
2176 bmap1
= isl_basic_map_cow(bmap1
);
2179 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2180 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2181 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2184 isl_vec_free(sample
);
2186 isl_vec_free(bmap1
->sample
);
2187 bmap1
->sample
= sample
;
2190 bmap1
= isl_basic_map_simplify(bmap1
);
2191 return isl_basic_map_finalize(bmap1
);
2194 isl_vec_free(sample
);
2195 isl_basic_map_free(bmap1
);
2196 isl_basic_map_free(bmap2
);
2200 struct isl_basic_set
*isl_basic_set_intersect(
2201 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2203 return (struct isl_basic_set
*)
2204 isl_basic_map_intersect(
2205 (struct isl_basic_map
*)bset1
,
2206 (struct isl_basic_map
*)bset2
);
2209 /* Special case of isl_map_intersect, where both map1 and map2
2210 * are convex, without any divs and such that either map1 or map2
2211 * contains a single constraint. This constraint is then simply
2212 * added to the other map.
2214 static __isl_give isl_map
*map_intersect_add_constraint(
2215 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2217 struct isl_basic_map
*bmap1
;
2218 struct isl_basic_map
*bmap2
;
2220 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2221 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2222 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2223 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2225 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2226 return isl_map_intersect(map2
, map1
);
2228 isl_assert(map2
->ctx
,
2229 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2231 map1
= isl_map_cow(map1
);
2234 if (isl_map_fast_is_empty(map1
)) {
2238 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2239 if (map2
->p
[0]->n_eq
== 1)
2240 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2242 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2243 map2
->p
[0]->ineq
[0]);
2245 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2246 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2250 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2251 isl_basic_map_free(map1
->p
[0]);
2264 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2267 struct isl_map
*result
;
2273 if (isl_map_fast_is_empty(map1
)) {
2277 if (isl_map_fast_is_empty(map2
)) {
2282 if (map1
->n
== 1 && map2
->n
== 1 &&
2283 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2284 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2285 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2286 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2287 return map_intersect_add_constraint(map1
, map2
);
2288 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2289 map2
->dim
, isl_dim_param
), goto error
);
2290 if (isl_dim_total(map1
->dim
) ==
2291 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2292 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2293 return isl_map_intersect(map2
, map1
);
2295 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2296 isl_assert(map1
->ctx
,
2297 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2299 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2300 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2301 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2303 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2304 map1
->n
* map2
->n
, flags
);
2307 for (i
= 0; i
< map1
->n
; ++i
)
2308 for (j
= 0; j
< map2
->n
; ++j
) {
2309 struct isl_basic_map
*part
;
2310 part
= isl_basic_map_intersect(
2311 isl_basic_map_copy(map1
->p
[i
]),
2312 isl_basic_map_copy(map2
->p
[j
]));
2313 if (isl_basic_map_is_empty(part
))
2314 isl_basic_map_free(part
);
2316 result
= isl_map_add_basic_map(result
, part
);
2329 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2331 return (struct isl_set
*)
2332 isl_map_intersect((struct isl_map
*)set1
,
2333 (struct isl_map
*)set2
);
2336 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2338 struct isl_dim
*dim
;
2339 struct isl_basic_set
*bset
;
2344 bmap
= isl_basic_map_cow(bmap
);
2347 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2348 in
= isl_basic_map_n_in(bmap
);
2349 bset
= isl_basic_set_from_basic_map(bmap
);
2350 bset
= isl_basic_set_swap_vars(bset
, in
);
2351 return isl_basic_map_from_basic_set(bset
, dim
);
2354 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2355 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2357 struct isl_dim
*res_dim
;
2358 struct isl_basic_map
*res
;
2359 struct isl_dim_map
*dim_map
;
2360 unsigned total
, off
;
2361 enum isl_dim_type t
;
2369 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2371 total
= isl_basic_map_total_dim(bmap
) + n
;
2372 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2374 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2376 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2378 unsigned size
= isl_basic_map_dim(bmap
, t
);
2379 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2381 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2382 pos
, size
- pos
, off
+ pos
+ n
);
2384 off
+= isl_dim_size(res_dim
, t
);
2386 isl_dim_map_div(dim_map
, bmap
, off
);
2388 res
= isl_basic_map_alloc_dim(res_dim
,
2389 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2390 if (isl_basic_map_is_rational(bmap
))
2391 res
= isl_basic_map_set_rational(res
);
2392 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2393 return isl_basic_map_finalize(res
);
2396 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2397 enum isl_dim_type type
, unsigned n
)
2401 return isl_basic_map_insert(bmap
, type
,
2402 isl_basic_map_dim(bmap
, type
), n
);
2405 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2406 enum isl_dim_type type
, unsigned n
)
2410 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2411 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2413 isl_basic_set_free(bset
);
2417 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2418 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2425 map
= isl_map_cow(map
);
2429 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2433 for (i
= 0; i
< map
->n
; ++i
) {
2434 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2445 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2446 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2448 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2451 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2452 enum isl_dim_type type
, unsigned n
)
2456 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2459 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2460 enum isl_dim_type type
, unsigned n
)
2464 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2465 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2471 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2472 __isl_take isl_basic_map
*bmap
,
2473 enum isl_dim_type dst_type
, unsigned dst_pos
,
2474 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2477 struct isl_dim_map
*dim_map
;
2478 struct isl_basic_map
*res
;
2479 enum isl_dim_type t
;
2480 unsigned total
, off
;
2487 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2490 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2493 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2495 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2496 pos(bmap
->dim
, src_type
) + src_pos
+
2497 ((src_type
< dst_type
) ? n
: 0)) {
2498 bmap
= isl_basic_map_cow(bmap
);
2502 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2503 src_type
, src_pos
, n
);
2507 bmap
= isl_basic_map_finalize(bmap
);
2512 total
= isl_basic_map_total_dim(bmap
);
2513 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2516 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2517 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2518 if (t
== dst_type
) {
2519 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2522 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2525 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2526 dst_pos
, size
- dst_pos
, off
);
2527 off
+= size
- dst_pos
;
2528 } else if (t
== src_type
) {
2529 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2532 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2533 src_pos
+ n
, size
- src_pos
- n
, off
);
2534 off
+= size
- src_pos
- n
;
2536 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2540 isl_dim_map_div(dim_map
, bmap
, off
);
2542 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2543 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2544 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2546 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2547 src_type
, src_pos
, n
);
2551 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2552 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2553 bmap
= isl_basic_map_finalize(bmap
);
2557 isl_basic_map_free(bmap
);
2561 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2562 enum isl_dim_type dst_type
, unsigned dst_pos
,
2563 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2565 return (isl_basic_set
*)isl_basic_map_move_dims(
2566 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2569 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2570 enum isl_dim_type dst_type
, unsigned dst_pos
,
2571 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2575 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2576 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2577 src_type
, src_pos
, n
);
2583 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2584 enum isl_dim_type dst_type
, unsigned dst_pos
,
2585 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2594 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2597 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2600 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2602 map
= isl_map_cow(map
);
2606 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2610 for (i
= 0; i
< map
->n
; ++i
) {
2611 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2613 src_type
, src_pos
, n
);
2624 /* Move the specified dimensions to the last columns right before
2625 * the divs. Don't change the dimension specification of bmap.
2626 * That's the responsibility of the caller.
2628 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2629 enum isl_dim_type type
, unsigned first
, unsigned n
)
2631 struct isl_dim_map
*dim_map
;
2632 struct isl_basic_map
*res
;
2633 enum isl_dim_type t
;
2634 unsigned total
, off
;
2638 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2641 total
= isl_basic_map_total_dim(bmap
);
2642 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2645 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2646 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2648 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2651 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2652 first
, n
, total
- bmap
->n_div
- n
);
2653 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2654 first
+ n
, size
- (first
+ n
), off
);
2655 off
+= size
- (first
+ n
);
2657 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2661 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2663 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2664 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2665 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2669 /* Turn the n dimensions of type type, starting at first
2670 * into existentially quantified variables.
2672 __isl_give isl_basic_map
*isl_basic_map_project_out(
2673 __isl_take isl_basic_map
*bmap
,
2674 enum isl_dim_type type
, unsigned first
, unsigned n
)
2687 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2688 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2690 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2693 bmap
= move_last(bmap
, type
, first
, n
);
2694 bmap
= isl_basic_map_cow(bmap
);
2698 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2699 old
= bmap
->block2
.data
;
2700 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2701 (bmap
->extra
+ n
) * (1 + row_size
));
2702 if (!bmap
->block2
.data
)
2704 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2707 for (i
= 0; i
< n
; ++i
) {
2708 new_div
[i
] = bmap
->block2
.data
+
2709 (bmap
->extra
+ i
) * (1 + row_size
);
2710 isl_seq_clr(new_div
[i
], 1 + row_size
);
2712 for (i
= 0; i
< bmap
->extra
; ++i
)
2713 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2715 bmap
->div
= new_div
;
2719 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2722 bmap
= isl_basic_map_simplify(bmap
);
2723 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2724 return isl_basic_map_finalize(bmap
);
2726 isl_basic_map_free(bmap
);
2730 /* Turn the n dimensions of type type, starting at first
2731 * into existentially quantified variables.
2733 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2734 enum isl_dim_type type
, unsigned first
, unsigned n
)
2736 return (isl_basic_set
*)isl_basic_map_project_out(
2737 (isl_basic_map
*)bset
, type
, first
, n
);
2740 /* Turn the n dimensions of type type, starting at first
2741 * into existentially quantified variables.
2743 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2744 enum isl_dim_type type
, unsigned first
, unsigned n
)
2754 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2756 map
= isl_map_cow(map
);
2760 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2764 for (i
= 0; i
< map
->n
; ++i
) {
2765 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2776 /* Turn the n dimensions of type type, starting at first
2777 * into existentially quantified variables.
2779 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2780 enum isl_dim_type type
, unsigned first
, unsigned n
)
2782 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2785 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2789 for (i
= 0; i
< n
; ++i
) {
2790 j
= isl_basic_map_alloc_div(bmap
);
2793 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2797 isl_basic_map_free(bmap
);
2801 struct isl_basic_map
*isl_basic_map_apply_range(
2802 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2804 struct isl_dim
*dim_result
= NULL
;
2805 struct isl_basic_map
*bmap
;
2806 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2807 struct isl_dim_map
*dim_map1
, *dim_map2
;
2809 if (!bmap1
|| !bmap2
)
2812 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2813 isl_dim_copy(bmap2
->dim
));
2815 n_in
= isl_basic_map_n_in(bmap1
);
2816 n_out
= isl_basic_map_n_out(bmap2
);
2817 n
= isl_basic_map_n_out(bmap1
);
2818 nparam
= isl_basic_map_n_param(bmap1
);
2820 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2821 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2822 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2823 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2824 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2825 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2826 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2827 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2828 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2829 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2830 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2832 bmap
= isl_basic_map_alloc_dim(dim_result
,
2833 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2834 bmap1
->n_eq
+ bmap2
->n_eq
,
2835 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2836 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2837 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2838 bmap
= add_divs(bmap
, n
);
2839 bmap
= isl_basic_map_simplify(bmap
);
2840 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2841 return isl_basic_map_finalize(bmap
);
2843 isl_basic_map_free(bmap1
);
2844 isl_basic_map_free(bmap2
);
2848 struct isl_basic_set
*isl_basic_set_apply(
2849 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2854 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2857 return (struct isl_basic_set
*)
2858 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2860 isl_basic_set_free(bset
);
2861 isl_basic_map_free(bmap
);
2865 struct isl_basic_map
*isl_basic_map_apply_domain(
2866 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2868 if (!bmap1
|| !bmap2
)
2871 isl_assert(bmap1
->ctx
,
2872 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2873 isl_assert(bmap1
->ctx
,
2874 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2877 bmap1
= isl_basic_map_reverse(bmap1
);
2878 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2879 return isl_basic_map_reverse(bmap1
);
2881 isl_basic_map_free(bmap1
);
2882 isl_basic_map_free(bmap2
);
2886 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2887 * A \cap B -> f(A) + f(B)
2889 struct isl_basic_map
*isl_basic_map_sum(
2890 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2892 unsigned n_in
, n_out
, nparam
, total
, pos
;
2893 struct isl_basic_map
*bmap
= NULL
;
2894 struct isl_dim_map
*dim_map1
, *dim_map2
;
2897 if (!bmap1
|| !bmap2
)
2900 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2903 nparam
= isl_basic_map_n_param(bmap1
);
2904 n_in
= isl_basic_map_n_in(bmap1
);
2905 n_out
= isl_basic_map_n_out(bmap1
);
2907 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2908 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2909 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2910 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2911 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2912 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2913 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2914 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2915 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2916 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2917 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2919 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2920 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2921 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2922 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2923 for (i
= 0; i
< n_out
; ++i
) {
2924 int j
= isl_basic_map_alloc_equality(bmap
);
2927 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2928 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2929 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2930 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2932 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2933 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2934 bmap
= add_divs(bmap
, 2 * n_out
);
2936 bmap
= isl_basic_map_simplify(bmap
);
2937 return isl_basic_map_finalize(bmap
);
2939 isl_basic_map_free(bmap
);
2940 isl_basic_map_free(bmap1
);
2941 isl_basic_map_free(bmap2
);
2945 /* Given two maps A -> f(A) and B -> g(B), construct a map
2946 * A \cap B -> f(A) + f(B)
2948 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2950 struct isl_map
*result
;
2956 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2958 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2959 map1
->n
* map2
->n
, 0);
2962 for (i
= 0; i
< map1
->n
; ++i
)
2963 for (j
= 0; j
< map2
->n
; ++j
) {
2964 struct isl_basic_map
*part
;
2965 part
= isl_basic_map_sum(
2966 isl_basic_map_copy(map1
->p
[i
]),
2967 isl_basic_map_copy(map2
->p
[j
]));
2968 if (isl_basic_map_is_empty(part
))
2969 isl_basic_map_free(part
);
2971 result
= isl_map_add_basic_map(result
, part
);
2984 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2985 __isl_take isl_set
*set2
)
2987 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2990 /* Given a basic map A -> f(A), construct A -> -f(A).
2992 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2997 bmap
= isl_basic_map_cow(bmap
);
3001 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3002 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3003 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3004 for (j
= 0; j
< n
; ++j
)
3005 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3006 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3007 for (j
= 0; j
< n
; ++j
)
3008 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3009 for (i
= 0; i
< bmap
->n_div
; ++i
)
3010 for (j
= 0; j
< n
; ++j
)
3011 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3012 return isl_basic_map_finalize(bmap
);
3015 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3017 return isl_basic_map_neg(bset
);
3020 /* Given a map A -> f(A), construct A -> -f(A).
3022 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3026 map
= isl_map_cow(map
);
3030 for (i
= 0; i
< map
->n
; ++i
) {
3031 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3042 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3044 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3047 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3048 * A -> floor(f(A)/d).
3050 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3053 unsigned n_in
, n_out
, nparam
, total
, pos
;
3054 struct isl_basic_map
*result
= NULL
;
3055 struct isl_dim_map
*dim_map
;
3061 nparam
= isl_basic_map_n_param(bmap
);
3062 n_in
= isl_basic_map_n_in(bmap
);
3063 n_out
= isl_basic_map_n_out(bmap
);
3065 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3066 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3067 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3068 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3069 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3070 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3072 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3073 bmap
->n_div
+ n_out
,
3074 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3075 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
3076 result
= add_divs(result
, n_out
);
3077 for (i
= 0; i
< n_out
; ++i
) {
3079 j
= isl_basic_map_alloc_inequality(result
);
3082 isl_seq_clr(result
->ineq
[j
], 1+total
);
3083 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3084 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3085 j
= isl_basic_map_alloc_inequality(result
);
3088 isl_seq_clr(result
->ineq
[j
], 1+total
);
3089 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3090 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3091 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3094 result
= isl_basic_map_simplify(result
);
3095 return isl_basic_map_finalize(result
);
3097 isl_basic_map_free(result
);
3101 /* Given a map A -> f(A) and an integer d, construct a map
3102 * A -> floor(f(A)/d).
3104 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3108 map
= isl_map_cow(map
);
3112 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3113 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3114 for (i
= 0; i
< map
->n
; ++i
) {
3115 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3126 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3132 i
= isl_basic_map_alloc_equality(bmap
);
3135 nparam
= isl_basic_map_n_param(bmap
);
3136 n_in
= isl_basic_map_n_in(bmap
);
3137 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3138 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3139 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3140 return isl_basic_map_finalize(bmap
);
3142 isl_basic_map_free(bmap
);
3146 /* Add a constraints to "bmap" expressing i_pos < o_pos
3148 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3154 i
= isl_basic_map_alloc_inequality(bmap
);
3157 nparam
= isl_basic_map_n_param(bmap
);
3158 n_in
= isl_basic_map_n_in(bmap
);
3159 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3160 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3161 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3162 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3163 return isl_basic_map_finalize(bmap
);
3165 isl_basic_map_free(bmap
);
3169 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3171 static __isl_give isl_basic_map
*var_less_or_equal(
3172 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3178 i
= isl_basic_map_alloc_inequality(bmap
);
3181 nparam
= isl_basic_map_n_param(bmap
);
3182 n_in
= isl_basic_map_n_in(bmap
);
3183 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3184 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3185 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3186 return isl_basic_map_finalize(bmap
);
3188 isl_basic_map_free(bmap
);
3192 /* Add a constraints to "bmap" expressing i_pos > o_pos
3194 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3200 i
= isl_basic_map_alloc_inequality(bmap
);
3203 nparam
= isl_basic_map_n_param(bmap
);
3204 n_in
= isl_basic_map_n_in(bmap
);
3205 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3206 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3207 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3208 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3209 return isl_basic_map_finalize(bmap
);
3211 isl_basic_map_free(bmap
);
3215 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3217 static __isl_give isl_basic_map
*var_more_or_equal(
3218 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3224 i
= isl_basic_map_alloc_inequality(bmap
);
3227 nparam
= isl_basic_map_n_param(bmap
);
3228 n_in
= isl_basic_map_n_in(bmap
);
3229 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3230 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3231 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3232 return isl_basic_map_finalize(bmap
);
3234 isl_basic_map_free(bmap
);
3238 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3241 struct isl_basic_map
*bmap
;
3242 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3245 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3246 bmap
= var_equal(bmap
, i
);
3247 return isl_basic_map_finalize(bmap
);
3250 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3252 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3255 struct isl_basic_map
*bmap
;
3256 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3259 for (i
= 0; i
< pos
&& bmap
; ++i
)
3260 bmap
= var_equal(bmap
, i
);
3262 bmap
= var_less(bmap
, pos
);
3263 return isl_basic_map_finalize(bmap
);
3266 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3268 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3269 __isl_take isl_dim
*dim
, unsigned pos
)
3272 isl_basic_map
*bmap
;
3274 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3275 for (i
= 0; i
< pos
; ++i
)
3276 bmap
= var_equal(bmap
, i
);
3277 bmap
= var_less_or_equal(bmap
, pos
);
3278 return isl_basic_map_finalize(bmap
);
3281 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3283 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3286 struct isl_basic_map
*bmap
;
3287 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3290 for (i
= 0; i
< pos
&& bmap
; ++i
)
3291 bmap
= var_equal(bmap
, i
);
3293 bmap
= var_more(bmap
, pos
);
3294 return isl_basic_map_finalize(bmap
);
3297 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3299 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3300 __isl_take isl_dim
*dim
, unsigned pos
)
3303 isl_basic_map
*bmap
;
3305 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3306 for (i
= 0; i
< pos
; ++i
)
3307 bmap
= var_equal(bmap
, i
);
3308 bmap
= var_more_or_equal(bmap
, pos
);
3309 return isl_basic_map_finalize(bmap
);
3312 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3313 unsigned n
, int equal
)
3315 struct isl_map
*map
;
3318 if (n
== 0 && equal
)
3319 return isl_map_universe(dims
);
3321 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3323 for (i
= 0; i
+ 1 < n
; ++i
)
3324 map
= isl_map_add_basic_map(map
,
3325 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3328 map
= isl_map_add_basic_map(map
,
3329 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3331 map
= isl_map_add_basic_map(map
,
3332 isl_basic_map_less_at(dims
, n
- 1));
3339 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3343 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3346 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3348 return map_lex_lte_first(dim
, n
, 0);
3351 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3353 return map_lex_lte_first(dim
, n
, 1);
3356 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3358 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3361 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3363 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3366 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3367 unsigned n
, int equal
)
3369 struct isl_map
*map
;
3372 if (n
== 0 && equal
)
3373 return isl_map_universe(dims
);
3375 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3377 for (i
= 0; i
+ 1 < n
; ++i
)
3378 map
= isl_map_add_basic_map(map
,
3379 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3382 map
= isl_map_add_basic_map(map
,
3383 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3385 map
= isl_map_add_basic_map(map
,
3386 isl_basic_map_more_at(dims
, n
- 1));
3393 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3397 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3400 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3402 return map_lex_gte_first(dim
, n
, 0);
3405 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3407 return map_lex_gte_first(dim
, n
, 1);
3410 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3412 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3415 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3417 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3420 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3421 __isl_take isl_set
*set2
)
3424 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3425 map
= isl_map_intersect_domain(map
, set1
);
3426 map
= isl_map_intersect_range(map
, set2
);
3430 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3431 __isl_take isl_set
*set2
)
3434 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3435 map
= isl_map_intersect_domain(map
, set1
);
3436 map
= isl_map_intersect_range(map
, set2
);
3440 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3441 __isl_take isl_set
*set2
)
3444 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3445 map
= isl_map_intersect_domain(map
, set1
);
3446 map
= isl_map_intersect_range(map
, set2
);
3450 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3451 __isl_take isl_set
*set2
)
3454 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3455 map
= isl_map_intersect_domain(map
, set1
);
3456 map
= isl_map_intersect_range(map
, set2
);
3460 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3461 __isl_take isl_map
*map2
)
3464 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3465 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3466 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3470 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3471 __isl_take isl_map
*map2
)
3474 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3475 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3476 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3480 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3481 __isl_take isl_map
*map2
)
3484 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3485 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3486 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3490 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3491 __isl_take isl_map
*map2
)
3494 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3495 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3496 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3500 struct isl_basic_map
*isl_basic_map_from_basic_set(
3501 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3503 struct isl_basic_map
*bmap
;
3505 bset
= isl_basic_set_cow(bset
);
3509 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3510 isl_dim_free(bset
->dim
);
3511 bmap
= (struct isl_basic_map
*) bset
;
3513 return isl_basic_map_finalize(bmap
);
3515 isl_basic_set_free(bset
);
3520 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3524 if (bmap
->dim
->n_in
== 0)
3525 return (struct isl_basic_set
*)bmap
;
3526 bmap
= isl_basic_map_cow(bmap
);
3529 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3532 bmap
= isl_basic_map_finalize(bmap
);
3533 return (struct isl_basic_set
*)bmap
;
3535 isl_basic_map_free(bmap
);
3539 /* For a div d = floor(f/m), add the constraints
3542 * -(f-(n-1)) + m d >= 0
3544 * Note that the second constraint is the negation of
3548 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3549 unsigned pos
, isl_int
*div
)
3552 unsigned total
= isl_basic_map_total_dim(bmap
);
3554 i
= isl_basic_map_alloc_inequality(bmap
);
3557 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3558 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3560 j
= isl_basic_map_alloc_inequality(bmap
);
3563 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3564 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3565 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3569 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3570 unsigned pos
, isl_int
*div
)
3572 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3576 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3578 unsigned total
= isl_basic_map_total_dim(bmap
);
3579 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3581 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3585 struct isl_basic_set
*isl_basic_map_underlying_set(
3586 struct isl_basic_map
*bmap
)
3590 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3592 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3593 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3594 return (struct isl_basic_set
*)bmap
;
3595 bmap
= isl_basic_map_cow(bmap
);
3598 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3601 bmap
->extra
-= bmap
->n_div
;
3603 bmap
= isl_basic_map_finalize(bmap
);
3604 return (struct isl_basic_set
*)bmap
;
3609 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3610 __isl_take isl_basic_set
*bset
)
3612 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3615 struct isl_basic_map
*isl_basic_map_overlying_set(
3616 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3618 struct isl_basic_map
*bmap
;
3619 struct isl_ctx
*ctx
;
3626 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3627 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3628 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3630 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3631 isl_basic_map_free(like
);
3632 return (struct isl_basic_map
*)bset
;
3634 bset
= isl_basic_set_cow(bset
);
3637 total
= bset
->dim
->n_out
+ bset
->extra
;
3638 bmap
= (struct isl_basic_map
*)bset
;
3639 isl_dim_free(bmap
->dim
);
3640 bmap
->dim
= isl_dim_copy(like
->dim
);
3643 bmap
->n_div
= like
->n_div
;
3644 bmap
->extra
+= like
->n_div
;
3647 ltotal
= total
- bmap
->extra
+ like
->extra
;
3650 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3651 bmap
->extra
* (1 + 1 + total
));
3652 if (isl_blk_is_error(bmap
->block2
))
3654 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3658 for (i
= 0; i
< bmap
->extra
; ++i
)
3659 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3660 for (i
= 0; i
< like
->n_div
; ++i
) {
3661 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3662 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3664 bmap
= isl_basic_map_extend_constraints(bmap
,
3665 0, 2 * like
->n_div
);
3666 for (i
= 0; i
< like
->n_div
; ++i
) {
3667 if (isl_int_is_zero(bmap
->div
[i
][0]))
3669 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3673 isl_basic_map_free(like
);
3674 bmap
= isl_basic_map_simplify(bmap
);
3675 bmap
= isl_basic_map_finalize(bmap
);
3678 isl_basic_map_free(like
);
3679 isl_basic_set_free(bset
);
3683 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3684 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3686 return (struct isl_basic_set
*)
3687 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3690 struct isl_set
*isl_set_from_underlying_set(
3691 struct isl_set
*set
, struct isl_basic_set
*like
)
3697 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3699 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3700 isl_basic_set_free(like
);
3703 set
= isl_set_cow(set
);
3706 for (i
= 0; i
< set
->n
; ++i
) {
3707 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3708 isl_basic_set_copy(like
));
3712 isl_dim_free(set
->dim
);
3713 set
->dim
= isl_dim_copy(like
->dim
);
3716 isl_basic_set_free(like
);
3719 isl_basic_set_free(like
);
3724 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3728 map
= isl_map_cow(map
);
3731 map
->dim
= isl_dim_cow(map
->dim
);
3735 for (i
= 1; i
< map
->n
; ++i
)
3736 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3738 for (i
= 0; i
< map
->n
; ++i
) {
3739 map
->p
[i
] = (struct isl_basic_map
*)
3740 isl_basic_map_underlying_set(map
->p
[i
]);
3745 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3747 isl_dim_free(map
->dim
);
3748 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3752 return (struct isl_set
*)map
;
3758 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3760 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3763 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3764 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3766 bmap
= isl_basic_map_cow(bmap
);
3770 isl_dim_free(bmap
->dim
);
3773 bmap
= isl_basic_map_finalize(bmap
);
3777 isl_basic_map_free(bmap
);
3782 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3783 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3785 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3789 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3790 __isl_take isl_dim
*dim
)
3794 map
= isl_map_cow(map
);
3798 for (i
= 0; i
< map
->n
; ++i
) {
3799 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3804 isl_dim_free(map
->dim
);
3814 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3815 __isl_take isl_dim
*dim
)
3817 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3820 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3823 struct isl_basic_set
*domain
;
3829 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3831 n_in
= isl_basic_map_n_in(bmap
);
3832 n_out
= isl_basic_map_n_out(bmap
);
3833 domain
= isl_basic_set_from_basic_map(bmap
);
3834 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3836 domain
= isl_basic_set_reset_dim(domain
, dim
);
3841 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3843 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3846 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3847 __isl_take isl_basic_map
*bmap
)
3851 isl_basic_map
*domain
;
3852 isl_basic_set
*bset
;
3853 int nparam
, n_in
, n_out
;
3856 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3857 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3858 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3860 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3861 domain
= isl_basic_map_universe(dim
);
3863 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3864 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3865 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3867 total
= isl_basic_map_total_dim(bmap
);
3869 for (i
= 0; i
< n_in
; ++i
) {
3870 k
= isl_basic_map_alloc_equality(bmap
);
3873 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3874 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3875 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3878 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3879 return isl_basic_map_finalize(bmap
);
3881 isl_basic_map_free(bmap
);
3885 __isl_give isl_basic_map
*isl_basic_map_range_map(
3886 __isl_take isl_basic_map
*bmap
)
3890 isl_basic_map
*range
;
3891 isl_basic_set
*bset
;
3892 int nparam
, n_in
, n_out
;
3895 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3896 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3897 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3899 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3900 range
= isl_basic_map_universe(dim
);
3902 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3903 bmap
= isl_basic_map_apply_range(bmap
, range
);
3904 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3906 total
= isl_basic_map_total_dim(bmap
);
3908 for (i
= 0; i
< n_out
; ++i
) {
3909 k
= isl_basic_map_alloc_equality(bmap
);
3912 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3913 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3914 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3917 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3918 return isl_basic_map_finalize(bmap
);
3920 isl_basic_map_free(bmap
);
3924 struct isl_set
*isl_map_range(struct isl_map
*map
)
3927 struct isl_set
*set
;
3931 if (isl_map_dim(map
, isl_dim_in
) == 0)
3932 return (isl_set
*)map
;
3934 map
= isl_map_cow(map
);
3938 set
= (struct isl_set
*) map
;
3939 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3942 for (i
= 0; i
< map
->n
; ++i
) {
3943 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3947 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3948 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3955 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3958 isl_dim
*domain_dim
;
3960 map
= isl_map_cow(map
);
3964 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3965 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3966 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3969 for (i
= 0; i
< map
->n
; ++i
) {
3970 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3974 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3975 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3982 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3987 map
= isl_map_cow(map
);
3991 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3992 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3993 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3996 for (i
= 0; i
< map
->n
; ++i
) {
3997 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4001 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4002 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4009 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
4012 struct isl_map
*map
= NULL
;
4014 set
= isl_set_cow(set
);
4017 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
4018 map
= (struct isl_map
*)set
;
4019 for (i
= 0; i
< set
->n
; ++i
) {
4020 map
->p
[i
] = isl_basic_map_from_basic_set(
4021 set
->p
[i
], isl_dim_copy(dim
));
4025 isl_dim_free(map
->dim
);
4034 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4035 __isl_take isl_basic_set
*bset
)
4037 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4040 __isl_give isl_basic_map
*isl_basic_map_from_range(
4041 __isl_take isl_basic_set
*bset
)
4043 return (isl_basic_map
*)bset
;
4046 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4048 return (struct isl_map
*)set
;
4051 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4053 return isl_map_reverse(isl_map_from_range(set
));
4056 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4057 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4059 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4060 isl_basic_map_from_range(range
));
4063 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4064 __isl_take isl_set
*range
)
4066 return isl_map_apply_range(isl_map_from_domain(domain
),
4067 isl_map_from_range(range
));
4070 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4073 struct isl_set
*set
= NULL
;
4077 map
= isl_map_cow(map
);
4080 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4083 set
= (struct isl_set
*)map
;
4084 for (i
= 0; i
< map
->n
; ++i
) {
4085 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4095 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4097 struct isl_map
*map
;
4101 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4102 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4103 sizeof(struct isl_map
) +
4104 (n
- 1) * sizeof(struct isl_basic_map
*));
4108 map
->ctx
= dim
->ctx
;
4109 isl_ctx_ref(map
->ctx
);
4121 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4122 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4125 struct isl_map
*map
;
4126 struct isl_dim
*dims
;
4128 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4132 map
= isl_map_alloc_dim(dims
, n
, flags
);
4136 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4138 struct isl_basic_map
*bmap
;
4139 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4140 bmap
= isl_basic_map_set_to_empty(bmap
);
4144 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4146 struct isl_basic_set
*bset
;
4147 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4148 bset
= isl_basic_set_set_to_empty(bset
);
4152 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4154 struct isl_basic_map
*bmap
;
4157 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4158 bmap
= isl_basic_map_set_to_empty(bmap
);
4162 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4164 struct isl_basic_map
*bmap
;
4167 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4168 bmap
= isl_basic_map_set_to_empty(bmap
);
4172 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4174 struct isl_basic_set
*bset
;
4177 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4178 bset
= isl_basic_set_set_to_empty(bset
);
4182 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4184 struct isl_basic_map
*bmap
;
4185 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4186 bmap
= isl_basic_map_finalize(bmap
);
4190 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4192 struct isl_basic_set
*bset
;
4193 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4194 bset
= isl_basic_set_finalize(bset
);
4198 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4201 unsigned total
= isl_dim_total(dim
);
4202 isl_basic_map
*bmap
;
4204 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4205 for (i
= 0; i
< total
; ++i
) {
4206 int k
= isl_basic_map_alloc_inequality(bmap
);
4209 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4210 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4214 isl_basic_map_free(bmap
);
4218 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4220 return isl_basic_map_nat_universe(dim
);
4223 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4225 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4228 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4230 return isl_map_nat_universe(dim
);
4233 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4234 __isl_keep isl_basic_map
*model
)
4238 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4241 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4245 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4248 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4249 __isl_keep isl_set
*model
)
4253 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4256 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4258 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4261 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4265 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4268 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4272 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4275 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4277 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4280 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4284 return isl_set_empty(isl_dim_copy(model
->dim
));
4287 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4289 struct isl_map
*map
;
4292 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4293 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4297 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4299 struct isl_set
*set
;
4302 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4303 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4307 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4311 return isl_set_universe(isl_dim_copy(model
->dim
));
4314 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4317 struct isl_map
*dup
;
4321 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4322 for (i
= 0; i
< map
->n
; ++i
)
4323 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4327 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4328 __isl_take isl_basic_map
*bmap
)
4332 if (isl_basic_map_fast_is_empty(bmap
)) {
4333 isl_basic_map_free(bmap
);
4336 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4337 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4338 map
->p
[map
->n
] = bmap
;
4340 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4346 isl_basic_map_free(bmap
);
4350 void isl_map_free(struct isl_map
*map
)
4360 isl_ctx_deref(map
->ctx
);
4361 for (i
= 0; i
< map
->n
; ++i
)
4362 isl_basic_map_free(map
->p
[i
]);
4363 isl_dim_free(map
->dim
);
4367 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4368 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4372 base
= isl_map_cow(base
);
4376 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4379 for (i
= 0; i
< base
->n
; ++i
) {
4380 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4381 isl_dim_copy(base
->dim
), 0, 0, 0);
4391 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4392 unsigned nparam
, unsigned dim
)
4394 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4398 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4399 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4403 bmap
= isl_basic_map_cow(bmap
);
4404 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4405 j
= isl_basic_map_alloc_equality(bmap
);
4408 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4409 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4410 isl_int_set_si(bmap
->eq
[j
][0], value
);
4411 bmap
= isl_basic_map_simplify(bmap
);
4412 return isl_basic_map_finalize(bmap
);
4414 isl_basic_map_free(bmap
);
4418 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4419 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4423 bmap
= isl_basic_map_cow(bmap
);
4424 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4425 j
= isl_basic_map_alloc_equality(bmap
);
4428 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4429 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4430 isl_int_set(bmap
->eq
[j
][0], value
);
4431 bmap
= isl_basic_map_simplify(bmap
);
4432 return isl_basic_map_finalize(bmap
);
4434 isl_basic_map_free(bmap
);
4438 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4439 enum isl_dim_type type
, unsigned pos
, int value
)
4443 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4444 return isl_basic_map_fix_pos_si(bmap
,
4445 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4447 isl_basic_map_free(bmap
);
4451 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4452 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4456 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4457 return isl_basic_map_fix_pos(bmap
,
4458 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4460 isl_basic_map_free(bmap
);
4464 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4465 enum isl_dim_type type
, unsigned pos
, int value
)
4467 return (struct isl_basic_set
*)
4468 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4472 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4473 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4475 return (struct isl_basic_set
*)
4476 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4480 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4481 unsigned input
, int value
)
4483 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4486 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4487 unsigned dim
, int value
)
4489 return (struct isl_basic_set
*)
4490 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4491 isl_dim_set
, dim
, value
);
4494 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4495 enum isl_dim_type type
, unsigned pos
, int value
)
4499 map
= isl_map_cow(map
);
4503 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4504 for (i
= 0; i
< map
->n
; ++i
) {
4505 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4509 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4516 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4517 enum isl_dim_type type
, unsigned pos
, int value
)
4519 return (struct isl_set
*)
4520 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4523 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4524 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4528 map
= isl_map_cow(map
);
4532 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4533 for (i
= 0; i
< map
->n
; ++i
) {
4534 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4538 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4545 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4546 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4548 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4551 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4552 unsigned input
, int value
)
4554 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4557 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4559 return (struct isl_set
*)
4560 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4563 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4564 __isl_take isl_basic_map
*bmap
,
4565 enum isl_dim_type type
, unsigned pos
, int value
)
4571 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4572 pos
+= isl_basic_map_offset(bmap
, type
);
4573 bmap
= isl_basic_map_cow(bmap
);
4574 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4575 j
= isl_basic_map_alloc_inequality(bmap
);
4578 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4579 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4580 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4581 bmap
= isl_basic_map_simplify(bmap
);
4582 return isl_basic_map_finalize(bmap
);
4584 isl_basic_map_free(bmap
);
4588 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4589 unsigned dim
, isl_int value
)
4593 bset
= isl_basic_set_cow(bset
);
4594 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4595 j
= isl_basic_set_alloc_inequality(bset
);
4598 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4599 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4600 isl_int_neg(bset
->ineq
[j
][0], value
);
4601 bset
= isl_basic_set_simplify(bset
);
4602 return isl_basic_set_finalize(bset
);
4604 isl_basic_set_free(bset
);
4608 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4609 enum isl_dim_type type
, unsigned pos
, int value
)
4613 map
= isl_map_cow(map
);
4617 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4618 for (i
= 0; i
< map
->n
; ++i
) {
4619 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4624 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4631 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4632 enum isl_dim_type type
, unsigned pos
, int value
)
4634 return (struct isl_set
*)
4635 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4638 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4643 set
= isl_set_cow(set
);
4647 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4648 for (i
= 0; i
< set
->n
; ++i
) {
4649 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4659 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4663 map
= isl_map_cow(map
);
4667 map
->dim
= isl_dim_reverse(map
->dim
);
4670 for (i
= 0; i
< map
->n
; ++i
) {
4671 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4675 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4682 static struct isl_map
*isl_basic_map_partial_lexopt(
4683 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4684 struct isl_set
**empty
, int max
)
4688 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4689 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4691 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4693 isl_basic_map_free(bmap
);
4694 isl_basic_set_free(dom
);
4700 struct isl_map
*isl_basic_map_partial_lexmax(
4701 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4702 struct isl_set
**empty
)
4704 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4707 struct isl_map
*isl_basic_map_partial_lexmin(
4708 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4709 struct isl_set
**empty
)
4711 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4714 struct isl_set
*isl_basic_set_partial_lexmin(
4715 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4716 struct isl_set
**empty
)
4718 return (struct isl_set
*)
4719 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4723 struct isl_set
*isl_basic_set_partial_lexmax(
4724 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4725 struct isl_set
**empty
)
4727 return (struct isl_set
*)
4728 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4732 /* Given a basic map "bmap", compute the lexicographically minimal
4733 * (or maximal) image element for each domain element in dom.
4734 * Set *empty to those elements in dom that do not have an image element.
4736 * We first make sure the basic sets in dom are disjoint and then
4737 * simply collect the results over each of the basic sets separately.
4738 * We could probably improve the efficiency a bit by moving the union
4739 * domain down into the parametric integer programming.
4741 static __isl_give isl_map
*basic_map_partial_lexopt(
4742 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4743 __isl_give isl_set
**empty
, int max
)
4746 struct isl_map
*res
;
4748 dom
= isl_set_make_disjoint(dom
);
4752 if (isl_set_fast_is_empty(dom
)) {
4753 res
= isl_map_empty_like_basic_map(bmap
);
4754 *empty
= isl_set_empty_like(dom
);
4756 isl_basic_map_free(bmap
);
4760 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4761 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4763 for (i
= 1; i
< dom
->n
; ++i
) {
4764 struct isl_map
*res_i
;
4765 struct isl_set
*empty_i
;
4767 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4768 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4770 res
= isl_map_union_disjoint(res
, res_i
);
4771 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4775 isl_basic_map_free(bmap
);
4780 isl_basic_map_free(bmap
);
4784 /* Given a map "map", compute the lexicographically minimal
4785 * (or maximal) image element for each domain element in dom.
4786 * Set *empty to those elements in dom that do not have an image element.
4788 * We first compute the lexicographically minimal or maximal element
4789 * in the first basic map. This results in a partial solution "res"
4790 * and a subset "todo" of dom that still need to be handled.
4791 * We then consider each of the remaining maps in "map" and successively
4792 * improve both "res" and "todo".
4794 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4795 * Assume we are computing the lexicographical maximum.
4796 * We first compute the lexicographically maximal element in basic map i.
4797 * This results in a partial solution res_i and a subset todo_i.
4798 * Then we combine these results with those obtain for the first k basic maps
4799 * to obtain a result that is valid for the first k+1 basic maps.
4800 * In particular, the set where there is no solution is the set where
4801 * there is no solution for the first k basic maps and also no solution
4802 * for the ith basic map, i.e.,
4804 * todo^i = todo^k * todo_i
4806 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4807 * solutions, arbitrarily breaking ties in favor of res^k.
4808 * That is, when res^k(a) >= res_i(a), we pick res^k and
4809 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4810 * the lexicographic order.)
4811 * In practice, we compute
4813 * res^k * (res_i . "<=")
4817 * res_i * (res^k . "<")
4819 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4820 * where only one of res^k and res_i provides a solution and we simply pick
4827 * Note that we only compute these intersections when dom(res^k) intersects
4828 * dom(res_i). Otherwise, the only effect of these intersections is to
4829 * potentially break up res^k and res_i into smaller pieces.
4830 * We want to avoid such splintering as much as possible.
4831 * In fact, an earlier implementation of this function would look for
4832 * better results in the domain of res^k and for extra results in todo^k,
4833 * but this would always result in a splintering according to todo^k,
4834 * even when the domain of basic map i is disjoint from the domains of
4835 * the previous basic maps.
4837 static __isl_give isl_map
*isl_map_partial_lexopt(
4838 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4839 __isl_give isl_set
**empty
, int max
)
4842 struct isl_map
*res
;
4843 struct isl_set
*todo
;
4848 if (isl_map_fast_is_empty(map
)) {
4856 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4857 isl_set_copy(dom
), &todo
, max
);
4859 for (i
= 1; i
< map
->n
; ++i
) {
4863 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4865 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4866 isl_set_copy(dom
), &todo_i
, max
);
4869 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4870 le
= isl_map_lex_le(dim
);
4872 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4873 le
= isl_map_lex_ge(dim
);
4875 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4876 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4877 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4878 le
= isl_map_intersect(le
, isl_map_copy(res
));
4880 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4881 res
= isl_map_intersect_domain(res
,
4882 isl_set_copy(todo_i
));
4883 res_i
= isl_map_intersect_domain(res_i
,
4884 isl_set_copy(todo
));
4887 res
= isl_map_union_disjoint(res
, res_i
);
4888 res
= isl_map_union_disjoint(res
, lt
);
4889 res
= isl_map_union_disjoint(res
, le
);
4891 todo
= isl_set_intersect(todo
, todo_i
);
4911 __isl_give isl_map
*isl_map_partial_lexmax(
4912 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4913 __isl_give isl_set
**empty
)
4915 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4918 __isl_give isl_map
*isl_map_partial_lexmin(
4919 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4920 __isl_give isl_set
**empty
)
4922 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4925 __isl_give isl_set
*isl_set_partial_lexmin(
4926 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4927 __isl_give isl_set
**empty
)
4929 return (struct isl_set
*)
4930 isl_map_partial_lexmin((struct isl_map
*)set
,
4934 __isl_give isl_set
*isl_set_partial_lexmax(
4935 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4936 __isl_give isl_set
**empty
)
4938 return (struct isl_set
*)
4939 isl_map_partial_lexmax((struct isl_map
*)set
,
4943 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4945 struct isl_basic_set
*dom
= NULL
;
4946 struct isl_dim
*dom_dim
;
4950 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4951 dom
= isl_basic_set_universe(dom_dim
);
4952 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4954 isl_basic_map_free(bmap
);
4958 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4960 return isl_basic_map_lexopt(bmap
, 0);
4963 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4965 return isl_basic_map_lexopt(bmap
, 1);
4968 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4970 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4973 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4975 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4978 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4980 struct isl_set
*dom
= NULL
;
4981 struct isl_dim
*dom_dim
;
4985 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4986 dom
= isl_set_universe(dom_dim
);
4987 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4993 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4995 return isl_map_lexopt(map
, 0);
4998 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5000 return isl_map_lexopt(map
, 1);
5003 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5005 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5008 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5010 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5013 /* Apply a preimage specified by "mat" on the parameters of "bset".
5014 * bset is assumed to have only parameters and divs.
5016 static struct isl_basic_set
*basic_set_parameter_preimage(
5017 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5024 bset
->dim
= isl_dim_cow(bset
->dim
);
5028 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5030 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5032 bset
->dim
->nparam
= 0;
5033 bset
->dim
->n_out
= nparam
;
5034 bset
= isl_basic_set_preimage(bset
, mat
);
5036 bset
->dim
->nparam
= bset
->dim
->n_out
;
5037 bset
->dim
->n_out
= 0;
5042 isl_basic_set_free(bset
);
5046 /* Apply a preimage specified by "mat" on the parameters of "set".
5047 * set is assumed to have only parameters and divs.
5049 static struct isl_set
*set_parameter_preimage(
5050 struct isl_set
*set
, struct isl_mat
*mat
)
5052 struct isl_dim
*dim
= NULL
;
5058 dim
= isl_dim_copy(set
->dim
);
5059 dim
= isl_dim_cow(dim
);
5063 nparam
= isl_set_dim(set
, isl_dim_param
);
5065 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5068 dim
->n_out
= nparam
;
5069 isl_set_reset_dim(set
, dim
);
5070 set
= isl_set_preimage(set
, mat
);
5073 dim
= isl_dim_copy(set
->dim
);
5074 dim
= isl_dim_cow(dim
);
5077 dim
->nparam
= dim
->n_out
;
5079 isl_set_reset_dim(set
, dim
);
5089 /* Intersect the basic set "bset" with the affine space specified by the
5090 * equalities in "eq".
5092 static struct isl_basic_set
*basic_set_append_equalities(
5093 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5101 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5106 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5107 for (i
= 0; i
< eq
->n_row
; ++i
) {
5108 k
= isl_basic_set_alloc_equality(bset
);
5111 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5112 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5116 bset
= isl_basic_set_gauss(bset
, NULL
);
5117 bset
= isl_basic_set_finalize(bset
);
5122 isl_basic_set_free(bset
);
5126 /* Intersect the set "set" with the affine space specified by the
5127 * equalities in "eq".
5129 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5137 for (i
= 0; i
< set
->n
; ++i
) {
5138 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5151 /* Project the given basic set onto its parameter domain, possibly introducing
5152 * new, explicit, existential variables in the constraints.
5153 * The input has parameters and (possibly implicit) existential variables.
5154 * The output has the same parameters, but only
5155 * explicit existentially quantified variables.
5157 * The actual projection is performed by pip, but pip doesn't seem
5158 * to like equalities very much, so we first remove the equalities
5159 * among the parameters by performing a variable compression on
5160 * the parameters. Afterward, an inverse transformation is performed
5161 * and the equalities among the parameters are inserted back in.
5163 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5167 struct isl_mat
*T
, *T2
;
5168 struct isl_set
*set
;
5169 unsigned nparam
, n_div
;
5171 bset
= isl_basic_set_cow(bset
);
5175 if (bset
->n_eq
== 0)
5176 return isl_basic_set_lexmin(bset
);
5178 isl_basic_set_gauss(bset
, NULL
);
5180 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5181 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5183 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5184 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5187 if (i
== bset
->n_eq
)
5188 return isl_basic_set_lexmin(bset
);
5190 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5192 eq
= isl_mat_cow(eq
);
5193 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5194 if (T
&& T
->n_col
== 0) {
5198 bset
= isl_basic_set_set_to_empty(bset
);
5199 return isl_set_from_basic_set(bset
);
5201 bset
= basic_set_parameter_preimage(bset
, T
);
5203 set
= isl_basic_set_lexmin(bset
);
5204 set
= set_parameter_preimage(set
, T2
);
5205 set
= set_append_equalities(set
, eq
);
5209 /* Compute an explicit representation for all the existentially
5210 * quantified variables.
5211 * The input and output dimensions are first turned into parameters.
5212 * compute_divs then returns a map with the same parameters and
5213 * no input or output dimensions and the dimension specification
5214 * is reset to that of the input.
5216 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5218 struct isl_basic_set
*bset
;
5219 struct isl_set
*set
;
5220 struct isl_map
*map
;
5221 struct isl_dim
*dim
, *orig_dim
= NULL
;
5226 bmap
= isl_basic_map_cow(bmap
);
5230 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5231 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5232 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5233 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5237 orig_dim
= bmap
->dim
;
5239 bset
= (struct isl_basic_set
*)bmap
;
5241 set
= parameter_compute_divs(bset
);
5242 map
= (struct isl_map
*)set
;
5243 map
= isl_map_reset_dim(map
, orig_dim
);
5247 isl_basic_map_free(bmap
);
5251 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5259 off
= isl_dim_total(bmap
->dim
);
5260 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5261 if (isl_int_is_zero(bmap
->div
[i
][0]))
5263 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5269 static int map_divs_known(__isl_keep isl_map
*map
)
5276 for (i
= 0; i
< map
->n
; ++i
) {
5277 int known
= basic_map_divs_known(map
->p
[i
]);
5285 /* If bmap contains any unknown divs, then compute explicit
5286 * expressions for them. However, this computation may be
5287 * quite expensive, so first try to remove divs that aren't
5290 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5294 struct isl_map
*map
;
5296 known
= basic_map_divs_known(bmap
);
5300 return isl_map_from_basic_map(bmap
);
5302 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5304 known
= basic_map_divs_known(bmap
);
5308 return isl_map_from_basic_map(bmap
);
5310 map
= compute_divs(bmap
);
5313 isl_basic_map_free(bmap
);
5317 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5321 struct isl_map
*res
;
5328 known
= map_divs_known(map
);
5336 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5337 for (i
= 1 ; i
< map
->n
; ++i
) {
5339 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5340 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5341 res
= isl_map_union_disjoint(res
, r2
);
5343 res
= isl_map_union(res
, r2
);
5350 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5352 return (struct isl_set
*)
5353 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5356 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5358 return (struct isl_set
*)
5359 isl_map_compute_divs((struct isl_map
*)set
);
5362 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5365 struct isl_set
*set
;
5370 map
= isl_map_cow(map
);
5374 set
= (struct isl_set
*)map
;
5375 set
->dim
= isl_dim_domain(set
->dim
);
5378 for (i
= 0; i
< map
->n
; ++i
) {
5379 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5383 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5384 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5391 struct isl_map
*isl_map_union_disjoint(
5392 struct isl_map
*map1
, struct isl_map
*map2
)
5396 struct isl_map
*map
= NULL
;
5410 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5412 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5413 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5414 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5416 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5417 map1
->n
+ map2
->n
, flags
);
5420 for (i
= 0; i
< map1
->n
; ++i
) {
5421 map
= isl_map_add_basic_map(map
,
5422 isl_basic_map_copy(map1
->p
[i
]));
5426 for (i
= 0; i
< map2
->n
; ++i
) {
5427 map
= isl_map_add_basic_map(map
,
5428 isl_basic_map_copy(map2
->p
[i
]));
5442 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5444 map1
= isl_map_union_disjoint(map1
, map2
);
5448 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5452 struct isl_set
*isl_set_union_disjoint(
5453 struct isl_set
*set1
, struct isl_set
*set2
)
5455 return (struct isl_set
*)
5456 isl_map_union_disjoint(
5457 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5460 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5462 return (struct isl_set
*)
5463 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5466 struct isl_map
*isl_map_intersect_range(
5467 struct isl_map
*map
, struct isl_set
*set
)
5470 struct isl_map
*result
;
5476 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5477 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5478 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5480 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5481 map
->n
* set
->n
, flags
);
5484 for (i
= 0; i
< map
->n
; ++i
)
5485 for (j
= 0; j
< set
->n
; ++j
) {
5486 result
= isl_map_add_basic_map(result
,
5487 isl_basic_map_intersect_range(
5488 isl_basic_map_copy(map
->p
[i
]),
5489 isl_basic_set_copy(set
->p
[j
])));
5502 struct isl_map
*isl_map_intersect_domain(
5503 struct isl_map
*map
, struct isl_set
*set
)
5505 return isl_map_reverse(
5506 isl_map_intersect_range(isl_map_reverse(map
), set
));
5509 struct isl_map
*isl_map_apply_domain(
5510 struct isl_map
*map1
, struct isl_map
*map2
)
5514 map1
= isl_map_reverse(map1
);
5515 map1
= isl_map_apply_range(map1
, map2
);
5516 return isl_map_reverse(map1
);
5523 struct isl_map
*isl_map_apply_range(
5524 struct isl_map
*map1
, struct isl_map
*map2
)
5526 struct isl_dim
*dim_result
;
5527 struct isl_map
*result
;
5533 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5534 isl_dim_copy(map2
->dim
));
5536 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5539 for (i
= 0; i
< map1
->n
; ++i
)
5540 for (j
= 0; j
< map2
->n
; ++j
) {
5541 result
= isl_map_add_basic_map(result
,
5542 isl_basic_map_apply_range(
5543 isl_basic_map_copy(map1
->p
[i
]),
5544 isl_basic_map_copy(map2
->p
[j
])));
5550 if (result
&& result
->n
<= 1)
5551 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5560 * returns range - domain
5562 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5564 isl_dim
*dims
, *target_dim
;
5565 struct isl_basic_set
*bset
;
5572 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5573 bmap
->dim
, isl_dim_out
),
5575 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5576 dim
= isl_basic_map_n_in(bmap
);
5577 nparam
= isl_basic_map_n_param(bmap
);
5578 bset
= isl_basic_set_from_basic_map(bmap
);
5579 bset
= isl_basic_set_cow(bset
);
5580 dims
= isl_basic_set_get_dim(bset
);
5581 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5582 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5583 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5584 for (i
= 0; i
< dim
; ++i
) {
5585 int j
= isl_basic_map_alloc_equality(
5586 (struct isl_basic_map
*)bset
);
5589 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5590 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5591 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5592 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5594 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5595 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5598 isl_basic_map_free(bmap
);
5603 * returns range - domain
5605 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5609 struct isl_set
*result
;
5614 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5615 map
->dim
, isl_dim_out
),
5617 dim
= isl_map_get_dim(map
);
5618 dim
= isl_dim_domain(dim
);
5619 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5622 for (i
= 0; i
< map
->n
; ++i
)
5623 result
= isl_set_add_basic_set(result
,
5624 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5633 * returns [domain -> range] -> range - domain
5635 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5636 __isl_take isl_basic_map
*bmap
)
5640 isl_basic_map
*domain
;
5641 isl_basic_set
*bset
;
5645 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5646 isl_die(bmap
->ctx
, isl_error_invalid
,
5647 "domain and range don't match", goto error
);
5649 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5650 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5652 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5653 domain
= isl_basic_map_universe(dim
);
5655 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5656 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5657 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5659 total
= isl_basic_map_total_dim(bmap
);
5661 for (i
= 0; i
< n
; ++i
) {
5662 k
= isl_basic_map_alloc_equality(bmap
);
5665 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5666 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5667 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5668 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5671 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5672 return isl_basic_map_finalize(bmap
);
5674 isl_basic_map_free(bmap
);
5679 * returns [domain -> range] -> range - domain
5681 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5684 isl_dim
*domain_dim
;
5689 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5690 isl_die(map
->ctx
, isl_error_invalid
,
5691 "domain and range don't match", goto error
);
5693 map
= isl_map_cow(map
);
5697 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5698 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5699 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5702 for (i
= 0; i
< map
->n
; ++i
) {
5703 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5707 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5714 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5716 struct isl_basic_map
*bmap
;
5724 nparam
= dims
->nparam
;
5726 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5730 for (i
= 0; i
< dim
; ++i
) {
5731 int j
= isl_basic_map_alloc_equality(bmap
);
5734 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5735 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5736 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5738 return isl_basic_map_finalize(bmap
);
5740 isl_basic_map_free(bmap
);
5744 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5748 if (dim
->n_in
!= dim
->n_out
)
5749 isl_die(dim
->ctx
, isl_error_invalid
,
5750 "number of input and output dimensions needs to be "
5751 "the same", goto error
);
5752 return basic_map_identity(dim
);
5758 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5760 if (!model
|| !model
->dim
)
5762 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5765 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5767 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5770 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5772 if (!model
|| !model
->dim
)
5774 return isl_map_identity(isl_dim_copy(model
->dim
));
5777 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5779 if (!model
|| !model
->dim
)
5781 return isl_map_identity(isl_dim_copy(model
->dim
));
5784 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5786 isl_dim
*dim
= isl_set_get_dim(set
);
5788 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5789 return isl_map_intersect_range(id
, set
);
5792 /* Construct a basic set with all set dimensions having only non-negative
5795 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5800 struct isl_basic_set
*bset
;
5804 nparam
= dims
->nparam
;
5806 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5809 for (i
= 0; i
< dim
; ++i
) {
5810 int k
= isl_basic_set_alloc_inequality(bset
);
5813 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5814 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5818 isl_basic_set_free(bset
);
5822 /* Construct the half-space x_pos >= 0.
5824 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5828 isl_basic_set
*nonneg
;
5830 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5831 k
= isl_basic_set_alloc_inequality(nonneg
);
5834 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5835 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5837 return isl_basic_set_finalize(nonneg
);
5839 isl_basic_set_free(nonneg
);
5843 /* Construct the half-space x_pos <= -1.
5845 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5850 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5851 k
= isl_basic_set_alloc_inequality(neg
);
5854 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5855 isl_int_set_si(neg
->ineq
[k
][0], -1);
5856 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5858 return isl_basic_set_finalize(neg
);
5860 isl_basic_set_free(neg
);
5864 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5865 enum isl_dim_type type
, unsigned first
, unsigned n
)
5868 isl_basic_set
*nonneg
;
5876 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5878 for (i
= 0; i
< n
; ++i
) {
5879 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5880 pos(set
->dim
, type
) + first
+ i
);
5881 neg
= neg_halfspace(isl_set_get_dim(set
),
5882 pos(set
->dim
, type
) + first
+ i
);
5884 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5893 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5894 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5901 if (isl_set_fast_is_empty(set
)) {
5906 return fn(set
, signs
, user
);
5909 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5911 half
= isl_set_intersect(half
, isl_set_copy(set
));
5912 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5916 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5918 half
= isl_set_intersect(half
, set
);
5919 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5925 /* Call "fn" on the intersections of "set" with each of the orthants
5926 * (except for obviously empty intersections). The orthant is identified
5927 * by the signs array, with each entry having value 1 or -1 according
5928 * to the sign of the corresponding variable.
5930 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5931 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5941 if (isl_set_fast_is_empty(set
))
5944 nparam
= isl_set_dim(set
, isl_dim_param
);
5945 nvar
= isl_set_dim(set
, isl_dim_set
);
5947 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5949 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5957 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5959 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5962 int isl_basic_map_is_subset(
5963 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5966 struct isl_map
*map1
;
5967 struct isl_map
*map2
;
5969 if (!bmap1
|| !bmap2
)
5972 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5973 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5975 is_subset
= isl_map_is_subset(map1
, map2
);
5983 int isl_basic_map_is_equal(
5984 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5988 if (!bmap1
|| !bmap2
)
5990 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5993 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5997 int isl_basic_set_is_equal(
5998 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6000 return isl_basic_map_is_equal(
6001 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6004 int isl_map_is_empty(struct isl_map
*map
)
6011 for (i
= 0; i
< map
->n
; ++i
) {
6012 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6021 int isl_map_fast_is_empty(struct isl_map
*map
)
6023 return map
? map
->n
== 0 : -1;
6026 int isl_set_fast_is_empty(struct isl_set
*set
)
6028 return set
? set
->n
== 0 : -1;
6031 int isl_set_is_empty(struct isl_set
*set
)
6033 return isl_map_is_empty((struct isl_map
*)set
);
6036 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6041 return isl_dim_equal(map1
->dim
, map2
->dim
);
6044 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6049 return isl_dim_equal(set1
->dim
, set2
->dim
);
6052 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6058 is_subset
= isl_map_is_subset(map1
, map2
);
6061 is_subset
= isl_map_is_subset(map2
, map1
);
6065 int isl_basic_map_is_strict_subset(
6066 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6070 if (!bmap1
|| !bmap2
)
6072 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6075 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6076 if (is_subset
== -1)
6081 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6087 is_subset
= isl_map_is_subset(map1
, map2
);
6090 is_subset
= isl_map_is_subset(map2
, map1
);
6091 if (is_subset
== -1)
6096 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6098 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6101 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6105 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6108 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6112 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6115 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
6122 for (i
= 0; i
< map
->n
; ++i
) {
6123 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6131 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6133 return isl_map_fast_is_universe((isl_map
*) set
);
6136 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6138 struct isl_basic_set
*bset
= NULL
;
6139 struct isl_vec
*sample
= NULL
;
6146 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6149 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6150 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6151 copy
= isl_basic_map_remove_redundancies(copy
);
6152 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6153 isl_basic_map_free(copy
);
6157 total
= 1 + isl_basic_map_total_dim(bmap
);
6158 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6159 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6165 isl_vec_free(bmap
->sample
);
6166 bmap
->sample
= NULL
;
6167 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6170 sample
= isl_basic_set_sample_vec(bset
);
6173 empty
= sample
->size
== 0;
6174 isl_vec_free(bmap
->sample
);
6175 bmap
->sample
= sample
;
6177 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6182 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6186 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6189 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6193 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6196 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6198 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6201 struct isl_map
*isl_basic_map_union(
6202 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6204 struct isl_map
*map
;
6205 if (!bmap1
|| !bmap2
)
6208 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6210 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6213 map
= isl_map_add_basic_map(map
, bmap1
);
6214 map
= isl_map_add_basic_map(map
, bmap2
);
6217 isl_basic_map_free(bmap1
);
6218 isl_basic_map_free(bmap2
);
6222 struct isl_set
*isl_basic_set_union(
6223 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6225 return (struct isl_set
*)isl_basic_map_union(
6226 (struct isl_basic_map
*)bset1
,
6227 (struct isl_basic_map
*)bset2
);
6230 /* Order divs such that any div only depends on previous divs */
6231 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6239 off
= isl_dim_total(bmap
->dim
);
6241 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6243 if (isl_int_is_zero(bmap
->div
[i
][0]))
6245 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6249 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6255 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6257 return (struct isl_basic_set
*)
6258 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6261 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6268 for (i
= 0; i
< map
->n
; ++i
) {
6269 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6280 /* Look for a div in dst that corresponds to the div "div" in src.
6281 * The divs before "div" in src and dst are assumed to be the same.
6283 * Returns -1 if no corresponding div was found and the position
6284 * of the corresponding div in dst otherwise.
6286 static int find_div(struct isl_basic_map
*dst
,
6287 struct isl_basic_map
*src
, unsigned div
)
6291 unsigned total
= isl_dim_total(src
->dim
);
6293 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6294 for (i
= div
; i
< dst
->n_div
; ++i
)
6295 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6296 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6297 dst
->n_div
- div
) == -1)
6302 struct isl_basic_map
*isl_basic_map_align_divs(
6303 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6306 unsigned total
= isl_dim_total(src
->dim
);
6311 if (src
->n_div
== 0)
6314 for (i
= 0; i
< src
->n_div
; ++i
)
6315 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6317 src
= isl_basic_map_order_divs(src
);
6318 dst
= isl_basic_map_cow(dst
);
6319 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6320 src
->n_div
, 0, 2 * src
->n_div
);
6323 for (i
= 0; i
< src
->n_div
; ++i
) {
6324 int j
= find_div(dst
, src
, i
);
6326 j
= isl_basic_map_alloc_div(dst
);
6329 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6330 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6331 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6335 isl_basic_map_swap_div(dst
, i
, j
);
6339 isl_basic_map_free(dst
);
6343 struct isl_basic_set
*isl_basic_set_align_divs(
6344 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6346 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6347 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6350 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6358 map
= isl_map_compute_divs(map
);
6359 map
= isl_map_cow(map
);
6363 for (i
= 1; i
< map
->n
; ++i
)
6364 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6365 for (i
= 1; i
< map
->n
; ++i
)
6366 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6368 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6372 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6374 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6377 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6381 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6382 map
= isl_map_intersect_domain(map
, set
);
6383 set
= isl_map_range(map
);
6391 /* There is no need to cow as removing empty parts doesn't change
6392 * the meaning of the set.
6394 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6401 for (i
= map
->n
-1; i
>= 0; --i
) {
6402 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6404 isl_basic_map_free(map
->p
[i
]);
6405 if (i
!= map
->n
-1) {
6406 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6407 map
->p
[i
] = map
->p
[map
->n
-1];
6415 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6417 return (struct isl_set
*)
6418 isl_map_remove_empty_parts((struct isl_map
*)set
);
6421 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6423 struct isl_basic_map
*bmap
;
6424 if (!map
|| map
->n
== 0)
6426 bmap
= map
->p
[map
->n
-1];
6427 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6428 return isl_basic_map_copy(bmap
);
6431 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6433 return (struct isl_basic_set
*)
6434 isl_map_copy_basic_map((struct isl_map
*)set
);
6437 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6438 __isl_keep isl_basic_map
*bmap
)
6444 for (i
= map
->n
-1; i
>= 0; --i
) {
6445 if (map
->p
[i
] != bmap
)
6447 map
= isl_map_cow(map
);
6450 isl_basic_map_free(map
->p
[i
]);
6451 if (i
!= map
->n
-1) {
6452 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6453 map
->p
[i
] = map
->p
[map
->n
-1];
6464 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6465 struct isl_basic_set
*bset
)
6467 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6468 (struct isl_basic_map
*)bset
);
6471 /* Given two basic sets bset1 and bset2, compute the maximal difference
6472 * between the values of dimension pos in bset1 and those in bset2
6473 * for any common value of the parameters and dimensions preceding pos.
6475 static enum isl_lp_result
basic_set_maximal_difference_at(
6476 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6477 int pos
, isl_int
*opt
)
6479 struct isl_dim
*dims
;
6480 struct isl_basic_map
*bmap1
= NULL
;
6481 struct isl_basic_map
*bmap2
= NULL
;
6482 struct isl_ctx
*ctx
;
6483 struct isl_vec
*obj
;
6486 unsigned dim1
, dim2
;
6487 enum isl_lp_result res
;
6489 if (!bset1
|| !bset2
)
6490 return isl_lp_error
;
6492 nparam
= isl_basic_set_n_param(bset1
);
6493 dim1
= isl_basic_set_n_dim(bset1
);
6494 dim2
= isl_basic_set_n_dim(bset2
);
6495 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6496 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6497 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6498 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6499 if (!bmap1
|| !bmap2
)
6501 bmap1
= isl_basic_map_cow(bmap1
);
6502 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6503 pos
, (dim1
- pos
) + (dim2
- pos
),
6504 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6505 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6508 total
= isl_basic_map_total_dim(bmap1
);
6510 obj
= isl_vec_alloc(ctx
, 1 + total
);
6511 isl_seq_clr(obj
->block
.data
, 1 + total
);
6512 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6513 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6516 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6518 isl_basic_map_free(bmap1
);
6522 isl_basic_map_free(bmap1
);
6523 isl_basic_map_free(bmap2
);
6524 return isl_lp_error
;
6527 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6528 * for any common value of the parameters and dimensions preceding pos
6529 * in both basic sets, the values of dimension pos in bset1 are
6530 * smaller or larger than those in bset2.
6533 * 1 if bset1 follows bset2
6534 * -1 if bset1 precedes bset2
6535 * 0 if bset1 and bset2 are incomparable
6536 * -2 if some error occurred.
6538 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6539 struct isl_basic_set
*bset2
, int pos
)
6542 enum isl_lp_result res
;
6547 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6549 if (res
== isl_lp_empty
)
6551 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6552 res
== isl_lp_unbounded
)
6554 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6563 /* Given two basic sets bset1 and bset2, check whether
6564 * for any common value of the parameters and dimensions preceding pos
6565 * there is a value of dimension pos in bset1 that is larger
6566 * than a value of the same dimension in bset2.
6569 * 1 if there exists such a pair
6570 * 0 if there is no such pair, but there is a pair of equal values
6572 * -2 if some error occurred.
6574 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6575 __isl_keep isl_basic_set
*bset2
, int pos
)
6578 enum isl_lp_result res
;
6583 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6585 if (res
== isl_lp_empty
)
6587 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6588 res
== isl_lp_unbounded
)
6590 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6592 else if (res
== isl_lp_ok
)
6601 /* Given two sets set1 and set2, check whether
6602 * for any common value of the parameters and dimensions preceding pos
6603 * there is a value of dimension pos in set1 that is larger
6604 * than a value of the same dimension in set2.
6607 * 1 if there exists such a pair
6608 * 0 if there is no such pair, but there is a pair of equal values
6610 * -2 if some error occurred.
6612 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6613 __isl_keep isl_set
*set2
, int pos
)
6621 for (i
= 0; i
< set1
->n
; ++i
)
6622 for (j
= 0; j
< set2
->n
; ++j
) {
6624 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6625 if (f
== 1 || f
== -2)
6634 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6635 unsigned pos
, isl_int
*val
)
6643 total
= isl_basic_map_total_dim(bmap
);
6644 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6645 for (; d
+1 > pos
; --d
)
6646 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6650 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6652 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6654 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6657 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6663 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6664 unsigned pos
, isl_int
*val
)
6676 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6679 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6680 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6681 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6682 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6686 isl_int_set(*val
, v
);
6692 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6693 unsigned pos
, isl_int
*val
)
6695 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6699 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6702 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6705 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6706 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6708 if (pos
>= isl_basic_map_dim(bmap
, type
))
6710 return isl_basic_map_fast_has_fixed_var(bmap
,
6711 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6714 int isl_map_fast_is_fixed(struct isl_map
*map
,
6715 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6717 if (pos
>= isl_map_dim(map
, type
))
6719 return isl_map_fast_has_fixed_var(map
,
6720 map_offset(map
, type
) - 1 + pos
, val
);
6723 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6724 * then return this fixed value in *val.
6726 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6729 return isl_basic_set_fast_has_fixed_var(bset
,
6730 isl_basic_set_n_param(bset
) + dim
, val
);
6733 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6734 * then return this fixed value in *val.
6736 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6738 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6741 /* Check if input variable in has fixed value and if so and if val is not NULL,
6742 * then return this fixed value in *val.
6744 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6746 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6749 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6750 * and if val is not NULL, then return this lower bound in *val.
6752 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6753 unsigned dim
, isl_int
*val
)
6755 int i
, i_eq
= -1, i_ineq
= -1;
6762 total
= isl_basic_set_total_dim(bset
);
6763 nparam
= isl_basic_set_n_param(bset
);
6764 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6765 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6771 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6772 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6774 if (i_eq
!= -1 || i_ineq
!= -1)
6778 if (i_eq
== -1 && i_ineq
== -1)
6780 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6781 /* The coefficient should always be one due to normalization. */
6782 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6784 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6786 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6787 total
- nparam
- dim
- 1) != -1)
6790 isl_int_neg(*val
, c
[0]);
6794 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6795 unsigned dim
, isl_int
*val
)
6807 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6811 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6813 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6814 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6816 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6820 isl_int_set(*val
, v
);
6831 /* uset_gist depends on constraints without existentially quantified
6832 * variables sorting first.
6834 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6836 const struct constraint
*c1
= (const struct constraint
*)p1
;
6837 const struct constraint
*c2
= (const struct constraint
*)p2
;
6839 unsigned size
= isl_min(c1
->size
, c2
->size
);
6841 l1
= isl_seq_last_non_zero(c1
->c
, size
);
6842 l2
= isl_seq_last_non_zero(c2
->c
, size
);
6847 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6850 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6851 struct isl_basic_map
*bmap
)
6854 struct constraint
*c
;
6859 total
= isl_basic_map_total_dim(bmap
);
6860 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6863 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6865 c
[i
].c
= bmap
->ineq
[i
];
6867 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6868 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6869 bmap
->ineq
[i
] = c
[i
].c
;
6873 isl_basic_map_free(bmap
);
6877 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6878 __isl_take isl_basic_set
*bset
)
6880 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6881 (struct isl_basic_map
*)bset
);
6884 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6888 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6890 bmap
= isl_basic_map_remove_redundancies(bmap
);
6891 bmap
= isl_basic_map_sort_constraints(bmap
);
6892 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6896 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6898 return (struct isl_basic_set
*)isl_basic_map_normalize(
6899 (struct isl_basic_map
*)bset
);
6902 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6903 const __isl_keep isl_basic_map
*bmap2
)
6910 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
6911 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
6912 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
6913 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6914 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6915 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6916 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6917 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6918 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6919 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6920 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6922 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6924 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6926 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6927 return bmap1
->n_eq
- bmap2
->n_eq
;
6928 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6929 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6930 if (bmap1
->n_div
!= bmap2
->n_div
)
6931 return bmap1
->n_div
- bmap2
->n_div
;
6932 total
= isl_basic_map_total_dim(bmap1
);
6933 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6934 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6938 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6939 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6943 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6944 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6951 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6952 __isl_keep isl_basic_map
*bmap2
)
6954 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6957 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6958 __isl_keep isl_basic_set
*bset2
)
6960 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6961 (isl_basic_map
*)bset2
);
6964 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6966 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6967 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6969 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6972 /* We normalize in place, but if anything goes wrong we need
6973 * to return NULL, so we need to make sure we don't change the
6974 * meaning of any possible other copies of map.
6976 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6979 struct isl_basic_map
*bmap
;
6983 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6985 for (i
= 0; i
< map
->n
; ++i
) {
6986 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6989 isl_basic_map_free(map
->p
[i
]);
6992 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6993 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6994 map
= isl_map_remove_empty_parts(map
);
6997 for (i
= map
->n
- 1; i
>= 1; --i
) {
6998 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
7000 isl_basic_map_free(map
->p
[i
-1]);
7001 for (j
= i
; j
< map
->n
; ++j
)
7002 map
->p
[j
-1] = map
->p
[j
];
7012 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7014 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7017 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
7027 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
7030 map1
= isl_map_copy(map1
);
7031 map2
= isl_map_copy(map2
);
7032 map1
= isl_map_normalize(map1
);
7033 map2
= isl_map_normalize(map2
);
7036 equal
= map1
->n
== map2
->n
;
7037 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7038 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
7051 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7053 return isl_map_fast_is_equal((struct isl_map
*)set1
,
7054 (struct isl_map
*)set2
);
7057 /* Return an interval that ranges from min to max (inclusive)
7059 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7060 isl_int min
, isl_int max
)
7063 struct isl_basic_set
*bset
= NULL
;
7065 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7069 k
= isl_basic_set_alloc_inequality(bset
);
7072 isl_int_set_si(bset
->ineq
[k
][1], 1);
7073 isl_int_neg(bset
->ineq
[k
][0], min
);
7075 k
= isl_basic_set_alloc_inequality(bset
);
7078 isl_int_set_si(bset
->ineq
[k
][1], -1);
7079 isl_int_set(bset
->ineq
[k
][0], max
);
7083 isl_basic_set_free(bset
);
7087 /* Return the Cartesian product of the basic sets in list (in the given order).
7089 __isl_give isl_basic_set
*isl_basic_set_list_product(
7090 __isl_take
struct isl_basic_set_list
*list
)
7098 struct isl_basic_set
*product
= NULL
;
7102 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7103 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7104 nparam
= isl_basic_set_n_param(list
->p
[0]);
7105 dim
= isl_basic_set_n_dim(list
->p
[0]);
7106 extra
= list
->p
[0]->n_div
;
7107 n_eq
= list
->p
[0]->n_eq
;
7108 n_ineq
= list
->p
[0]->n_ineq
;
7109 for (i
= 1; i
< list
->n
; ++i
) {
7110 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7111 isl_assert(list
->ctx
,
7112 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7113 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7114 extra
+= list
->p
[i
]->n_div
;
7115 n_eq
+= list
->p
[i
]->n_eq
;
7116 n_ineq
+= list
->p
[i
]->n_ineq
;
7118 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7123 for (i
= 0; i
< list
->n
; ++i
) {
7124 isl_basic_set_add_constraints(product
,
7125 isl_basic_set_copy(list
->p
[i
]), dim
);
7126 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7128 isl_basic_set_list_free(list
);
7131 isl_basic_set_free(product
);
7132 isl_basic_set_list_free(list
);
7136 struct isl_basic_map
*isl_basic_map_product(
7137 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7139 struct isl_dim
*dim_result
= NULL
;
7140 struct isl_basic_map
*bmap
;
7141 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7142 struct isl_dim_map
*dim_map1
, *dim_map2
;
7144 if (!bmap1
|| !bmap2
)
7147 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7148 bmap2
->dim
, isl_dim_param
), goto error
);
7149 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7150 isl_dim_copy(bmap2
->dim
));
7152 in1
= isl_basic_map_n_in(bmap1
);
7153 in2
= isl_basic_map_n_in(bmap2
);
7154 out1
= isl_basic_map_n_out(bmap1
);
7155 out2
= isl_basic_map_n_out(bmap2
);
7156 nparam
= isl_basic_map_n_param(bmap1
);
7158 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7159 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7160 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7161 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7162 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7163 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7164 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7165 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7166 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7167 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7168 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7170 bmap
= isl_basic_map_alloc_dim(dim_result
,
7171 bmap1
->n_div
+ bmap2
->n_div
,
7172 bmap1
->n_eq
+ bmap2
->n_eq
,
7173 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7174 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7175 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7176 bmap
= isl_basic_map_simplify(bmap
);
7177 return isl_basic_map_finalize(bmap
);
7179 isl_basic_map_free(bmap1
);
7180 isl_basic_map_free(bmap2
);
7184 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7185 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7187 isl_basic_map
*prod
;
7189 prod
= isl_basic_map_product(bmap1
, bmap2
);
7190 prod
= isl_basic_map_flatten(prod
);
7194 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7195 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7197 return isl_basic_map_flat_product(bset1
, bset2
);
7200 __isl_give isl_basic_map
*isl_basic_map_range_product(
7201 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7203 isl_dim
*dim_result
= NULL
;
7204 isl_basic_map
*bmap
;
7205 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7206 struct isl_dim_map
*dim_map1
, *dim_map2
;
7208 if (!bmap1
|| !bmap2
)
7211 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7212 isl_dim_copy(bmap2
->dim
));
7214 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7215 out1
= isl_basic_map_n_out(bmap1
);
7216 out2
= isl_basic_map_n_out(bmap2
);
7217 nparam
= isl_basic_map_n_param(bmap1
);
7219 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7220 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7221 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7222 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7223 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7224 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7225 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7226 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7227 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7228 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7229 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7231 bmap
= isl_basic_map_alloc_dim(dim_result
,
7232 bmap1
->n_div
+ bmap2
->n_div
,
7233 bmap1
->n_eq
+ bmap2
->n_eq
,
7234 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7235 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7236 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7237 bmap
= isl_basic_map_simplify(bmap
);
7238 return isl_basic_map_finalize(bmap
);
7240 isl_basic_map_free(bmap1
);
7241 isl_basic_map_free(bmap2
);
7245 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7246 __isl_take isl_map
*map2
,
7247 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7248 __isl_take isl_dim
*right
),
7249 __isl_give isl_basic_map
*(*basic_map_product
)(
7250 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7253 struct isl_map
*result
;
7259 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7260 map2
->dim
, isl_dim_param
), goto error
);
7262 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7263 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7264 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7266 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7267 isl_dim_copy(map2
->dim
)),
7268 map1
->n
* map2
->n
, flags
);
7271 for (i
= 0; i
< map1
->n
; ++i
)
7272 for (j
= 0; j
< map2
->n
; ++j
) {
7273 struct isl_basic_map
*part
;
7274 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7275 isl_basic_map_copy(map2
->p
[j
]));
7276 if (isl_basic_map_is_empty(part
))
7277 isl_basic_map_free(part
);
7279 result
= isl_map_add_basic_map(result
, part
);
7292 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7294 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7296 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7299 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7301 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7302 __isl_take isl_map
*map2
)
7306 prod
= isl_map_product(map1
, map2
);
7307 prod
= isl_map_flatten(prod
);
7311 /* Given two set A and B, construct its Cartesian product A x B.
7313 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7315 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7316 (struct isl_map
*)set2
);
7319 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7320 __isl_take isl_set
*set2
)
7322 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7325 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7327 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7328 __isl_take isl_map
*map2
)
7330 return map_product(map1
, map2
, &isl_dim_range_product
,
7331 &isl_basic_map_range_product
);
7334 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7337 uint32_t hash
= isl_hash_init();
7342 bmap
= isl_basic_map_copy(bmap
);
7343 bmap
= isl_basic_map_normalize(bmap
);
7346 total
= isl_basic_map_total_dim(bmap
);
7347 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7348 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7350 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7351 isl_hash_hash(hash
, c_hash
);
7353 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7354 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7356 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7357 isl_hash_hash(hash
, c_hash
);
7359 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7360 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7362 if (isl_int_is_zero(bmap
->div
[i
][0]))
7364 isl_hash_byte(hash
, i
& 0xFF);
7365 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7366 isl_hash_hash(hash
, c_hash
);
7368 isl_basic_map_free(bmap
);
7372 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7374 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7377 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7384 map
= isl_map_copy(map
);
7385 map
= isl_map_normalize(map
);
7389 hash
= isl_hash_init();
7390 for (i
= 0; i
< map
->n
; ++i
) {
7392 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7393 isl_hash_hash(hash
, bmap_hash
);
7401 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7403 return isl_map_get_hash((isl_map
*)set
);
7406 /* Check if the value for dimension dim is completely determined
7407 * by the values of the other parameters and variables.
7408 * That is, check if dimension dim is involved in an equality.
7410 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7417 nparam
= isl_basic_set_n_param(bset
);
7418 for (i
= 0; i
< bset
->n_eq
; ++i
)
7419 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7424 /* Check if the value for dimension dim is completely determined
7425 * by the values of the other parameters and variables.
7426 * That is, check if dimension dim is involved in an equality
7427 * for each of the subsets.
7429 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7435 for (i
= 0; i
< set
->n
; ++i
) {
7437 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7444 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7446 return set
? set
->n
: 0;
7449 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7450 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7457 for (i
= 0; i
< map
->n
; ++i
)
7458 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7464 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7465 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7472 for (i
= 0; i
< set
->n
; ++i
)
7473 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7479 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7481 struct isl_dim
*dim
;
7486 bset
= isl_basic_set_cow(bset
);
7490 dim
= isl_basic_set_get_dim(bset
);
7491 dim
= isl_dim_lift(dim
, bset
->n_div
);
7494 isl_dim_free(bset
->dim
);
7496 bset
->extra
-= bset
->n_div
;
7499 bset
= isl_basic_set_finalize(bset
);
7503 isl_basic_set_free(bset
);
7507 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7510 struct isl_dim
*dim
;
7513 set
= isl_set_align_divs(set
);
7518 set
= isl_set_cow(set
);
7522 n_div
= set
->p
[0]->n_div
;
7523 dim
= isl_set_get_dim(set
);
7524 dim
= isl_dim_lift(dim
, n_div
);
7527 isl_dim_free(set
->dim
);
7530 for (i
= 0; i
< set
->n
; ++i
) {
7531 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7542 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7544 struct isl_dim
*dim
;
7545 struct isl_basic_map
*bmap
;
7552 set
= isl_set_align_divs(set
);
7557 dim
= isl_set_get_dim(set
);
7558 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7560 return isl_map_identity(isl_dim_map_from_set(dim
));
7563 n_div
= set
->p
[0]->n_div
;
7564 dim
= isl_dim_map_from_set(dim
);
7565 n_param
= isl_dim_size(dim
, isl_dim_param
);
7566 n_set
= isl_dim_size(dim
, isl_dim_in
);
7567 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7568 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7569 for (i
= 0; i
< n_set
; ++i
)
7570 bmap
= var_equal(bmap
, i
);
7572 total
= n_param
+ n_set
+ n_set
+ n_div
;
7573 for (i
= 0; i
< n_div
; ++i
) {
7574 k
= isl_basic_map_alloc_inequality(bmap
);
7577 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7578 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7579 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7580 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7581 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7582 set
->p
[0]->div
[i
][0]);
7584 l
= isl_basic_map_alloc_inequality(bmap
);
7587 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7588 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7589 set
->p
[0]->div
[i
][0]);
7590 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7594 bmap
= isl_basic_map_simplify(bmap
);
7595 bmap
= isl_basic_map_finalize(bmap
);
7596 return isl_map_from_basic_map(bmap
);
7599 isl_basic_map_free(bmap
);
7603 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7611 dim
= isl_basic_set_total_dim(bset
);
7612 size
+= bset
->n_eq
* (1 + dim
);
7613 size
+= bset
->n_ineq
* (1 + dim
);
7614 size
+= bset
->n_div
* (2 + dim
);
7619 int isl_set_size(__isl_keep isl_set
*set
)
7627 for (i
= 0; i
< set
->n
; ++i
)
7628 size
+= isl_basic_set_size(set
->p
[i
]);
7633 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7634 enum isl_dim_type type
, unsigned pos
)
7642 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7644 pos
+= isl_basic_map_offset(bmap
, type
);
7646 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7647 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7651 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7652 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7659 return lower
&& upper
;
7662 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7663 enum isl_dim_type type
, unsigned pos
)
7670 for (i
= 0; i
< map
->n
; ++i
) {
7672 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7673 if (bounded
< 0 || !bounded
)
7680 /* Return 1 if the specified dim is involved in both an upper bound
7681 * and a lower bound.
7683 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7684 enum isl_dim_type type
, unsigned pos
)
7686 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7689 /* For each of the "n" variables starting at "first", determine
7690 * the sign of the variable and put the results in the first "n"
7691 * elements of the array "signs".
7693 * 1 means that the variable is non-negative
7694 * -1 means that the variable is non-positive
7695 * 0 means the variable attains both positive and negative values.
7697 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7698 unsigned first
, unsigned n
, int *signs
)
7700 isl_vec
*bound
= NULL
;
7701 struct isl_tab
*tab
= NULL
;
7702 struct isl_tab_undo
*snap
;
7705 if (!bset
|| !signs
)
7708 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7709 tab
= isl_tab_from_basic_set(bset
);
7713 isl_seq_clr(bound
->el
, bound
->size
);
7714 isl_int_set_si(bound
->el
[0], -1);
7716 snap
= isl_tab_snap(tab
);
7717 for (i
= 0; i
< n
; ++i
) {
7720 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7721 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7724 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7725 if (isl_tab_rollback(tab
, snap
) < 0)
7733 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7734 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7737 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7738 if (isl_tab_rollback(tab
, snap
) < 0)
7741 signs
[i
] = empty
? -1 : 0;
7745 isl_vec_free(bound
);
7749 isl_vec_free(bound
);
7753 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7754 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7756 if (!bset
|| !signs
)
7758 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7761 first
+= pos(bset
->dim
, type
) - 1;
7762 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7765 /* Check if the given basic map is obviously single-valued.
7766 * In particular, for each output dimension, check that there is
7767 * an equality that defines the output dimension in terms of
7768 * earlier dimensions.
7770 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7780 total
= 1 + isl_basic_map_total_dim(bmap
);
7781 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7782 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7784 for (i
= 0; i
< n_out
; ++i
) {
7785 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7786 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7788 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7789 total
- (o_out
+ i
+ 1)) == -1)
7792 if (j
>= bmap
->n_eq
)
7799 /* Check if the given map is obviously single-valued.
7801 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7812 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7815 /* Check if the given map is single-valued.
7820 * and check if the result is a subset of the identity mapping.
7822 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7829 sv
= isl_map_fast_is_single_valued(map
);
7833 test
= isl_map_reverse(isl_map_copy(map
));
7834 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7836 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
7837 id
= isl_map_identity(dim
);
7839 sv
= isl_map_is_subset(test
, id
);
7847 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7851 sv
= isl_map_is_single_valued(map
);
7855 map
= isl_map_copy(map
);
7856 map
= isl_map_reverse(map
);
7857 sv
= isl_map_is_single_valued(map
);
7863 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7865 return isl_map_is_single_valued((isl_map
*)set
);
7868 int isl_map_is_translation(__isl_keep isl_map
*map
)
7873 delta
= isl_map_deltas(isl_map_copy(map
));
7874 ok
= isl_set_is_singleton(delta
);
7875 isl_set_free(delta
);
7880 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7882 if (isl_seq_first_non_zero(p
, pos
) != -1)
7884 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7889 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7898 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7901 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7902 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7903 for (j
= 0; j
< nvar
; ++j
) {
7904 int lower
= 0, upper
= 0;
7905 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7906 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7908 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7914 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7915 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7917 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7919 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7924 if (!lower
|| !upper
)
7931 int isl_set_is_box(__isl_keep isl_set
*set
)
7938 return isl_basic_set_is_box(set
->p
[0]);
7941 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7946 return isl_dim_is_wrapping(bset
->dim
);
7949 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7954 return isl_dim_is_wrapping(set
->dim
);
7957 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7959 bmap
= isl_basic_map_cow(bmap
);
7963 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7967 bmap
= isl_basic_map_finalize(bmap
);
7969 return (isl_basic_set
*)bmap
;
7971 isl_basic_map_free(bmap
);
7975 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7979 map
= isl_map_cow(map
);
7983 for (i
= 0; i
< map
->n
; ++i
) {
7984 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7988 map
->dim
= isl_dim_wrap(map
->dim
);
7992 return (isl_set
*)map
;
7998 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8000 bset
= isl_basic_set_cow(bset
);
8004 bset
->dim
= isl_dim_unwrap(bset
->dim
);
8008 bset
= isl_basic_set_finalize(bset
);
8010 return (isl_basic_map
*)bset
;
8012 isl_basic_set_free(bset
);
8016 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8023 if (!isl_set_is_wrapping(set
))
8024 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8027 set
= isl_set_cow(set
);
8031 for (i
= 0; i
< set
->n
; ++i
) {
8032 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8037 set
->dim
= isl_dim_unwrap(set
->dim
);
8041 return (isl_map
*)set
;
8047 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8048 enum isl_dim_type type
)
8053 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
8056 bmap
= isl_basic_map_cow(bmap
);
8060 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
8064 bmap
= isl_basic_map_finalize(bmap
);
8068 isl_basic_map_free(bmap
);
8072 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8073 enum isl_dim_type type
)
8080 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
8083 map
= isl_map_cow(map
);
8087 for (i
= 0; i
< map
->n
; ++i
) {
8088 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8092 map
->dim
= isl_dim_reset(map
->dim
, type
);
8102 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8107 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8110 bmap
= isl_basic_map_cow(bmap
);
8114 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8118 bmap
= isl_basic_map_finalize(bmap
);
8122 isl_basic_map_free(bmap
);
8126 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8128 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8131 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8138 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8141 map
= isl_map_cow(map
);
8145 for (i
= 0; i
< map
->n
; ++i
) {
8146 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8150 map
->dim
= isl_dim_flatten(map
->dim
);
8160 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8162 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8165 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8167 isl_dim
*dim
, *flat_dim
;
8170 dim
= isl_set_get_dim(set
);
8171 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8172 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8173 map
= isl_map_intersect_domain(map
, set
);
8178 /* Extend the given dim_map with mappings for the divs in bmap.
8180 static __isl_give
struct isl_dim_map
*extend_dim_map(
8181 __isl_keep
struct isl_dim_map
*dim_map
,
8182 __isl_keep isl_basic_map
*bmap
)
8185 struct isl_dim_map
*res
;
8188 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
8190 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
8194 for (i
= 0; i
< dim_map
->len
; ++i
)
8195 res
->pos
[i
] = dim_map
->pos
[i
];
8196 for (i
= 0; i
< bmap
->n_div
; ++i
)
8197 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
8202 /* Extract a dim_map from a reordering.
8203 * We essentially need to reverse the mapping, and add an offset
8204 * of 1 for the constant term.
8206 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
8207 __isl_keep isl_reordering
*exp
)
8210 struct isl_dim_map
*dim_map
;
8215 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
8219 for (i
= 0; i
< exp
->len
; ++i
)
8220 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
8225 /* Reorder the dimensions of "bmap" according to the given dim_map
8226 * and set the dimension specification to "dim".
8228 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8229 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8233 bmap
= isl_basic_map_cow(bmap
);
8234 if (!bmap
|| !dim
|| !dim_map
)
8237 res
= isl_basic_map_alloc_dim(dim
,
8238 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8239 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
8240 res
= isl_basic_map_finalize(res
);
8244 isl_basic_map_free(bmap
);
8249 /* Reorder the dimensions of "map" according to given reordering.
8251 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8252 __isl_take isl_reordering
*r
)
8255 struct isl_dim_map
*dim_map
;
8257 map
= isl_map_cow(map
);
8258 dim_map
= isl_dim_map_from_reordering(r
);
8259 if (!map
|| !r
|| !dim_map
)
8262 for (i
= 0; i
< map
->n
; ++i
) {
8263 struct isl_dim_map
*dim_map_i
;
8265 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
8267 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8268 isl_dim_copy(r
->dim
), dim_map_i
);
8274 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8276 isl_reordering_free(r
);
8282 isl_reordering_free(r
);
8286 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8287 __isl_take isl_reordering
*r
)
8289 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8292 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8293 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8294 enum isl_dim_type c2
, enum isl_dim_type c3
,
8295 enum isl_dim_type c4
, enum isl_dim_type c5
)
8297 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8298 struct isl_mat
*mat
;
8304 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8305 isl_basic_map_total_dim(bmap
) + 1);
8308 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8309 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8310 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8311 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8312 isl_int_set(mat
->row
[i
][pos
],
8313 bmap
->eq
[i
][off
+ k
]);
8321 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8322 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8323 enum isl_dim_type c2
, enum isl_dim_type c3
,
8324 enum isl_dim_type c4
, enum isl_dim_type c5
)
8326 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8327 struct isl_mat
*mat
;
8333 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8334 isl_basic_map_total_dim(bmap
) + 1);
8337 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8338 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8339 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8340 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8341 isl_int_set(mat
->row
[i
][pos
],
8342 bmap
->ineq
[i
][off
+ k
]);
8350 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8351 __isl_take isl_dim
*dim
,
8352 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8353 enum isl_dim_type c2
, enum isl_dim_type c3
,
8354 enum isl_dim_type c4
, enum isl_dim_type c5
)
8356 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8357 isl_basic_map
*bmap
;
8363 if (!dim
|| !eq
|| !ineq
)
8366 if (eq
->n_col
!= ineq
->n_col
)
8367 isl_die(dim
->ctx
, isl_error_invalid
,
8368 "equalities and inequalities matrices should have "
8369 "same number of columns", goto error
);
8371 total
= 1 + isl_dim_total(dim
);
8373 if (eq
->n_col
< total
)
8374 isl_die(dim
->ctx
, isl_error_invalid
,
8375 "number of columns too small", goto error
);
8377 extra
= eq
->n_col
- total
;
8379 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8380 eq
->n_row
, ineq
->n_row
);
8383 for (i
= 0; i
< extra
; ++i
) {
8384 k
= isl_basic_map_alloc_div(bmap
);
8387 isl_int_set_si(bmap
->div
[k
][0], 0);
8389 for (i
= 0; i
< eq
->n_row
; ++i
) {
8390 l
= isl_basic_map_alloc_equality(bmap
);
8393 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8394 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8395 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8396 isl_int_set(bmap
->eq
[l
][off
+ k
],
8402 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8403 l
= isl_basic_map_alloc_inequality(bmap
);
8406 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8407 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8408 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8409 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8428 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8429 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8430 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8432 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8433 c1
, c2
, c3
, c4
, isl_dim_in
);
8436 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8437 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8438 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8440 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8441 c1
, c2
, c3
, c4
, isl_dim_in
);
8444 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8445 __isl_take isl_dim
*dim
,
8446 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8447 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8449 return (isl_basic_set
*)
8450 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8451 c1
, c2
, c3
, c4
, isl_dim_in
);
8454 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8459 return isl_dim_can_zip(bmap
->dim
);
8462 int isl_map_can_zip(__isl_keep isl_map
*map
)
8467 return isl_dim_can_zip(map
->dim
);
8470 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8471 * (A -> C) -> (B -> D).
8473 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8482 if (!isl_basic_map_can_zip(bmap
))
8483 isl_die(bmap
->ctx
, isl_error_invalid
,
8484 "basic map cannot be zipped", goto error
);
8485 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8486 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8487 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8488 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8489 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8492 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8497 isl_basic_map_free(bmap
);
8501 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8502 * (A -> C) -> (B -> D).
8504 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8511 if (!isl_map_can_zip(map
))
8512 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8515 map
= isl_map_cow(map
);
8519 for (i
= 0; i
< map
->n
; ++i
) {
8520 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8525 map
->dim
= isl_dim_zip(map
->dim
);