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 /* Given a map A -> f(A), construct A -> -f(A).
2997 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3001 map
= isl_map_cow(map
);
3005 for (i
= 0; i
< map
->n
; ++i
) {
3006 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3017 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3019 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3022 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3023 * A -> floor(f(A)/d).
3025 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3028 unsigned n_in
, n_out
, nparam
, total
, pos
;
3029 struct isl_basic_map
*result
= NULL
;
3030 struct isl_dim_map
*dim_map
;
3036 nparam
= isl_basic_map_n_param(bmap
);
3037 n_in
= isl_basic_map_n_in(bmap
);
3038 n_out
= isl_basic_map_n_out(bmap
);
3040 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3041 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3042 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3043 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3044 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3045 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3047 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3048 bmap
->n_div
+ n_out
,
3049 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3050 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
3051 result
= add_divs(result
, n_out
);
3052 for (i
= 0; i
< n_out
; ++i
) {
3054 j
= isl_basic_map_alloc_inequality(result
);
3057 isl_seq_clr(result
->ineq
[j
], 1+total
);
3058 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3059 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3060 j
= isl_basic_map_alloc_inequality(result
);
3063 isl_seq_clr(result
->ineq
[j
], 1+total
);
3064 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3065 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3066 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3069 result
= isl_basic_map_simplify(result
);
3070 return isl_basic_map_finalize(result
);
3072 isl_basic_map_free(result
);
3076 /* Given a map A -> f(A) and an integer d, construct a map
3077 * A -> floor(f(A)/d).
3079 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3083 map
= isl_map_cow(map
);
3087 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3088 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3089 for (i
= 0; i
< map
->n
; ++i
) {
3090 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3101 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3107 i
= isl_basic_map_alloc_equality(bmap
);
3110 nparam
= isl_basic_map_n_param(bmap
);
3111 n_in
= isl_basic_map_n_in(bmap
);
3112 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3113 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3114 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3115 return isl_basic_map_finalize(bmap
);
3117 isl_basic_map_free(bmap
);
3121 /* Add a constraints to "bmap" expressing i_pos < o_pos
3123 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3129 i
= isl_basic_map_alloc_inequality(bmap
);
3132 nparam
= isl_basic_map_n_param(bmap
);
3133 n_in
= isl_basic_map_n_in(bmap
);
3134 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3135 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3136 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3137 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3138 return isl_basic_map_finalize(bmap
);
3140 isl_basic_map_free(bmap
);
3144 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3146 static __isl_give isl_basic_map
*var_less_or_equal(
3147 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3153 i
= isl_basic_map_alloc_inequality(bmap
);
3156 nparam
= isl_basic_map_n_param(bmap
);
3157 n_in
= isl_basic_map_n_in(bmap
);
3158 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3159 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3160 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3161 return isl_basic_map_finalize(bmap
);
3163 isl_basic_map_free(bmap
);
3167 /* Add a constraints to "bmap" expressing i_pos > o_pos
3169 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3175 i
= isl_basic_map_alloc_inequality(bmap
);
3178 nparam
= isl_basic_map_n_param(bmap
);
3179 n_in
= isl_basic_map_n_in(bmap
);
3180 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3181 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3182 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3183 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3184 return isl_basic_map_finalize(bmap
);
3186 isl_basic_map_free(bmap
);
3190 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3192 static __isl_give isl_basic_map
*var_more_or_equal(
3193 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3199 i
= isl_basic_map_alloc_inequality(bmap
);
3202 nparam
= isl_basic_map_n_param(bmap
);
3203 n_in
= isl_basic_map_n_in(bmap
);
3204 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3205 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3206 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3207 return isl_basic_map_finalize(bmap
);
3209 isl_basic_map_free(bmap
);
3213 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3216 struct isl_basic_map
*bmap
;
3217 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3220 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3221 bmap
= var_equal(bmap
, i
);
3222 return isl_basic_map_finalize(bmap
);
3225 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3227 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3230 struct isl_basic_map
*bmap
;
3231 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3234 for (i
= 0; i
< pos
&& bmap
; ++i
)
3235 bmap
= var_equal(bmap
, i
);
3237 bmap
= var_less(bmap
, pos
);
3238 return isl_basic_map_finalize(bmap
);
3241 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3243 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3244 __isl_take isl_dim
*dim
, unsigned pos
)
3247 isl_basic_map
*bmap
;
3249 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3250 for (i
= 0; i
< pos
; ++i
)
3251 bmap
= var_equal(bmap
, i
);
3252 bmap
= var_less_or_equal(bmap
, pos
);
3253 return isl_basic_map_finalize(bmap
);
3256 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3258 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3261 struct isl_basic_map
*bmap
;
3262 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3265 for (i
= 0; i
< pos
&& bmap
; ++i
)
3266 bmap
= var_equal(bmap
, i
);
3268 bmap
= var_more(bmap
, pos
);
3269 return isl_basic_map_finalize(bmap
);
3272 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3274 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3275 __isl_take isl_dim
*dim
, unsigned pos
)
3278 isl_basic_map
*bmap
;
3280 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3281 for (i
= 0; i
< pos
; ++i
)
3282 bmap
= var_equal(bmap
, i
);
3283 bmap
= var_more_or_equal(bmap
, pos
);
3284 return isl_basic_map_finalize(bmap
);
3287 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3288 unsigned n
, int equal
)
3290 struct isl_map
*map
;
3293 if (n
== 0 && equal
)
3294 return isl_map_universe(dims
);
3296 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3298 for (i
= 0; i
+ 1 < n
; ++i
)
3299 map
= isl_map_add_basic_map(map
,
3300 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3303 map
= isl_map_add_basic_map(map
,
3304 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3306 map
= isl_map_add_basic_map(map
,
3307 isl_basic_map_less_at(dims
, n
- 1));
3314 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3318 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3321 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3323 return map_lex_lte_first(dim
, n
, 0);
3326 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3328 return map_lex_lte_first(dim
, n
, 1);
3331 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3333 return map_lex_lte(isl_dim_map(set_dim
), 0);
3336 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3338 return map_lex_lte(isl_dim_map(set_dim
), 1);
3341 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3342 unsigned n
, int equal
)
3344 struct isl_map
*map
;
3347 if (n
== 0 && equal
)
3348 return isl_map_universe(dims
);
3350 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3352 for (i
= 0; i
+ 1 < n
; ++i
)
3353 map
= isl_map_add_basic_map(map
,
3354 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3357 map
= isl_map_add_basic_map(map
,
3358 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3360 map
= isl_map_add_basic_map(map
,
3361 isl_basic_map_more_at(dims
, n
- 1));
3368 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3372 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3375 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3377 return map_lex_gte_first(dim
, n
, 0);
3380 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3382 return map_lex_gte_first(dim
, n
, 1);
3385 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3387 return map_lex_gte(isl_dim_map(set_dim
), 0);
3390 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3392 return map_lex_gte(isl_dim_map(set_dim
), 1);
3395 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3396 __isl_take isl_set
*set2
)
3399 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3400 map
= isl_map_intersect_domain(map
, set1
);
3401 map
= isl_map_intersect_range(map
, set2
);
3405 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3406 __isl_take isl_set
*set2
)
3409 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3410 map
= isl_map_intersect_domain(map
, set1
);
3411 map
= isl_map_intersect_range(map
, set2
);
3415 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3416 __isl_take isl_set
*set2
)
3419 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3420 map
= isl_map_intersect_domain(map
, set1
);
3421 map
= isl_map_intersect_range(map
, set2
);
3425 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3426 __isl_take isl_set
*set2
)
3429 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3430 map
= isl_map_intersect_domain(map
, set1
);
3431 map
= isl_map_intersect_range(map
, set2
);
3435 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3436 __isl_take isl_map
*map2
)
3439 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3440 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3441 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3445 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3446 __isl_take isl_map
*map2
)
3449 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3450 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3451 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3455 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3456 __isl_take isl_map
*map2
)
3459 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3460 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3461 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3465 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3466 __isl_take isl_map
*map2
)
3469 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3470 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3471 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3475 struct isl_basic_map
*isl_basic_map_from_basic_set(
3476 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3478 struct isl_basic_map
*bmap
;
3480 bset
= isl_basic_set_cow(bset
);
3484 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3485 isl_dim_free(bset
->dim
);
3486 bmap
= (struct isl_basic_map
*) bset
;
3488 return isl_basic_map_finalize(bmap
);
3490 isl_basic_set_free(bset
);
3495 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3499 if (bmap
->dim
->n_in
== 0)
3500 return (struct isl_basic_set
*)bmap
;
3501 bmap
= isl_basic_map_cow(bmap
);
3504 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3507 bmap
= isl_basic_map_finalize(bmap
);
3508 return (struct isl_basic_set
*)bmap
;
3510 isl_basic_map_free(bmap
);
3514 /* For a div d = floor(f/m), add the constraints
3517 * -(f-(n-1)) + m d >= 0
3519 * Note that the second constraint is the negation of
3523 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3524 unsigned pos
, isl_int
*div
)
3527 unsigned total
= isl_basic_map_total_dim(bmap
);
3529 i
= isl_basic_map_alloc_inequality(bmap
);
3532 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3533 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3535 j
= isl_basic_map_alloc_inequality(bmap
);
3538 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3539 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3540 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3544 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3545 unsigned pos
, isl_int
*div
)
3547 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3551 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3553 unsigned total
= isl_basic_map_total_dim(bmap
);
3554 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3556 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3560 struct isl_basic_set
*isl_basic_map_underlying_set(
3561 struct isl_basic_map
*bmap
)
3565 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3567 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3568 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3569 return (struct isl_basic_set
*)bmap
;
3570 bmap
= isl_basic_map_cow(bmap
);
3573 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3576 bmap
->extra
-= bmap
->n_div
;
3578 bmap
= isl_basic_map_finalize(bmap
);
3579 return (struct isl_basic_set
*)bmap
;
3584 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3585 __isl_take isl_basic_set
*bset
)
3587 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3590 struct isl_basic_map
*isl_basic_map_overlying_set(
3591 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3593 struct isl_basic_map
*bmap
;
3594 struct isl_ctx
*ctx
;
3601 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3602 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3603 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3605 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3606 isl_basic_map_free(like
);
3607 return (struct isl_basic_map
*)bset
;
3609 bset
= isl_basic_set_cow(bset
);
3612 total
= bset
->dim
->n_out
+ bset
->extra
;
3613 bmap
= (struct isl_basic_map
*)bset
;
3614 isl_dim_free(bmap
->dim
);
3615 bmap
->dim
= isl_dim_copy(like
->dim
);
3618 bmap
->n_div
= like
->n_div
;
3619 bmap
->extra
+= like
->n_div
;
3622 ltotal
= total
- bmap
->extra
+ like
->extra
;
3625 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3626 bmap
->extra
* (1 + 1 + total
));
3627 if (isl_blk_is_error(bmap
->block2
))
3629 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3633 for (i
= 0; i
< bmap
->extra
; ++i
)
3634 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3635 for (i
= 0; i
< like
->n_div
; ++i
) {
3636 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3637 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3639 bmap
= isl_basic_map_extend_constraints(bmap
,
3640 0, 2 * like
->n_div
);
3641 for (i
= 0; i
< like
->n_div
; ++i
) {
3642 if (isl_int_is_zero(bmap
->div
[i
][0]))
3644 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3648 isl_basic_map_free(like
);
3649 bmap
= isl_basic_map_simplify(bmap
);
3650 bmap
= isl_basic_map_finalize(bmap
);
3653 isl_basic_map_free(like
);
3654 isl_basic_set_free(bset
);
3658 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3659 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3661 return (struct isl_basic_set
*)
3662 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3665 struct isl_set
*isl_set_from_underlying_set(
3666 struct isl_set
*set
, struct isl_basic_set
*like
)
3672 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3674 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3675 isl_basic_set_free(like
);
3678 set
= isl_set_cow(set
);
3681 for (i
= 0; i
< set
->n
; ++i
) {
3682 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3683 isl_basic_set_copy(like
));
3687 isl_dim_free(set
->dim
);
3688 set
->dim
= isl_dim_copy(like
->dim
);
3691 isl_basic_set_free(like
);
3694 isl_basic_set_free(like
);
3699 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3703 map
= isl_map_cow(map
);
3706 map
->dim
= isl_dim_cow(map
->dim
);
3710 for (i
= 1; i
< map
->n
; ++i
)
3711 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3713 for (i
= 0; i
< map
->n
; ++i
) {
3714 map
->p
[i
] = (struct isl_basic_map
*)
3715 isl_basic_map_underlying_set(map
->p
[i
]);
3720 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3722 isl_dim_free(map
->dim
);
3723 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3727 return (struct isl_set
*)map
;
3733 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3735 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3738 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3739 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3741 bmap
= isl_basic_map_cow(bmap
);
3745 isl_dim_free(bmap
->dim
);
3748 bmap
= isl_basic_map_finalize(bmap
);
3752 isl_basic_map_free(bmap
);
3757 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3758 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3760 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3764 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3765 __isl_take isl_dim
*dim
)
3769 map
= isl_map_cow(map
);
3773 for (i
= 0; i
< map
->n
; ++i
) {
3774 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3779 isl_dim_free(map
->dim
);
3789 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3790 __isl_take isl_dim
*dim
)
3792 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3795 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3798 struct isl_basic_set
*domain
;
3804 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3806 n_in
= isl_basic_map_n_in(bmap
);
3807 n_out
= isl_basic_map_n_out(bmap
);
3808 domain
= isl_basic_set_from_basic_map(bmap
);
3809 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3811 domain
= isl_basic_set_reset_dim(domain
, dim
);
3816 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3818 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3821 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3822 __isl_take isl_basic_map
*bmap
)
3826 isl_basic_map
*domain
;
3827 isl_basic_set
*bset
;
3828 int nparam
, n_in
, n_out
;
3831 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3832 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3833 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3835 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3836 domain
= isl_basic_map_universe(dim
);
3838 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3839 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3840 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3842 total
= isl_basic_map_total_dim(bmap
);
3844 for (i
= 0; i
< n_in
; ++i
) {
3845 k
= isl_basic_map_alloc_equality(bmap
);
3848 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3849 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3850 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3853 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3854 return isl_basic_map_finalize(bmap
);
3856 isl_basic_map_free(bmap
);
3860 __isl_give isl_basic_map
*isl_basic_map_range_map(
3861 __isl_take isl_basic_map
*bmap
)
3865 isl_basic_map
*range
;
3866 isl_basic_set
*bset
;
3867 int nparam
, n_in
, n_out
;
3870 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3871 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3872 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3874 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3875 range
= isl_basic_map_universe(dim
);
3877 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3878 bmap
= isl_basic_map_apply_range(bmap
, range
);
3879 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3881 total
= isl_basic_map_total_dim(bmap
);
3883 for (i
= 0; i
< n_out
; ++i
) {
3884 k
= isl_basic_map_alloc_equality(bmap
);
3887 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3888 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3889 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3892 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3893 return isl_basic_map_finalize(bmap
);
3895 isl_basic_map_free(bmap
);
3899 struct isl_set
*isl_map_range(struct isl_map
*map
)
3902 struct isl_set
*set
;
3906 if (isl_map_dim(map
, isl_dim_in
) == 0)
3907 return (isl_set
*)map
;
3909 map
= isl_map_cow(map
);
3913 set
= (struct isl_set
*) map
;
3914 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3917 for (i
= 0; i
< map
->n
; ++i
) {
3918 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3922 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3923 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3930 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3933 isl_dim
*domain_dim
;
3935 map
= isl_map_cow(map
);
3939 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3940 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3941 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3944 for (i
= 0; i
< map
->n
; ++i
) {
3945 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3949 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3950 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3957 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3962 map
= isl_map_cow(map
);
3966 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3967 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3968 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3971 for (i
= 0; i
< map
->n
; ++i
) {
3972 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
3976 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3977 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3984 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3987 struct isl_map
*map
= NULL
;
3989 set
= isl_set_cow(set
);
3992 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3993 map
= (struct isl_map
*)set
;
3994 for (i
= 0; i
< set
->n
; ++i
) {
3995 map
->p
[i
] = isl_basic_map_from_basic_set(
3996 set
->p
[i
], isl_dim_copy(dim
));
4000 isl_dim_free(map
->dim
);
4009 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4010 __isl_take isl_basic_set
*bset
)
4012 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4015 __isl_give isl_basic_map
*isl_basic_map_from_range(
4016 __isl_take isl_basic_set
*bset
)
4018 return (isl_basic_map
*)bset
;
4021 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4023 return (struct isl_map
*)set
;
4026 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4028 return isl_map_reverse(isl_map_from_range(set
));
4031 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4032 __isl_take isl_set
*range
)
4034 return isl_map_apply_range(isl_map_from_domain(domain
),
4035 isl_map_from_range(range
));
4038 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4041 struct isl_set
*set
= NULL
;
4045 map
= isl_map_cow(map
);
4048 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4051 set
= (struct isl_set
*)map
;
4052 for (i
= 0; i
< map
->n
; ++i
) {
4053 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4063 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4065 struct isl_map
*map
;
4069 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4070 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4071 sizeof(struct isl_map
) +
4072 (n
- 1) * sizeof(struct isl_basic_map
*));
4076 map
->ctx
= dim
->ctx
;
4077 isl_ctx_ref(map
->ctx
);
4089 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4090 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4093 struct isl_map
*map
;
4094 struct isl_dim
*dims
;
4096 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4100 map
= isl_map_alloc_dim(dims
, n
, flags
);
4104 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4106 struct isl_basic_map
*bmap
;
4107 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4108 bmap
= isl_basic_map_set_to_empty(bmap
);
4112 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4114 struct isl_basic_set
*bset
;
4115 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4116 bset
= isl_basic_set_set_to_empty(bset
);
4120 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4122 struct isl_basic_map
*bmap
;
4125 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4126 bmap
= isl_basic_map_set_to_empty(bmap
);
4130 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4132 struct isl_basic_map
*bmap
;
4135 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4136 bmap
= isl_basic_map_set_to_empty(bmap
);
4140 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4142 struct isl_basic_set
*bset
;
4145 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4146 bset
= isl_basic_set_set_to_empty(bset
);
4150 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4152 struct isl_basic_map
*bmap
;
4153 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4154 bmap
= isl_basic_map_finalize(bmap
);
4158 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4160 struct isl_basic_set
*bset
;
4161 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4162 bset
= isl_basic_set_finalize(bset
);
4166 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4169 unsigned total
= isl_dim_total(dim
);
4170 isl_basic_map
*bmap
;
4172 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4173 for (i
= 0; i
< total
; ++i
) {
4174 int k
= isl_basic_map_alloc_inequality(bmap
);
4177 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4178 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4182 isl_basic_map_free(bmap
);
4186 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4188 return isl_basic_map_nat_universe(dim
);
4191 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4193 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4196 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4198 return isl_map_nat_universe(dim
);
4201 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4202 __isl_keep isl_basic_map
*model
)
4206 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4209 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4213 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4216 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4217 __isl_keep isl_set
*model
)
4221 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4224 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4226 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4229 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4233 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4236 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4240 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4243 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4245 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4248 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4252 return isl_set_empty(isl_dim_copy(model
->dim
));
4255 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4257 struct isl_map
*map
;
4260 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4261 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4265 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4267 struct isl_set
*set
;
4270 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4271 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4275 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4279 return isl_set_universe(isl_dim_copy(model
->dim
));
4282 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4285 struct isl_map
*dup
;
4289 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4290 for (i
= 0; i
< map
->n
; ++i
)
4291 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4295 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4296 __isl_take isl_basic_map
*bmap
)
4300 if (isl_basic_map_fast_is_empty(bmap
)) {
4301 isl_basic_map_free(bmap
);
4304 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4305 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4306 map
->p
[map
->n
] = bmap
;
4308 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4314 isl_basic_map_free(bmap
);
4318 void isl_map_free(struct isl_map
*map
)
4328 isl_ctx_deref(map
->ctx
);
4329 for (i
= 0; i
< map
->n
; ++i
)
4330 isl_basic_map_free(map
->p
[i
]);
4331 isl_dim_free(map
->dim
);
4335 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4336 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4340 base
= isl_map_cow(base
);
4344 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4347 for (i
= 0; i
< base
->n
; ++i
) {
4348 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4349 isl_dim_copy(base
->dim
), 0, 0, 0);
4359 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4360 unsigned nparam
, unsigned dim
)
4362 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4366 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4367 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4371 bmap
= isl_basic_map_cow(bmap
);
4372 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4373 j
= isl_basic_map_alloc_equality(bmap
);
4376 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4377 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4378 isl_int_set_si(bmap
->eq
[j
][0], value
);
4379 bmap
= isl_basic_map_simplify(bmap
);
4380 return isl_basic_map_finalize(bmap
);
4382 isl_basic_map_free(bmap
);
4386 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4387 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4391 bmap
= isl_basic_map_cow(bmap
);
4392 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4393 j
= isl_basic_map_alloc_equality(bmap
);
4396 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4397 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4398 isl_int_set(bmap
->eq
[j
][0], value
);
4399 bmap
= isl_basic_map_simplify(bmap
);
4400 return isl_basic_map_finalize(bmap
);
4402 isl_basic_map_free(bmap
);
4406 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4407 enum isl_dim_type type
, unsigned pos
, int value
)
4411 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4412 return isl_basic_map_fix_pos_si(bmap
,
4413 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4415 isl_basic_map_free(bmap
);
4419 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4420 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4424 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4425 return isl_basic_map_fix_pos(bmap
,
4426 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4428 isl_basic_map_free(bmap
);
4432 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4433 enum isl_dim_type type
, unsigned pos
, int value
)
4435 return (struct isl_basic_set
*)
4436 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4440 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4441 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4443 return (struct isl_basic_set
*)
4444 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4448 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4449 unsigned input
, int value
)
4451 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4454 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4455 unsigned dim
, int value
)
4457 return (struct isl_basic_set
*)
4458 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4459 isl_dim_set
, dim
, value
);
4462 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4463 enum isl_dim_type type
, unsigned pos
, int value
)
4467 map
= isl_map_cow(map
);
4471 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4472 for (i
= 0; i
< map
->n
; ++i
) {
4473 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4477 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4484 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4485 enum isl_dim_type type
, unsigned pos
, int value
)
4487 return (struct isl_set
*)
4488 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4491 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4492 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4496 map
= isl_map_cow(map
);
4500 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4501 for (i
= 0; i
< map
->n
; ++i
) {
4502 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4506 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4513 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4514 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4516 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4519 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4520 unsigned input
, int value
)
4522 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4525 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4527 return (struct isl_set
*)
4528 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4531 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4532 __isl_take isl_basic_map
*bmap
,
4533 enum isl_dim_type type
, unsigned pos
, int value
)
4539 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4540 pos
+= isl_basic_map_offset(bmap
, type
);
4541 bmap
= isl_basic_map_cow(bmap
);
4542 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4543 j
= isl_basic_map_alloc_inequality(bmap
);
4546 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4547 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4548 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4549 bmap
= isl_basic_map_simplify(bmap
);
4550 return isl_basic_map_finalize(bmap
);
4552 isl_basic_map_free(bmap
);
4556 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4557 unsigned dim
, isl_int value
)
4561 bset
= isl_basic_set_cow(bset
);
4562 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4563 j
= isl_basic_set_alloc_inequality(bset
);
4566 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4567 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4568 isl_int_neg(bset
->ineq
[j
][0], value
);
4569 bset
= isl_basic_set_simplify(bset
);
4570 return isl_basic_set_finalize(bset
);
4572 isl_basic_set_free(bset
);
4576 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4577 enum isl_dim_type type
, unsigned pos
, int value
)
4581 map
= isl_map_cow(map
);
4585 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4586 for (i
= 0; i
< map
->n
; ++i
) {
4587 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4592 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4599 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4600 enum isl_dim_type type
, unsigned pos
, int value
)
4602 return (struct isl_set
*)
4603 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4606 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4611 set
= isl_set_cow(set
);
4615 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4616 for (i
= 0; i
< set
->n
; ++i
) {
4617 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4627 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4631 map
= isl_map_cow(map
);
4635 map
->dim
= isl_dim_reverse(map
->dim
);
4638 for (i
= 0; i
< map
->n
; ++i
) {
4639 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4643 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4650 static struct isl_map
*isl_basic_map_partial_lexopt(
4651 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4652 struct isl_set
**empty
, int max
)
4656 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4657 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4659 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4661 isl_basic_map_free(bmap
);
4662 isl_basic_set_free(dom
);
4668 struct isl_map
*isl_basic_map_partial_lexmax(
4669 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4670 struct isl_set
**empty
)
4672 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4675 struct isl_map
*isl_basic_map_partial_lexmin(
4676 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4677 struct isl_set
**empty
)
4679 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4682 struct isl_set
*isl_basic_set_partial_lexmin(
4683 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4684 struct isl_set
**empty
)
4686 return (struct isl_set
*)
4687 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4691 struct isl_set
*isl_basic_set_partial_lexmax(
4692 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4693 struct isl_set
**empty
)
4695 return (struct isl_set
*)
4696 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4700 /* Given a basic map "bmap", compute the lexicographically minimal
4701 * (or maximal) image element for each domain element in dom.
4702 * Set *empty to those elements in dom that do not have an image element.
4704 * We first make sure the basic sets in dom are disjoint and then
4705 * simply collect the results over each of the basic sets separately.
4706 * We could probably improve the efficiency a bit by moving the union
4707 * domain down into the parametric integer programming.
4709 static __isl_give isl_map
*basic_map_partial_lexopt(
4710 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4711 __isl_give isl_set
**empty
, int max
)
4714 struct isl_map
*res
;
4716 dom
= isl_set_make_disjoint(dom
);
4720 if (isl_set_fast_is_empty(dom
)) {
4721 res
= isl_map_empty_like_basic_map(bmap
);
4722 *empty
= isl_set_empty_like(dom
);
4724 isl_basic_map_free(bmap
);
4728 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4729 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4731 for (i
= 1; i
< dom
->n
; ++i
) {
4732 struct isl_map
*res_i
;
4733 struct isl_set
*empty_i
;
4735 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4736 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4738 res
= isl_map_union_disjoint(res
, res_i
);
4739 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4743 isl_basic_map_free(bmap
);
4748 isl_basic_map_free(bmap
);
4752 /* Given a map "map", compute the lexicographically minimal
4753 * (or maximal) image element for each domain element in dom.
4754 * Set *empty to those elements in dom that do not have an image element.
4756 * We first compute the lexicographically minimal or maximal element
4757 * in the first basic map. This results in a partial solution "res"
4758 * and a subset "todo" of dom that still need to be handled.
4759 * We then consider each of the remaining maps in "map" and successively
4760 * improve both "res" and "todo".
4762 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4763 * Assume we are computing the lexicographical maximum.
4764 * We first compute the lexicographically maximal element in basic map i.
4765 * This results in a partial solution res_i and a subset todo_i.
4766 * Then we combine these results with those obtain for the first k basic maps
4767 * to obtain a result that is valid for the first k+1 basic maps.
4768 * In particular, the set where there is no solution is the set where
4769 * there is no solution for the first k basic maps and also no solution
4770 * for the ith basic map, i.e.,
4772 * todo^i = todo^k * todo_i
4774 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4775 * solutions, arbitrarily breaking ties in favor of res^k.
4776 * That is, when res^k(a) >= res_i(a), we pick res^k and
4777 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4778 * the lexicographic order.)
4779 * In practice, we compute
4781 * res^k * (res_i . "<=")
4785 * res_i * (res^k . "<")
4787 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4788 * where only one of res^k and res_i provides a solution and we simply pick
4795 * Note that we only compute these intersections when dom(res^k) intersects
4796 * dom(res_i). Otherwise, the only effect of these intersections is to
4797 * potentially break up res^k and res_i into smaller pieces.
4798 * We want to avoid such splintering as much as possible.
4799 * In fact, an earlier implementation of this function would look for
4800 * better results in the domain of res^k and for extra results in todo^k,
4801 * but this would always result in a splintering according to todo^k,
4802 * even when the domain of basic map i is disjoint from the domains of
4803 * the previous basic maps.
4805 static __isl_give isl_map
*isl_map_partial_lexopt(
4806 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4807 __isl_give isl_set
**empty
, int max
)
4810 struct isl_map
*res
;
4811 struct isl_set
*todo
;
4816 if (isl_map_fast_is_empty(map
)) {
4824 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4825 isl_set_copy(dom
), &todo
, max
);
4827 for (i
= 1; i
< map
->n
; ++i
) {
4831 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4833 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4834 isl_set_copy(dom
), &todo_i
, max
);
4837 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4838 le
= isl_map_lex_le(dim
);
4840 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4841 le
= isl_map_lex_ge(dim
);
4843 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4844 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4845 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4846 le
= isl_map_intersect(le
, isl_map_copy(res
));
4848 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4849 res
= isl_map_intersect_domain(res
,
4850 isl_set_copy(todo_i
));
4851 res_i
= isl_map_intersect_domain(res_i
,
4852 isl_set_copy(todo
));
4855 res
= isl_map_union_disjoint(res
, res_i
);
4856 res
= isl_map_union_disjoint(res
, lt
);
4857 res
= isl_map_union_disjoint(res
, le
);
4859 todo
= isl_set_intersect(todo
, todo_i
);
4879 __isl_give isl_map
*isl_map_partial_lexmax(
4880 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4881 __isl_give isl_set
**empty
)
4883 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4886 __isl_give isl_map
*isl_map_partial_lexmin(
4887 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4888 __isl_give isl_set
**empty
)
4890 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4893 __isl_give isl_set
*isl_set_partial_lexmin(
4894 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4895 __isl_give isl_set
**empty
)
4897 return (struct isl_set
*)
4898 isl_map_partial_lexmin((struct isl_map
*)set
,
4902 __isl_give isl_set
*isl_set_partial_lexmax(
4903 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4904 __isl_give isl_set
**empty
)
4906 return (struct isl_set
*)
4907 isl_map_partial_lexmax((struct isl_map
*)set
,
4911 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4913 struct isl_basic_set
*dom
= NULL
;
4914 struct isl_dim
*dom_dim
;
4918 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4919 dom
= isl_basic_set_universe(dom_dim
);
4920 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4922 isl_basic_map_free(bmap
);
4926 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4928 return isl_basic_map_lexopt(bmap
, 0);
4931 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4933 return isl_basic_map_lexopt(bmap
, 1);
4936 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4938 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4941 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4943 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4946 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4948 struct isl_set
*dom
= NULL
;
4949 struct isl_dim
*dom_dim
;
4953 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4954 dom
= isl_set_universe(dom_dim
);
4955 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4961 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4963 return isl_map_lexopt(map
, 0);
4966 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4968 return isl_map_lexopt(map
, 1);
4971 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4973 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4976 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4978 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4981 /* Apply a preimage specified by "mat" on the parameters of "bset".
4982 * bset is assumed to have only parameters and divs.
4984 static struct isl_basic_set
*basic_set_parameter_preimage(
4985 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4992 bset
->dim
= isl_dim_cow(bset
->dim
);
4996 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4998 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5000 bset
->dim
->nparam
= 0;
5001 bset
->dim
->n_out
= nparam
;
5002 bset
= isl_basic_set_preimage(bset
, mat
);
5004 bset
->dim
->nparam
= bset
->dim
->n_out
;
5005 bset
->dim
->n_out
= 0;
5010 isl_basic_set_free(bset
);
5014 /* Apply a preimage specified by "mat" on the parameters of "set".
5015 * set is assumed to have only parameters and divs.
5017 static struct isl_set
*set_parameter_preimage(
5018 struct isl_set
*set
, struct isl_mat
*mat
)
5020 struct isl_dim
*dim
= NULL
;
5026 dim
= isl_dim_copy(set
->dim
);
5027 dim
= isl_dim_cow(dim
);
5031 nparam
= isl_set_dim(set
, isl_dim_param
);
5033 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5036 dim
->n_out
= nparam
;
5037 isl_set_reset_dim(set
, dim
);
5038 set
= isl_set_preimage(set
, mat
);
5041 dim
= isl_dim_copy(set
->dim
);
5042 dim
= isl_dim_cow(dim
);
5045 dim
->nparam
= dim
->n_out
;
5047 isl_set_reset_dim(set
, dim
);
5057 /* Intersect the basic set "bset" with the affine space specified by the
5058 * equalities in "eq".
5060 static struct isl_basic_set
*basic_set_append_equalities(
5061 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5069 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5074 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5075 for (i
= 0; i
< eq
->n_row
; ++i
) {
5076 k
= isl_basic_set_alloc_equality(bset
);
5079 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5080 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5084 bset
= isl_basic_set_gauss(bset
, NULL
);
5085 bset
= isl_basic_set_finalize(bset
);
5090 isl_basic_set_free(bset
);
5094 /* Intersect the set "set" with the affine space specified by the
5095 * equalities in "eq".
5097 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5105 for (i
= 0; i
< set
->n
; ++i
) {
5106 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5119 /* Project the given basic set onto its parameter domain, possibly introducing
5120 * new, explicit, existential variables in the constraints.
5121 * The input has parameters and (possibly implicit) existential variables.
5122 * The output has the same parameters, but only
5123 * explicit existentially quantified variables.
5125 * The actual projection is performed by pip, but pip doesn't seem
5126 * to like equalities very much, so we first remove the equalities
5127 * among the parameters by performing a variable compression on
5128 * the parameters. Afterward, an inverse transformation is performed
5129 * and the equalities among the parameters are inserted back in.
5131 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5135 struct isl_mat
*T
, *T2
;
5136 struct isl_set
*set
;
5137 unsigned nparam
, n_div
;
5139 bset
= isl_basic_set_cow(bset
);
5143 if (bset
->n_eq
== 0)
5144 return isl_basic_set_lexmin(bset
);
5146 isl_basic_set_gauss(bset
, NULL
);
5148 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5149 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5151 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5152 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5155 if (i
== bset
->n_eq
)
5156 return isl_basic_set_lexmin(bset
);
5158 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5160 eq
= isl_mat_cow(eq
);
5161 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5162 if (T
&& T
->n_col
== 0) {
5166 bset
= isl_basic_set_set_to_empty(bset
);
5167 return isl_set_from_basic_set(bset
);
5169 bset
= basic_set_parameter_preimage(bset
, T
);
5171 set
= isl_basic_set_lexmin(bset
);
5172 set
= set_parameter_preimage(set
, T2
);
5173 set
= set_append_equalities(set
, eq
);
5177 /* Compute an explicit representation for all the existentially
5178 * quantified variables.
5179 * The input and output dimensions are first turned into parameters.
5180 * compute_divs then returns a map with the same parameters and
5181 * no input or output dimensions and the dimension specification
5182 * is reset to that of the input.
5184 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5186 struct isl_basic_set
*bset
;
5187 struct isl_set
*set
;
5188 struct isl_map
*map
;
5189 struct isl_dim
*dim
, *orig_dim
= NULL
;
5194 bmap
= isl_basic_map_cow(bmap
);
5198 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5199 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5200 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5201 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5205 orig_dim
= bmap
->dim
;
5207 bset
= (struct isl_basic_set
*)bmap
;
5209 set
= parameter_compute_divs(bset
);
5210 map
= (struct isl_map
*)set
;
5211 map
= isl_map_reset_dim(map
, orig_dim
);
5215 isl_basic_map_free(bmap
);
5219 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5227 off
= isl_dim_total(bmap
->dim
);
5228 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5229 if (isl_int_is_zero(bmap
->div
[i
][0]))
5231 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5237 static int map_divs_known(__isl_keep isl_map
*map
)
5244 for (i
= 0; i
< map
->n
; ++i
) {
5245 int known
= basic_map_divs_known(map
->p
[i
]);
5253 /* If bmap contains any unknown divs, then compute explicit
5254 * expressions for them. However, this computation may be
5255 * quite expensive, so first try to remove divs that aren't
5258 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5262 struct isl_map
*map
;
5264 known
= basic_map_divs_known(bmap
);
5268 return isl_map_from_basic_map(bmap
);
5270 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5272 known
= basic_map_divs_known(bmap
);
5276 return isl_map_from_basic_map(bmap
);
5278 map
= compute_divs(bmap
);
5281 isl_basic_map_free(bmap
);
5285 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5289 struct isl_map
*res
;
5296 known
= map_divs_known(map
);
5304 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5305 for (i
= 1 ; i
< map
->n
; ++i
) {
5307 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5308 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5309 res
= isl_map_union_disjoint(res
, r2
);
5311 res
= isl_map_union(res
, r2
);
5318 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5320 return (struct isl_set
*)
5321 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5324 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5326 return (struct isl_set
*)
5327 isl_map_compute_divs((struct isl_map
*)set
);
5330 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5333 struct isl_set
*set
;
5338 map
= isl_map_cow(map
);
5342 set
= (struct isl_set
*)map
;
5343 set
->dim
= isl_dim_domain(set
->dim
);
5346 for (i
= 0; i
< map
->n
; ++i
) {
5347 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5351 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5352 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5359 struct isl_map
*isl_map_union_disjoint(
5360 struct isl_map
*map1
, struct isl_map
*map2
)
5364 struct isl_map
*map
= NULL
;
5378 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5380 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5381 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5382 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5384 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5385 map1
->n
+ map2
->n
, flags
);
5388 for (i
= 0; i
< map1
->n
; ++i
) {
5389 map
= isl_map_add_basic_map(map
,
5390 isl_basic_map_copy(map1
->p
[i
]));
5394 for (i
= 0; i
< map2
->n
; ++i
) {
5395 map
= isl_map_add_basic_map(map
,
5396 isl_basic_map_copy(map2
->p
[i
]));
5410 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5412 map1
= isl_map_union_disjoint(map1
, map2
);
5416 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5420 struct isl_set
*isl_set_union_disjoint(
5421 struct isl_set
*set1
, struct isl_set
*set2
)
5423 return (struct isl_set
*)
5424 isl_map_union_disjoint(
5425 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5428 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5430 return (struct isl_set
*)
5431 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5434 struct isl_map
*isl_map_intersect_range(
5435 struct isl_map
*map
, struct isl_set
*set
)
5438 struct isl_map
*result
;
5444 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5445 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5446 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5448 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5449 map
->n
* set
->n
, flags
);
5452 for (i
= 0; i
< map
->n
; ++i
)
5453 for (j
= 0; j
< set
->n
; ++j
) {
5454 result
= isl_map_add_basic_map(result
,
5455 isl_basic_map_intersect_range(
5456 isl_basic_map_copy(map
->p
[i
]),
5457 isl_basic_set_copy(set
->p
[j
])));
5470 struct isl_map
*isl_map_intersect_domain(
5471 struct isl_map
*map
, struct isl_set
*set
)
5473 return isl_map_reverse(
5474 isl_map_intersect_range(isl_map_reverse(map
), set
));
5477 struct isl_map
*isl_map_apply_domain(
5478 struct isl_map
*map1
, struct isl_map
*map2
)
5482 map1
= isl_map_reverse(map1
);
5483 map1
= isl_map_apply_range(map1
, map2
);
5484 return isl_map_reverse(map1
);
5491 struct isl_map
*isl_map_apply_range(
5492 struct isl_map
*map1
, struct isl_map
*map2
)
5494 struct isl_dim
*dim_result
;
5495 struct isl_map
*result
;
5501 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5502 isl_dim_copy(map2
->dim
));
5504 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5507 for (i
= 0; i
< map1
->n
; ++i
)
5508 for (j
= 0; j
< map2
->n
; ++j
) {
5509 result
= isl_map_add_basic_map(result
,
5510 isl_basic_map_apply_range(
5511 isl_basic_map_copy(map1
->p
[i
]),
5512 isl_basic_map_copy(map2
->p
[j
])));
5518 if (result
&& result
->n
<= 1)
5519 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5528 * returns range - domain
5530 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5532 isl_dim
*dims
, *target_dim
;
5533 struct isl_basic_set
*bset
;
5540 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5541 bmap
->dim
, isl_dim_out
),
5543 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5544 dim
= isl_basic_map_n_in(bmap
);
5545 nparam
= isl_basic_map_n_param(bmap
);
5546 bset
= isl_basic_set_from_basic_map(bmap
);
5547 bset
= isl_basic_set_cow(bset
);
5548 dims
= isl_basic_set_get_dim(bset
);
5549 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5550 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5551 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5552 for (i
= 0; i
< dim
; ++i
) {
5553 int j
= isl_basic_map_alloc_equality(
5554 (struct isl_basic_map
*)bset
);
5557 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5558 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5559 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5560 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5562 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5563 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5566 isl_basic_map_free(bmap
);
5571 * returns range - domain
5573 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5577 struct isl_set
*result
;
5582 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5583 map
->dim
, isl_dim_out
),
5585 dim
= isl_map_get_dim(map
);
5586 dim
= isl_dim_domain(dim
);
5587 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5590 for (i
= 0; i
< map
->n
; ++i
)
5591 result
= isl_set_add_basic_set(result
,
5592 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5600 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5602 struct isl_basic_map
*bmap
;
5610 nparam
= dims
->nparam
;
5612 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5616 for (i
= 0; i
< dim
; ++i
) {
5617 int j
= isl_basic_map_alloc_equality(bmap
);
5620 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5621 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5622 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5624 return isl_basic_map_finalize(bmap
);
5626 isl_basic_map_free(bmap
);
5630 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5632 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5635 return basic_map_identity(dim
);
5638 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5640 if (!model
|| !model
->dim
)
5642 isl_assert(model
->ctx
,
5643 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5644 return basic_map_identity(isl_dim_copy(model
->dim
));
5647 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5649 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5650 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5653 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5655 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5658 return map_identity(dim
);
5661 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5663 if (!model
|| !model
->dim
)
5665 isl_assert(model
->ctx
,
5666 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5667 return map_identity(isl_dim_copy(model
->dim
));
5670 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5672 if (!model
|| !model
->dim
)
5674 isl_assert(model
->ctx
,
5675 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5676 return map_identity(isl_dim_copy(model
->dim
));
5679 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5681 isl_dim
*dim
= isl_set_get_dim(set
);
5683 id
= isl_map_identity(dim
);
5684 return isl_map_intersect_range(id
, set
);
5687 /* Construct a basic set with all set dimensions having only non-negative
5690 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5695 struct isl_basic_set
*bset
;
5699 nparam
= dims
->nparam
;
5701 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5704 for (i
= 0; i
< dim
; ++i
) {
5705 int k
= isl_basic_set_alloc_inequality(bset
);
5708 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5709 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5713 isl_basic_set_free(bset
);
5717 /* Construct the half-space x_pos >= 0.
5719 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5723 isl_basic_set
*nonneg
;
5725 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5726 k
= isl_basic_set_alloc_inequality(nonneg
);
5729 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5730 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5732 return isl_basic_set_finalize(nonneg
);
5734 isl_basic_set_free(nonneg
);
5738 /* Construct the half-space x_pos <= -1.
5740 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5745 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5746 k
= isl_basic_set_alloc_inequality(neg
);
5749 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5750 isl_int_set_si(neg
->ineq
[k
][0], -1);
5751 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5753 return isl_basic_set_finalize(neg
);
5755 isl_basic_set_free(neg
);
5759 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5760 enum isl_dim_type type
, unsigned first
, unsigned n
)
5763 isl_basic_set
*nonneg
;
5771 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5773 for (i
= 0; i
< n
; ++i
) {
5774 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5775 pos(set
->dim
, type
) + first
+ i
);
5776 neg
= neg_halfspace(isl_set_get_dim(set
),
5777 pos(set
->dim
, type
) + first
+ i
);
5779 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5788 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5789 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5796 if (isl_set_fast_is_empty(set
)) {
5801 return fn(set
, signs
, user
);
5804 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5806 half
= isl_set_intersect(half
, isl_set_copy(set
));
5807 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5811 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5813 half
= isl_set_intersect(half
, set
);
5814 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5820 /* Call "fn" on the intersections of "set" with each of the orthants
5821 * (except for obviously empty intersections). The orthant is identified
5822 * by the signs array, with each entry having value 1 or -1 according
5823 * to the sign of the corresponding variable.
5825 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5826 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5836 if (isl_set_fast_is_empty(set
))
5839 nparam
= isl_set_dim(set
, isl_dim_param
);
5840 nvar
= isl_set_dim(set
, isl_dim_set
);
5842 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5844 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5852 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5854 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5857 int isl_basic_map_is_subset(
5858 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5861 struct isl_map
*map1
;
5862 struct isl_map
*map2
;
5864 if (!bmap1
|| !bmap2
)
5867 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5868 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5870 is_subset
= isl_map_is_subset(map1
, map2
);
5878 int isl_basic_map_is_equal(
5879 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5883 if (!bmap1
|| !bmap2
)
5885 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5888 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5892 int isl_basic_set_is_equal(
5893 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5895 return isl_basic_map_is_equal(
5896 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5899 int isl_map_is_empty(struct isl_map
*map
)
5906 for (i
= 0; i
< map
->n
; ++i
) {
5907 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5916 int isl_map_fast_is_empty(struct isl_map
*map
)
5918 return map
? map
->n
== 0 : -1;
5921 int isl_set_fast_is_empty(struct isl_set
*set
)
5923 return set
? set
->n
== 0 : -1;
5926 int isl_set_is_empty(struct isl_set
*set
)
5928 return isl_map_is_empty((struct isl_map
*)set
);
5931 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5936 return isl_dim_equal(map1
->dim
, map2
->dim
);
5939 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5944 return isl_dim_equal(set1
->dim
, set2
->dim
);
5947 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5953 is_subset
= isl_map_is_subset(map1
, map2
);
5956 is_subset
= isl_map_is_subset(map2
, map1
);
5960 int isl_basic_map_is_strict_subset(
5961 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5965 if (!bmap1
|| !bmap2
)
5967 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5970 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5971 if (is_subset
== -1)
5976 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5982 is_subset
= isl_map_is_subset(map1
, map2
);
5985 is_subset
= isl_map_is_subset(map2
, map1
);
5986 if (is_subset
== -1)
5991 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5993 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5996 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6000 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6003 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6007 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6010 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
6017 for (i
= 0; i
< map
->n
; ++i
) {
6018 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6026 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6028 return isl_map_fast_is_universe((isl_map
*) set
);
6031 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6033 struct isl_basic_set
*bset
= NULL
;
6034 struct isl_vec
*sample
= NULL
;
6041 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6044 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6045 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6046 copy
= isl_basic_map_remove_redundancies(copy
);
6047 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6048 isl_basic_map_free(copy
);
6052 total
= 1 + isl_basic_map_total_dim(bmap
);
6053 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6054 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6060 isl_vec_free(bmap
->sample
);
6061 bmap
->sample
= NULL
;
6062 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6065 sample
= isl_basic_set_sample_vec(bset
);
6068 empty
= sample
->size
== 0;
6069 isl_vec_free(bmap
->sample
);
6070 bmap
->sample
= sample
;
6072 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6077 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6081 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6084 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6088 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6091 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6093 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6096 struct isl_map
*isl_basic_map_union(
6097 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6099 struct isl_map
*map
;
6100 if (!bmap1
|| !bmap2
)
6103 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6105 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6108 map
= isl_map_add_basic_map(map
, bmap1
);
6109 map
= isl_map_add_basic_map(map
, bmap2
);
6112 isl_basic_map_free(bmap1
);
6113 isl_basic_map_free(bmap2
);
6117 struct isl_set
*isl_basic_set_union(
6118 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6120 return (struct isl_set
*)isl_basic_map_union(
6121 (struct isl_basic_map
*)bset1
,
6122 (struct isl_basic_map
*)bset2
);
6125 /* Order divs such that any div only depends on previous divs */
6126 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6134 off
= isl_dim_total(bmap
->dim
);
6136 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6138 if (isl_int_is_zero(bmap
->div
[i
][0]))
6140 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6144 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6150 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6152 return (struct isl_basic_set
*)
6153 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6156 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6163 for (i
= 0; i
< map
->n
; ++i
) {
6164 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6175 /* Look for a div in dst that corresponds to the div "div" in src.
6176 * The divs before "div" in src and dst are assumed to be the same.
6178 * Returns -1 if no corresponding div was found and the position
6179 * of the corresponding div in dst otherwise.
6181 static int find_div(struct isl_basic_map
*dst
,
6182 struct isl_basic_map
*src
, unsigned div
)
6186 unsigned total
= isl_dim_total(src
->dim
);
6188 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6189 for (i
= div
; i
< dst
->n_div
; ++i
)
6190 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6191 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6192 dst
->n_div
- div
) == -1)
6197 struct isl_basic_map
*isl_basic_map_align_divs(
6198 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6201 unsigned total
= isl_dim_total(src
->dim
);
6206 if (src
->n_div
== 0)
6209 for (i
= 0; i
< src
->n_div
; ++i
)
6210 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6212 src
= isl_basic_map_order_divs(src
);
6213 dst
= isl_basic_map_cow(dst
);
6214 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6215 src
->n_div
, 0, 2 * src
->n_div
);
6218 for (i
= 0; i
< src
->n_div
; ++i
) {
6219 int j
= find_div(dst
, src
, i
);
6221 j
= isl_basic_map_alloc_div(dst
);
6224 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6225 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6226 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6230 isl_basic_map_swap_div(dst
, i
, j
);
6234 isl_basic_map_free(dst
);
6238 struct isl_basic_set
*isl_basic_set_align_divs(
6239 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6241 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6242 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6245 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6253 map
= isl_map_compute_divs(map
);
6254 map
= isl_map_cow(map
);
6258 for (i
= 1; i
< map
->n
; ++i
)
6259 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6260 for (i
= 1; i
< map
->n
; ++i
)
6261 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6263 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6267 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6269 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6272 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6276 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6277 map
= isl_map_intersect_domain(map
, set
);
6278 set
= isl_map_range(map
);
6286 /* There is no need to cow as removing empty parts doesn't change
6287 * the meaning of the set.
6289 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6296 for (i
= map
->n
-1; i
>= 0; --i
) {
6297 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6299 isl_basic_map_free(map
->p
[i
]);
6300 if (i
!= map
->n
-1) {
6301 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6302 map
->p
[i
] = map
->p
[map
->n
-1];
6310 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6312 return (struct isl_set
*)
6313 isl_map_remove_empty_parts((struct isl_map
*)set
);
6316 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6318 struct isl_basic_map
*bmap
;
6319 if (!map
|| map
->n
== 0)
6321 bmap
= map
->p
[map
->n
-1];
6322 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6323 return isl_basic_map_copy(bmap
);
6326 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6328 return (struct isl_basic_set
*)
6329 isl_map_copy_basic_map((struct isl_map
*)set
);
6332 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6333 __isl_keep isl_basic_map
*bmap
)
6339 for (i
= map
->n
-1; i
>= 0; --i
) {
6340 if (map
->p
[i
] != bmap
)
6342 map
= isl_map_cow(map
);
6345 isl_basic_map_free(map
->p
[i
]);
6346 if (i
!= map
->n
-1) {
6347 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6348 map
->p
[i
] = map
->p
[map
->n
-1];
6359 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6360 struct isl_basic_set
*bset
)
6362 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6363 (struct isl_basic_map
*)bset
);
6366 /* Given two basic sets bset1 and bset2, compute the maximal difference
6367 * between the values of dimension pos in bset1 and those in bset2
6368 * for any common value of the parameters and dimensions preceding pos.
6370 static enum isl_lp_result
basic_set_maximal_difference_at(
6371 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6372 int pos
, isl_int
*opt
)
6374 struct isl_dim
*dims
;
6375 struct isl_basic_map
*bmap1
= NULL
;
6376 struct isl_basic_map
*bmap2
= NULL
;
6377 struct isl_ctx
*ctx
;
6378 struct isl_vec
*obj
;
6381 unsigned dim1
, dim2
;
6382 enum isl_lp_result res
;
6384 if (!bset1
|| !bset2
)
6385 return isl_lp_error
;
6387 nparam
= isl_basic_set_n_param(bset1
);
6388 dim1
= isl_basic_set_n_dim(bset1
);
6389 dim2
= isl_basic_set_n_dim(bset2
);
6390 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6391 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6392 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6393 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6394 if (!bmap1
|| !bmap2
)
6396 bmap1
= isl_basic_map_cow(bmap1
);
6397 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6398 pos
, (dim1
- pos
) + (dim2
- pos
),
6399 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6400 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6403 total
= isl_basic_map_total_dim(bmap1
);
6405 obj
= isl_vec_alloc(ctx
, 1 + total
);
6406 isl_seq_clr(obj
->block
.data
, 1 + total
);
6407 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6408 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6411 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6413 isl_basic_map_free(bmap1
);
6417 isl_basic_map_free(bmap1
);
6418 isl_basic_map_free(bmap2
);
6419 return isl_lp_error
;
6422 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6423 * for any common value of the parameters and dimensions preceding pos
6424 * in both basic sets, the values of dimension pos in bset1 are
6425 * smaller or larger than those in bset2.
6428 * 1 if bset1 follows bset2
6429 * -1 if bset1 precedes bset2
6430 * 0 if bset1 and bset2 are incomparable
6431 * -2 if some error occurred.
6433 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6434 struct isl_basic_set
*bset2
, int pos
)
6437 enum isl_lp_result res
;
6442 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6444 if (res
== isl_lp_empty
)
6446 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6447 res
== isl_lp_unbounded
)
6449 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6458 /* Given two basic sets bset1 and bset2, check whether
6459 * for any common value of the parameters and dimensions preceding pos
6460 * there is a value of dimension pos in bset1 that is larger
6461 * than a value of the same dimension in bset2.
6464 * 1 if there exists such a pair
6465 * 0 if there is no such pair, but there is a pair of equal values
6467 * -2 if some error occurred.
6469 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6470 __isl_keep isl_basic_set
*bset2
, int pos
)
6473 enum isl_lp_result res
;
6478 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6480 if (res
== isl_lp_empty
)
6482 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6483 res
== isl_lp_unbounded
)
6485 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6487 else if (res
== isl_lp_ok
)
6496 /* Given two sets set1 and set2, check whether
6497 * for any common value of the parameters and dimensions preceding pos
6498 * there is a value of dimension pos in set1 that is larger
6499 * than a value of the same dimension in set2.
6502 * 1 if there exists such a pair
6503 * 0 if there is no such pair, but there is a pair of equal values
6505 * -2 if some error occurred.
6507 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6508 __isl_keep isl_set
*set2
, int pos
)
6516 for (i
= 0; i
< set1
->n
; ++i
)
6517 for (j
= 0; j
< set2
->n
; ++j
) {
6519 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6520 if (f
== 1 || f
== -2)
6529 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6530 unsigned pos
, isl_int
*val
)
6538 total
= isl_basic_map_total_dim(bmap
);
6539 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6540 for (; d
+1 > pos
; --d
)
6541 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6545 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6547 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6549 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6552 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6558 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6559 unsigned pos
, isl_int
*val
)
6571 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6574 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6575 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6576 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6577 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6581 isl_int_set(*val
, v
);
6587 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6588 unsigned pos
, isl_int
*val
)
6590 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6594 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6597 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6600 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6601 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6603 if (pos
>= isl_basic_map_dim(bmap
, type
))
6605 return isl_basic_map_fast_has_fixed_var(bmap
,
6606 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6609 int isl_map_fast_is_fixed(struct isl_map
*map
,
6610 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6612 if (pos
>= isl_map_dim(map
, type
))
6614 return isl_map_fast_has_fixed_var(map
,
6615 map_offset(map
, type
) - 1 + pos
, val
);
6618 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6619 * then return this fixed value in *val.
6621 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6624 return isl_basic_set_fast_has_fixed_var(bset
,
6625 isl_basic_set_n_param(bset
) + dim
, val
);
6628 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6629 * then return this fixed value in *val.
6631 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6633 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6636 /* Check if input variable in has fixed value and if so and if val is not NULL,
6637 * then return this fixed value in *val.
6639 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6641 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6644 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6645 * and if val is not NULL, then return this lower bound in *val.
6647 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6648 unsigned dim
, isl_int
*val
)
6650 int i
, i_eq
= -1, i_ineq
= -1;
6657 total
= isl_basic_set_total_dim(bset
);
6658 nparam
= isl_basic_set_n_param(bset
);
6659 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6660 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6666 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6667 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6669 if (i_eq
!= -1 || i_ineq
!= -1)
6673 if (i_eq
== -1 && i_ineq
== -1)
6675 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6676 /* The coefficient should always be one due to normalization. */
6677 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6679 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6681 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6682 total
- nparam
- dim
- 1) != -1)
6685 isl_int_neg(*val
, c
[0]);
6689 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6690 unsigned dim
, isl_int
*val
)
6702 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6706 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6708 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6709 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6711 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6715 isl_int_set(*val
, v
);
6726 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6728 const struct constraint
*c1
= (const struct constraint
*)p1
;
6729 const struct constraint
*c2
= (const struct constraint
*)p2
;
6730 unsigned size
= isl_min(c1
->size
, c2
->size
);
6731 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6734 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6735 struct isl_basic_map
*bmap
)
6738 struct constraint
*c
;
6743 total
= isl_basic_map_total_dim(bmap
);
6744 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6747 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6749 c
[i
].c
= bmap
->ineq
[i
];
6751 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6752 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6753 bmap
->ineq
[i
] = c
[i
].c
;
6757 isl_basic_map_free(bmap
);
6761 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6762 __isl_take isl_basic_set
*bset
)
6764 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6765 (struct isl_basic_map
*)bset
);
6768 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6772 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6774 bmap
= isl_basic_map_remove_redundancies(bmap
);
6775 bmap
= isl_basic_map_sort_constraints(bmap
);
6776 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6780 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6782 return (struct isl_basic_set
*)isl_basic_map_normalize(
6783 (struct isl_basic_map
*)bset
);
6786 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6787 const __isl_keep isl_basic_map
*bmap2
)
6794 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
6795 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
6796 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
6797 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6798 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6799 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6800 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6801 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6802 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6803 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6804 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6806 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6808 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6810 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6811 return bmap1
->n_eq
- bmap2
->n_eq
;
6812 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6813 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6814 if (bmap1
->n_div
!= bmap2
->n_div
)
6815 return bmap1
->n_div
- bmap2
->n_div
;
6816 total
= isl_basic_map_total_dim(bmap1
);
6817 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6818 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6822 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6823 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6827 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6828 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6835 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6836 __isl_keep isl_basic_map
*bmap2
)
6838 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6841 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6842 __isl_keep isl_basic_set
*bset2
)
6844 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6845 (isl_basic_map
*)bset2
);
6848 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6850 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6851 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6853 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6856 /* We normalize in place, but if anything goes wrong we need
6857 * to return NULL, so we need to make sure we don't change the
6858 * meaning of any possible other copies of map.
6860 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6863 struct isl_basic_map
*bmap
;
6867 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6869 for (i
= 0; i
< map
->n
; ++i
) {
6870 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6873 isl_basic_map_free(map
->p
[i
]);
6876 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6877 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6878 map
= isl_map_remove_empty_parts(map
);
6881 for (i
= map
->n
- 1; i
>= 1; --i
) {
6882 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6884 isl_basic_map_free(map
->p
[i
-1]);
6885 for (j
= i
; j
< map
->n
; ++j
)
6886 map
->p
[j
-1] = map
->p
[j
];
6896 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6898 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6901 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6911 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6914 map1
= isl_map_copy(map1
);
6915 map2
= isl_map_copy(map2
);
6916 map1
= isl_map_normalize(map1
);
6917 map2
= isl_map_normalize(map2
);
6920 equal
= map1
->n
== map2
->n
;
6921 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6922 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6935 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6937 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6938 (struct isl_map
*)set2
);
6941 /* Return an interval that ranges from min to max (inclusive)
6943 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6944 isl_int min
, isl_int max
)
6947 struct isl_basic_set
*bset
= NULL
;
6949 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6953 k
= isl_basic_set_alloc_inequality(bset
);
6956 isl_int_set_si(bset
->ineq
[k
][1], 1);
6957 isl_int_neg(bset
->ineq
[k
][0], min
);
6959 k
= isl_basic_set_alloc_inequality(bset
);
6962 isl_int_set_si(bset
->ineq
[k
][1], -1);
6963 isl_int_set(bset
->ineq
[k
][0], max
);
6967 isl_basic_set_free(bset
);
6971 /* Return the Cartesian product of the basic sets in list (in the given order).
6973 __isl_give isl_basic_set
*isl_basic_set_list_product(
6974 __isl_take
struct isl_basic_set_list
*list
)
6982 struct isl_basic_set
*product
= NULL
;
6986 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6987 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6988 nparam
= isl_basic_set_n_param(list
->p
[0]);
6989 dim
= isl_basic_set_n_dim(list
->p
[0]);
6990 extra
= list
->p
[0]->n_div
;
6991 n_eq
= list
->p
[0]->n_eq
;
6992 n_ineq
= list
->p
[0]->n_ineq
;
6993 for (i
= 1; i
< list
->n
; ++i
) {
6994 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6995 isl_assert(list
->ctx
,
6996 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6997 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6998 extra
+= list
->p
[i
]->n_div
;
6999 n_eq
+= list
->p
[i
]->n_eq
;
7000 n_ineq
+= list
->p
[i
]->n_ineq
;
7002 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7007 for (i
= 0; i
< list
->n
; ++i
) {
7008 isl_basic_set_add_constraints(product
,
7009 isl_basic_set_copy(list
->p
[i
]), dim
);
7010 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7012 isl_basic_set_list_free(list
);
7015 isl_basic_set_free(product
);
7016 isl_basic_set_list_free(list
);
7020 struct isl_basic_map
*isl_basic_map_product(
7021 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7023 struct isl_dim
*dim_result
= NULL
;
7024 struct isl_basic_map
*bmap
;
7025 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7026 struct isl_dim_map
*dim_map1
, *dim_map2
;
7028 if (!bmap1
|| !bmap2
)
7031 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7032 bmap2
->dim
, isl_dim_param
), goto error
);
7033 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7034 isl_dim_copy(bmap2
->dim
));
7036 in1
= isl_basic_map_n_in(bmap1
);
7037 in2
= isl_basic_map_n_in(bmap2
);
7038 out1
= isl_basic_map_n_out(bmap1
);
7039 out2
= isl_basic_map_n_out(bmap2
);
7040 nparam
= isl_basic_map_n_param(bmap1
);
7042 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7043 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7044 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7045 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7046 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7047 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7048 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7049 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7050 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7051 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7052 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7054 bmap
= isl_basic_map_alloc_dim(dim_result
,
7055 bmap1
->n_div
+ bmap2
->n_div
,
7056 bmap1
->n_eq
+ bmap2
->n_eq
,
7057 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7058 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7059 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7060 bmap
= isl_basic_map_simplify(bmap
);
7061 return isl_basic_map_finalize(bmap
);
7063 isl_basic_map_free(bmap1
);
7064 isl_basic_map_free(bmap2
);
7068 __isl_give isl_basic_map
*isl_basic_map_range_product(
7069 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7071 isl_dim
*dim_result
= NULL
;
7072 isl_basic_map
*bmap
;
7073 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7074 struct isl_dim_map
*dim_map1
, *dim_map2
;
7076 if (!bmap1
|| !bmap2
)
7079 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7080 isl_dim_copy(bmap2
->dim
));
7082 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7083 out1
= isl_basic_map_n_out(bmap1
);
7084 out2
= isl_basic_map_n_out(bmap2
);
7085 nparam
= isl_basic_map_n_param(bmap1
);
7087 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7088 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7089 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7090 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7091 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7092 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7093 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7094 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7095 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7096 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7097 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7099 bmap
= isl_basic_map_alloc_dim(dim_result
,
7100 bmap1
->n_div
+ bmap2
->n_div
,
7101 bmap1
->n_eq
+ bmap2
->n_eq
,
7102 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7103 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7104 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7105 bmap
= isl_basic_map_simplify(bmap
);
7106 return isl_basic_map_finalize(bmap
);
7108 isl_basic_map_free(bmap1
);
7109 isl_basic_map_free(bmap2
);
7113 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7114 __isl_take isl_map
*map2
,
7115 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7116 __isl_take isl_dim
*right
),
7117 __isl_give isl_basic_map
*(*basic_map_product
)(
7118 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7121 struct isl_map
*result
;
7127 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7128 map2
->dim
, isl_dim_param
), goto error
);
7130 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7131 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7132 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7134 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7135 isl_dim_copy(map2
->dim
)),
7136 map1
->n
* map2
->n
, flags
);
7139 for (i
= 0; i
< map1
->n
; ++i
)
7140 for (j
= 0; j
< map2
->n
; ++j
) {
7141 struct isl_basic_map
*part
;
7142 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7143 isl_basic_map_copy(map2
->p
[j
]));
7144 if (isl_basic_map_is_empty(part
))
7145 isl_basic_map_free(part
);
7147 result
= isl_map_add_basic_map(result
, part
);
7160 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7162 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7164 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7167 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7169 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7170 __isl_take isl_map
*map2
)
7174 prod
= isl_map_product(map1
, map2
);
7175 prod
= isl_map_flatten(prod
);
7179 /* Given two set A and B, construct its Cartesian product A x B.
7181 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7183 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7184 (struct isl_map
*)set2
);
7187 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7188 __isl_take isl_set
*set2
)
7190 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7193 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7195 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7196 __isl_take isl_map
*map2
)
7198 return map_product(map1
, map2
, &isl_dim_range_product
,
7199 &isl_basic_map_range_product
);
7202 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7205 uint32_t hash
= isl_hash_init();
7210 bmap
= isl_basic_map_copy(bmap
);
7211 bmap
= isl_basic_map_normalize(bmap
);
7214 total
= isl_basic_map_total_dim(bmap
);
7215 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7218 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7219 isl_hash_hash(hash
, c_hash
);
7221 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7222 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7224 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7225 isl_hash_hash(hash
, c_hash
);
7227 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7228 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7230 if (isl_int_is_zero(bmap
->div
[i
][0]))
7232 isl_hash_byte(hash
, i
& 0xFF);
7233 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7234 isl_hash_hash(hash
, c_hash
);
7236 isl_basic_map_free(bmap
);
7240 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7242 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7245 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7252 map
= isl_map_copy(map
);
7253 map
= isl_map_normalize(map
);
7257 hash
= isl_hash_init();
7258 for (i
= 0; i
< map
->n
; ++i
) {
7260 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7261 isl_hash_hash(hash
, bmap_hash
);
7269 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7271 return isl_map_get_hash((isl_map
*)set
);
7274 /* Check if the value for dimension dim is completely determined
7275 * by the values of the other parameters and variables.
7276 * That is, check if dimension dim is involved in an equality.
7278 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7285 nparam
= isl_basic_set_n_param(bset
);
7286 for (i
= 0; i
< bset
->n_eq
; ++i
)
7287 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7292 /* Check if the value for dimension dim is completely determined
7293 * by the values of the other parameters and variables.
7294 * That is, check if dimension dim is involved in an equality
7295 * for each of the subsets.
7297 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7303 for (i
= 0; i
< set
->n
; ++i
) {
7305 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7312 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7314 return set
? set
->n
: 0;
7317 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7318 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7325 for (i
= 0; i
< map
->n
; ++i
)
7326 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7332 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7333 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7340 for (i
= 0; i
< set
->n
; ++i
)
7341 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7347 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7349 struct isl_dim
*dim
;
7354 if (bset
->n_div
== 0)
7357 bset
= isl_basic_set_cow(bset
);
7361 dim
= isl_basic_set_get_dim(bset
);
7362 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
7365 isl_dim_free(bset
->dim
);
7367 bset
->extra
-= bset
->n_div
;
7370 bset
= isl_basic_set_finalize(bset
);
7374 isl_basic_set_free(bset
);
7378 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7381 struct isl_dim
*dim
;
7384 set
= isl_set_align_divs(set
);
7388 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
7391 set
= isl_set_cow(set
);
7395 n_div
= set
->p
[0]->n_div
;
7396 dim
= isl_set_get_dim(set
);
7397 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
7400 isl_dim_free(set
->dim
);
7403 for (i
= 0; i
< set
->n
; ++i
) {
7404 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7415 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7417 struct isl_dim
*dim
;
7418 struct isl_basic_map
*bmap
;
7425 set
= isl_set_align_divs(set
);
7430 dim
= isl_set_get_dim(set
);
7431 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7433 return isl_map_identity(dim
);
7436 n_div
= set
->p
[0]->n_div
;
7437 dim
= isl_dim_map(dim
);
7438 n_param
= isl_dim_size(dim
, isl_dim_param
);
7439 n_set
= isl_dim_size(dim
, isl_dim_in
);
7440 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7441 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7442 for (i
= 0; i
< n_set
; ++i
)
7443 bmap
= var_equal(bmap
, i
);
7445 total
= n_param
+ n_set
+ n_set
+ n_div
;
7446 for (i
= 0; i
< n_div
; ++i
) {
7447 k
= isl_basic_map_alloc_inequality(bmap
);
7450 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7451 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7452 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7453 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7454 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7455 set
->p
[0]->div
[i
][0]);
7457 l
= isl_basic_map_alloc_inequality(bmap
);
7460 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7461 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7462 set
->p
[0]->div
[i
][0]);
7463 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7467 return isl_map_from_basic_map(bmap
);
7470 isl_basic_map_free(bmap
);
7474 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7482 dim
= isl_basic_set_total_dim(bset
);
7483 size
+= bset
->n_eq
* (1 + dim
);
7484 size
+= bset
->n_ineq
* (1 + dim
);
7485 size
+= bset
->n_div
* (2 + dim
);
7490 int isl_set_size(__isl_keep isl_set
*set
)
7498 for (i
= 0; i
< set
->n
; ++i
)
7499 size
+= isl_basic_set_size(set
->p
[i
]);
7504 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7505 enum isl_dim_type type
, unsigned pos
)
7513 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7515 pos
+= isl_basic_map_offset(bmap
, type
);
7517 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7518 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7522 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7523 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7530 return lower
&& upper
;
7533 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7534 enum isl_dim_type type
, unsigned pos
)
7541 for (i
= 0; i
< map
->n
; ++i
) {
7543 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7544 if (bounded
< 0 || !bounded
)
7551 /* Return 1 if the specified dim is involved in both an upper bound
7552 * and a lower bound.
7554 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7555 enum isl_dim_type type
, unsigned pos
)
7557 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7560 /* For each of the "n" variables starting at "first", determine
7561 * the sign of the variable and put the results in the first "n"
7562 * elements of the array "signs".
7564 * 1 means that the variable is non-negative
7565 * -1 means that the variable is non-positive
7566 * 0 means the variable attains both positive and negative values.
7568 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7569 unsigned first
, unsigned n
, int *signs
)
7571 isl_vec
*bound
= NULL
;
7572 struct isl_tab
*tab
= NULL
;
7573 struct isl_tab_undo
*snap
;
7576 if (!bset
|| !signs
)
7579 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7580 tab
= isl_tab_from_basic_set(bset
);
7584 isl_seq_clr(bound
->el
, bound
->size
);
7585 isl_int_set_si(bound
->el
[0], -1);
7587 snap
= isl_tab_snap(tab
);
7588 for (i
= 0; i
< n
; ++i
) {
7591 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7592 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7595 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7596 if (isl_tab_rollback(tab
, snap
) < 0)
7604 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7605 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7608 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7609 if (isl_tab_rollback(tab
, snap
) < 0)
7612 signs
[i
] = empty
? -1 : 0;
7616 isl_vec_free(bound
);
7620 isl_vec_free(bound
);
7624 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7625 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7627 if (!bset
|| !signs
)
7629 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7632 first
+= pos(bset
->dim
, type
) - 1;
7633 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7636 /* Check if the given basic map is obviously single-valued.
7637 * In particular, for each output dimension, check that there is
7638 * an equality that defines the output dimension in terms of
7639 * earlier dimensions.
7641 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7651 total
= 1 + isl_basic_map_total_dim(bmap
);
7652 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7653 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7655 for (i
= 0; i
< n_out
; ++i
) {
7656 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7657 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7659 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7660 total
- (o_out
+ i
+ 1)) == -1)
7663 if (j
>= bmap
->n_eq
)
7670 /* Check if the given map is obviously single-valued.
7672 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7683 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7686 /* Check if the given map is single-valued.
7691 * and check if the result is a subset of the identity mapping.
7693 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7699 sv
= isl_map_fast_is_single_valued(map
);
7703 test
= isl_map_reverse(isl_map_copy(map
));
7704 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7706 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
7708 sv
= isl_map_is_subset(test
, id
);
7716 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7720 sv
= isl_map_is_single_valued(map
);
7724 map
= isl_map_copy(map
);
7725 map
= isl_map_reverse(map
);
7726 sv
= isl_map_is_single_valued(map
);
7732 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7734 return isl_map_is_single_valued((isl_map
*)set
);
7737 int isl_map_is_translation(__isl_keep isl_map
*map
)
7742 delta
= isl_map_deltas(isl_map_copy(map
));
7743 ok
= isl_set_is_singleton(delta
);
7744 isl_set_free(delta
);
7749 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7751 if (isl_seq_first_non_zero(p
, pos
) != -1)
7753 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7758 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7767 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7770 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7771 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7772 for (j
= 0; j
< nvar
; ++j
) {
7773 int lower
= 0, upper
= 0;
7774 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7775 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7777 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7783 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7784 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7786 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7788 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7793 if (!lower
|| !upper
)
7800 int isl_set_is_box(__isl_keep isl_set
*set
)
7807 return isl_basic_set_is_box(set
->p
[0]);
7810 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7815 return isl_dim_is_wrapping(bset
->dim
);
7818 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7823 return isl_dim_is_wrapping(set
->dim
);
7826 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7828 bmap
= isl_basic_map_cow(bmap
);
7832 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7836 bmap
= isl_basic_map_finalize(bmap
);
7838 return (isl_basic_set
*)bmap
;
7840 isl_basic_map_free(bmap
);
7844 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7848 map
= isl_map_cow(map
);
7852 for (i
= 0; i
< map
->n
; ++i
) {
7853 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7857 map
->dim
= isl_dim_wrap(map
->dim
);
7861 return (isl_set
*)map
;
7867 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7869 bset
= isl_basic_set_cow(bset
);
7873 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7877 bset
= isl_basic_set_finalize(bset
);
7879 return (isl_basic_map
*)bset
;
7881 isl_basic_set_free(bset
);
7885 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7892 if (!isl_set_is_wrapping(set
))
7893 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7896 set
= isl_set_cow(set
);
7900 for (i
= 0; i
< set
->n
; ++i
) {
7901 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7906 set
->dim
= isl_dim_unwrap(set
->dim
);
7910 return (isl_map
*)set
;
7916 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7917 enum isl_dim_type type
)
7922 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7925 bmap
= isl_basic_map_cow(bmap
);
7929 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7933 bmap
= isl_basic_map_finalize(bmap
);
7937 isl_basic_map_free(bmap
);
7941 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7942 enum isl_dim_type type
)
7949 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7952 map
= isl_map_cow(map
);
7956 for (i
= 0; i
< map
->n
; ++i
) {
7957 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7961 map
->dim
= isl_dim_reset(map
->dim
, type
);
7971 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7976 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7979 bmap
= isl_basic_map_cow(bmap
);
7983 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
7987 bmap
= isl_basic_map_finalize(bmap
);
7991 isl_basic_map_free(bmap
);
7995 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
7997 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8000 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8007 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8010 map
= isl_map_cow(map
);
8014 for (i
= 0; i
< map
->n
; ++i
) {
8015 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8019 map
->dim
= isl_dim_flatten(map
->dim
);
8029 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8031 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8034 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8036 isl_dim
*dim
, *flat_dim
;
8039 dim
= isl_set_get_dim(set
);
8040 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8041 map
= map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8042 map
= isl_map_intersect_domain(map
, set
);
8047 /* Extend the given dim_map with mappings for the divs in bmap.
8049 static __isl_give
struct isl_dim_map
*extend_dim_map(
8050 __isl_keep
struct isl_dim_map
*dim_map
,
8051 __isl_keep isl_basic_map
*bmap
)
8054 struct isl_dim_map
*res
;
8057 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
8059 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
8063 for (i
= 0; i
< dim_map
->len
; ++i
)
8064 res
->pos
[i
] = dim_map
->pos
[i
];
8065 for (i
= 0; i
< bmap
->n_div
; ++i
)
8066 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
8071 /* Extract a dim_map from a reordering.
8072 * We essentially need to reverse the mapping, and add an offset
8073 * of 1 for the constant term.
8075 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
8076 __isl_keep isl_reordering
*exp
)
8079 struct isl_dim_map
*dim_map
;
8084 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
8088 for (i
= 0; i
< exp
->len
; ++i
)
8089 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
8094 /* Reorder the dimensions of "bmap" according to the given dim_map
8095 * and set the dimension specification to "dim".
8097 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8098 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8102 bmap
= isl_basic_map_cow(bmap
);
8103 if (!bmap
|| !dim
|| !dim_map
)
8106 res
= isl_basic_map_alloc_dim(dim
,
8107 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8108 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
8109 res
= isl_basic_map_finalize(res
);
8113 isl_basic_map_free(bmap
);
8118 /* Reorder the dimensions of "map" according to given reordering.
8120 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8121 __isl_take isl_reordering
*r
)
8124 struct isl_dim_map
*dim_map
;
8126 map
= isl_map_cow(map
);
8127 dim_map
= isl_dim_map_from_reordering(r
);
8128 if (!map
|| !r
|| !dim_map
)
8131 for (i
= 0; i
< map
->n
; ++i
) {
8132 struct isl_dim_map
*dim_map_i
;
8134 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
8136 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8137 isl_dim_copy(r
->dim
), dim_map_i
);
8143 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8145 isl_reordering_free(r
);
8151 isl_reordering_free(r
);
8155 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8156 __isl_take isl_reordering
*r
)
8158 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8161 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8162 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8163 enum isl_dim_type c2
, enum isl_dim_type c3
,
8164 enum isl_dim_type c4
, enum isl_dim_type c5
)
8166 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8167 struct isl_mat
*mat
;
8173 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8174 isl_basic_map_total_dim(bmap
) + 1);
8177 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8178 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8179 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8180 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8181 isl_int_set(mat
->row
[i
][pos
],
8182 bmap
->eq
[i
][off
+ k
]);
8190 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8191 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8192 enum isl_dim_type c2
, enum isl_dim_type c3
,
8193 enum isl_dim_type c4
, enum isl_dim_type c5
)
8195 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8196 struct isl_mat
*mat
;
8202 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8203 isl_basic_map_total_dim(bmap
) + 1);
8206 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8207 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8208 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8209 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8210 isl_int_set(mat
->row
[i
][pos
],
8211 bmap
->ineq
[i
][off
+ k
]);
8219 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8220 __isl_take isl_dim
*dim
,
8221 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8222 enum isl_dim_type c2
, enum isl_dim_type c3
,
8223 enum isl_dim_type c4
, enum isl_dim_type c5
)
8225 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8226 isl_basic_map
*bmap
;
8232 if (!dim
|| !eq
|| !ineq
)
8235 if (eq
->n_col
!= ineq
->n_col
)
8236 isl_die(dim
->ctx
, isl_error_invalid
,
8237 "equalities and inequalities matrices should have "
8238 "same number of columns", goto error
);
8240 total
= 1 + isl_dim_total(dim
);
8242 if (eq
->n_col
< total
)
8243 isl_die(dim
->ctx
, isl_error_invalid
,
8244 "number of columns too small", goto error
);
8246 extra
= eq
->n_col
- total
;
8248 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8249 eq
->n_row
, ineq
->n_row
);
8252 for (i
= 0; i
< extra
; ++i
) {
8253 k
= isl_basic_map_alloc_div(bmap
);
8256 isl_int_set_si(bmap
->div
[k
][0], 0);
8258 for (i
= 0; i
< eq
->n_row
; ++i
) {
8259 l
= isl_basic_map_alloc_equality(bmap
);
8262 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8263 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8264 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8265 isl_int_set(bmap
->eq
[l
][off
+ k
],
8271 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8272 l
= isl_basic_map_alloc_inequality(bmap
);
8275 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8276 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8277 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8278 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8297 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8298 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8299 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8301 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8302 c1
, c2
, c3
, c4
, isl_dim_in
);
8305 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8306 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8307 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8309 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8310 c1
, c2
, c3
, c4
, isl_dim_in
);
8313 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8314 __isl_take isl_dim
*dim
,
8315 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8316 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8318 return (isl_basic_set
*)
8319 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8320 c1
, c2
, c3
, c4
, isl_dim_in
);