2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include <isl_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
);
328 isl_basic_map_free(bmap
);
332 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
333 enum isl_dim_type type
)
335 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
338 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
339 enum isl_dim_type type
, const char *s
)
343 map
= isl_map_cow(map
);
347 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
351 for (i
= 0; i
< map
->n
; ++i
) {
352 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
363 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
364 enum isl_dim_type type
)
366 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
369 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
372 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
375 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
377 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
380 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
381 enum isl_dim_type type
, unsigned pos
)
383 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
386 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
387 enum isl_dim_type type
, unsigned pos
)
389 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
392 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
393 enum isl_dim_type type
, unsigned pos
)
395 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
398 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
399 __isl_take isl_basic_map
*bmap
,
400 enum isl_dim_type type
, unsigned pos
, const char *s
)
404 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
409 isl_basic_map_free(bmap
);
413 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
414 enum isl_dim_type type
, unsigned pos
, const char *s
)
421 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
425 for (i
= 0; i
< map
->n
; ++i
) {
426 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
437 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
438 __isl_take isl_basic_set
*bset
,
439 enum isl_dim_type type
, unsigned pos
, const char *s
)
441 return (isl_basic_set
*)isl_basic_map_set_dim_name(
442 (isl_basic_map
*)bset
, type
, pos
, s
);
445 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
446 enum isl_dim_type type
, unsigned pos
, const char *s
)
448 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
451 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
455 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
458 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
459 struct isl_basic_map
*bmap
, unsigned extra
,
460 unsigned n_eq
, unsigned n_ineq
)
463 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
468 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
469 if (isl_blk_is_error(bmap
->block
))
472 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
477 bmap
->block2
= isl_blk_empty();
480 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
481 if (isl_blk_is_error(bmap
->block2
))
484 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
489 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
490 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
492 for (i
= 0; i
< extra
; ++i
)
493 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
497 bmap
->c_size
= n_eq
+ n_ineq
;
498 bmap
->eq
= bmap
->ineq
+ n_ineq
;
507 isl_basic_map_free(bmap
);
511 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
512 unsigned nparam
, unsigned dim
, unsigned extra
,
513 unsigned n_eq
, unsigned n_ineq
)
515 struct isl_basic_map
*bmap
;
516 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
517 return (struct isl_basic_set
*)bmap
;
520 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
521 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
523 struct isl_basic_map
*bmap
;
526 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
527 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
528 return (struct isl_basic_set
*)bmap
;
534 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
535 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
537 struct isl_basic_map
*bmap
;
541 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
546 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
552 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
553 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
554 unsigned n_eq
, unsigned n_ineq
)
556 struct isl_basic_map
*bmap
;
559 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
563 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
567 static void dup_constraints(
568 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
571 unsigned total
= isl_basic_map_total_dim(src
);
573 for (i
= 0; i
< src
->n_eq
; ++i
) {
574 int j
= isl_basic_map_alloc_equality(dst
);
575 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
578 for (i
= 0; i
< src
->n_ineq
; ++i
) {
579 int j
= isl_basic_map_alloc_inequality(dst
);
580 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
583 for (i
= 0; i
< src
->n_div
; ++i
) {
584 int j
= isl_basic_map_alloc_div(dst
);
585 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
587 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
590 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
592 struct isl_basic_map
*dup
;
596 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
597 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
600 dup_constraints(dup
, bmap
);
601 dup
->flags
= bmap
->flags
;
602 dup
->sample
= isl_vec_copy(bmap
->sample
);
606 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
608 struct isl_basic_map
*dup
;
610 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
611 return (struct isl_basic_set
*)dup
;
614 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
619 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
623 return isl_basic_set_dup(bset
);
626 struct isl_set
*isl_set_copy(struct isl_set
*set
)
635 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
640 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
644 bmap
= isl_basic_map_dup(bmap
);
646 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
650 struct isl_map
*isl_map_copy(struct isl_map
*map
)
659 void isl_basic_map_free(struct isl_basic_map
*bmap
)
667 isl_ctx_deref(bmap
->ctx
);
669 isl_blk_free(bmap
->ctx
, bmap
->block2
);
671 isl_blk_free(bmap
->ctx
, bmap
->block
);
672 isl_vec_free(bmap
->sample
);
673 isl_dim_free(bmap
->dim
);
677 void isl_basic_set_free(struct isl_basic_set
*bset
)
679 isl_basic_map_free((struct isl_basic_map
*)bset
);
682 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
684 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
687 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
693 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
694 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
696 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
697 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
698 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
699 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
700 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
701 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
703 int j
= isl_basic_map_alloc_inequality(bmap
);
707 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
708 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
715 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
716 bmap
->extra
- bmap
->n_div
);
720 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
722 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
725 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
729 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
734 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
736 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
739 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
744 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
746 if (pos
!= bmap
->n_eq
- 1) {
748 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
749 bmap
->eq
[bmap
->n_eq
- 1] = t
;
755 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
757 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
760 void isl_basic_map_inequality_to_equality(
761 struct isl_basic_map
*bmap
, unsigned pos
)
766 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
767 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
772 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
773 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
774 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
775 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
778 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
780 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
783 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
789 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
790 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
791 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
792 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
793 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
794 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
795 1 + isl_basic_map_total_dim(bmap
),
796 bmap
->extra
- bmap
->n_div
);
797 return bmap
->n_ineq
++;
800 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
802 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
805 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
809 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
814 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
816 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
819 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
824 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
826 if (pos
!= bmap
->n_ineq
- 1) {
828 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
829 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
830 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
836 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
838 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
841 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
846 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
849 k
= isl_basic_map_alloc_equality(bmap
);
852 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
855 isl_basic_map_free(bmap
);
859 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
862 return (isl_basic_set
*)
863 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
866 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
871 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
874 k
= isl_basic_map_alloc_inequality(bmap
);
877 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
880 isl_basic_map_free(bmap
);
884 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
887 return (isl_basic_set
*)
888 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
891 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
895 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
896 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
897 1 + 1 + isl_basic_map_total_dim(bmap
),
898 bmap
->extra
- bmap
->n_div
);
899 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
900 return bmap
->n_div
++;
903 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
905 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
908 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
912 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
917 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
919 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
922 /* Copy constraint from src to dst, putting the vars of src at offset
923 * dim_off in dst and the divs of src at offset div_off in dst.
924 * If both sets are actually map, then dim_off applies to the input
927 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
928 struct isl_basic_map
*src_map
, isl_int
*src
,
929 unsigned in_off
, unsigned out_off
, unsigned div_off
)
931 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
932 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
933 unsigned src_in
= isl_basic_map_n_in(src_map
);
934 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
935 unsigned src_out
= isl_basic_map_n_out(src_map
);
936 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
937 isl_int_set(dst
[0], src
[0]);
938 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
939 if (dst_nparam
> src_nparam
)
940 isl_seq_clr(dst
+1+src_nparam
,
941 dst_nparam
- src_nparam
);
942 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
943 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
945 isl_min(dst_in
-in_off
, src_in
));
946 if (dst_in
-in_off
> src_in
)
947 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
948 dst_in
- in_off
- src_in
);
949 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
950 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
951 src
+1+src_nparam
+src_in
,
952 isl_min(dst_out
-out_off
, src_out
));
953 if (dst_out
-out_off
> src_out
)
954 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
955 dst_out
- out_off
- src_out
);
956 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
957 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
958 src
+1+src_nparam
+src_in
+src_out
,
959 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
960 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
961 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
962 div_off
+src_map
->n_div
,
963 dst_map
->n_div
- div_off
- src_map
->n_div
);
966 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
967 struct isl_basic_map
*src_map
, isl_int
*src
,
968 unsigned in_off
, unsigned out_off
, unsigned div_off
)
970 isl_int_set(dst
[0], src
[0]);
971 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
974 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
975 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
980 if (!bmap1
|| !bmap2
)
983 div_off
= bmap1
->n_div
;
985 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
986 int i1
= isl_basic_map_alloc_equality(bmap1
);
989 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
990 i_pos
, o_pos
, div_off
);
993 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
994 int i1
= isl_basic_map_alloc_inequality(bmap1
);
997 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
998 i_pos
, o_pos
, div_off
);
1001 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1002 int i1
= isl_basic_map_alloc_div(bmap1
);
1005 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1006 i_pos
, o_pos
, div_off
);
1009 isl_basic_map_free(bmap2
);
1014 isl_basic_map_free(bmap1
);
1015 isl_basic_map_free(bmap2
);
1019 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
1020 struct isl_dim_map
*dim_map
)
1024 for (i
= 0; i
< dim_map
->len
; ++i
) {
1025 if (dim_map
->pos
[i
] < 0)
1026 isl_int_set_si(dst
[i
], 0);
1028 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
1032 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
1033 struct isl_dim_map
*dim_map
)
1035 isl_int_set(dst
[0], src
[0]);
1036 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
1039 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
1040 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
1044 if (!src
|| !dst
|| !dim_map
)
1047 for (i
= 0; i
< src
->n_eq
; ++i
) {
1048 int i1
= isl_basic_map_alloc_equality(dst
);
1051 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
1054 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1055 int i1
= isl_basic_map_alloc_inequality(dst
);
1058 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1061 for (i
= 0; i
< src
->n_div
; ++i
) {
1062 int i1
= isl_basic_map_alloc_div(dst
);
1065 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1069 isl_basic_map_free(src
);
1074 isl_basic_map_free(src
);
1075 isl_basic_map_free(dst
);
1079 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1080 struct isl_basic_set
*bset2
, unsigned pos
)
1082 return (struct isl_basic_set
*)
1083 add_constraints((struct isl_basic_map
*)bset1
,
1084 (struct isl_basic_map
*)bset2
, 0, pos
);
1087 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1088 struct isl_dim
*dim
, unsigned extra
,
1089 unsigned n_eq
, unsigned n_ineq
)
1091 struct isl_basic_map
*ext
;
1101 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1102 base
->extra
>= base
->n_div
+ extra
;
1104 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1105 room_for_ineq(base
, n_ineq
)) {
1110 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1111 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1112 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1113 extra
+= base
->extra
;
1115 n_ineq
+= base
->n_ineq
;
1117 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1123 ext
->sample
= isl_vec_copy(base
->sample
);
1124 flags
= base
->flags
;
1125 ext
= add_constraints(ext
, base
, 0, 0);
1128 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1135 isl_basic_map_free(base
);
1139 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1140 struct isl_dim
*dim
, unsigned extra
,
1141 unsigned n_eq
, unsigned n_ineq
)
1143 return (struct isl_basic_set
*)
1144 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1145 extra
, n_eq
, n_ineq
);
1148 struct isl_basic_map
*isl_basic_map_extend_constraints(
1149 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1153 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1157 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1158 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1159 unsigned n_eq
, unsigned n_ineq
)
1161 struct isl_basic_map
*bmap
;
1162 struct isl_dim
*dim
;
1166 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1170 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1173 isl_basic_map_free(base
);
1177 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1178 unsigned nparam
, unsigned dim
, unsigned extra
,
1179 unsigned n_eq
, unsigned n_ineq
)
1181 return (struct isl_basic_set
*)
1182 isl_basic_map_extend((struct isl_basic_map
*)base
,
1183 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1186 struct isl_basic_set
*isl_basic_set_extend_constraints(
1187 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1189 return (struct isl_basic_set
*)
1190 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1194 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1196 return (struct isl_basic_set
*)
1197 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1200 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1205 if (bmap
->ref
> 1) {
1207 bmap
= isl_basic_map_dup(bmap
);
1210 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1214 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1222 return isl_set_dup(set
);
1225 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1233 return isl_map_dup(map
);
1236 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1237 unsigned a_len
, unsigned b_len
)
1239 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1240 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1241 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1244 struct isl_basic_set
*isl_basic_set_swap_vars(
1245 struct isl_basic_set
*bset
, unsigned n
)
1255 nparam
= isl_basic_set_n_param(bset
);
1256 dim
= isl_basic_set_n_dim(bset
);
1257 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1262 bset
= isl_basic_set_cow(bset
);
1266 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1267 if (isl_blk_is_error(blk
))
1270 for (i
= 0; i
< bset
->n_eq
; ++i
)
1272 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1274 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1276 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1278 for (i
= 0; i
< bset
->n_div
; ++i
)
1280 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1282 isl_blk_free(bset
->ctx
, blk
);
1284 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1285 bset
= isl_basic_set_gauss(bset
, NULL
);
1286 return isl_basic_set_finalize(bset
);
1288 isl_basic_set_free(bset
);
1292 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1295 set
= isl_set_cow(set
);
1299 for (i
= 0; i
< set
->n
; ++i
) {
1300 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1306 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1310 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1316 total
= isl_basic_map_total_dim(bmap
);
1317 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1318 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1320 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1322 i
= isl_basic_map_alloc_equality(bmap
);
1326 isl_int_set_si(bmap
->eq
[i
][0], 1);
1327 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1328 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1329 isl_vec_free(bmap
->sample
);
1330 bmap
->sample
= NULL
;
1331 return isl_basic_map_finalize(bmap
);
1333 isl_basic_map_free(bmap
);
1337 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1339 return (struct isl_basic_set
*)
1340 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1343 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1346 unsigned off
= isl_dim_total(bmap
->dim
);
1347 isl_int
*t
= bmap
->div
[a
];
1348 bmap
->div
[a
] = bmap
->div
[b
];
1351 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1352 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1354 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1355 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1357 for (i
= 0; i
< bmap
->n_div
; ++i
)
1358 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1359 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1362 /* Eliminate the specified n dimensions starting at first from the
1363 * constraints using Fourier-Motzkin. The dimensions themselves
1366 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1367 enum isl_dim_type type
, unsigned first
, unsigned n
)
1376 map
= isl_map_cow(map
);
1379 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1380 first
+= pos(map
->dim
, type
) - 1;
1382 for (i
= 0; i
< map
->n
; ++i
) {
1383 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1397 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1398 enum isl_dim_type type
, unsigned first
, unsigned n
)
1400 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1403 /* Eliminate the specified n dimensions starting at first from the
1404 * constraints using Fourier-Motzkin. The dimensions themselves
1407 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1408 unsigned first
, unsigned n
)
1410 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1413 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1414 __isl_take isl_basic_map
*bmap
)
1416 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1424 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1425 __isl_take isl_basic_set
*bset
)
1427 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1428 (struct isl_basic_map
*)bset
);
1431 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1440 set
= isl_set_cow(set
);
1444 for (i
= 0; i
< set
->n
; ++i
) {
1445 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1455 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1456 enum isl_dim_type type
, unsigned first
, unsigned n
)
1460 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1464 bmap
= isl_basic_map_eliminate_vars(bmap
,
1465 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1468 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1470 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1473 isl_basic_map_free(bmap
);
1477 /* Return true if the definition of the given div (recursively) involves
1478 * any of the given variables.
1480 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1481 unsigned first
, unsigned n
)
1484 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1486 if (isl_int_is_zero(bmap
->div
[div
][0]))
1488 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1491 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1492 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1494 if (div_involves_vars(bmap
, i
, first
, n
))
1501 /* Remove all divs (recursively) involving any of the given dimensions
1502 * in their definitions.
1504 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1505 __isl_take isl_basic_map
*bmap
,
1506 enum isl_dim_type type
, unsigned first
, unsigned n
)
1512 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1514 first
+= isl_basic_map_offset(bmap
, type
);
1516 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1517 if (!div_involves_vars(bmap
, i
, first
, n
))
1519 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1524 isl_basic_map_free(bmap
);
1528 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1529 enum isl_dim_type type
, unsigned first
, unsigned n
)
1538 map
= isl_map_cow(map
);
1542 for (i
= 0; i
< map
->n
; ++i
) {
1543 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1554 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1555 enum isl_dim_type type
, unsigned first
, unsigned n
)
1557 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1561 /* Return true if the definition of the given div is unknown or depends
1564 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1567 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1569 if (isl_int_is_zero(bmap
->div
[div
][0]))
1572 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1573 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1575 if (div_is_unknown(bmap
, i
))
1582 /* Remove all divs that are unknown or defined in terms of unknown divs.
1584 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1585 __isl_take isl_basic_map
*bmap
)
1592 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1593 if (!div_is_unknown(bmap
, i
))
1595 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1600 isl_basic_map_free(bmap
);
1604 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1613 map
= isl_map_cow(map
);
1617 for (i
= 0; i
< map
->n
; ++i
) {
1618 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1628 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1630 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1633 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1634 __isl_take isl_basic_set
*bset
,
1635 enum isl_dim_type type
, unsigned first
, unsigned n
)
1637 return (isl_basic_set
*)
1638 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1641 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1642 enum isl_dim_type type
, unsigned first
, unsigned n
)
1649 map
= isl_map_cow(map
);
1652 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1654 for (i
= 0; i
< map
->n
; ++i
) {
1655 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1656 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1660 map
= isl_map_drop(map
, type
, first
, n
);
1667 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1668 enum isl_dim_type type
, unsigned first
, unsigned n
)
1670 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1673 /* Project out n inputs starting at first using Fourier-Motzkin */
1674 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1675 unsigned first
, unsigned n
)
1677 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1680 static void dump_term(struct isl_basic_map
*bmap
,
1681 isl_int c
, int pos
, FILE *out
)
1684 unsigned in
= isl_basic_map_n_in(bmap
);
1685 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1686 unsigned nparam
= isl_basic_map_n_param(bmap
);
1688 isl_int_print(out
, c
, 0);
1690 if (!isl_int_is_one(c
))
1691 isl_int_print(out
, c
, 0);
1692 if (pos
< 1 + nparam
) {
1693 name
= isl_dim_get_name(bmap
->dim
,
1694 isl_dim_param
, pos
- 1);
1696 fprintf(out
, "%s", name
);
1698 fprintf(out
, "p%d", pos
- 1);
1699 } else if (pos
< 1 + nparam
+ in
)
1700 fprintf(out
, "i%d", pos
- 1 - nparam
);
1701 else if (pos
< 1 + nparam
+ dim
)
1702 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1704 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1708 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1709 int sign
, FILE *out
)
1713 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1717 for (i
= 0, first
= 1; i
< len
; ++i
) {
1718 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1721 fprintf(out
, " + ");
1723 isl_int_abs(v
, c
[i
]);
1724 dump_term(bmap
, v
, i
, out
);
1731 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1732 const char *op
, FILE *out
, int indent
)
1736 fprintf(out
, "%*s", indent
, "");
1738 dump_constraint_sign(bmap
, c
, 1, out
);
1739 fprintf(out
, " %s ", op
);
1740 dump_constraint_sign(bmap
, c
, -1, out
);
1744 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1745 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1747 fprintf(out
, "%*s", indent
, "");
1748 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1753 static void dump_constraints(struct isl_basic_map
*bmap
,
1754 isl_int
**c
, unsigned n
,
1755 const char *op
, FILE *out
, int indent
)
1759 for (i
= 0; i
< n
; ++i
)
1760 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1763 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1767 unsigned total
= isl_basic_map_total_dim(bmap
);
1769 for (j
= 0; j
< 1 + total
; ++j
) {
1770 if (isl_int_is_zero(exp
[j
]))
1772 if (!first
&& isl_int_is_pos(exp
[j
]))
1774 dump_term(bmap
, exp
[j
], j
, out
);
1779 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1783 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1784 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1786 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1787 fprintf(out
, "%*s", indent
, "");
1788 fprintf(out
, "e%d = [(", i
);
1789 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1791 isl_int_print(out
, bmap
->div
[i
][0], 0);
1792 fprintf(out
, "]\n");
1796 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1797 FILE *out
, int indent
)
1800 fprintf(out
, "null basic set\n");
1804 fprintf(out
, "%*s", indent
, "");
1805 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1806 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1807 bset
->extra
, bset
->flags
);
1808 dump((struct isl_basic_map
*)bset
, out
, indent
);
1811 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1812 FILE *out
, int indent
)
1815 fprintf(out
, "null basic map\n");
1819 fprintf(out
, "%*s", indent
, "");
1820 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1821 "flags: %x, n_name: %d\n",
1823 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1824 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1825 dump(bmap
, out
, indent
);
1828 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1833 total
= isl_basic_map_total_dim(bmap
);
1834 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1835 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1836 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1837 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1841 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1843 struct isl_set
*set
;
1847 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1848 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1849 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1850 sizeof(struct isl_set
) +
1851 (n
- 1) * sizeof(struct isl_basic_set
*));
1855 set
->ctx
= dim
->ctx
;
1856 isl_ctx_ref(set
->ctx
);
1868 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1869 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1871 struct isl_set
*set
;
1872 struct isl_dim
*dims
;
1874 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1878 set
= isl_set_alloc_dim(dims
, n
, flags
);
1882 /* Make sure "map" has room for at least "n" more basic maps.
1884 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1887 struct isl_map
*grown
= NULL
;
1891 isl_assert(map
->ctx
, n
>= 0, goto error
);
1892 if (map
->n
+ n
<= map
->size
)
1894 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1897 for (i
= 0; i
< map
->n
; ++i
) {
1898 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1906 isl_map_free(grown
);
1911 /* Make sure "set" has room for at least "n" more basic sets.
1913 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1915 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1918 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1921 struct isl_set
*dup
;
1926 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1929 for (i
= 0; i
< set
->n
; ++i
)
1930 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1934 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1936 struct isl_set
*set
;
1941 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1943 isl_basic_set_free(bset
);
1946 return isl_set_add_basic_set(set
, bset
);
1949 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1951 struct isl_map
*map
;
1956 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1958 isl_basic_map_free(bmap
);
1961 return isl_map_add_basic_map(map
, bmap
);
1964 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1965 __isl_take isl_basic_set
*bset
)
1967 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1968 (struct isl_basic_map
*)bset
);
1971 void isl_set_free(struct isl_set
*set
)
1981 isl_ctx_deref(set
->ctx
);
1982 for (i
= 0; i
< set
->n
; ++i
)
1983 isl_basic_set_free(set
->p
[i
]);
1984 isl_dim_free(set
->dim
);
1988 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
1993 fprintf(out
, "null set\n");
1997 fprintf(out
, "%*s", indent
, "");
1998 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1999 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2001 for (i
= 0; i
< set
->n
; ++i
) {
2002 fprintf(out
, "%*s", indent
, "");
2003 fprintf(out
, "basic set %d:\n", i
);
2004 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2008 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2013 fprintf(out
, "null map\n");
2017 fprintf(out
, "%*s", indent
, "");
2018 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2019 "flags: %x, n_name: %d\n",
2020 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2021 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2022 for (i
= 0; i
< map
->n
; ++i
) {
2023 fprintf(out
, "%*s", indent
, "");
2024 fprintf(out
, "basic map %d:\n", i
);
2025 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2029 struct isl_basic_map
*isl_basic_map_intersect_domain(
2030 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2032 struct isl_basic_map
*bmap_domain
;
2033 struct isl_dim
*dim
;
2038 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2039 bset
->dim
, isl_dim_param
), goto error
);
2041 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2042 isl_assert(bset
->ctx
,
2043 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2045 bmap
= isl_basic_map_cow(bmap
);
2048 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2049 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2050 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
2051 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
2052 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2054 bmap
= isl_basic_map_simplify(bmap
);
2055 return isl_basic_map_finalize(bmap
);
2057 isl_basic_map_free(bmap
);
2058 isl_basic_set_free(bset
);
2062 struct isl_basic_map
*isl_basic_map_intersect_range(
2063 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2065 struct isl_basic_map
*bmap_range
;
2070 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2071 bset
->dim
, isl_dim_param
), goto error
);
2073 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2074 isl_assert(bset
->ctx
,
2075 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2077 bmap
= isl_basic_map_cow(bmap
);
2080 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2081 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2082 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2083 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2085 bmap
= isl_basic_map_simplify(bmap
);
2086 return isl_basic_map_finalize(bmap
);
2088 isl_basic_map_free(bmap
);
2089 isl_basic_set_free(bset
);
2093 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2099 total
= 1 + isl_basic_map_total_dim(bmap
);
2100 if (total
!= vec
->size
)
2105 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2106 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2107 if (!isl_int_is_zero(s
)) {
2113 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2114 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2115 if (isl_int_is_neg(s
)) {
2126 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2128 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2131 struct isl_basic_map
*isl_basic_map_intersect(
2132 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2134 struct isl_vec
*sample
= NULL
;
2136 if (!bmap1
|| !bmap2
)
2139 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2140 bmap2
->dim
, isl_dim_param
), goto error
);
2141 if (isl_dim_total(bmap1
->dim
) ==
2142 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2143 isl_dim_total(bmap2
->dim
) !=
2144 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2145 return isl_basic_map_intersect(bmap2
, bmap1
);
2147 if (isl_dim_total(bmap2
->dim
) !=
2148 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2149 isl_assert(bmap1
->ctx
,
2150 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2152 if (bmap1
->sample
&&
2153 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2154 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2155 sample
= isl_vec_copy(bmap1
->sample
);
2156 else if (bmap2
->sample
&&
2157 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2158 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2159 sample
= isl_vec_copy(bmap2
->sample
);
2161 bmap1
= isl_basic_map_cow(bmap1
);
2164 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2165 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2166 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2169 isl_vec_free(sample
);
2171 isl_vec_free(bmap1
->sample
);
2172 bmap1
->sample
= sample
;
2175 bmap1
= isl_basic_map_simplify(bmap1
);
2176 return isl_basic_map_finalize(bmap1
);
2179 isl_vec_free(sample
);
2180 isl_basic_map_free(bmap1
);
2181 isl_basic_map_free(bmap2
);
2185 struct isl_basic_set
*isl_basic_set_intersect(
2186 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2188 return (struct isl_basic_set
*)
2189 isl_basic_map_intersect(
2190 (struct isl_basic_map
*)bset1
,
2191 (struct isl_basic_map
*)bset2
);
2194 /* Special case of isl_map_intersect, where both map1 and map2
2195 * are convex, without any divs and such that either map1 or map2
2196 * contains a single constraint. This constraint is then simply
2197 * added to the other map.
2199 static __isl_give isl_map
*map_intersect_add_constraint(
2200 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2202 struct isl_basic_map
*bmap1
;
2203 struct isl_basic_map
*bmap2
;
2205 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2206 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2207 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2208 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2210 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2211 return isl_map_intersect(map2
, map1
);
2213 isl_assert(map2
->ctx
,
2214 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2216 map1
= isl_map_cow(map1
);
2219 if (isl_map_fast_is_empty(map1
)) {
2223 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2224 if (map2
->p
[0]->n_eq
== 1)
2225 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2227 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2228 map2
->p
[0]->ineq
[0]);
2230 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2231 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2235 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2236 isl_basic_map_free(map1
->p
[0]);
2249 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2252 struct isl_map
*result
;
2258 if (isl_map_fast_is_empty(map1
)) {
2262 if (isl_map_fast_is_empty(map2
)) {
2267 if (map1
->n
== 1 && map2
->n
== 1 &&
2268 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2269 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2270 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2271 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2272 return map_intersect_add_constraint(map1
, map2
);
2273 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2274 map2
->dim
, isl_dim_param
), goto error
);
2275 if (isl_dim_total(map1
->dim
) ==
2276 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2277 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2278 return isl_map_intersect(map2
, map1
);
2280 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2281 isl_assert(map1
->ctx
,
2282 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2284 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2285 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2286 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2288 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2289 map1
->n
* map2
->n
, flags
);
2292 for (i
= 0; i
< map1
->n
; ++i
)
2293 for (j
= 0; j
< map2
->n
; ++j
) {
2294 struct isl_basic_map
*part
;
2295 part
= isl_basic_map_intersect(
2296 isl_basic_map_copy(map1
->p
[i
]),
2297 isl_basic_map_copy(map2
->p
[j
]));
2298 if (isl_basic_map_is_empty(part
))
2299 isl_basic_map_free(part
);
2301 result
= isl_map_add_basic_map(result
, part
);
2314 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2316 return (struct isl_set
*)
2317 isl_map_intersect((struct isl_map
*)set1
,
2318 (struct isl_map
*)set2
);
2321 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2323 struct isl_dim
*dim
;
2324 struct isl_basic_set
*bset
;
2329 bmap
= isl_basic_map_cow(bmap
);
2332 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2333 in
= isl_basic_map_n_in(bmap
);
2334 bset
= isl_basic_set_from_basic_map(bmap
);
2335 bset
= isl_basic_set_swap_vars(bset
, in
);
2336 return isl_basic_map_from_basic_set(bset
, dim
);
2339 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2340 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2342 struct isl_dim
*res_dim
;
2343 struct isl_basic_map
*res
;
2344 struct isl_dim_map
*dim_map
;
2345 unsigned total
, off
;
2346 enum isl_dim_type t
;
2354 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2356 total
= isl_basic_map_total_dim(bmap
) + n
;
2357 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2359 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2361 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2363 unsigned size
= isl_basic_map_dim(bmap
, t
);
2364 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2366 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2367 pos
, size
- pos
, off
+ pos
+ n
);
2369 off
+= isl_dim_size(res_dim
, t
);
2371 isl_dim_map_div(dim_map
, bmap
, off
);
2373 res
= isl_basic_map_alloc_dim(res_dim
,
2374 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2375 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2376 return isl_basic_map_finalize(res
);
2379 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2380 enum isl_dim_type type
, unsigned n
)
2384 return isl_basic_map_insert(bmap
, type
,
2385 isl_basic_map_dim(bmap
, type
), n
);
2388 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2389 enum isl_dim_type type
, unsigned n
)
2393 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2394 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2396 isl_basic_set_free(bset
);
2400 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2401 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2408 map
= isl_map_cow(map
);
2412 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2416 for (i
= 0; i
< map
->n
; ++i
) {
2417 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2428 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2429 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2431 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2434 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2435 enum isl_dim_type type
, unsigned n
)
2439 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2442 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2443 enum isl_dim_type type
, unsigned n
)
2447 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2448 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2454 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2455 __isl_take isl_basic_map
*bmap
,
2456 enum isl_dim_type dst_type
, unsigned dst_pos
,
2457 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2460 struct isl_dim_map
*dim_map
;
2461 struct isl_basic_map
*res
;
2462 enum isl_dim_type t
;
2463 unsigned total
, off
;
2470 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2473 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2476 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2478 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2479 pos(bmap
->dim
, src_type
) + src_pos
+
2480 ((src_type
< dst_type
) ? n
: 0)) {
2481 bmap
= isl_basic_map_cow(bmap
);
2485 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2486 src_type
, src_pos
, n
);
2490 bmap
= isl_basic_map_finalize(bmap
);
2495 total
= isl_basic_map_total_dim(bmap
);
2496 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2499 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2500 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2501 if (t
== dst_type
) {
2502 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2505 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2508 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2509 dst_pos
, size
- dst_pos
, off
);
2510 off
+= size
- dst_pos
;
2511 } else if (t
== src_type
) {
2512 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2515 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2516 src_pos
+ n
, size
- src_pos
- n
, off
);
2517 off
+= size
- src_pos
- n
;
2519 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2523 isl_dim_map_div(dim_map
, bmap
, off
);
2525 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2526 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2527 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2529 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2530 src_type
, src_pos
, n
);
2534 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2535 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2536 bmap
= isl_basic_map_finalize(bmap
);
2540 isl_basic_map_free(bmap
);
2544 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2545 enum isl_dim_type dst_type
, unsigned dst_pos
,
2546 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2548 return (isl_basic_set
*)isl_basic_map_move_dims(
2549 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2552 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2553 enum isl_dim_type dst_type
, unsigned dst_pos
,
2554 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2558 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2559 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2560 src_type
, src_pos
, n
);
2566 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2567 enum isl_dim_type dst_type
, unsigned dst_pos
,
2568 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2577 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2580 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2583 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2585 map
= isl_map_cow(map
);
2589 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2593 for (i
= 0; i
< map
->n
; ++i
) {
2594 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2596 src_type
, src_pos
, n
);
2607 /* Move the specified dimensions to the last columns right before
2608 * the divs. Don't change the dimension specification of bmap.
2609 * That's the responsibility of the caller.
2611 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2612 enum isl_dim_type type
, unsigned first
, unsigned n
)
2614 struct isl_dim_map
*dim_map
;
2615 struct isl_basic_map
*res
;
2616 enum isl_dim_type t
;
2617 unsigned total
, off
;
2621 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2624 total
= isl_basic_map_total_dim(bmap
);
2625 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2628 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2629 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2631 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2634 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2635 first
, n
, total
- bmap
->n_div
- n
);
2636 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2637 first
+ n
, size
- (first
+ n
), off
);
2638 off
+= size
- (first
+ n
);
2640 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2644 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2646 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2647 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2648 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2652 /* Turn the n dimensions of type type, starting at first
2653 * into existentially quantified variables.
2655 __isl_give isl_basic_map
*isl_basic_map_project_out(
2656 __isl_take isl_basic_map
*bmap
,
2657 enum isl_dim_type type
, unsigned first
, unsigned n
)
2670 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2671 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2673 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2676 bmap
= move_last(bmap
, type
, first
, n
);
2677 bmap
= isl_basic_map_cow(bmap
);
2681 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2682 old
= bmap
->block2
.data
;
2683 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2684 (bmap
->extra
+ n
) * (1 + row_size
));
2685 if (!bmap
->block2
.data
)
2687 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2690 for (i
= 0; i
< n
; ++i
) {
2691 new_div
[i
] = bmap
->block2
.data
+
2692 (bmap
->extra
+ i
) * (1 + row_size
);
2693 isl_seq_clr(new_div
[i
], 1 + row_size
);
2695 for (i
= 0; i
< bmap
->extra
; ++i
)
2696 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2698 bmap
->div
= new_div
;
2702 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2705 bmap
= isl_basic_map_simplify(bmap
);
2706 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2707 return isl_basic_map_finalize(bmap
);
2709 isl_basic_map_free(bmap
);
2713 /* Turn the n dimensions of type type, starting at first
2714 * into existentially quantified variables.
2716 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2717 enum isl_dim_type type
, unsigned first
, unsigned n
)
2719 return (isl_basic_set
*)isl_basic_map_project_out(
2720 (isl_basic_map
*)bset
, type
, first
, n
);
2723 /* Turn the n dimensions of type type, starting at first
2724 * into existentially quantified variables.
2726 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2727 enum isl_dim_type type
, unsigned first
, unsigned n
)
2737 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2739 map
= isl_map_cow(map
);
2743 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2747 for (i
= 0; i
< map
->n
; ++i
) {
2748 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2759 /* Turn the n dimensions of type type, starting at first
2760 * into existentially quantified variables.
2762 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2763 enum isl_dim_type type
, unsigned first
, unsigned n
)
2765 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2768 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2772 for (i
= 0; i
< n
; ++i
) {
2773 j
= isl_basic_map_alloc_div(bmap
);
2776 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2780 isl_basic_map_free(bmap
);
2784 struct isl_basic_map
*isl_basic_map_apply_range(
2785 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2787 struct isl_dim
*dim_result
= NULL
;
2788 struct isl_basic_map
*bmap
;
2789 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2790 struct isl_dim_map
*dim_map1
, *dim_map2
;
2792 if (!bmap1
|| !bmap2
)
2795 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2796 isl_dim_copy(bmap2
->dim
));
2798 n_in
= isl_basic_map_n_in(bmap1
);
2799 n_out
= isl_basic_map_n_out(bmap2
);
2800 n
= isl_basic_map_n_out(bmap1
);
2801 nparam
= isl_basic_map_n_param(bmap1
);
2803 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2804 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2805 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2806 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2807 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2808 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2809 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2810 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2811 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2812 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2813 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2815 bmap
= isl_basic_map_alloc_dim(dim_result
,
2816 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2817 bmap1
->n_eq
+ bmap2
->n_eq
,
2818 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2819 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2820 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2821 bmap
= add_divs(bmap
, n
);
2822 bmap
= isl_basic_map_simplify(bmap
);
2823 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2824 return isl_basic_map_finalize(bmap
);
2826 isl_basic_map_free(bmap1
);
2827 isl_basic_map_free(bmap2
);
2831 struct isl_basic_set
*isl_basic_set_apply(
2832 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2837 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2840 return (struct isl_basic_set
*)
2841 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2843 isl_basic_set_free(bset
);
2844 isl_basic_map_free(bmap
);
2848 struct isl_basic_map
*isl_basic_map_apply_domain(
2849 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2851 if (!bmap1
|| !bmap2
)
2854 isl_assert(bmap1
->ctx
,
2855 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2856 isl_assert(bmap1
->ctx
,
2857 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2860 bmap1
= isl_basic_map_reverse(bmap1
);
2861 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2862 return isl_basic_map_reverse(bmap1
);
2864 isl_basic_map_free(bmap1
);
2865 isl_basic_map_free(bmap2
);
2869 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2870 * A \cap B -> f(A) + f(B)
2872 struct isl_basic_map
*isl_basic_map_sum(
2873 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2875 unsigned n_in
, n_out
, nparam
, total
, pos
;
2876 struct isl_basic_map
*bmap
= NULL
;
2877 struct isl_dim_map
*dim_map1
, *dim_map2
;
2880 if (!bmap1
|| !bmap2
)
2883 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2886 nparam
= isl_basic_map_n_param(bmap1
);
2887 n_in
= isl_basic_map_n_in(bmap1
);
2888 n_out
= isl_basic_map_n_out(bmap1
);
2890 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2891 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2892 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2895 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2896 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2897 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2898 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2899 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2900 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2902 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2903 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2904 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2905 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2906 for (i
= 0; i
< n_out
; ++i
) {
2907 int j
= isl_basic_map_alloc_equality(bmap
);
2910 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2911 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2912 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2913 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2915 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2916 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2917 bmap
= add_divs(bmap
, 2 * n_out
);
2919 bmap
= isl_basic_map_simplify(bmap
);
2920 return isl_basic_map_finalize(bmap
);
2922 isl_basic_map_free(bmap
);
2923 isl_basic_map_free(bmap1
);
2924 isl_basic_map_free(bmap2
);
2928 /* Given two maps A -> f(A) and B -> g(B), construct a map
2929 * A \cap B -> f(A) + f(B)
2931 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2933 struct isl_map
*result
;
2939 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2941 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2942 map1
->n
* map2
->n
, 0);
2945 for (i
= 0; i
< map1
->n
; ++i
)
2946 for (j
= 0; j
< map2
->n
; ++j
) {
2947 struct isl_basic_map
*part
;
2948 part
= isl_basic_map_sum(
2949 isl_basic_map_copy(map1
->p
[i
]),
2950 isl_basic_map_copy(map2
->p
[j
]));
2951 if (isl_basic_map_is_empty(part
))
2952 isl_basic_map_free(part
);
2954 result
= isl_map_add_basic_map(result
, part
);
2967 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2968 __isl_take isl_set
*set2
)
2970 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2973 /* Given a basic map A -> f(A), construct A -> -f(A).
2975 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2980 bmap
= isl_basic_map_cow(bmap
);
2984 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2985 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2986 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2987 for (j
= 0; j
< n
; ++j
)
2988 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2989 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2990 for (j
= 0; j
< n
; ++j
)
2991 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2992 for (i
= 0; i
< bmap
->n_div
; ++i
)
2993 for (j
= 0; j
< n
; ++j
)
2994 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2995 return isl_basic_map_finalize(bmap
);
2998 /* Given a map A -> f(A), construct A -> -f(A).
3000 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3004 map
= isl_map_cow(map
);
3008 for (i
= 0; i
< map
->n
; ++i
) {
3009 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3020 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3022 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3025 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3026 * A -> floor(f(A)/d).
3028 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3031 unsigned n_in
, n_out
, nparam
, total
, pos
;
3032 struct isl_basic_map
*result
= NULL
;
3033 struct isl_dim_map
*dim_map
;
3039 nparam
= isl_basic_map_n_param(bmap
);
3040 n_in
= isl_basic_map_n_in(bmap
);
3041 n_out
= isl_basic_map_n_out(bmap
);
3043 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3044 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3045 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3046 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3047 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3048 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3050 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3051 bmap
->n_div
+ n_out
,
3052 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3053 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
3054 result
= add_divs(result
, n_out
);
3055 for (i
= 0; i
< n_out
; ++i
) {
3057 j
= isl_basic_map_alloc_inequality(result
);
3060 isl_seq_clr(result
->ineq
[j
], 1+total
);
3061 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3062 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3063 j
= isl_basic_map_alloc_inequality(result
);
3066 isl_seq_clr(result
->ineq
[j
], 1+total
);
3067 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3068 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3069 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3072 result
= isl_basic_map_simplify(result
);
3073 return isl_basic_map_finalize(result
);
3075 isl_basic_map_free(result
);
3079 /* Given a map A -> f(A) and an integer d, construct a map
3080 * A -> floor(f(A)/d).
3082 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3086 map
= isl_map_cow(map
);
3090 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3091 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3092 for (i
= 0; i
< map
->n
; ++i
) {
3093 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3104 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3110 i
= isl_basic_map_alloc_equality(bmap
);
3113 nparam
= isl_basic_map_n_param(bmap
);
3114 n_in
= isl_basic_map_n_in(bmap
);
3115 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3116 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3117 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3118 return isl_basic_map_finalize(bmap
);
3120 isl_basic_map_free(bmap
);
3124 /* Add a constraints to "bmap" expressing i_pos < o_pos
3126 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3132 i
= isl_basic_map_alloc_inequality(bmap
);
3135 nparam
= isl_basic_map_n_param(bmap
);
3136 n_in
= isl_basic_map_n_in(bmap
);
3137 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3138 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3139 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3140 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3141 return isl_basic_map_finalize(bmap
);
3143 isl_basic_map_free(bmap
);
3147 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3149 static __isl_give isl_basic_map
*var_less_or_equal(
3150 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3156 i
= isl_basic_map_alloc_inequality(bmap
);
3159 nparam
= isl_basic_map_n_param(bmap
);
3160 n_in
= isl_basic_map_n_in(bmap
);
3161 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3162 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3163 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3164 return isl_basic_map_finalize(bmap
);
3166 isl_basic_map_free(bmap
);
3170 /* Add a constraints to "bmap" expressing i_pos > o_pos
3172 static struct isl_basic_map
*var_more(struct 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
][0], -1);
3185 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3186 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3187 return isl_basic_map_finalize(bmap
);
3189 isl_basic_map_free(bmap
);
3193 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3195 static __isl_give isl_basic_map
*var_more_or_equal(
3196 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3202 i
= isl_basic_map_alloc_inequality(bmap
);
3205 nparam
= isl_basic_map_n_param(bmap
);
3206 n_in
= isl_basic_map_n_in(bmap
);
3207 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3208 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3209 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3210 return isl_basic_map_finalize(bmap
);
3212 isl_basic_map_free(bmap
);
3216 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3219 struct isl_basic_map
*bmap
;
3220 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3223 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3224 bmap
= var_equal(bmap
, i
);
3225 return isl_basic_map_finalize(bmap
);
3228 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3230 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3233 struct isl_basic_map
*bmap
;
3234 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3237 for (i
= 0; i
< pos
&& bmap
; ++i
)
3238 bmap
= var_equal(bmap
, i
);
3240 bmap
= var_less(bmap
, pos
);
3241 return isl_basic_map_finalize(bmap
);
3244 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3246 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3247 __isl_take isl_dim
*dim
, unsigned pos
)
3250 isl_basic_map
*bmap
;
3252 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3253 for (i
= 0; i
< pos
; ++i
)
3254 bmap
= var_equal(bmap
, i
);
3255 bmap
= var_less_or_equal(bmap
, pos
);
3256 return isl_basic_map_finalize(bmap
);
3259 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3261 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3264 struct isl_basic_map
*bmap
;
3265 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3268 for (i
= 0; i
< pos
&& bmap
; ++i
)
3269 bmap
= var_equal(bmap
, i
);
3271 bmap
= var_more(bmap
, pos
);
3272 return isl_basic_map_finalize(bmap
);
3275 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3277 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3278 __isl_take isl_dim
*dim
, unsigned pos
)
3281 isl_basic_map
*bmap
;
3283 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3284 for (i
= 0; i
< pos
; ++i
)
3285 bmap
= var_equal(bmap
, i
);
3286 bmap
= var_more_or_equal(bmap
, pos
);
3287 return isl_basic_map_finalize(bmap
);
3290 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3291 unsigned n
, int equal
)
3293 struct isl_map
*map
;
3296 if (n
== 0 && equal
)
3297 return isl_map_universe(dims
);
3299 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3301 for (i
= 0; i
+ 1 < n
; ++i
)
3302 map
= isl_map_add_basic_map(map
,
3303 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3306 map
= isl_map_add_basic_map(map
,
3307 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3309 map
= isl_map_add_basic_map(map
,
3310 isl_basic_map_less_at(dims
, n
- 1));
3317 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3321 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3324 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3326 return map_lex_lte_first(dim
, n
, 0);
3329 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3331 return map_lex_lte_first(dim
, n
, 1);
3334 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3336 return map_lex_lte(isl_dim_map(set_dim
), 0);
3339 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3341 return map_lex_lte(isl_dim_map(set_dim
), 1);
3344 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3345 unsigned n
, int equal
)
3347 struct isl_map
*map
;
3350 if (n
== 0 && equal
)
3351 return isl_map_universe(dims
);
3353 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3355 for (i
= 0; i
+ 1 < n
; ++i
)
3356 map
= isl_map_add_basic_map(map
,
3357 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3360 map
= isl_map_add_basic_map(map
,
3361 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3363 map
= isl_map_add_basic_map(map
,
3364 isl_basic_map_more_at(dims
, n
- 1));
3371 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3375 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3378 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3380 return map_lex_gte_first(dim
, n
, 0);
3383 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3385 return map_lex_gte_first(dim
, n
, 1);
3388 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3390 return map_lex_gte(isl_dim_map(set_dim
), 0);
3393 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3395 return map_lex_gte(isl_dim_map(set_dim
), 1);
3398 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3399 __isl_take isl_set
*set2
)
3402 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3403 map
= isl_map_intersect_domain(map
, set1
);
3404 map
= isl_map_intersect_range(map
, set2
);
3408 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3409 __isl_take isl_set
*set2
)
3412 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3413 map
= isl_map_intersect_domain(map
, set1
);
3414 map
= isl_map_intersect_range(map
, set2
);
3418 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3419 __isl_take isl_set
*set2
)
3422 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3423 map
= isl_map_intersect_domain(map
, set1
);
3424 map
= isl_map_intersect_range(map
, set2
);
3428 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3429 __isl_take isl_set
*set2
)
3432 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3433 map
= isl_map_intersect_domain(map
, set1
);
3434 map
= isl_map_intersect_range(map
, set2
);
3438 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3439 __isl_take isl_map
*map2
)
3442 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3443 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3444 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3448 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3449 __isl_take isl_map
*map2
)
3452 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3453 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3454 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3458 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3459 __isl_take isl_map
*map2
)
3462 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3463 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3464 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3468 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3469 __isl_take isl_map
*map2
)
3472 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3473 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3474 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3478 struct isl_basic_map
*isl_basic_map_from_basic_set(
3479 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3481 struct isl_basic_map
*bmap
;
3483 bset
= isl_basic_set_cow(bset
);
3487 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3488 isl_dim_free(bset
->dim
);
3489 bmap
= (struct isl_basic_map
*) bset
;
3491 return isl_basic_map_finalize(bmap
);
3493 isl_basic_set_free(bset
);
3498 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3502 if (bmap
->dim
->n_in
== 0)
3503 return (struct isl_basic_set
*)bmap
;
3504 bmap
= isl_basic_map_cow(bmap
);
3507 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3510 bmap
= isl_basic_map_finalize(bmap
);
3511 return (struct isl_basic_set
*)bmap
;
3513 isl_basic_map_free(bmap
);
3517 /* For a div d = floor(f/m), add the constraints
3520 * -(f-(n-1)) + m d >= 0
3522 * Note that the second constraint is the negation of
3526 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3527 unsigned pos
, isl_int
*div
)
3530 unsigned total
= isl_basic_map_total_dim(bmap
);
3532 i
= isl_basic_map_alloc_inequality(bmap
);
3535 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3536 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3538 j
= isl_basic_map_alloc_inequality(bmap
);
3541 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3542 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3543 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3547 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3548 unsigned pos
, isl_int
*div
)
3550 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3554 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3556 unsigned total
= isl_basic_map_total_dim(bmap
);
3557 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3559 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3563 struct isl_basic_set
*isl_basic_map_underlying_set(
3564 struct isl_basic_map
*bmap
)
3568 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3570 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3571 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3572 return (struct isl_basic_set
*)bmap
;
3573 bmap
= isl_basic_map_cow(bmap
);
3576 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3579 bmap
->extra
-= bmap
->n_div
;
3581 bmap
= isl_basic_map_finalize(bmap
);
3582 return (struct isl_basic_set
*)bmap
;
3587 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3588 __isl_take isl_basic_set
*bset
)
3590 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3593 struct isl_basic_map
*isl_basic_map_overlying_set(
3594 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3596 struct isl_basic_map
*bmap
;
3597 struct isl_ctx
*ctx
;
3604 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3605 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3606 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3608 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3609 isl_basic_map_free(like
);
3610 return (struct isl_basic_map
*)bset
;
3612 bset
= isl_basic_set_cow(bset
);
3615 total
= bset
->dim
->n_out
+ bset
->extra
;
3616 bmap
= (struct isl_basic_map
*)bset
;
3617 isl_dim_free(bmap
->dim
);
3618 bmap
->dim
= isl_dim_copy(like
->dim
);
3621 bmap
->n_div
= like
->n_div
;
3622 bmap
->extra
+= like
->n_div
;
3625 ltotal
= total
- bmap
->extra
+ like
->extra
;
3628 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3629 bmap
->extra
* (1 + 1 + total
));
3630 if (isl_blk_is_error(bmap
->block2
))
3632 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3636 for (i
= 0; i
< bmap
->extra
; ++i
)
3637 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3638 for (i
= 0; i
< like
->n_div
; ++i
) {
3639 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3640 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3642 bmap
= isl_basic_map_extend_constraints(bmap
,
3643 0, 2 * like
->n_div
);
3644 for (i
= 0; i
< like
->n_div
; ++i
) {
3645 if (isl_int_is_zero(bmap
->div
[i
][0]))
3647 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3651 isl_basic_map_free(like
);
3652 bmap
= isl_basic_map_simplify(bmap
);
3653 bmap
= isl_basic_map_finalize(bmap
);
3656 isl_basic_map_free(like
);
3657 isl_basic_set_free(bset
);
3661 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3662 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3664 return (struct isl_basic_set
*)
3665 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3668 struct isl_set
*isl_set_from_underlying_set(
3669 struct isl_set
*set
, struct isl_basic_set
*like
)
3675 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3677 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3678 isl_basic_set_free(like
);
3681 set
= isl_set_cow(set
);
3684 for (i
= 0; i
< set
->n
; ++i
) {
3685 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3686 isl_basic_set_copy(like
));
3690 isl_dim_free(set
->dim
);
3691 set
->dim
= isl_dim_copy(like
->dim
);
3694 isl_basic_set_free(like
);
3697 isl_basic_set_free(like
);
3702 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3706 map
= isl_map_cow(map
);
3709 map
->dim
= isl_dim_cow(map
->dim
);
3713 for (i
= 1; i
< map
->n
; ++i
)
3714 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3716 for (i
= 0; i
< map
->n
; ++i
) {
3717 map
->p
[i
] = (struct isl_basic_map
*)
3718 isl_basic_map_underlying_set(map
->p
[i
]);
3723 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3725 isl_dim_free(map
->dim
);
3726 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3730 return (struct isl_set
*)map
;
3736 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3738 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3741 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3742 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3744 bmap
= isl_basic_map_cow(bmap
);
3748 isl_dim_free(bmap
->dim
);
3751 bmap
= isl_basic_map_finalize(bmap
);
3755 isl_basic_map_free(bmap
);
3760 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3761 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3763 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3767 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3768 __isl_take isl_dim
*dim
)
3772 map
= isl_map_cow(map
);
3776 for (i
= 0; i
< map
->n
; ++i
) {
3777 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3782 isl_dim_free(map
->dim
);
3792 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3793 __isl_take isl_dim
*dim
)
3795 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3798 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3801 struct isl_basic_set
*domain
;
3807 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3809 n_in
= isl_basic_map_n_in(bmap
);
3810 n_out
= isl_basic_map_n_out(bmap
);
3811 domain
= isl_basic_set_from_basic_map(bmap
);
3812 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3814 domain
= isl_basic_set_reset_dim(domain
, dim
);
3819 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3821 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3824 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3825 __isl_take isl_basic_map
*bmap
)
3829 isl_basic_map
*domain
;
3830 isl_basic_set
*bset
;
3831 int nparam
, n_in
, n_out
;
3834 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3835 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3836 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3838 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3839 domain
= isl_basic_map_universe(dim
);
3841 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3842 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3843 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3845 total
= isl_basic_map_total_dim(bmap
);
3847 for (i
= 0; i
< n_in
; ++i
) {
3848 k
= isl_basic_map_alloc_equality(bmap
);
3851 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3852 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3853 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3856 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3857 return isl_basic_map_finalize(bmap
);
3859 isl_basic_map_free(bmap
);
3863 __isl_give isl_basic_map
*isl_basic_map_range_map(
3864 __isl_take isl_basic_map
*bmap
)
3868 isl_basic_map
*range
;
3869 isl_basic_set
*bset
;
3870 int nparam
, n_in
, n_out
;
3873 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3874 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3875 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3877 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3878 range
= isl_basic_map_universe(dim
);
3880 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3881 bmap
= isl_basic_map_apply_range(bmap
, range
);
3882 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3884 total
= isl_basic_map_total_dim(bmap
);
3886 for (i
= 0; i
< n_out
; ++i
) {
3887 k
= isl_basic_map_alloc_equality(bmap
);
3890 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3891 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3892 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3895 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3896 return isl_basic_map_finalize(bmap
);
3898 isl_basic_map_free(bmap
);
3902 struct isl_set
*isl_map_range(struct isl_map
*map
)
3905 struct isl_set
*set
;
3909 if (isl_map_dim(map
, isl_dim_in
) == 0)
3910 return (isl_set
*)map
;
3912 map
= isl_map_cow(map
);
3916 set
= (struct isl_set
*) map
;
3917 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3920 for (i
= 0; i
< map
->n
; ++i
) {
3921 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3925 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3926 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3933 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3936 isl_dim
*domain_dim
;
3938 map
= isl_map_cow(map
);
3942 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3943 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3944 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3947 for (i
= 0; i
< map
->n
; ++i
) {
3948 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3952 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3953 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3960 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3965 map
= isl_map_cow(map
);
3969 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3970 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3971 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3974 for (i
= 0; i
< map
->n
; ++i
) {
3975 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
3979 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3980 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3987 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3990 struct isl_map
*map
= NULL
;
3992 set
= isl_set_cow(set
);
3995 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3996 map
= (struct isl_map
*)set
;
3997 for (i
= 0; i
< set
->n
; ++i
) {
3998 map
->p
[i
] = isl_basic_map_from_basic_set(
3999 set
->p
[i
], isl_dim_copy(dim
));
4003 isl_dim_free(map
->dim
);
4012 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4013 __isl_take isl_basic_set
*bset
)
4015 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4018 __isl_give isl_basic_map
*isl_basic_map_from_range(
4019 __isl_take isl_basic_set
*bset
)
4021 return (isl_basic_map
*)bset
;
4024 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4026 return (struct isl_map
*)set
;
4029 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4031 return isl_map_reverse(isl_map_from_range(set
));
4034 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4035 __isl_take isl_set
*range
)
4037 return isl_map_apply_range(isl_map_from_domain(domain
),
4038 isl_map_from_range(range
));
4041 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4044 struct isl_set
*set
= NULL
;
4048 map
= isl_map_cow(map
);
4051 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4054 set
= (struct isl_set
*)map
;
4055 for (i
= 0; i
< map
->n
; ++i
) {
4056 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4066 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4068 struct isl_map
*map
;
4072 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4073 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4074 sizeof(struct isl_map
) +
4075 (n
- 1) * sizeof(struct isl_basic_map
*));
4079 map
->ctx
= dim
->ctx
;
4080 isl_ctx_ref(map
->ctx
);
4092 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4093 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4096 struct isl_map
*map
;
4097 struct isl_dim
*dims
;
4099 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4103 map
= isl_map_alloc_dim(dims
, n
, flags
);
4107 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4109 struct isl_basic_map
*bmap
;
4110 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4111 bmap
= isl_basic_map_set_to_empty(bmap
);
4115 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4117 struct isl_basic_set
*bset
;
4118 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4119 bset
= isl_basic_set_set_to_empty(bset
);
4123 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4125 struct isl_basic_map
*bmap
;
4128 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4129 bmap
= isl_basic_map_set_to_empty(bmap
);
4133 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4135 struct isl_basic_map
*bmap
;
4138 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4139 bmap
= isl_basic_map_set_to_empty(bmap
);
4143 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4145 struct isl_basic_set
*bset
;
4148 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4149 bset
= isl_basic_set_set_to_empty(bset
);
4153 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4155 struct isl_basic_map
*bmap
;
4156 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4157 bmap
= isl_basic_map_finalize(bmap
);
4161 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4163 struct isl_basic_set
*bset
;
4164 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4165 bset
= isl_basic_set_finalize(bset
);
4169 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4170 __isl_keep isl_basic_map
*model
)
4174 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4177 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4181 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4184 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4185 __isl_keep isl_set
*model
)
4189 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4192 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4194 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4197 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4201 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4204 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4208 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4211 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4213 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4216 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4220 return isl_set_empty(isl_dim_copy(model
->dim
));
4223 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4225 struct isl_map
*map
;
4228 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4229 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4233 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4235 struct isl_set
*set
;
4238 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4239 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4243 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4247 return isl_set_universe(isl_dim_copy(model
->dim
));
4250 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4253 struct isl_map
*dup
;
4257 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4258 for (i
= 0; i
< map
->n
; ++i
)
4259 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4263 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4264 __isl_take isl_basic_map
*bmap
)
4268 if (isl_basic_map_fast_is_empty(bmap
)) {
4269 isl_basic_map_free(bmap
);
4272 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4273 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4274 map
->p
[map
->n
] = bmap
;
4276 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4282 isl_basic_map_free(bmap
);
4286 void isl_map_free(struct isl_map
*map
)
4296 isl_ctx_deref(map
->ctx
);
4297 for (i
= 0; i
< map
->n
; ++i
)
4298 isl_basic_map_free(map
->p
[i
]);
4299 isl_dim_free(map
->dim
);
4303 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4304 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4308 base
= isl_map_cow(base
);
4312 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4315 for (i
= 0; i
< base
->n
; ++i
) {
4316 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4317 isl_dim_copy(base
->dim
), 0, 0, 0);
4327 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4328 unsigned nparam
, unsigned dim
)
4330 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4334 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4335 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4339 bmap
= isl_basic_map_cow(bmap
);
4340 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4341 j
= isl_basic_map_alloc_equality(bmap
);
4344 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4345 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4346 isl_int_set_si(bmap
->eq
[j
][0], value
);
4347 bmap
= isl_basic_map_simplify(bmap
);
4348 return isl_basic_map_finalize(bmap
);
4350 isl_basic_map_free(bmap
);
4354 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4355 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4359 bmap
= isl_basic_map_cow(bmap
);
4360 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4361 j
= isl_basic_map_alloc_equality(bmap
);
4364 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4365 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4366 isl_int_set(bmap
->eq
[j
][0], value
);
4367 bmap
= isl_basic_map_simplify(bmap
);
4368 return isl_basic_map_finalize(bmap
);
4370 isl_basic_map_free(bmap
);
4374 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4375 enum isl_dim_type type
, unsigned pos
, int value
)
4379 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4380 return isl_basic_map_fix_pos_si(bmap
,
4381 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4383 isl_basic_map_free(bmap
);
4387 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4388 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4392 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4393 return isl_basic_map_fix_pos(bmap
,
4394 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4396 isl_basic_map_free(bmap
);
4400 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4401 enum isl_dim_type type
, unsigned pos
, int value
)
4403 return (struct isl_basic_set
*)
4404 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4408 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4409 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4411 return (struct isl_basic_set
*)
4412 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4416 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4417 unsigned input
, int value
)
4419 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4422 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4423 unsigned dim
, int value
)
4425 return (struct isl_basic_set
*)
4426 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4427 isl_dim_set
, dim
, value
);
4430 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4431 enum isl_dim_type type
, unsigned pos
, int value
)
4435 map
= isl_map_cow(map
);
4439 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4440 for (i
= 0; i
< map
->n
; ++i
) {
4441 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4445 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4452 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4453 enum isl_dim_type type
, unsigned pos
, int value
)
4455 return (struct isl_set
*)
4456 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4459 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4460 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4464 map
= isl_map_cow(map
);
4468 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4469 for (i
= 0; i
< map
->n
; ++i
) {
4470 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4474 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4481 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4482 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4484 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4487 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4488 unsigned input
, int value
)
4490 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4493 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4495 return (struct isl_set
*)
4496 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4499 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4500 __isl_take isl_basic_map
*bmap
,
4501 enum isl_dim_type type
, unsigned pos
, int value
)
4507 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4508 pos
+= isl_basic_map_offset(bmap
, type
);
4509 bmap
= isl_basic_map_cow(bmap
);
4510 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4511 j
= isl_basic_map_alloc_inequality(bmap
);
4514 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4515 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4516 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4517 bmap
= isl_basic_map_simplify(bmap
);
4518 return isl_basic_map_finalize(bmap
);
4520 isl_basic_map_free(bmap
);
4524 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4525 unsigned dim
, isl_int value
)
4529 bset
= isl_basic_set_cow(bset
);
4530 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4531 j
= isl_basic_set_alloc_inequality(bset
);
4534 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4535 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4536 isl_int_neg(bset
->ineq
[j
][0], value
);
4537 bset
= isl_basic_set_simplify(bset
);
4538 return isl_basic_set_finalize(bset
);
4540 isl_basic_set_free(bset
);
4544 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4545 enum isl_dim_type type
, unsigned pos
, int value
)
4549 map
= isl_map_cow(map
);
4553 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4554 for (i
= 0; i
< map
->n
; ++i
) {
4555 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4560 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4567 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4568 enum isl_dim_type type
, unsigned pos
, int value
)
4570 return (struct isl_set
*)
4571 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4574 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4579 set
= isl_set_cow(set
);
4583 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4584 for (i
= 0; i
< set
->n
; ++i
) {
4585 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4595 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4599 map
= isl_map_cow(map
);
4603 map
->dim
= isl_dim_reverse(map
->dim
);
4606 for (i
= 0; i
< map
->n
; ++i
) {
4607 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4611 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4618 static struct isl_map
*isl_basic_map_partial_lexopt(
4619 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4620 struct isl_set
**empty
, int max
)
4624 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4625 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4627 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4629 isl_basic_map_free(bmap
);
4630 isl_basic_set_free(dom
);
4636 struct isl_map
*isl_basic_map_partial_lexmax(
4637 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4638 struct isl_set
**empty
)
4640 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4643 struct isl_map
*isl_basic_map_partial_lexmin(
4644 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4645 struct isl_set
**empty
)
4647 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4650 struct isl_set
*isl_basic_set_partial_lexmin(
4651 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4652 struct isl_set
**empty
)
4654 return (struct isl_set
*)
4655 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4659 struct isl_set
*isl_basic_set_partial_lexmax(
4660 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4661 struct isl_set
**empty
)
4663 return (struct isl_set
*)
4664 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4668 /* Given a basic map "bmap", compute the lexicographically minimal
4669 * (or maximal) image element for each domain element in dom.
4670 * Set *empty to those elements in dom that do not have an image element.
4672 * We first make sure the basic sets in dom are disjoint and then
4673 * simply collect the results over each of the basic sets separately.
4674 * We could probably improve the efficiency a bit by moving the union
4675 * domain down into the parametric integer programming.
4677 static __isl_give isl_map
*basic_map_partial_lexopt(
4678 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4679 __isl_give isl_set
**empty
, int max
)
4682 struct isl_map
*res
;
4684 dom
= isl_set_make_disjoint(dom
);
4688 if (isl_set_fast_is_empty(dom
)) {
4689 res
= isl_map_empty_like_basic_map(bmap
);
4690 *empty
= isl_set_empty_like(dom
);
4692 isl_basic_map_free(bmap
);
4696 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4697 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4699 for (i
= 1; i
< dom
->n
; ++i
) {
4700 struct isl_map
*res_i
;
4701 struct isl_set
*empty_i
;
4703 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4704 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4706 res
= isl_map_union_disjoint(res
, res_i
);
4707 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4711 isl_basic_map_free(bmap
);
4716 isl_basic_map_free(bmap
);
4720 /* Given a map "map", compute the lexicographically minimal
4721 * (or maximal) image element for each domain element in dom.
4722 * Set *empty to those elements in dom that do not have an image element.
4724 * We first compute the lexicographically minimal or maximal element
4725 * in the first basic map. This results in a partial solution "res"
4726 * and a subset "todo" of dom that still need to be handled.
4727 * We then consider each of the remaining maps in "map" and successively
4728 * improve both "res" and "todo".
4730 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4731 * Assume we are computing the lexicographical maximum.
4732 * We first compute the lexicographically maximal element in basic map i.
4733 * This results in a partial solution res_i and a subset todo_i.
4734 * Then we combine these results with those obtain for the first k basic maps
4735 * to obtain a result that is valid for the first k+1 basic maps.
4736 * In particular, the set where there is no solution is the set where
4737 * there is no solution for the first k basic maps and also no solution
4738 * for the ith basic map, i.e.,
4740 * todo^i = todo^k * todo_i
4742 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4743 * solutions, arbitrarily breaking ties in favor of res^k.
4744 * That is, when res^k(a) >= res_i(a), we pick res^k and
4745 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4746 * the lexicographic order.)
4747 * In practice, we compute
4749 * res^k * (res_i . "<=")
4753 * res_i * (res^k . "<")
4755 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4756 * where only one of res^k and res_i provides a solution and we simply pick
4763 * Note that we only compute these intersections when dom(res^k) intersects
4764 * dom(res_i). Otherwise, the only effect of these intersections is to
4765 * potentially break up res^k and res_i into smaller pieces.
4766 * We want to avoid such splintering as much as possible.
4767 * In fact, an earlier implementation of this function would look for
4768 * better results in the domain of res^k and for extra results in todo^k,
4769 * but this would always result in a splintering according to todo^k,
4770 * even when the domain of basic map i is disjoint from the domains of
4771 * the previous basic maps.
4773 static __isl_give isl_map
*isl_map_partial_lexopt(
4774 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4775 __isl_give isl_set
**empty
, int max
)
4778 struct isl_map
*res
;
4779 struct isl_set
*todo
;
4784 if (isl_map_fast_is_empty(map
)) {
4792 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4793 isl_set_copy(dom
), &todo
, max
);
4795 for (i
= 1; i
< map
->n
; ++i
) {
4799 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4801 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4802 isl_set_copy(dom
), &todo_i
, max
);
4805 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4806 le
= isl_map_lex_le(dim
);
4808 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4809 le
= isl_map_lex_ge(dim
);
4811 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4812 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4813 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4814 le
= isl_map_intersect(le
, isl_map_copy(res
));
4816 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4817 res
= isl_map_intersect_domain(res
,
4818 isl_set_copy(todo_i
));
4819 res_i
= isl_map_intersect_domain(res_i
,
4820 isl_set_copy(todo
));
4823 res
= isl_map_union_disjoint(res
, res_i
);
4824 res
= isl_map_union_disjoint(res
, lt
);
4825 res
= isl_map_union_disjoint(res
, le
);
4827 todo
= isl_set_intersect(todo
, todo_i
);
4847 __isl_give isl_map
*isl_map_partial_lexmax(
4848 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4849 __isl_give isl_set
**empty
)
4851 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4854 __isl_give isl_map
*isl_map_partial_lexmin(
4855 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4856 __isl_give isl_set
**empty
)
4858 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4861 __isl_give isl_set
*isl_set_partial_lexmin(
4862 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4863 __isl_give isl_set
**empty
)
4865 return (struct isl_set
*)
4866 isl_map_partial_lexmin((struct isl_map
*)set
,
4870 __isl_give isl_set
*isl_set_partial_lexmax(
4871 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4872 __isl_give isl_set
**empty
)
4874 return (struct isl_set
*)
4875 isl_map_partial_lexmax((struct isl_map
*)set
,
4879 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4881 struct isl_basic_set
*dom
= NULL
;
4882 struct isl_dim
*dom_dim
;
4886 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4887 dom
= isl_basic_set_universe(dom_dim
);
4888 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4890 isl_basic_map_free(bmap
);
4894 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4896 return isl_basic_map_lexopt(bmap
, 0);
4899 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4901 return isl_basic_map_lexopt(bmap
, 1);
4904 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4906 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4909 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4911 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4914 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4916 struct isl_set
*dom
= NULL
;
4917 struct isl_dim
*dom_dim
;
4921 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4922 dom
= isl_set_universe(dom_dim
);
4923 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4929 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4931 return isl_map_lexopt(map
, 0);
4934 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4936 return isl_map_lexopt(map
, 1);
4939 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4941 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4944 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4946 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4949 /* Apply a preimage specified by "mat" on the parameters of "bset".
4950 * bset is assumed to have only parameters and divs.
4952 static struct isl_basic_set
*basic_set_parameter_preimage(
4953 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4960 bset
->dim
= isl_dim_cow(bset
->dim
);
4964 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4966 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4968 bset
->dim
->nparam
= 0;
4969 bset
->dim
->n_out
= nparam
;
4970 bset
= isl_basic_set_preimage(bset
, mat
);
4972 bset
->dim
->nparam
= bset
->dim
->n_out
;
4973 bset
->dim
->n_out
= 0;
4978 isl_basic_set_free(bset
);
4982 /* Apply a preimage specified by "mat" on the parameters of "set".
4983 * set is assumed to have only parameters and divs.
4985 static struct isl_set
*set_parameter_preimage(
4986 struct isl_set
*set
, struct isl_mat
*mat
)
4988 struct isl_dim
*dim
= NULL
;
4994 dim
= isl_dim_copy(set
->dim
);
4995 dim
= isl_dim_cow(dim
);
4999 nparam
= isl_set_dim(set
, isl_dim_param
);
5001 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5004 dim
->n_out
= nparam
;
5005 isl_set_reset_dim(set
, dim
);
5006 set
= isl_set_preimage(set
, mat
);
5009 dim
= isl_dim_copy(set
->dim
);
5010 dim
= isl_dim_cow(dim
);
5013 dim
->nparam
= dim
->n_out
;
5015 isl_set_reset_dim(set
, dim
);
5025 /* Intersect the basic set "bset" with the affine space specified by the
5026 * equalities in "eq".
5028 static struct isl_basic_set
*basic_set_append_equalities(
5029 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5037 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5042 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5043 for (i
= 0; i
< eq
->n_row
; ++i
) {
5044 k
= isl_basic_set_alloc_equality(bset
);
5047 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5048 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5052 bset
= isl_basic_set_gauss(bset
, NULL
);
5053 bset
= isl_basic_set_finalize(bset
);
5058 isl_basic_set_free(bset
);
5062 /* Intersect the set "set" with the affine space specified by the
5063 * equalities in "eq".
5065 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5073 for (i
= 0; i
< set
->n
; ++i
) {
5074 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5087 /* Project the given basic set onto its parameter domain, possibly introducing
5088 * new, explicit, existential variables in the constraints.
5089 * The input has parameters and (possibly implicit) existential variables.
5090 * The output has the same parameters, but only
5091 * explicit existentially quantified variables.
5093 * The actual projection is performed by pip, but pip doesn't seem
5094 * to like equalities very much, so we first remove the equalities
5095 * among the parameters by performing a variable compression on
5096 * the parameters. Afterward, an inverse transformation is performed
5097 * and the equalities among the parameters are inserted back in.
5099 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5103 struct isl_mat
*T
, *T2
;
5104 struct isl_set
*set
;
5105 unsigned nparam
, n_div
;
5107 bset
= isl_basic_set_cow(bset
);
5111 if (bset
->n_eq
== 0)
5112 return isl_basic_set_lexmin(bset
);
5114 isl_basic_set_gauss(bset
, NULL
);
5116 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5117 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5119 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5120 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5123 if (i
== bset
->n_eq
)
5124 return isl_basic_set_lexmin(bset
);
5126 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5128 eq
= isl_mat_cow(eq
);
5129 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5130 if (T
&& T
->n_col
== 0) {
5134 bset
= isl_basic_set_set_to_empty(bset
);
5135 return isl_set_from_basic_set(bset
);
5137 bset
= basic_set_parameter_preimage(bset
, T
);
5139 set
= isl_basic_set_lexmin(bset
);
5140 set
= set_parameter_preimage(set
, T2
);
5141 set
= set_append_equalities(set
, eq
);
5145 /* Compute an explicit representation for all the existentially
5146 * quantified variables.
5147 * The input and output dimensions are first turned into parameters.
5148 * compute_divs then returns a map with the same parameters and
5149 * no input or output dimensions and the dimension specification
5150 * is reset to that of the input.
5152 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5154 struct isl_basic_set
*bset
;
5155 struct isl_set
*set
;
5156 struct isl_map
*map
;
5157 struct isl_dim
*dim
, *orig_dim
= NULL
;
5162 bmap
= isl_basic_map_cow(bmap
);
5166 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5167 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5168 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5169 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5173 orig_dim
= bmap
->dim
;
5175 bset
= (struct isl_basic_set
*)bmap
;
5177 set
= parameter_compute_divs(bset
);
5178 map
= (struct isl_map
*)set
;
5179 map
= isl_map_reset_dim(map
, orig_dim
);
5183 isl_basic_map_free(bmap
);
5187 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5195 off
= isl_dim_total(bmap
->dim
);
5196 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5197 if (isl_int_is_zero(bmap
->div
[i
][0]))
5199 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5205 static int map_divs_known(__isl_keep isl_map
*map
)
5212 for (i
= 0; i
< map
->n
; ++i
) {
5213 int known
= basic_map_divs_known(map
->p
[i
]);
5221 /* If bmap contains any unknown divs, then compute explicit
5222 * expressions for them. However, this computation may be
5223 * quite expensive, so first try to remove divs that aren't
5226 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5230 struct isl_map
*map
;
5232 known
= basic_map_divs_known(bmap
);
5236 return isl_map_from_basic_map(bmap
);
5238 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5240 known
= basic_map_divs_known(bmap
);
5244 return isl_map_from_basic_map(bmap
);
5246 map
= compute_divs(bmap
);
5249 isl_basic_map_free(bmap
);
5253 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5257 struct isl_map
*res
;
5264 known
= map_divs_known(map
);
5272 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5273 for (i
= 1 ; i
< map
->n
; ++i
) {
5275 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5276 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5277 res
= isl_map_union_disjoint(res
, r2
);
5279 res
= isl_map_union(res
, r2
);
5286 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5288 return (struct isl_set
*)
5289 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5292 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5294 return (struct isl_set
*)
5295 isl_map_compute_divs((struct isl_map
*)set
);
5298 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5301 struct isl_set
*set
;
5306 map
= isl_map_cow(map
);
5310 set
= (struct isl_set
*)map
;
5311 set
->dim
= isl_dim_domain(set
->dim
);
5314 for (i
= 0; i
< map
->n
; ++i
) {
5315 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5319 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5320 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5327 struct isl_map
*isl_map_union_disjoint(
5328 struct isl_map
*map1
, struct isl_map
*map2
)
5332 struct isl_map
*map
= NULL
;
5346 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5348 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5349 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5350 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5352 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5353 map1
->n
+ map2
->n
, flags
);
5356 for (i
= 0; i
< map1
->n
; ++i
) {
5357 map
= isl_map_add_basic_map(map
,
5358 isl_basic_map_copy(map1
->p
[i
]));
5362 for (i
= 0; i
< map2
->n
; ++i
) {
5363 map
= isl_map_add_basic_map(map
,
5364 isl_basic_map_copy(map2
->p
[i
]));
5378 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5380 map1
= isl_map_union_disjoint(map1
, map2
);
5384 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5388 struct isl_set
*isl_set_union_disjoint(
5389 struct isl_set
*set1
, struct isl_set
*set2
)
5391 return (struct isl_set
*)
5392 isl_map_union_disjoint(
5393 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5396 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5398 return (struct isl_set
*)
5399 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5402 struct isl_map
*isl_map_intersect_range(
5403 struct isl_map
*map
, struct isl_set
*set
)
5406 struct isl_map
*result
;
5412 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5413 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5414 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5416 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5417 map
->n
* set
->n
, flags
);
5420 for (i
= 0; i
< map
->n
; ++i
)
5421 for (j
= 0; j
< set
->n
; ++j
) {
5422 result
= isl_map_add_basic_map(result
,
5423 isl_basic_map_intersect_range(
5424 isl_basic_map_copy(map
->p
[i
]),
5425 isl_basic_set_copy(set
->p
[j
])));
5438 struct isl_map
*isl_map_intersect_domain(
5439 struct isl_map
*map
, struct isl_set
*set
)
5441 return isl_map_reverse(
5442 isl_map_intersect_range(isl_map_reverse(map
), set
));
5445 struct isl_map
*isl_map_apply_domain(
5446 struct isl_map
*map1
, struct isl_map
*map2
)
5450 map1
= isl_map_reverse(map1
);
5451 map1
= isl_map_apply_range(map1
, map2
);
5452 return isl_map_reverse(map1
);
5459 struct isl_map
*isl_map_apply_range(
5460 struct isl_map
*map1
, struct isl_map
*map2
)
5462 struct isl_dim
*dim_result
;
5463 struct isl_map
*result
;
5469 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5470 isl_dim_copy(map2
->dim
));
5472 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5475 for (i
= 0; i
< map1
->n
; ++i
)
5476 for (j
= 0; j
< map2
->n
; ++j
) {
5477 result
= isl_map_add_basic_map(result
,
5478 isl_basic_map_apply_range(
5479 isl_basic_map_copy(map1
->p
[i
]),
5480 isl_basic_map_copy(map2
->p
[j
])));
5486 if (result
&& result
->n
<= 1)
5487 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5496 * returns range - domain
5498 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5500 isl_dim
*dims
, *target_dim
;
5501 struct isl_basic_set
*bset
;
5508 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5509 bmap
->dim
, isl_dim_out
),
5511 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5512 dim
= isl_basic_map_n_in(bmap
);
5513 nparam
= isl_basic_map_n_param(bmap
);
5514 bset
= isl_basic_set_from_basic_map(bmap
);
5515 bset
= isl_basic_set_cow(bset
);
5516 dims
= isl_basic_set_get_dim(bset
);
5517 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5518 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5519 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5520 for (i
= 0; i
< dim
; ++i
) {
5521 int j
= isl_basic_map_alloc_equality(
5522 (struct isl_basic_map
*)bset
);
5525 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5526 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5527 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5528 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5530 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5531 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5534 isl_basic_map_free(bmap
);
5539 * returns range - domain
5541 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5545 struct isl_set
*result
;
5550 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5551 map
->dim
, isl_dim_out
),
5553 dim
= isl_map_get_dim(map
);
5554 dim
= isl_dim_domain(dim
);
5555 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5558 for (i
= 0; i
< map
->n
; ++i
)
5559 result
= isl_set_add_basic_set(result
,
5560 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5568 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5570 struct isl_basic_map
*bmap
;
5578 nparam
= dims
->nparam
;
5580 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5584 for (i
= 0; i
< dim
; ++i
) {
5585 int j
= isl_basic_map_alloc_equality(bmap
);
5588 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5589 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5590 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5592 return isl_basic_map_finalize(bmap
);
5594 isl_basic_map_free(bmap
);
5598 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5600 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5603 return basic_map_identity(dim
);
5606 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5608 if (!model
|| !model
->dim
)
5610 isl_assert(model
->ctx
,
5611 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5612 return basic_map_identity(isl_dim_copy(model
->dim
));
5615 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5617 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5618 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5621 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5623 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5626 return map_identity(dim
);
5629 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5631 if (!model
|| !model
->dim
)
5633 isl_assert(model
->ctx
,
5634 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5635 return map_identity(isl_dim_copy(model
->dim
));
5638 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5640 if (!model
|| !model
->dim
)
5642 isl_assert(model
->ctx
,
5643 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5644 return map_identity(isl_dim_copy(model
->dim
));
5647 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5649 isl_dim
*dim
= isl_set_get_dim(set
);
5651 id
= isl_map_identity(dim
);
5652 return isl_map_intersect_range(id
, set
);
5655 /* Construct a basic set with all set dimensions having only non-negative
5658 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5663 struct isl_basic_set
*bset
;
5667 nparam
= dims
->nparam
;
5669 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5672 for (i
= 0; i
< dim
; ++i
) {
5673 int k
= isl_basic_set_alloc_inequality(bset
);
5676 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5677 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5681 isl_basic_set_free(bset
);
5685 /* Construct the half-space x_pos >= 0.
5687 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5691 isl_basic_set
*nonneg
;
5693 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5694 k
= isl_basic_set_alloc_inequality(nonneg
);
5697 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5698 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5700 return isl_basic_set_finalize(nonneg
);
5702 isl_basic_set_free(nonneg
);
5706 /* Construct the half-space x_pos <= -1.
5708 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5713 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5714 k
= isl_basic_set_alloc_inequality(neg
);
5717 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5718 isl_int_set_si(neg
->ineq
[k
][0], -1);
5719 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5721 return isl_basic_set_finalize(neg
);
5723 isl_basic_set_free(neg
);
5727 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5728 enum isl_dim_type type
, unsigned first
, unsigned n
)
5731 isl_basic_set
*nonneg
;
5739 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5741 for (i
= 0; i
< n
; ++i
) {
5742 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5743 pos(set
->dim
, type
) + first
+ i
);
5744 neg
= neg_halfspace(isl_set_get_dim(set
),
5745 pos(set
->dim
, type
) + first
+ i
);
5747 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5756 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5757 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5764 if (isl_set_fast_is_empty(set
)) {
5769 return fn(set
, signs
, user
);
5772 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5774 half
= isl_set_intersect(half
, isl_set_copy(set
));
5775 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5779 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5781 half
= isl_set_intersect(half
, set
);
5782 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5788 /* Call "fn" on the intersections of "set" with each of the orthants
5789 * (except for obviously empty intersections). The orthant is identified
5790 * by the signs array, with each entry having value 1 or -1 according
5791 * to the sign of the corresponding variable.
5793 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5794 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5804 if (isl_set_fast_is_empty(set
))
5807 nparam
= isl_set_dim(set
, isl_dim_param
);
5808 nvar
= isl_set_dim(set
, isl_dim_set
);
5810 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5812 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5820 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5822 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5825 int isl_basic_map_is_subset(
5826 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5829 struct isl_map
*map1
;
5830 struct isl_map
*map2
;
5832 if (!bmap1
|| !bmap2
)
5835 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5836 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5838 is_subset
= isl_map_is_subset(map1
, map2
);
5846 int isl_basic_map_is_equal(
5847 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5851 if (!bmap1
|| !bmap2
)
5853 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5856 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5860 int isl_basic_set_is_equal(
5861 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5863 return isl_basic_map_is_equal(
5864 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5867 int isl_map_is_empty(struct isl_map
*map
)
5874 for (i
= 0; i
< map
->n
; ++i
) {
5875 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5884 int isl_map_fast_is_empty(struct isl_map
*map
)
5886 return map
? map
->n
== 0 : -1;
5889 int isl_set_fast_is_empty(struct isl_set
*set
)
5891 return set
? set
->n
== 0 : -1;
5894 int isl_set_is_empty(struct isl_set
*set
)
5896 return isl_map_is_empty((struct isl_map
*)set
);
5899 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5904 return isl_dim_equal(map1
->dim
, map2
->dim
);
5907 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5912 return isl_dim_equal(set1
->dim
, set2
->dim
);
5915 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5921 is_subset
= isl_map_is_subset(map1
, map2
);
5924 is_subset
= isl_map_is_subset(map2
, map1
);
5928 int isl_basic_map_is_strict_subset(
5929 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5933 if (!bmap1
|| !bmap2
)
5935 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5938 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5939 if (is_subset
== -1)
5944 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5950 is_subset
= isl_map_is_subset(map1
, map2
);
5953 is_subset
= isl_map_is_subset(map2
, map1
);
5954 if (is_subset
== -1)
5959 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5961 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5964 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5968 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5971 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5975 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5978 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5985 for (i
= 0; i
< map
->n
; ++i
) {
5986 int r
= isl_basic_map_is_universe(map
->p
[i
]);
5994 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5996 return isl_map_fast_is_universe((isl_map
*) set
);
5999 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6001 struct isl_basic_set
*bset
= NULL
;
6002 struct isl_vec
*sample
= NULL
;
6009 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6012 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6013 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6014 copy
= isl_basic_map_remove_redundancies(copy
);
6015 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6016 isl_basic_map_free(copy
);
6020 total
= 1 + isl_basic_map_total_dim(bmap
);
6021 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6022 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6028 isl_vec_free(bmap
->sample
);
6029 bmap
->sample
= NULL
;
6030 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6033 sample
= isl_basic_set_sample_vec(bset
);
6036 empty
= sample
->size
== 0;
6037 isl_vec_free(bmap
->sample
);
6038 bmap
->sample
= sample
;
6040 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6045 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6049 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6052 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6056 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6059 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6061 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6064 struct isl_map
*isl_basic_map_union(
6065 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6067 struct isl_map
*map
;
6068 if (!bmap1
|| !bmap2
)
6071 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6073 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6076 map
= isl_map_add_basic_map(map
, bmap1
);
6077 map
= isl_map_add_basic_map(map
, bmap2
);
6080 isl_basic_map_free(bmap1
);
6081 isl_basic_map_free(bmap2
);
6085 struct isl_set
*isl_basic_set_union(
6086 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6088 return (struct isl_set
*)isl_basic_map_union(
6089 (struct isl_basic_map
*)bset1
,
6090 (struct isl_basic_map
*)bset2
);
6093 /* Order divs such that any div only depends on previous divs */
6094 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6102 off
= isl_dim_total(bmap
->dim
);
6104 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6106 if (isl_int_is_zero(bmap
->div
[i
][0]))
6108 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6112 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6118 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6120 return (struct isl_basic_set
*)
6121 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6124 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6131 for (i
= 0; i
< map
->n
; ++i
) {
6132 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6143 /* Look for a div in dst that corresponds to the div "div" in src.
6144 * The divs before "div" in src and dst are assumed to be the same.
6146 * Returns -1 if no corresponding div was found and the position
6147 * of the corresponding div in dst otherwise.
6149 static int find_div(struct isl_basic_map
*dst
,
6150 struct isl_basic_map
*src
, unsigned div
)
6154 unsigned total
= isl_dim_total(src
->dim
);
6156 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6157 for (i
= div
; i
< dst
->n_div
; ++i
)
6158 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6159 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6160 dst
->n_div
- div
) == -1)
6165 struct isl_basic_map
*isl_basic_map_align_divs(
6166 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6169 unsigned total
= isl_dim_total(src
->dim
);
6174 if (src
->n_div
== 0)
6177 for (i
= 0; i
< src
->n_div
; ++i
)
6178 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6180 src
= isl_basic_map_order_divs(src
);
6181 dst
= isl_basic_map_cow(dst
);
6182 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6183 src
->n_div
, 0, 2 * src
->n_div
);
6186 for (i
= 0; i
< src
->n_div
; ++i
) {
6187 int j
= find_div(dst
, src
, i
);
6189 j
= isl_basic_map_alloc_div(dst
);
6192 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6193 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6194 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6198 isl_basic_map_swap_div(dst
, i
, j
);
6202 isl_basic_map_free(dst
);
6206 struct isl_basic_set
*isl_basic_set_align_divs(
6207 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6209 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6210 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6213 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6221 map
= isl_map_compute_divs(map
);
6222 map
= isl_map_cow(map
);
6226 for (i
= 1; i
< map
->n
; ++i
)
6227 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6228 for (i
= 1; i
< map
->n
; ++i
)
6229 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6231 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6235 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6237 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6240 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6244 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6245 map
= isl_map_intersect_domain(map
, set
);
6246 set
= isl_map_range(map
);
6254 /* There is no need to cow as removing empty parts doesn't change
6255 * the meaning of the set.
6257 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6264 for (i
= map
->n
-1; i
>= 0; --i
) {
6265 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6267 isl_basic_map_free(map
->p
[i
]);
6268 if (i
!= map
->n
-1) {
6269 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6270 map
->p
[i
] = map
->p
[map
->n
-1];
6278 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6280 return (struct isl_set
*)
6281 isl_map_remove_empty_parts((struct isl_map
*)set
);
6284 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6286 struct isl_basic_map
*bmap
;
6287 if (!map
|| map
->n
== 0)
6289 bmap
= map
->p
[map
->n
-1];
6290 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6291 return isl_basic_map_copy(bmap
);
6294 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6296 return (struct isl_basic_set
*)
6297 isl_map_copy_basic_map((struct isl_map
*)set
);
6300 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6301 __isl_keep isl_basic_map
*bmap
)
6307 for (i
= map
->n
-1; i
>= 0; --i
) {
6308 if (map
->p
[i
] != bmap
)
6310 map
= isl_map_cow(map
);
6313 isl_basic_map_free(map
->p
[i
]);
6314 if (i
!= map
->n
-1) {
6315 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6316 map
->p
[i
] = map
->p
[map
->n
-1];
6327 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6328 struct isl_basic_set
*bset
)
6330 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6331 (struct isl_basic_map
*)bset
);
6334 /* Given two basic sets bset1 and bset2, compute the maximal difference
6335 * between the values of dimension pos in bset1 and those in bset2
6336 * for any common value of the parameters and dimensions preceding pos.
6338 static enum isl_lp_result
basic_set_maximal_difference_at(
6339 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6340 int pos
, isl_int
*opt
)
6342 struct isl_dim
*dims
;
6343 struct isl_basic_map
*bmap1
= NULL
;
6344 struct isl_basic_map
*bmap2
= NULL
;
6345 struct isl_ctx
*ctx
;
6346 struct isl_vec
*obj
;
6349 unsigned dim1
, dim2
;
6350 enum isl_lp_result res
;
6352 if (!bset1
|| !bset2
)
6353 return isl_lp_error
;
6355 nparam
= isl_basic_set_n_param(bset1
);
6356 dim1
= isl_basic_set_n_dim(bset1
);
6357 dim2
= isl_basic_set_n_dim(bset2
);
6358 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6359 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6360 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6361 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6362 if (!bmap1
|| !bmap2
)
6364 bmap1
= isl_basic_map_cow(bmap1
);
6365 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6366 pos
, (dim1
- pos
) + (dim2
- pos
),
6367 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6368 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6371 total
= isl_basic_map_total_dim(bmap1
);
6373 obj
= isl_vec_alloc(ctx
, 1 + total
);
6374 isl_seq_clr(obj
->block
.data
, 1 + total
);
6375 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6376 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6379 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6381 isl_basic_map_free(bmap1
);
6385 isl_basic_map_free(bmap1
);
6386 isl_basic_map_free(bmap2
);
6387 return isl_lp_error
;
6390 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6391 * for any common value of the parameters and dimensions preceding pos
6392 * in both basic sets, the values of dimension pos in bset1 are
6393 * smaller or larger than those in bset2.
6396 * 1 if bset1 follows bset2
6397 * -1 if bset1 precedes bset2
6398 * 0 if bset1 and bset2 are incomparable
6399 * -2 if some error occurred.
6401 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6402 struct isl_basic_set
*bset2
, int pos
)
6405 enum isl_lp_result res
;
6410 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6412 if (res
== isl_lp_empty
)
6414 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6415 res
== isl_lp_unbounded
)
6417 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6426 /* Given two basic sets bset1 and bset2, check whether
6427 * for any common value of the parameters and dimensions preceding pos
6428 * there is a value of dimension pos in bset1 that is larger
6429 * than a value of the same dimension in bset2.
6432 * 1 if there exists such a pair
6433 * 0 if there is no such pair, but there is a pair of equal values
6435 * -2 if some error occurred.
6437 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6438 __isl_keep isl_basic_set
*bset2
, int pos
)
6441 enum isl_lp_result res
;
6446 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6448 if (res
== isl_lp_empty
)
6450 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6451 res
== isl_lp_unbounded
)
6453 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6455 else if (res
== isl_lp_ok
)
6464 /* Given two sets set1 and set2, check whether
6465 * for any common value of the parameters and dimensions preceding pos
6466 * there is a value of dimension pos in set1 that is larger
6467 * than a value of the same dimension in set2.
6470 * 1 if there exists such a pair
6471 * 0 if there is no such pair, but there is a pair of equal values
6473 * -2 if some error occurred.
6475 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6476 __isl_keep isl_set
*set2
, int pos
)
6484 for (i
= 0; i
< set1
->n
; ++i
)
6485 for (j
= 0; j
< set2
->n
; ++j
) {
6487 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6488 if (f
== 1 || f
== -2)
6497 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6498 unsigned pos
, isl_int
*val
)
6506 total
= isl_basic_map_total_dim(bmap
);
6507 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6508 for (; d
+1 > pos
; --d
)
6509 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6513 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6515 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6517 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6520 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6526 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6527 unsigned pos
, isl_int
*val
)
6539 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6542 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6543 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6544 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6545 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6549 isl_int_set(*val
, v
);
6555 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6556 unsigned pos
, isl_int
*val
)
6558 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6562 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6565 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6568 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6569 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6571 if (pos
>= isl_basic_map_dim(bmap
, type
))
6573 return isl_basic_map_fast_has_fixed_var(bmap
,
6574 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6577 int isl_map_fast_is_fixed(struct isl_map
*map
,
6578 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6580 if (pos
>= isl_map_dim(map
, type
))
6582 return isl_map_fast_has_fixed_var(map
,
6583 map_offset(map
, type
) - 1 + pos
, val
);
6586 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6587 * then return this fixed value in *val.
6589 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6592 return isl_basic_set_fast_has_fixed_var(bset
,
6593 isl_basic_set_n_param(bset
) + dim
, val
);
6596 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6597 * then return this fixed value in *val.
6599 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6601 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6604 /* Check if input variable in has fixed value and if so and if val is not NULL,
6605 * then return this fixed value in *val.
6607 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6609 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6612 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6613 * and if val is not NULL, then return this lower bound in *val.
6615 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6616 unsigned dim
, isl_int
*val
)
6618 int i
, i_eq
= -1, i_ineq
= -1;
6625 total
= isl_basic_set_total_dim(bset
);
6626 nparam
= isl_basic_set_n_param(bset
);
6627 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6628 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6634 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6635 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6637 if (i_eq
!= -1 || i_ineq
!= -1)
6641 if (i_eq
== -1 && i_ineq
== -1)
6643 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6644 /* The coefficient should always be one due to normalization. */
6645 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6647 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6649 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6650 total
- nparam
- dim
- 1) != -1)
6653 isl_int_neg(*val
, c
[0]);
6657 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6658 unsigned dim
, isl_int
*val
)
6670 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6674 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6676 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6677 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6679 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6683 isl_int_set(*val
, v
);
6694 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6696 const struct constraint
*c1
= (const struct constraint
*)p1
;
6697 const struct constraint
*c2
= (const struct constraint
*)p2
;
6698 unsigned size
= isl_min(c1
->size
, c2
->size
);
6699 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6702 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6703 struct isl_basic_map
*bmap
)
6706 struct constraint
*c
;
6711 total
= isl_basic_map_total_dim(bmap
);
6712 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6715 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6717 c
[i
].c
= bmap
->ineq
[i
];
6719 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6720 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6721 bmap
->ineq
[i
] = c
[i
].c
;
6725 isl_basic_map_free(bmap
);
6729 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6730 __isl_take isl_basic_set
*bset
)
6732 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6733 (struct isl_basic_map
*)bset
);
6736 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6740 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6742 bmap
= isl_basic_map_remove_redundancies(bmap
);
6743 bmap
= isl_basic_map_sort_constraints(bmap
);
6744 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6748 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6750 return (struct isl_basic_set
*)isl_basic_map_normalize(
6751 (struct isl_basic_map
*)bset
);
6754 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6755 const __isl_keep isl_basic_map
*bmap2
)
6762 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6763 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6764 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6765 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6766 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6767 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6768 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6769 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6771 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6773 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6775 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6776 return bmap1
->n_eq
- bmap2
->n_eq
;
6777 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6778 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6779 if (bmap1
->n_div
!= bmap2
->n_div
)
6780 return bmap1
->n_div
- bmap2
->n_div
;
6781 total
= isl_basic_map_total_dim(bmap1
);
6782 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6783 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6787 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6788 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6792 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6793 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6800 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6801 __isl_keep isl_basic_map
*bmap2
)
6803 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6806 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6807 __isl_keep isl_basic_set
*bset2
)
6809 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6810 (isl_basic_map
*)bset2
);
6813 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6815 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6816 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6818 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6821 /* We normalize in place, but if anything goes wrong we need
6822 * to return NULL, so we need to make sure we don't change the
6823 * meaning of any possible other copies of map.
6825 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6828 struct isl_basic_map
*bmap
;
6832 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6834 for (i
= 0; i
< map
->n
; ++i
) {
6835 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6838 isl_basic_map_free(map
->p
[i
]);
6841 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6842 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6843 map
= isl_map_remove_empty_parts(map
);
6846 for (i
= map
->n
- 1; i
>= 1; --i
) {
6847 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6849 isl_basic_map_free(map
->p
[i
-1]);
6850 for (j
= i
; j
< map
->n
; ++j
)
6851 map
->p
[j
-1] = map
->p
[j
];
6861 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6863 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6866 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6876 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6879 map1
= isl_map_copy(map1
);
6880 map2
= isl_map_copy(map2
);
6881 map1
= isl_map_normalize(map1
);
6882 map2
= isl_map_normalize(map2
);
6885 equal
= map1
->n
== map2
->n
;
6886 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6887 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6900 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6902 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6903 (struct isl_map
*)set2
);
6906 /* Return an interval that ranges from min to max (inclusive)
6908 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6909 isl_int min
, isl_int max
)
6912 struct isl_basic_set
*bset
= NULL
;
6914 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6918 k
= isl_basic_set_alloc_inequality(bset
);
6921 isl_int_set_si(bset
->ineq
[k
][1], 1);
6922 isl_int_neg(bset
->ineq
[k
][0], min
);
6924 k
= isl_basic_set_alloc_inequality(bset
);
6927 isl_int_set_si(bset
->ineq
[k
][1], -1);
6928 isl_int_set(bset
->ineq
[k
][0], max
);
6932 isl_basic_set_free(bset
);
6936 /* Return the Cartesian product of the basic sets in list (in the given order).
6938 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6946 struct isl_basic_set
*product
= NULL
;
6950 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6951 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6952 nparam
= isl_basic_set_n_param(list
->p
[0]);
6953 dim
= isl_basic_set_n_dim(list
->p
[0]);
6954 extra
= list
->p
[0]->n_div
;
6955 n_eq
= list
->p
[0]->n_eq
;
6956 n_ineq
= list
->p
[0]->n_ineq
;
6957 for (i
= 1; i
< list
->n
; ++i
) {
6958 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6959 isl_assert(list
->ctx
,
6960 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6961 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6962 extra
+= list
->p
[i
]->n_div
;
6963 n_eq
+= list
->p
[i
]->n_eq
;
6964 n_ineq
+= list
->p
[i
]->n_ineq
;
6966 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6971 for (i
= 0; i
< list
->n
; ++i
) {
6972 isl_basic_set_add_constraints(product
,
6973 isl_basic_set_copy(list
->p
[i
]), dim
);
6974 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6976 isl_basic_set_list_free(list
);
6979 isl_basic_set_free(product
);
6980 isl_basic_set_list_free(list
);
6984 struct isl_basic_map
*isl_basic_map_product(
6985 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6987 struct isl_dim
*dim_result
= NULL
;
6988 struct isl_basic_map
*bmap
;
6989 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6990 struct isl_dim_map
*dim_map1
, *dim_map2
;
6992 if (!bmap1
|| !bmap2
)
6995 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6996 bmap2
->dim
, isl_dim_param
), goto error
);
6997 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6998 isl_dim_copy(bmap2
->dim
));
7000 in1
= isl_basic_map_n_in(bmap1
);
7001 in2
= isl_basic_map_n_in(bmap2
);
7002 out1
= isl_basic_map_n_out(bmap1
);
7003 out2
= isl_basic_map_n_out(bmap2
);
7004 nparam
= isl_basic_map_n_param(bmap1
);
7006 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7007 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7008 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7009 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7010 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7011 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7012 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7013 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7014 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7015 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7016 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7018 bmap
= isl_basic_map_alloc_dim(dim_result
,
7019 bmap1
->n_div
+ bmap2
->n_div
,
7020 bmap1
->n_eq
+ bmap2
->n_eq
,
7021 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7022 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7023 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7024 bmap
= isl_basic_map_simplify(bmap
);
7025 return isl_basic_map_finalize(bmap
);
7027 isl_basic_map_free(bmap1
);
7028 isl_basic_map_free(bmap2
);
7032 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7034 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7037 struct isl_map
*result
;
7043 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7044 map2
->dim
, isl_dim_param
), goto error
);
7046 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7047 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7048 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7050 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
7051 isl_dim_copy(map2
->dim
)),
7052 map1
->n
* map2
->n
, flags
);
7055 for (i
= 0; i
< map1
->n
; ++i
)
7056 for (j
= 0; j
< map2
->n
; ++j
) {
7057 struct isl_basic_map
*part
;
7058 part
= isl_basic_map_product(
7059 isl_basic_map_copy(map1
->p
[i
]),
7060 isl_basic_map_copy(map2
->p
[j
]));
7061 if (isl_basic_map_is_empty(part
))
7062 isl_basic_map_free(part
);
7064 result
= isl_map_add_basic_map(result
, part
);
7077 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7079 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7080 __isl_take isl_map
*map2
)
7084 prod
= isl_map_product(map1
, map2
);
7085 prod
= isl_map_flatten(prod
);
7089 /* Given two set A and B, construct its Cartesian product A x B.
7091 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7093 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7094 (struct isl_map
*)set2
);
7097 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7098 __isl_take isl_set
*set2
)
7100 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7103 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7106 uint32_t hash
= isl_hash_init();
7111 bmap
= isl_basic_map_copy(bmap
);
7112 bmap
= isl_basic_map_normalize(bmap
);
7115 total
= isl_basic_map_total_dim(bmap
);
7116 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7117 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7119 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7120 isl_hash_hash(hash
, c_hash
);
7122 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7123 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7125 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7126 isl_hash_hash(hash
, c_hash
);
7128 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7129 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7131 if (isl_int_is_zero(bmap
->div
[i
][0]))
7133 isl_hash_byte(hash
, i
& 0xFF);
7134 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7135 isl_hash_hash(hash
, c_hash
);
7137 isl_basic_map_free(bmap
);
7141 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7143 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7146 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7153 map
= isl_map_copy(map
);
7154 map
= isl_map_normalize(map
);
7158 hash
= isl_hash_init();
7159 for (i
= 0; i
< map
->n
; ++i
) {
7161 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7162 isl_hash_hash(hash
, bmap_hash
);
7170 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7172 return isl_map_get_hash((isl_map
*)set
);
7175 /* Check if the value for dimension dim is completely determined
7176 * by the values of the other parameters and variables.
7177 * That is, check if dimension dim is involved in an equality.
7179 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7186 nparam
= isl_basic_set_n_param(bset
);
7187 for (i
= 0; i
< bset
->n_eq
; ++i
)
7188 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7193 /* Check if the value for dimension dim is completely determined
7194 * by the values of the other parameters and variables.
7195 * That is, check if dimension dim is involved in an equality
7196 * for each of the subsets.
7198 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7204 for (i
= 0; i
< set
->n
; ++i
) {
7206 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7213 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7215 return set
? set
->n
: 0;
7218 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7219 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7226 for (i
= 0; i
< map
->n
; ++i
)
7227 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7233 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7234 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7241 for (i
= 0; i
< set
->n
; ++i
)
7242 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7248 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7250 struct isl_dim
*dim
;
7255 if (bset
->n_div
== 0)
7258 bset
= isl_basic_set_cow(bset
);
7262 dim
= isl_basic_set_get_dim(bset
);
7263 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
7266 isl_dim_free(bset
->dim
);
7268 bset
->extra
-= bset
->n_div
;
7271 bset
= isl_basic_set_finalize(bset
);
7275 isl_basic_set_free(bset
);
7279 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7282 struct isl_dim
*dim
;
7285 set
= isl_set_align_divs(set
);
7289 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
7292 set
= isl_set_cow(set
);
7296 n_div
= set
->p
[0]->n_div
;
7297 dim
= isl_set_get_dim(set
);
7298 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
7301 isl_dim_free(set
->dim
);
7304 for (i
= 0; i
< set
->n
; ++i
) {
7305 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7316 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7318 struct isl_dim
*dim
;
7319 struct isl_basic_map
*bmap
;
7326 set
= isl_set_align_divs(set
);
7331 dim
= isl_set_get_dim(set
);
7332 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7334 return isl_map_identity(dim
);
7337 n_div
= set
->p
[0]->n_div
;
7338 dim
= isl_dim_map(dim
);
7339 n_param
= isl_dim_size(dim
, isl_dim_param
);
7340 n_set
= isl_dim_size(dim
, isl_dim_in
);
7341 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7342 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7343 for (i
= 0; i
< n_set
; ++i
)
7344 bmap
= var_equal(bmap
, i
);
7346 total
= n_param
+ n_set
+ n_set
+ n_div
;
7347 for (i
= 0; i
< n_div
; ++i
) {
7348 k
= isl_basic_map_alloc_inequality(bmap
);
7351 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7352 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7353 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7354 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7355 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7356 set
->p
[0]->div
[i
][0]);
7358 l
= isl_basic_map_alloc_inequality(bmap
);
7361 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7362 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7363 set
->p
[0]->div
[i
][0]);
7364 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7368 return isl_map_from_basic_map(bmap
);
7371 isl_basic_map_free(bmap
);
7375 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7383 dim
= isl_basic_set_total_dim(bset
);
7384 size
+= bset
->n_eq
* (1 + dim
);
7385 size
+= bset
->n_ineq
* (1 + dim
);
7386 size
+= bset
->n_div
* (2 + dim
);
7391 int isl_set_size(__isl_keep isl_set
*set
)
7399 for (i
= 0; i
< set
->n
; ++i
)
7400 size
+= isl_basic_set_size(set
->p
[i
]);
7405 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7406 enum isl_dim_type type
, unsigned pos
)
7414 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7416 pos
+= isl_basic_map_offset(bmap
, type
);
7418 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7419 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7423 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7424 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7431 return lower
&& upper
;
7434 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7435 enum isl_dim_type type
, unsigned pos
)
7442 for (i
= 0; i
< map
->n
; ++i
) {
7444 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7445 if (bounded
< 0 || !bounded
)
7452 /* Return 1 if the specified dim is involved in both an upper bound
7453 * and a lower bound.
7455 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7456 enum isl_dim_type type
, unsigned pos
)
7458 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7461 /* For each of the "n" variables starting at "first", determine
7462 * the sign of the variable and put the results in the first "n"
7463 * elements of the array "signs".
7465 * 1 means that the variable is non-negative
7466 * -1 means that the variable is non-positive
7467 * 0 means the variable attains both positive and negative values.
7469 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7470 unsigned first
, unsigned n
, int *signs
)
7472 isl_vec
*bound
= NULL
;
7473 struct isl_tab
*tab
= NULL
;
7474 struct isl_tab_undo
*snap
;
7477 if (!bset
|| !signs
)
7480 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7481 tab
= isl_tab_from_basic_set(bset
);
7485 isl_seq_clr(bound
->el
, bound
->size
);
7486 isl_int_set_si(bound
->el
[0], -1);
7488 snap
= isl_tab_snap(tab
);
7489 for (i
= 0; i
< n
; ++i
) {
7492 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7493 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7496 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7497 if (isl_tab_rollback(tab
, snap
) < 0)
7505 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7506 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7509 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7510 if (isl_tab_rollback(tab
, snap
) < 0)
7513 signs
[i
] = empty
? -1 : 0;
7517 isl_vec_free(bound
);
7521 isl_vec_free(bound
);
7525 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7526 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7528 if (!bset
|| !signs
)
7530 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7533 first
+= pos(bset
->dim
, type
) - 1;
7534 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7537 /* Check if the given basic map is obviously single-valued.
7538 * In particular, for each output dimension, check that there is
7539 * an equality that defines the output dimension in terms of
7540 * earlier dimensions.
7542 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7552 total
= 1 + isl_basic_map_total_dim(bmap
);
7553 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7554 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7556 for (i
= 0; i
< n_out
; ++i
) {
7557 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7558 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7560 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7561 total
- (o_out
+ i
+ 1)) == -1)
7564 if (j
>= bmap
->n_eq
)
7571 /* Check if the given map is obviously single-valued.
7573 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7584 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7587 /* Check if the given map is single-valued.
7592 * and check if the result is a subset of the identity mapping.
7594 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7600 sv
= isl_map_fast_is_single_valued(map
);
7604 test
= isl_map_reverse(isl_map_copy(map
));
7605 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7607 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
7609 sv
= isl_map_is_subset(test
, id
);
7617 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7621 sv
= isl_map_is_single_valued(map
);
7625 map
= isl_map_copy(map
);
7626 map
= isl_map_reverse(map
);
7627 sv
= isl_map_is_single_valued(map
);
7633 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7635 return isl_map_is_single_valued((isl_map
*)set
);
7638 int isl_map_is_translation(__isl_keep isl_map
*map
)
7643 delta
= isl_map_deltas(isl_map_copy(map
));
7644 ok
= isl_set_is_singleton(delta
);
7645 isl_set_free(delta
);
7650 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7652 if (isl_seq_first_non_zero(p
, pos
) != -1)
7654 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7659 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7668 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7671 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7672 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7673 for (j
= 0; j
< nvar
; ++j
) {
7674 int lower
= 0, upper
= 0;
7675 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7676 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7678 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7684 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7685 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7687 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7689 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7694 if (!lower
|| !upper
)
7701 int isl_set_is_box(__isl_keep isl_set
*set
)
7708 return isl_basic_set_is_box(set
->p
[0]);
7711 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7716 return isl_dim_is_wrapping(bset
->dim
);
7719 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7724 return isl_dim_is_wrapping(set
->dim
);
7727 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7729 bmap
= isl_basic_map_cow(bmap
);
7733 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7737 bmap
= isl_basic_map_finalize(bmap
);
7739 return (isl_basic_set
*)bmap
;
7741 isl_basic_map_free(bmap
);
7745 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7749 map
= isl_map_cow(map
);
7753 for (i
= 0; i
< map
->n
; ++i
) {
7754 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7758 map
->dim
= isl_dim_wrap(map
->dim
);
7762 return (isl_set
*)map
;
7768 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7770 bset
= isl_basic_set_cow(bset
);
7774 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7778 bset
= isl_basic_set_finalize(bset
);
7780 return (isl_basic_map
*)bset
;
7782 isl_basic_set_free(bset
);
7786 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7793 if (!isl_set_is_wrapping(set
))
7794 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7797 set
= isl_set_cow(set
);
7801 for (i
= 0; i
< set
->n
; ++i
) {
7802 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7807 set
->dim
= isl_dim_unwrap(set
->dim
);
7811 return (isl_map
*)set
;
7817 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7818 enum isl_dim_type type
)
7823 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7826 bmap
= isl_basic_map_cow(bmap
);
7830 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7834 bmap
= isl_basic_map_finalize(bmap
);
7838 isl_basic_map_free(bmap
);
7842 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7843 enum isl_dim_type type
)
7850 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7853 map
= isl_map_cow(map
);
7857 for (i
= 0; i
< map
->n
; ++i
) {
7858 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7862 map
->dim
= isl_dim_reset(map
->dim
, type
);
7872 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7877 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7880 bmap
= isl_basic_map_cow(bmap
);
7884 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
7888 bmap
= isl_basic_map_finalize(bmap
);
7892 isl_basic_map_free(bmap
);
7896 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
7898 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
7901 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
7908 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
7911 map
= isl_map_cow(map
);
7915 for (i
= 0; i
< map
->n
; ++i
) {
7916 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
7920 map
->dim
= isl_dim_flatten(map
->dim
);
7930 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
7932 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
7935 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
7937 isl_dim
*dim
, *flat_dim
;
7940 dim
= isl_set_get_dim(set
);
7941 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
7942 map
= map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
7943 map
= isl_map_intersect_domain(map
, set
);
7948 /* Extend the given dim_map with mappings for the divs in bmap.
7950 static __isl_give
struct isl_dim_map
*extend_dim_map(
7951 __isl_keep
struct isl_dim_map
*dim_map
,
7952 __isl_keep isl_basic_map
*bmap
)
7955 struct isl_dim_map
*res
;
7958 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
7960 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
7964 for (i
= 0; i
< dim_map
->len
; ++i
)
7965 res
->pos
[i
] = dim_map
->pos
[i
];
7966 for (i
= 0; i
< bmap
->n_div
; ++i
)
7967 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
7972 /* Extract a dim_map from a reordering.
7973 * We essentially need to reverse the mapping, and add an offset
7974 * of 1 for the constant term.
7976 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
7977 __isl_keep isl_reordering
*exp
)
7980 struct isl_dim_map
*dim_map
;
7985 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
7989 for (i
= 0; i
< exp
->len
; ++i
)
7990 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
7995 /* Reorder the dimensions of "bmap" according to the given dim_map
7996 * and set the dimension specification to "dim".
7998 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
7999 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8003 bmap
= isl_basic_map_cow(bmap
);
8004 if (!bmap
|| !dim
|| !dim_map
)
8007 res
= isl_basic_map_alloc_dim(dim
,
8008 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8009 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
8010 res
= isl_basic_map_finalize(res
);
8014 isl_basic_map_free(bmap
);
8019 /* Reorder the dimensions of "map" according to given reordering.
8021 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8022 __isl_take isl_reordering
*r
)
8025 struct isl_dim_map
*dim_map
;
8027 map
= isl_map_cow(map
);
8028 dim_map
= isl_dim_map_from_reordering(r
);
8029 if (!map
|| !r
|| !dim_map
)
8032 for (i
= 0; i
< map
->n
; ++i
) {
8033 struct isl_dim_map
*dim_map_i
;
8035 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
8037 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8038 isl_dim_copy(r
->dim
), dim_map_i
);
8044 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8046 isl_reordering_free(r
);
8052 isl_reordering_free(r
);
8056 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8057 __isl_take isl_reordering
*r
)
8059 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8062 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8063 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8064 enum isl_dim_type c2
, enum isl_dim_type c3
,
8065 enum isl_dim_type c4
, enum isl_dim_type c5
)
8067 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8068 struct isl_mat
*mat
;
8074 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8075 isl_basic_map_total_dim(bmap
) + 1);
8078 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8079 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8080 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8081 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8082 isl_int_set(mat
->row
[i
][pos
],
8083 bmap
->eq
[i
][off
+ k
]);
8091 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8092 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8093 enum isl_dim_type c2
, enum isl_dim_type c3
,
8094 enum isl_dim_type c4
, enum isl_dim_type c5
)
8096 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8097 struct isl_mat
*mat
;
8103 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8104 isl_basic_map_total_dim(bmap
) + 1);
8107 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8108 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8109 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8110 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8111 isl_int_set(mat
->row
[i
][pos
],
8112 bmap
->ineq
[i
][off
+ k
]);
8120 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8121 __isl_take isl_dim
*dim
,
8122 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8123 enum isl_dim_type c2
, enum isl_dim_type c3
,
8124 enum isl_dim_type c4
, enum isl_dim_type c5
)
8126 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8127 isl_basic_map
*bmap
;
8133 if (!dim
|| !eq
|| !ineq
)
8136 if (eq
->n_col
!= ineq
->n_col
)
8137 isl_die(dim
->ctx
, isl_error_invalid
,
8138 "equalities and inequalities matrices should have "
8139 "same number of columns", goto error
);
8141 total
= 1 + isl_dim_total(dim
);
8143 if (eq
->n_col
< total
)
8144 isl_die(dim
->ctx
, isl_error_invalid
,
8145 "number of columns too small", goto error
);
8147 extra
= eq
->n_col
- total
;
8149 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8150 eq
->n_row
, ineq
->n_row
);
8153 for (i
= 0; i
< extra
; ++i
) {
8154 k
= isl_basic_map_alloc_div(bmap
);
8157 isl_int_set_si(bmap
->div
[k
][0], 0);
8159 for (i
= 0; i
< eq
->n_row
; ++i
) {
8160 l
= isl_basic_map_alloc_equality(bmap
);
8163 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8164 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8165 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8166 isl_int_set(bmap
->eq
[l
][off
+ k
],
8172 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8173 l
= isl_basic_map_alloc_inequality(bmap
);
8176 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8177 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8178 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8179 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8198 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8199 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8200 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8202 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8203 c1
, c2
, c3
, c4
, isl_dim_in
);
8206 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8207 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8208 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8210 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8211 c1
, c2
, c3
, c4
, isl_dim_in
);
8214 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8215 __isl_take isl_dim
*dim
,
8216 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8217 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8219 return (isl_basic_set
*)
8220 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8221 c1
, c2
, c3
, c4
, isl_dim_in
);