2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
38 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
41 struct isl_dim_map
*dim_map
;
42 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
43 sizeof(struct isl_dim_map
) + len
* sizeof(int));
46 dim_map
->len
= 1 + len
;
48 for (i
= 0; i
< len
; ++i
)
49 dim_map
->pos
[1 + i
] = -1;
53 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
56 case isl_dim_param
: return dim
->nparam
;
57 case isl_dim_in
: return dim
->n_in
;
58 case isl_dim_out
: return dim
->n_out
;
59 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
64 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return 1;
68 case isl_dim_in
: return 1 + dim
->nparam
;
69 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
74 static void isl_dim_map_dim_range(struct isl_dim_map
*dim_map
,
75 struct isl_dim
*dim
, enum isl_dim_type type
,
76 unsigned first
, unsigned n
, unsigned dst_pos
)
84 src_pos
= pos(dim
, type
);
85 for (i
= 0; i
< n
; ++i
)
86 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
89 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
90 enum isl_dim_type type
, unsigned dst_pos
)
92 isl_dim_map_dim_range(dim_map
, dim
, type
, 0, n(dim
, type
), dst_pos
);
95 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
96 struct isl_basic_map
*bmap
, unsigned dst_pos
)
101 if (!dim_map
|| !bmap
)
104 src_pos
= 1 + isl_dim_total(bmap
->dim
);
105 for (i
= 0; i
< bmap
->n_div
; ++i
)
106 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
109 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
113 for (i
= 0; i
< dim_map
->len
; ++i
)
114 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
115 fprintf(stderr
, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
119 enum isl_dim_type type
)
124 case isl_dim_cst
: return 1;
127 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
128 case isl_dim_div
: return bmap
->n_div
;
129 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
134 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
136 return map
? n(map
->dim
, type
) : 0;
139 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
141 return set
? n(set
->dim
, type
) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
145 enum isl_dim_type type
)
147 struct isl_dim
*dim
= bmap
->dim
;
149 case isl_dim_cst
: return 0;
150 case isl_dim_param
: return 1;
151 case isl_dim_in
: return 1 + dim
->nparam
;
152 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
153 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
158 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
160 return pos(map
->dim
, type
);
163 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
164 enum isl_dim_type type
)
166 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
171 return isl_basic_set_dim(bset
, isl_dim_set
);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
176 return isl_basic_set_dim(bset
, isl_dim_param
);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
181 return isl_dim_total(bset
->dim
) + bset
->n_div
;
184 unsigned isl_set_n_dim(const struct isl_set
*set
)
186 return isl_set_dim(set
, isl_dim_set
);
189 unsigned isl_set_n_param(const struct isl_set
*set
)
191 return isl_set_dim(set
, isl_dim_param
);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
196 return bmap
? bmap
->dim
->n_in
: 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
201 return bmap
? bmap
->dim
->n_out
: 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
206 return bmap
? bmap
->dim
->nparam
: 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
211 return bmap
? bmap
->n_div
: 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
216 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
219 unsigned isl_map_n_in(const struct isl_map
*map
)
221 return map
->dim
->n_in
;
224 unsigned isl_map_n_out(const struct isl_map
*map
)
226 return map
->dim
->n_out
;
229 unsigned isl_map_n_param(const struct isl_map
*map
)
231 return map
->dim
->nparam
;
234 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
239 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
242 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
245 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
246 struct isl_basic_set
*bset
)
251 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
254 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
257 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
258 struct isl_basic_set
*bset
)
263 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
266 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
269 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
271 return bmap
? bmap
->ctx
: NULL
;
274 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
276 return bset
? bset
->ctx
: NULL
;
279 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
281 return map
? map
->ctx
: NULL
;
284 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
286 return set
? set
->ctx
: NULL
;
289 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
293 return isl_dim_copy(bmap
->dim
);
296 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
300 return isl_dim_copy(bset
->dim
);
303 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
307 return isl_dim_copy(map
->dim
);
310 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
314 return isl_dim_copy(set
->dim
);
317 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
320 bmap
= isl_basic_map_cow(bmap
);
323 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
326 bmap
= isl_basic_map_finalize(bmap
);
329 isl_basic_map_free(bmap
);
333 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
334 enum isl_dim_type type
)
336 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
339 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
340 enum isl_dim_type type
, const char *s
)
344 map
= isl_map_cow(map
);
348 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
352 for (i
= 0; i
< map
->n
; ++i
) {
353 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
364 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
365 enum isl_dim_type type
)
367 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
370 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
373 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
376 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
378 return bset
? isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
381 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
383 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
386 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
387 enum isl_dim_type type
, unsigned pos
)
389 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
392 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
393 enum isl_dim_type type
, unsigned pos
)
395 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
398 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
399 enum isl_dim_type type
, unsigned pos
)
401 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
404 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
405 __isl_take isl_basic_map
*bmap
,
406 enum isl_dim_type type
, unsigned pos
, const char *s
)
410 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
415 isl_basic_map_free(bmap
);
419 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
420 enum isl_dim_type type
, unsigned pos
, const char *s
)
427 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
431 for (i
= 0; i
< map
->n
; ++i
) {
432 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
443 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
444 __isl_take isl_basic_set
*bset
,
445 enum isl_dim_type type
, unsigned pos
, const char *s
)
447 return (isl_basic_set
*)isl_basic_map_set_dim_name(
448 (isl_basic_map
*)bset
, type
, pos
, s
);
451 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
452 enum isl_dim_type type
, unsigned pos
, const char *s
)
454 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
457 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
461 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
464 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
465 struct isl_basic_map
*bmap
, unsigned extra
,
466 unsigned n_eq
, unsigned n_ineq
)
469 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
474 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
475 if (isl_blk_is_error(bmap
->block
))
478 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
483 bmap
->block2
= isl_blk_empty();
486 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
487 if (isl_blk_is_error(bmap
->block2
))
490 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
495 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
496 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
498 for (i
= 0; i
< extra
; ++i
)
499 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
503 bmap
->c_size
= n_eq
+ n_ineq
;
504 bmap
->eq
= bmap
->ineq
+ n_ineq
;
513 isl_basic_map_free(bmap
);
517 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
518 unsigned nparam
, unsigned dim
, unsigned extra
,
519 unsigned n_eq
, unsigned n_ineq
)
521 struct isl_basic_map
*bmap
;
522 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
523 return (struct isl_basic_set
*)bmap
;
526 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
527 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
529 struct isl_basic_map
*bmap
;
532 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
533 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
534 return (struct isl_basic_set
*)bmap
;
540 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
541 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
543 struct isl_basic_map
*bmap
;
547 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
552 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
558 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
559 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
560 unsigned n_eq
, unsigned n_ineq
)
562 struct isl_basic_map
*bmap
;
565 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
569 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
573 static void dup_constraints(
574 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
577 unsigned total
= isl_basic_map_total_dim(src
);
579 for (i
= 0; i
< src
->n_eq
; ++i
) {
580 int j
= isl_basic_map_alloc_equality(dst
);
581 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
584 for (i
= 0; i
< src
->n_ineq
; ++i
) {
585 int j
= isl_basic_map_alloc_inequality(dst
);
586 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
589 for (i
= 0; i
< src
->n_div
; ++i
) {
590 int j
= isl_basic_map_alloc_div(dst
);
591 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
593 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
596 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
598 struct isl_basic_map
*dup
;
602 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
603 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
606 dup_constraints(dup
, bmap
);
607 dup
->flags
= bmap
->flags
;
608 dup
->sample
= isl_vec_copy(bmap
->sample
);
612 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
614 struct isl_basic_map
*dup
;
616 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
617 return (struct isl_basic_set
*)dup
;
620 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
625 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
629 return isl_basic_set_dup(bset
);
632 struct isl_set
*isl_set_copy(struct isl_set
*set
)
641 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
646 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
650 bmap
= isl_basic_map_dup(bmap
);
652 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
656 struct isl_map
*isl_map_copy(struct isl_map
*map
)
665 void isl_basic_map_free(struct isl_basic_map
*bmap
)
673 isl_ctx_deref(bmap
->ctx
);
675 isl_blk_free(bmap
->ctx
, bmap
->block2
);
677 isl_blk_free(bmap
->ctx
, bmap
->block
);
678 isl_vec_free(bmap
->sample
);
679 isl_dim_free(bmap
->dim
);
683 void isl_basic_set_free(struct isl_basic_set
*bset
)
685 isl_basic_map_free((struct isl_basic_map
*)bset
);
688 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
690 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
693 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
699 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
700 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
702 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
703 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
704 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
705 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
706 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
707 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
709 int j
= isl_basic_map_alloc_inequality(bmap
);
713 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
714 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
721 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
722 bmap
->extra
- bmap
->n_div
);
726 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
728 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
731 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
735 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
740 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
742 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
745 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
750 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
752 if (pos
!= bmap
->n_eq
- 1) {
754 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
755 bmap
->eq
[bmap
->n_eq
- 1] = t
;
761 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
763 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
766 void isl_basic_map_inequality_to_equality(
767 struct isl_basic_map
*bmap
, unsigned pos
)
772 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
773 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
778 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
779 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
780 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
781 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
784 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
786 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
789 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
795 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
796 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
797 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
798 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
799 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
800 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
801 1 + isl_basic_map_total_dim(bmap
),
802 bmap
->extra
- bmap
->n_div
);
803 return bmap
->n_ineq
++;
806 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
808 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
811 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
815 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
820 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
822 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
825 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
830 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
832 if (pos
!= bmap
->n_ineq
- 1) {
834 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
835 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
836 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
842 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
844 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
847 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
852 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
855 k
= isl_basic_map_alloc_equality(bmap
);
858 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
861 isl_basic_map_free(bmap
);
865 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
868 return (isl_basic_set
*)
869 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
872 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
877 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
880 k
= isl_basic_map_alloc_inequality(bmap
);
883 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
886 isl_basic_map_free(bmap
);
890 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
893 return (isl_basic_set
*)
894 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
897 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
901 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
902 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
903 1 + 1 + isl_basic_map_total_dim(bmap
),
904 bmap
->extra
- bmap
->n_div
);
905 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
906 return bmap
->n_div
++;
909 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
911 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
914 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
918 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
923 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
925 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
928 /* Copy constraint from src to dst, putting the vars of src at offset
929 * dim_off in dst and the divs of src at offset div_off in dst.
930 * If both sets are actually map, then dim_off applies to the input
933 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
934 struct isl_basic_map
*src_map
, isl_int
*src
,
935 unsigned in_off
, unsigned out_off
, unsigned div_off
)
937 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
938 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
939 unsigned src_in
= isl_basic_map_n_in(src_map
);
940 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
941 unsigned src_out
= isl_basic_map_n_out(src_map
);
942 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
943 isl_int_set(dst
[0], src
[0]);
944 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
945 if (dst_nparam
> src_nparam
)
946 isl_seq_clr(dst
+1+src_nparam
,
947 dst_nparam
- src_nparam
);
948 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
949 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
951 isl_min(dst_in
-in_off
, src_in
));
952 if (dst_in
-in_off
> src_in
)
953 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
954 dst_in
- in_off
- src_in
);
955 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
956 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
957 src
+1+src_nparam
+src_in
,
958 isl_min(dst_out
-out_off
, src_out
));
959 if (dst_out
-out_off
> src_out
)
960 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
961 dst_out
- out_off
- src_out
);
962 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
963 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
964 src
+1+src_nparam
+src_in
+src_out
,
965 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
966 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
967 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
968 div_off
+src_map
->n_div
,
969 dst_map
->n_div
- div_off
- src_map
->n_div
);
972 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
973 struct isl_basic_map
*src_map
, isl_int
*src
,
974 unsigned in_off
, unsigned out_off
, unsigned div_off
)
976 isl_int_set(dst
[0], src
[0]);
977 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
980 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
981 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
986 if (!bmap1
|| !bmap2
)
989 div_off
= bmap1
->n_div
;
991 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
992 int i1
= isl_basic_map_alloc_equality(bmap1
);
995 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
996 i_pos
, o_pos
, div_off
);
999 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1000 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1003 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1004 i_pos
, o_pos
, div_off
);
1007 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1008 int i1
= isl_basic_map_alloc_div(bmap1
);
1011 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1012 i_pos
, o_pos
, div_off
);
1015 isl_basic_map_free(bmap2
);
1020 isl_basic_map_free(bmap1
);
1021 isl_basic_map_free(bmap2
);
1025 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
1026 struct isl_dim_map
*dim_map
)
1030 for (i
= 0; i
< dim_map
->len
; ++i
) {
1031 if (dim_map
->pos
[i
] < 0)
1032 isl_int_set_si(dst
[i
], 0);
1034 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
1038 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
1039 struct isl_dim_map
*dim_map
)
1041 isl_int_set(dst
[0], src
[0]);
1042 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
1045 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
1046 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
1050 if (!src
|| !dst
|| !dim_map
)
1053 for (i
= 0; i
< src
->n_eq
; ++i
) {
1054 int i1
= isl_basic_map_alloc_equality(dst
);
1057 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
1060 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1061 int i1
= isl_basic_map_alloc_inequality(dst
);
1064 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1067 for (i
= 0; i
< src
->n_div
; ++i
) {
1068 int i1
= isl_basic_map_alloc_div(dst
);
1071 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1075 isl_basic_map_free(src
);
1080 isl_basic_map_free(src
);
1081 isl_basic_map_free(dst
);
1085 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1086 struct isl_basic_set
*bset2
, unsigned pos
)
1088 return (struct isl_basic_set
*)
1089 add_constraints((struct isl_basic_map
*)bset1
,
1090 (struct isl_basic_map
*)bset2
, 0, pos
);
1093 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1094 struct isl_dim
*dim
, unsigned extra
,
1095 unsigned n_eq
, unsigned n_ineq
)
1097 struct isl_basic_map
*ext
;
1107 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1108 base
->extra
>= base
->n_div
+ extra
;
1110 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1111 room_for_ineq(base
, n_ineq
)) {
1116 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1117 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1118 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1119 extra
+= base
->extra
;
1121 n_ineq
+= base
->n_ineq
;
1123 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1129 ext
->sample
= isl_vec_copy(base
->sample
);
1130 flags
= base
->flags
;
1131 ext
= add_constraints(ext
, base
, 0, 0);
1134 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1141 isl_basic_map_free(base
);
1145 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1146 struct isl_dim
*dim
, unsigned extra
,
1147 unsigned n_eq
, unsigned n_ineq
)
1149 return (struct isl_basic_set
*)
1150 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1151 extra
, n_eq
, n_ineq
);
1154 struct isl_basic_map
*isl_basic_map_extend_constraints(
1155 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1159 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1163 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1164 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1165 unsigned n_eq
, unsigned n_ineq
)
1167 struct isl_basic_map
*bmap
;
1168 struct isl_dim
*dim
;
1172 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1176 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1179 isl_basic_map_free(base
);
1183 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1184 unsigned nparam
, unsigned dim
, unsigned extra
,
1185 unsigned n_eq
, unsigned n_ineq
)
1187 return (struct isl_basic_set
*)
1188 isl_basic_map_extend((struct isl_basic_map
*)base
,
1189 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1192 struct isl_basic_set
*isl_basic_set_extend_constraints(
1193 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1195 return (struct isl_basic_set
*)
1196 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1200 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1202 return (struct isl_basic_set
*)
1203 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1206 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1211 if (bmap
->ref
> 1) {
1213 bmap
= isl_basic_map_dup(bmap
);
1216 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1220 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1228 return isl_set_dup(set
);
1231 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1239 return isl_map_dup(map
);
1242 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1243 unsigned a_len
, unsigned b_len
)
1245 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1246 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1247 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1250 struct isl_basic_set
*isl_basic_set_swap_vars(
1251 struct isl_basic_set
*bset
, unsigned n
)
1261 nparam
= isl_basic_set_n_param(bset
);
1262 dim
= isl_basic_set_n_dim(bset
);
1263 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1268 bset
= isl_basic_set_cow(bset
);
1272 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1273 if (isl_blk_is_error(blk
))
1276 for (i
= 0; i
< bset
->n_eq
; ++i
)
1278 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1280 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1282 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1284 for (i
= 0; i
< bset
->n_div
; ++i
)
1286 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1288 isl_blk_free(bset
->ctx
, blk
);
1290 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1291 bset
= isl_basic_set_gauss(bset
, NULL
);
1292 return isl_basic_set_finalize(bset
);
1294 isl_basic_set_free(bset
);
1298 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1301 set
= isl_set_cow(set
);
1305 for (i
= 0; i
< set
->n
; ++i
) {
1306 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1312 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1316 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1322 total
= isl_basic_map_total_dim(bmap
);
1323 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1324 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1326 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1328 i
= isl_basic_map_alloc_equality(bmap
);
1332 isl_int_set_si(bmap
->eq
[i
][0], 1);
1333 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1334 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1335 isl_vec_free(bmap
->sample
);
1336 bmap
->sample
= NULL
;
1337 return isl_basic_map_finalize(bmap
);
1339 isl_basic_map_free(bmap
);
1343 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1345 return (struct isl_basic_set
*)
1346 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1349 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1352 unsigned off
= isl_dim_total(bmap
->dim
);
1353 isl_int
*t
= bmap
->div
[a
];
1354 bmap
->div
[a
] = bmap
->div
[b
];
1357 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1358 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1360 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1361 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1363 for (i
= 0; i
< bmap
->n_div
; ++i
)
1364 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1365 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1368 /* Eliminate the specified n dimensions starting at first from the
1369 * constraints using Fourier-Motzkin. The dimensions themselves
1372 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1373 enum isl_dim_type type
, unsigned first
, unsigned n
)
1382 map
= isl_map_cow(map
);
1385 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1386 first
+= pos(map
->dim
, type
) - 1;
1388 for (i
= 0; i
< map
->n
; ++i
) {
1389 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1399 /* Eliminate the specified n dimensions starting at first from the
1400 * constraints using Fourier-Motzkin. The dimensions themselves
1403 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1404 enum isl_dim_type type
, unsigned first
, unsigned n
)
1406 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1409 /* Eliminate the specified n dimensions starting at first from the
1410 * constraints using Fourier-Motzkin. The dimensions themselves
1413 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1414 unsigned first
, unsigned n
)
1416 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1419 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1420 __isl_take isl_basic_map
*bmap
)
1422 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1427 return isl_basic_map_finalize(bmap
);
1430 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1431 __isl_take isl_basic_set
*bset
)
1433 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1434 (struct isl_basic_map
*)bset
);
1437 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1446 set
= isl_set_cow(set
);
1450 for (i
= 0; i
< set
->n
; ++i
) {
1451 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1461 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1462 enum isl_dim_type type
, unsigned first
, unsigned n
)
1466 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1470 bmap
= isl_basic_map_eliminate_vars(bmap
,
1471 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1474 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1476 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1479 isl_basic_map_free(bmap
);
1483 /* Return true if the definition of the given div (recursively) involves
1484 * any of the given variables.
1486 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1487 unsigned first
, unsigned n
)
1490 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1492 if (isl_int_is_zero(bmap
->div
[div
][0]))
1494 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1497 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1498 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1500 if (div_involves_vars(bmap
, i
, first
, n
))
1507 /* Remove all divs (recursively) involving any of the given dimensions
1508 * in their definitions.
1510 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1511 __isl_take isl_basic_map
*bmap
,
1512 enum isl_dim_type type
, unsigned first
, unsigned n
)
1518 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1520 first
+= isl_basic_map_offset(bmap
, type
);
1522 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1523 if (!div_involves_vars(bmap
, i
, first
, n
))
1525 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1530 isl_basic_map_free(bmap
);
1534 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1535 enum isl_dim_type type
, unsigned first
, unsigned n
)
1544 map
= isl_map_cow(map
);
1548 for (i
= 0; i
< map
->n
; ++i
) {
1549 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1560 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1561 enum isl_dim_type type
, unsigned first
, unsigned n
)
1563 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1567 /* Return true if the definition of the given div is unknown or depends
1570 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1573 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1575 if (isl_int_is_zero(bmap
->div
[div
][0]))
1578 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1579 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1581 if (div_is_unknown(bmap
, i
))
1588 /* Remove all divs that are unknown or defined in terms of unknown divs.
1590 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1591 __isl_take isl_basic_map
*bmap
)
1598 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1599 if (!div_is_unknown(bmap
, i
))
1601 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1606 isl_basic_map_free(bmap
);
1610 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1619 map
= isl_map_cow(map
);
1623 for (i
= 0; i
< map
->n
; ++i
) {
1624 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1634 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1636 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1639 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1640 __isl_take isl_basic_set
*bset
,
1641 enum isl_dim_type type
, unsigned first
, unsigned n
)
1643 return (isl_basic_set
*)
1644 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1647 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1648 enum isl_dim_type type
, unsigned first
, unsigned n
)
1655 map
= isl_map_cow(map
);
1658 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1660 for (i
= 0; i
< map
->n
; ++i
) {
1661 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1662 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1666 map
= isl_map_drop(map
, type
, first
, n
);
1673 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1674 enum isl_dim_type type
, unsigned first
, unsigned n
)
1676 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1679 /* Project out n inputs starting at first using Fourier-Motzkin */
1680 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1681 unsigned first
, unsigned n
)
1683 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1686 static void dump_term(struct isl_basic_map
*bmap
,
1687 isl_int c
, int pos
, FILE *out
)
1690 unsigned in
= isl_basic_map_n_in(bmap
);
1691 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1692 unsigned nparam
= isl_basic_map_n_param(bmap
);
1694 isl_int_print(out
, c
, 0);
1696 if (!isl_int_is_one(c
))
1697 isl_int_print(out
, c
, 0);
1698 if (pos
< 1 + nparam
) {
1699 name
= isl_dim_get_name(bmap
->dim
,
1700 isl_dim_param
, pos
- 1);
1702 fprintf(out
, "%s", name
);
1704 fprintf(out
, "p%d", pos
- 1);
1705 } else if (pos
< 1 + nparam
+ in
)
1706 fprintf(out
, "i%d", pos
- 1 - nparam
);
1707 else if (pos
< 1 + nparam
+ dim
)
1708 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1710 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1714 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1715 int sign
, FILE *out
)
1719 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1723 for (i
= 0, first
= 1; i
< len
; ++i
) {
1724 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1727 fprintf(out
, " + ");
1729 isl_int_abs(v
, c
[i
]);
1730 dump_term(bmap
, v
, i
, out
);
1737 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1738 const char *op
, FILE *out
, int indent
)
1742 fprintf(out
, "%*s", indent
, "");
1744 dump_constraint_sign(bmap
, c
, 1, out
);
1745 fprintf(out
, " %s ", op
);
1746 dump_constraint_sign(bmap
, c
, -1, out
);
1750 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1751 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1753 fprintf(out
, "%*s", indent
, "");
1754 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1759 static void dump_constraints(struct isl_basic_map
*bmap
,
1760 isl_int
**c
, unsigned n
,
1761 const char *op
, FILE *out
, int indent
)
1765 for (i
= 0; i
< n
; ++i
)
1766 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1769 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1773 unsigned total
= isl_basic_map_total_dim(bmap
);
1775 for (j
= 0; j
< 1 + total
; ++j
) {
1776 if (isl_int_is_zero(exp
[j
]))
1778 if (!first
&& isl_int_is_pos(exp
[j
]))
1780 dump_term(bmap
, exp
[j
], j
, out
);
1785 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1789 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1790 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1792 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1793 fprintf(out
, "%*s", indent
, "");
1794 fprintf(out
, "e%d = [(", i
);
1795 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1797 isl_int_print(out
, bmap
->div
[i
][0], 0);
1798 fprintf(out
, "]\n");
1802 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1803 FILE *out
, int indent
)
1806 fprintf(out
, "null basic set\n");
1810 fprintf(out
, "%*s", indent
, "");
1811 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1812 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1813 bset
->extra
, bset
->flags
);
1814 dump((struct isl_basic_map
*)bset
, out
, indent
);
1817 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1818 FILE *out
, int indent
)
1821 fprintf(out
, "null basic map\n");
1825 fprintf(out
, "%*s", indent
, "");
1826 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1827 "flags: %x, n_name: %d\n",
1829 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1830 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1831 dump(bmap
, out
, indent
);
1834 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1839 total
= isl_basic_map_total_dim(bmap
);
1840 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1841 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1842 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1843 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1847 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1849 struct isl_set
*set
;
1853 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1854 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1855 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1856 sizeof(struct isl_set
) +
1857 (n
- 1) * sizeof(struct isl_basic_set
*));
1861 set
->ctx
= dim
->ctx
;
1862 isl_ctx_ref(set
->ctx
);
1874 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1875 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1877 struct isl_set
*set
;
1878 struct isl_dim
*dims
;
1880 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1884 set
= isl_set_alloc_dim(dims
, n
, flags
);
1888 /* Make sure "map" has room for at least "n" more basic maps.
1890 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1893 struct isl_map
*grown
= NULL
;
1897 isl_assert(map
->ctx
, n
>= 0, goto error
);
1898 if (map
->n
+ n
<= map
->size
)
1900 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1903 for (i
= 0; i
< map
->n
; ++i
) {
1904 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1912 isl_map_free(grown
);
1917 /* Make sure "set" has room for at least "n" more basic sets.
1919 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1921 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1924 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1927 struct isl_set
*dup
;
1932 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1935 for (i
= 0; i
< set
->n
; ++i
)
1936 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1940 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1942 return isl_map_from_basic_map(bset
);
1945 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1947 struct isl_map
*map
;
1952 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1953 return isl_map_add_basic_map(map
, bmap
);
1956 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1957 __isl_take isl_basic_set
*bset
)
1959 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1960 (struct isl_basic_map
*)bset
);
1963 void isl_set_free(struct isl_set
*set
)
1973 isl_ctx_deref(set
->ctx
);
1974 for (i
= 0; i
< set
->n
; ++i
)
1975 isl_basic_set_free(set
->p
[i
]);
1976 isl_dim_free(set
->dim
);
1980 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
1985 fprintf(out
, "null set\n");
1989 fprintf(out
, "%*s", indent
, "");
1990 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1991 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1993 for (i
= 0; i
< set
->n
; ++i
) {
1994 fprintf(out
, "%*s", indent
, "");
1995 fprintf(out
, "basic set %d:\n", i
);
1996 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2000 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2005 fprintf(out
, "null map\n");
2009 fprintf(out
, "%*s", indent
, "");
2010 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2011 "flags: %x, n_name: %d\n",
2012 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2013 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2014 for (i
= 0; i
< map
->n
; ++i
) {
2015 fprintf(out
, "%*s", indent
, "");
2016 fprintf(out
, "basic map %d:\n", i
);
2017 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2021 struct isl_basic_map
*isl_basic_map_intersect_domain(
2022 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2024 struct isl_basic_map
*bmap_domain
;
2029 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2030 bset
->dim
, isl_dim_param
), goto error
);
2032 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2033 isl_assert(bset
->ctx
,
2034 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2036 bmap
= isl_basic_map_cow(bmap
);
2039 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2040 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2041 bmap_domain
= isl_basic_map_from_domain(bset
);
2042 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2044 bmap
= isl_basic_map_simplify(bmap
);
2045 return isl_basic_map_finalize(bmap
);
2047 isl_basic_map_free(bmap
);
2048 isl_basic_set_free(bset
);
2052 struct isl_basic_map
*isl_basic_map_intersect_range(
2053 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2055 struct isl_basic_map
*bmap_range
;
2060 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2061 bset
->dim
, isl_dim_param
), goto error
);
2063 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2064 isl_assert(bset
->ctx
,
2065 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2067 bmap
= isl_basic_map_cow(bmap
);
2070 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2071 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2072 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2073 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2075 bmap
= isl_basic_map_simplify(bmap
);
2076 return isl_basic_map_finalize(bmap
);
2078 isl_basic_map_free(bmap
);
2079 isl_basic_set_free(bset
);
2083 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2089 total
= 1 + isl_basic_map_total_dim(bmap
);
2090 if (total
!= vec
->size
)
2095 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2096 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2097 if (!isl_int_is_zero(s
)) {
2103 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2104 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2105 if (isl_int_is_neg(s
)) {
2116 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2118 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2121 struct isl_basic_map
*isl_basic_map_intersect(
2122 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2124 struct isl_vec
*sample
= NULL
;
2126 if (!bmap1
|| !bmap2
)
2129 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2130 bmap2
->dim
, isl_dim_param
), goto error
);
2131 if (isl_dim_total(bmap1
->dim
) ==
2132 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2133 isl_dim_total(bmap2
->dim
) !=
2134 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2135 return isl_basic_map_intersect(bmap2
, bmap1
);
2137 if (isl_dim_total(bmap2
->dim
) !=
2138 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2139 isl_assert(bmap1
->ctx
,
2140 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2142 if (bmap1
->sample
&&
2143 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2144 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2145 sample
= isl_vec_copy(bmap1
->sample
);
2146 else if (bmap2
->sample
&&
2147 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2148 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2149 sample
= isl_vec_copy(bmap2
->sample
);
2151 bmap1
= isl_basic_map_cow(bmap1
);
2154 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2155 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2156 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2159 isl_vec_free(sample
);
2161 isl_vec_free(bmap1
->sample
);
2162 bmap1
->sample
= sample
;
2165 bmap1
= isl_basic_map_simplify(bmap1
);
2166 return isl_basic_map_finalize(bmap1
);
2169 isl_vec_free(sample
);
2170 isl_basic_map_free(bmap1
);
2171 isl_basic_map_free(bmap2
);
2175 struct isl_basic_set
*isl_basic_set_intersect(
2176 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2178 return (struct isl_basic_set
*)
2179 isl_basic_map_intersect(
2180 (struct isl_basic_map
*)bset1
,
2181 (struct isl_basic_map
*)bset2
);
2184 /* Special case of isl_map_intersect, where both map1 and map2
2185 * are convex, without any divs and such that either map1 or map2
2186 * contains a single constraint. This constraint is then simply
2187 * added to the other map.
2189 static __isl_give isl_map
*map_intersect_add_constraint(
2190 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2192 struct isl_basic_map
*bmap1
;
2193 struct isl_basic_map
*bmap2
;
2195 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2196 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2197 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2198 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2200 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2201 return isl_map_intersect(map2
, map1
);
2203 isl_assert(map2
->ctx
,
2204 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2206 map1
= isl_map_cow(map1
);
2209 if (isl_map_fast_is_empty(map1
)) {
2213 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2214 if (map2
->p
[0]->n_eq
== 1)
2215 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2217 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2218 map2
->p
[0]->ineq
[0]);
2220 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2221 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2225 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2226 isl_basic_map_free(map1
->p
[0]);
2239 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2242 struct isl_map
*result
;
2248 if (isl_map_fast_is_empty(map1
)) {
2252 if (isl_map_fast_is_empty(map2
)) {
2257 if (map1
->n
== 1 && map2
->n
== 1 &&
2258 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2259 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2260 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2261 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2262 return map_intersect_add_constraint(map1
, map2
);
2263 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2264 map2
->dim
, isl_dim_param
), goto error
);
2265 if (isl_dim_total(map1
->dim
) ==
2266 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2267 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2268 return isl_map_intersect(map2
, map1
);
2270 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2271 isl_assert(map1
->ctx
,
2272 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2274 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2275 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2276 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2278 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2279 map1
->n
* map2
->n
, flags
);
2282 for (i
= 0; i
< map1
->n
; ++i
)
2283 for (j
= 0; j
< map2
->n
; ++j
) {
2284 struct isl_basic_map
*part
;
2285 part
= isl_basic_map_intersect(
2286 isl_basic_map_copy(map1
->p
[i
]),
2287 isl_basic_map_copy(map2
->p
[j
]));
2288 if (isl_basic_map_is_empty(part
))
2289 isl_basic_map_free(part
);
2291 result
= isl_map_add_basic_map(result
, part
);
2304 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2306 return (struct isl_set
*)
2307 isl_map_intersect((struct isl_map
*)set1
,
2308 (struct isl_map
*)set2
);
2311 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2313 struct isl_dim
*dim
;
2314 struct isl_basic_set
*bset
;
2319 bmap
= isl_basic_map_cow(bmap
);
2322 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2323 in
= isl_basic_map_n_in(bmap
);
2324 bset
= isl_basic_set_from_basic_map(bmap
);
2325 bset
= isl_basic_set_swap_vars(bset
, in
);
2326 return isl_basic_map_from_basic_set(bset
, dim
);
2329 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2330 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2332 struct isl_dim
*res_dim
;
2333 struct isl_basic_map
*res
;
2334 struct isl_dim_map
*dim_map
;
2335 unsigned total
, off
;
2336 enum isl_dim_type t
;
2344 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2346 total
= isl_basic_map_total_dim(bmap
) + n
;
2347 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2349 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2351 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2353 unsigned size
= isl_basic_map_dim(bmap
, t
);
2354 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2356 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2357 pos
, size
- pos
, off
+ pos
+ n
);
2359 off
+= isl_dim_size(res_dim
, t
);
2361 isl_dim_map_div(dim_map
, bmap
, off
);
2363 res
= isl_basic_map_alloc_dim(res_dim
,
2364 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2365 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2366 return isl_basic_map_finalize(res
);
2369 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2370 enum isl_dim_type type
, unsigned n
)
2374 return isl_basic_map_insert(bmap
, type
,
2375 isl_basic_map_dim(bmap
, type
), n
);
2378 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2379 enum isl_dim_type type
, unsigned n
)
2383 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2384 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2386 isl_basic_set_free(bset
);
2390 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2391 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2398 map
= isl_map_cow(map
);
2402 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2406 for (i
= 0; i
< map
->n
; ++i
) {
2407 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2418 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2419 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2421 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2424 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2425 enum isl_dim_type type
, unsigned n
)
2429 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2432 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2433 enum isl_dim_type type
, unsigned n
)
2437 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2438 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2444 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2445 __isl_take isl_basic_map
*bmap
,
2446 enum isl_dim_type dst_type
, unsigned dst_pos
,
2447 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2450 struct isl_dim_map
*dim_map
;
2451 struct isl_basic_map
*res
;
2452 enum isl_dim_type t
;
2453 unsigned total
, off
;
2460 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2463 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2466 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2468 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2469 pos(bmap
->dim
, src_type
) + src_pos
+
2470 ((src_type
< dst_type
) ? n
: 0)) {
2471 bmap
= isl_basic_map_cow(bmap
);
2475 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2476 src_type
, src_pos
, n
);
2480 bmap
= isl_basic_map_finalize(bmap
);
2485 total
= isl_basic_map_total_dim(bmap
);
2486 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2489 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2490 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2491 if (t
== dst_type
) {
2492 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2495 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2498 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2499 dst_pos
, size
- dst_pos
, off
);
2500 off
+= size
- dst_pos
;
2501 } else if (t
== src_type
) {
2502 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2505 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2506 src_pos
+ n
, size
- src_pos
- n
, off
);
2507 off
+= size
- src_pos
- n
;
2509 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2513 isl_dim_map_div(dim_map
, bmap
, off
);
2515 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2516 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2517 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2519 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2520 src_type
, src_pos
, n
);
2524 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2525 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2526 bmap
= isl_basic_map_finalize(bmap
);
2530 isl_basic_map_free(bmap
);
2534 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2535 enum isl_dim_type dst_type
, unsigned dst_pos
,
2536 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2538 return (isl_basic_set
*)isl_basic_map_move_dims(
2539 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2542 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2543 enum isl_dim_type dst_type
, unsigned dst_pos
,
2544 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2548 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2549 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2550 src_type
, src_pos
, n
);
2556 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2557 enum isl_dim_type dst_type
, unsigned dst_pos
,
2558 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2567 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2570 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2573 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2575 map
= isl_map_cow(map
);
2579 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2583 for (i
= 0; i
< map
->n
; ++i
) {
2584 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2586 src_type
, src_pos
, n
);
2597 /* Move the specified dimensions to the last columns right before
2598 * the divs. Don't change the dimension specification of bmap.
2599 * That's the responsibility of the caller.
2601 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2602 enum isl_dim_type type
, unsigned first
, unsigned n
)
2604 struct isl_dim_map
*dim_map
;
2605 struct isl_basic_map
*res
;
2606 enum isl_dim_type t
;
2607 unsigned total
, off
;
2611 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2614 total
= isl_basic_map_total_dim(bmap
);
2615 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2618 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2619 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2621 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2624 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2625 first
, n
, total
- bmap
->n_div
- n
);
2626 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2627 first
+ n
, size
- (first
+ n
), off
);
2628 off
+= size
- (first
+ n
);
2630 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2634 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2636 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2637 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2638 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2642 /* Turn the n dimensions of type type, starting at first
2643 * into existentially quantified variables.
2645 __isl_give isl_basic_map
*isl_basic_map_project_out(
2646 __isl_take isl_basic_map
*bmap
,
2647 enum isl_dim_type type
, unsigned first
, unsigned n
)
2660 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2661 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2663 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2666 bmap
= move_last(bmap
, type
, first
, n
);
2667 bmap
= isl_basic_map_cow(bmap
);
2671 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2672 old
= bmap
->block2
.data
;
2673 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2674 (bmap
->extra
+ n
) * (1 + row_size
));
2675 if (!bmap
->block2
.data
)
2677 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2680 for (i
= 0; i
< n
; ++i
) {
2681 new_div
[i
] = bmap
->block2
.data
+
2682 (bmap
->extra
+ i
) * (1 + row_size
);
2683 isl_seq_clr(new_div
[i
], 1 + row_size
);
2685 for (i
= 0; i
< bmap
->extra
; ++i
)
2686 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2688 bmap
->div
= new_div
;
2692 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2695 bmap
= isl_basic_map_simplify(bmap
);
2696 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2697 return isl_basic_map_finalize(bmap
);
2699 isl_basic_map_free(bmap
);
2703 /* Turn the n dimensions of type type, starting at first
2704 * into existentially quantified variables.
2706 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2707 enum isl_dim_type type
, unsigned first
, unsigned n
)
2709 return (isl_basic_set
*)isl_basic_map_project_out(
2710 (isl_basic_map
*)bset
, type
, first
, n
);
2713 /* Turn the n dimensions of type type, starting at first
2714 * into existentially quantified variables.
2716 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2717 enum isl_dim_type type
, unsigned first
, unsigned n
)
2727 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2729 map
= isl_map_cow(map
);
2733 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2737 for (i
= 0; i
< map
->n
; ++i
) {
2738 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2749 /* Turn the n dimensions of type type, starting at first
2750 * into existentially quantified variables.
2752 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2753 enum isl_dim_type type
, unsigned first
, unsigned n
)
2755 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2758 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2762 for (i
= 0; i
< n
; ++i
) {
2763 j
= isl_basic_map_alloc_div(bmap
);
2766 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2770 isl_basic_map_free(bmap
);
2774 struct isl_basic_map
*isl_basic_map_apply_range(
2775 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2777 struct isl_dim
*dim_result
= NULL
;
2778 struct isl_basic_map
*bmap
;
2779 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2780 struct isl_dim_map
*dim_map1
, *dim_map2
;
2782 if (!bmap1
|| !bmap2
)
2785 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2786 isl_dim_copy(bmap2
->dim
));
2788 n_in
= isl_basic_map_n_in(bmap1
);
2789 n_out
= isl_basic_map_n_out(bmap2
);
2790 n
= isl_basic_map_n_out(bmap1
);
2791 nparam
= isl_basic_map_n_param(bmap1
);
2793 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2794 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2795 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2796 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2797 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2798 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2799 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2800 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2801 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2802 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2803 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2805 bmap
= isl_basic_map_alloc_dim(dim_result
,
2806 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2807 bmap1
->n_eq
+ bmap2
->n_eq
,
2808 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2809 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2810 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2811 bmap
= add_divs(bmap
, n
);
2812 bmap
= isl_basic_map_simplify(bmap
);
2813 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2814 return isl_basic_map_finalize(bmap
);
2816 isl_basic_map_free(bmap1
);
2817 isl_basic_map_free(bmap2
);
2821 struct isl_basic_set
*isl_basic_set_apply(
2822 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2827 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2830 return (struct isl_basic_set
*)
2831 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2833 isl_basic_set_free(bset
);
2834 isl_basic_map_free(bmap
);
2838 struct isl_basic_map
*isl_basic_map_apply_domain(
2839 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2841 if (!bmap1
|| !bmap2
)
2844 isl_assert(bmap1
->ctx
,
2845 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2846 isl_assert(bmap1
->ctx
,
2847 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2850 bmap1
= isl_basic_map_reverse(bmap1
);
2851 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2852 return isl_basic_map_reverse(bmap1
);
2854 isl_basic_map_free(bmap1
);
2855 isl_basic_map_free(bmap2
);
2859 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2860 * A \cap B -> f(A) + f(B)
2862 struct isl_basic_map
*isl_basic_map_sum(
2863 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2865 unsigned n_in
, n_out
, nparam
, total
, pos
;
2866 struct isl_basic_map
*bmap
= NULL
;
2867 struct isl_dim_map
*dim_map1
, *dim_map2
;
2870 if (!bmap1
|| !bmap2
)
2873 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2876 nparam
= isl_basic_map_n_param(bmap1
);
2877 n_in
= isl_basic_map_n_in(bmap1
);
2878 n_out
= isl_basic_map_n_out(bmap1
);
2880 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2881 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2882 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2883 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2884 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2885 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2886 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2887 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2888 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2889 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2890 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2892 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2893 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2894 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2895 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2896 for (i
= 0; i
< n_out
; ++i
) {
2897 int j
= isl_basic_map_alloc_equality(bmap
);
2900 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2901 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2902 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2903 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2905 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2906 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2907 bmap
= add_divs(bmap
, 2 * n_out
);
2909 bmap
= isl_basic_map_simplify(bmap
);
2910 return isl_basic_map_finalize(bmap
);
2912 isl_basic_map_free(bmap
);
2913 isl_basic_map_free(bmap1
);
2914 isl_basic_map_free(bmap2
);
2918 /* Given two maps A -> f(A) and B -> g(B), construct a map
2919 * A \cap B -> f(A) + f(B)
2921 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2923 struct isl_map
*result
;
2929 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2931 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2932 map1
->n
* map2
->n
, 0);
2935 for (i
= 0; i
< map1
->n
; ++i
)
2936 for (j
= 0; j
< map2
->n
; ++j
) {
2937 struct isl_basic_map
*part
;
2938 part
= isl_basic_map_sum(
2939 isl_basic_map_copy(map1
->p
[i
]),
2940 isl_basic_map_copy(map2
->p
[j
]));
2941 if (isl_basic_map_is_empty(part
))
2942 isl_basic_map_free(part
);
2944 result
= isl_map_add_basic_map(result
, part
);
2957 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2958 __isl_take isl_set
*set2
)
2960 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2963 /* Given a basic map A -> f(A), construct A -> -f(A).
2965 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2970 bmap
= isl_basic_map_cow(bmap
);
2974 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2975 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2976 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2977 for (j
= 0; j
< n
; ++j
)
2978 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2979 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2980 for (j
= 0; j
< n
; ++j
)
2981 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2982 for (i
= 0; i
< bmap
->n_div
; ++i
)
2983 for (j
= 0; j
< n
; ++j
)
2984 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2985 return isl_basic_map_finalize(bmap
);
2988 /* Given a map A -> f(A), construct A -> -f(A).
2990 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2994 map
= isl_map_cow(map
);
2998 for (i
= 0; i
< map
->n
; ++i
) {
2999 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3010 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3012 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3015 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3016 * A -> floor(f(A)/d).
3018 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3021 unsigned n_in
, n_out
, nparam
, total
, pos
;
3022 struct isl_basic_map
*result
= NULL
;
3023 struct isl_dim_map
*dim_map
;
3029 nparam
= isl_basic_map_n_param(bmap
);
3030 n_in
= isl_basic_map_n_in(bmap
);
3031 n_out
= isl_basic_map_n_out(bmap
);
3033 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3034 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3035 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3036 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3037 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3038 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3040 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3041 bmap
->n_div
+ n_out
,
3042 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3043 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
3044 result
= add_divs(result
, n_out
);
3045 for (i
= 0; i
< n_out
; ++i
) {
3047 j
= isl_basic_map_alloc_inequality(result
);
3050 isl_seq_clr(result
->ineq
[j
], 1+total
);
3051 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3052 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3053 j
= isl_basic_map_alloc_inequality(result
);
3056 isl_seq_clr(result
->ineq
[j
], 1+total
);
3057 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3058 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3059 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3062 result
= isl_basic_map_simplify(result
);
3063 return isl_basic_map_finalize(result
);
3065 isl_basic_map_free(result
);
3069 /* Given a map A -> f(A) and an integer d, construct a map
3070 * A -> floor(f(A)/d).
3072 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3076 map
= isl_map_cow(map
);
3080 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3081 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3082 for (i
= 0; i
< map
->n
; ++i
) {
3083 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3094 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3100 i
= isl_basic_map_alloc_equality(bmap
);
3103 nparam
= isl_basic_map_n_param(bmap
);
3104 n_in
= isl_basic_map_n_in(bmap
);
3105 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3106 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3107 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3108 return isl_basic_map_finalize(bmap
);
3110 isl_basic_map_free(bmap
);
3114 /* Add a constraints to "bmap" expressing i_pos < o_pos
3116 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3122 i
= isl_basic_map_alloc_inequality(bmap
);
3125 nparam
= isl_basic_map_n_param(bmap
);
3126 n_in
= isl_basic_map_n_in(bmap
);
3127 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3128 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3129 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3130 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3131 return isl_basic_map_finalize(bmap
);
3133 isl_basic_map_free(bmap
);
3137 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3139 static __isl_give isl_basic_map
*var_less_or_equal(
3140 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3146 i
= isl_basic_map_alloc_inequality(bmap
);
3149 nparam
= isl_basic_map_n_param(bmap
);
3150 n_in
= isl_basic_map_n_in(bmap
);
3151 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3152 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3153 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3154 return isl_basic_map_finalize(bmap
);
3156 isl_basic_map_free(bmap
);
3160 /* Add a constraints to "bmap" expressing i_pos > o_pos
3162 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3168 i
= isl_basic_map_alloc_inequality(bmap
);
3171 nparam
= isl_basic_map_n_param(bmap
);
3172 n_in
= isl_basic_map_n_in(bmap
);
3173 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3174 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3175 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3176 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3177 return isl_basic_map_finalize(bmap
);
3179 isl_basic_map_free(bmap
);
3183 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3185 static __isl_give isl_basic_map
*var_more_or_equal(
3186 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3192 i
= isl_basic_map_alloc_inequality(bmap
);
3195 nparam
= isl_basic_map_n_param(bmap
);
3196 n_in
= isl_basic_map_n_in(bmap
);
3197 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3198 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3199 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3200 return isl_basic_map_finalize(bmap
);
3202 isl_basic_map_free(bmap
);
3206 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3209 struct isl_basic_map
*bmap
;
3210 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3213 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3214 bmap
= var_equal(bmap
, i
);
3215 return isl_basic_map_finalize(bmap
);
3218 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3220 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3223 struct isl_basic_map
*bmap
;
3224 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3227 for (i
= 0; i
< pos
&& bmap
; ++i
)
3228 bmap
= var_equal(bmap
, i
);
3230 bmap
= var_less(bmap
, pos
);
3231 return isl_basic_map_finalize(bmap
);
3234 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3236 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3237 __isl_take isl_dim
*dim
, unsigned pos
)
3240 isl_basic_map
*bmap
;
3242 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3243 for (i
= 0; i
< pos
; ++i
)
3244 bmap
= var_equal(bmap
, i
);
3245 bmap
= var_less_or_equal(bmap
, pos
);
3246 return isl_basic_map_finalize(bmap
);
3249 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3251 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3254 struct isl_basic_map
*bmap
;
3255 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3258 for (i
= 0; i
< pos
&& bmap
; ++i
)
3259 bmap
= var_equal(bmap
, i
);
3261 bmap
= var_more(bmap
, pos
);
3262 return isl_basic_map_finalize(bmap
);
3265 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3267 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3268 __isl_take isl_dim
*dim
, unsigned pos
)
3271 isl_basic_map
*bmap
;
3273 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3274 for (i
= 0; i
< pos
; ++i
)
3275 bmap
= var_equal(bmap
, i
);
3276 bmap
= var_more_or_equal(bmap
, pos
);
3277 return isl_basic_map_finalize(bmap
);
3280 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3281 unsigned n
, int equal
)
3283 struct isl_map
*map
;
3286 if (n
== 0 && equal
)
3287 return isl_map_universe(dims
);
3289 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3291 for (i
= 0; i
+ 1 < n
; ++i
)
3292 map
= isl_map_add_basic_map(map
,
3293 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3296 map
= isl_map_add_basic_map(map
,
3297 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3299 map
= isl_map_add_basic_map(map
,
3300 isl_basic_map_less_at(dims
, n
- 1));
3307 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3311 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3314 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3316 return map_lex_lte_first(dim
, n
, 0);
3319 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3321 return map_lex_lte_first(dim
, n
, 1);
3324 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3326 return map_lex_lte(isl_dim_map(set_dim
), 0);
3329 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3331 return map_lex_lte(isl_dim_map(set_dim
), 1);
3334 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3335 unsigned n
, int equal
)
3337 struct isl_map
*map
;
3340 if (n
== 0 && equal
)
3341 return isl_map_universe(dims
);
3343 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3345 for (i
= 0; i
+ 1 < n
; ++i
)
3346 map
= isl_map_add_basic_map(map
,
3347 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3350 map
= isl_map_add_basic_map(map
,
3351 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3353 map
= isl_map_add_basic_map(map
,
3354 isl_basic_map_more_at(dims
, n
- 1));
3361 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3365 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3368 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3370 return map_lex_gte_first(dim
, n
, 0);
3373 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3375 return map_lex_gte_first(dim
, n
, 1);
3378 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3380 return map_lex_gte(isl_dim_map(set_dim
), 0);
3383 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3385 return map_lex_gte(isl_dim_map(set_dim
), 1);
3388 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3389 __isl_take isl_set
*set2
)
3392 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3393 map
= isl_map_intersect_domain(map
, set1
);
3394 map
= isl_map_intersect_range(map
, set2
);
3398 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3399 __isl_take isl_set
*set2
)
3402 map
= isl_map_lex_lt(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_ge_set(__isl_take isl_set
*set1
,
3409 __isl_take isl_set
*set2
)
3412 map
= isl_map_lex_ge(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_gt_set(__isl_take isl_set
*set1
,
3419 __isl_take isl_set
*set2
)
3422 map
= isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map
*map1
,
3429 __isl_take isl_map
*map2
)
3432 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3433 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3434 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3438 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3439 __isl_take isl_map
*map2
)
3442 map
= isl_map_lex_lt(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_ge_map(__isl_take isl_map
*map1
,
3449 __isl_take isl_map
*map2
)
3452 map
= isl_map_lex_ge(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_gt_map(__isl_take isl_map
*map1
,
3459 __isl_take isl_map
*map2
)
3462 map
= isl_map_lex_gt(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 struct isl_basic_map
*isl_basic_map_from_basic_set(
3469 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3471 struct isl_basic_map
*bmap
;
3473 bset
= isl_basic_set_cow(bset
);
3477 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3478 isl_dim_free(bset
->dim
);
3479 bmap
= (struct isl_basic_map
*) bset
;
3481 return isl_basic_map_finalize(bmap
);
3483 isl_basic_set_free(bset
);
3488 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3492 if (bmap
->dim
->n_in
== 0)
3493 return (struct isl_basic_set
*)bmap
;
3494 bmap
= isl_basic_map_cow(bmap
);
3497 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3500 bmap
= isl_basic_map_finalize(bmap
);
3501 return (struct isl_basic_set
*)bmap
;
3503 isl_basic_map_free(bmap
);
3507 /* For a div d = floor(f/m), add the constraints
3510 * -(f-(n-1)) + m d >= 0
3512 * Note that the second constraint is the negation of
3516 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3517 unsigned pos
, isl_int
*div
)
3520 unsigned total
= isl_basic_map_total_dim(bmap
);
3522 i
= isl_basic_map_alloc_inequality(bmap
);
3525 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3526 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3528 j
= isl_basic_map_alloc_inequality(bmap
);
3531 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3532 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3533 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3537 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3538 unsigned pos
, isl_int
*div
)
3540 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3544 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3546 unsigned total
= isl_basic_map_total_dim(bmap
);
3547 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3549 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3553 struct isl_basic_set
*isl_basic_map_underlying_set(
3554 struct isl_basic_map
*bmap
)
3558 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3560 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3561 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3562 return (struct isl_basic_set
*)bmap
;
3563 bmap
= isl_basic_map_cow(bmap
);
3566 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3569 bmap
->extra
-= bmap
->n_div
;
3571 bmap
= isl_basic_map_finalize(bmap
);
3572 return (struct isl_basic_set
*)bmap
;
3577 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3578 __isl_take isl_basic_set
*bset
)
3580 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3583 struct isl_basic_map
*isl_basic_map_overlying_set(
3584 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3586 struct isl_basic_map
*bmap
;
3587 struct isl_ctx
*ctx
;
3594 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3595 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3596 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3598 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3599 isl_basic_map_free(like
);
3600 return (struct isl_basic_map
*)bset
;
3602 bset
= isl_basic_set_cow(bset
);
3605 total
= bset
->dim
->n_out
+ bset
->extra
;
3606 bmap
= (struct isl_basic_map
*)bset
;
3607 isl_dim_free(bmap
->dim
);
3608 bmap
->dim
= isl_dim_copy(like
->dim
);
3611 bmap
->n_div
= like
->n_div
;
3612 bmap
->extra
+= like
->n_div
;
3615 ltotal
= total
- bmap
->extra
+ like
->extra
;
3618 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3619 bmap
->extra
* (1 + 1 + total
));
3620 if (isl_blk_is_error(bmap
->block2
))
3622 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3626 for (i
= 0; i
< bmap
->extra
; ++i
)
3627 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3628 for (i
= 0; i
< like
->n_div
; ++i
) {
3629 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3630 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3632 bmap
= isl_basic_map_extend_constraints(bmap
,
3633 0, 2 * like
->n_div
);
3634 for (i
= 0; i
< like
->n_div
; ++i
) {
3635 if (isl_int_is_zero(bmap
->div
[i
][0]))
3637 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3641 isl_basic_map_free(like
);
3642 bmap
= isl_basic_map_simplify(bmap
);
3643 bmap
= isl_basic_map_finalize(bmap
);
3646 isl_basic_map_free(like
);
3647 isl_basic_set_free(bset
);
3651 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3652 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3654 return (struct isl_basic_set
*)
3655 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3658 struct isl_set
*isl_set_from_underlying_set(
3659 struct isl_set
*set
, struct isl_basic_set
*like
)
3665 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3667 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3668 isl_basic_set_free(like
);
3671 set
= isl_set_cow(set
);
3674 for (i
= 0; i
< set
->n
; ++i
) {
3675 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3676 isl_basic_set_copy(like
));
3680 isl_dim_free(set
->dim
);
3681 set
->dim
= isl_dim_copy(like
->dim
);
3684 isl_basic_set_free(like
);
3687 isl_basic_set_free(like
);
3692 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3696 map
= isl_map_cow(map
);
3699 map
->dim
= isl_dim_cow(map
->dim
);
3703 for (i
= 1; i
< map
->n
; ++i
)
3704 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3706 for (i
= 0; i
< map
->n
; ++i
) {
3707 map
->p
[i
] = (struct isl_basic_map
*)
3708 isl_basic_map_underlying_set(map
->p
[i
]);
3713 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3715 isl_dim_free(map
->dim
);
3716 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3720 return (struct isl_set
*)map
;
3726 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3728 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3731 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3732 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3734 bmap
= isl_basic_map_cow(bmap
);
3738 isl_dim_free(bmap
->dim
);
3741 bmap
= isl_basic_map_finalize(bmap
);
3745 isl_basic_map_free(bmap
);
3750 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3751 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3753 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3757 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3758 __isl_take isl_dim
*dim
)
3762 map
= isl_map_cow(map
);
3766 for (i
= 0; i
< map
->n
; ++i
) {
3767 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3772 isl_dim_free(map
->dim
);
3782 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3783 __isl_take isl_dim
*dim
)
3785 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3788 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3791 struct isl_basic_set
*domain
;
3797 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3799 n_in
= isl_basic_map_n_in(bmap
);
3800 n_out
= isl_basic_map_n_out(bmap
);
3801 domain
= isl_basic_set_from_basic_map(bmap
);
3802 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3804 domain
= isl_basic_set_reset_dim(domain
, dim
);
3809 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3811 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3814 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3815 __isl_take isl_basic_map
*bmap
)
3819 isl_basic_map
*domain
;
3820 isl_basic_set
*bset
;
3821 int nparam
, n_in
, n_out
;
3824 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3825 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3826 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3828 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3829 domain
= isl_basic_map_universe(dim
);
3831 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3832 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3833 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3835 total
= isl_basic_map_total_dim(bmap
);
3837 for (i
= 0; i
< n_in
; ++i
) {
3838 k
= isl_basic_map_alloc_equality(bmap
);
3841 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3842 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3843 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3846 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3847 return isl_basic_map_finalize(bmap
);
3849 isl_basic_map_free(bmap
);
3853 __isl_give isl_basic_map
*isl_basic_map_range_map(
3854 __isl_take isl_basic_map
*bmap
)
3858 isl_basic_map
*range
;
3859 isl_basic_set
*bset
;
3860 int nparam
, n_in
, n_out
;
3863 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3864 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3865 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3867 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3868 range
= isl_basic_map_universe(dim
);
3870 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3871 bmap
= isl_basic_map_apply_range(bmap
, range
);
3872 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3874 total
= isl_basic_map_total_dim(bmap
);
3876 for (i
= 0; i
< n_out
; ++i
) {
3877 k
= isl_basic_map_alloc_equality(bmap
);
3880 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3881 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3882 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3885 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3886 return isl_basic_map_finalize(bmap
);
3888 isl_basic_map_free(bmap
);
3892 struct isl_set
*isl_map_range(struct isl_map
*map
)
3895 struct isl_set
*set
;
3899 if (isl_map_dim(map
, isl_dim_in
) == 0)
3900 return (isl_set
*)map
;
3902 map
= isl_map_cow(map
);
3906 set
= (struct isl_set
*) map
;
3907 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3910 for (i
= 0; i
< map
->n
; ++i
) {
3911 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3915 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3916 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3923 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3926 isl_dim
*domain_dim
;
3928 map
= isl_map_cow(map
);
3932 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3933 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3934 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3937 for (i
= 0; i
< map
->n
; ++i
) {
3938 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3942 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3943 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3950 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3955 map
= isl_map_cow(map
);
3959 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3960 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3961 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3964 for (i
= 0; i
< map
->n
; ++i
) {
3965 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
3969 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3970 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3977 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3980 struct isl_map
*map
= NULL
;
3982 set
= isl_set_cow(set
);
3985 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3986 map
= (struct isl_map
*)set
;
3987 for (i
= 0; i
< set
->n
; ++i
) {
3988 map
->p
[i
] = isl_basic_map_from_basic_set(
3989 set
->p
[i
], isl_dim_copy(dim
));
3993 isl_dim_free(map
->dim
);
4002 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4003 __isl_take isl_basic_set
*bset
)
4005 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4008 __isl_give isl_basic_map
*isl_basic_map_from_range(
4009 __isl_take isl_basic_set
*bset
)
4011 return (isl_basic_map
*)bset
;
4014 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4016 return (struct isl_map
*)set
;
4019 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4021 return isl_map_reverse(isl_map_from_range(set
));
4024 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4025 __isl_take isl_set
*range
)
4027 return isl_map_apply_range(isl_map_from_domain(domain
),
4028 isl_map_from_range(range
));
4031 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4034 struct isl_set
*set
= NULL
;
4038 map
= isl_map_cow(map
);
4041 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4044 set
= (struct isl_set
*)map
;
4045 for (i
= 0; i
< map
->n
; ++i
) {
4046 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4056 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4058 struct isl_map
*map
;
4062 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4063 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4064 sizeof(struct isl_map
) +
4065 (n
- 1) * sizeof(struct isl_basic_map
*));
4069 map
->ctx
= dim
->ctx
;
4070 isl_ctx_ref(map
->ctx
);
4082 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4083 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4086 struct isl_map
*map
;
4087 struct isl_dim
*dims
;
4089 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4093 map
= isl_map_alloc_dim(dims
, n
, flags
);
4097 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4099 struct isl_basic_map
*bmap
;
4100 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4101 bmap
= isl_basic_map_set_to_empty(bmap
);
4105 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4107 struct isl_basic_set
*bset
;
4108 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4109 bset
= isl_basic_set_set_to_empty(bset
);
4113 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4115 struct isl_basic_map
*bmap
;
4118 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4119 bmap
= isl_basic_map_set_to_empty(bmap
);
4123 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_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_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4135 struct isl_basic_set
*bset
;
4138 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4139 bset
= isl_basic_set_set_to_empty(bset
);
4143 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4145 struct isl_basic_map
*bmap
;
4146 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4147 bmap
= isl_basic_map_finalize(bmap
);
4151 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4153 struct isl_basic_set
*bset
;
4154 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4155 bset
= isl_basic_set_finalize(bset
);
4159 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4162 unsigned total
= isl_dim_total(dim
);
4163 isl_basic_map
*bmap
;
4165 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4166 for (i
= 0; i
< total
; ++i
) {
4167 int k
= isl_basic_map_alloc_inequality(bmap
);
4170 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4171 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4175 isl_basic_map_free(bmap
);
4179 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4181 return isl_basic_map_nat_universe(dim
);
4184 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4186 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4189 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4191 return isl_map_nat_universe(dim
);
4194 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4195 __isl_keep isl_basic_map
*model
)
4199 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4202 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4206 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4209 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4210 __isl_keep isl_set
*model
)
4214 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4217 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4219 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4222 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4226 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4229 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4233 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4236 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4238 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4241 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4245 return isl_set_empty(isl_dim_copy(model
->dim
));
4248 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4250 struct isl_map
*map
;
4253 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4254 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4258 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4260 struct isl_set
*set
;
4263 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4264 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4268 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4272 return isl_set_universe(isl_dim_copy(model
->dim
));
4275 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4278 struct isl_map
*dup
;
4282 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4283 for (i
= 0; i
< map
->n
; ++i
)
4284 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4288 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4289 __isl_take isl_basic_map
*bmap
)
4293 if (isl_basic_map_fast_is_empty(bmap
)) {
4294 isl_basic_map_free(bmap
);
4297 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4298 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4299 map
->p
[map
->n
] = bmap
;
4301 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4307 isl_basic_map_free(bmap
);
4311 void isl_map_free(struct isl_map
*map
)
4321 isl_ctx_deref(map
->ctx
);
4322 for (i
= 0; i
< map
->n
; ++i
)
4323 isl_basic_map_free(map
->p
[i
]);
4324 isl_dim_free(map
->dim
);
4328 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4329 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4333 base
= isl_map_cow(base
);
4337 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4340 for (i
= 0; i
< base
->n
; ++i
) {
4341 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4342 isl_dim_copy(base
->dim
), 0, 0, 0);
4352 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4353 unsigned nparam
, unsigned dim
)
4355 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4359 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4360 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4364 bmap
= isl_basic_map_cow(bmap
);
4365 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4366 j
= isl_basic_map_alloc_equality(bmap
);
4369 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4370 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4371 isl_int_set_si(bmap
->eq
[j
][0], value
);
4372 bmap
= isl_basic_map_simplify(bmap
);
4373 return isl_basic_map_finalize(bmap
);
4375 isl_basic_map_free(bmap
);
4379 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4380 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4384 bmap
= isl_basic_map_cow(bmap
);
4385 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4386 j
= isl_basic_map_alloc_equality(bmap
);
4389 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4390 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4391 isl_int_set(bmap
->eq
[j
][0], value
);
4392 bmap
= isl_basic_map_simplify(bmap
);
4393 return isl_basic_map_finalize(bmap
);
4395 isl_basic_map_free(bmap
);
4399 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4400 enum isl_dim_type type
, unsigned pos
, int value
)
4404 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4405 return isl_basic_map_fix_pos_si(bmap
,
4406 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4408 isl_basic_map_free(bmap
);
4412 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4413 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4417 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4418 return isl_basic_map_fix_pos(bmap
,
4419 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4421 isl_basic_map_free(bmap
);
4425 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4426 enum isl_dim_type type
, unsigned pos
, int value
)
4428 return (struct isl_basic_set
*)
4429 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4433 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4434 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4436 return (struct isl_basic_set
*)
4437 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4441 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4442 unsigned input
, int value
)
4444 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4447 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4448 unsigned dim
, int value
)
4450 return (struct isl_basic_set
*)
4451 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4452 isl_dim_set
, dim
, value
);
4455 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4456 enum isl_dim_type type
, unsigned pos
, int value
)
4460 map
= isl_map_cow(map
);
4464 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4465 for (i
= 0; i
< map
->n
; ++i
) {
4466 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4470 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4477 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4478 enum isl_dim_type type
, unsigned pos
, int value
)
4480 return (struct isl_set
*)
4481 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4484 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4485 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4489 map
= isl_map_cow(map
);
4493 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4494 for (i
= 0; i
< map
->n
; ++i
) {
4495 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4499 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4506 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4507 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4509 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4512 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4513 unsigned input
, int value
)
4515 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4518 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4520 return (struct isl_set
*)
4521 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4524 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4525 __isl_take isl_basic_map
*bmap
,
4526 enum isl_dim_type type
, unsigned pos
, int value
)
4532 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4533 pos
+= isl_basic_map_offset(bmap
, type
);
4534 bmap
= isl_basic_map_cow(bmap
);
4535 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4536 j
= isl_basic_map_alloc_inequality(bmap
);
4539 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4540 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4541 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4542 bmap
= isl_basic_map_simplify(bmap
);
4543 return isl_basic_map_finalize(bmap
);
4545 isl_basic_map_free(bmap
);
4549 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4550 unsigned dim
, isl_int value
)
4554 bset
= isl_basic_set_cow(bset
);
4555 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4556 j
= isl_basic_set_alloc_inequality(bset
);
4559 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4560 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4561 isl_int_neg(bset
->ineq
[j
][0], value
);
4562 bset
= isl_basic_set_simplify(bset
);
4563 return isl_basic_set_finalize(bset
);
4565 isl_basic_set_free(bset
);
4569 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4570 enum isl_dim_type type
, unsigned pos
, int value
)
4574 map
= isl_map_cow(map
);
4578 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4579 for (i
= 0; i
< map
->n
; ++i
) {
4580 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4585 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4592 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4593 enum isl_dim_type type
, unsigned pos
, int value
)
4595 return (struct isl_set
*)
4596 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4599 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4604 set
= isl_set_cow(set
);
4608 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4609 for (i
= 0; i
< set
->n
; ++i
) {
4610 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4620 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4624 map
= isl_map_cow(map
);
4628 map
->dim
= isl_dim_reverse(map
->dim
);
4631 for (i
= 0; i
< map
->n
; ++i
) {
4632 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4636 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4643 static struct isl_map
*isl_basic_map_partial_lexopt(
4644 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4645 struct isl_set
**empty
, int max
)
4649 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4650 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4652 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4654 isl_basic_map_free(bmap
);
4655 isl_basic_set_free(dom
);
4661 struct isl_map
*isl_basic_map_partial_lexmax(
4662 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4663 struct isl_set
**empty
)
4665 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4668 struct isl_map
*isl_basic_map_partial_lexmin(
4669 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4670 struct isl_set
**empty
)
4672 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4675 struct isl_set
*isl_basic_set_partial_lexmin(
4676 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4677 struct isl_set
**empty
)
4679 return (struct isl_set
*)
4680 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4684 struct isl_set
*isl_basic_set_partial_lexmax(
4685 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4686 struct isl_set
**empty
)
4688 return (struct isl_set
*)
4689 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4693 /* Given a basic map "bmap", compute the lexicographically minimal
4694 * (or maximal) image element for each domain element in dom.
4695 * Set *empty to those elements in dom that do not have an image element.
4697 * We first make sure the basic sets in dom are disjoint and then
4698 * simply collect the results over each of the basic sets separately.
4699 * We could probably improve the efficiency a bit by moving the union
4700 * domain down into the parametric integer programming.
4702 static __isl_give isl_map
*basic_map_partial_lexopt(
4703 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4704 __isl_give isl_set
**empty
, int max
)
4707 struct isl_map
*res
;
4709 dom
= isl_set_make_disjoint(dom
);
4713 if (isl_set_fast_is_empty(dom
)) {
4714 res
= isl_map_empty_like_basic_map(bmap
);
4715 *empty
= isl_set_empty_like(dom
);
4717 isl_basic_map_free(bmap
);
4721 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4722 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4724 for (i
= 1; i
< dom
->n
; ++i
) {
4725 struct isl_map
*res_i
;
4726 struct isl_set
*empty_i
;
4728 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4729 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4731 res
= isl_map_union_disjoint(res
, res_i
);
4732 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4736 isl_basic_map_free(bmap
);
4741 isl_basic_map_free(bmap
);
4745 /* Given a map "map", compute the lexicographically minimal
4746 * (or maximal) image element for each domain element in dom.
4747 * Set *empty to those elements in dom that do not have an image element.
4749 * We first compute the lexicographically minimal or maximal element
4750 * in the first basic map. This results in a partial solution "res"
4751 * and a subset "todo" of dom that still need to be handled.
4752 * We then consider each of the remaining maps in "map" and successively
4753 * improve both "res" and "todo".
4755 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4756 * Assume we are computing the lexicographical maximum.
4757 * We first compute the lexicographically maximal element in basic map i.
4758 * This results in a partial solution res_i and a subset todo_i.
4759 * Then we combine these results with those obtain for the first k basic maps
4760 * to obtain a result that is valid for the first k+1 basic maps.
4761 * In particular, the set where there is no solution is the set where
4762 * there is no solution for the first k basic maps and also no solution
4763 * for the ith basic map, i.e.,
4765 * todo^i = todo^k * todo_i
4767 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4768 * solutions, arbitrarily breaking ties in favor of res^k.
4769 * That is, when res^k(a) >= res_i(a), we pick res^k and
4770 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4771 * the lexicographic order.)
4772 * In practice, we compute
4774 * res^k * (res_i . "<=")
4778 * res_i * (res^k . "<")
4780 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4781 * where only one of res^k and res_i provides a solution and we simply pick
4788 * Note that we only compute these intersections when dom(res^k) intersects
4789 * dom(res_i). Otherwise, the only effect of these intersections is to
4790 * potentially break up res^k and res_i into smaller pieces.
4791 * We want to avoid such splintering as much as possible.
4792 * In fact, an earlier implementation of this function would look for
4793 * better results in the domain of res^k and for extra results in todo^k,
4794 * but this would always result in a splintering according to todo^k,
4795 * even when the domain of basic map i is disjoint from the domains of
4796 * the previous basic maps.
4798 static __isl_give isl_map
*isl_map_partial_lexopt(
4799 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4800 __isl_give isl_set
**empty
, int max
)
4803 struct isl_map
*res
;
4804 struct isl_set
*todo
;
4809 if (isl_map_fast_is_empty(map
)) {
4817 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4818 isl_set_copy(dom
), &todo
, max
);
4820 for (i
= 1; i
< map
->n
; ++i
) {
4824 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4826 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4827 isl_set_copy(dom
), &todo_i
, max
);
4830 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4831 le
= isl_map_lex_le(dim
);
4833 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4834 le
= isl_map_lex_ge(dim
);
4836 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4837 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4838 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4839 le
= isl_map_intersect(le
, isl_map_copy(res
));
4841 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4842 res
= isl_map_intersect_domain(res
,
4843 isl_set_copy(todo_i
));
4844 res_i
= isl_map_intersect_domain(res_i
,
4845 isl_set_copy(todo
));
4848 res
= isl_map_union_disjoint(res
, res_i
);
4849 res
= isl_map_union_disjoint(res
, lt
);
4850 res
= isl_map_union_disjoint(res
, le
);
4852 todo
= isl_set_intersect(todo
, todo_i
);
4872 __isl_give isl_map
*isl_map_partial_lexmax(
4873 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4874 __isl_give isl_set
**empty
)
4876 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4879 __isl_give isl_map
*isl_map_partial_lexmin(
4880 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4881 __isl_give isl_set
**empty
)
4883 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4886 __isl_give isl_set
*isl_set_partial_lexmin(
4887 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4888 __isl_give isl_set
**empty
)
4890 return (struct isl_set
*)
4891 isl_map_partial_lexmin((struct isl_map
*)set
,
4895 __isl_give isl_set
*isl_set_partial_lexmax(
4896 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4897 __isl_give isl_set
**empty
)
4899 return (struct isl_set
*)
4900 isl_map_partial_lexmax((struct isl_map
*)set
,
4904 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4906 struct isl_basic_set
*dom
= NULL
;
4907 struct isl_dim
*dom_dim
;
4911 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4912 dom
= isl_basic_set_universe(dom_dim
);
4913 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4915 isl_basic_map_free(bmap
);
4919 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4921 return isl_basic_map_lexopt(bmap
, 0);
4924 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4926 return isl_basic_map_lexopt(bmap
, 1);
4929 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4931 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4934 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4936 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4939 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4941 struct isl_set
*dom
= NULL
;
4942 struct isl_dim
*dom_dim
;
4946 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4947 dom
= isl_set_universe(dom_dim
);
4948 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4954 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4956 return isl_map_lexopt(map
, 0);
4959 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4961 return isl_map_lexopt(map
, 1);
4964 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4966 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4969 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4971 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4974 /* Apply a preimage specified by "mat" on the parameters of "bset".
4975 * bset is assumed to have only parameters and divs.
4977 static struct isl_basic_set
*basic_set_parameter_preimage(
4978 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4985 bset
->dim
= isl_dim_cow(bset
->dim
);
4989 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4991 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4993 bset
->dim
->nparam
= 0;
4994 bset
->dim
->n_out
= nparam
;
4995 bset
= isl_basic_set_preimage(bset
, mat
);
4997 bset
->dim
->nparam
= bset
->dim
->n_out
;
4998 bset
->dim
->n_out
= 0;
5003 isl_basic_set_free(bset
);
5007 /* Apply a preimage specified by "mat" on the parameters of "set".
5008 * set is assumed to have only parameters and divs.
5010 static struct isl_set
*set_parameter_preimage(
5011 struct isl_set
*set
, struct isl_mat
*mat
)
5013 struct isl_dim
*dim
= NULL
;
5019 dim
= isl_dim_copy(set
->dim
);
5020 dim
= isl_dim_cow(dim
);
5024 nparam
= isl_set_dim(set
, isl_dim_param
);
5026 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5029 dim
->n_out
= nparam
;
5030 isl_set_reset_dim(set
, dim
);
5031 set
= isl_set_preimage(set
, mat
);
5034 dim
= isl_dim_copy(set
->dim
);
5035 dim
= isl_dim_cow(dim
);
5038 dim
->nparam
= dim
->n_out
;
5040 isl_set_reset_dim(set
, dim
);
5050 /* Intersect the basic set "bset" with the affine space specified by the
5051 * equalities in "eq".
5053 static struct isl_basic_set
*basic_set_append_equalities(
5054 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5062 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5067 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5068 for (i
= 0; i
< eq
->n_row
; ++i
) {
5069 k
= isl_basic_set_alloc_equality(bset
);
5072 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5073 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5077 bset
= isl_basic_set_gauss(bset
, NULL
);
5078 bset
= isl_basic_set_finalize(bset
);
5083 isl_basic_set_free(bset
);
5087 /* Intersect the set "set" with the affine space specified by the
5088 * equalities in "eq".
5090 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5098 for (i
= 0; i
< set
->n
; ++i
) {
5099 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5112 /* Project the given basic set onto its parameter domain, possibly introducing
5113 * new, explicit, existential variables in the constraints.
5114 * The input has parameters and (possibly implicit) existential variables.
5115 * The output has the same parameters, but only
5116 * explicit existentially quantified variables.
5118 * The actual projection is performed by pip, but pip doesn't seem
5119 * to like equalities very much, so we first remove the equalities
5120 * among the parameters by performing a variable compression on
5121 * the parameters. Afterward, an inverse transformation is performed
5122 * and the equalities among the parameters are inserted back in.
5124 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5128 struct isl_mat
*T
, *T2
;
5129 struct isl_set
*set
;
5130 unsigned nparam
, n_div
;
5132 bset
= isl_basic_set_cow(bset
);
5136 if (bset
->n_eq
== 0)
5137 return isl_basic_set_lexmin(bset
);
5139 isl_basic_set_gauss(bset
, NULL
);
5141 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5142 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5144 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5145 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5148 if (i
== bset
->n_eq
)
5149 return isl_basic_set_lexmin(bset
);
5151 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5153 eq
= isl_mat_cow(eq
);
5154 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5155 if (T
&& T
->n_col
== 0) {
5159 bset
= isl_basic_set_set_to_empty(bset
);
5160 return isl_set_from_basic_set(bset
);
5162 bset
= basic_set_parameter_preimage(bset
, T
);
5164 set
= isl_basic_set_lexmin(bset
);
5165 set
= set_parameter_preimage(set
, T2
);
5166 set
= set_append_equalities(set
, eq
);
5170 /* Compute an explicit representation for all the existentially
5171 * quantified variables.
5172 * The input and output dimensions are first turned into parameters.
5173 * compute_divs then returns a map with the same parameters and
5174 * no input or output dimensions and the dimension specification
5175 * is reset to that of the input.
5177 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5179 struct isl_basic_set
*bset
;
5180 struct isl_set
*set
;
5181 struct isl_map
*map
;
5182 struct isl_dim
*dim
, *orig_dim
= NULL
;
5187 bmap
= isl_basic_map_cow(bmap
);
5191 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5192 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5193 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5194 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5198 orig_dim
= bmap
->dim
;
5200 bset
= (struct isl_basic_set
*)bmap
;
5202 set
= parameter_compute_divs(bset
);
5203 map
= (struct isl_map
*)set
;
5204 map
= isl_map_reset_dim(map
, orig_dim
);
5208 isl_basic_map_free(bmap
);
5212 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5220 off
= isl_dim_total(bmap
->dim
);
5221 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5222 if (isl_int_is_zero(bmap
->div
[i
][0]))
5224 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5230 static int map_divs_known(__isl_keep isl_map
*map
)
5237 for (i
= 0; i
< map
->n
; ++i
) {
5238 int known
= basic_map_divs_known(map
->p
[i
]);
5246 /* If bmap contains any unknown divs, then compute explicit
5247 * expressions for them. However, this computation may be
5248 * quite expensive, so first try to remove divs that aren't
5251 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5255 struct isl_map
*map
;
5257 known
= basic_map_divs_known(bmap
);
5261 return isl_map_from_basic_map(bmap
);
5263 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5265 known
= basic_map_divs_known(bmap
);
5269 return isl_map_from_basic_map(bmap
);
5271 map
= compute_divs(bmap
);
5274 isl_basic_map_free(bmap
);
5278 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5282 struct isl_map
*res
;
5289 known
= map_divs_known(map
);
5297 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5298 for (i
= 1 ; i
< map
->n
; ++i
) {
5300 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5301 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5302 res
= isl_map_union_disjoint(res
, r2
);
5304 res
= isl_map_union(res
, r2
);
5311 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5313 return (struct isl_set
*)
5314 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5317 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5319 return (struct isl_set
*)
5320 isl_map_compute_divs((struct isl_map
*)set
);
5323 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5326 struct isl_set
*set
;
5331 map
= isl_map_cow(map
);
5335 set
= (struct isl_set
*)map
;
5336 set
->dim
= isl_dim_domain(set
->dim
);
5339 for (i
= 0; i
< map
->n
; ++i
) {
5340 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5344 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5345 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5352 struct isl_map
*isl_map_union_disjoint(
5353 struct isl_map
*map1
, struct isl_map
*map2
)
5357 struct isl_map
*map
= NULL
;
5371 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5373 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5374 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5375 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5377 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5378 map1
->n
+ map2
->n
, flags
);
5381 for (i
= 0; i
< map1
->n
; ++i
) {
5382 map
= isl_map_add_basic_map(map
,
5383 isl_basic_map_copy(map1
->p
[i
]));
5387 for (i
= 0; i
< map2
->n
; ++i
) {
5388 map
= isl_map_add_basic_map(map
,
5389 isl_basic_map_copy(map2
->p
[i
]));
5403 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5405 map1
= isl_map_union_disjoint(map1
, map2
);
5409 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5413 struct isl_set
*isl_set_union_disjoint(
5414 struct isl_set
*set1
, struct isl_set
*set2
)
5416 return (struct isl_set
*)
5417 isl_map_union_disjoint(
5418 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5421 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5423 return (struct isl_set
*)
5424 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5427 struct isl_map
*isl_map_intersect_range(
5428 struct isl_map
*map
, struct isl_set
*set
)
5431 struct isl_map
*result
;
5437 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5438 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5439 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5441 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5442 map
->n
* set
->n
, flags
);
5445 for (i
= 0; i
< map
->n
; ++i
)
5446 for (j
= 0; j
< set
->n
; ++j
) {
5447 result
= isl_map_add_basic_map(result
,
5448 isl_basic_map_intersect_range(
5449 isl_basic_map_copy(map
->p
[i
]),
5450 isl_basic_set_copy(set
->p
[j
])));
5463 struct isl_map
*isl_map_intersect_domain(
5464 struct isl_map
*map
, struct isl_set
*set
)
5466 return isl_map_reverse(
5467 isl_map_intersect_range(isl_map_reverse(map
), set
));
5470 struct isl_map
*isl_map_apply_domain(
5471 struct isl_map
*map1
, struct isl_map
*map2
)
5475 map1
= isl_map_reverse(map1
);
5476 map1
= isl_map_apply_range(map1
, map2
);
5477 return isl_map_reverse(map1
);
5484 struct isl_map
*isl_map_apply_range(
5485 struct isl_map
*map1
, struct isl_map
*map2
)
5487 struct isl_dim
*dim_result
;
5488 struct isl_map
*result
;
5494 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5495 isl_dim_copy(map2
->dim
));
5497 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5500 for (i
= 0; i
< map1
->n
; ++i
)
5501 for (j
= 0; j
< map2
->n
; ++j
) {
5502 result
= isl_map_add_basic_map(result
,
5503 isl_basic_map_apply_range(
5504 isl_basic_map_copy(map1
->p
[i
]),
5505 isl_basic_map_copy(map2
->p
[j
])));
5511 if (result
&& result
->n
<= 1)
5512 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5521 * returns range - domain
5523 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5525 isl_dim
*dims
, *target_dim
;
5526 struct isl_basic_set
*bset
;
5533 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5534 bmap
->dim
, isl_dim_out
),
5536 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5537 dim
= isl_basic_map_n_in(bmap
);
5538 nparam
= isl_basic_map_n_param(bmap
);
5539 bset
= isl_basic_set_from_basic_map(bmap
);
5540 bset
= isl_basic_set_cow(bset
);
5541 dims
= isl_basic_set_get_dim(bset
);
5542 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5543 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5544 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5545 for (i
= 0; i
< dim
; ++i
) {
5546 int j
= isl_basic_map_alloc_equality(
5547 (struct isl_basic_map
*)bset
);
5550 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5551 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5552 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5553 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5555 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5556 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5559 isl_basic_map_free(bmap
);
5564 * returns range - domain
5566 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5570 struct isl_set
*result
;
5575 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5576 map
->dim
, isl_dim_out
),
5578 dim
= isl_map_get_dim(map
);
5579 dim
= isl_dim_domain(dim
);
5580 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5583 for (i
= 0; i
< map
->n
; ++i
)
5584 result
= isl_set_add_basic_set(result
,
5585 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5593 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5595 struct isl_basic_map
*bmap
;
5603 nparam
= dims
->nparam
;
5605 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5609 for (i
= 0; i
< dim
; ++i
) {
5610 int j
= isl_basic_map_alloc_equality(bmap
);
5613 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5614 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5615 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5617 return isl_basic_map_finalize(bmap
);
5619 isl_basic_map_free(bmap
);
5623 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5625 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5628 return basic_map_identity(dim
);
5631 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5633 if (!model
|| !model
->dim
)
5635 isl_assert(model
->ctx
,
5636 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5637 return basic_map_identity(isl_dim_copy(model
->dim
));
5640 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5642 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5643 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5646 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5648 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5651 return map_identity(dim
);
5654 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5656 if (!model
|| !model
->dim
)
5658 isl_assert(model
->ctx
,
5659 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5660 return map_identity(isl_dim_copy(model
->dim
));
5663 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5665 if (!model
|| !model
->dim
)
5667 isl_assert(model
->ctx
,
5668 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5669 return map_identity(isl_dim_copy(model
->dim
));
5672 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5674 isl_dim
*dim
= isl_set_get_dim(set
);
5676 id
= isl_map_identity(dim
);
5677 return isl_map_intersect_range(id
, set
);
5680 /* Construct a basic set with all set dimensions having only non-negative
5683 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5688 struct isl_basic_set
*bset
;
5692 nparam
= dims
->nparam
;
5694 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5697 for (i
= 0; i
< dim
; ++i
) {
5698 int k
= isl_basic_set_alloc_inequality(bset
);
5701 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5702 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5706 isl_basic_set_free(bset
);
5710 /* Construct the half-space x_pos >= 0.
5712 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5716 isl_basic_set
*nonneg
;
5718 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5719 k
= isl_basic_set_alloc_inequality(nonneg
);
5722 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5723 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5725 return isl_basic_set_finalize(nonneg
);
5727 isl_basic_set_free(nonneg
);
5731 /* Construct the half-space x_pos <= -1.
5733 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5738 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5739 k
= isl_basic_set_alloc_inequality(neg
);
5742 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5743 isl_int_set_si(neg
->ineq
[k
][0], -1);
5744 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5746 return isl_basic_set_finalize(neg
);
5748 isl_basic_set_free(neg
);
5752 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5753 enum isl_dim_type type
, unsigned first
, unsigned n
)
5756 isl_basic_set
*nonneg
;
5764 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5766 for (i
= 0; i
< n
; ++i
) {
5767 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5768 pos(set
->dim
, type
) + first
+ i
);
5769 neg
= neg_halfspace(isl_set_get_dim(set
),
5770 pos(set
->dim
, type
) + first
+ i
);
5772 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5781 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5782 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5789 if (isl_set_fast_is_empty(set
)) {
5794 return fn(set
, signs
, user
);
5797 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5799 half
= isl_set_intersect(half
, isl_set_copy(set
));
5800 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5804 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5806 half
= isl_set_intersect(half
, set
);
5807 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5813 /* Call "fn" on the intersections of "set" with each of the orthants
5814 * (except for obviously empty intersections). The orthant is identified
5815 * by the signs array, with each entry having value 1 or -1 according
5816 * to the sign of the corresponding variable.
5818 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5819 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5829 if (isl_set_fast_is_empty(set
))
5832 nparam
= isl_set_dim(set
, isl_dim_param
);
5833 nvar
= isl_set_dim(set
, isl_dim_set
);
5835 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5837 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5845 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5847 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5850 int isl_basic_map_is_subset(
5851 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5854 struct isl_map
*map1
;
5855 struct isl_map
*map2
;
5857 if (!bmap1
|| !bmap2
)
5860 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5861 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5863 is_subset
= isl_map_is_subset(map1
, map2
);
5871 int isl_basic_map_is_equal(
5872 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5876 if (!bmap1
|| !bmap2
)
5878 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5881 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5885 int isl_basic_set_is_equal(
5886 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5888 return isl_basic_map_is_equal(
5889 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5892 int isl_map_is_empty(struct isl_map
*map
)
5899 for (i
= 0; i
< map
->n
; ++i
) {
5900 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5909 int isl_map_fast_is_empty(struct isl_map
*map
)
5911 return map
? map
->n
== 0 : -1;
5914 int isl_set_fast_is_empty(struct isl_set
*set
)
5916 return set
? set
->n
== 0 : -1;
5919 int isl_set_is_empty(struct isl_set
*set
)
5921 return isl_map_is_empty((struct isl_map
*)set
);
5924 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5929 return isl_dim_equal(map1
->dim
, map2
->dim
);
5932 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5937 return isl_dim_equal(set1
->dim
, set2
->dim
);
5940 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5946 is_subset
= isl_map_is_subset(map1
, map2
);
5949 is_subset
= isl_map_is_subset(map2
, map1
);
5953 int isl_basic_map_is_strict_subset(
5954 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5958 if (!bmap1
|| !bmap2
)
5960 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5963 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5964 if (is_subset
== -1)
5969 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5975 is_subset
= isl_map_is_subset(map1
, map2
);
5978 is_subset
= isl_map_is_subset(map2
, map1
);
5979 if (is_subset
== -1)
5984 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5986 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5989 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5993 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5996 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6000 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6003 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
6010 for (i
= 0; i
< map
->n
; ++i
) {
6011 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6019 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6021 return isl_map_fast_is_universe((isl_map
*) set
);
6024 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6026 struct isl_basic_set
*bset
= NULL
;
6027 struct isl_vec
*sample
= NULL
;
6034 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6037 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6038 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6039 copy
= isl_basic_map_remove_redundancies(copy
);
6040 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6041 isl_basic_map_free(copy
);
6045 total
= 1 + isl_basic_map_total_dim(bmap
);
6046 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6047 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6053 isl_vec_free(bmap
->sample
);
6054 bmap
->sample
= NULL
;
6055 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6058 sample
= isl_basic_set_sample_vec(bset
);
6061 empty
= sample
->size
== 0;
6062 isl_vec_free(bmap
->sample
);
6063 bmap
->sample
= sample
;
6065 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6070 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6074 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6077 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6081 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6084 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6086 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6089 struct isl_map
*isl_basic_map_union(
6090 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6092 struct isl_map
*map
;
6093 if (!bmap1
|| !bmap2
)
6096 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6098 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6101 map
= isl_map_add_basic_map(map
, bmap1
);
6102 map
= isl_map_add_basic_map(map
, bmap2
);
6105 isl_basic_map_free(bmap1
);
6106 isl_basic_map_free(bmap2
);
6110 struct isl_set
*isl_basic_set_union(
6111 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6113 return (struct isl_set
*)isl_basic_map_union(
6114 (struct isl_basic_map
*)bset1
,
6115 (struct isl_basic_map
*)bset2
);
6118 /* Order divs such that any div only depends on previous divs */
6119 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6127 off
= isl_dim_total(bmap
->dim
);
6129 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6131 if (isl_int_is_zero(bmap
->div
[i
][0]))
6133 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6137 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6143 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6145 return (struct isl_basic_set
*)
6146 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6149 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6156 for (i
= 0; i
< map
->n
; ++i
) {
6157 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6168 /* Look for a div in dst that corresponds to the div "div" in src.
6169 * The divs before "div" in src and dst are assumed to be the same.
6171 * Returns -1 if no corresponding div was found and the position
6172 * of the corresponding div in dst otherwise.
6174 static int find_div(struct isl_basic_map
*dst
,
6175 struct isl_basic_map
*src
, unsigned div
)
6179 unsigned total
= isl_dim_total(src
->dim
);
6181 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6182 for (i
= div
; i
< dst
->n_div
; ++i
)
6183 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6184 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6185 dst
->n_div
- div
) == -1)
6190 struct isl_basic_map
*isl_basic_map_align_divs(
6191 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6194 unsigned total
= isl_dim_total(src
->dim
);
6199 if (src
->n_div
== 0)
6202 for (i
= 0; i
< src
->n_div
; ++i
)
6203 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6205 src
= isl_basic_map_order_divs(src
);
6206 dst
= isl_basic_map_cow(dst
);
6207 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6208 src
->n_div
, 0, 2 * src
->n_div
);
6211 for (i
= 0; i
< src
->n_div
; ++i
) {
6212 int j
= find_div(dst
, src
, i
);
6214 j
= isl_basic_map_alloc_div(dst
);
6217 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6218 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6219 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6223 isl_basic_map_swap_div(dst
, i
, j
);
6227 isl_basic_map_free(dst
);
6231 struct isl_basic_set
*isl_basic_set_align_divs(
6232 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6234 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6235 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6238 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6246 map
= isl_map_compute_divs(map
);
6247 map
= isl_map_cow(map
);
6251 for (i
= 1; i
< map
->n
; ++i
)
6252 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6253 for (i
= 1; i
< map
->n
; ++i
)
6254 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6256 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6260 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6262 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6265 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6269 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6270 map
= isl_map_intersect_domain(map
, set
);
6271 set
= isl_map_range(map
);
6279 /* There is no need to cow as removing empty parts doesn't change
6280 * the meaning of the set.
6282 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6289 for (i
= map
->n
-1; i
>= 0; --i
) {
6290 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6292 isl_basic_map_free(map
->p
[i
]);
6293 if (i
!= map
->n
-1) {
6294 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6295 map
->p
[i
] = map
->p
[map
->n
-1];
6303 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6305 return (struct isl_set
*)
6306 isl_map_remove_empty_parts((struct isl_map
*)set
);
6309 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6311 struct isl_basic_map
*bmap
;
6312 if (!map
|| map
->n
== 0)
6314 bmap
= map
->p
[map
->n
-1];
6315 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6316 return isl_basic_map_copy(bmap
);
6319 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6321 return (struct isl_basic_set
*)
6322 isl_map_copy_basic_map((struct isl_map
*)set
);
6325 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6326 __isl_keep isl_basic_map
*bmap
)
6332 for (i
= map
->n
-1; i
>= 0; --i
) {
6333 if (map
->p
[i
] != bmap
)
6335 map
= isl_map_cow(map
);
6338 isl_basic_map_free(map
->p
[i
]);
6339 if (i
!= map
->n
-1) {
6340 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6341 map
->p
[i
] = map
->p
[map
->n
-1];
6352 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6353 struct isl_basic_set
*bset
)
6355 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6356 (struct isl_basic_map
*)bset
);
6359 /* Given two basic sets bset1 and bset2, compute the maximal difference
6360 * between the values of dimension pos in bset1 and those in bset2
6361 * for any common value of the parameters and dimensions preceding pos.
6363 static enum isl_lp_result
basic_set_maximal_difference_at(
6364 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6365 int pos
, isl_int
*opt
)
6367 struct isl_dim
*dims
;
6368 struct isl_basic_map
*bmap1
= NULL
;
6369 struct isl_basic_map
*bmap2
= NULL
;
6370 struct isl_ctx
*ctx
;
6371 struct isl_vec
*obj
;
6374 unsigned dim1
, dim2
;
6375 enum isl_lp_result res
;
6377 if (!bset1
|| !bset2
)
6378 return isl_lp_error
;
6380 nparam
= isl_basic_set_n_param(bset1
);
6381 dim1
= isl_basic_set_n_dim(bset1
);
6382 dim2
= isl_basic_set_n_dim(bset2
);
6383 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6384 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6385 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6386 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6387 if (!bmap1
|| !bmap2
)
6389 bmap1
= isl_basic_map_cow(bmap1
);
6390 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6391 pos
, (dim1
- pos
) + (dim2
- pos
),
6392 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6393 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6396 total
= isl_basic_map_total_dim(bmap1
);
6398 obj
= isl_vec_alloc(ctx
, 1 + total
);
6399 isl_seq_clr(obj
->block
.data
, 1 + total
);
6400 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6401 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6404 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6406 isl_basic_map_free(bmap1
);
6410 isl_basic_map_free(bmap1
);
6411 isl_basic_map_free(bmap2
);
6412 return isl_lp_error
;
6415 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6416 * for any common value of the parameters and dimensions preceding pos
6417 * in both basic sets, the values of dimension pos in bset1 are
6418 * smaller or larger than those in bset2.
6421 * 1 if bset1 follows bset2
6422 * -1 if bset1 precedes bset2
6423 * 0 if bset1 and bset2 are incomparable
6424 * -2 if some error occurred.
6426 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6427 struct isl_basic_set
*bset2
, int pos
)
6430 enum isl_lp_result res
;
6435 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6437 if (res
== isl_lp_empty
)
6439 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6440 res
== isl_lp_unbounded
)
6442 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6451 /* Given two basic sets bset1 and bset2, check whether
6452 * for any common value of the parameters and dimensions preceding pos
6453 * there is a value of dimension pos in bset1 that is larger
6454 * than a value of the same dimension in bset2.
6457 * 1 if there exists such a pair
6458 * 0 if there is no such pair, but there is a pair of equal values
6460 * -2 if some error occurred.
6462 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6463 __isl_keep isl_basic_set
*bset2
, int pos
)
6466 enum isl_lp_result res
;
6471 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6473 if (res
== isl_lp_empty
)
6475 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6476 res
== isl_lp_unbounded
)
6478 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6480 else if (res
== isl_lp_ok
)
6489 /* Given two sets set1 and set2, check whether
6490 * for any common value of the parameters and dimensions preceding pos
6491 * there is a value of dimension pos in set1 that is larger
6492 * than a value of the same dimension in set2.
6495 * 1 if there exists such a pair
6496 * 0 if there is no such pair, but there is a pair of equal values
6498 * -2 if some error occurred.
6500 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6501 __isl_keep isl_set
*set2
, int pos
)
6509 for (i
= 0; i
< set1
->n
; ++i
)
6510 for (j
= 0; j
< set2
->n
; ++j
) {
6512 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6513 if (f
== 1 || f
== -2)
6522 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6523 unsigned pos
, isl_int
*val
)
6531 total
= isl_basic_map_total_dim(bmap
);
6532 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6533 for (; d
+1 > pos
; --d
)
6534 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6538 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6540 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6542 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6545 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6551 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6552 unsigned pos
, isl_int
*val
)
6564 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6567 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6568 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6569 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6570 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6574 isl_int_set(*val
, v
);
6580 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6581 unsigned pos
, isl_int
*val
)
6583 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6587 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6590 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6593 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6594 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6596 if (pos
>= isl_basic_map_dim(bmap
, type
))
6598 return isl_basic_map_fast_has_fixed_var(bmap
,
6599 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6602 int isl_map_fast_is_fixed(struct isl_map
*map
,
6603 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6605 if (pos
>= isl_map_dim(map
, type
))
6607 return isl_map_fast_has_fixed_var(map
,
6608 map_offset(map
, type
) - 1 + pos
, val
);
6611 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6612 * then return this fixed value in *val.
6614 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6617 return isl_basic_set_fast_has_fixed_var(bset
,
6618 isl_basic_set_n_param(bset
) + dim
, val
);
6621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6622 * then return this fixed value in *val.
6624 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6626 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6629 /* Check if input variable in has fixed value and if so and if val is not NULL,
6630 * then return this fixed value in *val.
6632 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6634 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6637 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6638 * and if val is not NULL, then return this lower bound in *val.
6640 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6641 unsigned dim
, isl_int
*val
)
6643 int i
, i_eq
= -1, i_ineq
= -1;
6650 total
= isl_basic_set_total_dim(bset
);
6651 nparam
= isl_basic_set_n_param(bset
);
6652 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6653 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6659 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6660 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6662 if (i_eq
!= -1 || i_ineq
!= -1)
6666 if (i_eq
== -1 && i_ineq
== -1)
6668 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6669 /* The coefficient should always be one due to normalization. */
6670 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6672 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6674 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6675 total
- nparam
- dim
- 1) != -1)
6678 isl_int_neg(*val
, c
[0]);
6682 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6683 unsigned dim
, isl_int
*val
)
6695 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6699 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6701 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6702 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6704 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6708 isl_int_set(*val
, v
);
6719 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6721 const struct constraint
*c1
= (const struct constraint
*)p1
;
6722 const struct constraint
*c2
= (const struct constraint
*)p2
;
6723 unsigned size
= isl_min(c1
->size
, c2
->size
);
6724 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6727 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6728 struct isl_basic_map
*bmap
)
6731 struct constraint
*c
;
6736 total
= isl_basic_map_total_dim(bmap
);
6737 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6740 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6742 c
[i
].c
= bmap
->ineq
[i
];
6744 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6745 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6746 bmap
->ineq
[i
] = c
[i
].c
;
6750 isl_basic_map_free(bmap
);
6754 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6755 __isl_take isl_basic_set
*bset
)
6757 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6758 (struct isl_basic_map
*)bset
);
6761 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6765 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6767 bmap
= isl_basic_map_remove_redundancies(bmap
);
6768 bmap
= isl_basic_map_sort_constraints(bmap
);
6769 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6773 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6775 return (struct isl_basic_set
*)isl_basic_map_normalize(
6776 (struct isl_basic_map
*)bset
);
6779 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6780 const __isl_keep isl_basic_map
*bmap2
)
6787 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6788 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6789 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6790 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6791 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6792 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6793 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6794 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6796 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6798 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6800 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6801 return bmap1
->n_eq
- bmap2
->n_eq
;
6802 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6803 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6804 if (bmap1
->n_div
!= bmap2
->n_div
)
6805 return bmap1
->n_div
- bmap2
->n_div
;
6806 total
= isl_basic_map_total_dim(bmap1
);
6807 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6808 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6812 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6813 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6817 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6818 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6825 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6826 __isl_keep isl_basic_map
*bmap2
)
6828 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6831 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6832 __isl_keep isl_basic_set
*bset2
)
6834 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6835 (isl_basic_map
*)bset2
);
6838 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6840 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6841 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6843 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6846 /* We normalize in place, but if anything goes wrong we need
6847 * to return NULL, so we need to make sure we don't change the
6848 * meaning of any possible other copies of map.
6850 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6853 struct isl_basic_map
*bmap
;
6857 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6859 for (i
= 0; i
< map
->n
; ++i
) {
6860 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6863 isl_basic_map_free(map
->p
[i
]);
6866 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6867 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6868 map
= isl_map_remove_empty_parts(map
);
6871 for (i
= map
->n
- 1; i
>= 1; --i
) {
6872 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6874 isl_basic_map_free(map
->p
[i
-1]);
6875 for (j
= i
; j
< map
->n
; ++j
)
6876 map
->p
[j
-1] = map
->p
[j
];
6886 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6888 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6891 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6901 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6904 map1
= isl_map_copy(map1
);
6905 map2
= isl_map_copy(map2
);
6906 map1
= isl_map_normalize(map1
);
6907 map2
= isl_map_normalize(map2
);
6910 equal
= map1
->n
== map2
->n
;
6911 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6912 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6925 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6927 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6928 (struct isl_map
*)set2
);
6931 /* Return an interval that ranges from min to max (inclusive)
6933 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6934 isl_int min
, isl_int max
)
6937 struct isl_basic_set
*bset
= NULL
;
6939 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6943 k
= isl_basic_set_alloc_inequality(bset
);
6946 isl_int_set_si(bset
->ineq
[k
][1], 1);
6947 isl_int_neg(bset
->ineq
[k
][0], min
);
6949 k
= isl_basic_set_alloc_inequality(bset
);
6952 isl_int_set_si(bset
->ineq
[k
][1], -1);
6953 isl_int_set(bset
->ineq
[k
][0], max
);
6957 isl_basic_set_free(bset
);
6961 /* Return the Cartesian product of the basic sets in list (in the given order).
6963 __isl_give isl_basic_set
*isl_basic_set_list_product(
6964 __isl_take
struct isl_basic_set_list
*list
)
6972 struct isl_basic_set
*product
= NULL
;
6976 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6977 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6978 nparam
= isl_basic_set_n_param(list
->p
[0]);
6979 dim
= isl_basic_set_n_dim(list
->p
[0]);
6980 extra
= list
->p
[0]->n_div
;
6981 n_eq
= list
->p
[0]->n_eq
;
6982 n_ineq
= list
->p
[0]->n_ineq
;
6983 for (i
= 1; i
< list
->n
; ++i
) {
6984 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6985 isl_assert(list
->ctx
,
6986 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6987 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6988 extra
+= list
->p
[i
]->n_div
;
6989 n_eq
+= list
->p
[i
]->n_eq
;
6990 n_ineq
+= list
->p
[i
]->n_ineq
;
6992 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6997 for (i
= 0; i
< list
->n
; ++i
) {
6998 isl_basic_set_add_constraints(product
,
6999 isl_basic_set_copy(list
->p
[i
]), dim
);
7000 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7002 isl_basic_set_list_free(list
);
7005 isl_basic_set_free(product
);
7006 isl_basic_set_list_free(list
);
7010 struct isl_basic_map
*isl_basic_map_product(
7011 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7013 struct isl_dim
*dim_result
= NULL
;
7014 struct isl_basic_map
*bmap
;
7015 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7016 struct isl_dim_map
*dim_map1
, *dim_map2
;
7018 if (!bmap1
|| !bmap2
)
7021 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7022 bmap2
->dim
, isl_dim_param
), goto error
);
7023 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7024 isl_dim_copy(bmap2
->dim
));
7026 in1
= isl_basic_map_n_in(bmap1
);
7027 in2
= isl_basic_map_n_in(bmap2
);
7028 out1
= isl_basic_map_n_out(bmap1
);
7029 out2
= isl_basic_map_n_out(bmap2
);
7030 nparam
= isl_basic_map_n_param(bmap1
);
7032 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7033 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7034 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7035 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7036 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7037 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7038 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7039 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7040 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7041 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7042 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7044 bmap
= isl_basic_map_alloc_dim(dim_result
,
7045 bmap1
->n_div
+ bmap2
->n_div
,
7046 bmap1
->n_eq
+ bmap2
->n_eq
,
7047 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7048 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7049 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7050 bmap
= isl_basic_map_simplify(bmap
);
7051 return isl_basic_map_finalize(bmap
);
7053 isl_basic_map_free(bmap1
);
7054 isl_basic_map_free(bmap2
);
7058 __isl_give isl_basic_map
*isl_basic_map_range_product(
7059 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7061 isl_dim
*dim_result
= NULL
;
7062 isl_basic_map
*bmap
;
7063 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7064 struct isl_dim_map
*dim_map1
, *dim_map2
;
7066 if (!bmap1
|| !bmap2
)
7069 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7070 isl_dim_copy(bmap2
->dim
));
7072 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7073 out1
= isl_basic_map_n_out(bmap1
);
7074 out2
= isl_basic_map_n_out(bmap2
);
7075 nparam
= isl_basic_map_n_param(bmap1
);
7077 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7078 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7079 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7080 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7081 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7082 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7083 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7084 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7085 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7086 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7087 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7089 bmap
= isl_basic_map_alloc_dim(dim_result
,
7090 bmap1
->n_div
+ bmap2
->n_div
,
7091 bmap1
->n_eq
+ bmap2
->n_eq
,
7092 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7093 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7094 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7095 bmap
= isl_basic_map_simplify(bmap
);
7096 return isl_basic_map_finalize(bmap
);
7098 isl_basic_map_free(bmap1
);
7099 isl_basic_map_free(bmap2
);
7103 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7104 __isl_take isl_map
*map2
,
7105 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7106 __isl_take isl_dim
*right
),
7107 __isl_give isl_basic_map
*(*basic_map_product
)(
7108 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7111 struct isl_map
*result
;
7117 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7118 map2
->dim
, isl_dim_param
), goto error
);
7120 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7121 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7122 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7124 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7125 isl_dim_copy(map2
->dim
)),
7126 map1
->n
* map2
->n
, flags
);
7129 for (i
= 0; i
< map1
->n
; ++i
)
7130 for (j
= 0; j
< map2
->n
; ++j
) {
7131 struct isl_basic_map
*part
;
7132 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7133 isl_basic_map_copy(map2
->p
[j
]));
7134 if (isl_basic_map_is_empty(part
))
7135 isl_basic_map_free(part
);
7137 result
= isl_map_add_basic_map(result
, part
);
7150 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7152 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7154 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7157 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7159 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7160 __isl_take isl_map
*map2
)
7164 prod
= isl_map_product(map1
, map2
);
7165 prod
= isl_map_flatten(prod
);
7169 /* Given two set A and B, construct its Cartesian product A x B.
7171 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7173 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7174 (struct isl_map
*)set2
);
7177 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7178 __isl_take isl_set
*set2
)
7180 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7183 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7185 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7186 __isl_take isl_map
*map2
)
7188 return map_product(map1
, map2
, &isl_dim_range_product
,
7189 &isl_basic_map_range_product
);
7192 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7195 uint32_t hash
= isl_hash_init();
7200 bmap
= isl_basic_map_copy(bmap
);
7201 bmap
= isl_basic_map_normalize(bmap
);
7204 total
= isl_basic_map_total_dim(bmap
);
7205 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7206 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7208 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7209 isl_hash_hash(hash
, c_hash
);
7211 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7212 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7214 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7215 isl_hash_hash(hash
, c_hash
);
7217 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7218 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7220 if (isl_int_is_zero(bmap
->div
[i
][0]))
7222 isl_hash_byte(hash
, i
& 0xFF);
7223 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7224 isl_hash_hash(hash
, c_hash
);
7226 isl_basic_map_free(bmap
);
7230 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7232 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7235 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7242 map
= isl_map_copy(map
);
7243 map
= isl_map_normalize(map
);
7247 hash
= isl_hash_init();
7248 for (i
= 0; i
< map
->n
; ++i
) {
7250 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7251 isl_hash_hash(hash
, bmap_hash
);
7259 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7261 return isl_map_get_hash((isl_map
*)set
);
7264 /* Check if the value for dimension dim is completely determined
7265 * by the values of the other parameters and variables.
7266 * That is, check if dimension dim is involved in an equality.
7268 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7275 nparam
= isl_basic_set_n_param(bset
);
7276 for (i
= 0; i
< bset
->n_eq
; ++i
)
7277 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7282 /* Check if the value for dimension dim is completely determined
7283 * by the values of the other parameters and variables.
7284 * That is, check if dimension dim is involved in an equality
7285 * for each of the subsets.
7287 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7293 for (i
= 0; i
< set
->n
; ++i
) {
7295 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7302 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7304 return set
? set
->n
: 0;
7307 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7308 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7315 for (i
= 0; i
< map
->n
; ++i
)
7316 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7322 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7323 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7330 for (i
= 0; i
< set
->n
; ++i
)
7331 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7337 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7339 struct isl_dim
*dim
;
7344 if (bset
->n_div
== 0)
7347 bset
= isl_basic_set_cow(bset
);
7351 dim
= isl_basic_set_get_dim(bset
);
7352 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
7355 isl_dim_free(bset
->dim
);
7357 bset
->extra
-= bset
->n_div
;
7360 bset
= isl_basic_set_finalize(bset
);
7364 isl_basic_set_free(bset
);
7368 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7371 struct isl_dim
*dim
;
7374 set
= isl_set_align_divs(set
);
7378 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
7381 set
= isl_set_cow(set
);
7385 n_div
= set
->p
[0]->n_div
;
7386 dim
= isl_set_get_dim(set
);
7387 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
7390 isl_dim_free(set
->dim
);
7393 for (i
= 0; i
< set
->n
; ++i
) {
7394 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7405 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7407 struct isl_dim
*dim
;
7408 struct isl_basic_map
*bmap
;
7415 set
= isl_set_align_divs(set
);
7420 dim
= isl_set_get_dim(set
);
7421 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7423 return isl_map_identity(dim
);
7426 n_div
= set
->p
[0]->n_div
;
7427 dim
= isl_dim_map(dim
);
7428 n_param
= isl_dim_size(dim
, isl_dim_param
);
7429 n_set
= isl_dim_size(dim
, isl_dim_in
);
7430 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7431 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7432 for (i
= 0; i
< n_set
; ++i
)
7433 bmap
= var_equal(bmap
, i
);
7435 total
= n_param
+ n_set
+ n_set
+ n_div
;
7436 for (i
= 0; i
< n_div
; ++i
) {
7437 k
= isl_basic_map_alloc_inequality(bmap
);
7440 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7441 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7442 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7443 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7444 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7445 set
->p
[0]->div
[i
][0]);
7447 l
= isl_basic_map_alloc_inequality(bmap
);
7450 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7451 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7452 set
->p
[0]->div
[i
][0]);
7453 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7457 return isl_map_from_basic_map(bmap
);
7460 isl_basic_map_free(bmap
);
7464 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7472 dim
= isl_basic_set_total_dim(bset
);
7473 size
+= bset
->n_eq
* (1 + dim
);
7474 size
+= bset
->n_ineq
* (1 + dim
);
7475 size
+= bset
->n_div
* (2 + dim
);
7480 int isl_set_size(__isl_keep isl_set
*set
)
7488 for (i
= 0; i
< set
->n
; ++i
)
7489 size
+= isl_basic_set_size(set
->p
[i
]);
7494 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7495 enum isl_dim_type type
, unsigned pos
)
7503 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7505 pos
+= isl_basic_map_offset(bmap
, type
);
7507 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7508 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7512 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7513 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7520 return lower
&& upper
;
7523 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7524 enum isl_dim_type type
, unsigned pos
)
7531 for (i
= 0; i
< map
->n
; ++i
) {
7533 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7534 if (bounded
< 0 || !bounded
)
7541 /* Return 1 if the specified dim is involved in both an upper bound
7542 * and a lower bound.
7544 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7545 enum isl_dim_type type
, unsigned pos
)
7547 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7550 /* For each of the "n" variables starting at "first", determine
7551 * the sign of the variable and put the results in the first "n"
7552 * elements of the array "signs".
7554 * 1 means that the variable is non-negative
7555 * -1 means that the variable is non-positive
7556 * 0 means the variable attains both positive and negative values.
7558 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7559 unsigned first
, unsigned n
, int *signs
)
7561 isl_vec
*bound
= NULL
;
7562 struct isl_tab
*tab
= NULL
;
7563 struct isl_tab_undo
*snap
;
7566 if (!bset
|| !signs
)
7569 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7570 tab
= isl_tab_from_basic_set(bset
);
7574 isl_seq_clr(bound
->el
, bound
->size
);
7575 isl_int_set_si(bound
->el
[0], -1);
7577 snap
= isl_tab_snap(tab
);
7578 for (i
= 0; i
< n
; ++i
) {
7581 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7582 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7585 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7586 if (isl_tab_rollback(tab
, snap
) < 0)
7594 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7595 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7598 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7599 if (isl_tab_rollback(tab
, snap
) < 0)
7602 signs
[i
] = empty
? -1 : 0;
7606 isl_vec_free(bound
);
7610 isl_vec_free(bound
);
7614 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7615 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7617 if (!bset
|| !signs
)
7619 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7622 first
+= pos(bset
->dim
, type
) - 1;
7623 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7626 /* Check if the given basic map is obviously single-valued.
7627 * In particular, for each output dimension, check that there is
7628 * an equality that defines the output dimension in terms of
7629 * earlier dimensions.
7631 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7641 total
= 1 + isl_basic_map_total_dim(bmap
);
7642 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7643 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7645 for (i
= 0; i
< n_out
; ++i
) {
7646 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7647 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7649 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7650 total
- (o_out
+ i
+ 1)) == -1)
7653 if (j
>= bmap
->n_eq
)
7660 /* Check if the given map is obviously single-valued.
7662 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7673 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7676 /* Check if the given map is single-valued.
7681 * and check if the result is a subset of the identity mapping.
7683 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7689 sv
= isl_map_fast_is_single_valued(map
);
7693 test
= isl_map_reverse(isl_map_copy(map
));
7694 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7696 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
7698 sv
= isl_map_is_subset(test
, id
);
7706 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7710 sv
= isl_map_is_single_valued(map
);
7714 map
= isl_map_copy(map
);
7715 map
= isl_map_reverse(map
);
7716 sv
= isl_map_is_single_valued(map
);
7722 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7724 return isl_map_is_single_valued((isl_map
*)set
);
7727 int isl_map_is_translation(__isl_keep isl_map
*map
)
7732 delta
= isl_map_deltas(isl_map_copy(map
));
7733 ok
= isl_set_is_singleton(delta
);
7734 isl_set_free(delta
);
7739 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7741 if (isl_seq_first_non_zero(p
, pos
) != -1)
7743 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7748 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7757 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7760 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7761 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7762 for (j
= 0; j
< nvar
; ++j
) {
7763 int lower
= 0, upper
= 0;
7764 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7765 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7767 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7773 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7774 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7776 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7778 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7783 if (!lower
|| !upper
)
7790 int isl_set_is_box(__isl_keep isl_set
*set
)
7797 return isl_basic_set_is_box(set
->p
[0]);
7800 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7805 return isl_dim_is_wrapping(bset
->dim
);
7808 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7813 return isl_dim_is_wrapping(set
->dim
);
7816 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7818 bmap
= isl_basic_map_cow(bmap
);
7822 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7826 bmap
= isl_basic_map_finalize(bmap
);
7828 return (isl_basic_set
*)bmap
;
7830 isl_basic_map_free(bmap
);
7834 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7838 map
= isl_map_cow(map
);
7842 for (i
= 0; i
< map
->n
; ++i
) {
7843 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7847 map
->dim
= isl_dim_wrap(map
->dim
);
7851 return (isl_set
*)map
;
7857 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7859 bset
= isl_basic_set_cow(bset
);
7863 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7867 bset
= isl_basic_set_finalize(bset
);
7869 return (isl_basic_map
*)bset
;
7871 isl_basic_set_free(bset
);
7875 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7882 if (!isl_set_is_wrapping(set
))
7883 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7886 set
= isl_set_cow(set
);
7890 for (i
= 0; i
< set
->n
; ++i
) {
7891 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7896 set
->dim
= isl_dim_unwrap(set
->dim
);
7900 return (isl_map
*)set
;
7906 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7907 enum isl_dim_type type
)
7912 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7915 bmap
= isl_basic_map_cow(bmap
);
7919 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7923 bmap
= isl_basic_map_finalize(bmap
);
7927 isl_basic_map_free(bmap
);
7931 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7932 enum isl_dim_type type
)
7939 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7942 map
= isl_map_cow(map
);
7946 for (i
= 0; i
< map
->n
; ++i
) {
7947 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7951 map
->dim
= isl_dim_reset(map
->dim
, type
);
7961 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7966 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7969 bmap
= isl_basic_map_cow(bmap
);
7973 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
7977 bmap
= isl_basic_map_finalize(bmap
);
7981 isl_basic_map_free(bmap
);
7985 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
7987 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
7990 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
7997 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8000 map
= isl_map_cow(map
);
8004 for (i
= 0; i
< map
->n
; ++i
) {
8005 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8009 map
->dim
= isl_dim_flatten(map
->dim
);
8019 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8021 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8024 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8026 isl_dim
*dim
, *flat_dim
;
8029 dim
= isl_set_get_dim(set
);
8030 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8031 map
= map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8032 map
= isl_map_intersect_domain(map
, set
);
8037 /* Extend the given dim_map with mappings for the divs in bmap.
8039 static __isl_give
struct isl_dim_map
*extend_dim_map(
8040 __isl_keep
struct isl_dim_map
*dim_map
,
8041 __isl_keep isl_basic_map
*bmap
)
8044 struct isl_dim_map
*res
;
8047 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
8049 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
8053 for (i
= 0; i
< dim_map
->len
; ++i
)
8054 res
->pos
[i
] = dim_map
->pos
[i
];
8055 for (i
= 0; i
< bmap
->n_div
; ++i
)
8056 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
8061 /* Extract a dim_map from a reordering.
8062 * We essentially need to reverse the mapping, and add an offset
8063 * of 1 for the constant term.
8065 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
8066 __isl_keep isl_reordering
*exp
)
8069 struct isl_dim_map
*dim_map
;
8074 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
8078 for (i
= 0; i
< exp
->len
; ++i
)
8079 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
8084 /* Reorder the dimensions of "bmap" according to the given dim_map
8085 * and set the dimension specification to "dim".
8087 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8088 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8092 bmap
= isl_basic_map_cow(bmap
);
8093 if (!bmap
|| !dim
|| !dim_map
)
8096 res
= isl_basic_map_alloc_dim(dim
,
8097 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8098 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
8099 res
= isl_basic_map_finalize(res
);
8103 isl_basic_map_free(bmap
);
8108 /* Reorder the dimensions of "map" according to given reordering.
8110 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8111 __isl_take isl_reordering
*r
)
8114 struct isl_dim_map
*dim_map
;
8116 map
= isl_map_cow(map
);
8117 dim_map
= isl_dim_map_from_reordering(r
);
8118 if (!map
|| !r
|| !dim_map
)
8121 for (i
= 0; i
< map
->n
; ++i
) {
8122 struct isl_dim_map
*dim_map_i
;
8124 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
8126 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8127 isl_dim_copy(r
->dim
), dim_map_i
);
8133 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8135 isl_reordering_free(r
);
8141 isl_reordering_free(r
);
8145 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8146 __isl_take isl_reordering
*r
)
8148 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8151 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8152 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8153 enum isl_dim_type c2
, enum isl_dim_type c3
,
8154 enum isl_dim_type c4
, enum isl_dim_type c5
)
8156 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8157 struct isl_mat
*mat
;
8163 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8164 isl_basic_map_total_dim(bmap
) + 1);
8167 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8168 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8169 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8170 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8171 isl_int_set(mat
->row
[i
][pos
],
8172 bmap
->eq
[i
][off
+ k
]);
8180 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8181 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8182 enum isl_dim_type c2
, enum isl_dim_type c3
,
8183 enum isl_dim_type c4
, enum isl_dim_type c5
)
8185 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8186 struct isl_mat
*mat
;
8192 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8193 isl_basic_map_total_dim(bmap
) + 1);
8196 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8197 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8198 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8199 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8200 isl_int_set(mat
->row
[i
][pos
],
8201 bmap
->ineq
[i
][off
+ k
]);
8209 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8210 __isl_take isl_dim
*dim
,
8211 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8212 enum isl_dim_type c2
, enum isl_dim_type c3
,
8213 enum isl_dim_type c4
, enum isl_dim_type c5
)
8215 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8216 isl_basic_map
*bmap
;
8222 if (!dim
|| !eq
|| !ineq
)
8225 if (eq
->n_col
!= ineq
->n_col
)
8226 isl_die(dim
->ctx
, isl_error_invalid
,
8227 "equalities and inequalities matrices should have "
8228 "same number of columns", goto error
);
8230 total
= 1 + isl_dim_total(dim
);
8232 if (eq
->n_col
< total
)
8233 isl_die(dim
->ctx
, isl_error_invalid
,
8234 "number of columns too small", goto error
);
8236 extra
= eq
->n_col
- total
;
8238 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8239 eq
->n_row
, ineq
->n_row
);
8242 for (i
= 0; i
< extra
; ++i
) {
8243 k
= isl_basic_map_alloc_div(bmap
);
8246 isl_int_set_si(bmap
->div
[k
][0], 0);
8248 for (i
= 0; i
< eq
->n_row
; ++i
) {
8249 l
= isl_basic_map_alloc_equality(bmap
);
8252 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8253 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8254 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8255 isl_int_set(bmap
->eq
[l
][off
+ k
],
8261 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8262 l
= isl_basic_map_alloc_inequality(bmap
);
8265 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8266 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8267 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8268 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8287 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8288 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8289 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8291 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8292 c1
, c2
, c3
, c4
, isl_dim_in
);
8295 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8296 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8297 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8299 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8300 c1
, c2
, c3
, c4
, isl_dim_in
);
8303 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8304 __isl_take isl_dim
*dim
,
8305 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8306 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8308 return (isl_basic_set
*)
8309 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8310 c1
, c2
, c3
, c4
, isl_dim_in
);