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 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
466 return isl_basic_map_is_rational(bset
);
469 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
470 struct isl_basic_map
*bmap
, unsigned extra
,
471 unsigned n_eq
, unsigned n_ineq
)
474 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
479 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
480 if (isl_blk_is_error(bmap
->block
))
483 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
488 bmap
->block2
= isl_blk_empty();
491 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
492 if (isl_blk_is_error(bmap
->block2
))
495 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
500 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
501 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
503 for (i
= 0; i
< extra
; ++i
)
504 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
508 bmap
->c_size
= n_eq
+ n_ineq
;
509 bmap
->eq
= bmap
->ineq
+ n_ineq
;
518 isl_basic_map_free(bmap
);
522 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
523 unsigned nparam
, unsigned dim
, unsigned extra
,
524 unsigned n_eq
, unsigned n_ineq
)
526 struct isl_basic_map
*bmap
;
527 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
528 return (struct isl_basic_set
*)bmap
;
531 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
532 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
534 struct isl_basic_map
*bmap
;
537 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
538 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
539 return (struct isl_basic_set
*)bmap
;
545 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
546 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
548 struct isl_basic_map
*bmap
;
552 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
557 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
563 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
564 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
565 unsigned n_eq
, unsigned n_ineq
)
567 struct isl_basic_map
*bmap
;
570 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
574 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
578 static void dup_constraints(
579 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
582 unsigned total
= isl_basic_map_total_dim(src
);
584 for (i
= 0; i
< src
->n_eq
; ++i
) {
585 int j
= isl_basic_map_alloc_equality(dst
);
586 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
589 for (i
= 0; i
< src
->n_ineq
; ++i
) {
590 int j
= isl_basic_map_alloc_inequality(dst
);
591 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
594 for (i
= 0; i
< src
->n_div
; ++i
) {
595 int j
= isl_basic_map_alloc_div(dst
);
596 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
598 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
601 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
603 struct isl_basic_map
*dup
;
607 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
608 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
611 dup_constraints(dup
, bmap
);
612 dup
->flags
= bmap
->flags
;
613 dup
->sample
= isl_vec_copy(bmap
->sample
);
617 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
619 struct isl_basic_map
*dup
;
621 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
622 return (struct isl_basic_set
*)dup
;
625 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
630 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
634 return isl_basic_set_dup(bset
);
637 struct isl_set
*isl_set_copy(struct isl_set
*set
)
646 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
651 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
655 bmap
= isl_basic_map_dup(bmap
);
657 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
661 struct isl_map
*isl_map_copy(struct isl_map
*map
)
670 void isl_basic_map_free(struct isl_basic_map
*bmap
)
678 isl_ctx_deref(bmap
->ctx
);
680 isl_blk_free(bmap
->ctx
, bmap
->block2
);
682 isl_blk_free(bmap
->ctx
, bmap
->block
);
683 isl_vec_free(bmap
->sample
);
684 isl_dim_free(bmap
->dim
);
688 void isl_basic_set_free(struct isl_basic_set
*bset
)
690 isl_basic_map_free((struct isl_basic_map
*)bset
);
693 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
695 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
698 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
704 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
705 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
707 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
708 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
709 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
710 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
711 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
712 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
714 int j
= isl_basic_map_alloc_inequality(bmap
);
718 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
719 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
726 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
727 bmap
->extra
- bmap
->n_div
);
731 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
733 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
736 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
740 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
745 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
747 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
750 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
755 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
757 if (pos
!= bmap
->n_eq
- 1) {
759 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
760 bmap
->eq
[bmap
->n_eq
- 1] = t
;
766 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
768 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
771 void isl_basic_map_inequality_to_equality(
772 struct isl_basic_map
*bmap
, unsigned pos
)
777 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
778 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
783 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
784 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
785 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
786 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
789 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
791 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
794 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
800 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
801 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
802 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
803 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
804 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
805 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
806 1 + isl_basic_map_total_dim(bmap
),
807 bmap
->extra
- bmap
->n_div
);
808 return bmap
->n_ineq
++;
811 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
813 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
816 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
820 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
825 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
827 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
830 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
835 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
837 if (pos
!= bmap
->n_ineq
- 1) {
839 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
840 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
841 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
847 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
849 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
852 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
857 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
860 k
= isl_basic_map_alloc_equality(bmap
);
863 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
866 isl_basic_map_free(bmap
);
870 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
873 return (isl_basic_set
*)
874 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
877 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
882 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
885 k
= isl_basic_map_alloc_inequality(bmap
);
888 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
891 isl_basic_map_free(bmap
);
895 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
898 return (isl_basic_set
*)
899 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
902 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
906 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
907 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
908 1 + 1 + isl_basic_map_total_dim(bmap
),
909 bmap
->extra
- bmap
->n_div
);
910 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
911 return bmap
->n_div
++;
914 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
916 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
919 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
923 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
928 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
930 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
933 /* Copy constraint from src to dst, putting the vars of src at offset
934 * dim_off in dst and the divs of src at offset div_off in dst.
935 * If both sets are actually map, then dim_off applies to the input
938 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
939 struct isl_basic_map
*src_map
, isl_int
*src
,
940 unsigned in_off
, unsigned out_off
, unsigned div_off
)
942 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
943 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
944 unsigned src_in
= isl_basic_map_n_in(src_map
);
945 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
946 unsigned src_out
= isl_basic_map_n_out(src_map
);
947 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
948 isl_int_set(dst
[0], src
[0]);
949 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
950 if (dst_nparam
> src_nparam
)
951 isl_seq_clr(dst
+1+src_nparam
,
952 dst_nparam
- src_nparam
);
953 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
954 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
956 isl_min(dst_in
-in_off
, src_in
));
957 if (dst_in
-in_off
> src_in
)
958 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
959 dst_in
- in_off
- src_in
);
960 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
961 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
962 src
+1+src_nparam
+src_in
,
963 isl_min(dst_out
-out_off
, src_out
));
964 if (dst_out
-out_off
> src_out
)
965 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
966 dst_out
- out_off
- src_out
);
967 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
968 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
969 src
+1+src_nparam
+src_in
+src_out
,
970 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
971 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
972 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
973 div_off
+src_map
->n_div
,
974 dst_map
->n_div
- div_off
- src_map
->n_div
);
977 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
978 struct isl_basic_map
*src_map
, isl_int
*src
,
979 unsigned in_off
, unsigned out_off
, unsigned div_off
)
981 isl_int_set(dst
[0], src
[0]);
982 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
985 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
986 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
991 if (!bmap1
|| !bmap2
)
994 div_off
= bmap1
->n_div
;
996 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
997 int i1
= isl_basic_map_alloc_equality(bmap1
);
1000 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1001 i_pos
, o_pos
, div_off
);
1004 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1005 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1008 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1009 i_pos
, o_pos
, div_off
);
1012 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1013 int i1
= isl_basic_map_alloc_div(bmap1
);
1016 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1017 i_pos
, o_pos
, div_off
);
1020 isl_basic_map_free(bmap2
);
1025 isl_basic_map_free(bmap1
);
1026 isl_basic_map_free(bmap2
);
1030 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
1031 struct isl_dim_map
*dim_map
)
1035 for (i
= 0; i
< dim_map
->len
; ++i
) {
1036 if (dim_map
->pos
[i
] < 0)
1037 isl_int_set_si(dst
[i
], 0);
1039 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
1043 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
1044 struct isl_dim_map
*dim_map
)
1046 isl_int_set(dst
[0], src
[0]);
1047 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
1050 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
1051 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
1055 if (!src
|| !dst
|| !dim_map
)
1058 for (i
= 0; i
< src
->n_eq
; ++i
) {
1059 int i1
= isl_basic_map_alloc_equality(dst
);
1062 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
1065 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1066 int i1
= isl_basic_map_alloc_inequality(dst
);
1069 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1072 for (i
= 0; i
< src
->n_div
; ++i
) {
1073 int i1
= isl_basic_map_alloc_div(dst
);
1076 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1080 isl_basic_map_free(src
);
1085 isl_basic_map_free(src
);
1086 isl_basic_map_free(dst
);
1090 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1091 struct isl_basic_set
*bset2
, unsigned pos
)
1093 return (struct isl_basic_set
*)
1094 add_constraints((struct isl_basic_map
*)bset1
,
1095 (struct isl_basic_map
*)bset2
, 0, pos
);
1098 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1099 struct isl_dim
*dim
, unsigned extra
,
1100 unsigned n_eq
, unsigned n_ineq
)
1102 struct isl_basic_map
*ext
;
1112 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1113 base
->extra
>= base
->n_div
+ extra
;
1115 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1116 room_for_ineq(base
, n_ineq
)) {
1121 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1122 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1123 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1124 extra
+= base
->extra
;
1126 n_ineq
+= base
->n_ineq
;
1128 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1134 ext
->sample
= isl_vec_copy(base
->sample
);
1135 flags
= base
->flags
;
1136 ext
= add_constraints(ext
, base
, 0, 0);
1139 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1146 isl_basic_map_free(base
);
1150 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1151 struct isl_dim
*dim
, unsigned extra
,
1152 unsigned n_eq
, unsigned n_ineq
)
1154 return (struct isl_basic_set
*)
1155 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1156 extra
, n_eq
, n_ineq
);
1159 struct isl_basic_map
*isl_basic_map_extend_constraints(
1160 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1164 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1168 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1169 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1170 unsigned n_eq
, unsigned n_ineq
)
1172 struct isl_basic_map
*bmap
;
1173 struct isl_dim
*dim
;
1177 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1181 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1184 isl_basic_map_free(base
);
1188 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1189 unsigned nparam
, unsigned dim
, unsigned extra
,
1190 unsigned n_eq
, unsigned n_ineq
)
1192 return (struct isl_basic_set
*)
1193 isl_basic_map_extend((struct isl_basic_map
*)base
,
1194 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1197 struct isl_basic_set
*isl_basic_set_extend_constraints(
1198 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1200 return (struct isl_basic_set
*)
1201 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1205 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1207 return (struct isl_basic_set
*)
1208 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1211 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1216 if (bmap
->ref
> 1) {
1218 bmap
= isl_basic_map_dup(bmap
);
1221 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1225 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1233 return isl_set_dup(set
);
1236 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1244 return isl_map_dup(map
);
1247 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1248 unsigned a_len
, unsigned b_len
)
1250 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1251 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1252 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1255 struct isl_basic_set
*isl_basic_set_swap_vars(
1256 struct isl_basic_set
*bset
, unsigned n
)
1266 nparam
= isl_basic_set_n_param(bset
);
1267 dim
= isl_basic_set_n_dim(bset
);
1268 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1273 bset
= isl_basic_set_cow(bset
);
1277 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1278 if (isl_blk_is_error(blk
))
1281 for (i
= 0; i
< bset
->n_eq
; ++i
)
1283 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1285 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1287 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1289 for (i
= 0; i
< bset
->n_div
; ++i
)
1291 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1293 isl_blk_free(bset
->ctx
, blk
);
1295 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1296 bset
= isl_basic_set_gauss(bset
, NULL
);
1297 return isl_basic_set_finalize(bset
);
1299 isl_basic_set_free(bset
);
1303 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1306 set
= isl_set_cow(set
);
1310 for (i
= 0; i
< set
->n
; ++i
) {
1311 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1317 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1321 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1327 total
= isl_basic_map_total_dim(bmap
);
1328 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1329 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1331 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1333 i
= isl_basic_map_alloc_equality(bmap
);
1337 isl_int_set_si(bmap
->eq
[i
][0], 1);
1338 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1339 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1340 isl_vec_free(bmap
->sample
);
1341 bmap
->sample
= NULL
;
1342 return isl_basic_map_finalize(bmap
);
1344 isl_basic_map_free(bmap
);
1348 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1350 return (struct isl_basic_set
*)
1351 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1354 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1357 unsigned off
= isl_dim_total(bmap
->dim
);
1358 isl_int
*t
= bmap
->div
[a
];
1359 bmap
->div
[a
] = bmap
->div
[b
];
1362 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1363 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1365 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1366 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1368 for (i
= 0; i
< bmap
->n_div
; ++i
)
1369 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1373 /* Eliminate the specified n dimensions starting at first from the
1374 * constraints using Fourier-Motzkin. The dimensions themselves
1377 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1378 enum isl_dim_type type
, unsigned first
, unsigned n
)
1387 map
= isl_map_cow(map
);
1390 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1391 first
+= pos(map
->dim
, type
) - 1;
1393 for (i
= 0; i
< map
->n
; ++i
) {
1394 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1404 /* Eliminate the specified n dimensions starting at first from the
1405 * constraints using Fourier-Motzkin. The dimensions themselves
1408 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1409 enum isl_dim_type type
, unsigned first
, unsigned n
)
1411 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1414 /* Eliminate the specified n dimensions starting at first from the
1415 * constraints using Fourier-Motzkin. The dimensions themselves
1418 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1419 unsigned first
, unsigned n
)
1421 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1424 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1425 __isl_take isl_basic_map
*bmap
)
1427 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1432 return isl_basic_map_finalize(bmap
);
1435 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1436 __isl_take isl_basic_set
*bset
)
1438 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1439 (struct isl_basic_map
*)bset
);
1442 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1451 set
= isl_set_cow(set
);
1455 for (i
= 0; i
< set
->n
; ++i
) {
1456 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1466 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1467 enum isl_dim_type type
, unsigned first
, unsigned n
)
1471 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1475 bmap
= isl_basic_map_eliminate_vars(bmap
,
1476 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1479 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1481 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1484 isl_basic_map_free(bmap
);
1488 /* Return true if the definition of the given div (recursively) involves
1489 * any of the given variables.
1491 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1492 unsigned first
, unsigned n
)
1495 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1497 if (isl_int_is_zero(bmap
->div
[div
][0]))
1499 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1502 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1503 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1505 if (div_involves_vars(bmap
, i
, first
, n
))
1512 /* Remove all divs (recursively) involving any of the given dimensions
1513 * in their definitions.
1515 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1516 __isl_take isl_basic_map
*bmap
,
1517 enum isl_dim_type type
, unsigned first
, unsigned n
)
1523 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1525 first
+= isl_basic_map_offset(bmap
, type
);
1527 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1528 if (!div_involves_vars(bmap
, i
, first
, n
))
1530 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1535 isl_basic_map_free(bmap
);
1539 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1540 enum isl_dim_type type
, unsigned first
, unsigned n
)
1549 map
= isl_map_cow(map
);
1553 for (i
= 0; i
< map
->n
; ++i
) {
1554 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1565 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1566 enum isl_dim_type type
, unsigned first
, unsigned n
)
1568 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1572 /* Return true if the definition of the given div is unknown or depends
1575 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1578 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1580 if (isl_int_is_zero(bmap
->div
[div
][0]))
1583 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1584 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1586 if (div_is_unknown(bmap
, i
))
1593 /* Remove all divs that are unknown or defined in terms of unknown divs.
1595 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1596 __isl_take isl_basic_map
*bmap
)
1603 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1604 if (!div_is_unknown(bmap
, i
))
1606 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1611 isl_basic_map_free(bmap
);
1615 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1624 map
= isl_map_cow(map
);
1628 for (i
= 0; i
< map
->n
; ++i
) {
1629 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1639 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1641 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1644 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1645 __isl_take isl_basic_set
*bset
,
1646 enum isl_dim_type type
, unsigned first
, unsigned n
)
1648 return (isl_basic_set
*)
1649 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1652 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1653 enum isl_dim_type type
, unsigned first
, unsigned n
)
1660 map
= isl_map_cow(map
);
1663 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1665 for (i
= 0; i
< map
->n
; ++i
) {
1666 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1667 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1671 map
= isl_map_drop(map
, type
, first
, n
);
1678 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1679 enum isl_dim_type type
, unsigned first
, unsigned n
)
1681 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1684 /* Project out n inputs starting at first using Fourier-Motzkin */
1685 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1686 unsigned first
, unsigned n
)
1688 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1691 static void dump_term(struct isl_basic_map
*bmap
,
1692 isl_int c
, int pos
, FILE *out
)
1695 unsigned in
= isl_basic_map_n_in(bmap
);
1696 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1697 unsigned nparam
= isl_basic_map_n_param(bmap
);
1699 isl_int_print(out
, c
, 0);
1701 if (!isl_int_is_one(c
))
1702 isl_int_print(out
, c
, 0);
1703 if (pos
< 1 + nparam
) {
1704 name
= isl_dim_get_name(bmap
->dim
,
1705 isl_dim_param
, pos
- 1);
1707 fprintf(out
, "%s", name
);
1709 fprintf(out
, "p%d", pos
- 1);
1710 } else if (pos
< 1 + nparam
+ in
)
1711 fprintf(out
, "i%d", pos
- 1 - nparam
);
1712 else if (pos
< 1 + nparam
+ dim
)
1713 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1715 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1719 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1720 int sign
, FILE *out
)
1724 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1728 for (i
= 0, first
= 1; i
< len
; ++i
) {
1729 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1732 fprintf(out
, " + ");
1734 isl_int_abs(v
, c
[i
]);
1735 dump_term(bmap
, v
, i
, out
);
1742 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1743 const char *op
, FILE *out
, int indent
)
1747 fprintf(out
, "%*s", indent
, "");
1749 dump_constraint_sign(bmap
, c
, 1, out
);
1750 fprintf(out
, " %s ", op
);
1751 dump_constraint_sign(bmap
, c
, -1, out
);
1755 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1756 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1758 fprintf(out
, "%*s", indent
, "");
1759 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1764 static void dump_constraints(struct isl_basic_map
*bmap
,
1765 isl_int
**c
, unsigned n
,
1766 const char *op
, FILE *out
, int indent
)
1770 for (i
= 0; i
< n
; ++i
)
1771 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1774 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1778 unsigned total
= isl_basic_map_total_dim(bmap
);
1780 for (j
= 0; j
< 1 + total
; ++j
) {
1781 if (isl_int_is_zero(exp
[j
]))
1783 if (!first
&& isl_int_is_pos(exp
[j
]))
1785 dump_term(bmap
, exp
[j
], j
, out
);
1790 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1794 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1795 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1797 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1798 fprintf(out
, "%*s", indent
, "");
1799 fprintf(out
, "e%d = [(", i
);
1800 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1802 isl_int_print(out
, bmap
->div
[i
][0], 0);
1803 fprintf(out
, "]\n");
1807 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1808 FILE *out
, int indent
)
1811 fprintf(out
, "null basic set\n");
1815 fprintf(out
, "%*s", indent
, "");
1816 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1817 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1818 bset
->extra
, bset
->flags
);
1819 dump((struct isl_basic_map
*)bset
, out
, indent
);
1822 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1823 FILE *out
, int indent
)
1826 fprintf(out
, "null basic map\n");
1830 fprintf(out
, "%*s", indent
, "");
1831 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1832 "flags: %x, n_name: %d\n",
1834 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1835 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1836 dump(bmap
, out
, indent
);
1839 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1844 total
= isl_basic_map_total_dim(bmap
);
1845 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1846 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1847 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1848 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1852 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1854 struct isl_set
*set
;
1858 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1859 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1860 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1861 sizeof(struct isl_set
) +
1862 (n
- 1) * sizeof(struct isl_basic_set
*));
1866 set
->ctx
= dim
->ctx
;
1867 isl_ctx_ref(set
->ctx
);
1879 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1880 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1882 struct isl_set
*set
;
1883 struct isl_dim
*dims
;
1885 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1889 set
= isl_set_alloc_dim(dims
, n
, flags
);
1893 /* Make sure "map" has room for at least "n" more basic maps.
1895 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1898 struct isl_map
*grown
= NULL
;
1902 isl_assert(map
->ctx
, n
>= 0, goto error
);
1903 if (map
->n
+ n
<= map
->size
)
1905 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1908 for (i
= 0; i
< map
->n
; ++i
) {
1909 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1917 isl_map_free(grown
);
1922 /* Make sure "set" has room for at least "n" more basic sets.
1924 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1926 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1929 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1932 struct isl_set
*dup
;
1937 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1940 for (i
= 0; i
< set
->n
; ++i
)
1941 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1945 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1947 return isl_map_from_basic_map(bset
);
1950 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1952 struct isl_map
*map
;
1957 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1958 return isl_map_add_basic_map(map
, bmap
);
1961 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1962 __isl_take isl_basic_set
*bset
)
1964 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1965 (struct isl_basic_map
*)bset
);
1968 void isl_set_free(struct isl_set
*set
)
1978 isl_ctx_deref(set
->ctx
);
1979 for (i
= 0; i
< set
->n
; ++i
)
1980 isl_basic_set_free(set
->p
[i
]);
1981 isl_dim_free(set
->dim
);
1985 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
1990 fprintf(out
, "null set\n");
1994 fprintf(out
, "%*s", indent
, "");
1995 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1996 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1998 for (i
= 0; i
< set
->n
; ++i
) {
1999 fprintf(out
, "%*s", indent
, "");
2000 fprintf(out
, "basic set %d:\n", i
);
2001 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2005 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2010 fprintf(out
, "null map\n");
2014 fprintf(out
, "%*s", indent
, "");
2015 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2016 "flags: %x, n_name: %d\n",
2017 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2018 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2019 for (i
= 0; i
< map
->n
; ++i
) {
2020 fprintf(out
, "%*s", indent
, "");
2021 fprintf(out
, "basic map %d:\n", i
);
2022 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2026 struct isl_basic_map
*isl_basic_map_intersect_domain(
2027 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2029 struct isl_basic_map
*bmap_domain
;
2034 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2035 bset
->dim
, isl_dim_param
), goto error
);
2037 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2038 isl_assert(bset
->ctx
,
2039 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2041 bmap
= isl_basic_map_cow(bmap
);
2044 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2045 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2046 bmap_domain
= isl_basic_map_from_domain(bset
);
2047 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2049 bmap
= isl_basic_map_simplify(bmap
);
2050 return isl_basic_map_finalize(bmap
);
2052 isl_basic_map_free(bmap
);
2053 isl_basic_set_free(bset
);
2057 struct isl_basic_map
*isl_basic_map_intersect_range(
2058 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2060 struct isl_basic_map
*bmap_range
;
2065 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2066 bset
->dim
, isl_dim_param
), goto error
);
2068 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2069 isl_assert(bset
->ctx
,
2070 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2072 bmap
= isl_basic_map_cow(bmap
);
2075 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2076 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2077 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2078 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2080 bmap
= isl_basic_map_simplify(bmap
);
2081 return isl_basic_map_finalize(bmap
);
2083 isl_basic_map_free(bmap
);
2084 isl_basic_set_free(bset
);
2088 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2094 total
= 1 + isl_basic_map_total_dim(bmap
);
2095 if (total
!= vec
->size
)
2100 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2101 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2102 if (!isl_int_is_zero(s
)) {
2108 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2109 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2110 if (isl_int_is_neg(s
)) {
2121 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2123 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2126 struct isl_basic_map
*isl_basic_map_intersect(
2127 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2129 struct isl_vec
*sample
= NULL
;
2131 if (!bmap1
|| !bmap2
)
2134 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2135 bmap2
->dim
, isl_dim_param
), goto error
);
2136 if (isl_dim_total(bmap1
->dim
) ==
2137 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2138 isl_dim_total(bmap2
->dim
) !=
2139 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2140 return isl_basic_map_intersect(bmap2
, bmap1
);
2142 if (isl_dim_total(bmap2
->dim
) !=
2143 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2144 isl_assert(bmap1
->ctx
,
2145 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2147 if (bmap1
->sample
&&
2148 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2149 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2150 sample
= isl_vec_copy(bmap1
->sample
);
2151 else if (bmap2
->sample
&&
2152 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2153 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2154 sample
= isl_vec_copy(bmap2
->sample
);
2156 bmap1
= isl_basic_map_cow(bmap1
);
2159 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2160 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2161 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2164 isl_vec_free(sample
);
2166 isl_vec_free(bmap1
->sample
);
2167 bmap1
->sample
= sample
;
2170 bmap1
= isl_basic_map_simplify(bmap1
);
2171 return isl_basic_map_finalize(bmap1
);
2174 isl_vec_free(sample
);
2175 isl_basic_map_free(bmap1
);
2176 isl_basic_map_free(bmap2
);
2180 struct isl_basic_set
*isl_basic_set_intersect(
2181 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2183 return (struct isl_basic_set
*)
2184 isl_basic_map_intersect(
2185 (struct isl_basic_map
*)bset1
,
2186 (struct isl_basic_map
*)bset2
);
2189 /* Special case of isl_map_intersect, where both map1 and map2
2190 * are convex, without any divs and such that either map1 or map2
2191 * contains a single constraint. This constraint is then simply
2192 * added to the other map.
2194 static __isl_give isl_map
*map_intersect_add_constraint(
2195 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2197 struct isl_basic_map
*bmap1
;
2198 struct isl_basic_map
*bmap2
;
2200 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2201 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2202 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2203 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2205 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2206 return isl_map_intersect(map2
, map1
);
2208 isl_assert(map2
->ctx
,
2209 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2211 map1
= isl_map_cow(map1
);
2214 if (isl_map_fast_is_empty(map1
)) {
2218 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2219 if (map2
->p
[0]->n_eq
== 1)
2220 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2222 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2223 map2
->p
[0]->ineq
[0]);
2225 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2226 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2230 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2231 isl_basic_map_free(map1
->p
[0]);
2244 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2247 struct isl_map
*result
;
2253 if (isl_map_fast_is_empty(map1
)) {
2257 if (isl_map_fast_is_empty(map2
)) {
2262 if (map1
->n
== 1 && map2
->n
== 1 &&
2263 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2264 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2265 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2266 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2267 return map_intersect_add_constraint(map1
, map2
);
2268 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2269 map2
->dim
, isl_dim_param
), goto error
);
2270 if (isl_dim_total(map1
->dim
) ==
2271 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2272 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2273 return isl_map_intersect(map2
, map1
);
2275 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2276 isl_assert(map1
->ctx
,
2277 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2279 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2280 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2281 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2283 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2284 map1
->n
* map2
->n
, flags
);
2287 for (i
= 0; i
< map1
->n
; ++i
)
2288 for (j
= 0; j
< map2
->n
; ++j
) {
2289 struct isl_basic_map
*part
;
2290 part
= isl_basic_map_intersect(
2291 isl_basic_map_copy(map1
->p
[i
]),
2292 isl_basic_map_copy(map2
->p
[j
]));
2293 if (isl_basic_map_is_empty(part
))
2294 isl_basic_map_free(part
);
2296 result
= isl_map_add_basic_map(result
, part
);
2309 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2311 return (struct isl_set
*)
2312 isl_map_intersect((struct isl_map
*)set1
,
2313 (struct isl_map
*)set2
);
2316 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2318 struct isl_dim
*dim
;
2319 struct isl_basic_set
*bset
;
2324 bmap
= isl_basic_map_cow(bmap
);
2327 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2328 in
= isl_basic_map_n_in(bmap
);
2329 bset
= isl_basic_set_from_basic_map(bmap
);
2330 bset
= isl_basic_set_swap_vars(bset
, in
);
2331 return isl_basic_map_from_basic_set(bset
, dim
);
2334 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2335 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2337 struct isl_dim
*res_dim
;
2338 struct isl_basic_map
*res
;
2339 struct isl_dim_map
*dim_map
;
2340 unsigned total
, off
;
2341 enum isl_dim_type t
;
2349 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2351 total
= isl_basic_map_total_dim(bmap
) + n
;
2352 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2354 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2356 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2358 unsigned size
= isl_basic_map_dim(bmap
, t
);
2359 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2361 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2362 pos
, size
- pos
, off
+ pos
+ n
);
2364 off
+= isl_dim_size(res_dim
, t
);
2366 isl_dim_map_div(dim_map
, bmap
, off
);
2368 res
= isl_basic_map_alloc_dim(res_dim
,
2369 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2370 if (isl_basic_map_is_rational(bmap
))
2371 res
= isl_basic_map_set_rational(res
);
2372 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2373 return isl_basic_map_finalize(res
);
2376 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2377 enum isl_dim_type type
, unsigned n
)
2381 return isl_basic_map_insert(bmap
, type
,
2382 isl_basic_map_dim(bmap
, type
), n
);
2385 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2386 enum isl_dim_type type
, unsigned n
)
2390 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2391 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2393 isl_basic_set_free(bset
);
2397 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2398 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2405 map
= isl_map_cow(map
);
2409 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2413 for (i
= 0; i
< map
->n
; ++i
) {
2414 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2425 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2426 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2428 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2431 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2432 enum isl_dim_type type
, unsigned n
)
2436 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2439 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2440 enum isl_dim_type type
, unsigned n
)
2444 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2445 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2451 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2452 __isl_take isl_basic_map
*bmap
,
2453 enum isl_dim_type dst_type
, unsigned dst_pos
,
2454 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2457 struct isl_dim_map
*dim_map
;
2458 struct isl_basic_map
*res
;
2459 enum isl_dim_type t
;
2460 unsigned total
, off
;
2467 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2470 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2473 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2475 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2476 pos(bmap
->dim
, src_type
) + src_pos
+
2477 ((src_type
< dst_type
) ? n
: 0)) {
2478 bmap
= isl_basic_map_cow(bmap
);
2482 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2483 src_type
, src_pos
, n
);
2487 bmap
= isl_basic_map_finalize(bmap
);
2492 total
= isl_basic_map_total_dim(bmap
);
2493 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2496 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2497 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2498 if (t
== dst_type
) {
2499 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2502 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2505 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2506 dst_pos
, size
- dst_pos
, off
);
2507 off
+= size
- dst_pos
;
2508 } else if (t
== src_type
) {
2509 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2512 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2513 src_pos
+ n
, size
- src_pos
- n
, off
);
2514 off
+= size
- src_pos
- n
;
2516 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2520 isl_dim_map_div(dim_map
, bmap
, off
);
2522 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2523 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2524 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2526 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2527 src_type
, src_pos
, n
);
2531 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2532 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2533 bmap
= isl_basic_map_finalize(bmap
);
2537 isl_basic_map_free(bmap
);
2541 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2542 enum isl_dim_type dst_type
, unsigned dst_pos
,
2543 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2545 return (isl_basic_set
*)isl_basic_map_move_dims(
2546 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2549 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2550 enum isl_dim_type dst_type
, unsigned dst_pos
,
2551 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2555 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2556 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2557 src_type
, src_pos
, n
);
2563 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2564 enum isl_dim_type dst_type
, unsigned dst_pos
,
2565 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2574 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2577 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2580 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2582 map
= isl_map_cow(map
);
2586 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2590 for (i
= 0; i
< map
->n
; ++i
) {
2591 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2593 src_type
, src_pos
, n
);
2604 /* Move the specified dimensions to the last columns right before
2605 * the divs. Don't change the dimension specification of bmap.
2606 * That's the responsibility of the caller.
2608 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2609 enum isl_dim_type type
, unsigned first
, unsigned n
)
2611 struct isl_dim_map
*dim_map
;
2612 struct isl_basic_map
*res
;
2613 enum isl_dim_type t
;
2614 unsigned total
, off
;
2618 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2621 total
= isl_basic_map_total_dim(bmap
);
2622 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2625 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2626 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2628 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2631 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2632 first
, n
, total
- bmap
->n_div
- n
);
2633 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2634 first
+ n
, size
- (first
+ n
), off
);
2635 off
+= size
- (first
+ n
);
2637 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2641 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2643 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2644 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2645 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2649 /* Turn the n dimensions of type type, starting at first
2650 * into existentially quantified variables.
2652 __isl_give isl_basic_map
*isl_basic_map_project_out(
2653 __isl_take isl_basic_map
*bmap
,
2654 enum isl_dim_type type
, unsigned first
, unsigned n
)
2667 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2668 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2670 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2673 bmap
= move_last(bmap
, type
, first
, n
);
2674 bmap
= isl_basic_map_cow(bmap
);
2678 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2679 old
= bmap
->block2
.data
;
2680 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2681 (bmap
->extra
+ n
) * (1 + row_size
));
2682 if (!bmap
->block2
.data
)
2684 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2687 for (i
= 0; i
< n
; ++i
) {
2688 new_div
[i
] = bmap
->block2
.data
+
2689 (bmap
->extra
+ i
) * (1 + row_size
);
2690 isl_seq_clr(new_div
[i
], 1 + row_size
);
2692 for (i
= 0; i
< bmap
->extra
; ++i
)
2693 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2695 bmap
->div
= new_div
;
2699 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2702 bmap
= isl_basic_map_simplify(bmap
);
2703 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2704 return isl_basic_map_finalize(bmap
);
2706 isl_basic_map_free(bmap
);
2710 /* Turn the n dimensions of type type, starting at first
2711 * into existentially quantified variables.
2713 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2714 enum isl_dim_type type
, unsigned first
, unsigned n
)
2716 return (isl_basic_set
*)isl_basic_map_project_out(
2717 (isl_basic_map
*)bset
, type
, first
, n
);
2720 /* Turn the n dimensions of type type, starting at first
2721 * into existentially quantified variables.
2723 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2724 enum isl_dim_type type
, unsigned first
, unsigned n
)
2734 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2736 map
= isl_map_cow(map
);
2740 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2744 for (i
= 0; i
< map
->n
; ++i
) {
2745 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2756 /* Turn the n dimensions of type type, starting at first
2757 * into existentially quantified variables.
2759 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2760 enum isl_dim_type type
, unsigned first
, unsigned n
)
2762 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2765 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2769 for (i
= 0; i
< n
; ++i
) {
2770 j
= isl_basic_map_alloc_div(bmap
);
2773 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2777 isl_basic_map_free(bmap
);
2781 struct isl_basic_map
*isl_basic_map_apply_range(
2782 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2784 struct isl_dim
*dim_result
= NULL
;
2785 struct isl_basic_map
*bmap
;
2786 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2787 struct isl_dim_map
*dim_map1
, *dim_map2
;
2789 if (!bmap1
|| !bmap2
)
2792 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2793 isl_dim_copy(bmap2
->dim
));
2795 n_in
= isl_basic_map_n_in(bmap1
);
2796 n_out
= isl_basic_map_n_out(bmap2
);
2797 n
= isl_basic_map_n_out(bmap1
);
2798 nparam
= isl_basic_map_n_param(bmap1
);
2800 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2801 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2802 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2803 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2804 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2805 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2806 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2807 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2808 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2809 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2810 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2812 bmap
= isl_basic_map_alloc_dim(dim_result
,
2813 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2814 bmap1
->n_eq
+ bmap2
->n_eq
,
2815 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2816 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2817 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2818 bmap
= add_divs(bmap
, n
);
2819 bmap
= isl_basic_map_simplify(bmap
);
2820 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2821 return isl_basic_map_finalize(bmap
);
2823 isl_basic_map_free(bmap1
);
2824 isl_basic_map_free(bmap2
);
2828 struct isl_basic_set
*isl_basic_set_apply(
2829 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2834 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2837 return (struct isl_basic_set
*)
2838 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2840 isl_basic_set_free(bset
);
2841 isl_basic_map_free(bmap
);
2845 struct isl_basic_map
*isl_basic_map_apply_domain(
2846 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2848 if (!bmap1
|| !bmap2
)
2851 isl_assert(bmap1
->ctx
,
2852 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2853 isl_assert(bmap1
->ctx
,
2854 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2857 bmap1
= isl_basic_map_reverse(bmap1
);
2858 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2859 return isl_basic_map_reverse(bmap1
);
2861 isl_basic_map_free(bmap1
);
2862 isl_basic_map_free(bmap2
);
2866 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2867 * A \cap B -> f(A) + f(B)
2869 struct isl_basic_map
*isl_basic_map_sum(
2870 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2872 unsigned n_in
, n_out
, nparam
, total
, pos
;
2873 struct isl_basic_map
*bmap
= NULL
;
2874 struct isl_dim_map
*dim_map1
, *dim_map2
;
2877 if (!bmap1
|| !bmap2
)
2880 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2883 nparam
= isl_basic_map_n_param(bmap1
);
2884 n_in
= isl_basic_map_n_in(bmap1
);
2885 n_out
= isl_basic_map_n_out(bmap1
);
2887 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2888 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2889 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2890 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2891 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2892 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2893 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2894 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2895 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2896 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2897 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2899 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2900 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2901 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2902 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2903 for (i
= 0; i
< n_out
; ++i
) {
2904 int j
= isl_basic_map_alloc_equality(bmap
);
2907 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2908 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2909 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2910 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2912 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2913 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2914 bmap
= add_divs(bmap
, 2 * n_out
);
2916 bmap
= isl_basic_map_simplify(bmap
);
2917 return isl_basic_map_finalize(bmap
);
2919 isl_basic_map_free(bmap
);
2920 isl_basic_map_free(bmap1
);
2921 isl_basic_map_free(bmap2
);
2925 /* Given two maps A -> f(A) and B -> g(B), construct a map
2926 * A \cap B -> f(A) + f(B)
2928 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2930 struct isl_map
*result
;
2936 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2938 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2939 map1
->n
* map2
->n
, 0);
2942 for (i
= 0; i
< map1
->n
; ++i
)
2943 for (j
= 0; j
< map2
->n
; ++j
) {
2944 struct isl_basic_map
*part
;
2945 part
= isl_basic_map_sum(
2946 isl_basic_map_copy(map1
->p
[i
]),
2947 isl_basic_map_copy(map2
->p
[j
]));
2948 if (isl_basic_map_is_empty(part
))
2949 isl_basic_map_free(part
);
2951 result
= isl_map_add_basic_map(result
, part
);
2964 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2965 __isl_take isl_set
*set2
)
2967 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2970 /* Given a basic map A -> f(A), construct A -> -f(A).
2972 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2977 bmap
= isl_basic_map_cow(bmap
);
2981 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2982 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2983 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2984 for (j
= 0; j
< n
; ++j
)
2985 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2986 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2987 for (j
= 0; j
< n
; ++j
)
2988 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2989 for (i
= 0; i
< bmap
->n_div
; ++i
)
2990 for (j
= 0; j
< n
; ++j
)
2991 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2992 return isl_basic_map_finalize(bmap
);
2995 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
2997 return isl_basic_map_neg(bset
);
3000 /* Given a map A -> f(A), construct A -> -f(A).
3002 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3006 map
= isl_map_cow(map
);
3010 for (i
= 0; i
< map
->n
; ++i
) {
3011 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3022 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3024 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3027 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3028 * A -> floor(f(A)/d).
3030 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3033 unsigned n_in
, n_out
, nparam
, total
, pos
;
3034 struct isl_basic_map
*result
= NULL
;
3035 struct isl_dim_map
*dim_map
;
3041 nparam
= isl_basic_map_n_param(bmap
);
3042 n_in
= isl_basic_map_n_in(bmap
);
3043 n_out
= isl_basic_map_n_out(bmap
);
3045 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3046 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3047 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3048 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3049 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3050 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3052 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3053 bmap
->n_div
+ n_out
,
3054 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3055 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
3056 result
= add_divs(result
, n_out
);
3057 for (i
= 0; i
< n_out
; ++i
) {
3059 j
= isl_basic_map_alloc_inequality(result
);
3062 isl_seq_clr(result
->ineq
[j
], 1+total
);
3063 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3064 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3065 j
= isl_basic_map_alloc_inequality(result
);
3068 isl_seq_clr(result
->ineq
[j
], 1+total
);
3069 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3070 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3071 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3074 result
= isl_basic_map_simplify(result
);
3075 return isl_basic_map_finalize(result
);
3077 isl_basic_map_free(result
);
3081 /* Given a map A -> f(A) and an integer d, construct a map
3082 * A -> floor(f(A)/d).
3084 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3088 map
= isl_map_cow(map
);
3092 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3093 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3094 for (i
= 0; i
< map
->n
; ++i
) {
3095 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3106 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3112 i
= isl_basic_map_alloc_equality(bmap
);
3115 nparam
= isl_basic_map_n_param(bmap
);
3116 n_in
= isl_basic_map_n_in(bmap
);
3117 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3118 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3119 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3120 return isl_basic_map_finalize(bmap
);
3122 isl_basic_map_free(bmap
);
3126 /* Add a constraints to "bmap" expressing i_pos < o_pos
3128 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3134 i
= isl_basic_map_alloc_inequality(bmap
);
3137 nparam
= isl_basic_map_n_param(bmap
);
3138 n_in
= isl_basic_map_n_in(bmap
);
3139 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3140 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3141 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3142 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3143 return isl_basic_map_finalize(bmap
);
3145 isl_basic_map_free(bmap
);
3149 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3151 static __isl_give isl_basic_map
*var_less_or_equal(
3152 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3158 i
= isl_basic_map_alloc_inequality(bmap
);
3161 nparam
= isl_basic_map_n_param(bmap
);
3162 n_in
= isl_basic_map_n_in(bmap
);
3163 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3164 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3165 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3166 return isl_basic_map_finalize(bmap
);
3168 isl_basic_map_free(bmap
);
3172 /* Add a constraints to "bmap" expressing i_pos > o_pos
3174 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3180 i
= isl_basic_map_alloc_inequality(bmap
);
3183 nparam
= isl_basic_map_n_param(bmap
);
3184 n_in
= isl_basic_map_n_in(bmap
);
3185 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3186 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3187 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3188 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3189 return isl_basic_map_finalize(bmap
);
3191 isl_basic_map_free(bmap
);
3195 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3197 static __isl_give isl_basic_map
*var_more_or_equal(
3198 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3204 i
= isl_basic_map_alloc_inequality(bmap
);
3207 nparam
= isl_basic_map_n_param(bmap
);
3208 n_in
= isl_basic_map_n_in(bmap
);
3209 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3210 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3211 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3212 return isl_basic_map_finalize(bmap
);
3214 isl_basic_map_free(bmap
);
3218 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3221 struct isl_basic_map
*bmap
;
3222 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3225 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3226 bmap
= var_equal(bmap
, i
);
3227 return isl_basic_map_finalize(bmap
);
3230 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3232 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3235 struct isl_basic_map
*bmap
;
3236 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3239 for (i
= 0; i
< pos
&& bmap
; ++i
)
3240 bmap
= var_equal(bmap
, i
);
3242 bmap
= var_less(bmap
, pos
);
3243 return isl_basic_map_finalize(bmap
);
3246 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3248 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3249 __isl_take isl_dim
*dim
, unsigned pos
)
3252 isl_basic_map
*bmap
;
3254 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3255 for (i
= 0; i
< pos
; ++i
)
3256 bmap
= var_equal(bmap
, i
);
3257 bmap
= var_less_or_equal(bmap
, pos
);
3258 return isl_basic_map_finalize(bmap
);
3261 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3263 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3266 struct isl_basic_map
*bmap
;
3267 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3270 for (i
= 0; i
< pos
&& bmap
; ++i
)
3271 bmap
= var_equal(bmap
, i
);
3273 bmap
= var_more(bmap
, pos
);
3274 return isl_basic_map_finalize(bmap
);
3277 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3279 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3280 __isl_take isl_dim
*dim
, unsigned pos
)
3283 isl_basic_map
*bmap
;
3285 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3286 for (i
= 0; i
< pos
; ++i
)
3287 bmap
= var_equal(bmap
, i
);
3288 bmap
= var_more_or_equal(bmap
, pos
);
3289 return isl_basic_map_finalize(bmap
);
3292 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3293 unsigned n
, int equal
)
3295 struct isl_map
*map
;
3298 if (n
== 0 && equal
)
3299 return isl_map_universe(dims
);
3301 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3303 for (i
= 0; i
+ 1 < n
; ++i
)
3304 map
= isl_map_add_basic_map(map
,
3305 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3308 map
= isl_map_add_basic_map(map
,
3309 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3311 map
= isl_map_add_basic_map(map
,
3312 isl_basic_map_less_at(dims
, n
- 1));
3319 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3323 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3326 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3328 return map_lex_lte_first(dim
, n
, 0);
3331 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3333 return map_lex_lte_first(dim
, n
, 1);
3336 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3338 return map_lex_lte(isl_dim_map(set_dim
), 0);
3341 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3343 return map_lex_lte(isl_dim_map(set_dim
), 1);
3346 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3347 unsigned n
, int equal
)
3349 struct isl_map
*map
;
3352 if (n
== 0 && equal
)
3353 return isl_map_universe(dims
);
3355 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3357 for (i
= 0; i
+ 1 < n
; ++i
)
3358 map
= isl_map_add_basic_map(map
,
3359 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3362 map
= isl_map_add_basic_map(map
,
3363 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3365 map
= isl_map_add_basic_map(map
,
3366 isl_basic_map_more_at(dims
, n
- 1));
3373 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3377 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3380 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3382 return map_lex_gte_first(dim
, n
, 0);
3385 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3387 return map_lex_gte_first(dim
, n
, 1);
3390 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3392 return map_lex_gte(isl_dim_map(set_dim
), 0);
3395 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3397 return map_lex_gte(isl_dim_map(set_dim
), 1);
3400 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3401 __isl_take isl_set
*set2
)
3404 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3405 map
= isl_map_intersect_domain(map
, set1
);
3406 map
= isl_map_intersect_range(map
, set2
);
3410 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3411 __isl_take isl_set
*set2
)
3414 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3415 map
= isl_map_intersect_domain(map
, set1
);
3416 map
= isl_map_intersect_range(map
, set2
);
3420 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3421 __isl_take isl_set
*set2
)
3424 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3425 map
= isl_map_intersect_domain(map
, set1
);
3426 map
= isl_map_intersect_range(map
, set2
);
3430 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3431 __isl_take isl_set
*set2
)
3434 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3435 map
= isl_map_intersect_domain(map
, set1
);
3436 map
= isl_map_intersect_range(map
, set2
);
3440 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3441 __isl_take isl_map
*map2
)
3444 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3445 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3446 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3450 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3451 __isl_take isl_map
*map2
)
3454 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3455 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3456 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3460 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3461 __isl_take isl_map
*map2
)
3464 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3465 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3466 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3470 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3471 __isl_take isl_map
*map2
)
3474 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3475 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3476 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3480 struct isl_basic_map
*isl_basic_map_from_basic_set(
3481 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3483 struct isl_basic_map
*bmap
;
3485 bset
= isl_basic_set_cow(bset
);
3489 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3490 isl_dim_free(bset
->dim
);
3491 bmap
= (struct isl_basic_map
*) bset
;
3493 return isl_basic_map_finalize(bmap
);
3495 isl_basic_set_free(bset
);
3500 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3504 if (bmap
->dim
->n_in
== 0)
3505 return (struct isl_basic_set
*)bmap
;
3506 bmap
= isl_basic_map_cow(bmap
);
3509 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3512 bmap
= isl_basic_map_finalize(bmap
);
3513 return (struct isl_basic_set
*)bmap
;
3515 isl_basic_map_free(bmap
);
3519 /* For a div d = floor(f/m), add the constraints
3522 * -(f-(n-1)) + m d >= 0
3524 * Note that the second constraint is the negation of
3528 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3529 unsigned pos
, isl_int
*div
)
3532 unsigned total
= isl_basic_map_total_dim(bmap
);
3534 i
= isl_basic_map_alloc_inequality(bmap
);
3537 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3538 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3540 j
= isl_basic_map_alloc_inequality(bmap
);
3543 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3544 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3545 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3549 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3550 unsigned pos
, isl_int
*div
)
3552 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3556 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3558 unsigned total
= isl_basic_map_total_dim(bmap
);
3559 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3561 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3565 struct isl_basic_set
*isl_basic_map_underlying_set(
3566 struct isl_basic_map
*bmap
)
3570 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3572 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3573 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3574 return (struct isl_basic_set
*)bmap
;
3575 bmap
= isl_basic_map_cow(bmap
);
3578 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3581 bmap
->extra
-= bmap
->n_div
;
3583 bmap
= isl_basic_map_finalize(bmap
);
3584 return (struct isl_basic_set
*)bmap
;
3589 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3590 __isl_take isl_basic_set
*bset
)
3592 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3595 struct isl_basic_map
*isl_basic_map_overlying_set(
3596 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3598 struct isl_basic_map
*bmap
;
3599 struct isl_ctx
*ctx
;
3606 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3607 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3608 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3610 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3611 isl_basic_map_free(like
);
3612 return (struct isl_basic_map
*)bset
;
3614 bset
= isl_basic_set_cow(bset
);
3617 total
= bset
->dim
->n_out
+ bset
->extra
;
3618 bmap
= (struct isl_basic_map
*)bset
;
3619 isl_dim_free(bmap
->dim
);
3620 bmap
->dim
= isl_dim_copy(like
->dim
);
3623 bmap
->n_div
= like
->n_div
;
3624 bmap
->extra
+= like
->n_div
;
3627 ltotal
= total
- bmap
->extra
+ like
->extra
;
3630 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3631 bmap
->extra
* (1 + 1 + total
));
3632 if (isl_blk_is_error(bmap
->block2
))
3634 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3638 for (i
= 0; i
< bmap
->extra
; ++i
)
3639 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3640 for (i
= 0; i
< like
->n_div
; ++i
) {
3641 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3642 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3644 bmap
= isl_basic_map_extend_constraints(bmap
,
3645 0, 2 * like
->n_div
);
3646 for (i
= 0; i
< like
->n_div
; ++i
) {
3647 if (isl_int_is_zero(bmap
->div
[i
][0]))
3649 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3653 isl_basic_map_free(like
);
3654 bmap
= isl_basic_map_simplify(bmap
);
3655 bmap
= isl_basic_map_finalize(bmap
);
3658 isl_basic_map_free(like
);
3659 isl_basic_set_free(bset
);
3663 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3664 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3666 return (struct isl_basic_set
*)
3667 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3670 struct isl_set
*isl_set_from_underlying_set(
3671 struct isl_set
*set
, struct isl_basic_set
*like
)
3677 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3679 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3680 isl_basic_set_free(like
);
3683 set
= isl_set_cow(set
);
3686 for (i
= 0; i
< set
->n
; ++i
) {
3687 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3688 isl_basic_set_copy(like
));
3692 isl_dim_free(set
->dim
);
3693 set
->dim
= isl_dim_copy(like
->dim
);
3696 isl_basic_set_free(like
);
3699 isl_basic_set_free(like
);
3704 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3708 map
= isl_map_cow(map
);
3711 map
->dim
= isl_dim_cow(map
->dim
);
3715 for (i
= 1; i
< map
->n
; ++i
)
3716 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3718 for (i
= 0; i
< map
->n
; ++i
) {
3719 map
->p
[i
] = (struct isl_basic_map
*)
3720 isl_basic_map_underlying_set(map
->p
[i
]);
3725 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3727 isl_dim_free(map
->dim
);
3728 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3732 return (struct isl_set
*)map
;
3738 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3740 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3743 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3744 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3746 bmap
= isl_basic_map_cow(bmap
);
3750 isl_dim_free(bmap
->dim
);
3753 bmap
= isl_basic_map_finalize(bmap
);
3757 isl_basic_map_free(bmap
);
3762 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3763 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3765 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3769 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3770 __isl_take isl_dim
*dim
)
3774 map
= isl_map_cow(map
);
3778 for (i
= 0; i
< map
->n
; ++i
) {
3779 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3784 isl_dim_free(map
->dim
);
3794 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3795 __isl_take isl_dim
*dim
)
3797 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3800 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3803 struct isl_basic_set
*domain
;
3809 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3811 n_in
= isl_basic_map_n_in(bmap
);
3812 n_out
= isl_basic_map_n_out(bmap
);
3813 domain
= isl_basic_set_from_basic_map(bmap
);
3814 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3816 domain
= isl_basic_set_reset_dim(domain
, dim
);
3821 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3823 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3826 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3827 __isl_take isl_basic_map
*bmap
)
3831 isl_basic_map
*domain
;
3832 isl_basic_set
*bset
;
3833 int nparam
, n_in
, n_out
;
3836 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3837 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3838 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3840 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3841 domain
= isl_basic_map_universe(dim
);
3843 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3844 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3845 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3847 total
= isl_basic_map_total_dim(bmap
);
3849 for (i
= 0; i
< n_in
; ++i
) {
3850 k
= isl_basic_map_alloc_equality(bmap
);
3853 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3854 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3855 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3858 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3859 return isl_basic_map_finalize(bmap
);
3861 isl_basic_map_free(bmap
);
3865 __isl_give isl_basic_map
*isl_basic_map_range_map(
3866 __isl_take isl_basic_map
*bmap
)
3870 isl_basic_map
*range
;
3871 isl_basic_set
*bset
;
3872 int nparam
, n_in
, n_out
;
3875 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3876 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3877 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3879 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3880 range
= isl_basic_map_universe(dim
);
3882 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3883 bmap
= isl_basic_map_apply_range(bmap
, range
);
3884 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3886 total
= isl_basic_map_total_dim(bmap
);
3888 for (i
= 0; i
< n_out
; ++i
) {
3889 k
= isl_basic_map_alloc_equality(bmap
);
3892 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3893 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3894 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3897 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3898 return isl_basic_map_finalize(bmap
);
3900 isl_basic_map_free(bmap
);
3904 struct isl_set
*isl_map_range(struct isl_map
*map
)
3907 struct isl_set
*set
;
3911 if (isl_map_dim(map
, isl_dim_in
) == 0)
3912 return (isl_set
*)map
;
3914 map
= isl_map_cow(map
);
3918 set
= (struct isl_set
*) map
;
3919 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3922 for (i
= 0; i
< map
->n
; ++i
) {
3923 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3927 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3928 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3935 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3938 isl_dim
*domain_dim
;
3940 map
= isl_map_cow(map
);
3944 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3945 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3946 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3949 for (i
= 0; i
< map
->n
; ++i
) {
3950 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3954 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3955 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3962 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3967 map
= isl_map_cow(map
);
3971 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3972 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3973 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3976 for (i
= 0; i
< map
->n
; ++i
) {
3977 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
3981 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3982 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3989 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3992 struct isl_map
*map
= NULL
;
3994 set
= isl_set_cow(set
);
3997 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3998 map
= (struct isl_map
*)set
;
3999 for (i
= 0; i
< set
->n
; ++i
) {
4000 map
->p
[i
] = isl_basic_map_from_basic_set(
4001 set
->p
[i
], isl_dim_copy(dim
));
4005 isl_dim_free(map
->dim
);
4014 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4015 __isl_take isl_basic_set
*bset
)
4017 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4020 __isl_give isl_basic_map
*isl_basic_map_from_range(
4021 __isl_take isl_basic_set
*bset
)
4023 return (isl_basic_map
*)bset
;
4026 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4028 return (struct isl_map
*)set
;
4031 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4033 return isl_map_reverse(isl_map_from_range(set
));
4036 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4037 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4039 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4040 isl_basic_map_from_range(range
));
4043 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4044 __isl_take isl_set
*range
)
4046 return isl_map_apply_range(isl_map_from_domain(domain
),
4047 isl_map_from_range(range
));
4050 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4053 struct isl_set
*set
= NULL
;
4057 map
= isl_map_cow(map
);
4060 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4063 set
= (struct isl_set
*)map
;
4064 for (i
= 0; i
< map
->n
; ++i
) {
4065 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4075 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4077 struct isl_map
*map
;
4081 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4082 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4083 sizeof(struct isl_map
) +
4084 (n
- 1) * sizeof(struct isl_basic_map
*));
4088 map
->ctx
= dim
->ctx
;
4089 isl_ctx_ref(map
->ctx
);
4101 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4102 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4105 struct isl_map
*map
;
4106 struct isl_dim
*dims
;
4108 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4112 map
= isl_map_alloc_dim(dims
, n
, flags
);
4116 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4118 struct isl_basic_map
*bmap
;
4119 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4120 bmap
= isl_basic_map_set_to_empty(bmap
);
4124 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4126 struct isl_basic_set
*bset
;
4127 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4128 bset
= isl_basic_set_set_to_empty(bset
);
4132 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4134 struct isl_basic_map
*bmap
;
4137 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4138 bmap
= isl_basic_map_set_to_empty(bmap
);
4142 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4144 struct isl_basic_map
*bmap
;
4147 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4148 bmap
= isl_basic_map_set_to_empty(bmap
);
4152 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4154 struct isl_basic_set
*bset
;
4157 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4158 bset
= isl_basic_set_set_to_empty(bset
);
4162 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4164 struct isl_basic_map
*bmap
;
4165 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4166 bmap
= isl_basic_map_finalize(bmap
);
4170 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4172 struct isl_basic_set
*bset
;
4173 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4174 bset
= isl_basic_set_finalize(bset
);
4178 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4181 unsigned total
= isl_dim_total(dim
);
4182 isl_basic_map
*bmap
;
4184 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4185 for (i
= 0; i
< total
; ++i
) {
4186 int k
= isl_basic_map_alloc_inequality(bmap
);
4189 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4190 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4194 isl_basic_map_free(bmap
);
4198 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4200 return isl_basic_map_nat_universe(dim
);
4203 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4205 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4208 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4210 return isl_map_nat_universe(dim
);
4213 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4214 __isl_keep isl_basic_map
*model
)
4218 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4221 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4225 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4228 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4229 __isl_keep isl_set
*model
)
4233 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4236 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4238 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4241 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4245 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4248 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4252 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4255 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4257 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4260 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4264 return isl_set_empty(isl_dim_copy(model
->dim
));
4267 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4269 struct isl_map
*map
;
4272 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4273 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4277 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4279 struct isl_set
*set
;
4282 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4283 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4287 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4291 return isl_set_universe(isl_dim_copy(model
->dim
));
4294 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4297 struct isl_map
*dup
;
4301 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4302 for (i
= 0; i
< map
->n
; ++i
)
4303 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4307 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4308 __isl_take isl_basic_map
*bmap
)
4312 if (isl_basic_map_fast_is_empty(bmap
)) {
4313 isl_basic_map_free(bmap
);
4316 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4317 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4318 map
->p
[map
->n
] = bmap
;
4320 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4326 isl_basic_map_free(bmap
);
4330 void isl_map_free(struct isl_map
*map
)
4340 isl_ctx_deref(map
->ctx
);
4341 for (i
= 0; i
< map
->n
; ++i
)
4342 isl_basic_map_free(map
->p
[i
]);
4343 isl_dim_free(map
->dim
);
4347 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4348 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4352 base
= isl_map_cow(base
);
4356 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4359 for (i
= 0; i
< base
->n
; ++i
) {
4360 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4361 isl_dim_copy(base
->dim
), 0, 0, 0);
4371 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4372 unsigned nparam
, unsigned dim
)
4374 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4378 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4379 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4383 bmap
= isl_basic_map_cow(bmap
);
4384 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4385 j
= isl_basic_map_alloc_equality(bmap
);
4388 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4389 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4390 isl_int_set_si(bmap
->eq
[j
][0], value
);
4391 bmap
= isl_basic_map_simplify(bmap
);
4392 return isl_basic_map_finalize(bmap
);
4394 isl_basic_map_free(bmap
);
4398 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4399 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4403 bmap
= isl_basic_map_cow(bmap
);
4404 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4405 j
= isl_basic_map_alloc_equality(bmap
);
4408 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4409 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4410 isl_int_set(bmap
->eq
[j
][0], value
);
4411 bmap
= isl_basic_map_simplify(bmap
);
4412 return isl_basic_map_finalize(bmap
);
4414 isl_basic_map_free(bmap
);
4418 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4419 enum isl_dim_type type
, unsigned pos
, int value
)
4423 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4424 return isl_basic_map_fix_pos_si(bmap
,
4425 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4427 isl_basic_map_free(bmap
);
4431 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4432 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4436 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4437 return isl_basic_map_fix_pos(bmap
,
4438 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4440 isl_basic_map_free(bmap
);
4444 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4445 enum isl_dim_type type
, unsigned pos
, int value
)
4447 return (struct isl_basic_set
*)
4448 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4452 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4453 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4455 return (struct isl_basic_set
*)
4456 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4460 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4461 unsigned input
, int value
)
4463 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4466 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4467 unsigned dim
, int value
)
4469 return (struct isl_basic_set
*)
4470 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4471 isl_dim_set
, dim
, value
);
4474 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4475 enum isl_dim_type type
, unsigned pos
, int value
)
4479 map
= isl_map_cow(map
);
4483 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4484 for (i
= 0; i
< map
->n
; ++i
) {
4485 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4489 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4496 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4497 enum isl_dim_type type
, unsigned pos
, int value
)
4499 return (struct isl_set
*)
4500 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4503 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4504 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4508 map
= isl_map_cow(map
);
4512 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4513 for (i
= 0; i
< map
->n
; ++i
) {
4514 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4518 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4525 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4526 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4528 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4531 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4532 unsigned input
, int value
)
4534 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4537 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4539 return (struct isl_set
*)
4540 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4543 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4544 __isl_take isl_basic_map
*bmap
,
4545 enum isl_dim_type type
, unsigned pos
, int value
)
4551 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4552 pos
+= isl_basic_map_offset(bmap
, type
);
4553 bmap
= isl_basic_map_cow(bmap
);
4554 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4555 j
= isl_basic_map_alloc_inequality(bmap
);
4558 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4559 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4560 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4561 bmap
= isl_basic_map_simplify(bmap
);
4562 return isl_basic_map_finalize(bmap
);
4564 isl_basic_map_free(bmap
);
4568 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4569 unsigned dim
, isl_int value
)
4573 bset
= isl_basic_set_cow(bset
);
4574 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4575 j
= isl_basic_set_alloc_inequality(bset
);
4578 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4579 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4580 isl_int_neg(bset
->ineq
[j
][0], value
);
4581 bset
= isl_basic_set_simplify(bset
);
4582 return isl_basic_set_finalize(bset
);
4584 isl_basic_set_free(bset
);
4588 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4589 enum isl_dim_type type
, unsigned pos
, int value
)
4593 map
= isl_map_cow(map
);
4597 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4598 for (i
= 0; i
< map
->n
; ++i
) {
4599 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4604 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4611 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4612 enum isl_dim_type type
, unsigned pos
, int value
)
4614 return (struct isl_set
*)
4615 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4618 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4623 set
= isl_set_cow(set
);
4627 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4628 for (i
= 0; i
< set
->n
; ++i
) {
4629 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4639 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4643 map
= isl_map_cow(map
);
4647 map
->dim
= isl_dim_reverse(map
->dim
);
4650 for (i
= 0; i
< map
->n
; ++i
) {
4651 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4655 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4662 static struct isl_map
*isl_basic_map_partial_lexopt(
4663 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4664 struct isl_set
**empty
, int max
)
4668 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4669 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4671 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4673 isl_basic_map_free(bmap
);
4674 isl_basic_set_free(dom
);
4680 struct isl_map
*isl_basic_map_partial_lexmax(
4681 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4682 struct isl_set
**empty
)
4684 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4687 struct isl_map
*isl_basic_map_partial_lexmin(
4688 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4689 struct isl_set
**empty
)
4691 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4694 struct isl_set
*isl_basic_set_partial_lexmin(
4695 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4696 struct isl_set
**empty
)
4698 return (struct isl_set
*)
4699 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4703 struct isl_set
*isl_basic_set_partial_lexmax(
4704 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4705 struct isl_set
**empty
)
4707 return (struct isl_set
*)
4708 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4712 /* Given a basic map "bmap", compute the lexicographically minimal
4713 * (or maximal) image element for each domain element in dom.
4714 * Set *empty to those elements in dom that do not have an image element.
4716 * We first make sure the basic sets in dom are disjoint and then
4717 * simply collect the results over each of the basic sets separately.
4718 * We could probably improve the efficiency a bit by moving the union
4719 * domain down into the parametric integer programming.
4721 static __isl_give isl_map
*basic_map_partial_lexopt(
4722 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4723 __isl_give isl_set
**empty
, int max
)
4726 struct isl_map
*res
;
4728 dom
= isl_set_make_disjoint(dom
);
4732 if (isl_set_fast_is_empty(dom
)) {
4733 res
= isl_map_empty_like_basic_map(bmap
);
4734 *empty
= isl_set_empty_like(dom
);
4736 isl_basic_map_free(bmap
);
4740 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4741 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4743 for (i
= 1; i
< dom
->n
; ++i
) {
4744 struct isl_map
*res_i
;
4745 struct isl_set
*empty_i
;
4747 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4748 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4750 res
= isl_map_union_disjoint(res
, res_i
);
4751 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4755 isl_basic_map_free(bmap
);
4760 isl_basic_map_free(bmap
);
4764 /* Given a map "map", compute the lexicographically minimal
4765 * (or maximal) image element for each domain element in dom.
4766 * Set *empty to those elements in dom that do not have an image element.
4768 * We first compute the lexicographically minimal or maximal element
4769 * in the first basic map. This results in a partial solution "res"
4770 * and a subset "todo" of dom that still need to be handled.
4771 * We then consider each of the remaining maps in "map" and successively
4772 * improve both "res" and "todo".
4774 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4775 * Assume we are computing the lexicographical maximum.
4776 * We first compute the lexicographically maximal element in basic map i.
4777 * This results in a partial solution res_i and a subset todo_i.
4778 * Then we combine these results with those obtain for the first k basic maps
4779 * to obtain a result that is valid for the first k+1 basic maps.
4780 * In particular, the set where there is no solution is the set where
4781 * there is no solution for the first k basic maps and also no solution
4782 * for the ith basic map, i.e.,
4784 * todo^i = todo^k * todo_i
4786 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4787 * solutions, arbitrarily breaking ties in favor of res^k.
4788 * That is, when res^k(a) >= res_i(a), we pick res^k and
4789 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4790 * the lexicographic order.)
4791 * In practice, we compute
4793 * res^k * (res_i . "<=")
4797 * res_i * (res^k . "<")
4799 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4800 * where only one of res^k and res_i provides a solution and we simply pick
4807 * Note that we only compute these intersections when dom(res^k) intersects
4808 * dom(res_i). Otherwise, the only effect of these intersections is to
4809 * potentially break up res^k and res_i into smaller pieces.
4810 * We want to avoid such splintering as much as possible.
4811 * In fact, an earlier implementation of this function would look for
4812 * better results in the domain of res^k and for extra results in todo^k,
4813 * but this would always result in a splintering according to todo^k,
4814 * even when the domain of basic map i is disjoint from the domains of
4815 * the previous basic maps.
4817 static __isl_give isl_map
*isl_map_partial_lexopt(
4818 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4819 __isl_give isl_set
**empty
, int max
)
4822 struct isl_map
*res
;
4823 struct isl_set
*todo
;
4828 if (isl_map_fast_is_empty(map
)) {
4836 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4837 isl_set_copy(dom
), &todo
, max
);
4839 for (i
= 1; i
< map
->n
; ++i
) {
4843 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4845 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4846 isl_set_copy(dom
), &todo_i
, max
);
4849 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4850 le
= isl_map_lex_le(dim
);
4852 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4853 le
= isl_map_lex_ge(dim
);
4855 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4856 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4857 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4858 le
= isl_map_intersect(le
, isl_map_copy(res
));
4860 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4861 res
= isl_map_intersect_domain(res
,
4862 isl_set_copy(todo_i
));
4863 res_i
= isl_map_intersect_domain(res_i
,
4864 isl_set_copy(todo
));
4867 res
= isl_map_union_disjoint(res
, res_i
);
4868 res
= isl_map_union_disjoint(res
, lt
);
4869 res
= isl_map_union_disjoint(res
, le
);
4871 todo
= isl_set_intersect(todo
, todo_i
);
4891 __isl_give isl_map
*isl_map_partial_lexmax(
4892 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4893 __isl_give isl_set
**empty
)
4895 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4898 __isl_give isl_map
*isl_map_partial_lexmin(
4899 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4900 __isl_give isl_set
**empty
)
4902 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4905 __isl_give isl_set
*isl_set_partial_lexmin(
4906 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4907 __isl_give isl_set
**empty
)
4909 return (struct isl_set
*)
4910 isl_map_partial_lexmin((struct isl_map
*)set
,
4914 __isl_give isl_set
*isl_set_partial_lexmax(
4915 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4916 __isl_give isl_set
**empty
)
4918 return (struct isl_set
*)
4919 isl_map_partial_lexmax((struct isl_map
*)set
,
4923 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4925 struct isl_basic_set
*dom
= NULL
;
4926 struct isl_dim
*dom_dim
;
4930 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4931 dom
= isl_basic_set_universe(dom_dim
);
4932 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4934 isl_basic_map_free(bmap
);
4938 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4940 return isl_basic_map_lexopt(bmap
, 0);
4943 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4945 return isl_basic_map_lexopt(bmap
, 1);
4948 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4950 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4953 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4955 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4958 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4960 struct isl_set
*dom
= NULL
;
4961 struct isl_dim
*dom_dim
;
4965 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4966 dom
= isl_set_universe(dom_dim
);
4967 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4973 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4975 return isl_map_lexopt(map
, 0);
4978 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4980 return isl_map_lexopt(map
, 1);
4983 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4985 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4988 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4990 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4993 /* Apply a preimage specified by "mat" on the parameters of "bset".
4994 * bset is assumed to have only parameters and divs.
4996 static struct isl_basic_set
*basic_set_parameter_preimage(
4997 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5004 bset
->dim
= isl_dim_cow(bset
->dim
);
5008 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5010 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5012 bset
->dim
->nparam
= 0;
5013 bset
->dim
->n_out
= nparam
;
5014 bset
= isl_basic_set_preimage(bset
, mat
);
5016 bset
->dim
->nparam
= bset
->dim
->n_out
;
5017 bset
->dim
->n_out
= 0;
5022 isl_basic_set_free(bset
);
5026 /* Apply a preimage specified by "mat" on the parameters of "set".
5027 * set is assumed to have only parameters and divs.
5029 static struct isl_set
*set_parameter_preimage(
5030 struct isl_set
*set
, struct isl_mat
*mat
)
5032 struct isl_dim
*dim
= NULL
;
5038 dim
= isl_dim_copy(set
->dim
);
5039 dim
= isl_dim_cow(dim
);
5043 nparam
= isl_set_dim(set
, isl_dim_param
);
5045 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5048 dim
->n_out
= nparam
;
5049 isl_set_reset_dim(set
, dim
);
5050 set
= isl_set_preimage(set
, mat
);
5053 dim
= isl_dim_copy(set
->dim
);
5054 dim
= isl_dim_cow(dim
);
5057 dim
->nparam
= dim
->n_out
;
5059 isl_set_reset_dim(set
, dim
);
5069 /* Intersect the basic set "bset" with the affine space specified by the
5070 * equalities in "eq".
5072 static struct isl_basic_set
*basic_set_append_equalities(
5073 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5081 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5086 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5087 for (i
= 0; i
< eq
->n_row
; ++i
) {
5088 k
= isl_basic_set_alloc_equality(bset
);
5091 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5092 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5096 bset
= isl_basic_set_gauss(bset
, NULL
);
5097 bset
= isl_basic_set_finalize(bset
);
5102 isl_basic_set_free(bset
);
5106 /* Intersect the set "set" with the affine space specified by the
5107 * equalities in "eq".
5109 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5117 for (i
= 0; i
< set
->n
; ++i
) {
5118 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5131 /* Project the given basic set onto its parameter domain, possibly introducing
5132 * new, explicit, existential variables in the constraints.
5133 * The input has parameters and (possibly implicit) existential variables.
5134 * The output has the same parameters, but only
5135 * explicit existentially quantified variables.
5137 * The actual projection is performed by pip, but pip doesn't seem
5138 * to like equalities very much, so we first remove the equalities
5139 * among the parameters by performing a variable compression on
5140 * the parameters. Afterward, an inverse transformation is performed
5141 * and the equalities among the parameters are inserted back in.
5143 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5147 struct isl_mat
*T
, *T2
;
5148 struct isl_set
*set
;
5149 unsigned nparam
, n_div
;
5151 bset
= isl_basic_set_cow(bset
);
5155 if (bset
->n_eq
== 0)
5156 return isl_basic_set_lexmin(bset
);
5158 isl_basic_set_gauss(bset
, NULL
);
5160 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5161 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5163 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5164 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5167 if (i
== bset
->n_eq
)
5168 return isl_basic_set_lexmin(bset
);
5170 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5172 eq
= isl_mat_cow(eq
);
5173 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5174 if (T
&& T
->n_col
== 0) {
5178 bset
= isl_basic_set_set_to_empty(bset
);
5179 return isl_set_from_basic_set(bset
);
5181 bset
= basic_set_parameter_preimage(bset
, T
);
5183 set
= isl_basic_set_lexmin(bset
);
5184 set
= set_parameter_preimage(set
, T2
);
5185 set
= set_append_equalities(set
, eq
);
5189 /* Compute an explicit representation for all the existentially
5190 * quantified variables.
5191 * The input and output dimensions are first turned into parameters.
5192 * compute_divs then returns a map with the same parameters and
5193 * no input or output dimensions and the dimension specification
5194 * is reset to that of the input.
5196 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5198 struct isl_basic_set
*bset
;
5199 struct isl_set
*set
;
5200 struct isl_map
*map
;
5201 struct isl_dim
*dim
, *orig_dim
= NULL
;
5206 bmap
= isl_basic_map_cow(bmap
);
5210 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5211 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5212 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5213 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5217 orig_dim
= bmap
->dim
;
5219 bset
= (struct isl_basic_set
*)bmap
;
5221 set
= parameter_compute_divs(bset
);
5222 map
= (struct isl_map
*)set
;
5223 map
= isl_map_reset_dim(map
, orig_dim
);
5227 isl_basic_map_free(bmap
);
5231 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5239 off
= isl_dim_total(bmap
->dim
);
5240 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5241 if (isl_int_is_zero(bmap
->div
[i
][0]))
5243 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5249 static int map_divs_known(__isl_keep isl_map
*map
)
5256 for (i
= 0; i
< map
->n
; ++i
) {
5257 int known
= basic_map_divs_known(map
->p
[i
]);
5265 /* If bmap contains any unknown divs, then compute explicit
5266 * expressions for them. However, this computation may be
5267 * quite expensive, so first try to remove divs that aren't
5270 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5274 struct isl_map
*map
;
5276 known
= basic_map_divs_known(bmap
);
5280 return isl_map_from_basic_map(bmap
);
5282 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5284 known
= basic_map_divs_known(bmap
);
5288 return isl_map_from_basic_map(bmap
);
5290 map
= compute_divs(bmap
);
5293 isl_basic_map_free(bmap
);
5297 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5301 struct isl_map
*res
;
5308 known
= map_divs_known(map
);
5316 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5317 for (i
= 1 ; i
< map
->n
; ++i
) {
5319 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5320 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5321 res
= isl_map_union_disjoint(res
, r2
);
5323 res
= isl_map_union(res
, r2
);
5330 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5332 return (struct isl_set
*)
5333 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5336 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5338 return (struct isl_set
*)
5339 isl_map_compute_divs((struct isl_map
*)set
);
5342 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5345 struct isl_set
*set
;
5350 map
= isl_map_cow(map
);
5354 set
= (struct isl_set
*)map
;
5355 set
->dim
= isl_dim_domain(set
->dim
);
5358 for (i
= 0; i
< map
->n
; ++i
) {
5359 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5363 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5364 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5371 struct isl_map
*isl_map_union_disjoint(
5372 struct isl_map
*map1
, struct isl_map
*map2
)
5376 struct isl_map
*map
= NULL
;
5390 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5392 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5393 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5394 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5396 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5397 map1
->n
+ map2
->n
, flags
);
5400 for (i
= 0; i
< map1
->n
; ++i
) {
5401 map
= isl_map_add_basic_map(map
,
5402 isl_basic_map_copy(map1
->p
[i
]));
5406 for (i
= 0; i
< map2
->n
; ++i
) {
5407 map
= isl_map_add_basic_map(map
,
5408 isl_basic_map_copy(map2
->p
[i
]));
5422 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5424 map1
= isl_map_union_disjoint(map1
, map2
);
5428 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5432 struct isl_set
*isl_set_union_disjoint(
5433 struct isl_set
*set1
, struct isl_set
*set2
)
5435 return (struct isl_set
*)
5436 isl_map_union_disjoint(
5437 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5440 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5442 return (struct isl_set
*)
5443 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5446 struct isl_map
*isl_map_intersect_range(
5447 struct isl_map
*map
, struct isl_set
*set
)
5450 struct isl_map
*result
;
5456 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5457 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5458 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5460 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5461 map
->n
* set
->n
, flags
);
5464 for (i
= 0; i
< map
->n
; ++i
)
5465 for (j
= 0; j
< set
->n
; ++j
) {
5466 result
= isl_map_add_basic_map(result
,
5467 isl_basic_map_intersect_range(
5468 isl_basic_map_copy(map
->p
[i
]),
5469 isl_basic_set_copy(set
->p
[j
])));
5482 struct isl_map
*isl_map_intersect_domain(
5483 struct isl_map
*map
, struct isl_set
*set
)
5485 return isl_map_reverse(
5486 isl_map_intersect_range(isl_map_reverse(map
), set
));
5489 struct isl_map
*isl_map_apply_domain(
5490 struct isl_map
*map1
, struct isl_map
*map2
)
5494 map1
= isl_map_reverse(map1
);
5495 map1
= isl_map_apply_range(map1
, map2
);
5496 return isl_map_reverse(map1
);
5503 struct isl_map
*isl_map_apply_range(
5504 struct isl_map
*map1
, struct isl_map
*map2
)
5506 struct isl_dim
*dim_result
;
5507 struct isl_map
*result
;
5513 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5514 isl_dim_copy(map2
->dim
));
5516 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5519 for (i
= 0; i
< map1
->n
; ++i
)
5520 for (j
= 0; j
< map2
->n
; ++j
) {
5521 result
= isl_map_add_basic_map(result
,
5522 isl_basic_map_apply_range(
5523 isl_basic_map_copy(map1
->p
[i
]),
5524 isl_basic_map_copy(map2
->p
[j
])));
5530 if (result
&& result
->n
<= 1)
5531 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5540 * returns range - domain
5542 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5544 isl_dim
*dims
, *target_dim
;
5545 struct isl_basic_set
*bset
;
5552 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5553 bmap
->dim
, isl_dim_out
),
5555 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5556 dim
= isl_basic_map_n_in(bmap
);
5557 nparam
= isl_basic_map_n_param(bmap
);
5558 bset
= isl_basic_set_from_basic_map(bmap
);
5559 bset
= isl_basic_set_cow(bset
);
5560 dims
= isl_basic_set_get_dim(bset
);
5561 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5562 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5563 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5564 for (i
= 0; i
< dim
; ++i
) {
5565 int j
= isl_basic_map_alloc_equality(
5566 (struct isl_basic_map
*)bset
);
5569 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5570 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5571 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5572 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5574 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5575 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5578 isl_basic_map_free(bmap
);
5583 * returns range - domain
5585 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5589 struct isl_set
*result
;
5594 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5595 map
->dim
, isl_dim_out
),
5597 dim
= isl_map_get_dim(map
);
5598 dim
= isl_dim_domain(dim
);
5599 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5602 for (i
= 0; i
< map
->n
; ++i
)
5603 result
= isl_set_add_basic_set(result
,
5604 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5612 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5614 struct isl_basic_map
*bmap
;
5622 nparam
= dims
->nparam
;
5624 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5628 for (i
= 0; i
< dim
; ++i
) {
5629 int j
= isl_basic_map_alloc_equality(bmap
);
5632 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5633 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5634 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5636 return isl_basic_map_finalize(bmap
);
5638 isl_basic_map_free(bmap
);
5642 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5644 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5647 return basic_map_identity(dim
);
5650 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5652 if (!model
|| !model
->dim
)
5654 isl_assert(model
->ctx
,
5655 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5656 return basic_map_identity(isl_dim_copy(model
->dim
));
5659 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5661 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5662 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5665 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5667 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5670 return map_identity(dim
);
5673 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5675 if (!model
|| !model
->dim
)
5677 isl_assert(model
->ctx
,
5678 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5679 return map_identity(isl_dim_copy(model
->dim
));
5682 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5684 if (!model
|| !model
->dim
)
5686 isl_assert(model
->ctx
,
5687 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5688 return map_identity(isl_dim_copy(model
->dim
));
5691 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5693 isl_dim
*dim
= isl_set_get_dim(set
);
5695 id
= isl_map_identity(dim
);
5696 return isl_map_intersect_range(id
, set
);
5699 /* Construct a basic set with all set dimensions having only non-negative
5702 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5707 struct isl_basic_set
*bset
;
5711 nparam
= dims
->nparam
;
5713 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5716 for (i
= 0; i
< dim
; ++i
) {
5717 int k
= isl_basic_set_alloc_inequality(bset
);
5720 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5721 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5725 isl_basic_set_free(bset
);
5729 /* Construct the half-space x_pos >= 0.
5731 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5735 isl_basic_set
*nonneg
;
5737 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5738 k
= isl_basic_set_alloc_inequality(nonneg
);
5741 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5742 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5744 return isl_basic_set_finalize(nonneg
);
5746 isl_basic_set_free(nonneg
);
5750 /* Construct the half-space x_pos <= -1.
5752 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5757 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5758 k
= isl_basic_set_alloc_inequality(neg
);
5761 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5762 isl_int_set_si(neg
->ineq
[k
][0], -1);
5763 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5765 return isl_basic_set_finalize(neg
);
5767 isl_basic_set_free(neg
);
5771 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5772 enum isl_dim_type type
, unsigned first
, unsigned n
)
5775 isl_basic_set
*nonneg
;
5783 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5785 for (i
= 0; i
< n
; ++i
) {
5786 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5787 pos(set
->dim
, type
) + first
+ i
);
5788 neg
= neg_halfspace(isl_set_get_dim(set
),
5789 pos(set
->dim
, type
) + first
+ i
);
5791 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5800 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5801 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5808 if (isl_set_fast_is_empty(set
)) {
5813 return fn(set
, signs
, user
);
5816 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5818 half
= isl_set_intersect(half
, isl_set_copy(set
));
5819 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5823 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5825 half
= isl_set_intersect(half
, set
);
5826 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5832 /* Call "fn" on the intersections of "set" with each of the orthants
5833 * (except for obviously empty intersections). The orthant is identified
5834 * by the signs array, with each entry having value 1 or -1 according
5835 * to the sign of the corresponding variable.
5837 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5838 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5848 if (isl_set_fast_is_empty(set
))
5851 nparam
= isl_set_dim(set
, isl_dim_param
);
5852 nvar
= isl_set_dim(set
, isl_dim_set
);
5854 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5856 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5864 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5866 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5869 int isl_basic_map_is_subset(
5870 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5873 struct isl_map
*map1
;
5874 struct isl_map
*map2
;
5876 if (!bmap1
|| !bmap2
)
5879 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5880 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5882 is_subset
= isl_map_is_subset(map1
, map2
);
5890 int isl_basic_map_is_equal(
5891 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5895 if (!bmap1
|| !bmap2
)
5897 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5900 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5904 int isl_basic_set_is_equal(
5905 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5907 return isl_basic_map_is_equal(
5908 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5911 int isl_map_is_empty(struct isl_map
*map
)
5918 for (i
= 0; i
< map
->n
; ++i
) {
5919 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5928 int isl_map_fast_is_empty(struct isl_map
*map
)
5930 return map
? map
->n
== 0 : -1;
5933 int isl_set_fast_is_empty(struct isl_set
*set
)
5935 return set
? set
->n
== 0 : -1;
5938 int isl_set_is_empty(struct isl_set
*set
)
5940 return isl_map_is_empty((struct isl_map
*)set
);
5943 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5948 return isl_dim_equal(map1
->dim
, map2
->dim
);
5951 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5956 return isl_dim_equal(set1
->dim
, set2
->dim
);
5959 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5965 is_subset
= isl_map_is_subset(map1
, map2
);
5968 is_subset
= isl_map_is_subset(map2
, map1
);
5972 int isl_basic_map_is_strict_subset(
5973 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5977 if (!bmap1
|| !bmap2
)
5979 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5982 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5983 if (is_subset
== -1)
5988 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5994 is_subset
= isl_map_is_subset(map1
, map2
);
5997 is_subset
= isl_map_is_subset(map2
, map1
);
5998 if (is_subset
== -1)
6003 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6005 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6008 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6012 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6015 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6019 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6022 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
6029 for (i
= 0; i
< map
->n
; ++i
) {
6030 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6038 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6040 return isl_map_fast_is_universe((isl_map
*) set
);
6043 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6045 struct isl_basic_set
*bset
= NULL
;
6046 struct isl_vec
*sample
= NULL
;
6053 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6056 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6057 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6058 copy
= isl_basic_map_remove_redundancies(copy
);
6059 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6060 isl_basic_map_free(copy
);
6064 total
= 1 + isl_basic_map_total_dim(bmap
);
6065 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6066 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6072 isl_vec_free(bmap
->sample
);
6073 bmap
->sample
= NULL
;
6074 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6077 sample
= isl_basic_set_sample_vec(bset
);
6080 empty
= sample
->size
== 0;
6081 isl_vec_free(bmap
->sample
);
6082 bmap
->sample
= sample
;
6084 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6089 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6093 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6096 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6100 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6103 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6105 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6108 struct isl_map
*isl_basic_map_union(
6109 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6111 struct isl_map
*map
;
6112 if (!bmap1
|| !bmap2
)
6115 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6117 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6120 map
= isl_map_add_basic_map(map
, bmap1
);
6121 map
= isl_map_add_basic_map(map
, bmap2
);
6124 isl_basic_map_free(bmap1
);
6125 isl_basic_map_free(bmap2
);
6129 struct isl_set
*isl_basic_set_union(
6130 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6132 return (struct isl_set
*)isl_basic_map_union(
6133 (struct isl_basic_map
*)bset1
,
6134 (struct isl_basic_map
*)bset2
);
6137 /* Order divs such that any div only depends on previous divs */
6138 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6146 off
= isl_dim_total(bmap
->dim
);
6148 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6150 if (isl_int_is_zero(bmap
->div
[i
][0]))
6152 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6156 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6162 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6164 return (struct isl_basic_set
*)
6165 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6168 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6175 for (i
= 0; i
< map
->n
; ++i
) {
6176 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6187 /* Look for a div in dst that corresponds to the div "div" in src.
6188 * The divs before "div" in src and dst are assumed to be the same.
6190 * Returns -1 if no corresponding div was found and the position
6191 * of the corresponding div in dst otherwise.
6193 static int find_div(struct isl_basic_map
*dst
,
6194 struct isl_basic_map
*src
, unsigned div
)
6198 unsigned total
= isl_dim_total(src
->dim
);
6200 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6201 for (i
= div
; i
< dst
->n_div
; ++i
)
6202 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6203 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6204 dst
->n_div
- div
) == -1)
6209 struct isl_basic_map
*isl_basic_map_align_divs(
6210 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6213 unsigned total
= isl_dim_total(src
->dim
);
6218 if (src
->n_div
== 0)
6221 for (i
= 0; i
< src
->n_div
; ++i
)
6222 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6224 src
= isl_basic_map_order_divs(src
);
6225 dst
= isl_basic_map_cow(dst
);
6226 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6227 src
->n_div
, 0, 2 * src
->n_div
);
6230 for (i
= 0; i
< src
->n_div
; ++i
) {
6231 int j
= find_div(dst
, src
, i
);
6233 j
= isl_basic_map_alloc_div(dst
);
6236 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6237 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6238 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6242 isl_basic_map_swap_div(dst
, i
, j
);
6246 isl_basic_map_free(dst
);
6250 struct isl_basic_set
*isl_basic_set_align_divs(
6251 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6253 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6254 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6257 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6265 map
= isl_map_compute_divs(map
);
6266 map
= isl_map_cow(map
);
6270 for (i
= 1; i
< map
->n
; ++i
)
6271 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6272 for (i
= 1; i
< map
->n
; ++i
)
6273 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6275 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6279 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6281 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6284 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6288 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6289 map
= isl_map_intersect_domain(map
, set
);
6290 set
= isl_map_range(map
);
6298 /* There is no need to cow as removing empty parts doesn't change
6299 * the meaning of the set.
6301 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6308 for (i
= map
->n
-1; i
>= 0; --i
) {
6309 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6311 isl_basic_map_free(map
->p
[i
]);
6312 if (i
!= map
->n
-1) {
6313 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6314 map
->p
[i
] = map
->p
[map
->n
-1];
6322 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6324 return (struct isl_set
*)
6325 isl_map_remove_empty_parts((struct isl_map
*)set
);
6328 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6330 struct isl_basic_map
*bmap
;
6331 if (!map
|| map
->n
== 0)
6333 bmap
= map
->p
[map
->n
-1];
6334 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6335 return isl_basic_map_copy(bmap
);
6338 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6340 return (struct isl_basic_set
*)
6341 isl_map_copy_basic_map((struct isl_map
*)set
);
6344 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6345 __isl_keep isl_basic_map
*bmap
)
6351 for (i
= map
->n
-1; i
>= 0; --i
) {
6352 if (map
->p
[i
] != bmap
)
6354 map
= isl_map_cow(map
);
6357 isl_basic_map_free(map
->p
[i
]);
6358 if (i
!= map
->n
-1) {
6359 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6360 map
->p
[i
] = map
->p
[map
->n
-1];
6371 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6372 struct isl_basic_set
*bset
)
6374 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6375 (struct isl_basic_map
*)bset
);
6378 /* Given two basic sets bset1 and bset2, compute the maximal difference
6379 * between the values of dimension pos in bset1 and those in bset2
6380 * for any common value of the parameters and dimensions preceding pos.
6382 static enum isl_lp_result
basic_set_maximal_difference_at(
6383 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6384 int pos
, isl_int
*opt
)
6386 struct isl_dim
*dims
;
6387 struct isl_basic_map
*bmap1
= NULL
;
6388 struct isl_basic_map
*bmap2
= NULL
;
6389 struct isl_ctx
*ctx
;
6390 struct isl_vec
*obj
;
6393 unsigned dim1
, dim2
;
6394 enum isl_lp_result res
;
6396 if (!bset1
|| !bset2
)
6397 return isl_lp_error
;
6399 nparam
= isl_basic_set_n_param(bset1
);
6400 dim1
= isl_basic_set_n_dim(bset1
);
6401 dim2
= isl_basic_set_n_dim(bset2
);
6402 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6403 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6404 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6405 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6406 if (!bmap1
|| !bmap2
)
6408 bmap1
= isl_basic_map_cow(bmap1
);
6409 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6410 pos
, (dim1
- pos
) + (dim2
- pos
),
6411 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6412 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6415 total
= isl_basic_map_total_dim(bmap1
);
6417 obj
= isl_vec_alloc(ctx
, 1 + total
);
6418 isl_seq_clr(obj
->block
.data
, 1 + total
);
6419 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6420 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6423 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6425 isl_basic_map_free(bmap1
);
6429 isl_basic_map_free(bmap1
);
6430 isl_basic_map_free(bmap2
);
6431 return isl_lp_error
;
6434 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6435 * for any common value of the parameters and dimensions preceding pos
6436 * in both basic sets, the values of dimension pos in bset1 are
6437 * smaller or larger than those in bset2.
6440 * 1 if bset1 follows bset2
6441 * -1 if bset1 precedes bset2
6442 * 0 if bset1 and bset2 are incomparable
6443 * -2 if some error occurred.
6445 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6446 struct isl_basic_set
*bset2
, int pos
)
6449 enum isl_lp_result res
;
6454 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6456 if (res
== isl_lp_empty
)
6458 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6459 res
== isl_lp_unbounded
)
6461 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6470 /* Given two basic sets bset1 and bset2, check whether
6471 * for any common value of the parameters and dimensions preceding pos
6472 * there is a value of dimension pos in bset1 that is larger
6473 * than a value of the same dimension in bset2.
6476 * 1 if there exists such a pair
6477 * 0 if there is no such pair, but there is a pair of equal values
6479 * -2 if some error occurred.
6481 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6482 __isl_keep isl_basic_set
*bset2
, int pos
)
6485 enum isl_lp_result res
;
6490 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6492 if (res
== isl_lp_empty
)
6494 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6495 res
== isl_lp_unbounded
)
6497 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6499 else if (res
== isl_lp_ok
)
6508 /* Given two sets set1 and set2, check whether
6509 * for any common value of the parameters and dimensions preceding pos
6510 * there is a value of dimension pos in set1 that is larger
6511 * than a value of the same dimension in set2.
6514 * 1 if there exists such a pair
6515 * 0 if there is no such pair, but there is a pair of equal values
6517 * -2 if some error occurred.
6519 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6520 __isl_keep isl_set
*set2
, int pos
)
6528 for (i
= 0; i
< set1
->n
; ++i
)
6529 for (j
= 0; j
< set2
->n
; ++j
) {
6531 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6532 if (f
== 1 || f
== -2)
6541 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6542 unsigned pos
, isl_int
*val
)
6550 total
= isl_basic_map_total_dim(bmap
);
6551 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6552 for (; d
+1 > pos
; --d
)
6553 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6557 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6559 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6561 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6564 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6570 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6571 unsigned pos
, isl_int
*val
)
6583 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6586 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6587 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6588 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6589 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6593 isl_int_set(*val
, v
);
6599 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6600 unsigned pos
, isl_int
*val
)
6602 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6606 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6609 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6612 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6613 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6615 if (pos
>= isl_basic_map_dim(bmap
, type
))
6617 return isl_basic_map_fast_has_fixed_var(bmap
,
6618 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6621 int isl_map_fast_is_fixed(struct isl_map
*map
,
6622 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6624 if (pos
>= isl_map_dim(map
, type
))
6626 return isl_map_fast_has_fixed_var(map
,
6627 map_offset(map
, type
) - 1 + pos
, val
);
6630 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6631 * then return this fixed value in *val.
6633 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6636 return isl_basic_set_fast_has_fixed_var(bset
,
6637 isl_basic_set_n_param(bset
) + dim
, val
);
6640 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6641 * then return this fixed value in *val.
6643 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6645 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6648 /* Check if input variable in has fixed value and if so and if val is not NULL,
6649 * then return this fixed value in *val.
6651 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6653 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6656 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6657 * and if val is not NULL, then return this lower bound in *val.
6659 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6660 unsigned dim
, isl_int
*val
)
6662 int i
, i_eq
= -1, i_ineq
= -1;
6669 total
= isl_basic_set_total_dim(bset
);
6670 nparam
= isl_basic_set_n_param(bset
);
6671 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6672 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6678 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6679 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6681 if (i_eq
!= -1 || i_ineq
!= -1)
6685 if (i_eq
== -1 && i_ineq
== -1)
6687 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6688 /* The coefficient should always be one due to normalization. */
6689 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6691 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6693 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6694 total
- nparam
- dim
- 1) != -1)
6697 isl_int_neg(*val
, c
[0]);
6701 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6702 unsigned dim
, isl_int
*val
)
6714 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6718 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6720 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6721 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6723 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6727 isl_int_set(*val
, v
);
6738 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6740 const struct constraint
*c1
= (const struct constraint
*)p1
;
6741 const struct constraint
*c2
= (const struct constraint
*)p2
;
6742 unsigned size
= isl_min(c1
->size
, c2
->size
);
6743 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6746 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6747 struct isl_basic_map
*bmap
)
6750 struct constraint
*c
;
6755 total
= isl_basic_map_total_dim(bmap
);
6756 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6759 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6761 c
[i
].c
= bmap
->ineq
[i
];
6763 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6764 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6765 bmap
->ineq
[i
] = c
[i
].c
;
6769 isl_basic_map_free(bmap
);
6773 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6774 __isl_take isl_basic_set
*bset
)
6776 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6777 (struct isl_basic_map
*)bset
);
6780 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6784 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6786 bmap
= isl_basic_map_remove_redundancies(bmap
);
6787 bmap
= isl_basic_map_sort_constraints(bmap
);
6788 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6792 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6794 return (struct isl_basic_set
*)isl_basic_map_normalize(
6795 (struct isl_basic_map
*)bset
);
6798 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6799 const __isl_keep isl_basic_map
*bmap2
)
6806 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
6807 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
6808 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
6809 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6810 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6811 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6812 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6813 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6814 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6815 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6816 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6818 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6820 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6822 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6823 return bmap1
->n_eq
- bmap2
->n_eq
;
6824 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6825 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6826 if (bmap1
->n_div
!= bmap2
->n_div
)
6827 return bmap1
->n_div
- bmap2
->n_div
;
6828 total
= isl_basic_map_total_dim(bmap1
);
6829 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6830 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6834 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6835 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6839 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6840 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6847 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6848 __isl_keep isl_basic_map
*bmap2
)
6850 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6853 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6854 __isl_keep isl_basic_set
*bset2
)
6856 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6857 (isl_basic_map
*)bset2
);
6860 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6862 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6863 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6865 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6868 /* We normalize in place, but if anything goes wrong we need
6869 * to return NULL, so we need to make sure we don't change the
6870 * meaning of any possible other copies of map.
6872 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6875 struct isl_basic_map
*bmap
;
6879 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6881 for (i
= 0; i
< map
->n
; ++i
) {
6882 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6885 isl_basic_map_free(map
->p
[i
]);
6888 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6889 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6890 map
= isl_map_remove_empty_parts(map
);
6893 for (i
= map
->n
- 1; i
>= 1; --i
) {
6894 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6896 isl_basic_map_free(map
->p
[i
-1]);
6897 for (j
= i
; j
< map
->n
; ++j
)
6898 map
->p
[j
-1] = map
->p
[j
];
6908 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6910 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6913 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6923 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6926 map1
= isl_map_copy(map1
);
6927 map2
= isl_map_copy(map2
);
6928 map1
= isl_map_normalize(map1
);
6929 map2
= isl_map_normalize(map2
);
6932 equal
= map1
->n
== map2
->n
;
6933 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6934 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6947 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6949 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6950 (struct isl_map
*)set2
);
6953 /* Return an interval that ranges from min to max (inclusive)
6955 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6956 isl_int min
, isl_int max
)
6959 struct isl_basic_set
*bset
= NULL
;
6961 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6965 k
= isl_basic_set_alloc_inequality(bset
);
6968 isl_int_set_si(bset
->ineq
[k
][1], 1);
6969 isl_int_neg(bset
->ineq
[k
][0], min
);
6971 k
= isl_basic_set_alloc_inequality(bset
);
6974 isl_int_set_si(bset
->ineq
[k
][1], -1);
6975 isl_int_set(bset
->ineq
[k
][0], max
);
6979 isl_basic_set_free(bset
);
6983 /* Return the Cartesian product of the basic sets in list (in the given order).
6985 __isl_give isl_basic_set
*isl_basic_set_list_product(
6986 __isl_take
struct isl_basic_set_list
*list
)
6994 struct isl_basic_set
*product
= NULL
;
6998 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6999 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7000 nparam
= isl_basic_set_n_param(list
->p
[0]);
7001 dim
= isl_basic_set_n_dim(list
->p
[0]);
7002 extra
= list
->p
[0]->n_div
;
7003 n_eq
= list
->p
[0]->n_eq
;
7004 n_ineq
= list
->p
[0]->n_ineq
;
7005 for (i
= 1; i
< list
->n
; ++i
) {
7006 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7007 isl_assert(list
->ctx
,
7008 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7009 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7010 extra
+= list
->p
[i
]->n_div
;
7011 n_eq
+= list
->p
[i
]->n_eq
;
7012 n_ineq
+= list
->p
[i
]->n_ineq
;
7014 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7019 for (i
= 0; i
< list
->n
; ++i
) {
7020 isl_basic_set_add_constraints(product
,
7021 isl_basic_set_copy(list
->p
[i
]), dim
);
7022 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7024 isl_basic_set_list_free(list
);
7027 isl_basic_set_free(product
);
7028 isl_basic_set_list_free(list
);
7032 struct isl_basic_map
*isl_basic_map_product(
7033 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7035 struct isl_dim
*dim_result
= NULL
;
7036 struct isl_basic_map
*bmap
;
7037 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7038 struct isl_dim_map
*dim_map1
, *dim_map2
;
7040 if (!bmap1
|| !bmap2
)
7043 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7044 bmap2
->dim
, isl_dim_param
), goto error
);
7045 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7046 isl_dim_copy(bmap2
->dim
));
7048 in1
= isl_basic_map_n_in(bmap1
);
7049 in2
= isl_basic_map_n_in(bmap2
);
7050 out1
= isl_basic_map_n_out(bmap1
);
7051 out2
= isl_basic_map_n_out(bmap2
);
7052 nparam
= isl_basic_map_n_param(bmap1
);
7054 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7055 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7056 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7057 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7058 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7059 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7060 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7061 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7062 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7063 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7064 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7066 bmap
= isl_basic_map_alloc_dim(dim_result
,
7067 bmap1
->n_div
+ bmap2
->n_div
,
7068 bmap1
->n_eq
+ bmap2
->n_eq
,
7069 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7070 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7071 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7072 bmap
= isl_basic_map_simplify(bmap
);
7073 return isl_basic_map_finalize(bmap
);
7075 isl_basic_map_free(bmap1
);
7076 isl_basic_map_free(bmap2
);
7080 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7081 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7083 isl_basic_map
*prod
;
7085 prod
= isl_basic_map_product(bmap1
, bmap2
);
7086 prod
= isl_basic_map_flatten(prod
);
7090 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7091 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7093 return isl_basic_map_flat_product(bset1
, bset2
);
7096 __isl_give isl_basic_map
*isl_basic_map_range_product(
7097 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7099 isl_dim
*dim_result
= NULL
;
7100 isl_basic_map
*bmap
;
7101 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7102 struct isl_dim_map
*dim_map1
, *dim_map2
;
7104 if (!bmap1
|| !bmap2
)
7107 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7108 isl_dim_copy(bmap2
->dim
));
7110 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7111 out1
= isl_basic_map_n_out(bmap1
);
7112 out2
= isl_basic_map_n_out(bmap2
);
7113 nparam
= isl_basic_map_n_param(bmap1
);
7115 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7116 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7117 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7118 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7119 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7120 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7121 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7122 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7123 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7124 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7125 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7127 bmap
= isl_basic_map_alloc_dim(dim_result
,
7128 bmap1
->n_div
+ bmap2
->n_div
,
7129 bmap1
->n_eq
+ bmap2
->n_eq
,
7130 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7131 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7132 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7133 bmap
= isl_basic_map_simplify(bmap
);
7134 return isl_basic_map_finalize(bmap
);
7136 isl_basic_map_free(bmap1
);
7137 isl_basic_map_free(bmap2
);
7141 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7142 __isl_take isl_map
*map2
,
7143 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7144 __isl_take isl_dim
*right
),
7145 __isl_give isl_basic_map
*(*basic_map_product
)(
7146 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7149 struct isl_map
*result
;
7155 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7156 map2
->dim
, isl_dim_param
), goto error
);
7158 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7159 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7160 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7162 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7163 isl_dim_copy(map2
->dim
)),
7164 map1
->n
* map2
->n
, flags
);
7167 for (i
= 0; i
< map1
->n
; ++i
)
7168 for (j
= 0; j
< map2
->n
; ++j
) {
7169 struct isl_basic_map
*part
;
7170 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7171 isl_basic_map_copy(map2
->p
[j
]));
7172 if (isl_basic_map_is_empty(part
))
7173 isl_basic_map_free(part
);
7175 result
= isl_map_add_basic_map(result
, part
);
7188 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7190 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7192 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7195 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7197 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7198 __isl_take isl_map
*map2
)
7202 prod
= isl_map_product(map1
, map2
);
7203 prod
= isl_map_flatten(prod
);
7207 /* Given two set A and B, construct its Cartesian product A x B.
7209 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7211 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7212 (struct isl_map
*)set2
);
7215 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7216 __isl_take isl_set
*set2
)
7218 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7221 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7223 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7224 __isl_take isl_map
*map2
)
7226 return map_product(map1
, map2
, &isl_dim_range_product
,
7227 &isl_basic_map_range_product
);
7230 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7233 uint32_t hash
= isl_hash_init();
7238 bmap
= isl_basic_map_copy(bmap
);
7239 bmap
= isl_basic_map_normalize(bmap
);
7242 total
= isl_basic_map_total_dim(bmap
);
7243 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7244 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7246 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7247 isl_hash_hash(hash
, c_hash
);
7249 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7250 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7252 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7253 isl_hash_hash(hash
, c_hash
);
7255 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7256 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7258 if (isl_int_is_zero(bmap
->div
[i
][0]))
7260 isl_hash_byte(hash
, i
& 0xFF);
7261 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7262 isl_hash_hash(hash
, c_hash
);
7264 isl_basic_map_free(bmap
);
7268 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7270 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7273 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7280 map
= isl_map_copy(map
);
7281 map
= isl_map_normalize(map
);
7285 hash
= isl_hash_init();
7286 for (i
= 0; i
< map
->n
; ++i
) {
7288 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7289 isl_hash_hash(hash
, bmap_hash
);
7297 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7299 return isl_map_get_hash((isl_map
*)set
);
7302 /* Check if the value for dimension dim is completely determined
7303 * by the values of the other parameters and variables.
7304 * That is, check if dimension dim is involved in an equality.
7306 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7313 nparam
= isl_basic_set_n_param(bset
);
7314 for (i
= 0; i
< bset
->n_eq
; ++i
)
7315 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7320 /* Check if the value for dimension dim is completely determined
7321 * by the values of the other parameters and variables.
7322 * That is, check if dimension dim is involved in an equality
7323 * for each of the subsets.
7325 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7331 for (i
= 0; i
< set
->n
; ++i
) {
7333 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7340 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7342 return set
? set
->n
: 0;
7345 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7346 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7353 for (i
= 0; i
< map
->n
; ++i
)
7354 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7360 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7361 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7368 for (i
= 0; i
< set
->n
; ++i
)
7369 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7375 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7377 struct isl_dim
*dim
;
7382 if (bset
->n_div
== 0)
7385 bset
= isl_basic_set_cow(bset
);
7389 dim
= isl_basic_set_get_dim(bset
);
7390 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
7393 isl_dim_free(bset
->dim
);
7395 bset
->extra
-= bset
->n_div
;
7398 bset
= isl_basic_set_finalize(bset
);
7402 isl_basic_set_free(bset
);
7406 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7409 struct isl_dim
*dim
;
7412 set
= isl_set_align_divs(set
);
7416 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
7419 set
= isl_set_cow(set
);
7423 n_div
= set
->p
[0]->n_div
;
7424 dim
= isl_set_get_dim(set
);
7425 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
7428 isl_dim_free(set
->dim
);
7431 for (i
= 0; i
< set
->n
; ++i
) {
7432 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7443 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7445 struct isl_dim
*dim
;
7446 struct isl_basic_map
*bmap
;
7453 set
= isl_set_align_divs(set
);
7458 dim
= isl_set_get_dim(set
);
7459 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7461 return isl_map_identity(dim
);
7464 n_div
= set
->p
[0]->n_div
;
7465 dim
= isl_dim_map(dim
);
7466 n_param
= isl_dim_size(dim
, isl_dim_param
);
7467 n_set
= isl_dim_size(dim
, isl_dim_in
);
7468 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7469 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7470 for (i
= 0; i
< n_set
; ++i
)
7471 bmap
= var_equal(bmap
, i
);
7473 total
= n_param
+ n_set
+ n_set
+ n_div
;
7474 for (i
= 0; i
< n_div
; ++i
) {
7475 k
= isl_basic_map_alloc_inequality(bmap
);
7478 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7479 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7480 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7481 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7482 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7483 set
->p
[0]->div
[i
][0]);
7485 l
= isl_basic_map_alloc_inequality(bmap
);
7488 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7489 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7490 set
->p
[0]->div
[i
][0]);
7491 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7495 return isl_map_from_basic_map(bmap
);
7498 isl_basic_map_free(bmap
);
7502 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7510 dim
= isl_basic_set_total_dim(bset
);
7511 size
+= bset
->n_eq
* (1 + dim
);
7512 size
+= bset
->n_ineq
* (1 + dim
);
7513 size
+= bset
->n_div
* (2 + dim
);
7518 int isl_set_size(__isl_keep isl_set
*set
)
7526 for (i
= 0; i
< set
->n
; ++i
)
7527 size
+= isl_basic_set_size(set
->p
[i
]);
7532 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7533 enum isl_dim_type type
, unsigned pos
)
7541 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7543 pos
+= isl_basic_map_offset(bmap
, type
);
7545 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7546 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7550 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7551 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7558 return lower
&& upper
;
7561 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7562 enum isl_dim_type type
, unsigned pos
)
7569 for (i
= 0; i
< map
->n
; ++i
) {
7571 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7572 if (bounded
< 0 || !bounded
)
7579 /* Return 1 if the specified dim is involved in both an upper bound
7580 * and a lower bound.
7582 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7583 enum isl_dim_type type
, unsigned pos
)
7585 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7588 /* For each of the "n" variables starting at "first", determine
7589 * the sign of the variable and put the results in the first "n"
7590 * elements of the array "signs".
7592 * 1 means that the variable is non-negative
7593 * -1 means that the variable is non-positive
7594 * 0 means the variable attains both positive and negative values.
7596 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7597 unsigned first
, unsigned n
, int *signs
)
7599 isl_vec
*bound
= NULL
;
7600 struct isl_tab
*tab
= NULL
;
7601 struct isl_tab_undo
*snap
;
7604 if (!bset
|| !signs
)
7607 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7608 tab
= isl_tab_from_basic_set(bset
);
7612 isl_seq_clr(bound
->el
, bound
->size
);
7613 isl_int_set_si(bound
->el
[0], -1);
7615 snap
= isl_tab_snap(tab
);
7616 for (i
= 0; i
< n
; ++i
) {
7619 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7620 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7623 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7624 if (isl_tab_rollback(tab
, snap
) < 0)
7632 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7633 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7636 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7637 if (isl_tab_rollback(tab
, snap
) < 0)
7640 signs
[i
] = empty
? -1 : 0;
7644 isl_vec_free(bound
);
7648 isl_vec_free(bound
);
7652 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7653 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7655 if (!bset
|| !signs
)
7657 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7660 first
+= pos(bset
->dim
, type
) - 1;
7661 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7664 /* Check if the given basic map is obviously single-valued.
7665 * In particular, for each output dimension, check that there is
7666 * an equality that defines the output dimension in terms of
7667 * earlier dimensions.
7669 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7679 total
= 1 + isl_basic_map_total_dim(bmap
);
7680 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7681 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7683 for (i
= 0; i
< n_out
; ++i
) {
7684 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7685 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7687 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7688 total
- (o_out
+ i
+ 1)) == -1)
7691 if (j
>= bmap
->n_eq
)
7698 /* Check if the given map is obviously single-valued.
7700 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7711 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7714 /* Check if the given map is single-valued.
7719 * and check if the result is a subset of the identity mapping.
7721 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7727 sv
= isl_map_fast_is_single_valued(map
);
7731 test
= isl_map_reverse(isl_map_copy(map
));
7732 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7734 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
7736 sv
= isl_map_is_subset(test
, id
);
7744 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7748 sv
= isl_map_is_single_valued(map
);
7752 map
= isl_map_copy(map
);
7753 map
= isl_map_reverse(map
);
7754 sv
= isl_map_is_single_valued(map
);
7760 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7762 return isl_map_is_single_valued((isl_map
*)set
);
7765 int isl_map_is_translation(__isl_keep isl_map
*map
)
7770 delta
= isl_map_deltas(isl_map_copy(map
));
7771 ok
= isl_set_is_singleton(delta
);
7772 isl_set_free(delta
);
7777 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7779 if (isl_seq_first_non_zero(p
, pos
) != -1)
7781 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7786 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7795 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7798 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7799 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7800 for (j
= 0; j
< nvar
; ++j
) {
7801 int lower
= 0, upper
= 0;
7802 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7803 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7805 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7811 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7812 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7814 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7816 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7821 if (!lower
|| !upper
)
7828 int isl_set_is_box(__isl_keep isl_set
*set
)
7835 return isl_basic_set_is_box(set
->p
[0]);
7838 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7843 return isl_dim_is_wrapping(bset
->dim
);
7846 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7851 return isl_dim_is_wrapping(set
->dim
);
7854 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7856 bmap
= isl_basic_map_cow(bmap
);
7860 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7864 bmap
= isl_basic_map_finalize(bmap
);
7866 return (isl_basic_set
*)bmap
;
7868 isl_basic_map_free(bmap
);
7872 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7876 map
= isl_map_cow(map
);
7880 for (i
= 0; i
< map
->n
; ++i
) {
7881 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7885 map
->dim
= isl_dim_wrap(map
->dim
);
7889 return (isl_set
*)map
;
7895 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7897 bset
= isl_basic_set_cow(bset
);
7901 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7905 bset
= isl_basic_set_finalize(bset
);
7907 return (isl_basic_map
*)bset
;
7909 isl_basic_set_free(bset
);
7913 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7920 if (!isl_set_is_wrapping(set
))
7921 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7924 set
= isl_set_cow(set
);
7928 for (i
= 0; i
< set
->n
; ++i
) {
7929 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7934 set
->dim
= isl_dim_unwrap(set
->dim
);
7938 return (isl_map
*)set
;
7944 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7945 enum isl_dim_type type
)
7950 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7953 bmap
= isl_basic_map_cow(bmap
);
7957 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7961 bmap
= isl_basic_map_finalize(bmap
);
7965 isl_basic_map_free(bmap
);
7969 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7970 enum isl_dim_type type
)
7977 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7980 map
= isl_map_cow(map
);
7984 for (i
= 0; i
< map
->n
; ++i
) {
7985 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7989 map
->dim
= isl_dim_reset(map
->dim
, type
);
7999 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8004 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8007 bmap
= isl_basic_map_cow(bmap
);
8011 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8015 bmap
= isl_basic_map_finalize(bmap
);
8019 isl_basic_map_free(bmap
);
8023 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8025 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8028 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8035 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8038 map
= isl_map_cow(map
);
8042 for (i
= 0; i
< map
->n
; ++i
) {
8043 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8047 map
->dim
= isl_dim_flatten(map
->dim
);
8057 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8059 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8062 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8064 isl_dim
*dim
, *flat_dim
;
8067 dim
= isl_set_get_dim(set
);
8068 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8069 map
= map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8070 map
= isl_map_intersect_domain(map
, set
);
8075 /* Extend the given dim_map with mappings for the divs in bmap.
8077 static __isl_give
struct isl_dim_map
*extend_dim_map(
8078 __isl_keep
struct isl_dim_map
*dim_map
,
8079 __isl_keep isl_basic_map
*bmap
)
8082 struct isl_dim_map
*res
;
8085 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
8087 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
8091 for (i
= 0; i
< dim_map
->len
; ++i
)
8092 res
->pos
[i
] = dim_map
->pos
[i
];
8093 for (i
= 0; i
< bmap
->n_div
; ++i
)
8094 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
8099 /* Extract a dim_map from a reordering.
8100 * We essentially need to reverse the mapping, and add an offset
8101 * of 1 for the constant term.
8103 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
8104 __isl_keep isl_reordering
*exp
)
8107 struct isl_dim_map
*dim_map
;
8112 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
8116 for (i
= 0; i
< exp
->len
; ++i
)
8117 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
8122 /* Reorder the dimensions of "bmap" according to the given dim_map
8123 * and set the dimension specification to "dim".
8125 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8126 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8130 bmap
= isl_basic_map_cow(bmap
);
8131 if (!bmap
|| !dim
|| !dim_map
)
8134 res
= isl_basic_map_alloc_dim(dim
,
8135 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8136 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
8137 res
= isl_basic_map_finalize(res
);
8141 isl_basic_map_free(bmap
);
8146 /* Reorder the dimensions of "map" according to given reordering.
8148 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8149 __isl_take isl_reordering
*r
)
8152 struct isl_dim_map
*dim_map
;
8154 map
= isl_map_cow(map
);
8155 dim_map
= isl_dim_map_from_reordering(r
);
8156 if (!map
|| !r
|| !dim_map
)
8159 for (i
= 0; i
< map
->n
; ++i
) {
8160 struct isl_dim_map
*dim_map_i
;
8162 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
8164 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8165 isl_dim_copy(r
->dim
), dim_map_i
);
8171 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8173 isl_reordering_free(r
);
8179 isl_reordering_free(r
);
8183 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8184 __isl_take isl_reordering
*r
)
8186 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8189 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8190 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8191 enum isl_dim_type c2
, enum isl_dim_type c3
,
8192 enum isl_dim_type c4
, enum isl_dim_type c5
)
8194 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8195 struct isl_mat
*mat
;
8201 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8202 isl_basic_map_total_dim(bmap
) + 1);
8205 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8206 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8207 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8208 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8209 isl_int_set(mat
->row
[i
][pos
],
8210 bmap
->eq
[i
][off
+ k
]);
8218 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8219 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8220 enum isl_dim_type c2
, enum isl_dim_type c3
,
8221 enum isl_dim_type c4
, enum isl_dim_type c5
)
8223 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8224 struct isl_mat
*mat
;
8230 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8231 isl_basic_map_total_dim(bmap
) + 1);
8234 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8235 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8236 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8237 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8238 isl_int_set(mat
->row
[i
][pos
],
8239 bmap
->ineq
[i
][off
+ k
]);
8247 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8248 __isl_take isl_dim
*dim
,
8249 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8250 enum isl_dim_type c2
, enum isl_dim_type c3
,
8251 enum isl_dim_type c4
, enum isl_dim_type c5
)
8253 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8254 isl_basic_map
*bmap
;
8260 if (!dim
|| !eq
|| !ineq
)
8263 if (eq
->n_col
!= ineq
->n_col
)
8264 isl_die(dim
->ctx
, isl_error_invalid
,
8265 "equalities and inequalities matrices should have "
8266 "same number of columns", goto error
);
8268 total
= 1 + isl_dim_total(dim
);
8270 if (eq
->n_col
< total
)
8271 isl_die(dim
->ctx
, isl_error_invalid
,
8272 "number of columns too small", goto error
);
8274 extra
= eq
->n_col
- total
;
8276 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8277 eq
->n_row
, ineq
->n_row
);
8280 for (i
= 0; i
< extra
; ++i
) {
8281 k
= isl_basic_map_alloc_div(bmap
);
8284 isl_int_set_si(bmap
->div
[k
][0], 0);
8286 for (i
= 0; i
< eq
->n_row
; ++i
) {
8287 l
= isl_basic_map_alloc_equality(bmap
);
8290 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8291 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8292 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8293 isl_int_set(bmap
->eq
[l
][off
+ k
],
8299 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8300 l
= isl_basic_map_alloc_inequality(bmap
);
8303 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8304 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8305 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8306 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8325 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8326 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8327 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8329 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8330 c1
, c2
, c3
, c4
, isl_dim_in
);
8333 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8334 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8335 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8337 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8338 c1
, c2
, c3
, c4
, isl_dim_in
);
8341 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8342 __isl_take isl_dim
*dim
,
8343 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8344 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8346 return (isl_basic_set
*)
8347 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8348 c1
, c2
, c3
, c4
, isl_dim_in
);